Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xindoo
redis
提交
f8ea19e5
R
redis
项目概览
xindoo
/
redis
通知
2
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f8ea19e5
编写于
4月 02, 2012
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
DUMP/RESTORE now use CRC64 instead of truncated SHA1.
上级
9510d65d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
22 addition
and
24 deletion
+22
-24
src/cluster.c
src/cluster.c
+22
-24
未找到文件。
src/cluster.c
浏览文件 @
f8ea19e5
#include "redis.h"
#include "
sha1
.h"
#include "
endianconv
.h"
#include <arpa/inet.h>
#include <fcntl.h>
...
...
@@ -1464,8 +1464,8 @@ void clusterCommand(redisClient *c) {
/* Generates a DUMP-format representation of the object 'o', adding it to the
* io stream pointed by 'rio'. This function can't fail. */
void
createDumpPayload
(
rio
*
payload
,
robj
*
o
)
{
unsigned
char
hash
[
20
],
buf
[
2
];
SHA1_CTX
ctx
;
unsigned
char
buf
[
2
];
uint64_t
crc
;
/* Serialize the object in a RDB-like format. It consist of an object type
* byte followed by the serialized object. This is understood by RESTORE. */
...
...
@@ -1474,35 +1474,34 @@ void createDumpPayload(rio *payload, robj *o) {
redisAssert
(
rdbSaveObject
(
payload
,
o
));
/* Write the footer, this is how it looks like:
* ----------------+---------------------+--------------+
* ... RDB payload | 2 bytes RDB version | 8 bytes
SHA1
|
* ----------------+---------------------+--------------+
*
The SHA1 is just 8 bytes of truncated SHA1 of everything excluding itself
.
*
The 2 bytes RDB version is a little endian unsigned integer. *
/
* ----------------+---------------------+--------------
-
+
* ... RDB payload | 2 bytes RDB version | 8 bytes
CRC64
|
* ----------------+---------------------+--------------
-
+
*
RDB version and CRC are both in little endian
.
*/
/* RDB version */
buf
[
0
]
=
REDIS_RDB_VERSION
&
0xff
;
buf
[
1
]
=
(
REDIS_RDB_VERSION
>>
8
)
&
0xff
;
payload
->
io
.
buffer
.
ptr
=
sdscatlen
(
payload
->
io
.
buffer
.
ptr
,
buf
,
2
);
/* Truncated SHA1 */
SHA1Init
(
&
ctx
);
SHA1Update
(
&
ctx
,(
unsigned
char
*
)
payload
->
io
.
buffer
.
ptr
,
sdslen
(
payload
->
io
.
buffer
.
ptr
));
SHA1Final
(
hash
,
&
ctx
);
payload
->
io
.
buffer
.
ptr
=
sdscatlen
(
payload
->
io
.
buffer
.
ptr
,
hash
,
8
);
/* CRC64 */
crc
=
crc64
((
unsigned
char
*
)
payload
->
io
.
buffer
.
ptr
,
sdslen
(
payload
->
io
.
buffer
.
ptr
));
memrev64ifbe
(
&
crc
);
payload
->
io
.
buffer
.
ptr
=
sdscatlen
(
payload
->
io
.
buffer
.
ptr
,
&
crc
,
8
);
}
/* Verify that the RDB version of the dump payload matches the one of this Redis
* instance and that the
truncated SHA1
is ok.
* instance and that the
checksum
is ok.
* If the DUMP payload looks valid REDIS_OK is returned, otherwise REDIS_ERR
* is returned. */
int
verifyDumpPayload
(
unsigned
char
*
p
,
size_t
len
)
{
unsigned
char
hash
[
20
],
*
footer
;
SHA1_CTX
ctx
;
unsigned
char
*
footer
;
uint16_t
rdbver
;
uint64_t
crc
;
/* At least 2 bytes of RDB version and 8 of
truncated SHA
should be present. */
/* At least 2 bytes of RDB version and 8 of
CRC64
should be present. */
if
(
len
<
10
)
return
REDIS_ERR
;
footer
=
p
+
(
len
-
10
);
...
...
@@ -1510,11 +1509,10 @@ int verifyDumpPayload(unsigned char *p, size_t len) {
rdbver
=
(
footer
[
1
]
<<
8
)
|
footer
[
0
];
if
(
rdbver
!=
REDIS_RDB_VERSION
)
return
REDIS_ERR
;
/* Verify truncated SHA1 */
SHA1Init
(
&
ctx
);
SHA1Update
(
&
ctx
,
p
,
len
-
8
);
SHA1Final
(
hash
,
&
ctx
);
return
(
memcmp
(
hash
,
footer
+
2
,
8
)
==
0
)
?
REDIS_OK
:
REDIS_ERR
;
/* Verify CRC64 */
crc
=
crc64
(
p
,
len
-
8
);
memrev64ifbe
(
&
crc
);
return
(
memcmp
(
&
crc
,
footer
+
2
,
8
)
==
0
)
?
REDIS_OK
:
REDIS_ERR
;
}
/* DUMP keyname
...
...
@@ -1561,7 +1559,7 @@ void restoreCommand(redisClient *c) {
return
;
}
/* Verify
truncated SHA1 and RDB version
. */
/* Verify
RDB version and data checksum
. */
if
(
verifyDumpPayload
(
c
->
argv
[
3
]
->
ptr
,
sdslen
(
c
->
argv
[
3
]
->
ptr
))
==
REDIS_ERR
)
{
addReplyError
(
c
,
"DUMP payload version or checksum are wrong"
);
return
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录