Skip to content

Commit c3826f8

Browse files
committed
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
1 parent 7519bd1 commit c3826f8

File tree

8 files changed

+127
-116
lines changed

8 files changed

+127
-116
lines changed

src/backend/utils/adt/encode.c

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <ctype.h>
1717

18+
#include "common/hex_decode.h"
1819
#include "mb/pg_wchar.h"
1920
#include "utils/builtins.h"
2021
#include "utils/memutils.h"
@@ -146,17 +147,6 @@ binary_decode(PG_FUNCTION_ARGS)
146147

147148
static const char hextbl[] = "0123456789abcdef";
148149

149-
static const int8 hexlookup[128] = {
150-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
151-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
152-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
153-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
154-
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
155-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
156-
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
157-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
158-
};
159-
160150
uint64
161151
hex_encode(const char *src, size_t len, char *dst)
162152
{
@@ -171,58 +161,6 @@ hex_encode(const char *src, size_t len, char *dst)
171161
return (uint64) len * 2;
172162
}
173163

174-
static inline char
175-
get_hex(const char *cp)
176-
{
177-
unsigned char c = (unsigned char) *cp;
178-
int res = -1;
179-
180-
if (c < 127)
181-
res = hexlookup[c];
182-
183-
if (res < 0)
184-
ereport(ERROR,
185-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
186-
errmsg("invalid hexadecimal digit: \"%.*s\"",
187-
pg_mblen(cp), cp)));
188-
189-
return (char) res;
190-
}
191-
192-
uint64
193-
hex_decode(const char *src, size_t len, char *dst)
194-
{
195-
const char *s,
196-
*srcend;
197-
char v1,
198-
v2,
199-
*p;
200-
201-
srcend = src + len;
202-
s = src;
203-
p = dst;
204-
while (s < srcend)
205-
{
206-
if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r')
207-
{
208-
s++;
209-
continue;
210-
}
211-
v1 = get_hex(s) << 4;
212-
s++;
213-
if (s >= srcend)
214-
ereport(ERROR,
215-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
216-
errmsg("invalid hexadecimal data: odd number of digits")));
217-
218-
v2 = get_hex(s);
219-
s++;
220-
*p++ = v1 | v2;
221-
}
222-
223-
return p - dst;
224-
}
225-
226164
static uint64
227165
hex_enc_len(const char *src, size_t srclen)
228166
{

src/backend/utils/adt/varlena.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "catalog/pg_type.h"
2323
#include "common/hashfn.h"
2424
#include "common/int.h"
25+
#include "common/hex_decode.h"
2526
#include "common/unicode_norm.h"
2627
#include "lib/hyperloglog.h"
2728
#include "libpq/pqformat.h"

src/common/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ OBJS_COMMON = \
5858
file_perm.o \
5959
file_utils.o \
6060
hashfn.o \
61+
hex_decode.o \
6162
ip.o \
6263
jsonapi.o \
6364
keywords.o \

src/common/hex_decode.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* hex_decode.c
4+
* hex decoding
5+
*
6+
*
7+
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8+
* Portions Copyright (c) 1994, Regents of the University of California
9+
*
10+
*
11+
* IDENTIFICATION
12+
* src/common/hex_decode.c
13+
*
14+
*-------------------------------------------------------------------------
15+
*/
16+
17+
18+
#ifndef FRONTEND
19+
#include "postgres.h"
20+
#else
21+
#include "postgres_fe.h"
22+
#endif
23+
24+
#ifdef FRONTEND
25+
#include "common/logging.h"
26+
#else
27+
#include "mb/pg_wchar.h"
28+
#endif
29+
#include "common/hex_decode.h"
30+
31+
32+
static const int8 hexlookup[128] = {
33+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
37+
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39+
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41+
};
42+
43+
static inline char
44+
get_hex(const char *cp)
45+
{
46+
unsigned char c = (unsigned char) *cp;
47+
int res = -1;
48+
49+
if (c < 127)
50+
res = hexlookup[c];
51+
52+
if (res < 0)
53+
{
54+
#ifdef FRONTEND
55+
pg_log_fatal("invalid hexadecimal digit");
56+
exit(EXIT_FAILURE);
57+
#else
58+
ereport(ERROR,
59+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
60+
errmsg("invalid hexadecimal digit: \"%.*s\"",
61+
pg_mblen(cp), cp)));
62+
#endif
63+
}
64+
65+
return (char) res;
66+
}
67+
68+
uint64
69+
hex_decode(const char *src, size_t len, char *dst)
70+
{
71+
const char *s,
72+
*srcend;
73+
char v1,
74+
v2,
75+
*p;
76+
77+
srcend = src + len;
78+
s = src;
79+
p = dst;
80+
while (s < srcend)
81+
{
82+
if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r')
83+
{
84+
s++;
85+
continue;
86+
}
87+
v1 = get_hex(s) << 4;
88+
s++;
89+
if (s >= srcend)
90+
{
91+
#ifdef FRONTEND
92+
pg_log_fatal("invalid hexadecimal data: odd number of digits");
93+
exit(EXIT_FAILURE);
94+
#else
95+
ereport(ERROR,
96+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
97+
errmsg("invalid hexadecimal data: odd number of digits")));
98+
#endif
99+
}
100+
v2 = get_hex(s);
101+
s++;
102+
*p++ = v1 | v2;
103+
}
104+
105+
return p - dst;
106+
}

src/include/common/hex_decode.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* hex_decode.h
3+
* hex decoding
4+
*
5+
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
6+
* Portions Copyright (c) 1994, Regents of the University of California
7+
*
8+
* src/include/common/hex_decode.h
9+
*/
10+
#ifndef COMMON_HEX_DECODE_H
11+
#define COMMON_HEX_DECODE_H
12+
13+
extern uint64 hex_decode(const char *src, size_t len, char *dst);
14+
15+
16+
#endif /* COMMON_HEX_DECODE_H */

src/include/utils/builtins.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ extern int errdomainconstraint(Oid datatypeOid, const char *conname);
3333

3434
/* encode.c */
3535
extern uint64 hex_encode(const char *src, size_t len, char *dst);
36-
extern uint64 hex_decode(const char *src, size_t len, char *dst);
3736

3837
/* int.c */
3938
extern int2vector *buildint2vector(const int16 *int2s, int n);

src/interfaces/ecpg/ecpglib/data.c

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <math.h>
77

8+
#include "common/hex_decode.h"
89
#include "ecpgerrno.h"
910
#include "ecpglib.h"
1011
#include "ecpglib_extern.h"
@@ -136,57 +137,6 @@ ecpg_hex_dec_len(unsigned srclen)
136137
return srclen >> 1;
137138
}
138139

139-
static inline char
140-
get_hex(char c)
141-
{
142-
static const int8 hexlookup[128] = {
143-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
144-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
145-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
146-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
147-
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
148-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
149-
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
150-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
151-
};
152-
int res = -1;
153-
154-
if (c > 0 && c < 127)
155-
res = hexlookup[(unsigned char) c];
156-
157-
return (char) res;
158-
}
159-
160-
static unsigned
161-
hex_decode(const char *src, unsigned len, char *dst)
162-
{
163-
const char *s,
164-
*srcend;
165-
char v1,
166-
v2,
167-
*p;
168-
169-
srcend = src + len;
170-
s = src;
171-
p = dst;
172-
while (s < srcend)
173-
{
174-
if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r')
175-
{
176-
s++;
177-
continue;
178-
}
179-
v1 = get_hex(*s++) << 4;
180-
if (s >= srcend)
181-
return -1;
182-
183-
v2 = get_hex(*s++);
184-
*p++ = v1 | v2;
185-
}
186-
187-
return p - dst;
188-
}
189-
190140
unsigned
191141
ecpg_hex_encode(const char *src, unsigned len, char *dst)
192142
{

src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ sub mkvcbuild
121121
our @pgcommonallfiles = qw(
122122
archive.c base64.c checksum_helper.c
123123
config_info.c controldata_utils.c d2s.c encnames.c exec.c
124-
f2s.c file_perm.c file_utils.c hashfn.c ip.c jsonapi.c
124+
f2s.c file_perm.c file_utils.c hashfn.c hex_decode.c ip.c jsonapi.c
125125
keywords.c kwlookup.c link-canary.c md5_common.c
126126
pg_get_line.c pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
127127
saslprep.c scram-common.c string.c stringinfo.c unicode_norm.c username.c

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy