#include "utl_raw.hpp"
|
|
extern "C" {
|
#include "utils/builtins.h"
|
#include "utils/varlena.h"
|
#include "utils/numeric.h"
|
#include "catalog/pg_type.h"
|
#include "mb/pg_wchar.h"
|
#include "varatt.h"
|
}
|
|
#include <algorithm>
|
#include <cmath>
|
#include <cstring>
|
#include <limits>
|
|
#ifndef ERRCODE_PROGRAM_LIMIT_EXCEEDED
|
#define ERRCODE_PROGRAM_LIMIT_EXCEEDED MAKE_SQLSTATE('5','4','0','2','3')
|
#endif
|
|
/// @brief Версия RUDB пакета UTL_RAW (Oracle UTL_RAW пакет)
|
#define RUDB_UTL_RAW_VERSION "1.0.0"
|
|
// ---- Общее ----
|
|
static inline void check_raw_32k(Size n) {
|
if (n > 32767)
|
ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
errmsg("UTL_RAW: result exceeds RAW limit 32767 bytes")));
|
}
|
|
static inline bool is_little_endian() {
|
const uint16 x = 1;
|
return *reinterpret_cast<const unsigned char*>(&x) == 1;
|
}
|
|
|
static inline void encode_ieee_bytes(const unsigned char* machine, int endian, unsigned char* out, Size n) {
|
const bool little = is_little_endian();
|
if (endian == 3) { // machine
|
memcpy(out, machine, n);
|
} else if (endian == 1) { // big
|
if (little) {
|
for (Size i = 0; i < n; ++i) out[i] = machine[n - 1 - i];
|
} else {
|
memcpy(out, machine, n);
|
}
|
} else if (endian == 2) { // little
|
if (little) {
|
memcpy(out, machine, n);
|
} else {
|
for (Size i = 0; i < n; ++i) out[i] = machine[n - 1 - i];
|
}
|
} else {
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW: invalid endianess (use 1,2,3)")));
|
}
|
}
|
|
static inline void decode_ieee_bytes(const unsigned char* raw, int endian, unsigned char* machine_out, Size n) {
|
const bool little = is_little_endian();
|
if (endian == 3) {
|
memcpy(machine_out, raw, n);
|
} else if (endian == 1) {
|
if (little) {
|
for (Size i = 0; i < n; ++i) machine_out[i] = raw[n - 1 - i];
|
} else {
|
memcpy(machine_out, raw, n);
|
}
|
} else if (endian == 2) {
|
if (little) {
|
memcpy(machine_out, raw, n);
|
} else {
|
for (Size i = 0; i < n; ++i) machine_out[i] = raw[n - 1 - i];
|
}
|
} else {
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW: invalid endianess (use 1,2,3)")));
|
}
|
}
|
|
static inline Numeric utl_raw_numeric_from_int64(int64 v) {
|
return DatumGetNumeric(DirectFunctionCall1(int8_numeric, Int64GetDatum(v)));
|
}
|
static inline int64 utl_raw_int64_from_numeric(Numeric n) {
|
return DatumGetInt64(DirectFunctionCall1(numeric_int8, NumericGetDatum(n)));
|
}
|
|
// ---- BIT_* -----------------------------------------------------------------
|
|
extern "C" {
|
|
PG_FUNCTION_INFO_V1(utl_raw_bit_and);
|
/*
|
* @qdb_func: utl_raw_bit_and(raw, raw)
|
* @ora_func: UTL_RAW.BIT_AND(RAW, RAW)
|
*/
|
Datum utl_raw_bit_and(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL();
|
|
bytea* r1 = PG_GETARG_BYTEA_PP(0);
|
bytea* r2 = PG_GETARG_BYTEA_PP(1);
|
Size n1 = VARSIZE_ANY_EXHDR(r1), n2 = VARSIZE_ANY_EXHDR(r2);
|
Size nmin = std::min(n1, n2), nmax = std::max(n1, n2);
|
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)nmax);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
const unsigned char* p1 = (unsigned char*) VARDATA_ANY(r1);
|
const unsigned char* p2 = (unsigned char*) VARDATA_ANY(r2);
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
|
for (Size i=0;i<nmin;i++) po[i] = p1[i] & p2[i];
|
if (n1 > n2) memcpy(po + nmin, p1 + nmin, n1 - nmin);
|
else if (n2 > n1) memcpy(po + nmin, p2 + nmin, n2 - nmin);
|
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_bit_or);
|
/*
|
* @qdb_func: utl_raw_bit_or(raw, raw)
|
* @ora_func: UTL_RAW.BIT_OR(RAW, RAW)
|
*/
|
Datum utl_raw_bit_or(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL();
|
|
bytea* r1 = PG_GETARG_BYTEA_PP(0);
|
bytea* r2 = PG_GETARG_BYTEA_PP(1);
|
Size n1 = VARSIZE_ANY_EXHDR(r1), n2 = VARSIZE_ANY_EXHDR(r2);
|
Size nmin = std::min(n1, n2), nmax = std::max(n1, n2);
|
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)nmax);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
const unsigned char* p1 = (unsigned char*) VARDATA_ANY(r1);
|
const unsigned char* p2 = (unsigned char*) VARDATA_ANY(r2);
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
|
for (Size i=0;i<nmin;i++) po[i] = p1[i] | p2[i];
|
if (n1 > n2) memcpy(po + nmin, p1 + nmin, n1 - nmin);
|
else if (n2 > n1) memcpy(po + nmin, p2 + nmin, n2 - nmin);
|
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_bit_xor);
|
/*
|
* @qdb_func: utl_raw_bit_xor(raw, raw)
|
* @ora_func: UTL_RAW.BIT_XOR(RAW, RAW)
|
*/
|
Datum utl_raw_bit_xor(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL();
|
|
bytea* r1 = PG_GETARG_BYTEA_PP(0);
|
bytea* r2 = PG_GETARG_BYTEA_PP(1);
|
Size n1 = VARSIZE_ANY_EXHDR(r1), n2 = VARSIZE_ANY_EXHDR(r2);
|
Size nmin = std::min(n1, n2), nmax = std::max(n1, n2);
|
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)nmax);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
const unsigned char* p1 = (unsigned char*) VARDATA_ANY(r1);
|
const unsigned char* p2 = (unsigned char*) VARDATA_ANY(r2);
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
|
for (Size i=0;i<nmin;i++) po[i] = p1[i] ^ p2[i];
|
if (n1 > n2) memcpy(po + nmin, p1 + nmin, n1 - nmin);
|
else if (n2 > n1) memcpy(po + nmin, p2 + nmin, n2 - nmin);
|
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_bit_complement);
|
/*
|
* @qdb_func: utl_raw_bit_complement(raw)
|
* @ora_func: UTL_RAW.BIT_COMPLEMENT(RAW)
|
*/
|
Datum utl_raw_bit_complement(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(r);
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)n);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
const unsigned char* p = (unsigned char*) VARDATA_ANY(r);
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
for (Size i=0;i<n;i++) po[i] = ~p[i];
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
// ---- CAST FROM BINARY_* ---------------------------------------------------
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_from_binary_double);
|
/*
|
* @qdb_func: utl_raw_cast_from_binary_double(double, int4)
|
* @ora_func: UTL_RAW.CAST_FROM_BINARY_DOUBLE(BINARY_DOUBLE, BINARY_INTEGER)
|
*/
|
Datum utl_raw_cast_from_binary_double(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
double d = DatumGetFloat8(PG_GETARG_DATUM(0));
|
int endian = (PG_NARGS()>=2 && !PG_ARGISNULL(1)) ? PG_GETARG_INT32(1) : 1;
|
|
union { double d; unsigned char b[8]; } u;
|
u.d = d;
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum(8);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
encode_ieee_bytes(u.b, endian, (unsigned char*) VARDATA(out), 8);
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_from_binary_float);
|
/*
|
* @qdb_func: utl_raw_cast_from_binary_float(real, int4)
|
* @ora_func: UTL_RAW.CAST_FROM_BINARY_FLOAT(BINARY_FLOAT, BINARY_INTEGER)
|
*/
|
Datum utl_raw_cast_from_binary_float(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
float f = DatumGetFloat4(PG_GETARG_DATUM(0));
|
int endian = (PG_NARGS()>=2 && !PG_ARGISNULL(1)) ? PG_GETARG_INT32(1) : 1;
|
|
union { float f; unsigned char b[4]; } u;
|
u.f = f;
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum(4);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
encode_ieee_bytes(u.b, endian, (unsigned char*) VARDATA(out), 4);
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_from_binary_integer);
|
/*
|
* @qdb_func: utl_raw_cast_from_binary_integer(int4, int4)
|
* @ora_func: UTL_RAW.CAST_FROM_BINARY_INTEGER(BINARY_INTEGER, BINARY_INTEGER)
|
*/
|
Datum utl_raw_cast_from_binary_integer(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
int32 v = DatumGetInt32(PG_GETARG_DATUM(0));
|
int endian = (PG_NARGS()>=2 && !PG_ARGISNULL(1)) ? PG_GETARG_INT32(1) : 1;
|
|
union { int32 v; unsigned char b[4]; } u;
|
u.v = v;
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum(4);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
encode_ieee_bytes(u.b, endian, (unsigned char*) VARDATA(out), 4);
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_from_number);
|
/*
|
* @qdb_func: utl_raw_cast_from_number(numeric)
|
* @ora_func: UTL_RAW.CAST_FROM_NUMBER(NUMBER)
|
*/
|
Datum utl_raw_cast_from_number(PG_FUNCTION_ARGS) {
|
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
errmsg("UTL_RAW.CAST_FROM_NUMBER is not implemented yet (Oracle NUMBER binary format required)")));
|
PG_RETURN_NULL();
|
}
|
|
// ---- CAST TO BINARY_* -----------------------------------------------------
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_to_binary_double);
|
/*
|
* @qdb_func: utl_raw_cast_to_binary_double(raw, int4)
|
* @ora_func: UTL_RAW.CAST_TO_BINARY_DOUBLE(RAW, BINARY_INTEGER)
|
*/
|
Datum utl_raw_cast_to_binary_double(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(r);
|
if (n < 8)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.CAST_TO_BINARY_DOUBLE: RAW shorter than 8 bytes")));
|
int endian = (PG_NARGS()>=2 && !PG_ARGISNULL(1)) ? PG_GETARG_INT32(1) : 1;
|
|
union { double d; unsigned char b[8]; } u;
|
decode_ieee_bytes((unsigned char*) VARDATA_ANY(r), endian, u.b, 8);
|
|
// normalize -0 => +0; NaN оставляем NaN
|
if (u.d == 0.0) u.d = 0.0;
|
PG_RETURN_FLOAT8(u.d);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_to_binary_float);
|
/*
|
* @qdb_func: utl_raw_cast_to_binary_float(raw, int4)
|
* @ora_func: UTL_RAW.CAST_TO_BINARY_FLOAT(RAW, BINARY_INTEGER)
|
*/
|
Datum utl_raw_cast_to_binary_float(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(r);
|
if (n < 4)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.CAST_TO_BINARY_FLOAT: RAW shorter than 4 bytes")));
|
int endian = (PG_NARGS()>=2 && !PG_ARGISNULL(1)) ? PG_GETARG_INT32(1) : 1;
|
|
union { float f; unsigned char b[4]; } u;
|
decode_ieee_bytes((unsigned char*) VARDATA_ANY(r), endian, u.b, 4);
|
if (u.f == 0.0f) u.f = 0.0f;
|
PG_RETURN_FLOAT4(u.f);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_to_binary_integer);
|
/*
|
* @qdb_func: utl_raw_cast_to_binary_integer(raw, int4)
|
* @ora_func: UTL_RAW.CAST_TO_BINARY_INTEGER(RAW, BINARY_INTEGER)
|
*/
|
Datum utl_raw_cast_to_binary_integer(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(r);
|
if (n < 4)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.CAST_TO_BINARY_INTEGER: RAW shorter than 4 bytes")));
|
int endian = (PG_NARGS()>=2 && !PG_ARGISNULL(1)) ? PG_GETARG_INT32(1) : 1;
|
|
union { int32 v; unsigned char b[4]; } u;
|
decode_ieee_bytes((unsigned char*) VARDATA_ANY(r), endian, u.b, 4);
|
PG_RETURN_INT32(u.v);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_to_number);
|
/*
|
* @qdb_func: utl_raw_cast_to_number(raw)
|
* @ora_func: UTL_RAW.CAST_TO_NUMBER(RAW)
|
*/
|
Datum utl_raw_cast_to_number(PG_FUNCTION_ARGS) {
|
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
errmsg("UTL_RAW.CAST_TO_NUMBER is not implemented yet (Oracle NUMBER binary format required)")));
|
PG_RETURN_NULL();
|
}
|
|
// ---- CAST TO/FROM (N)VARCHAR2/RAW -----------------------------------------
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_to_nvarchar2);
|
/*
|
* @qdb_func: utl_raw_cast_to_nvarchar2(raw)
|
* @ora_func: UTL_RAW.CAST_TO_NVARCHAR2(RAW)
|
*/
|
Datum utl_raw_cast_to_nvarchar2(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(r);
|
text* t = cstring_to_text_with_len((char*) VARDATA_ANY(r), n);
|
PG_RETURN_TEXT_P(t);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_to_varchar2);
|
/*
|
* @qdb_func: utl_raw_cast_to_varchar2(raw)
|
* @ora_func: UTL_RAW.CAST_TO_VARCHAR2(RAW)
|
*/
|
Datum utl_raw_cast_to_varchar2(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(r);
|
text* t = cstring_to_text_with_len((char*) VARDATA_ANY(r), n);
|
PG_RETURN_TEXT_P(t);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_cast_to_raw);
|
/*
|
* @qdb_func: utl_raw_cast_to_raw(text)
|
* @ora_func: UTL_RAW.CAST_TO_RAW(VARCHAR2)
|
*/
|
Datum utl_raw_cast_to_raw(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
text* t = PG_GETARG_TEXT_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(t);
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)n);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
memcpy(VARDATA(out), VARDATA_ANY(t), n);
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_compare);
|
/*
|
* @qdb_func: utl_raw_compare(raw, raw, raw)
|
* @ora_func: UTL_RAW.COMPARE(RAW, RAW, RAW)
|
*/
|
Datum utl_raw_compare(PG_FUNCTION_ARGS) {
|
bytea* r1 = PG_ARGISNULL(0) ? nullptr : PG_GETARG_BYTEA_PP(0);
|
bytea* r2 = PG_ARGISNULL(1) ? nullptr : PG_GETARG_BYTEA_PP(1);
|
|
if (r1 == nullptr && r2 == nullptr) {
|
PG_RETURN_NUMERIC(utl_raw_numeric_from_int64(0));
|
}
|
|
unsigned char pad = 0x00;
|
if (PG_NARGS() >= 3 && !PG_ARGISNULL(2)) {
|
bytea* p = PG_GETARG_BYTEA_PP(2);
|
Size pn = VARSIZE_ANY_EXHDR(p);
|
if (pn != 1)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.COMPARE: pad must be 1 byte")));
|
pad = *(unsigned char*) VARDATA_ANY(p);
|
}
|
|
const unsigned char* p1 = r1 ? (unsigned char*) VARDATA_ANY(r1) : nullptr;
|
const unsigned char* p2 = r2 ? (unsigned char*) VARDATA_ANY(r2) : nullptr;
|
Size n1 = r1 ? VARSIZE_ANY_EXHDR(r1) : 0;
|
Size n2 = r2 ? VARSIZE_ANY_EXHDR(r2) : 0;
|
|
Size nmax = std::max(n1, n2);
|
for (Size i=0;i<nmax;i++) {
|
unsigned char a = (i<n1)? p1[i] : pad;
|
unsigned char b = (i<n2)? p2[i] : pad;
|
if (a!=b) PG_RETURN_NUMERIC(utl_raw_numeric_from_int64((int64)(i+1)));
|
}
|
PG_RETURN_NUMERIC(utl_raw_numeric_from_int64(0));
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_concat);
|
/*
|
* @qdb_func: utl_raw_concat(raw, raw, raw, raw, raw, raw, raw, raw, raw, raw, raw, raw)
|
* @ora_func: UTL_RAW.CONCAT(RAW, RAW, RAW, RAW, RAW, RAW, RAW, RAW, RAW, RAW, RAW, RAW)
|
*/
|
Datum utl_raw_concat(PG_FUNCTION_ARGS) {
|
// До 12 аргументов raw; NULL трактуем как пусто
|
Size total = 0;
|
bytea* args[12] = {nullptr};
|
int nargs = std::min(12, (int)PG_NARGS());
|
for (int i=0;i<nargs;i++) {
|
if (!PG_ARGISNULL(i)) {
|
args[i] = PG_GETARG_BYTEA_PP(i);
|
total += VARSIZE_ANY_EXHDR(args[i]);
|
if (total > 32767)
|
ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
errmsg("UTL_RAW.CONCAT: result exceeds RAW limit 32767 bytes")));
|
}
|
}
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)total);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
Size off=0;
|
for (int i=0;i<nargs;i++) {
|
if (!args[i]) continue;
|
Size n = VARSIZE_ANY_EXHDR(args[i]);
|
memcpy(po+off, VARDATA_ANY(args[i]), n);
|
off += n;
|
}
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_convert);
|
/*
|
* @qdb_func: utl_raw_convert(raw, text, text)
|
* @ora_func: UTL_RAW.CONVERT(RAW, VARCHAR2, VARCHAR2)
|
*/
|
Datum utl_raw_convert(PG_FUNCTION_ARGS) {
|
// Требуется точное соответствие Oracle NLS. Здесь — честная заглушка с ошибкой.
|
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
errmsg("UTL_RAW.CONVERT is not implemented yet (requires Oracle↔PG charset mapping/NLS)")));
|
PG_RETURN_NULL();
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_copies);
|
/*
|
* @qdb_func: utl_raw_copies(raw, numeric)
|
* @ora_func: UTL_RAW.COPIES(RAW, NUMBER)
|
*/
|
Datum utl_raw_copies(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.COPIES: r and n must be provided")));
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size rn = VARSIZE_ANY_EXHDR(r);
|
if (rn == 0)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.COPIES: r must be non-empty")));
|
|
Numeric nnum = PG_GETARG_NUMERIC(1);
|
int64 n = utl_raw_int64_from_numeric(nnum);
|
if (n < 1)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.COPIES: n must be >= 1")));
|
|
// Проверка переполнения
|
if (rn > 0 && n > (int64)(32767 / rn))
|
ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
errmsg("UTL_RAW.COPIES: result exceeds RAW limit 32767 bytes")));
|
|
Size outn = rn * (Size)n;
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)outn);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
const unsigned char* pr = (unsigned char*) VARDATA_ANY(r);
|
Size off=0;
|
for (int64 i=0; i<n; ++i) {
|
memcpy(po+off, pr, rn);
|
off += rn;
|
}
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_length);
|
/*
|
* @qdb_func: utl_raw_length(raw)
|
* @ora_func: UTL_RAW.LENGTH(RAW)
|
*/
|
Datum utl_raw_length(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(r);
|
PG_RETURN_NUMERIC(utl_raw_numeric_from_int64((int64)n));
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_overlay);
|
/*
|
* @qdb_func: utl_raw_overlay(raw, raw, int4, int4, raw)
|
* @ora_func: UTL_RAW.OVERLAY(RAW, RAW, BINARY_INTEGER, BINARY_INTEGER, RAW)
|
*/
|
Datum utl_raw_overlay(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.OVERLAY: overlay_str must be provided")));
|
if (PG_ARGISNULL(1))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.OVERLAY: target must be provided")));
|
|
bytea* overlay = PG_GETARG_BYTEA_PP(0);
|
bytea* target = PG_GETARG_BYTEA_PP(1);
|
Size on = VARSIZE_ANY_EXHDR(overlay);
|
Size tn = VARSIZE_ANY_EXHDR(target);
|
if (on == 0)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.OVERLAY: overlay_str must be non-empty")));
|
|
int32 pos = 1;
|
int32 len = -1;
|
unsigned char pad = 0x00;
|
|
if (PG_NARGS() >= 3 && !PG_ARGISNULL(2)) pos = DatumGetInt32(PG_GETARG_DATUM(2));
|
if (PG_NARGS() >= 4 && !PG_ARGISNULL(3)) len = DatumGetInt32(PG_GETARG_DATUM(3));
|
if (PG_NARGS() >= 5 && !PG_ARGISNULL(4)) {
|
bytea* p = PG_GETARG_BYTEA_PP(4);
|
Size pn = VARSIZE_ANY_EXHDR(p);
|
if (pn != 1)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.OVERLAY: pad must be 1 byte")));
|
pad = *(unsigned char*) VARDATA_ANY(p);
|
}
|
|
if (pos < 1)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.OVERLAY: pos must be >= 1")));
|
if (len < 0) len = (int32) on;
|
|
// Рассчитать длину результата и собрать.
|
Size prefix = (pos > (int32)tn) ? (Size)tn : (Size)(pos - 1);
|
Size needed_pad = (pos > (int32)tn) ? (Size)(pos - 1 - tn) : 0;
|
|
Size used_from_overlay = std::min<Size>(len, on);
|
Size overlay_out_len = len; // ровно len байт попадут в результат на участке
|
|
Size tail_start_in_target = (pos - 1) + len;
|
Size tail_len = (tail_start_in_target < tn) ? (tn - tail_start_in_target) : 0;
|
|
Size outn = prefix + needed_pad + overlay_out_len + tail_len;
|
check_raw_32k(outn);
|
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)outn);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
const unsigned char* pt = (unsigned char*) VARDATA_ANY(target);
|
const unsigned char* ov = (unsigned char*) VARDATA_ANY(overlay);
|
|
// 1) prefix (существующая часть target до pos-1)
|
if (prefix > 0) memcpy(po, pt, prefix);
|
Size off = prefix;
|
|
// 2) pad до pos-1 если требовался
|
for (Size i=0;i<needed_pad;i++) po[off++] = pad;
|
|
// 3) overlay (used_from_overlay) + паддинг до len
|
if (used_from_overlay > 0) {
|
memcpy(po+off, ov, used_from_overlay);
|
off += used_from_overlay;
|
}
|
for (Size i=used_from_overlay;i<(Size)len;i++) po[off++] = pad;
|
|
// 4) хвост исходного target после замененного окна
|
if (tail_len > 0) {
|
memcpy(po+off, pt + tail_start_in_target, tail_len);
|
off += tail_len;
|
}
|
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_reverse);
|
/*
|
* @qdb_func: utl_raw_reverse(raw)
|
* @ora_func: UTL_RAW.REVERSE(RAW)
|
*/
|
Datum utl_raw_reverse(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.REVERSE: r must be provided")));
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(r);
|
if (n == 0)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.REVERSE: r must be non-empty")));
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)n);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
const unsigned char* pr = (unsigned char*) VARDATA_ANY(r);
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
for (Size i=0;i<n;i++) po[i] = pr[n-1-i];
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_substr);
|
/*
|
* @qdb_func: utl_raw_substr(raw, int4, int4)
|
* @ora_func: UTL_RAW.SUBSTR(RAW, BINARY_INTEGER, BINARY_INTEGER)
|
*/
|
Datum utl_raw_substr(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
|
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size n = VARSIZE_ANY_EXHDR(r);
|
if (PG_ARGISNULL(1))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.SUBSTR: pos is required")));
|
int32 pos = DatumGetInt32(PG_GETARG_DATUM(1));
|
if (pos == 0)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.SUBSTR: pos cannot be 0")));
|
|
// Вычисляем 1-based старт
|
int64 start;
|
if (pos > 0) start = pos;
|
else start = (int64)n + pos + 1; // pos<0: от конца
|
|
if (start < 1 || start > (int64)n)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.SUBSTR: pos out of range")));
|
|
int64 len;
|
if (PG_NARGS() >= 3 && !PG_ARGISNULL(2)) len = DatumGetInt32(PG_GETARG_DATUM(2));
|
else len = (int64)n - (start - 1);
|
if (len < 1) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.SUBSTR: len must be >= 1")));
|
if (len > (int64)n - (start - 1))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.SUBSTR: len out of range")));
|
|
Size s = (Size) (start - 1);
|
Size L = (Size) len;
|
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)L);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
memcpy(VARDATA(out), ((unsigned char*) VARDATA_ANY(r)) + s, L);
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_translate);
|
/*
|
* @qdb_func: utl_raw_translate(raw, raw, raw)
|
* @ora_func: UTL_RAW.TRANSLATE(RAW, RAW, RAW)
|
*/
|
Datum utl_raw_translate(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.TRANSLATE: r, from_set, to_set must be provided")));
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
bytea* fs = PG_GETARG_BYTEA_PP(1);
|
bytea* ts = PG_GETARG_BYTEA_PP(2);
|
Size rn = VARSIZE_ANY_EXHDR(r);
|
Size fn = VARSIZE_ANY_EXHDR(fs);
|
Size tn = VARSIZE_ANY_EXHDR(ts);
|
if (rn == 0 || fn == 0 || tn == 0)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.TRANSLATE: r/from_set/to_set must be non-empty")));
|
|
// map: -1=no map; -2=delete; 0..255=map byte
|
int map[256];
|
bool seen[256];
|
for (int i=0;i<256;i++){ map[i] = -1; seen[i]=false; }
|
|
const unsigned char* f = (unsigned char*) VARDATA_ANY(fs);
|
const unsigned char* t = (unsigned char*) VARDATA_ANY(ts);
|
for (Size i=0;i<fn;i++) {
|
unsigned char k = f[i];
|
if (seen[k]) continue;
|
seen[k] = true;
|
if (i < tn) map[k] = (int)t[i];
|
else map[k] = -2; // delete
|
}
|
|
const unsigned char* pr = (unsigned char*) VARDATA_ANY(r);
|
// Подсчёт длины результата
|
Size outn = 0;
|
for (Size i=0;i<rn;i++) {
|
int m = map[pr[i]];
|
if (m == -2) continue; // delete
|
outn += 1; // keep original or mapped
|
}
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)outn);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
Size off=0;
|
for (Size i=0;i<rn;i++) {
|
int m = map[pr[i]];
|
if (m == -2) continue;
|
po[off++] = (m >= 0 ? (unsigned char)m : pr[i]);
|
}
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_transliterate);
|
/*
|
* @qdb_func: utl_raw_transliterate(raw, raw, raw, raw)
|
* @ora_func: UTL_RAW.TRANSLITERATE(RAW, RAW, RAW, RAW)
|
*/
|
Datum utl_raw_transliterate(PG_FUNCTION_ARGS) {
|
if (PG_ARGISNULL(0))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.TRANSLITERATE: r must be provided")));
|
bytea* r = PG_GETARG_BYTEA_PP(0);
|
Size rn = VARSIZE_ANY_EXHDR(r);
|
if (rn == 0)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.TRANSLITERATE: r must be non-empty")));
|
|
bytea* ts = (PG_NARGS()>=2 && !PG_ARGISNULL(1)) ? PG_GETARG_BYTEA_PP(1) : nullptr;
|
bytea* fs = (PG_NARGS()>=3 && !PG_ARGISNULL(2)) ? PG_GETARG_BYTEA_PP(2) : nullptr;
|
unsigned char pad = 0x00;
|
if (PG_NARGS()>=4 && !PG_ARGISNULL(3)) {
|
bytea* p = PG_GETARG_BYTEA_PP(3);
|
Size pn = VARSIZE_ANY_EXHDR(p);
|
if (pn != 1)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.TRANSLITERATE: pad must be 1 byte")));
|
pad = *(unsigned char*) VARDATA_ANY(p);
|
}
|
|
Size fn, tn;
|
const unsigned char *f, *t;
|
unsigned char full_from[256];
|
if (fs) {
|
fn = VARSIZE_ANY_EXHDR(fs);
|
f = (unsigned char*) VARDATA_ANY(fs);
|
} else {
|
fn = 256;
|
for (int i=0;i<256;i++) full_from[i]=(unsigned char)i;
|
f = full_from;
|
}
|
if (ts) {
|
tn = VARSIZE_ANY_EXHDR(ts);
|
t = (unsigned char*) VARDATA_ANY(ts);
|
} else {
|
tn = 0; t = nullptr;
|
}
|
|
int map[256];
|
bool seen[256];
|
for (int i=0;i<256;i++){ map[i] = -1; seen[i]=false; }
|
for (Size i=0;i<fn;i++) {
|
unsigned char k = f[i];
|
if (seen[k]) continue;
|
seen[k] = true;
|
if (i < tn) map[k] = (int)t[i];
|
else map[k] = (int)pad;
|
}
|
|
const unsigned char* pr = (unsigned char*) VARDATA_ANY(r);
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)rn);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
for (Size i=0;i<rn;i++) {
|
int m = map[pr[i]];
|
po[i] = (m >= 0 ? (unsigned char)m : pr[i]);
|
}
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_xrange);
|
/*
|
* @qdb_func: utl_raw_xrange(raw, raw)
|
* @ora_func: UTL_RAW.XRANGE(RAW, RAW)
|
*/
|
Datum utl_raw_xrange(PG_FUNCTION_ARGS) {
|
unsigned char start = 0x00, end = 0xFF;
|
|
if (PG_NARGS()>=1 && !PG_ARGISNULL(0)) {
|
bytea* s = PG_GETARG_BYTEA_PP(0);
|
Size sn = VARSIZE_ANY_EXHDR(s);
|
if (sn != 1)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.XRANGE: start_byte must be 1 byte")));
|
start = *(unsigned char*) VARDATA_ANY(s);
|
}
|
if (PG_NARGS()>=2 && !PG_ARGISNULL(1)) {
|
bytea* e = PG_GETARG_BYTEA_PP(1);
|
Size en = VARSIZE_ANY_EXHDR(e);
|
if (en != 1)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("UTL_RAW.XRANGE: end_byte must be 1 byte")));
|
end = *(unsigned char*) VARDATA_ANY(e);
|
}
|
|
Size outn;
|
if (start <= end) outn = (Size)(end - start + 1);
|
else outn = (Size)((255 - start + 1) + (end + 1)); // wrap
|
bytea* out;
|
{
|
LOCAL_FCINFO(fcinfo2, 1);
|
InitFunctionCallInfoData(*fcinfo2, NULL, 1, InvalidOid, NULL, NULL);
|
|
fcinfo2->args[0].value = Int64GetDatum((int64_t)outn);
|
fcinfo2->args[0].isnull = false;
|
|
out = DatumGetByteaP(utl_raw_make_raw(fcinfo2));
|
}
|
unsigned char* po = (unsigned char*) VARDATA(out);
|
if (start <= end) {
|
for (unsigned int v = start, i=0; v <= end; ++v, ++i) po[i] = (unsigned char)v;
|
} else {
|
Size i=0;
|
for (unsigned int v = start; v <= 255; ++v) po[i++] = (unsigned char)v;
|
for (unsigned int v = 0; v <= end; ++v) po[i++] = (unsigned char)v;
|
}
|
PG_RETURN_BYTEA_P(out);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_get_version);
|
/// @brief Возвращает версию RUDB пакета UTL_RAW (Oracle UTL_RAW пакет)
|
Datum utl_raw_get_version(PG_FUNCTION_ARGS) {
|
PG_RETURN_TEXT_P(cstring_to_text(RUDB_UTL_RAW_VERSION));
|
}
|
|
PG_FUNCTION_INFO_V1(utl_raw_make_raw);
|
Datum utl_raw_make_raw(PG_FUNCTION_ARGS) {
|
int64_t n = PG_GETARG_INT64(0);
|
|
bytea* b = (bytea*) palloc(VARHDRSZ + n);
|
SET_VARSIZE(b, VARHDRSZ + n);
|
PG_RETURN_BYTEA_P(b);
|
}
|
} // extern "C"
|