*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/encode.c,v 1.2 2001/09/14 17:46:40 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/encode.c,v 1.3 2001/09/30 22:03:41 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <ctype.h>
+
#include "utils/builtins.h"
Datum name = PG_GETARG_DATUM(1);
text *result;
char *namebuf;
- int namelen, datalen, resultlen, res;
+ int datalen, resultlen, res;
struct pg_encoding *enc;
datalen = VARSIZE(data) - VARHDRSZ;
- namelen = VARSIZE(name) - VARHDRSZ;
- namebuf = (char *)DirectFunctionCall1(textout, name);
+ namebuf = DatumGetCString(DirectFunctionCall1(textout, name));
enc = pg_find_encoding(namebuf);
if (enc == NULL)
- elog(ERROR, "No such encoding");
+ elog(ERROR, "No such encoding as '%s'", namebuf);
resultlen = enc->encode_len(VARDATA(data), datalen);
result = palloc(VARHDRSZ + resultlen);
res = enc->encode(VARDATA(data), datalen, VARDATA(result));
+
+ /* Make this FATAL 'cause we've trodden on memory ... */
if (res > resultlen)
- elog(ERROR, "Overflow - encode estimate too small");
+ elog(FATAL, "Overflow - encode estimate too small");
VARATT_SIZEP(result) = VARHDRSZ + res;
Datum name = PG_GETARG_DATUM(1);
bytea *result;
char *namebuf;
- int namelen, datalen, resultlen, res;
+ int datalen, resultlen, res;
struct pg_encoding *enc;
datalen = VARSIZE(data) - VARHDRSZ;
- namelen = VARSIZE(name) - VARHDRSZ;
- namebuf = (char *)DirectFunctionCall1(textout, name);
+ namebuf = DatumGetCString(DirectFunctionCall1(textout, name));
enc = pg_find_encoding(namebuf);
if (enc == NULL)
- elog(ERROR, "No such encoding");
+ elog(ERROR, "No such encoding as '%s'", namebuf);
resultlen = enc->decode_len(VARDATA(data), datalen);
result = palloc(VARHDRSZ + resultlen);
res = enc->decode(VARDATA(data), datalen, VARDATA(result));
+
+ /* Make this FATAL 'cause we've trodden on memory ... */
if (res > resultlen)
- elog(ERROR, "Overflow - decode estimate too small");
+ elog(FATAL, "Overflow - decode estimate too small");
VARATT_SIZEP(result) = VARHDRSZ + res;
{
const uint8 *end = src + srclen;
uint8 *rp = dst;
- int val;
int len = 0;
while (src < end)
{
if (*src == '\0')
{
- val = *src;
rp[0] = '\\';
rp[1] = '0';
rp[2] = '0';
}
else if (*src == '\\')
{
- val = *src;
rp[0] = '\\';
rp[1] = '\\';
rp += 2;
src++;
}
- *rp = '\0';
return len;
}
{
const uint8 *end = src + srclen;
uint8 *rp = dst;
- int val;
int len = 0;
while (src < end)
{
*rp++ = *src++;
}
- else if ( (src[0] == '\\') &&
+ else if ( src+3 < end &&
(src[1] >= '0' && src[1] <= '3') &&
(src[2] >= '0' && src[2] <= '7') &&
(src[3] >= '0' && src[3] <= '7') )
{
+ int val;
+
val = VAL(src[1]);
val <<= 3;
val += VAL(src[2]);
*rp++ = val + VAL(src[3]);
src += 4;
}
- else if ( (src[0] == '\\') &&
+ else if ( src+1 < end &&
(src[1] == '\\') )
{
*rp++ = '\\';
len++;
}
+
return len;
}
src++;
}
- /*
- * Allow for null terminator
- */
- len++;
-
return len;
}
{
src++;
}
- else if ( (src[0] == '\\') &&
+ else if ( src+3 < end &&
(src[1] >= '0' && src[1] <= '3') &&
(src[2] >= '0' && src[2] <= '7') &&
(src[3] >= '0' && src[3] <= '7') )
*/
src += 4;
}
- else if ( (src[0] == '\\') &&
+ else if ( src+1 < end &&
(src[1] == '\\') )
{
/*
int i;
for (i = 0; enclist[i].name; i++)
- if (!strcasecmp(enclist[i].name, name))
+ if (strcasecmp(enclist[i].name, name) == 0)
return &enclist[i].enc;
return NULL;