Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Functions | Variables

encode.c File Reference

#include "postgres.h"
#include <ctype.h>
#include "utils/builtins.h"
Include dependency graph for encode.c:

Go to the source code of this file.

Data Structures

struct  pg_encoding

Defines

#define VAL(CH)   ((CH) - '0')
#define DIG(VAL)   ((VAL) + '0')

Functions

static struct pg_encodingpg_find_encoding (const char *name)
Datum binary_encode (PG_FUNCTION_ARGS)
Datum binary_decode (PG_FUNCTION_ARGS)
unsigned hex_encode (const char *src, unsigned len, char *dst)
static char get_hex (char c)
unsigned hex_decode (const char *src, unsigned len, char *dst)
static unsigned hex_enc_len (const char *src, unsigned srclen)
static unsigned hex_dec_len (const char *src, unsigned srclen)
static unsigned b64_encode (const char *src, unsigned len, char *dst)
static unsigned b64_decode (const char *src, unsigned len, char *dst)
static unsigned b64_enc_len (const char *src, unsigned srclen)
static unsigned b64_dec_len (const char *src, unsigned srclen)
static unsigned esc_encode (const char *src, unsigned srclen, char *dst)
static unsigned esc_decode (const char *src, unsigned srclen, char *dst)
static unsigned esc_enc_len (const char *src, unsigned srclen)
static unsigned esc_dec_len (const char *src, unsigned srclen)

Variables

static const char hextbl [] = "0123456789abcdef"
static const int8 hexlookup [128]
static const char _base64 [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
static const int8 b64lookup [128]
struct {
   const char *   name
   struct pg_encoding   enc
enclist []

Define Documentation

#define DIG (   VAL  )     ((VAL) + '0')

Definition at line 361 of file encode.c.

Referenced by esc_encode().

#define VAL (   CH  )     ((CH) - '0')

Definition at line 360 of file encode.c.

Referenced by esc_decode().


Function Documentation

static unsigned b64_dec_len ( const char *  src,
unsigned  srclen 
) [static]

Definition at line 341 of file encode.c.

{
    return (srclen * 3) >> 2;
}

static unsigned b64_decode ( const char *  src,
unsigned  len,
char *  dst 
) [static]

Definition at line 265 of file encode.c.

References b64lookup, buf, end, ereport, errcode(), errmsg(), and ERROR.

{
    const char *srcend = src + len,
               *s = src;
    char       *p = dst;
    char        c;
    int         b = 0;
    uint32      buf = 0;
    int         pos = 0,
                end = 0;

    while (s < srcend)
    {
        c = *s++;

        if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
            continue;

        if (c == '=')
        {
            /* end sequence */
            if (!end)
            {
                if (pos == 2)
                    end = 1;
                else if (pos == 3)
                    end = 2;
                else
                    ereport(ERROR,
                            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                             errmsg("unexpected \"=\"")));
            }
            b = 0;
        }
        else
        {
            b = -1;
            if (c > 0 && c < 127)
                b = b64lookup[(unsigned char) c];
            if (b < 0)
                ereport(ERROR,
                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                         errmsg("invalid symbol")));
        }
        /* add it to buffer */
        buf = (buf << 6) + b;
        pos++;
        if (pos == 4)
        {
            *p++ = (buf >> 16) & 255;
            if (end == 0 || end > 1)
                *p++ = (buf >> 8) & 255;
            if (end == 0 || end > 2)
                *p++ = buf & 255;
            buf = 0;
            pos = 0;
        }
    }

    if (pos != 0)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("invalid end sequence")));

    return p - dst;
}

static unsigned b64_enc_len ( const char *  src,
unsigned  srclen 
) [static]

Definition at line 334 of file encode.c.

{
    /* 3 bytes will be converted to 4, linefeed after 76 chars */
    return (srclen + 2) * 4 / 3 + srclen / (76 * 3 / 4);
}

static unsigned b64_encode ( const char *  src,
unsigned  len,
char *  dst 
) [static]

Definition at line 218 of file encode.c.

References _base64, buf, and end.

{
    char       *p,
               *lend = dst + 76;
    const char *s,
               *end = src + len;
    int         pos = 2;
    uint32      buf = 0;

    s = src;
    p = dst;

    while (s < end)
    {
        buf |= (unsigned char) *s << (pos << 3);
        pos--;
        s++;

        /* write it out */
        if (pos < 0)
        {
            *p++ = _base64[(buf >> 18) & 0x3f];
            *p++ = _base64[(buf >> 12) & 0x3f];
            *p++ = _base64[(buf >> 6) & 0x3f];
            *p++ = _base64[buf & 0x3f];

            pos = 2;
            buf = 0;
        }
        if (p >= lend)
        {
            *p++ = '\n';
            lend = p + 76;
        }
    }
    if (pos != 2)
    {
        *p++ = _base64[(buf >> 18) & 0x3f];
        *p++ = _base64[(buf >> 12) & 0x3f];
        *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '=';
        *p++ = '=';
    }

    return p - dst;
}

Datum binary_decode ( PG_FUNCTION_ARGS   ) 

Definition at line 72 of file encode.c.

References pg_encoding::decode, pg_encoding::decode_len, elog, enc, ereport, errcode(), errmsg(), ERROR, FATAL, name, NULL, palloc(), pg_find_encoding(), PG_GETARG_DATUM, PG_GETARG_TEXT_P, PG_RETURN_BYTEA_P, SET_VARSIZE, TextDatumGetCString, VARDATA, VARHDRSZ, and VARSIZE.

{
    text       *data = PG_GETARG_TEXT_P(0);
    Datum       name = PG_GETARG_DATUM(1);
    bytea      *result;
    char       *namebuf;
    int         datalen,
                resultlen,
                res;
    const struct pg_encoding *enc;

    datalen = VARSIZE(data) - VARHDRSZ;

    namebuf = TextDatumGetCString(name);

    enc = pg_find_encoding(namebuf);
    if (enc == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("unrecognized encoding: \"%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(FATAL, "overflow - decode estimate too small");

    SET_VARSIZE(result, VARHDRSZ + res);

    PG_RETURN_BYTEA_P(result);
}

Datum binary_encode ( PG_FUNCTION_ARGS   ) 

Definition at line 36 of file encode.c.

References elog, enc, pg_encoding::encode, pg_encoding::encode_len, ereport, errcode(), errmsg(), ERROR, FATAL, name, NULL, palloc(), pg_find_encoding(), PG_GETARG_BYTEA_P, PG_GETARG_DATUM, PG_RETURN_TEXT_P, SET_VARSIZE, TextDatumGetCString, VARDATA, VARHDRSZ, and VARSIZE.

{
    bytea      *data = PG_GETARG_BYTEA_P(0);
    Datum       name = PG_GETARG_DATUM(1);
    text       *result;
    char       *namebuf;
    int         datalen,
                resultlen,
                res;
    const struct pg_encoding *enc;

    datalen = VARSIZE(data) - VARHDRSZ;

    namebuf = TextDatumGetCString(name);

    enc = pg_find_encoding(namebuf);
    if (enc == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("unrecognized encoding: \"%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(FATAL, "overflow - encode estimate too small");

    SET_VARSIZE(result, VARHDRSZ + res);

    PG_RETURN_TEXT_P(result);
}

static unsigned esc_dec_len ( const char *  src,
unsigned  srclen 
) [static]

Definition at line 472 of file encode.c.

References end, ereport, errcode(), errmsg(), and ERROR.

{
    const char *end = src + srclen;
    int         len = 0;

    while (src < end)
    {
        if (src[0] != '\\')
            src++;
        else if (src + 3 < end &&
                 (src[1] >= '0' && src[1] <= '3') &&
                 (src[2] >= '0' && src[2] <= '7') &&
                 (src[3] >= '0' && src[3] <= '7'))
        {
            /*
             * backslash + valid octal
             */
            src += 4;
        }
        else if (src + 1 < end &&
                 (src[1] == '\\'))
        {
            /*
             * two backslashes = backslash
             */
            src += 2;
        }
        else
        {
            /*
             * one backslash, not followed by ### valid octal
             */
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                     errmsg("invalid input syntax for type bytea")));
        }

        len++;
    }
    return len;
}

static unsigned esc_decode ( const char *  src,
unsigned  srclen,
char *  dst 
) [static]

Definition at line 403 of file encode.c.

References end, ereport, errcode(), errmsg(), ERROR, VAL, and val.

{
    const char *end = src + srclen;
    char       *rp = dst;
    int         len = 0;

    while (src < end)
    {
        if (src[0] != '\\')
            *rp++ = *src++;
        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]);
            val <<= 3;
            *rp++ = val + VAL(src[3]);
            src += 4;
        }
        else if (src + 1 < end &&
                 (src[1] == '\\'))
        {
            *rp++ = '\\';
            src += 2;
        }
        else
        {
            /*
             * One backslash, not followed by ### valid octal. Should never
             * get here, since esc_dec_len does same check.
             */
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                     errmsg("invalid input syntax for type bytea")));
        }

        len++;
    }

    return len;
}

static unsigned esc_enc_len ( const char *  src,
unsigned  srclen 
) [static]

Definition at line 451 of file encode.c.

References end, and IS_HIGHBIT_SET.

{
    const char *end = src + srclen;
    int         len = 0;

    while (src < end)
    {
        if (*src == '\0' || IS_HIGHBIT_SET(*src))
            len += 4;
        else if (*src == '\\')
            len += 2;
        else
            len++;

        src++;
    }

    return len;
}

static unsigned esc_encode ( const char *  src,
unsigned  srclen,
char *  dst 
) [static]

Definition at line 364 of file encode.c.

References DIG, end, and IS_HIGHBIT_SET.

{
    const char *end = src + srclen;
    char       *rp = dst;
    int         len = 0;

    while (src < end)
    {
        unsigned char c = (unsigned char) *src;

        if (c == '\0' || IS_HIGHBIT_SET(c))
        {
            rp[0] = '\\';
            rp[1] = DIG(c >> 6);
            rp[2] = DIG((c >> 3) & 7);
            rp[3] = DIG(c & 7);
            rp += 4;
            len += 4;
        }
        else if (c == '\\')
        {
            rp[0] = '\\';
            rp[1] = '\\';
            rp += 2;
            len += 2;
        }
        else
        {
            *rp++ = c;
            len++;
        }

        src++;
    }

    return len;
}

static char get_hex ( char  c  )  [inline, static]

Definition at line 140 of file encode.c.

References ereport, errcode(), errmsg(), ERROR, and hexlookup.

Referenced by hex_decode().

{
    int         res = -1;

    if (c > 0 && c < 127)
        res = hexlookup[(unsigned char) c];

    if (res < 0)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("invalid hexadecimal digit: \"%c\"", c)));

    return (char) res;
}

static unsigned hex_dec_len ( const char *  src,
unsigned  srclen 
) [static]

Definition at line 194 of file encode.c.

{
    return srclen >> 1;
}

unsigned hex_decode ( const char *  src,
unsigned  len,
char *  dst 
)

Definition at line 156 of file encode.c.

References ereport, errcode(), errmsg(), ERROR, and get_hex().

Referenced by byteain().

{
    const char *s,
               *srcend;
    char        v1,
                v2,
               *p;

    srcend = src + len;
    s = src;
    p = dst;
    while (s < srcend)
    {
        if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r')
        {
            s++;
            continue;
        }
        v1 = get_hex(*s++) << 4;
        if (s >= srcend)
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                  errmsg("invalid hexadecimal data: odd number of digits")));

        v2 = get_hex(*s++);
        *p++ = v1 | v2;
    }

    return p - dst;
}

static unsigned hex_enc_len ( const char *  src,
unsigned  srclen 
) [static]

Definition at line 188 of file encode.c.

{
    return srclen << 1;
}

unsigned hex_encode ( const char *  src,
unsigned  len,
char *  dst 
)

Definition at line 126 of file encode.c.

References end, and hextbl.

Referenced by byteaout().

{
    const char *end = src + len;

    while (src < end)
    {
        *dst++ = hextbl[(*src >> 4) & 0xF];
        *dst++ = hextbl[*src & 0xF];
        src++;
    }
    return len * 2;
}

static struct pg_encoding * pg_find_encoding ( const char *  name  )  [static, read]

Definition at line 552 of file encode.c.

References enclist, i, and pg_strcasecmp().

Referenced by binary_decode(), and binary_encode().

{
    int         i;

    for (i = 0; enclist[i].name; i++)
        if (pg_strcasecmp(enclist[i].name, name) == 0)
            return &enclist[i].enc;

    return NULL;
}


Variable Documentation

const char _base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" [static]

Definition at line 203 of file encode.c.

Referenced by b64_encode().

const int8 b64lookup[128] [static]
Initial value:
 {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
    -1, 0, 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, -1, -1, -1, -1, -1,
    -1, 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, -1, -1, -1, -1, -1,
}

Definition at line 206 of file encode.c.

Referenced by b64_decode().

struct pg_encoding enc
struct { ... } enclist[] [static]

Referenced by pg_find_encoding().

const int8 hexlookup[128] [static]
Initial value:
 {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
    -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
}

Definition at line 114 of file encode.c.

Referenced by get_hex().

const char hextbl[] = "0123456789abcdef" [static]

Definition at line 112 of file encode.c.

Referenced by hex_encode().

const char* name

Definition at line 520 of file encode.c.

Referenced by _copyAExpr(), _copyAlterDomainStmt(), _copyAlterTableCmd(), _copyCreateOpClassItem(), _copyDeallocateStmt(), _copyExecuteStmt(), _copyFunctionParameter(), _copyIndexElem(), _copyNamedArgExpr(), _copyPrepareStmt(), _copyReindexStmt(), _copyResTarget(), _copyVariableSetStmt(), _copyVariableShowStmt(), _copyWindowClause(), _copyWindowDef(), _copyXmlExpr(), _equalAExpr(), _equalAlterDomainStmt(), _equalAlterTableCmd(), _equalCreateOpClassItem(), _equalDeallocateStmt(), _equalExecuteStmt(), _equalFunctionParameter(), _equalIndexElem(), _equalNamedArgExpr(), _equalPrepareStmt(), _equalReindexStmt(), _equalResTarget(), _equalVariableSetStmt(), _equalVariableShowStmt(), _equalWindowClause(), _equalWindowDef(), _equalXmlExpr(), _outAExpr(), _outIndexElem(), _outNamedArgExpr(), _outResTarget(), _outWindowClause(), _outWindowDef(), _outXmlExpr(), _PG_init(), _readNamedArgExpr(), _readWindowClause(), _readXmlExpr(), aclparse(), AlterObjectNamespace_internal(), assignVariables(), AuxiliaryProcessMain(), binary_decode(), binary_encode(), check_encoding_conversion_args(), checkInsertTargets(), ChooseIndexNameAddition(), CommitTransactionCommand(), compareVariables(), convert_sourcefiles_in(), convertOperatorReference(), convertRegProcReference(), CopyGetAttnums(), CreateTrigger(), deallocate_one(), define_custom_variable(), DefineSequence(), doCustom(), does_not_exist_skipping(), dump_block(), dumpOpr(), dupEvents(), ECPGdescribe(), enum_in(), enum_recv(), expandRelAttrs(), FigureColname(), FigureIndexColname(), findTargetlistEntrySQL92(), get_current_username(), get_object_address_unqualified(), get_prompt(), getopt_long(), gettype(), hash_object_field_end(), initialize_reloptions(), InitializeClientEncoding(), main(), makeObjectName(), MergeAttributes(), mkdirs(), NameListToString(), parseQuery(), parseVariable(), pg_any_to_server(), pg_bind_textdomain_codeset(), pg_digest(), pg_fe_getauthname(), pg_get_functiondef(), pg_hmac(), pgp_load_digest(), PLy_generate_spi_exceptions(), PLy_traceback(), populate_recordset_object_field_end(), PostmasterMain(), pp_require_safe(), process_postgres_switches(), process_startup_options(), ProcessGUCArray(), reapply_stacked_values(), ReleaseSavepoint(), repairDependencyLoop(), report_invalid_encoding(), report_untranslatable_char(), ri_ReportViolation(), RollbackToSavepoint(), run_permutation(), scan_available_timezones(), set_config_by_name(), set_ps_display(), standard_ProcessUtility(), tarOpen(), tsa_set_curcfg(), tsa_set_curcfg_byname(), tsa_set_curdict_byname(), tsa_set_curprs_byname(), TSConfigIsVisible(), TSDictionaryIsVisible(), TSParserIsVisible(), TSTemplateIsVisible(), TypeCreate(), TypeShellMake(), uuid_generate_v3(), and uuid_generate_v5().