Skip to content

Commit 7eff804

Browse files
committed
Clean up encode/decode functions a little bit.
1 parent 12054ba commit 7eff804

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

src/backend/utils/adt/encode.c

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/encode.c,v 1.2 2001/09/14 17:46:40 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/encode.c,v 1.3 2001/09/30 22:03:41 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include "postgres.h"
1515

1616
#include <ctype.h>
17+
1718
#include "utils/builtins.h"
1819

1920

@@ -38,24 +39,25 @@ binary_encode(PG_FUNCTION_ARGS)
3839
Datum name = PG_GETARG_DATUM(1);
3940
text *result;
4041
char *namebuf;
41-
int namelen, datalen, resultlen, res;
42+
int datalen, resultlen, res;
4243
struct pg_encoding *enc;
4344

4445
datalen = VARSIZE(data) - VARHDRSZ;
45-
namelen = VARSIZE(name) - VARHDRSZ;
4646

47-
namebuf = (char *)DirectFunctionCall1(textout, name);
47+
namebuf = DatumGetCString(DirectFunctionCall1(textout, name));
4848

4949
enc = pg_find_encoding(namebuf);
5050
if (enc == NULL)
51-
elog(ERROR, "No such encoding");
51+
elog(ERROR, "No such encoding as '%s'", namebuf);
5252

5353
resultlen = enc->encode_len(VARDATA(data), datalen);
5454
result = palloc(VARHDRSZ + resultlen);
5555

5656
res = enc->encode(VARDATA(data), datalen, VARDATA(result));
57+
58+
/* Make this FATAL 'cause we've trodden on memory ... */
5759
if (res > resultlen)
58-
elog(ERROR, "Overflow - encode estimate too small");
60+
elog(FATAL, "Overflow - encode estimate too small");
5961

6062
VARATT_SIZEP(result) = VARHDRSZ + res;
6163

@@ -69,24 +71,25 @@ binary_decode(PG_FUNCTION_ARGS)
6971
Datum name = PG_GETARG_DATUM(1);
7072
bytea *result;
7173
char *namebuf;
72-
int namelen, datalen, resultlen, res;
74+
int datalen, resultlen, res;
7375
struct pg_encoding *enc;
7476

7577
datalen = VARSIZE(data) - VARHDRSZ;
76-
namelen = VARSIZE(name) - VARHDRSZ;
7778

78-
namebuf = (char *)DirectFunctionCall1(textout, name);
79+
namebuf = DatumGetCString(DirectFunctionCall1(textout, name));
7980

8081
enc = pg_find_encoding(namebuf);
8182
if (enc == NULL)
82-
elog(ERROR, "No such encoding");
83+
elog(ERROR, "No such encoding as '%s'", namebuf);
8384

8485
resultlen = enc->decode_len(VARDATA(data), datalen);
8586
result = palloc(VARHDRSZ + resultlen);
8687

8788
res = enc->decode(VARDATA(data), datalen, VARDATA(result));
89+
90+
/* Make this FATAL 'cause we've trodden on memory ... */
8891
if (res > resultlen)
89-
elog(ERROR, "Overflow - decode estimate too small");
92+
elog(FATAL, "Overflow - decode estimate too small");
9093

9194
VARATT_SIZEP(result) = VARHDRSZ + res;
9295

@@ -339,14 +342,12 @@ esc_encode(const uint8 *src, unsigned srclen, uint8 *dst)
339342
{
340343
const uint8 *end = src + srclen;
341344
uint8 *rp = dst;
342-
int val;
343345
int len = 0;
344346

345347
while (src < end)
346348
{
347349
if (*src == '\0')
348350
{
349-
val = *src;
350351
rp[0] = '\\';
351352
rp[1] = '0';
352353
rp[2] = '0';
@@ -356,7 +357,6 @@ esc_encode(const uint8 *src, unsigned srclen, uint8 *dst)
356357
}
357358
else if (*src == '\\')
358359
{
359-
val = *src;
360360
rp[0] = '\\';
361361
rp[1] = '\\';
362362
rp += 2;
@@ -370,7 +370,6 @@ esc_encode(const uint8 *src, unsigned srclen, uint8 *dst)
370370

371371
src++;
372372
}
373-
*rp = '\0';
374373

375374
return len;
376375
}
@@ -380,7 +379,6 @@ esc_decode(const uint8 *src, unsigned srclen, uint8 *dst)
380379
{
381380
const uint8 *end = src + srclen;
382381
uint8 *rp = dst;
383-
int val;
384382
int len = 0;
385383

386384
while (src < end)
@@ -389,19 +387,21 @@ esc_decode(const uint8 *src, unsigned srclen, uint8 *dst)
389387
{
390388
*rp++ = *src++;
391389
}
392-
else if ( (src[0] == '\\') &&
390+
else if ( src+3 < end &&
393391
(src[1] >= '0' && src[1] <= '3') &&
394392
(src[2] >= '0' && src[2] <= '7') &&
395393
(src[3] >= '0' && src[3] <= '7') )
396394
{
395+
int val;
396+
397397
val = VAL(src[1]);
398398
val <<= 3;
399399
val += VAL(src[2]);
400400
val <<= 3;
401401
*rp++ = val + VAL(src[3]);
402402
src += 4;
403403
}
404-
else if ( (src[0] == '\\') &&
404+
else if ( src+1 < end &&
405405
(src[1] == '\\') )
406406
{
407407
*rp++ = '\\';
@@ -418,6 +418,7 @@ esc_decode(const uint8 *src, unsigned srclen, uint8 *dst)
418418

419419
len++;
420420
}
421+
421422
return len;
422423
}
423424

@@ -439,11 +440,6 @@ esc_enc_len(const uint8 *src, unsigned srclen)
439440
src++;
440441
}
441442

442-
/*
443-
* Allow for null terminator
444-
*/
445-
len++;
446-
447443
return len;
448444
}
449445

@@ -459,7 +455,7 @@ esc_dec_len(const uint8 *src, unsigned srclen)
459455
{
460456
src++;
461457
}
462-
else if ( (src[0] == '\\') &&
458+
else if ( src+3 < end &&
463459
(src[1] >= '0' && src[1] <= '3') &&
464460
(src[2] >= '0' && src[2] <= '7') &&
465461
(src[3] >= '0' && src[3] <= '7') )
@@ -469,7 +465,7 @@ esc_dec_len(const uint8 *src, unsigned srclen)
469465
*/
470466
src += 4;
471467
}
472-
else if ( (src[0] == '\\') &&
468+
else if ( src+1 < end &&
473469
(src[1] == '\\') )
474470
{
475471
/*
@@ -510,7 +506,7 @@ pg_find_encoding(const char *name)
510506
int i;
511507

512508
for (i = 0; enclist[i].name; i++)
513-
if (!strcasecmp(enclist[i].name, name))
509+
if (strcasecmp(enclist[i].name, name) == 0)
514510
return &enclist[i].enc;
515511

516512
return NULL;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy