|
| 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 | +} |
0 commit comments