PostgreSQL Source Code  git master
checksum_helper.h File Reference
#include "common/cryptohash.h"
#include "common/sha2.h"
#include "port/pg_crc32c.h"
Include dependency graph for checksum_helper.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

union  pg_checksum_raw_context
 
struct  pg_checksum_context
 

Macros

#define PG_CHECKSUM_MAX_LENGTH   PG_SHA512_DIGEST_LENGTH
 

Typedefs

typedef enum pg_checksum_type pg_checksum_type
 
typedef union pg_checksum_raw_context pg_checksum_raw_context
 
typedef struct pg_checksum_context pg_checksum_context
 

Enumerations

enum  pg_checksum_type {
  CHECKSUM_TYPE_NONE , CHECKSUM_TYPE_CRC32C , CHECKSUM_TYPE_SHA224 , CHECKSUM_TYPE_SHA256 ,
  CHECKSUM_TYPE_SHA384 , CHECKSUM_TYPE_SHA512
}
 

Functions

bool pg_checksum_parse_type (char *name, pg_checksum_type *)
 
char * pg_checksum_type_name (pg_checksum_type)
 
int pg_checksum_init (pg_checksum_context *, pg_checksum_type)
 
int pg_checksum_update (pg_checksum_context *, const uint8 *input, size_t len)
 
int pg_checksum_final (pg_checksum_context *, uint8 *output)
 

Macro Definition Documentation

◆ PG_CHECKSUM_MAX_LENGTH

#define PG_CHECKSUM_MAX_LENGTH   PG_SHA512_DIGEST_LENGTH

Definition at line 62 of file checksum_helper.h.

Typedef Documentation

◆ pg_checksum_context

◆ pg_checksum_raw_context

◆ pg_checksum_type

Enumeration Type Documentation

◆ pg_checksum_type

Enumerator
CHECKSUM_TYPE_NONE 
CHECKSUM_TYPE_CRC32C 
CHECKSUM_TYPE_SHA224 
CHECKSUM_TYPE_SHA256 
CHECKSUM_TYPE_SHA384 
CHECKSUM_TYPE_SHA512 

Definition at line 29 of file checksum_helper.h.

30 {
pg_checksum_type
@ CHECKSUM_TYPE_SHA384
@ CHECKSUM_TYPE_SHA256
@ CHECKSUM_TYPE_SHA224
@ CHECKSUM_TYPE_NONE
@ CHECKSUM_TYPE_CRC32C
@ CHECKSUM_TYPE_SHA512

Function Documentation

◆ pg_checksum_final()

int pg_checksum_final ( pg_checksum_context context,
uint8 output 
)

Definition at line 176 of file checksum_helper.c.

177 {
178  int retval = 0;
179 
181  "CRC-32C digest too big for PG_CHECKSUM_MAX_LENGTH");
183  "SHA224 digest too big for PG_CHECKSUM_MAX_LENGTH");
185  "SHA256 digest too big for PG_CHECKSUM_MAX_LENGTH");
187  "SHA384 digest too big for PG_CHECKSUM_MAX_LENGTH");
189  "SHA512 digest too big for PG_CHECKSUM_MAX_LENGTH");
190 
191  switch (context->type)
192  {
193  case CHECKSUM_TYPE_NONE:
194  break;
196  FIN_CRC32C(context->raw_context.c_crc32c);
197  retval = sizeof(pg_crc32c);
198  memcpy(output, &context->raw_context.c_crc32c, retval);
199  break;
201  retval = PG_SHA224_DIGEST_LENGTH;
202  if (pg_cryptohash_final(context->raw_context.c_sha2,
203  output, retval) < 0)
204  return -1;
205  pg_cryptohash_free(context->raw_context.c_sha2);
206  break;
208  retval = PG_SHA256_DIGEST_LENGTH;
209  if (pg_cryptohash_final(context->raw_context.c_sha2,
210  output, retval) < 0)
211  return -1;
212  pg_cryptohash_free(context->raw_context.c_sha2);
213  break;
215  retval = PG_SHA384_DIGEST_LENGTH;
216  if (pg_cryptohash_final(context->raw_context.c_sha2,
217  output, retval) < 0)
218  return -1;
219  pg_cryptohash_free(context->raw_context.c_sha2);
220  break;
222  retval = PG_SHA512_DIGEST_LENGTH;
223  if (pg_cryptohash_final(context->raw_context.c_sha2,
224  output, retval) < 0)
225  return -1;
226  pg_cryptohash_free(context->raw_context.c_sha2);
227  break;
228  }
229 
230  Assert(retval <= PG_CHECKSUM_MAX_LENGTH);
231  return retval;
232 }
#define Assert(condition)
Definition: c.h:858
#define StaticAssertDecl(condition, errmessage)
Definition: c.h:936
#define PG_CHECKSUM_MAX_LENGTH
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:238
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:172
FILE * output
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:103
tree context
Definition: radixtree.h:1829
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:26
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:29
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:20

References Assert, CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_SHA224, CHECKSUM_TYPE_SHA256, CHECKSUM_TYPE_SHA384, CHECKSUM_TYPE_SHA512, context, FIN_CRC32C, output, PG_CHECKSUM_MAX_LENGTH, pg_cryptohash_final(), pg_cryptohash_free(), PG_SHA224_DIGEST_LENGTH, PG_SHA256_DIGEST_LENGTH, PG_SHA384_DIGEST_LENGTH, PG_SHA512_DIGEST_LENGTH, and StaticAssertDecl.

Referenced by AddFileToBackupManifest(), finalize_manifest(), process_directory_recursively(), reconstruct_from_incremental_file(), verify_file_checksum(), and write_backup_label().

◆ pg_checksum_init()

int pg_checksum_init ( pg_checksum_context context,
pg_checksum_type  type 
)

Definition at line 83 of file checksum_helper.c.

84 {
85  context->type = type;
86 
87  switch (type)
88  {
89  case CHECKSUM_TYPE_NONE:
90  /* do nothing */
91  break;
93  INIT_CRC32C(context->raw_context.c_crc32c);
94  break;
96  context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA224);
97  if (context->raw_context.c_sha2 == NULL)
98  return -1;
99  if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
100  {
101  pg_cryptohash_free(context->raw_context.c_sha2);
102  return -1;
103  }
104  break;
106  context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA256);
107  if (context->raw_context.c_sha2 == NULL)
108  return -1;
109  if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
110  {
111  pg_cryptohash_free(context->raw_context.c_sha2);
112  return -1;
113  }
114  break;
116  context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA384);
117  if (context->raw_context.c_sha2 == NULL)
118  return -1;
119  if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
120  {
121  pg_cryptohash_free(context->raw_context.c_sha2);
122  return -1;
123  }
124  break;
126  context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA512);
127  if (context->raw_context.c_sha2 == NULL)
128  return -1;
129  if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
130  {
131  pg_cryptohash_free(context->raw_context.c_sha2);
132  return -1;
133  }
134  break;
135  }
136 
137  return 0;
138 }
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:100
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:74
@ PG_SHA512
Definition: cryptohash.h:26
@ PG_SHA224
Definition: cryptohash.h:23
@ PG_SHA384
Definition: cryptohash.h:25
@ PG_SHA256
Definition: cryptohash.h:24
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
const char * type

References CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_SHA224, CHECKSUM_TYPE_SHA256, CHECKSUM_TYPE_SHA384, CHECKSUM_TYPE_SHA512, context, INIT_CRC32C, pg_cryptohash_create(), pg_cryptohash_free(), pg_cryptohash_init(), PG_SHA224, PG_SHA256, PG_SHA384, PG_SHA512, and type.

Referenced by create_manifest_writer(), process_directory_recursively(), reconstruct_from_incremental_file(), sendFile(), sendFileWithContent(), verify_file_checksum(), and write_backup_label().

◆ pg_checksum_parse_type()

bool pg_checksum_parse_type ( char *  name,
pg_checksum_type type 
)

Definition at line 28 of file checksum_helper.c.

29 {
31  bool result = true;
32 
33  if (pg_strcasecmp(name, "none") == 0)
34  result_type = CHECKSUM_TYPE_NONE;
35  else if (pg_strcasecmp(name, "crc32c") == 0)
36  result_type = CHECKSUM_TYPE_CRC32C;
37  else if (pg_strcasecmp(name, "sha224") == 0)
38  result_type = CHECKSUM_TYPE_SHA224;
39  else if (pg_strcasecmp(name, "sha256") == 0)
40  result_type = CHECKSUM_TYPE_SHA256;
41  else if (pg_strcasecmp(name, "sha384") == 0)
42  result_type = CHECKSUM_TYPE_SHA384;
43  else if (pg_strcasecmp(name, "sha512") == 0)
44  result_type = CHECKSUM_TYPE_SHA512;
45  else
46  result = false;
47 
48  *type = result_type;
49  return result;
50 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * name

References CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_SHA224, CHECKSUM_TYPE_SHA256, CHECKSUM_TYPE_SHA384, CHECKSUM_TYPE_SHA512, name, pg_strcasecmp(), and type.

Referenced by json_manifest_finalize_file(), main(), and parse_basebackup_options().

◆ pg_checksum_type_name()

char* pg_checksum_type_name ( pg_checksum_type  type)

Definition at line 56 of file checksum_helper.c.

57 {
58  switch (type)
59  {
60  case CHECKSUM_TYPE_NONE:
61  return "NONE";
63  return "CRC32C";
65  return "SHA224";
67  return "SHA256";
69  return "SHA384";
71  return "SHA512";
72  }
73 
74  Assert(false);
75  return "???";
76 }

References Assert, CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_SHA224, CHECKSUM_TYPE_SHA256, CHECKSUM_TYPE_SHA384, CHECKSUM_TYPE_SHA512, and type.

Referenced by add_file_to_manifest(), AddFileToBackupManifest(), copy_file(), and write_reconstructed_file().

◆ pg_checksum_update()

int pg_checksum_update ( pg_checksum_context context,
const uint8 input,
size_t  len 
)

Definition at line 145 of file checksum_helper.c.

147 {
148  switch (context->type)
149  {
150  case CHECKSUM_TYPE_NONE:
151  /* do nothing */
152  break;
154  COMP_CRC32C(context->raw_context.c_crc32c, input, len);
155  break;
160  if (pg_cryptohash_update(context->raw_context.c_sha2, input, len) < 0)
161  return -1;
162  break;
163  }
164 
165  return 0;
166 }
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:136
FILE * input
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:98
const void size_t len

References CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_SHA224, CHECKSUM_TYPE_SHA256, CHECKSUM_TYPE_SHA384, CHECKSUM_TYPE_SHA512, COMP_CRC32C, context, input, len, and pg_cryptohash_update().

Referenced by checksum_file(), copy_file_blocks(), flush_manifest(), push_to_sink(), sendFile(), sendFileWithContent(), verify_file_checksum(), write_backup_label(), write_block(), and write_reconstructed_file().