Header And Logo

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

Enumerations | Functions | Variables

bytea.h File Reference

#include "fmgr.h"
Include dependency graph for bytea.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  ByteaOutputType { BYTEA_OUTPUT_ESCAPE, BYTEA_OUTPUT_HEX }

Functions

Datum byteain (PG_FUNCTION_ARGS)
Datum byteaout (PG_FUNCTION_ARGS)
Datum bytearecv (PG_FUNCTION_ARGS)
Datum byteasend (PG_FUNCTION_ARGS)
Datum byteaoctetlen (PG_FUNCTION_ARGS)
Datum byteaGetByte (PG_FUNCTION_ARGS)
Datum byteaGetBit (PG_FUNCTION_ARGS)
Datum byteaSetByte (PG_FUNCTION_ARGS)
Datum byteaSetBit (PG_FUNCTION_ARGS)
Datum byteaeq (PG_FUNCTION_ARGS)
Datum byteane (PG_FUNCTION_ARGS)
Datum bytealt (PG_FUNCTION_ARGS)
Datum byteale (PG_FUNCTION_ARGS)
Datum byteagt (PG_FUNCTION_ARGS)
Datum byteage (PG_FUNCTION_ARGS)
Datum byteacmp (PG_FUNCTION_ARGS)
Datum byteacat (PG_FUNCTION_ARGS)
Datum byteapos (PG_FUNCTION_ARGS)
Datum bytea_substr (PG_FUNCTION_ARGS)
Datum bytea_substr_no_len (PG_FUNCTION_ARGS)
Datum byteaoverlay (PG_FUNCTION_ARGS)
Datum byteaoverlay_no_len (PG_FUNCTION_ARGS)

Variables

int bytea_output

Enumeration Type Documentation

Enumerator:
BYTEA_OUTPUT_ESCAPE 
BYTEA_OUTPUT_HEX 

Definition at line 20 of file bytea.h.

{
    BYTEA_OUTPUT_ESCAPE,
    BYTEA_OUTPUT_HEX
}   ByteaOutputType;


Function Documentation

Datum bytea_substr ( PG_FUNCTION_ARGS   ) 
Datum bytea_substr_no_len ( PG_FUNCTION_ARGS   ) 
Datum byteacat ( PG_FUNCTION_ARGS   ) 
Datum byteacmp ( PG_FUNCTION_ARGS   ) 

Definition at line 2724 of file varlena.c.

References memcmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_bitcmp(), and gbt_byteacmp().

{
    bytea      *arg1 = PG_GETARG_BYTEA_PP(0);
    bytea      *arg2 = PG_GETARG_BYTEA_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = VARSIZE_ANY_EXHDR(arg1);
    len2 = VARSIZE_ANY_EXHDR(arg2);

    cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
    if ((cmp == 0) && (len1 != len2))
        cmp = (len1 < len2) ? -1 : 1;

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_INT32(cmp);
}

Datum byteaeq ( PG_FUNCTION_ARGS   ) 

Definition at line 2580 of file varlena.c.

References DatumGetByteaPP, memcmp(), PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_RETURN_BOOL, toast_raw_datum_size(), VARDATA_ANY, and VARHDRSZ.

Referenced by gbt_byteaeq().

{
    Datum       arg1 = PG_GETARG_DATUM(0);
    Datum       arg2 = PG_GETARG_DATUM(1);
    bool        result;
    Size        len1,
                len2;

    /*
     * We can use a fast path for unequal lengths, which might save us from
     * having to detoast one or both values.
     */
    len1 = toast_raw_datum_size(arg1);
    len2 = toast_raw_datum_size(arg2);
    if (len1 != len2)
        result = false;
    else
    {
        bytea      *barg1 = DatumGetByteaPP(arg1);
        bytea      *barg2 = DatumGetByteaPP(arg2);

        result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
                         len1 - VARHDRSZ) == 0);

        PG_FREE_IF_COPY(barg1, 0);
        PG_FREE_IF_COPY(barg2, 1);
    }

    PG_RETURN_BOOL(result);
}

Datum byteage ( PG_FUNCTION_ARGS   ) 

Definition at line 2704 of file varlena.c.

References memcmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_byteage().

{
    bytea      *arg1 = PG_GETARG_BYTEA_PP(0);
    bytea      *arg2 = PG_GETARG_BYTEA_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = VARSIZE_ANY_EXHDR(arg1);
    len2 = VARSIZE_ANY_EXHDR(arg2);

    cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
}

Datum byteaGetBit ( PG_FUNCTION_ARGS   ) 

Definition at line 2126 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_BYTEA_PP, PG_GETARG_INT32, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    bytea      *v = PG_GETARG_BYTEA_PP(0);
    int32       n = PG_GETARG_INT32(1);
    int         byteNo,
                bitNo;
    int         len;
    int         byte;

    len = VARSIZE_ANY_EXHDR(v);

    if (n < 0 || n >= len * 8)
        ereport(ERROR,
                (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
                 errmsg("index %d out of valid range, 0..%d",
                        n, len * 8 - 1)));

    byteNo = n / 8;
    bitNo = n % 8;

    byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];

    if (byte & (1 << bitNo))
        PG_RETURN_INT32(1);
    else
        PG_RETURN_INT32(0);
}

Datum byteaGetByte ( PG_FUNCTION_ARGS   ) 

Definition at line 2097 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_BYTEA_PP, PG_GETARG_INT32, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    bytea      *v = PG_GETARG_BYTEA_PP(0);
    int32       n = PG_GETARG_INT32(1);
    int         len;
    int         byte;

    len = VARSIZE_ANY_EXHDR(v);

    if (n < 0 || n >= len)
        ereport(ERROR,
                (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
                 errmsg("index %d out of valid range, 0..%d",
                        n, len - 1)));

    byte = ((unsigned char *) VARDATA_ANY(v))[n];

    PG_RETURN_INT32(byte);
}

Datum byteagt ( PG_FUNCTION_ARGS   ) 

Definition at line 2684 of file varlena.c.

References memcmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_byteagt().

{
    bytea      *arg1 = PG_GETARG_BYTEA_PP(0);
    bytea      *arg2 = PG_GETARG_BYTEA_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = VARSIZE_ANY_EXHDR(arg1);
    len2 = VARSIZE_ANY_EXHDR(arg2);

    cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
}

Datum byteain ( PG_FUNCTION_ARGS   ) 

Definition at line 214 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, hex_decode(), palloc(), PG_GETARG_CSTRING, PG_RETURN_BYTEA_P, SET_VARSIZE, VAL, VARDATA, and VARHDRSZ.

Referenced by CreateTrigger(), and string_to_datum().

{
    char       *inputText = PG_GETARG_CSTRING(0);
    char       *tp;
    char       *rp;
    int         bc;
    bytea      *result;

    /* Recognize hex input */
    if (inputText[0] == '\\' && inputText[1] == 'x')
    {
        size_t      len = strlen(inputText);

        bc = (len - 2) / 2 + VARHDRSZ;  /* maximum possible length */
        result = palloc(bc);
        bc = hex_decode(inputText + 2, len - 2, VARDATA(result));
        SET_VARSIZE(result, bc + VARHDRSZ);     /* actual length */

        PG_RETURN_BYTEA_P(result);
    }

    /* Else, it's the traditional escaped style */
    for (bc = 0, tp = inputText; *tp != '\0'; bc++)
    {
        if (tp[0] != '\\')
            tp++;
        else if ((tp[0] == '\\') &&
                 (tp[1] >= '0' && tp[1] <= '3') &&
                 (tp[2] >= '0' && tp[2] <= '7') &&
                 (tp[3] >= '0' && tp[3] <= '7'))
            tp += 4;
        else if ((tp[0] == '\\') &&
                 (tp[1] == '\\'))
            tp += 2;
        else
        {
            /*
             * one backslash, not followed by another or ### valid octal
             */
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                     errmsg("invalid input syntax for type bytea")));
        }
    }

    bc += VARHDRSZ;

    result = (bytea *) palloc(bc);
    SET_VARSIZE(result, bc);

    tp = inputText;
    rp = VARDATA(result);
    while (*tp != '\0')
    {
        if (tp[0] != '\\')
            *rp++ = *tp++;
        else if ((tp[0] == '\\') &&
                 (tp[1] >= '0' && tp[1] <= '3') &&
                 (tp[2] >= '0' && tp[2] <= '7') &&
                 (tp[3] >= '0' && tp[3] <= '7'))
        {
            bc = VAL(tp[1]);
            bc <<= 3;
            bc += VAL(tp[2]);
            bc <<= 3;
            *rp++ = bc + VAL(tp[3]);

            tp += 4;
        }
        else if ((tp[0] == '\\') &&
                 (tp[1] == '\\'))
        {
            *rp++ = '\\';
            tp += 2;
        }
        else
        {
            /*
             * We should never get here. The first pass should not allow it.
             */
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                     errmsg("invalid input syntax for type bytea")));
        }
    }

    PG_RETURN_BYTEA_P(result);
}

Datum byteale ( PG_FUNCTION_ARGS   ) 

Definition at line 2664 of file varlena.c.

References memcmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_byteale().

{
    bytea      *arg1 = PG_GETARG_BYTEA_PP(0);
    bytea      *arg2 = PG_GETARG_BYTEA_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = VARSIZE_ANY_EXHDR(arg1);
    len2 = VARSIZE_ANY_EXHDR(arg2);

    cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
}

Datum bytealt ( PG_FUNCTION_ARGS   ) 

Definition at line 2644 of file varlena.c.

References memcmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_bytealt().

{
    bytea      *arg1 = PG_GETARG_BYTEA_PP(0);
    bytea      *arg2 = PG_GETARG_BYTEA_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = VARSIZE_ANY_EXHDR(arg1);
    len2 = VARSIZE_ANY_EXHDR(arg2);

    cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
}

Datum byteane ( PG_FUNCTION_ARGS   ) 

Definition at line 2612 of file varlena.c.

References DatumGetByteaPP, memcmp(), PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_RETURN_BOOL, toast_raw_datum_size(), VARDATA_ANY, and VARHDRSZ.

{
    Datum       arg1 = PG_GETARG_DATUM(0);
    Datum       arg2 = PG_GETARG_DATUM(1);
    bool        result;
    Size        len1,
                len2;

    /*
     * We can use a fast path for unequal lengths, which might save us from
     * having to detoast one or both values.
     */
    len1 = toast_raw_datum_size(arg1);
    len2 = toast_raw_datum_size(arg2);
    if (len1 != len2)
        result = true;
    else
    {
        bytea      *barg1 = DatumGetByteaPP(arg1);
        bytea      *barg2 = DatumGetByteaPP(arg2);

        result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
                         len1 - VARHDRSZ) != 0);

        PG_FREE_IF_COPY(barg1, 0);
        PG_FREE_IF_COPY(barg2, 1);
    }

    PG_RETURN_BOOL(result);
}

Datum byteaoctetlen ( PG_FUNCTION_ARGS   ) 

Definition at line 1828 of file varlena.c.

References PG_GETARG_DATUM, PG_RETURN_INT32, toast_raw_datum_size(), and VARHDRSZ.

{
    Datum       str = PG_GETARG_DATUM(0);

    /* We need not detoast the input at all */
    PG_RETURN_INT32(toast_raw_datum_size(str) - VARHDRSZ);
}

Datum byteaout ( PG_FUNCTION_ARGS   ) 

Definition at line 310 of file varlena.c.

References bytea_output, BYTEA_OUTPUT_ESCAPE, BYTEA_OUTPUT_HEX, DIG, elog, ERROR, hex_encode(), i, palloc(), PG_GETARG_BYTEA_PP, PG_RETURN_CSTRING, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by patternsel(), and prefix_quals().

{
    bytea      *vlena = PG_GETARG_BYTEA_PP(0);
    char       *result;
    char       *rp;

    if (bytea_output == BYTEA_OUTPUT_HEX)
    {
        /* Print hex format */
        rp = result = palloc(VARSIZE_ANY_EXHDR(vlena) * 2 + 2 + 1);
        *rp++ = '\\';
        *rp++ = 'x';
        rp += hex_encode(VARDATA_ANY(vlena), VARSIZE_ANY_EXHDR(vlena), rp);
    }
    else if (bytea_output == BYTEA_OUTPUT_ESCAPE)
    {
        /* Print traditional escaped format */
        char       *vp;
        int         len;
        int         i;

        len = 1;                /* empty string has 1 char */
        vp = VARDATA_ANY(vlena);
        for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
        {
            if (*vp == '\\')
                len += 2;
            else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
                len += 4;
            else
                len++;
        }
        rp = result = (char *) palloc(len);
        vp = VARDATA_ANY(vlena);
        for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
        {
            if (*vp == '\\')
            {
                *rp++ = '\\';
                *rp++ = '\\';
            }
            else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
            {
                int         val;    /* holds unprintable chars */

                val = *vp;
                rp[0] = '\\';
                rp[3] = DIG(val & 07);
                val >>= 3;
                rp[2] = DIG(val & 07);
                val >>= 3;
                rp[1] = DIG(val & 03);
                rp += 4;
            }
            else
                *rp++ = *vp;
        }
    }
    else
    {
        elog(ERROR, "unrecognized bytea_output setting: %d",
             bytea_output);
        rp = result = NULL;     /* keep compiler quiet */
    }
    *rp = '\0';
    PG_RETURN_CSTRING(result);
}

Datum byteaoverlay ( PG_FUNCTION_ARGS   ) 

Definition at line 1993 of file varlena.c.

References bytea_overlay(), PG_GETARG_BYTEA_PP, PG_GETARG_INT32, and PG_RETURN_BYTEA_P.

{
    bytea      *t1 = PG_GETARG_BYTEA_PP(0);
    bytea      *t2 = PG_GETARG_BYTEA_PP(1);
    int         sp = PG_GETARG_INT32(2);        /* substring start position */
    int         sl = PG_GETARG_INT32(3);        /* substring length */

    PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
}

Datum byteaoverlay_no_len ( PG_FUNCTION_ARGS   ) 

Definition at line 2004 of file varlena.c.

References bytea_overlay(), PG_GETARG_BYTEA_PP, PG_GETARG_INT32, PG_RETURN_BYTEA_P, and VARSIZE_ANY_EXHDR.

{
    bytea      *t1 = PG_GETARG_BYTEA_PP(0);
    bytea      *t2 = PG_GETARG_BYTEA_PP(1);
    int         sp = PG_GETARG_INT32(2);        /* substring start position */
    int         sl;

    sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
    PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
}

Datum byteapos ( PG_FUNCTION_ARGS   ) 

Definition at line 2053 of file varlena.c.

References memcmp(), PG_GETARG_BYTEA_PP, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    bytea      *t1 = PG_GETARG_BYTEA_PP(0);
    bytea      *t2 = PG_GETARG_BYTEA_PP(1);
    int         pos;
    int         px,
                p;
    int         len1,
                len2;
    char       *p1,
               *p2;

    len1 = VARSIZE_ANY_EXHDR(t1);
    len2 = VARSIZE_ANY_EXHDR(t2);

    if (len2 <= 0)
        PG_RETURN_INT32(1);     /* result for empty pattern */

    p1 = VARDATA_ANY(t1);
    p2 = VARDATA_ANY(t2);

    pos = 0;
    px = (len1 - len2);
    for (p = 0; p <= px; p++)
    {
        if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
        {
            pos = p + 1;
            break;
        };
        p1++;
    };

    PG_RETURN_INT32(pos);
}

Datum bytearecv ( PG_FUNCTION_ARGS   ) 

Definition at line 382 of file varlena.c.

References buf, StringInfoData::cursor, StringInfoData::len, palloc(), PG_GETARG_POINTER, PG_RETURN_BYTEA_P, pq_copymsgbytes(), SET_VARSIZE, VARDATA, and VARHDRSZ.

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    bytea      *result;
    int         nbytes;

    nbytes = buf->len - buf->cursor;
    result = (bytea *) palloc(nbytes + VARHDRSZ);
    SET_VARSIZE(result, nbytes + VARHDRSZ);
    pq_copymsgbytes(buf, VARDATA(result), nbytes);
    PG_RETURN_BYTEA_P(result);
}

Datum byteasend ( PG_FUNCTION_ARGS   ) 

Definition at line 401 of file varlena.c.

References PG_GETARG_BYTEA_P_COPY, and PG_RETURN_BYTEA_P.

Datum byteaSetBit ( PG_FUNCTION_ARGS   ) 

Definition at line 2202 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_BYTEA_P, PG_GETARG_INT32, PG_RETURN_BYTEA_P, VARDATA, and VARSIZE.

{
    bytea      *v = PG_GETARG_BYTEA_P(0);
    int32       n = PG_GETARG_INT32(1);
    int32       newBit = PG_GETARG_INT32(2);
    bytea      *res;
    int         len;
    int         oldByte,
                newByte;
    int         byteNo,
                bitNo;

    len = VARSIZE(v) - VARHDRSZ;

    if (n < 0 || n >= len * 8)
        ereport(ERROR,
                (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
                 errmsg("index %d out of valid range, 0..%d",
                        n, len * 8 - 1)));

    byteNo = n / 8;
    bitNo = n % 8;

    /*
     * sanity check!
     */
    if (newBit != 0 && newBit != 1)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("new bit must be 0 or 1")));

    /*
     * Make a copy of the original varlena.
     */
    res = (bytea *) palloc(VARSIZE(v));
    memcpy((char *) res, (char *) v, VARSIZE(v));

    /*
     * Update the byte.
     */
    oldByte = ((unsigned char *) VARDATA(res))[byteNo];

    if (newBit == 0)
        newByte = oldByte & (~(1 << bitNo));
    else
        newByte = oldByte | (1 << bitNo);

    ((unsigned char *) VARDATA(res))[byteNo] = newByte;

    PG_RETURN_BYTEA_P(res);
}

Datum byteaSetByte ( PG_FUNCTION_ARGS   ) 

Definition at line 2163 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_BYTEA_P, PG_GETARG_INT32, PG_RETURN_BYTEA_P, VARDATA, and VARSIZE.

{
    bytea      *v = PG_GETARG_BYTEA_P(0);
    int32       n = PG_GETARG_INT32(1);
    int32       newByte = PG_GETARG_INT32(2);
    int         len;
    bytea      *res;

    len = VARSIZE(v) - VARHDRSZ;

    if (n < 0 || n >= len)
        ereport(ERROR,
                (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
                 errmsg("index %d out of valid range, 0..%d",
                        n, len - 1)));

    /*
     * Make a copy of the original varlena.
     */
    res = (bytea *) palloc(VARSIZE(v));
    memcpy((char *) res, (char *) v, VARSIZE(v));

    /*
     * Now set the byte.
     */
    ((unsigned char *) VARDATA(res))[n] = newByte;

    PG_RETURN_BYTEA_P(res);
}


Variable Documentation

Definition at line 35 of file varlena.c.

Referenced by byteaout().