|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * base64.c |
| 4 | + * Set of encoding and decoding routines for base64 without support |
| 5 | + * for whitespace. In case of failure, those routines return -1 in case |
| 6 | + * of error to let the caller do any error handling. |
| 7 | + * |
| 8 | + * Copyright (c) 2001-2016, PostgreSQL Global Development Group |
| 9 | + * |
| 10 | + * |
| 11 | + * IDENTIFICATION |
| 12 | + * src/common/base64.c |
| 13 | + * |
| 14 | + *------------------------------------------------------------------------- |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef FRONTEND |
| 18 | +#include "postgres.h" |
| 19 | +#else |
| 20 | +#include "postgres_fe.h" |
| 21 | +#endif |
| 22 | + |
| 23 | +#include "common/base64.h" |
| 24 | + |
| 25 | +/* |
| 26 | + * BASE64 |
| 27 | + */ |
| 28 | + |
| 29 | +static const char _base64[] = |
| 30 | +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 31 | + |
| 32 | +static const int8 b64lookup[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, 62, -1, -1, -1, 63, |
| 36 | + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, |
| 37 | + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 38 | + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
| 39 | + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 40 | + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
| 41 | +}; |
| 42 | + |
| 43 | +/* |
| 44 | + * pg_b64_encode |
| 45 | + * |
| 46 | + * Encode into base64 the given string. Returns the length of the encoded |
| 47 | + * string on success, and -1 in the event of an error. |
| 48 | + */ |
| 49 | +int |
| 50 | +pg_b64_encode(const char *src, int len, char *dst) |
| 51 | +{ |
| 52 | + char *p; |
| 53 | + const char *s, |
| 54 | + *end = src + len; |
| 55 | + int pos = 2; |
| 56 | + uint32 buf = 0; |
| 57 | + |
| 58 | + s = src; |
| 59 | + p = dst; |
| 60 | + |
| 61 | + while (s < end) |
| 62 | + { |
| 63 | + buf |= (unsigned char) *s << (pos << 3); |
| 64 | + pos--; |
| 65 | + s++; |
| 66 | + |
| 67 | + /* write it out */ |
| 68 | + if (pos < 0) |
| 69 | + { |
| 70 | + *p++ = _base64[(buf >> 18) & 0x3f]; |
| 71 | + *p++ = _base64[(buf >> 12) & 0x3f]; |
| 72 | + *p++ = _base64[(buf >> 6) & 0x3f]; |
| 73 | + *p++ = _base64[buf & 0x3f]; |
| 74 | + |
| 75 | + pos = 2; |
| 76 | + buf = 0; |
| 77 | + } |
| 78 | + } |
| 79 | + if (pos != 2) |
| 80 | + { |
| 81 | + *p++ = _base64[(buf >> 18) & 0x3f]; |
| 82 | + *p++ = _base64[(buf >> 12) & 0x3f]; |
| 83 | + *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '='; |
| 84 | + *p++ = '='; |
| 85 | + } |
| 86 | + |
| 87 | + return p - dst; |
| 88 | +} |
| 89 | + |
| 90 | +/* |
| 91 | + * pg_b64_decode |
| 92 | + * |
| 93 | + * Decode the given base64 string. Returns the length of the decoded |
| 94 | + * string on success, and -1 in the event of an error. |
| 95 | + */ |
| 96 | +int |
| 97 | +pg_b64_decode(const char *src, int len, char *dst) |
| 98 | +{ |
| 99 | + const char *srcend = src + len, |
| 100 | + *s = src; |
| 101 | + char *p = dst; |
| 102 | + char c; |
| 103 | + int b = 0; |
| 104 | + uint32 buf = 0; |
| 105 | + int pos = 0, |
| 106 | + end = 0; |
| 107 | + |
| 108 | + while (s < srcend) |
| 109 | + { |
| 110 | + c = *s++; |
| 111 | + |
| 112 | + /* Leave if a whitespace is found */ |
| 113 | + if (c == ' ' || c == '\t' || c == '\n' || c == '\r') |
| 114 | + return -1; |
| 115 | + |
| 116 | + if (c == '=') |
| 117 | + { |
| 118 | + /* end sequence */ |
| 119 | + if (!end) |
| 120 | + { |
| 121 | + if (pos == 2) |
| 122 | + end = 1; |
| 123 | + else if (pos == 3) |
| 124 | + end = 2; |
| 125 | + else |
| 126 | + { |
| 127 | + /* |
| 128 | + * Unexpected "=" character found while decoding base64 |
| 129 | + * sequence. |
| 130 | + */ |
| 131 | + return -1; |
| 132 | + } |
| 133 | + } |
| 134 | + b = 0; |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + b = -1; |
| 139 | + if (c > 0 && c < 127) |
| 140 | + b = b64lookup[(unsigned char) c]; |
| 141 | + if (b < 0) |
| 142 | + { |
| 143 | + /* invalid symbol found */ |
| 144 | + return -1; |
| 145 | + } |
| 146 | + } |
| 147 | + /* add it to buffer */ |
| 148 | + buf = (buf << 6) + b; |
| 149 | + pos++; |
| 150 | + if (pos == 4) |
| 151 | + { |
| 152 | + *p++ = (buf >> 16) & 255; |
| 153 | + if (end == 0 || end > 1) |
| 154 | + *p++ = (buf >> 8) & 255; |
| 155 | + if (end == 0 || end > 2) |
| 156 | + *p++ = buf & 255; |
| 157 | + buf = 0; |
| 158 | + pos = 0; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if (pos != 0) |
| 163 | + { |
| 164 | + /* |
| 165 | + * base64 end sequence is invalid. Input data is missing padding, |
| 166 | + * is truncated or is otherwise corrupted. |
| 167 | + */ |
| 168 | + return -1; |
| 169 | + } |
| 170 | + |
| 171 | + return p - dst; |
| 172 | +} |
| 173 | + |
| 174 | +/* |
| 175 | + * pg_b64_enc_len |
| 176 | + * |
| 177 | + * Returns to caller the length of the string if it were encoded with |
| 178 | + * base64 based on the length provided by caller. This is useful to |
| 179 | + * estimate how large a buffer allocation needs to be done before doing |
| 180 | + * the actual encoding. |
| 181 | + */ |
| 182 | +int |
| 183 | +pg_b64_enc_len(int srclen) |
| 184 | +{ |
| 185 | + /* 3 bytes will be converted to 4 */ |
| 186 | + return (srclen + 2) * 4 / 3; |
| 187 | +} |
| 188 | + |
| 189 | +/* |
| 190 | + * pg_b64_dec_len |
| 191 | + * |
| 192 | + * Returns to caller the length of the string if it were to be decoded |
| 193 | + * with base64, based on the length given by caller. This is useful to |
| 194 | + * estimate how large a buffer allocation needs to be done before doing |
| 195 | + * the actual decoding. |
| 196 | + */ |
| 197 | +int |
| 198 | +pg_b64_dec_len(int srclen) |
| 199 | +{ |
| 200 | + return (srclen * 3) >> 2; |
| 201 | +} |
0 commit comments