Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc12e43a

Browse files
committed
Add checksum helper functions.
These functions make it easier to write code that wants to compute achecksum for some data while allowing the user to configure the typeof checksum that gets used.This is another piece of infrastructure for the upcoming patch to addbackup manifests.Patch written from scratch by me, but it is similar to previous workby Rushabh Lathia and Suraj Kharage. Suraj also reviewed this versionoff-list. Advice on how not to break Windows from Davinder Singh.Discussion:http://postgr.es/m/CA+TgmoZV8dw1H2bzZ9xkKwdrk8+XYa+DC9H=F7heO2zna5T6qg@mail.gmail.comDiscussion:http://postgr.es/m/CA+TgmoZRTBiPyvQEwV79PU1ePTtSEo2UeVncrkJMbn1sU1gnRA@mail.gmail.com
1 parent6dd9f35 commitc12e43a

File tree

4 files changed

+267
-2
lines changed

4 files changed

+267
-2
lines changed

‎src/common/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ LIBS += $(PTHREAD_LIBS)
4848
OBJS_COMMON =\
4949
archive.o\
5050
base64.o\
51+
checksum_helper.o\
5152
config_info.o\
5253
controldata_utils.o\
5354
d2s.o\

‎src/common/checksum_helper.c

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* checksum_helper.c
4+
* Compute a checksum of any of various types using common routines
5+
*
6+
* Portions Copyright (c) 2016-2020, PostgreSQL Global Development Group
7+
*
8+
* IDENTIFICATION
9+
* src/common/checksum_helper.c
10+
*
11+
*-------------------------------------------------------------------------
12+
*/
13+
14+
#ifndefFRONTEND
15+
#include"postgres.h"
16+
#else
17+
#include"postgres_fe.h"
18+
#endif
19+
20+
#include"common/checksum_helper.h"
21+
22+
/*
23+
* If 'name' is a recognized checksum type, set *type to the corresponding
24+
* constant and return true. Otherwise, set *type to CHECKSUM_TYPE_NONE and
25+
* return false.
26+
*/
27+
bool
28+
pg_checksum_parse_type(char*name,pg_checksum_type*type)
29+
{
30+
pg_checksum_typeresult_type=CHECKSUM_TYPE_NONE;
31+
boolresult= true;
32+
33+
if (pg_strcasecmp(name,"none")==0)
34+
result_type=CHECKSUM_TYPE_NONE;
35+
elseif (pg_strcasecmp(name,"crc32c")==0)
36+
result_type=CHECKSUM_TYPE_CRC32C;
37+
elseif (pg_strcasecmp(name,"sha224")==0)
38+
result_type=CHECKSUM_TYPE_SHA224;
39+
elseif (pg_strcasecmp(name,"sha256")==0)
40+
result_type=CHECKSUM_TYPE_SHA256;
41+
elseif (pg_strcasecmp(name,"sha384")==0)
42+
result_type=CHECKSUM_TYPE_SHA384;
43+
elseif (pg_strcasecmp(name,"sha512")==0)
44+
result_type=CHECKSUM_TYPE_SHA512;
45+
else
46+
result= false;
47+
48+
*type=result_type;
49+
returnresult;
50+
}
51+
52+
/*
53+
* Get the canonical human-readable name corresponding to a checksum type.
54+
*/
55+
char*
56+
pg_checksum_type_name(pg_checksum_typetype)
57+
{
58+
switch (type)
59+
{
60+
caseCHECKSUM_TYPE_NONE:
61+
return"NONE";
62+
caseCHECKSUM_TYPE_CRC32C:
63+
return"CRC32C";
64+
caseCHECKSUM_TYPE_SHA224:
65+
return"SHA224";
66+
caseCHECKSUM_TYPE_SHA256:
67+
return"SHA256";
68+
caseCHECKSUM_TYPE_SHA384:
69+
return"SHA384";
70+
caseCHECKSUM_TYPE_SHA512:
71+
return"SHA512";
72+
}
73+
74+
Assert(false);
75+
return"???";
76+
}
77+
78+
/*
79+
* Initialize a checksum context for checksums of the given type.
80+
*/
81+
void
82+
pg_checksum_init(pg_checksum_context*context,pg_checksum_typetype)
83+
{
84+
context->type=type;
85+
86+
switch (type)
87+
{
88+
caseCHECKSUM_TYPE_NONE:
89+
/* do nothing */
90+
break;
91+
caseCHECKSUM_TYPE_CRC32C:
92+
INIT_CRC32C(context->raw_context.c_crc32c);
93+
break;
94+
caseCHECKSUM_TYPE_SHA224:
95+
pg_sha224_init(&context->raw_context.c_sha224);
96+
break;
97+
caseCHECKSUM_TYPE_SHA256:
98+
pg_sha256_init(&context->raw_context.c_sha256);
99+
break;
100+
caseCHECKSUM_TYPE_SHA384:
101+
pg_sha384_init(&context->raw_context.c_sha384);
102+
break;
103+
caseCHECKSUM_TYPE_SHA512:
104+
pg_sha512_init(&context->raw_context.c_sha512);
105+
break;
106+
}
107+
}
108+
109+
/*
110+
* Update a checksum context with new data.
111+
*/
112+
void
113+
pg_checksum_update(pg_checksum_context*context,constuint8*input,
114+
size_tlen)
115+
{
116+
switch (context->type)
117+
{
118+
caseCHECKSUM_TYPE_NONE:
119+
/* do nothing */
120+
break;
121+
caseCHECKSUM_TYPE_CRC32C:
122+
COMP_CRC32C(context->raw_context.c_crc32c,input,len);
123+
break;
124+
caseCHECKSUM_TYPE_SHA224:
125+
pg_sha224_update(&context->raw_context.c_sha224,input,len);
126+
break;
127+
caseCHECKSUM_TYPE_SHA256:
128+
pg_sha256_update(&context->raw_context.c_sha256,input,len);
129+
break;
130+
caseCHECKSUM_TYPE_SHA384:
131+
pg_sha384_update(&context->raw_context.c_sha384,input,len);
132+
break;
133+
caseCHECKSUM_TYPE_SHA512:
134+
pg_sha512_update(&context->raw_context.c_sha512,input,len);
135+
break;
136+
}
137+
}
138+
139+
/*
140+
* Finalize a checksum computation and write the result to an output buffer.
141+
*
142+
* The caller must ensure that the buffer is at least PG_CHECKSUM_MAX_LENGTH
143+
* bytes in length. The return value is the number of bytes actually written.
144+
*/
145+
int
146+
pg_checksum_final(pg_checksum_context*context,uint8*output)
147+
{
148+
intretval=0;
149+
150+
StaticAssertStmt(sizeof(pg_crc32c) <=PG_CHECKSUM_MAX_LENGTH,
151+
"CRC-32C digest too big for PG_CHECKSUM_MAX_LENGTH");
152+
StaticAssertStmt(PG_SHA224_DIGEST_LENGTH <=PG_CHECKSUM_MAX_LENGTH,
153+
"SHA224 digest too for PG_CHECKSUM_MAX_LENGTH");
154+
StaticAssertStmt(PG_SHA256_DIGEST_LENGTH <=PG_CHECKSUM_MAX_LENGTH,
155+
"SHA256 digest too for PG_CHECKSUM_MAX_LENGTH");
156+
StaticAssertStmt(PG_SHA384_DIGEST_LENGTH <=PG_CHECKSUM_MAX_LENGTH,
157+
"SHA384 digest too for PG_CHECKSUM_MAX_LENGTH");
158+
StaticAssertStmt(PG_SHA512_DIGEST_LENGTH <=PG_CHECKSUM_MAX_LENGTH,
159+
"SHA512 digest too for PG_CHECKSUM_MAX_LENGTH");
160+
161+
switch (context->type)
162+
{
163+
caseCHECKSUM_TYPE_NONE:
164+
break;
165+
caseCHECKSUM_TYPE_CRC32C:
166+
FIN_CRC32C(context->raw_context.c_crc32c);
167+
retval=sizeof(pg_crc32c);
168+
memcpy(output,&context->raw_context.c_crc32c,retval);
169+
break;
170+
caseCHECKSUM_TYPE_SHA224:
171+
pg_sha224_final(&context->raw_context.c_sha224,output);
172+
retval=PG_SHA224_DIGEST_LENGTH;
173+
break;
174+
caseCHECKSUM_TYPE_SHA256:
175+
pg_sha256_final(&context->raw_context.c_sha256,output);
176+
retval=PG_SHA256_DIGEST_LENGTH;
177+
break;
178+
caseCHECKSUM_TYPE_SHA384:
179+
pg_sha384_final(&context->raw_context.c_sha384,output);
180+
retval=PG_SHA384_DIGEST_LENGTH;
181+
break;
182+
caseCHECKSUM_TYPE_SHA512:
183+
pg_sha512_final(&context->raw_context.c_sha512,output);
184+
retval=PG_SHA512_DIGEST_LENGTH;
185+
break;
186+
}
187+
188+
Assert(retval <=PG_CHECKSUM_MAX_LENGTH);
189+
returnretval;
190+
}

‎src/include/common/checksum_helper.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* checksum_helper.h
4+
* Compute a checksum of any of various types using common routines
5+
*
6+
* Portions Copyright (c) 2016-2020, PostgreSQL Global Development Group
7+
*
8+
* IDENTIFICATION
9+
* src/include/common/checksum_helper.h
10+
*
11+
*-------------------------------------------------------------------------
12+
*/
13+
14+
#ifndefCHECKSUM_HELPER_H
15+
#defineCHECKSUM_HELPER_H
16+
17+
#include"common/sha2.h"
18+
#include"port/pg_crc32c.h"
19+
20+
/*
21+
* Supported checksum types. It's not necessarily the case that code using
22+
* these functions needs a cryptographically strong checksum; it may only
23+
* need to detect accidental modification. That's why we include CRC-32C: it's
24+
* much faster than any of the other algorithms. On the other hand, we omit
25+
* MD5 here because any new that does need a cryptographically strong checksum
26+
* should use something better.
27+
*/
28+
typedefenumpg_checksum_type
29+
{
30+
CHECKSUM_TYPE_NONE,
31+
CHECKSUM_TYPE_CRC32C,
32+
CHECKSUM_TYPE_SHA224,
33+
CHECKSUM_TYPE_SHA256,
34+
CHECKSUM_TYPE_SHA384,
35+
CHECKSUM_TYPE_SHA512
36+
}pg_checksum_type;
37+
38+
/*
39+
* This is just a union of all applicable context types.
40+
*/
41+
typedefunionpg_checksum_raw_context
42+
{
43+
pg_crc32cc_crc32c;
44+
pg_sha224_ctxc_sha224;
45+
pg_sha256_ctxc_sha256;
46+
pg_sha384_ctxc_sha384;
47+
pg_sha512_ctxc_sha512;
48+
}pg_checksum_raw_context;
49+
50+
/*
51+
* This structure provides a convenient way to pass the checksum type and the
52+
* checksum context around together.
53+
*/
54+
typedefstructpg_checksum_context
55+
{
56+
pg_checksum_typetype;
57+
pg_checksum_raw_contextraw_context;
58+
}pg_checksum_context;
59+
60+
/*
61+
* This is the longest possible output for any checksum algorithm supported
62+
* by this file.
63+
*/
64+
#definePG_CHECKSUM_MAX_LENGTHPG_SHA512_DIGEST_LENGTH
65+
66+
externboolpg_checksum_parse_type(char*name,pg_checksum_type*);
67+
externchar*pg_checksum_type_name(pg_checksum_type);
68+
69+
externvoidpg_checksum_init(pg_checksum_context*,pg_checksum_type);
70+
externvoidpg_checksum_update(pg_checksum_context*,constuint8*input,
71+
size_tlen);
72+
externintpg_checksum_final(pg_checksum_context*,uint8*output);
73+
74+
#endif

‎src/tools/msvc/Mkvcbuild.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ sub mkvcbuild
120120
}
121121

122122
our@pgcommonallfiles =qw(
123-
archive.c
124-
base64.c config_info.c controldata_utils.c d2s.c encnames.c exec.c
123+
archive.c base64.c checksum_helper.c
124+
config_info.c controldata_utils.c d2s.c encnames.c exec.c
125125
f2s.c file_perm.c hashfn.c ip.c jsonapi.c
126126
keywords.c kwlookup.c link-canary.c md5.c
127127
pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp