1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
#include <postgres.h>
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "parser/keywords.h"
#if 1
#define talloc(len) malloc(len)
#define trealloc(p, len) realloc(p, len)
#define tfree(p) free(p)
#else
#define talloc(len) palloc(len)
#define trealloc(p, len) repalloc(p, len)
#define tfree(p) pfree(p)
#endif
#include "textbuf.h"
#ifndef SET_VARSIZE
#define SET_VARSIZE(x, len) VARATT_SIZEP(x) = (len)
#endif
struct TBuf {
text *data;
int size;
};
static void request_avail(TBuf *tbuf, int len)
{
int newlen = tbuf->size;
int need = VARSIZE(tbuf->data) + len;
if (need < newlen)
return;
while (need > newlen)
newlen *= 2;
tbuf->data = trealloc(tbuf->data, newlen);
tbuf->size = newlen;
}
static inline char *get_endp(TBuf *tbuf)
{
char *p = VARDATA(tbuf->data);
int len = VARSIZE(tbuf->data) - VARHDRSZ;
return p + len;
}
static inline void inc_used(TBuf *tbuf, int len)
{
SET_VARSIZE(tbuf->data, VARSIZE(tbuf->data) + len);
}
static void tbuf_init(TBuf *tbuf, int start_size)
{
if (start_size < VARHDRSZ)
start_size = VARHDRSZ;
tbuf->data = talloc(start_size);
tbuf->size = start_size;
SET_VARSIZE(tbuf->data, VARHDRSZ);
}
TBuf *tbuf_alloc(int start_size)
{
TBuf *res;
res = talloc(sizeof(TBuf));
tbuf_init(res, start_size);
return res;
}
void tbuf_free(TBuf *tbuf)
{
if (tbuf->data)
tfree(tbuf->data);
tfree(tbuf);
}
int tbuf_get_size(TBuf *tbuf)
{
return VARSIZE(tbuf->data) - VARHDRSZ;
}
void tbuf_reset(TBuf *tbuf)
{
SET_VARSIZE(tbuf->data, VARHDRSZ);
}
const text *tbuf_look_text(TBuf *tbuf)
{
return tbuf->data;
}
const char *tbuf_look_cstring(TBuf *tbuf)
{
char *p;
request_avail(tbuf, 1);
p = get_endp(tbuf);
*p = 0;
return VARDATA(tbuf->data);
}
void tbuf_append_cstring(TBuf *tbuf, const char *str)
{
int len = strlen(str);
request_avail(tbuf, len);
memcpy(get_endp(tbuf), str, len);
inc_used(tbuf, len);
}
void tbuf_append_text(TBuf *tbuf, const text *str)
{
int len = VARSIZE(str) - VARHDRSZ;
request_avail(tbuf, len);
memcpy(get_endp(tbuf), VARDATA(str), len);
inc_used(tbuf, len);
}
void tbuf_append_char(TBuf *tbuf, char chr)
{
char *p;
request_avail(tbuf, 1);
p = get_endp(tbuf);
*p = chr;
inc_used(tbuf, 1);
}
text *tbuf_steal_text(TBuf *tbuf)
{
text *data = tbuf->data;
tbuf->data = NULL;
return data;
}
static const char b64tbl[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static int b64encode(char *dst, const uint8 *src, int srclen)
{
char *p = dst;
const uint8 *s = src, *end = src + srclen;
int pos = 2;
uint32 buf = 0;
while (s < end) {
buf |= (unsigned char) *s << (pos << 3);
pos--;
s++;
/* write it out */
if (pos < 0) {
*p++ = b64tbl[ (buf >> 18) & 0x3f ];
*p++ = b64tbl[ (buf >> 12) & 0x3f ];
*p++ = b64tbl[ (buf >> 6) & 0x3f ];
*p++ = b64tbl[ buf & 0x3f ];
pos = 2;
buf = 0;
}
}
if (pos != 2) {
*p++ = b64tbl[ (buf >> 18) & 0x3f ];
*p++ = b64tbl[ (buf >> 12) & 0x3f ];
*p++ = (pos == 0) ? b64tbl[ (buf >> 6) & 0x3f ] : '=';
*p++ = '=';
}
return p - dst;
}
static const char hextbl[] = "0123456789abcdef";
static int urlencode(char *dst, const uint8 *src, int srclen)
{
const uint8 *end = src + srclen;
char *p = dst;
while (src < end) {
if (*src == '=')
*p++ = '+';
else if ((*src >= '0' && *src <= '9')
|| (*src >= 'A' && *src <= 'Z')
|| (*src >= 'a' && *src <= 'z'))
*p++ = *src;
else {
*p++ = '%';
*p++ = hextbl[*src >> 4];
*p++ = hextbl[*src & 15];
}
}
return p - dst;
}
static int quote_literal(char *dst, const uint8 *src, int srclen)
{
const uint8 *cp1;
char *cp2;
int wl;
cp1 = src;
cp2 = dst;
*cp2++ = '\'';
while (srclen > 0)
{
if ((wl = pg_mblen((const char *)cp1)) != 1)
{
srclen -= wl;
while (wl-- > 0)
*cp2++ = *cp1++;
continue;
}
if (*cp1 == '\'')
*cp2++ = '\'';
if (*cp1 == '\\')
*cp2++ = '\\';
*cp2++ = *cp1++;
srclen--;
}
*cp2++ = '\'';
return cp2 - dst;
}
/*
* slon_quote_identifier - Quote an identifier only if needed
*
* When quotes are needed, we palloc the required space; slightly
* space-wasteful but well worth it for notational simplicity.
*
* Version: pgsql/src/backend/utils/adt/ruleutils.c,v 1.188 2005/01/13 17:19:10
*/
static int
quote_ident(char *dst, const uint8 *src, int srclen)
{
/*
* Can avoid quoting if ident starts with a lowercase letter or
* underscore and contains only lowercase letters, digits, and
* underscores, *and* is not any SQL keyword. Otherwise, supply
* quotes.
*/
int nquotes = 0;
bool safe;
const char *ptr;
char *optr;
char ident[NAMEDATALEN + 1];
/* expect idents be not bigger than NAMEDATALEN */
if (srclen > NAMEDATALEN)
srclen = NAMEDATALEN;
memcpy(ident, src, srclen);
ident[srclen] = 0;
/*
* would like to use <ctype.h> macros here, but they might yield
* unwanted locale-specific results...
*/
safe = ((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_');
for (ptr = ident; *ptr; ptr++)
{
char ch = *ptr;
if ((ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') ||
(ch == '_'))
continue; /* okay */
safe = false;
if (ch == '"')
nquotes++;
}
if (safe)
{
/*
* Check for keyword. This test is overly strong, since many of
* the "keywords" known to the parser are usable as column names,
* but the parser doesn't provide any easy way to test for whether
* an identifier is safe or not... so be safe not sorry.
*
* Note: ScanKeywordLookup() does case-insensitive comparison, but
* that's fine, since we already know we have all-lower-case.
*/
if (ScanKeywordLookup(ident) != NULL)
safe = false;
}
optr = dst;
if (!safe)
*optr++ = '"';
for (ptr = ident; *ptr; ptr++)
{
char ch = *ptr;
if (ch == '"')
*optr++ = '"';
*optr++ = ch;
}
if (!safe)
*optr++ = '"';
return optr - dst;
}
void tbuf_encode_cstring(TBuf *tbuf,
const char *str,
const char *encoding)
{
if (str == NULL)
elog(ERROR, "tbuf_encode_cstring: NULL");
tbuf_encode_data(tbuf, (const uint8 *)str, strlen(str), encoding);
}
void tbuf_encode_data(TBuf *tbuf,
const uint8 *data, int len,
const char *encoding)
{
int dlen = 0;
char *dst;
if (strcmp(encoding, "url") == 0) {
request_avail(tbuf, len*3);
dst = get_endp(tbuf);
dlen = urlencode(dst, data, len);
} else if (strcmp(encoding, "base64") == 0) {
request_avail(tbuf, (len + 2) * 4 / 3);
dst = get_endp(tbuf);
dlen = b64encode(dst, data, len);
} else if (strcmp(encoding, "quote_literal") == 0) {
request_avail(tbuf, len * 2 + 2);
dst = get_endp(tbuf);
dlen = quote_literal(dst, data, len);
} else if (strcmp(encoding, "quote_ident") == 0) {
request_avail(tbuf, len * 2 + 2);
dst = get_endp(tbuf);
dlen = quote_ident(dst, data, len);
} else
elog(ERROR, "bad encoding");
inc_used(tbuf, dlen);
}
|