mirror of
https://github.com/postgres/postgres.git
synced 2025-04-25 21:42:33 +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:
parent
7519bd16d1
commit
c3826f831e
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#include "common/hex_decode.h"
|
||||||
#include "mb/pg_wchar.h"
|
#include "mb/pg_wchar.h"
|
||||||
#include "utils/builtins.h"
|
#include "utils/builtins.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
@ -146,17 +147,6 @@ binary_decode(PG_FUNCTION_ARGS)
|
|||||||
|
|
||||||
static const char hextbl[] = "0123456789abcdef";
|
static const char hextbl[] = "0123456789abcdef";
|
||||||
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|
||||||
uint64
|
uint64
|
||||||
hex_encode(const char *src, size_t len, char *dst)
|
hex_encode(const char *src, size_t len, char *dst)
|
||||||
{
|
{
|
||||||
@ -171,58 +161,6 @@ hex_encode(const char *src, size_t len, char *dst)
|
|||||||
return (uint64) len * 2;
|
return (uint64) len * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline char
|
|
||||||
get_hex(const char *cp)
|
|
||||||
{
|
|
||||||
unsigned char c = (unsigned char) *cp;
|
|
||||||
int res = -1;
|
|
||||||
|
|
||||||
if (c < 127)
|
|
||||||
res = hexlookup[c];
|
|
||||||
|
|
||||||
if (res < 0)
|
|
||||||
ereport(ERROR,
|
|
||||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
||||||
errmsg("invalid hexadecimal digit: \"%.*s\"",
|
|
||||||
pg_mblen(cp), cp)));
|
|
||||||
|
|
||||||
return (char) res;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64
|
|
||||||
hex_decode(const char *src, size_t 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;
|
|
||||||
s++;
|
|
||||||
if (s >= srcend)
|
|
||||||
ereport(ERROR,
|
|
||||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
||||||
errmsg("invalid hexadecimal data: odd number of digits")));
|
|
||||||
|
|
||||||
v2 = get_hex(s);
|
|
||||||
s++;
|
|
||||||
*p++ = v1 | v2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return p - dst;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint64
|
static uint64
|
||||||
hex_enc_len(const char *src, size_t srclen)
|
hex_enc_len(const char *src, size_t srclen)
|
||||||
{
|
{
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include "catalog/pg_type.h"
|
#include "catalog/pg_type.h"
|
||||||
#include "common/hashfn.h"
|
#include "common/hashfn.h"
|
||||||
#include "common/int.h"
|
#include "common/int.h"
|
||||||
|
#include "common/hex_decode.h"
|
||||||
#include "common/unicode_norm.h"
|
#include "common/unicode_norm.h"
|
||||||
#include "lib/hyperloglog.h"
|
#include "lib/hyperloglog.h"
|
||||||
#include "libpq/pqformat.h"
|
#include "libpq/pqformat.h"
|
||||||
|
@ -58,6 +58,7 @@ OBJS_COMMON = \
|
|||||||
file_perm.o \
|
file_perm.o \
|
||||||
file_utils.o \
|
file_utils.o \
|
||||||
hashfn.o \
|
hashfn.o \
|
||||||
|
hex_decode.o \
|
||||||
ip.o \
|
ip.o \
|
||||||
jsonapi.o \
|
jsonapi.o \
|
||||||
keywords.o \
|
keywords.o \
|
||||||
|
106
src/common/hex_decode.c
Normal file
106
src/common/hex_decode.c
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
*
|
||||||
|
* hex_decode.c
|
||||||
|
* hex decoding
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
||||||
|
* Portions Copyright (c) 1994, Regents of the University of California
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* IDENTIFICATION
|
||||||
|
* src/common/hex_decode.c
|
||||||
|
*
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef FRONTEND
|
||||||
|
#include "postgres.h"
|
||||||
|
#else
|
||||||
|
#include "postgres_fe.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef FRONTEND
|
||||||
|
#include "common/logging.h"
|
||||||
|
#else
|
||||||
|
#include "mb/pg_wchar.h"
|
||||||
|
#endif
|
||||||
|
#include "common/hex_decode.h"
|
||||||
|
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline char
|
||||||
|
get_hex(const char *cp)
|
||||||
|
{
|
||||||
|
unsigned char c = (unsigned char) *cp;
|
||||||
|
int res = -1;
|
||||||
|
|
||||||
|
if (c < 127)
|
||||||
|
res = hexlookup[c];
|
||||||
|
|
||||||
|
if (res < 0)
|
||||||
|
{
|
||||||
|
#ifdef FRONTEND
|
||||||
|
pg_log_fatal("invalid hexadecimal digit");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
#else
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||||
|
errmsg("invalid hexadecimal digit: \"%.*s\"",
|
||||||
|
pg_mblen(cp), cp)));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return (char) res;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64
|
||||||
|
hex_decode(const char *src, size_t 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;
|
||||||
|
s++;
|
||||||
|
if (s >= srcend)
|
||||||
|
{
|
||||||
|
#ifdef FRONTEND
|
||||||
|
pg_log_fatal("invalid hexadecimal data: odd number of digits");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
#else
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||||
|
errmsg("invalid hexadecimal data: odd number of digits")));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
v2 = get_hex(s);
|
||||||
|
s++;
|
||||||
|
*p++ = v1 | v2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return p - dst;
|
||||||
|
}
|
16
src/include/common/hex_decode.h
Normal file
16
src/include/common/hex_decode.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* hex_decode.h
|
||||||
|
* hex decoding
|
||||||
|
*
|
||||||
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
||||||
|
* Portions Copyright (c) 1994, Regents of the University of California
|
||||||
|
*
|
||||||
|
* src/include/common/hex_decode.h
|
||||||
|
*/
|
||||||
|
#ifndef COMMON_HEX_DECODE_H
|
||||||
|
#define COMMON_HEX_DECODE_H
|
||||||
|
|
||||||
|
extern uint64 hex_decode(const char *src, size_t len, char *dst);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* COMMON_HEX_DECODE_H */
|
@ -33,7 +33,6 @@ extern int errdomainconstraint(Oid datatypeOid, const char *conname);
|
|||||||
|
|
||||||
/* encode.c */
|
/* encode.c */
|
||||||
extern uint64 hex_encode(const char *src, size_t len, char *dst);
|
extern uint64 hex_encode(const char *src, size_t len, char *dst);
|
||||||
extern uint64 hex_decode(const char *src, size_t len, char *dst);
|
|
||||||
|
|
||||||
/* int.c */
|
/* int.c */
|
||||||
extern int2vector *buildint2vector(const int16 *int2s, int n);
|
extern int2vector *buildint2vector(const int16 *int2s, int n);
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
#include "common/hex_decode.h"
|
||||||
#include "ecpgerrno.h"
|
#include "ecpgerrno.h"
|
||||||
#include "ecpglib.h"
|
#include "ecpglib.h"
|
||||||
#include "ecpglib_extern.h"
|
#include "ecpglib_extern.h"
|
||||||
@ -136,57 +137,6 @@ ecpg_hex_dec_len(unsigned srclen)
|
|||||||
return srclen >> 1;
|
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
|
unsigned
|
||||||
ecpg_hex_encode(const char *src, unsigned len, char *dst)
|
ecpg_hex_encode(const char *src, unsigned len, char *dst)
|
||||||
{
|
{
|
||||||
|
@ -121,7 +121,7 @@ sub mkvcbuild
|
|||||||
our @pgcommonallfiles = qw(
|
our @pgcommonallfiles = qw(
|
||||||
archive.c base64.c checksum_helper.c
|
archive.c base64.c checksum_helper.c
|
||||||
config_info.c controldata_utils.c d2s.c encnames.c exec.c
|
config_info.c controldata_utils.c d2s.c encnames.c exec.c
|
||||||
f2s.c file_perm.c file_utils.c hashfn.c ip.c jsonapi.c
|
f2s.c file_perm.c file_utils.c hashfn.c hex_decode.c ip.c jsonapi.c
|
||||||
keywords.c kwlookup.c link-canary.c md5_common.c
|
keywords.c kwlookup.c link-canary.c md5_common.c
|
||||||
pg_get_line.c pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
|
pg_get_line.c pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
|
||||||
saslprep.c scram-common.c string.c stringinfo.c unicode_norm.c username.c
|
saslprep.c scram-common.c string.c stringinfo.c unicode_norm.c username.c
|
||||||
|
Loading…
x
Reference in New Issue
Block a user