PostgreSQL Source Code  git master
hex.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

uint64 pg_hex_decode (const char *src, size_t srclen, char *dst, size_t dstlen)
 
uint64 pg_hex_encode (const char *src, size_t srclen, char *dst, size_t dstlen)
 
uint64 pg_hex_enc_len (size_t srclen)
 
uint64 pg_hex_dec_len (size_t srclen)
 

Function Documentation

◆ pg_hex_dec_len()

uint64 pg_hex_dec_len ( size_t  srclen)

Definition at line 189 of file hex.c.

Referenced by byteain(), and hex_dec_len().

190 {
191  return (uint64) srclen >> 1;
192 }

◆ pg_hex_decode()

uint64 pg_hex_decode ( const char *  src,
size_t  srclen,
char *  dst,
size_t  dstlen 
)

Definition at line 112 of file hex.c.

References Assert, elog, ereport, errcode(), errmsg(), ERROR, EXIT_FAILURE, get_hex(), and pg_log_fatal.

Referenced by byteain().

113 {
114  const char *s,
115  *srcend;
116  char v1,
117  v2,
118  *p;
119 
120  srcend = src + srclen;
121  s = src;
122  p = dst;
123  while (s < srcend)
124  {
125  if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r')
126  {
127  s++;
128  continue;
129  }
130  v1 = get_hex(s) << 4;
131  s++;
132 
133  if (s >= srcend)
134  {
135 #ifdef FRONTEND
136  pg_log_fatal("invalid hexadecimal data: odd number of digits");
137  exit(EXIT_FAILURE);
138 #else
139  ereport(ERROR,
140  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
141  errmsg("invalid hexadecimal data: odd number of digits")));
142 #endif
143  }
144 
145  v2 = get_hex(s);
146  s++;
147 
148  /* overflow check */
149  if ((p - dst + 1) > dstlen)
150  {
151 #ifdef FRONTEND
152  pg_log_fatal("overflow of destination buffer in hex decoding");
153  exit(EXIT_FAILURE);
154 #else
155  elog(ERROR, "overflow of destination buffer in hex decoding");
156 #endif
157  }
158 
159  *p++ = v1 | v2;
160  }
161 
162  Assert((p - dst) <= dstlen);
163  return p - dst;
164 }
static char get_hex(const char *cp)
Definition: hex.c:43
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define EXIT_FAILURE
Definition: settings.h:162
#define pg_log_fatal(...)
Definition: logging.h:76

◆ pg_hex_enc_len()

uint64 pg_hex_enc_len ( size_t  srclen)

Definition at line 175 of file hex.c.

Referenced by AddFileToBackupManifest(), byteaout(), hex_enc_len(), and SendBackupManifest().

176 {
177  return (uint64) srclen << 1;
178 }

◆ pg_hex_encode()

uint64 pg_hex_encode ( const char *  src,
size_t  srclen,
char *  dst,
size_t  dstlen 
)

Definition at line 74 of file hex.c.

References Assert, elog, ERROR, EXIT_FAILURE, hextbl, and pg_log_fatal.

Referenced by AddFileToBackupManifest(), byteaout(), and SendBackupManifest().

75 {
76  const char *end = src + srclen;
77  char *p;
78 
79  p = dst;
80 
81  while (src < end)
82  {
83  /*
84  * Leave if there is an overflow in the area allocated for the encoded
85  * string.
86  */
87  if ((p - dst + 2) > dstlen)
88  {
89 #ifdef FRONTEND
90  pg_log_fatal("overflow of destination buffer in hex encoding");
91  exit(EXIT_FAILURE);
92 #else
93  elog(ERROR, "overflow of destination buffer in hex encoding");
94 #endif
95  }
96 
97  *p++ = hextbl[(*src >> 4) & 0xF];
98  *p++ = hextbl[*src & 0xF];
99  src++;
100  }
101 
102  Assert((p - dst) <= dstlen);
103  return p - dst;
104 }
#define ERROR
Definition: elog.h:46
#define Assert(condition)
Definition: c.h:804
#define elog(elevel,...)
Definition: elog.h:232
#define EXIT_FAILURE
Definition: settings.h:162
static const char hextbl[]
Definition: hex.c:40
#define pg_log_fatal(...)
Definition: logging.h:76