|
| 1 | +/* |
| 2 | + * encode.c |
| 3 | + * Various data encoding/decoding things. |
| 4 | + * |
| 5 | + * Copyright (c) 2001 Marko Kreen |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions |
| 10 | + * are met: |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer in the |
| 15 | + * documentation and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | + * SUCH DAMAGE. |
| 28 | + * |
| 29 | + * $Id: encode.c,v 1.1 2001/01/24 03:46:16 momjian Exp $ |
| 30 | + */ |
| 31 | + |
| 32 | +#include <postgres.h> |
| 33 | +#include <fmgr.h> |
| 34 | + |
| 35 | +#include "encode.h" |
| 36 | + |
| 37 | +/* |
| 38 | + * NAMEDATALEN is used for hash names |
| 39 | + */ |
| 40 | +#if NAMEDATALEN < 16 |
| 41 | +#error "NAMEDATALEN < 16: too small" |
| 42 | +#endif |
| 43 | + |
| 44 | +static pg_coding * |
| 45 | +find_coding(pg_coding *hbuf, text *name, int silent); |
| 46 | +static pg_coding * |
| 47 | +pg_find_coding(pg_coding *res, char *name); |
| 48 | + |
| 49 | + |
| 50 | +/* SQL function: encode(bytea, text) returns text */ |
| 51 | +PG_FUNCTION_INFO_V1(encode); |
| 52 | + |
| 53 | +Datum |
| 54 | +encode(PG_FUNCTION_ARGS) |
| 55 | +{ |
| 56 | + text *arg; |
| 57 | + text *name; |
| 58 | + uint len, rlen, rlen0; |
| 59 | + pg_coding *c, cbuf; |
| 60 | + text *res; |
| 61 | + |
| 62 | + if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) |
| 63 | + PG_RETURN_NULL(); |
| 64 | + |
| 65 | + name = PG_GETARG_TEXT_P(1); |
| 66 | + c = find_coding(&cbuf, name, 0); /* will give error if fails */ |
| 67 | + |
| 68 | + arg = PG_GETARG_TEXT_P(0); |
| 69 | + len = VARSIZE(arg) - VARHDRSZ; |
| 70 | + |
| 71 | + rlen0 = c->encode_len(len); |
| 72 | + |
| 73 | + res = (text *)palloc(rlen0 + VARHDRSZ); |
| 74 | + |
| 75 | + rlen = c->encode(VARDATA(arg), len, VARDATA(res)); |
| 76 | + VARATT_SIZEP(res) = rlen + VARHDRSZ; |
| 77 | + |
| 78 | + if (rlen > rlen0) |
| 79 | + elog(FATAL, "pg_encode: overflow, encode estimate too small"); |
| 80 | + |
| 81 | + PG_FREE_IF_COPY(arg, 0); |
| 82 | + PG_FREE_IF_COPY(name, 0); |
| 83 | + |
| 84 | + PG_RETURN_TEXT_P(res); |
| 85 | +} |
| 86 | + |
| 87 | +/* SQL function: decode(text, text) returns bytea */ |
| 88 | +PG_FUNCTION_INFO_V1(decode); |
| 89 | + |
| 90 | +Datum |
| 91 | +decode(PG_FUNCTION_ARGS) |
| 92 | +{ |
| 93 | + text *arg; |
| 94 | + text *name; |
| 95 | + uint len, rlen, rlen0; |
| 96 | + pg_coding *c, cbuf; |
| 97 | + text *res; |
| 98 | + |
| 99 | + if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) |
| 100 | + PG_RETURN_NULL(); |
| 101 | + |
| 102 | + name = PG_GETARG_TEXT_P(1); |
| 103 | + c = find_coding(&cbuf, name, 0); /* will give error if fails */ |
| 104 | + |
| 105 | + arg = PG_GETARG_TEXT_P(0); |
| 106 | + len = VARSIZE(arg) - VARHDRSZ; |
| 107 | + |
| 108 | + rlen0 = c->decode_len(len); |
| 109 | + |
| 110 | + res = (text *)palloc(rlen0 + VARHDRSZ); |
| 111 | + |
| 112 | + rlen = c->decode(VARDATA(arg), len, VARDATA(res)); |
| 113 | + VARATT_SIZEP(res) = rlen + VARHDRSZ; |
| 114 | + |
| 115 | + if (rlen > rlen0) |
| 116 | + elog(FATAL, "pg_decode: overflow, decode estimate too small"); |
| 117 | + |
| 118 | + PG_FREE_IF_COPY(arg, 0); |
| 119 | + PG_FREE_IF_COPY(name, 0); |
| 120 | + |
| 121 | + PG_RETURN_TEXT_P(res); |
| 122 | +} |
| 123 | + |
| 124 | +static pg_coding * |
| 125 | +find_coding(pg_coding *dst, text *name, int silent) |
| 126 | +{ |
| 127 | + pg_coding *p; |
| 128 | + char buf[NAMEDATALEN]; |
| 129 | + uint len; |
| 130 | + |
| 131 | + len = VARSIZE(name) - VARHDRSZ; |
| 132 | + if (len >= NAMEDATALEN) { |
| 133 | + if (silent) |
| 134 | + return NULL; |
| 135 | + elog(ERROR, "Encoding type does not exist (name too long)"); |
| 136 | + } |
| 137 | + |
| 138 | + memcpy(buf, VARDATA(name), len); |
| 139 | + buf[len] = 0; |
| 140 | + |
| 141 | + p = pg_find_coding(dst, buf); |
| 142 | + |
| 143 | + if (p == NULL && !silent) |
| 144 | + elog(ERROR, "Encoding type does not exist: '%s'", buf); |
| 145 | + return p; |
| 146 | +} |
| 147 | + |
| 148 | +static char *hextbl = "0123456789abcdef"; |
| 149 | + |
| 150 | +uint |
| 151 | +hex_encode(uint8 *src, uint len, uint8 *dst) |
| 152 | +{ |
| 153 | + uint8 *end = src + len; |
| 154 | + while (src < end) { |
| 155 | + *dst++ = hextbl[(*src >> 4) & 0xF]; |
| 156 | + *dst++ = hextbl[*src & 0xF]; |
| 157 | + src++; |
| 158 | + } |
| 159 | + return len*2; |
| 160 | +} |
| 161 | + |
| 162 | +/* probably should use lookup table */ |
| 163 | +static uint8 |
| 164 | +get_hex(char c) |
| 165 | +{ |
| 166 | + uint8 res = 0; |
| 167 | + |
| 168 | + if (c >= '0' && c <= '9') |
| 169 | + res = c - '0'; |
| 170 | + else if (c >= 'a' && c <= 'f') |
| 171 | + res = c - 'a' + 10; |
| 172 | + else if (c >= 'A' && c <= 'F') |
| 173 | + res = c - 'A' + 10; |
| 174 | + else |
| 175 | + elog(ERROR, "Bad hex code: '%c'", c); |
| 176 | + |
| 177 | + return res; |
| 178 | +} |
| 179 | + |
| 180 | +uint |
| 181 | +hex_decode(uint8 *src, uint len, uint8 *dst) |
| 182 | +{ |
| 183 | + uint8 *s, *srcend, v1, v2, *p = dst; |
| 184 | + |
| 185 | + srcend = src + len; |
| 186 | + s = src; p = dst; |
| 187 | + while (s < srcend) { |
| 188 | + if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r') { |
| 189 | + s++; |
| 190 | + continue; |
| 191 | + } |
| 192 | + v1 = get_hex(*s++) << 4; |
| 193 | + if (s >= srcend) |
| 194 | + elog(ERROR, "hex_decode: invalid data"); |
| 195 | + v2 = get_hex(*s++); |
| 196 | + *p++ = v1 | v2; |
| 197 | + } |
| 198 | + |
| 199 | + return p - dst; |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +static unsigned char _base64[] = |
| 204 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 205 | + |
| 206 | +uint |
| 207 | +b64_encode(uint8 *src, uint len, uint8 *dst) |
| 208 | +{ |
| 209 | + uint8 *s, *p, *end = src + len, *lend = dst + 76; |
| 210 | + int pos = 2; |
| 211 | + unsigned long buf = 0; |
| 212 | + |
| 213 | + s = src; p = dst; |
| 214 | + |
| 215 | + while (s < end) { |
| 216 | + buf |= *s << (pos << 3); |
| 217 | + pos--; |
| 218 | + s++; |
| 219 | + |
| 220 | + /* write it out */ |
| 221 | + if (pos < 0) { |
| 222 | + *p++ = _base64[(buf >> 18) & 0x3f]; |
| 223 | + *p++ = _base64[(buf >> 12) & 0x3f]; |
| 224 | + *p++ = _base64[(buf >> 6) & 0x3f]; |
| 225 | + *p++ = _base64[buf & 0x3f]; |
| 226 | + |
| 227 | + pos = 2; |
| 228 | + buf = 0; |
| 229 | + } |
| 230 | + if (p >= lend) { |
| 231 | + *p++ = '\n'; |
| 232 | + lend = p + 76; |
| 233 | + } |
| 234 | + } |
| 235 | + if (pos != 2) { |
| 236 | + *p++ = _base64[(buf >> 18) & 0x3f]; |
| 237 | + *p++ = _base64[(buf >> 12) & 0x3f]; |
| 238 | + *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '='; |
| 239 | + *p++ = '='; |
| 240 | + } |
| 241 | + |
| 242 | + return p - dst; |
| 243 | +} |
| 244 | + |
| 245 | +/* probably should use lookup table */ |
| 246 | +uint |
| 247 | +b64_decode(uint8 *src, uint len, uint8 *dst) |
| 248 | +{ |
| 249 | + char *srcend = src + len, *s = src; |
| 250 | + uint8 *p = dst; |
| 251 | + char c; |
| 252 | + uint b = 0; |
| 253 | + unsigned long buf = 0; |
| 254 | + int pos = 0, end = 0; |
| 255 | + |
| 256 | + while (s < srcend) { |
| 257 | + c = *s++; |
| 258 | + if (c >= 'A' && c <= 'Z') |
| 259 | + b = c - 'A'; |
| 260 | + else if (c >= 'a' && c <= 'z') |
| 261 | + b = c - 'a' + 26; |
| 262 | + else if (c >= '0' && c <= '9') |
| 263 | + b = c - '0' + 52; |
| 264 | + else if (c == '+') |
| 265 | + b = 62; |
| 266 | + else if (c == '/') |
| 267 | + b = 63; |
| 268 | + else if (c == '=') { |
| 269 | + /* end sequence */ |
| 270 | + if (!end) { |
| 271 | + if (pos == 2) end = 1; |
| 272 | + else if (pos == 3) end = 2; |
| 273 | + else |
| 274 | + elog(ERROR, "base64: unexpected '='"); |
| 275 | + } |
| 276 | + b = 0; |
| 277 | + } else if (c == ' ' || c == '\t' || c == '\n' || c == '\r') |
| 278 | + continue; |
| 279 | + else |
| 280 | + elog(ERROR, "base64: Invalid symbol"); |
| 281 | + |
| 282 | + /* add it to buffer */ |
| 283 | + buf = (buf << 6) + b; |
| 284 | + pos++; |
| 285 | + if (pos == 4) { |
| 286 | + *p++ = (buf >> 16) & 255; |
| 287 | + if (end == 0 || end > 1) |
| 288 | + *p++ = (buf >> 8) & 255; |
| 289 | + if (end == 0 || end > 2) |
| 290 | + *p++ = buf & 255; |
| 291 | + buf = 0; |
| 292 | + pos = 0; |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + if (pos != 0) |
| 297 | + elog(ERROR, "base64: invalid end sequence"); |
| 298 | + |
| 299 | + return p - dst; |
| 300 | +} |
| 301 | + |
| 302 | + |
| 303 | +uint |
| 304 | +hex_enc_len(uint srclen) |
| 305 | +{ |
| 306 | + return srclen << 1; |
| 307 | +} |
| 308 | + |
| 309 | +uint |
| 310 | +hex_dec_len(uint srclen) |
| 311 | +{ |
| 312 | + return srclen >> 1; |
| 313 | +} |
| 314 | + |
| 315 | +uint |
| 316 | +b64_enc_len(uint srclen) |
| 317 | +{ |
| 318 | + return srclen + (srclen / 3) + (srclen / (76 / 2)); |
| 319 | +} |
| 320 | + |
| 321 | +uint |
| 322 | +b64_dec_len(uint srclen) |
| 323 | +{ |
| 324 | + return (srclen * 3) >> 2; |
| 325 | +} |
| 326 | + |
| 327 | +static pg_coding |
| 328 | +encoding_list [] = { |
| 329 | + { "hex", hex_enc_len, hex_dec_len, hex_encode, hex_decode}, |
| 330 | + { "base64", b64_enc_len, b64_dec_len, b64_encode, b64_decode}, |
| 331 | + { NULL, NULL, NULL, NULL, NULL} |
| 332 | +}; |
| 333 | + |
| 334 | + |
| 335 | +static pg_coding * |
| 336 | +pg_find_coding(pg_coding *res, char *name) |
| 337 | +{ |
| 338 | + pg_coding *p; |
| 339 | + for (p = encoding_list; p->name; p++) { |
| 340 | + if (!strcasecmp(p->name, name)) |
| 341 | + return p; |
| 342 | + } |
| 343 | + return NULL; |
| 344 | +} |
| 345 | + |
0 commit comments