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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*
$Id: quote.c,v 1.50 2006/04/20 20:55:55 turnstep Exp $
Copyright (c) 2003-2006 PostgreSQL Global Development Group
You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file.
*/
#include "Pg.h"
#include "types.h"
char * null_quote(string, len, retlen)
char *string;
STRLEN len;
STRLEN *retlen;
{
char *result;
New(0, result, len+1, char);
strncpy(result,string,len);
result[len]='\0';
*retlen = len;
return result;
}
char * quote_string(string, len, retlen)
char * string;
STRLEN len;
STRLEN * retlen;
{
char * result;
STRLEN oldlen = len;
result = string;
(*retlen) = 2;
while (len > 0 && *string != '\0') {
if (*string == '\'' || *string == '\\') {
(*retlen)++;
}
(*retlen)++;
*string++;
len--;
}
string = result;
New(0, result, 1+(*retlen), char);
*result++ = '\'';
len = oldlen;
while (len > 0 && *string != '\0') {
if (*string == '\'' || *string == '\\') {
*result++ = *string;
}
*result++ = *string++;
len--;
}
*result++ = '\'';
*result = '\0';
return result - (*retlen);
}
char * quote_geom(string, len, retlen)
char * string;
STRLEN len;
STRLEN * retlen;
{
char * result;
len = 0; /* stops compiler warnings. Remove entirely someday */
result = string;
(*retlen) = 2;
while (*string != '\0') {
if (*string !=9 && *string != 32 && *string != '(' && *string != ')'
&& *string != ',' && (*string < '0' || *string > '9'))
croak("Invalid input for geometric type");
(*retlen)++;
*string++;
}
string = result;
New(0, result, 1+(*retlen), char);
*result++ = '\'';
while (*string != '\0') {
*result++ = *string++;
}
*result++ = '\'';
*result = '\0';
return result - (*retlen);
}
char * quote_path(string, len, retlen)
char * string;
STRLEN len;
STRLEN * retlen;
{
char * result;
len = 0; /* stops compiler warnings. Remove entirely someday */
result = string;
(*retlen) = 2;
while (*string != '\0') {
if (*string !=9 && *string != 32 && *string != '(' && *string != ')'
&& *string != ',' && *string != '[' && *string != ']'
&& (*string < '0' || *string > '9'))
croak("Invalid input for geometric path type");
(*retlen)++;
*string++;
}
string = result;
New(0, result, 1+(*retlen), char);
*result++ = '\'';
while (*string != '\0') {
*result++ = *string++;
}
*result++ = '\'';
*result = '\0';
return result - (*retlen);
}
char * quote_circle(string, len, retlen)
char * string;
STRLEN len;
STRLEN * retlen;
{
char * result;
len = 0; /* stops compiler warnings. Remove entirely someday */
result = string;
(*retlen) = 2;
while (*string != '\0') {
if (*string !=9 && *string != 32 && *string != '(' && *string != ')'
&& *string != ',' && *string != '<' && *string != '>'
&& (*string < '0' || *string > '9'))
croak("Invalid input for geometric circle type");
(*retlen)++;
*string++;
}
string = result;
New(0, result, 1+(*retlen), char);
*result++ = '\'';
while (*string != '\0') {
*result++ = *string++;
}
*result++ = '\'';
*result = '\0';
return result - (*retlen);
}
char * quote_bytea(string, len, retlen)
unsigned char * string;
STRLEN len;
STRLEN * retlen;
{
unsigned char * result;
STRLEN oldlen = len;
result = string;
(*retlen) = 2;
while (len > 0) {
if (*string == '\'') {
(*retlen) += 2;
}
else if (*string == '\\') {
(*retlen) += 4;
}
else if (*string < 0x20 || *string > 0x7e) {
(*retlen) += 5;
}
else {
(*retlen)++;
}
*string++;
len--;
}
string = result;
New(0, result, 1+(*retlen), unsigned char);
*result++ = '\'';
len = oldlen;
while (len > 0) {
if (*string == '\'') { /* Single quote becomes double quotes */
*result++ = *string;
*result++ = *string++;
}
else if (*string == '\\') { /* Backslash becomes 4 backslashes */
*result++ = *string;
*result++ = *string++;
*result++ = '\\';
*result++ = '\\';
}
else if (*string < 0x20 || *string > 0x7e) {
(void) snprintf((char *)result, 6, "\\\\%03o", *string++);
result += 5;
}
else {
*result++ = *string++;
}
len--;
}
*result++ = '\'';
*result = '\0';
return (char *)result - (*retlen);
}
char * quote_sql_binary(string, len, retlen)
unsigned char *string;
STRLEN len;
STRLEN *retlen;
{
/* We are going to return a quote_bytea() for backwards compat but
we warn first */
warn("Use of SQL_BINARY invalid in quote()");
return quote_bytea(string, len, retlen);
}
char * quote_bool(value, len, retlen)
char *value;
STRLEN len;
STRLEN *retlen;
{
char *result;
long int int_value;
STRLEN max_len=6;
len = 0;
if (isDIGIT(*(char*)value)) {
/* For now -- will go away when quote* take SVs */
int_value = atoi(value);
} else {
int_value = 42; /* Not true, not false. Just is */
}
New(0, result, max_len, char);
if (0 == int_value)
strncpy(result,"FALSE\0",6);
else if (1 == int_value)
strncpy(result,"TRUE\0",5);
else
croak("Error: Bool must be either 1 or 0");
*retlen = strlen(result);
assert(*retlen+1 <= max_len);
return result;
}
char * quote_integer(value, len, retlen)
char *value;
STRLEN len;
STRLEN *retlen;
{
char *result;
STRLEN max_len=6;
len = 0;
New(0, result, max_len, char);
if (0 == *((int*)value) )
strncpy(result,"FALSE\0",6);
if (1 == *((int*)value))
strncpy(result,"TRUE\0",5);
*retlen = strlen(result);
assert(*retlen+1 <= max_len);
return result;
}
void dequote_char(string, retlen)
char *string;
STRLEN *retlen;
{
/* TODO: chop_blanks if requested */
*retlen = strlen(string);
}
void dequote_string (string, retlen)
char *string;
STRLEN *retlen;
{
*retlen = strlen(string);
}
void dequote_bytea(string, retlen)
unsigned char *string;
STRLEN *retlen;
{
unsigned char *result;
(*retlen) = 0;
if (NULL == string)
return;
result = string;
while (*string != '\0') {
(*retlen)++;
if ('\\' == *string) {
if ('\\' == *(string+1)) {
*result++ = '\\';
string +=2;
}
else if (
(*(string+1) >= '0' && *(string+1) <= '3') &&
(*(string+2) >= '0' && *(string+2) <= '7') &&
(*(string+3) >= '0' && *(string+3) <= '7'))
{
*result++ = (*(string+1)-'0')*64 + (*(string+2)-'0')*8 + (*(string+3)-'0');
string += 4;
}
else { /* Invalid escape sequence - ignore the backslash */
(*retlen)--;
*string++;
}
}
else {
*result++ = *string++;
}
}
result = '\0';
string = result - (*retlen);
return;
}
/*
This one is not used in PG, but since we have a quote_sql_binary,
it might be nice to let people go the other way too. Say when talking
to something that uses SQL_BINARY
*/
void dequote_sql_binary (string, retlen)
unsigned char *string;
STRLEN *retlen;
{
/* We are going to retun a dequote_bytea(), JIC */
warn("Use of SQL_BINARY invalid in dequote()");
dequote_bytea(string, retlen);
return;
/* Put dequote_sql_binary function here at some point */
}
void dequote_bool (string, retlen)
char *string;
STRLEN *retlen;
{
switch(*string){
case 'f': *string = '0'; break;
case 't': *string = '1'; break;
default:
croak("I do not know how to deal with %c as a bool", *string);
}
*retlen = 1;
}
void null_dequote (string, retlen)
char *string;
STRLEN *retlen;
{
*retlen = strlen(string);
}
/* end of quote.c */
|