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
|
/*
* stringutil.c - some tools for string handling
*
* Copyright (c) 2007 Marko Kreen, Skype Technologies OÜ
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <postgres.h>
#include <lib/stringinfo.h>
#include <utils/memutils.h>
#include <utils/builtins.h>
#if PG_VERSION_NUM >= 90200
#include <utils/json.h>
#endif
#if PG_VERSION_NUM >= 90300
#include <access/htup_details.h>
#endif
#if PG_VERSION_NUM >= 160000
#include <varatt.h>
#endif
#include "stringutil.h"
#ifndef SET_VARSIZE
#define SET_VARSIZE(x, len) VARATT_SIZEP(x) = len
#endif
#if PG_VERSION_NUM < 90100
static char *quote_literal_cstr(const char *str)
{
StringInfoData buf;
initStringInfo(&buf);
if (strchr(str, '\\'))
appendStringInfoCharMacro(&buf, 'E');
appendStringInfoCharMacro(&buf, '\'');
for (; *str; str++) {
if (*str == '\'' || *str == '\\')
appendStringInfoCharMacro(&buf, *str);
appendStringInfoCharMacro(&buf, *str);
}
appendStringInfoCharMacro(&buf, '\'');
return buf.data;
}
#endif
StringInfo pgq_init_varbuf(void)
{
StringInfo buf;
buf = makeStringInfo();
appendStringInfoString(buf, "XXXX");
return buf;
}
Datum pgq_finish_varbuf(StringInfo buf)
{
if (!buf) return (Datum)0;
SET_VARSIZE(buf->data, buf->len);
return PointerGetDatum(buf->data);
}
/*
* Find a string in comma-separated list.
*
* It does not support space inside tokens.
*/
bool pgq_strlist_contains(const char *liststr, const char *str)
{
int c, len = strlen(str);
const char *p, *listpos = liststr;
loop:
/* find string fragment, later check if actual token */
p = strstr(listpos, str);
if (p == NULL)
return false;
/* move listpos further */
listpos = p + len;
/* survive len=0 and avoid unnecessary compare */
if (*listpos)
listpos++;
/* check previous symbol */
if (p > liststr) {
c = *(p - 1);
if (!isspace(c) && c != ',')
goto loop;
}
/* check following symbol */
c = p[len];
if (c != 0 && !isspace(c) && c != ',')
goto loop;
return true;
}
/*
* quoting
*/
static void pgq_urlencode(StringInfo buf, const char *src)
{
static const char hextbl[] = "0123456789abcdef";
while (*src) {
unsigned c = (unsigned char)*src++;
if (c == ' ') {
appendStringInfoCharMacro(buf, '+');
} else if ((c >= '0' && c <= '9')
|| (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| c == '_' || c == '.' || c == '-') {
appendStringInfoCharMacro(buf, c);
} else {
appendStringInfoCharMacro(buf, '%');
appendStringInfoCharMacro(buf, hextbl[c >> 4]);
appendStringInfoCharMacro(buf, hextbl[c & 15]);
}
}
}
static void pgq_quote_literal(StringInfo buf, const char *src)
{
const char *quoted = quote_literal_cstr(src);
appendStringInfoString(buf, quoted);
pfree((char*)quoted);
}
/*
* pgq_quote_ident - Quote an identifier only if needed
*/
static void pgq_quote_ident(StringInfo buf, const char *src)
{
const char *quoted = quote_identifier(src);
appendStringInfoString(buf, quoted);
if (quoted != src)
pfree((char *)quoted);
}
#if PG_VERSION_NUM < 90200
static void escape_json(StringInfo buf, const char *p)
{
appendStringInfoCharMacro(buf, '\"');
for (; *p; p++) {
switch (*p) {
case '\b': appendStringInfoString(buf, "\\b"); break;
case '\f': appendStringInfoString(buf, "\\f"); break;
case '\n': appendStringInfoString(buf, "\\n"); break;
case '\r': appendStringInfoString(buf, "\\r"); break;
case '\t': appendStringInfoString(buf, "\\t"); break;
case '"': appendStringInfoString(buf, "\\\""); break;
case '\\': appendStringInfoString(buf, "\\\\"); break;
default:
if ((unsigned char) *p < ' ')
appendStringInfo(buf, "\\u%04x", (int) *p);
else
appendStringInfoCharMacro(buf, *p);
break;
}
}
appendStringInfoCharMacro(buf, '\"');
}
#endif
void pgq_encode_cstring(StringInfo tbuf, const char *str, enum PgqEncode encoding)
{
if (str == NULL)
elog(ERROR, "tbuf_encode_cstring: NULL");
switch (encoding) {
case TBUF_QUOTE_LITERAL:
pgq_quote_literal(tbuf, str);
break;
case TBUF_QUOTE_IDENT:
pgq_quote_ident(tbuf, str);
break;
case TBUF_QUOTE_URLENC:
pgq_urlencode(tbuf, str);
break;
case TBUF_QUOTE_JSON:
escape_json(tbuf, str);
break;
default:
elog(ERROR, "bad encoding");
}
}
|