mirror of
https://github.com/postgres/postgres.git
synced 2025-06-25 01:02:05 +03:00
move hex_decode() to /common so it can be called from frontend
This allows removal of a copy of hex_decode() from ecpg, and will be used by the soon-to-be added pg_alterckey command. Backpatch-through: master
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "common/hex_decode.h"
|
||||
#include "ecpgerrno.h"
|
||||
#include "ecpglib.h"
|
||||
#include "ecpglib_extern.h"
|
||||
@ -136,57 +137,6 @@ ecpg_hex_dec_len(unsigned srclen)
|
||||
return srclen >> 1;
|
||||
}
|
||||
|
||||
static inline char
|
||||
get_hex(char c)
|
||||
{
|
||||
static const int8 hexlookup[128] = {
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
|
||||
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
};
|
||||
int res = -1;
|
||||
|
||||
if (c > 0 && c < 127)
|
||||
res = hexlookup[(unsigned char) c];
|
||||
|
||||
return (char) res;
|
||||
}
|
||||
|
||||
static unsigned
|
||||
hex_decode(const char *src, unsigned len, char *dst)
|
||||
{
|
||||
const char *s,
|
||||
*srcend;
|
||||
char v1,
|
||||
v2,
|
||||
*p;
|
||||
|
||||
srcend = src + len;
|
||||
s = src;
|
||||
p = dst;
|
||||
while (s < srcend)
|
||||
{
|
||||
if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r')
|
||||
{
|
||||
s++;
|
||||
continue;
|
||||
}
|
||||
v1 = get_hex(*s++) << 4;
|
||||
if (s >= srcend)
|
||||
return -1;
|
||||
|
||||
v2 = get_hex(*s++);
|
||||
*p++ = v1 | v2;
|
||||
}
|
||||
|
||||
return p - dst;
|
||||
}
|
||||
|
||||
unsigned
|
||||
ecpg_hex_encode(const char *src, unsigned len, char *dst)
|
||||
{
|
||||
|
Reference in New Issue
Block a user