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
|
/*
* Some utility functions for Postgres.
*
* - Literal & ident quoting.
* - Array parsing
*/
#include <usual/pgutil.h>
#include <usual/ctype.h>
/* str -> E'str' */
bool pg_quote_literal(char *_dst, const char *_src, int dstlen)
{
char *dst = _dst;
char *end = _dst + dstlen - 2;
const char *src = _src;
bool stdquote = true;
if (dstlen < 3)
return false;
if (_src == NULL) {
if (dstlen < 5)
return false;
memcpy(_dst, "NULL", 5);
return true;
}
retry:
*dst++ = '\'';
while (*src && dst < end) {
if (*src == '\'')
*dst++ = '\'';
else if (*src == '\\') {
if (stdquote)
goto retry_ext;
*dst++ = '\\';
}
*dst++ = *src++;
}
if (*src || dst > end)
return false;
*dst++ = '\'';
*dst = 0;
return true;
retry_ext:
/* string contains '\\', retry as E'' string */
dst = _dst;
src = _src;
*dst++ = 'E';
stdquote = false;
goto retry;
}
static inline bool id_start(unsigned char c)
{
return (c >= 'a' && c <= 'z') || c == '_';
}
static inline bool id_body(unsigned char c)
{
return id_start(c) || (c >= '0' && c <= '9');
}
/* ident -> "ident" */
bool pg_quote_ident(char *_dst, const char *_src, int dstlen)
{
char *dst = _dst;
char *end = _dst + dstlen - 1;
const char *src = _src;
if (dstlen < 1)
return false;
if (!id_start(*src))
goto needs_quoting;
while (*src && dst < end) {
if (!id_body(*src))
goto needs_quoting;
*dst++ = *src++;
}
if (*src)
return false;
*dst = 0;
if (!pg_is_reserved_word(_dst))
return true;
needs_quoting:
dst = _dst;
src = _src;
end = _dst + dstlen - 2;
if (dstlen < 3)
return false;
*dst++ = '"';
while (*src && dst < end) {
if (*src == '"')
*dst++ = *src;
*dst++ = *src++;
}
if (*src)
return false;
*dst++ = '"';
*dst = 0;
return true;
}
/* schema.name -> "schema"."name" */
bool pg_quote_fqident(char *_dst, const char *_src, int dstlen)
{
const char *dot = strchr(_src, '.');
char scmbuf[128];
const char *scm;
int scmlen;
if (dot) {
scmlen = dot - _src;
if (scmlen >= (int)sizeof(scmbuf))
return false;
memcpy(scmbuf, _src, scmlen);
scmbuf[scmlen] = 0;
scm = scmbuf;
_src = dot + 1;
} else {
scm = "public";
}
if (!pg_quote_ident(_dst, scm, dstlen))
return false;
scmlen = strlen(_dst);
_dst[scmlen] = '.';
_dst += scmlen + 1;
dstlen -= scmlen + 1;
if (!pg_quote_ident(_dst, _src, dstlen))
return false;
return true;
}
/*
* pgarray parsing
*/
static bool parse_value(struct StrList *arr, const char *val, const char *vend,
CxMem *cx)
{
int len;
const char *s;
char *str, *p;
unsigned c;
while (val < vend && isspace(*val))
val++;
while (vend > val && isspace(vend[-1]))
vend--;
if (val == vend) return false;
s = val;
len = vend - val;
if (len == 4 && !strncasecmp(val, "null", len)) {
return strlist_append_ref(arr, NULL);
}
p = str = cx_alloc(cx, len + 1);
if (!str)
return false;
/* unquote & copy */
while (s < vend) {
c = *s++;
if (c == '"') {
while (1) {
c = *s++;
if (c == '"')
break;
else if (c == '\\')
*p++ = *s++;
else
*p++ = c;
}
} else if (c == '\\') {
*p++ = *s++;
} else
*p++ = c;
}
*p++ = 0;
if (!strlist_append_ref(arr, str)) {
cx_free(cx, str);
return false;
}
return true;
}
struct StrList *pg_parse_array(const char *pgarr, CxMem *cx)
{
const char *s = pgarr;
struct StrList *lst;
const char *val = NULL;
unsigned c;
/* skip dimension def "[x,y]={..}" */
if (*s == '[') {
s = strchr(s, ']');
if (!s || s[1] != '=')
return NULL;
s += 2;
}
if (*s++ != '{')
return NULL;
lst = strlist_new(cx);
if (!lst)
return NULL;
while (*s) {
/* array end */
if (s[0] == '}') {
if (s[1] != 0) {
goto failed;
}
if (val) {
if (!parse_value(lst, val, s, cx))
goto failed;
}
return lst;
}
/* cannot init earlier to support empty arrays */
if (!val)
val = s;
/* val done? */
if (*s == ',') {
if (!parse_value(lst, val, s, cx))
goto failed;
val = ++s;
continue;
}
/* scan value */
c = *s++;
if (c == '"') {
while (1) {
c = *s++;
if (c == '"')
break;
else if (c == '\\') {
if (!*s) goto failed;
s++;
} else if (!*s)
goto failed;
}
} else if (c == '\\') {
if (!*s) goto failed;
s++;
}
}
if (s[-1] != '}')
goto failed;
return lst;
failed:
strlist_free(lst);
return NULL;
}
/*
* Postgres keyword lookup.
*/
/* gperf tries ot inline a non-static function. */
#undef inline
#undef __inline
#undef __attribute__
#define inline
#define __inline
#define __attribute__(x)
#define long uintptr_t
/* include gperf code */
const char *pg_keyword_lookup_real(const char *str, unsigned int len);
#include <usual/pgutil_kwlookup.h>
bool pg_is_reserved_word(const char *str)
{
const char *kw = pg_keyword_lookup_real(str, strlen(str));
return kw != NULL;
}
|