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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
/*-------------------------------------------------------------------------
*
* varlena.c--
* Functions for the variable-length built-in types.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.1.1.1 1996/07/09 06:22:06 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
#include <ctype.h>
#include <string.h>
#include "postgres.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "utils/builtins.h" /* where function declarations go */
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
#define VAL(CH) ((CH) - '0')
#define DIG(VAL) ((VAL) + '0')
/*
* byteain - converts from printable representation of byte array
*
* Non-printable characters must be passed as '\nnn' (octal) and are
* converted to internal form. '\' must be passed as '\\'.
* elog(WARN, ...) if bad form.
*
* BUGS:
* The input is scaned twice.
* The error checking of input is minimal.
*/
struct varlena *
byteain(char *inputText)
{
register char *tp;
register char *rp;
register int byte;
struct varlena *result;
if (inputText == NULL)
elog(WARN, "Bad input string for type bytea");
for (byte = 0, tp = inputText; *tp != '\0'; byte++)
if (*tp++ == '\\')
{
if (*tp == '\\')
tp++;
else if (!isdigit(*tp++) ||
!isdigit(*tp++) ||
!isdigit(*tp++))
elog(WARN, "Bad input string for type bytea");
}
tp = inputText;
byte += sizeof(int32); /* varlena? */
result = (struct varlena *) palloc(byte);
result->vl_len = byte; /* varlena? */
rp = result->vl_dat;
while (*tp != '\0')
if (*tp != '\\' || *++tp == '\\')
*rp++ = *tp++;
else {
byte = VAL(*tp++);
byte <<= 3;
byte += VAL(*tp++);
byte <<= 3;
*rp++ = byte + VAL(*tp++);
}
return(result);
}
/*
* Shoves a bunch of memory pointed at by bytes into varlena.
* BUGS: Extremely unportable as things shoved can be string
* representations of structs, etc.
*/
struct varlena *
shove_bytes(unsigned char *stuff, int len)
{
struct varlena *result;
result = (struct varlena *) palloc(len + sizeof(int32));
result->vl_len = len;
memmove(result->vl_dat,
stuff + sizeof(int32),
len - sizeof(int32));
return(result);
}
/*
* byteaout - converts to printable representation of byte array
*
* Non-printable characters are inserted as '\nnn' (octal) and '\' as
* '\\'.
*
* NULL vlena should be an error--returning string with NULL for now.
*/
char *
byteaout(struct varlena *vlena)
{
register char *vp;
register char *rp;
register int val; /* holds unprintable chars */
int i;
int len;
static char *result;
if (vlena == NULL) {
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
return(result);
}
vp = vlena->vl_dat;
len = 1; /* empty string has 1 char */
for (i = vlena->vl_len - sizeof(int32); i != 0; i--, vp++) /* varlena? */
if (*vp == '\\')
len += 2;
else if (isascii(*vp) && isprint(*vp))
len++;
else
len += 4;
rp = result = (char *) palloc(len);
vp = vlena->vl_dat;
for (i = vlena->vl_len - sizeof(int32); i != 0; i--) /* varlena? */
if (*vp == '\\') {
*vp++;
*rp++ = '\\';
*rp++ = '\\';
} else if (isascii(*vp) && isprint(*vp))
*rp++ = *vp++;
else {
val = *vp++;
*rp = '\\';
rp += 3;
*rp-- = DIG(val & 07);
val >>= 3;
*rp-- = DIG(val & 07);
val >>= 3;
*rp = DIG(val & 03);
rp += 3;
}
*rp = '\0';
return(result);
}
/*
* textin - converts "..." to internal representation
*/
struct varlena *
textin(char *inputText)
{
struct varlena *result;
int len;
if (inputText == NULL)
return(NULL);
len = strlen(inputText) + VARHDRSZ;
result = (struct varlena *) palloc(len);
VARSIZE(result) = len;
memmove(VARDATA(result), inputText, len - VARHDRSZ);
return(result);
}
/*
* textout - converts internal representation to "..."
*/
char *
textout(struct varlena *vlena)
{
int len;
char *result;
if (vlena == NULL) {
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
return(result);
}
len = VARSIZE(vlena) - VARHDRSZ;
result = (char *) palloc(len + 1);
memmove(result, VARDATA(vlena), len);
result[len] = '\0';
return(result);
}
/* ========== PUBLIC ROUTINES ========== */
/*
* textcat -
* takes two text* and returns a text* that is the concatentation of
* the two
*/
text*
textcat(text* t1, text* t2)
{
int newlen;
char *str1, *str2;
text* result;
if (t1 == NULL) return t2;
if (t2 == NULL) return t1;
/* since t1, and t2 are non-null, str1 and str2 must also be non-null */
str1 = textout(t1);
str2 = textout(t2);
/* we use strlen here to calculate the length because the size fields
of t1, t2 may be longer than necessary to hold the string */
newlen = strlen(str1) + strlen(str2) + VARHDRSZ;
result = (text*)palloc(newlen);
strcpy(VARDATA(result), str1);
strncat(VARDATA(result), str2, newlen - VARHDRSZ);
/* [TRH] Was:
strcat(VARDATA(result), str2);
which may corrupt the malloc arena due to writing trailing \0. */
pfree(str1);
pfree(str2);
return result;
}
/*
* texteq - returns 1 iff arguments are equal
* textne - returns 1 iff arguments are not equal
*/
int32
texteq(struct varlena *arg1, struct varlena *arg2)
{
register int len;
register char *a1p, *a2p;
if (arg1 == NULL || arg2 == NULL)
return((int32) NULL);
if ((len = arg1->vl_len) != arg2->vl_len)
return((int32) 0);
a1p = arg1->vl_dat;
a2p = arg2->vl_dat;
/*
* Varlenas are stored as the total size (data + size variable)
* followed by the data. The size variable is an int32 so the
* length of the data is the total length less sizeof(int32)
*/
len -= sizeof(int32);
while (len-- != 0)
if (*a1p++ != *a2p++)
return((int32) 0);
return((int32) 1);
}
int32
textne(struct varlena *arg1, struct varlena *arg2)
{
return((int32) !texteq(arg1, arg2));
}
int32
text_lt(struct varlena *arg1, struct varlena *arg2)
{
int len;
char *a1p, *a2p;
if (arg1 == NULL || arg2 == NULL)
return((int32) 0);
a1p = VARDATA(arg1);
a2p = VARDATA(arg2);
if ((len = arg1->vl_len) > arg2->vl_len)
len = arg2->vl_len;
len -= sizeof(int32);
while (len != 0 && *a1p == *a2p)
{
a1p++;
a2p++;
len--;
}
if (len)
return (int32) (*a1p < *a2p);
else
return (int32) (arg1->vl_len < arg2->vl_len);
}
int32
text_le(struct varlena *arg1, struct varlena *arg2)
{
int len;
char *a1p, *a2p;
if (arg1 == NULL || arg2 == NULL)
return((int32) 0);
a1p = VARDATA(arg1);
a2p = VARDATA(arg2);
if ((len = arg1->vl_len) > arg2->vl_len)
len = arg2->vl_len;
len -= sizeof(int32); /* varlena! */
while (len != 0 && *a1p == *a2p)
{
a1p++;
a2p++;
len--;
}
if (len)
return (int32) (*a1p < *a2p);
else
return ((int32) VARSIZE(arg1) <= VARSIZE(arg2));
}
int32
text_gt(struct varlena *arg1, struct varlena *arg2)
{
return ((int32) !text_le(arg1, arg2));
}
int32
text_ge(struct varlena *arg1, struct varlena *arg2)
{
return ((int32) !text_lt(arg1, arg2));
}
/*-------------------------------------------------------------
* byteaGetSize
*
* get the number of bytes contained in an instance of type 'bytea'
*-------------------------------------------------------------
*/
int32
byteaGetSize(struct varlena *v)
{
register int len;
len = v->vl_len - sizeof(v->vl_len);
return(len);
}
/*-------------------------------------------------------------
* byteaGetByte
*
* this routine treats "bytea" as an array of bytes.
* It returns the Nth byte (a number between 0 and 255) or
* it dies if the length of this array is less than n.
*-------------------------------------------------------------
*/
int32
byteaGetByte(struct varlena *v, int32 n)
{
int len;
int byte;
len = byteaGetSize(v);
if (n>=len) {
elog(WARN, "byteaGetByte: index (=%d) out of range [0..%d]",
n,len-1);
}
byte = (unsigned char) (v->vl_dat[n]);
return((int32) byte);
}
/*-------------------------------------------------------------
* byteaGetBit
*
* This routine treats a "bytea" type like an array of bits.
* It returns the value of the Nth bit (0 or 1).
* If 'n' is out of range, it dies!
*
*-------------------------------------------------------------
*/
int32
byteaGetBit(struct varlena *v, int32 n)
{
int byteNo, bitNo;
int byte;
byteNo = n/8;
bitNo = n%8;
byte = byteaGetByte(v, byteNo);
if (byte & (1<<bitNo)) {
return((int32)1);
} else {
return((int32)0);
}
}
/*-------------------------------------------------------------
* byteaSetByte
*
* Given an instance of type 'bytea' creates a new one with
* the Nth byte set to the given value.
*
*-------------------------------------------------------------
*/
struct varlena *
byteaSetByte(struct varlena *v, int32 n, int32 newByte)
{
int len;
struct varlena *res;
len = byteaGetSize(v);
if (n>=len) {
elog(WARN,
"byteaSetByte: index (=%d) out of range [0..%d]",
n, len-1);
}
/*
* Make a copy of the original varlena.
*/
res = (struct varlena *) palloc(VARSIZE(v));
if (res==NULL) {
elog(WARN, "byteaSetByte: Out of memory (%d bytes requested)",
VARSIZE(v));
}
memmove((char *)res, (char *)v, VARSIZE(v));
/*
* Now set the byte.
*/
res->vl_dat[n] = newByte;
return(res);
}
/*-------------------------------------------------------------
* byteaSetBit
*
* Given an instance of type 'bytea' creates a new one with
* the Nth bit set to the given value.
*
*-------------------------------------------------------------
*/
struct varlena *
byteaSetBit(struct varlena *v, int32 n, int32 newBit)
{
struct varlena *res;
int oldByte, newByte;
int byteNo, bitNo;
/*
* sanity check!
*/
if (newBit != 0 && newBit != 1) {
elog(WARN, "byteaSetByte: new bit must be 0 or 1");
}
/*
* get the byte where the bit we want is stored.
*/
byteNo = n / 8;
bitNo = n % 8;
oldByte = byteaGetByte(v, byteNo);
/*
* calculate the new value for that byte
*/
if (newBit == 0) {
newByte = oldByte & (~(1<<bitNo));
} else {
newByte = oldByte | (1<<bitNo);
}
/*
* NOTE: 'byteaSetByte' creates a copy of 'v' & sets the byte.
*/
res = byteaSetByte(v, byteNo, newByte);
return(res);
}
|