|
5 | 5 |
|
6 | 6 | #include <math.h>
|
7 | 7 |
|
8 |
| -#include "common/hex_decode.h" |
9 | 8 | #include "ecpgerrno.h"
|
10 | 9 | #include "ecpglib.h"
|
11 | 10 | #include "ecpglib_extern.h"
|
@@ -137,6 +136,57 @@ ecpg_hex_dec_len(unsigned srclen)
|
137 | 136 | return srclen >> 1;
|
138 | 137 | }
|
139 | 138 |
|
| 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 | + |
140 | 190 | unsigned
|
141 | 191 | ecpg_hex_encode(const char *src, unsigned len, char *dst)
|
142 | 192 | {
|
|
0 commit comments