Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7eff804

Browse files
committed
Clean up encode/decode functions a little bit.
1 parent12054ba commit7eff804

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
Datumname=PG_GETARG_DATUM(1);
3940
text*result;
4041
char*namebuf;
41-
intnamelen,datalen,resultlen,res;
42+
intdatalen,resultlen,res;
4243
structpg_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
Datumname=PG_GETARG_DATUM(1);
7072
bytea*result;
7173
char*namebuf;
72-
intnamelen,datalen,resultlen,res;
74+
intdatalen,resultlen,res;
7375
structpg_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
constuint8*end=src+srclen;
341344
uint8*rp=dst;
342-
intval;
343345
intlen=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
elseif (*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
returnlen;
376375
}
@@ -380,7 +379,6 @@ esc_decode(const uint8 *src, unsigned srclen, uint8 *dst)
380379
{
381380
constuint8*end=src+srclen;
382381
uint8*rp=dst;
383-
intval;
384382
intlen=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-
elseif((src[0]=='\\')&&
390+
elseif(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+
intval;
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-
elseif ((src[0]=='\\')&&
404+
elseif (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
returnlen;
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
returnlen;
448444
}
449445

@@ -459,7 +455,7 @@ esc_dec_len(const uint8 *src, unsigned srclen)
459455
{
460456
src++;
461457
}
462-
elseif((src[0]=='\\')&&
458+
elseif(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-
elseif ((src[0]=='\\')&&
468+
elseif (src+1<end&&
473469
(src[1]=='\\') )
474470
{
475471
/*
@@ -510,7 +506,7 @@ pg_find_encoding(const char *name)
510506
inti;
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
returnNULL;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp