PostgreSQL Source Code  git master
checksum_helper.c File Reference
#include "postgres.h"
#include "common/checksum_helper.h"
Include dependency graph for checksum_helper.c:

Go to the source code of this file.

Functions

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

Function Documentation

◆ pg_checksum_final()

int pg_checksum_final ( pg_checksum_context context,
uint8 output 
)

Definition at line 146 of file checksum_helper.c.

References Assert, pg_checksum_raw_context::c_crc32c, pg_checksum_raw_context::c_sha224, pg_checksum_raw_context::c_sha256, pg_checksum_raw_context::c_sha384, pg_checksum_raw_context::c_sha512, CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_SHA224, CHECKSUM_TYPE_SHA256, CHECKSUM_TYPE_SHA384, CHECKSUM_TYPE_SHA512, FIN_CRC32C, PG_CHECKSUM_MAX_LENGTH, PG_SHA224_DIGEST_LENGTH, pg_sha224_final(), PG_SHA256_DIGEST_LENGTH, pg_sha256_final(), PG_SHA384_DIGEST_LENGTH, pg_sha384_final(), PG_SHA512_DIGEST_LENGTH, pg_sha512_final(), pg_checksum_context::raw_context, StaticAssertStmt, and pg_checksum_context::type.

Referenced by AddFileToBackupManifest(), and verify_file_checksum().

147 {
148  int retval = 0;
149 
151  "CRC-32C digest too big for PG_CHECKSUM_MAX_LENGTH");
153  "SHA224 digest too for PG_CHECKSUM_MAX_LENGTH");
155  "SHA256 digest too for PG_CHECKSUM_MAX_LENGTH");
157  "SHA384 digest too for PG_CHECKSUM_MAX_LENGTH");
159  "SHA512 digest too for PG_CHECKSUM_MAX_LENGTH");
160 
161  switch (context->type)
162  {
163  case CHECKSUM_TYPE_NONE:
164  break;
166  FIN_CRC32C(context->raw_context.c_crc32c);
167  retval = sizeof(pg_crc32c);
168  memcpy(output, &context->raw_context.c_crc32c, retval);
169  break;
172  retval = PG_SHA224_DIGEST_LENGTH;
173  break;
176  retval = PG_SHA256_DIGEST_LENGTH;
177  break;
180  retval = PG_SHA384_DIGEST_LENGTH;
181  break;
184  retval = PG_SHA512_DIGEST_LENGTH;
185  break;
186  }
187 
188  Assert(retval <= PG_CHECKSUM_MAX_LENGTH);
189  return retval;
190 }
static void output(uint64 loop_count)
uint32 pg_crc32c
Definition: pg_crc32c.h:38
pg_checksum_raw_context raw_context
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:62
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:68
#define PG_CHECKSUM_MAX_LENGTH
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:852
void pg_sha512_final(pg_sha512_ctx *context, uint8 *digest)
Definition: sha2.c:894
void pg_sha224_final(pg_sha224_ctx *context, uint8 *digest)
Definition: sha2.c:983
void pg_sha384_final(pg_sha384_ctx *context, uint8 *digest)
Definition: sha2.c:939
#define Assert(condition)
Definition: c.h:738
void pg_sha256_final(pg_sha256_ctx *context, uint8 *digest)
Definition: sha2.c:566
pg_checksum_type type
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:59
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:65

◆ pg_checksum_init()

void pg_checksum_init ( pg_checksum_context context,
pg_checksum_type  type 
)

Definition at line 82 of file checksum_helper.c.

References pg_checksum_raw_context::c_crc32c, pg_checksum_raw_context::c_sha224, pg_checksum_raw_context::c_sha256, pg_checksum_raw_context::c_sha384, pg_checksum_raw_context::c_sha512, CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_SHA224, CHECKSUM_TYPE_SHA256, CHECKSUM_TYPE_SHA384, CHECKSUM_TYPE_SHA512, INIT_CRC32C, pg_sha224_init(), pg_sha256_init(), pg_sha384_init(), pg_sha512_init(), pg_checksum_context::raw_context, pg_checksum_context::type, and generate_unaccent_rules::type.

Referenced by sendFile(), sendFileWithContent(), and verify_file_checksum().

83 {
84  context->type = type;
85 
86  switch (type)
87  {
88  case CHECKSUM_TYPE_NONE:
89  /* do nothing */
90  break;
93  break;
96  break;
99  break;
102  break;
105  break;
106  }
107 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
pg_checksum_raw_context raw_context
void pg_sha256_init(pg_sha256_ctx *context)
Definition: sha2.c:268
void pg_sha224_init(pg_sha224_ctx *context)
Definition: sha2.c:967
void pg_sha384_init(pg_sha384_ctx *context)
Definition: sha2.c:923
void pg_sha512_init(pg_sha512_ctx *context)
Definition: sha2.c:594
pg_checksum_type type

◆ pg_checksum_parse_type()

bool pg_checksum_parse_type ( char *  name,
pg_checksum_type type 
)

Definition at line 28 of file checksum_helper.c.

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

Referenced by json_manifest_finalize_file(), and parse_basebackup_options().

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
pg_checksum_type
const char * name
Definition: encode.c:561

◆ pg_checksum_type_name()

char* pg_checksum_type_name ( pg_checksum_type  type)

Definition at line 56 of file checksum_helper.c.

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

Referenced by AddFileToBackupManifest().

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 }
#define Assert(condition)
Definition: c.h:738

◆ pg_checksum_update()

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

Definition at line 113 of file checksum_helper.c.

References pg_checksum_raw_context::c_crc32c, pg_checksum_raw_context::c_sha224, pg_checksum_raw_context::c_sha256, pg_checksum_raw_context::c_sha384, pg_checksum_raw_context::c_sha512, CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_SHA224, CHECKSUM_TYPE_SHA256, CHECKSUM_TYPE_SHA384, CHECKSUM_TYPE_SHA512, COMP_CRC32C, pg_sha224_update(), pg_sha256_update(), pg_sha384_update(), pg_sha512_update(), pg_checksum_context::raw_context, and pg_checksum_context::type.

Referenced by sendFile(), sendFileWithContent(), and verify_file_checksum().

115 {
116  switch (context->type)
117  {
118  case CHECKSUM_TYPE_NONE:
119  /* do nothing */
120  break;
122  COMP_CRC32C(context->raw_context.c_crc32c, input, len);
123  break;
125  pg_sha224_update(&context->raw_context.c_sha224, input, len);
126  break;
128  pg_sha256_update(&context->raw_context.c_sha256, input, len);
129  break;
131  pg_sha384_update(&context->raw_context.c_sha384, input, len);
132  break;
134  pg_sha512_update(&context->raw_context.c_sha512, input, len);
135  break;
136  }
137 }
pg_checksum_raw_context raw_context
void pg_sha384_update(pg_sha384_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:933
void pg_sha512_update(pg_sha512_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:791
void pg_sha256_update(pg_sha256_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:465
void pg_sha224_update(pg_sha224_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:977
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
pg_checksum_type type