summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils/adt')
-rw-r--r--src/backend/utils/adt/encode.c189
-rw-r--r--src/backend/utils/adt/like.c234
-rw-r--r--src/backend/utils/adt/oracle_compat.c74
-rw-r--r--src/backend/utils/adt/selfuncs.c7
-rw-r--r--src/backend/utils/adt/varlena.c201
5 files changed, 680 insertions, 25 deletions
diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c
index 89ba357aa57..53825851847 100644
--- a/src/backend/utils/adt/encode.c
+++ b/src/backend/utils/adt/encode.c
@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/encode.c,v 1.1 2001/07/12 14:05:31 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/encode.c,v 1.2 2001/09/14 17:46:40 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -19,8 +19,8 @@
struct pg_encoding
{
- unsigned (*encode_len) (unsigned dlen);
- unsigned (*decode_len) (unsigned dlen);
+ unsigned (*encode_len) (const uint8 *data, unsigned dlen);
+ unsigned (*decode_len) (const uint8 *data, unsigned dlen);
unsigned (*encode) (const uint8 *data, unsigned dlen, uint8 *res);
unsigned (*decode) (const uint8 *data, unsigned dlen, uint8 *res);
};
@@ -50,7 +50,7 @@ binary_encode(PG_FUNCTION_ARGS)
if (enc == NULL)
elog(ERROR, "No such encoding");
- resultlen = enc->encode_len(datalen);
+ resultlen = enc->encode_len(VARDATA(data), datalen);
result = palloc(VARHDRSZ + resultlen);
res = enc->encode(VARDATA(data), datalen, VARDATA(result));
@@ -81,7 +81,7 @@ binary_decode(PG_FUNCTION_ARGS)
if (enc == NULL)
elog(ERROR, "No such encoding");
- resultlen = enc->decode_len(datalen);
+ resultlen = enc->decode_len(VARDATA(data), datalen);
result = palloc(VARHDRSZ + resultlen);
res = enc->decode(VARDATA(data), datalen, VARDATA(result));
@@ -169,13 +169,13 @@ hex_decode(const uint8 * src, unsigned len, uint8 * dst)
}
static unsigned
-hex_enc_len(unsigned srclen)
+hex_enc_len(const uint8 * src, unsigned srclen)
{
return srclen << 1;
}
static unsigned
-hex_dec_len(unsigned srclen)
+hex_dec_len(const uint8 * src, unsigned srclen)
{
return srclen >> 1;
}
@@ -308,19 +308,189 @@ b64_decode(const uint8 * src, unsigned len, uint8 * dst)
static unsigned
-b64_enc_len(unsigned srclen)
+b64_enc_len(const uint8 * src, unsigned srclen)
{
/* 3 bytes will be converted to 4, linefeed after 76 chars */
return (srclen + 2) * 4 / 3 + srclen / (76 * 3 / 4);
}
static unsigned
-b64_dec_len(unsigned srclen)
+b64_dec_len(const uint8 * src, unsigned srclen)
{
return (srclen * 3) >> 2;
}
/*
+ * Escape
+ * Minimally escape bytea to text.
+ * De-escape text to bytea.
+ *
+ * Only two characters are escaped:
+ * \0 (null) and \\ (backslash)
+ *
+ * De-escapes \\ and any \### octal
+ */
+
+#define VAL(CH) ((CH) - '0')
+#define DIG(VAL) ((VAL) + '0')
+
+static unsigned
+esc_encode(const uint8 *src, unsigned srclen, uint8 *dst)
+{
+ 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';
+ rp[3] = '0';
+ rp += 4;
+ len += 4;
+ }
+ else if (*src == '\\')
+ {
+ val = *src;
+ rp[0] = '\\';
+ rp[1] = '\\';
+ rp += 2;
+ len += 2;
+ }
+ else
+ {
+ *rp++ = *src;
+ len++;
+ }
+
+ src++;
+ }
+ *rp = '\0';
+
+ return len;
+}
+
+static unsigned
+esc_decode(const uint8 *src, unsigned srclen, uint8 *dst)
+{
+ const uint8 *end = src + srclen;
+ uint8 *rp = dst;
+ int val;
+ int len = 0;
+
+ while (src < end)
+ {
+ if (src[0] != '\\')
+ {
+ *rp++ = *src++;
+ }
+ else if ( (src[0] == '\\') &&
+ (src[1] >= '0' && src[1] <= '3') &&
+ (src[2] >= '0' && src[2] <= '7') &&
+ (src[3] >= '0' && src[3] <= '7') )
+ {
+ val = VAL(src[1]);
+ val <<= 3;
+ val += VAL(src[2]);
+ val <<= 3;
+ *rp++ = val + VAL(src[3]);
+ src += 4;
+ }
+ else if ( (src[0] == '\\') &&
+ (src[1] == '\\') )
+ {
+ *rp++ = '\\';
+ src += 2;
+ }
+ else
+ {
+ /*
+ * One backslash, not followed by ### valid octal.
+ * Should never get here, since esc_dec_len does same check.
+ */
+ elog(ERROR, "decode: Bad input string for type bytea");
+ }
+
+ len++;
+ }
+ return len;
+}
+
+static unsigned
+esc_enc_len(const uint8 *src, unsigned srclen)
+{
+ const uint8 *end = src + srclen;
+ int len = 0;
+
+ while (src < end)
+ {
+ if (*src == '\0')
+ len += 4;
+ else if (*src == '\\')
+ len += 2;
+ else
+ len++;
+
+ src++;
+ }
+
+ /*
+ * Allow for null terminator
+ */
+ len++;
+
+ return len;
+}
+
+static unsigned
+esc_dec_len(const uint8 *src, unsigned srclen)
+{
+ const uint8 *end = src + srclen;
+ int len = 0;
+
+ while (src < end)
+ {
+ if (src[0] != '\\')
+ {
+ src++;
+ }
+ else if ( (src[0] == '\\') &&
+ (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[0] == '\\') &&
+ (src[1] == '\\') )
+ {
+ /*
+ * two backslashes = backslash
+ */
+ src += 2;
+ }
+ else
+ {
+ /*
+ * one backslash, not followed by ### valid octal
+ */
+ elog(ERROR, "decode: Bad input string for type bytea");
+ }
+
+ len++;
+ }
+ return len;
+}
+
+/*
* Common
*/
@@ -330,6 +500,7 @@ static struct {
} enclist[] = {
{"hex", { hex_enc_len, hex_dec_len, hex_encode, hex_decode }},
{"base64", { b64_enc_len, b64_dec_len, b64_encode, b64_decode }},
+ {"escape", { esc_enc_len, esc_dec_len, esc_encode, esc_decode }},
{NULL, { NULL, NULL, NULL, NULL } }
};
diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c
index f27bc067d6a..4f6d6d0212b 100644
--- a/src/backend/utils/adt/like.c
+++ b/src/backend/utils/adt/like.c
@@ -11,7 +11,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/like.c,v 1.45 2001/03/22 03:59:51 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/like.c,v 1.46 2001/09/14 17:46:40 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -34,6 +34,8 @@ static int MatchText(unsigned char *t, int tlen,
unsigned char *p, int plen);
static int MatchTextIC(unsigned char *t, int tlen,
unsigned char *p, int plen);
+static int MatchBytea(unsigned char *t, int tlen,
+ unsigned char *p, int plen);
#ifdef MULTIBYTE
@@ -120,6 +122,9 @@ iwchareq(unsigned char *p1, unsigned char *p2)
#define CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
#endif
+#define BYTEA_CHAREQ(p1, p2) (*(p1) == *(p2))
+#define BYTEA_NextChar(p, plen) ((p)++, (plen)--)
+#define BYTEA_CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
/*
* interface routines called by the function manager
@@ -209,6 +214,48 @@ textnlike(PG_FUNCTION_ARGS)
PG_RETURN_BOOL(result);
}
+Datum
+bytealike(PG_FUNCTION_ARGS)
+{
+ bytea *str = PG_GETARG_BYTEA_P(0);
+ bytea *pat = PG_GETARG_BYTEA_P(1);
+ bool result;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
+
+ s = VARDATA(str);
+ slen = (VARSIZE(str) - VARHDRSZ);
+ p = VARDATA(pat);
+ plen = (VARSIZE(pat) - VARHDRSZ);
+
+ result = (MatchBytea(s, slen, p, plen) == LIKE_TRUE);
+
+ PG_RETURN_BOOL(result);
+}
+
+Datum
+byteanlike(PG_FUNCTION_ARGS)
+{
+ bytea *str = PG_GETARG_BYTEA_P(0);
+ bytea *pat = PG_GETARG_BYTEA_P(1);
+ bool result;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
+
+ s = VARDATA(str);
+ slen = (VARSIZE(str) - VARHDRSZ);
+ p = VARDATA(pat);
+ plen = (VARSIZE(pat) - VARHDRSZ);
+
+ result = (MatchBytea(s, slen, p, plen) != LIKE_TRUE);
+
+ PG_RETURN_BOOL(result);
+}
+
/*
* Case-insensitive versions
*/
@@ -395,6 +442,103 @@ like_escape(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(result);
}
+/*
+ * like_escape_bytea() --- given a pattern and an ESCAPE string,
+ * convert the pattern to use Postgres' standard backslash escape convention.
+ */
+Datum
+like_escape_bytea(PG_FUNCTION_ARGS)
+{
+ bytea *pat = PG_GETARG_BYTEA_P(0);
+ bytea *esc = PG_GETARG_BYTEA_P(1);
+ bytea *result;
+ unsigned char *p,
+ *e,
+ *r;
+ int plen,
+ elen;
+ bool afterescape;
+
+ p = VARDATA(pat);
+ plen = (VARSIZE(pat) - VARHDRSZ);
+ e = VARDATA(esc);
+ elen = (VARSIZE(esc) - VARHDRSZ);
+
+ /*
+ * Worst-case pattern growth is 2x --- unlikely, but it's hardly worth
+ * trying to calculate the size more accurately than that.
+ */
+ result = (text *) palloc(plen * 2 + VARHDRSZ);
+ r = VARDATA(result);
+
+ if (elen == 0)
+ {
+
+ /*
+ * No escape character is wanted. Double any backslashes in the
+ * pattern to make them act like ordinary characters.
+ */
+ while (plen > 0)
+ {
+ if (*p == '\\')
+ *r++ = '\\';
+ BYTEA_CopyAdvChar(r, p, plen);
+ }
+ }
+ else
+ {
+
+ /*
+ * The specified escape must be only a single character.
+ */
+ BYTEA_NextChar(e, elen);
+ if (elen != 0)
+ elog(ERROR, "ESCAPE string must be empty or one character");
+ e = VARDATA(esc);
+
+ /*
+ * If specified escape is '\', just copy the pattern as-is.
+ */
+ if (*e == '\\')
+ {
+ memcpy(result, pat, VARSIZE(pat));
+ PG_RETURN_BYTEA_P(result);
+ }
+
+ /*
+ * Otherwise, convert occurrences of the specified escape
+ * character to '\', and double occurrences of '\' --- unless they
+ * immediately follow an escape character!
+ */
+ afterescape = false;
+ while (plen > 0)
+ {
+ if (BYTEA_CHAREQ(p, e) && !afterescape)
+ {
+ *r++ = '\\';
+ BYTEA_NextChar(p, plen);
+ afterescape = true;
+ }
+ else if (*p == '\\')
+ {
+ *r++ = '\\';
+ if (!afterescape)
+ *r++ = '\\';
+ BYTEA_NextChar(p, plen);
+ afterescape = false;
+ }
+ else
+ {
+ BYTEA_CopyAdvChar(r, p, plen);
+ afterescape = false;
+ }
+ }
+ }
+
+ VARATT_SIZEP(result) = r - ((unsigned char *) result);
+
+ PG_RETURN_BYTEA_P(result);
+}
/*
** Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
@@ -614,3 +758,91 @@ MatchTextIC(unsigned char *t, int tlen, unsigned char *p, int plen)
*/
return LIKE_ABORT;
} /* MatchTextIC() */
+
+/*
+ * Same as above, but specifically for bytea (binary) datatype
+ */
+static int
+MatchBytea(unsigned char *t, int tlen, unsigned char *p, int plen)
+{
+ /* Fast path for match-everything pattern */
+ if ((plen == 1) && (*p == '%'))
+ return LIKE_TRUE;
+
+ while ((tlen > 0) && (plen > 0))
+ {
+ if (*p == '\\')
+ {
+ /* Next pattern char must match literally, whatever it is */
+ BYTEA_NextChar(p, plen);
+ if ((plen <= 0) || !BYTEA_CHAREQ(t, p))
+ return LIKE_FALSE;
+ }
+ else if (*p == '%')
+ {
+ /* %% is the same as % according to the SQL standard */
+ /* Advance past all %'s */
+ while ((plen > 0) && (*p == '%'))
+ BYTEA_NextChar(p, plen);
+ /* Trailing percent matches everything. */
+ if (plen <= 0)
+ return LIKE_TRUE;
+
+ /*
+ * Otherwise, scan for a text position at which we can match
+ * the rest of the pattern.
+ */
+ while (tlen > 0)
+ {
+
+ /*
+ * Optimization to prevent most recursion: don't recurse
+ * unless first pattern char might match this text char.
+ */
+ if (BYTEA_CHAREQ(t, p) || (*p == '\\') || (*p == '_'))
+ {
+ int matched = MatchBytea(t, tlen, p, plen);
+
+ if (matched != LIKE_FALSE)
+ return matched; /* TRUE or ABORT */
+ }
+
+ BYTEA_NextChar(t, tlen);
+ }
+
+ /*
+ * End of text with no match, so no point in trying later
+ * places to start matching this pattern.
+ */
+ return LIKE_ABORT;
+ }
+ else if ((*p != '_') && !BYTEA_CHAREQ(t, p))
+ {
+
+ /*
+ * Not the single-character wildcard and no explicit match?
+ * Then time to quit...
+ */
+ return LIKE_FALSE;
+ }
+
+ BYTEA_NextChar(t, tlen);
+ BYTEA_NextChar(p, plen);
+ }
+
+ if (tlen > 0)
+ return LIKE_FALSE; /* end of pattern, but not of text */
+
+ /* End of input string. Do we have matching pattern remaining? */
+ while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of
+ * pattern */
+ BYTEA_NextChar(p, plen);
+ if (plen <= 0)
+ return LIKE_TRUE;
+
+ /*
+ * End of text with no match, so no point in trying later places to
+ * start matching this pattern.
+ */
+ return LIKE_ABORT;
+} /* MatchBytea() */
diff --git a/src/backend/utils/adt/oracle_compat.c b/src/backend/utils/adt/oracle_compat.c
index b91230b1f0a..a0018522e8a 100644
--- a/src/backend/utils/adt/oracle_compat.c
+++ b/src/backend/utils/adt/oracle_compat.c
@@ -1,7 +1,7 @@
/*
* Edmund Mergl <E.Mergl@bawue.de>
*
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.31 2001/03/22 03:59:52 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.32 2001/09/14 17:46:40 momjian Exp $
*
*/
@@ -349,6 +349,78 @@ btrim(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(ret);
}
+/********************************************************************
+ *
+ * byteatrim
+ *
+ * Syntax:
+ *
+ * bytea byteatrim(byta string, bytea set)
+ *
+ * Purpose:
+ *
+ * Returns string with characters removed from the front and back
+ * up to the first character not in set.
+ *
+ * Cloned from btrim and modified as required.
+ ********************************************************************/
+
+Datum
+byteatrim(PG_FUNCTION_ARGS)
+{
+ bytea *string = PG_GETARG_BYTEA_P(0);
+ bytea *set = PG_GETARG_BYTEA_P(1);
+ bytea *ret;
+ char *ptr,
+ *end,
+ *ptr2,
+ *end2;
+ int m;
+
+ if ((m = VARSIZE(string) - VARHDRSZ) <= 0 ||
+ (VARSIZE(set) - VARHDRSZ) <= 0)
+ PG_RETURN_BYTEA_P(string);
+
+ ptr = VARDATA(string);
+ end = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
+ end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
+
+ while (m > 0)
+ {
+ ptr2 = VARDATA(set);
+ while (ptr2 <= end2)
+ {
+ if (*ptr == *ptr2)
+ break;
+ ++ptr2;
+ }
+ if (ptr2 > end2)
+ break;
+ ptr++;
+ m--;
+ }
+
+ while (m > 0)
+ {
+ ptr2 = VARDATA(set);
+ while (ptr2 <= end2)
+ {
+ if (*end == *ptr2)
+ break;
+ ++ptr2;
+ }
+ if (ptr2 > end2)
+ break;
+ end--;
+ m--;
+ }
+
+ ret = (bytea *) palloc(VARHDRSZ + m);
+ VARATT_SIZEP(ret) = VARHDRSZ + m;
+ memcpy(VARDATA(ret), ptr, m);
+
+ PG_RETURN_BYTEA_P(ret);
+}
/********************************************************************
*
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 406916c8873..f92f3c20f1e 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.96 2001/08/13 18:45:35 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.97 2001/09/14 17:46:40 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -2992,6 +2992,11 @@ string_lessthan(const char *str1, const char *str2, Oid datatype)
datum1, datum2));
break;
+ case BYTEAOID:
+ result = DatumGetBool(DirectFunctionCall2(bytealt,
+ datum1, datum2));
+ break;
+
default:
elog(ERROR, "string_lessthan: unexpected datatype %u", datatype);
result = false;
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 75eee05eeae..1c6de93f014 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.72 2001/09/11 05:18:59 ishii Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.73 2001/09/14 17:46:40 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -53,14 +53,28 @@ byteain(PG_FUNCTION_ARGS)
for (byte = 0, tp = inputText; *tp != '\0'; byte++)
{
- if (*tp++ == '\\')
+ if (tp[0] != '\\')
{
- if (*tp == '\\')
- tp++;
- else if (!isdigit((unsigned char) *tp++) ||
- !isdigit((unsigned char) *tp++) ||
- !isdigit((unsigned char) *tp++))
- elog(ERROR, "Bad input string for type bytea");
+ 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 0 or ### valid octal
+ */
+ elog(ERROR, "Bad input string for type bytea");
}
}
@@ -72,15 +86,35 @@ byteain(PG_FUNCTION_ARGS)
rp = result->vl_dat;
while (*tp != '\0')
{
- if (*tp != '\\' || *++tp == '\\')
+ if (tp[0] != '\\')
+ {
*rp++ = *tp++;
- else
+ }
+ else if ( (tp[0] == '\\') &&
+ (tp[1] >= '0' && tp[1] <= '3') &&
+ (tp[2] >= '0' && tp[2] <= '7') &&
+ (tp[3] >= '0' && tp[3] <= '7') )
{
- byte = VAL(*tp++);
+ byte = VAL(tp[1]);
byte <<= 3;
- byte += VAL(*tp++);
+ byte += VAL(tp[2]);
byte <<= 3;
- *rp++ = byte + VAL(*tp++);
+ *rp++ = byte + 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.
+ */
+ elog(ERROR, "Bad input string for type bytea");
}
}
@@ -671,6 +705,147 @@ byteaoctetlen(PG_FUNCTION_ARGS)
PG_RETURN_INT32(VARSIZE(v) - VARHDRSZ);
}
+/*
+ * byteacat -
+ * takes two bytea* and returns a bytea* that is the concatenation of
+ * the two.
+ *
+ * Cloned from textcat and modified as required.
+ */
+Datum
+byteacat(PG_FUNCTION_ARGS)
+{
+ bytea *t1 = PG_GETARG_BYTEA_P(0);
+ bytea *t2 = PG_GETARG_BYTEA_P(1);
+ int len1,
+ len2,
+ len;
+ bytea *result;
+ char *ptr;
+
+ len1 = (VARSIZE(t1) - VARHDRSZ);
+ if (len1 < 0)
+ len1 = 0;
+
+ len2 = (VARSIZE(t2) - VARHDRSZ);
+ if (len2 < 0)
+ len2 = 0;
+
+ len = len1 + len2 + VARHDRSZ;
+ result = (bytea *) palloc(len);
+
+ /* Set size of result string... */
+ VARATT_SIZEP(result) = len;
+
+ /* Fill data field of result string... */
+ ptr = VARDATA(result);
+ if (len1 > 0)
+ memcpy(ptr, VARDATA(t1), len1);
+ if (len2 > 0)
+ memcpy(ptr + len1, VARDATA(t2), len2);
+
+ PG_RETURN_BYTEA_P(result);
+}
+
+/*
+ * bytea_substr()
+ * Return a substring starting at the specified position.
+ * Cloned from text_substr and modified as required.
+ *
+ * Input:
+ * - string
+ * - starting position (is one-based)
+ * - string length
+ *
+ * If the starting position is zero or less, then return from the start of the string
+ * adjusting the length to be consistant with the "negative start" per SQL92.
+ * If the length is less than zero, return the remaining string.
+ *
+ */
+Datum
+bytea_substr(PG_FUNCTION_ARGS)
+{
+ bytea *string = PG_GETARG_BYTEA_P(0);
+ int32 m = PG_GETARG_INT32(1);
+ int32 n = PG_GETARG_INT32(2);
+ bytea *ret;
+ int len;
+
+ len = VARSIZE(string) - VARHDRSZ;
+
+ /* starting position after the end of the string? */
+ if (m > len)
+ {
+ m = 1;
+ n = 0;
+ }
+
+ /*
+ * starting position before the start of the string? then offset into
+ * the string per SQL92 spec...
+ */
+ else if (m < 1)
+ {
+ n += (m - 1);
+ m = 1;
+ }
+
+ /* m will now become a zero-based starting position */
+ m--;
+ if (((m + n) > len) || (n < 0))
+ n = (len - m);
+
+ ret = (bytea *) palloc(VARHDRSZ + n);
+ VARATT_SIZEP(ret) = VARHDRSZ + n;
+
+ memcpy(VARDATA(ret), VARDATA(string) + m, n);
+
+ PG_RETURN_BYTEA_P(ret);
+}
+
+/*
+ * byteapos -
+ * Return the position of the specified substring.
+ * Implements the SQL92 POSITION() function.
+ * Cloned from textpos and modified as required.
+ */
+Datum
+byteapos(PG_FUNCTION_ARGS)
+{
+ bytea *t1 = PG_GETARG_BYTEA_P(0);
+ bytea *t2 = PG_GETARG_BYTEA_P(1);
+ int pos;
+ int px,
+ p;
+ int len1,
+ len2;
+ char *p1,
+ *p2;
+
+ if (VARSIZE(t2) <= VARHDRSZ)
+ PG_RETURN_INT32(1); /* result for empty pattern */
+
+ len1 = (VARSIZE(t1) - VARHDRSZ);
+ len2 = (VARSIZE(t2) - VARHDRSZ);
+
+ p1 = VARDATA(t1);
+ p2 = VARDATA(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);
+}
+
/*-------------------------------------------------------------
* byteaGetByte
*