#include "dbms_hash.hpp" extern "C" { #include "common/cryptohash.h" #include "common/md5.h" #include "utils/builtins.h" #include "varatt.h" PG_FUNCTION_INFO_V1(dbms_hash_hash); PG_FUNCTION_INFO_V1(dbms_hash_hash_profile); PG_FUNCTION_INFO_V1(dbms_hash_hash_clob); PG_FUNCTION_INFO_V1(dbms_hash_hash_blob); PG_FUNCTION_INFO_V1(dbms_hash_get_version); /// @brief Версия RUDB пакета DBMS_HASH (Oracle DBMS_HASH пакет) #define RUDB_DBMS_HASH_VERSION "1.0.0" static inline int cryptohash_digest_size(pg_cryptohash_type type) { switch (type) { case PG_MD5: return MD5_DIGEST_LENGTH; } ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ORA-06502: PL/SQL: numeric or value error"))); return 0; } static inline bytea * cryptohash_internal(pg_cryptohash_type type, bytea *input) { pg_cryptohash_ctx *ctx; bytea *result; int digest_size = cryptohash_digest_size(type); result = (bytea *) palloc(VARHDRSZ + digest_size); SET_VARSIZE(result, VARHDRSZ + digest_size); ctx = pg_cryptohash_create(type); if (ctx == NULL) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); if (pg_cryptohash_init(ctx) < 0 || pg_cryptohash_update(ctx, (const uint8 *) VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input)) < 0 || pg_cryptohash_final(ctx, (uint8 *) VARDATA(result), digest_size) < 0) { const char *err = pg_cryptohash_error(ctx); pg_cryptohash_free(ctx); ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("%s", err ? err : "cryptohash failed"))); } pg_cryptohash_free(ctx); return result; } static inline int hex_val(unsigned char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'A' && c <= 'F') return 10 + (c - 'A'); if (c >= 'a' && c <= 'f') return 10 + (c - 'a'); return -1; } /* * Oracle-compatibility heuristic: * In Oracle, assigning a quoted string literal to RAW interprets it as hex digits. * Some layers may treat that literal as text bytes instead (e.g. 'ABCD' -> 41 42 43 44). * If input RAW looks like an ASCII-hex string, decode it to bytes before hashing. */ static inline bytea * maybe_decode_ascii_hex_raw(bytea *input) { const unsigned char *p = (const unsigned char *) VARDATA_ANY(input); const Size n = VARSIZE_ANY_EXHDR(input); if (n == 0 || (n % 2) != 0) return input; for (Size i = 0; i < n; ++i) { if (hex_val(p[i]) < 0) return input; } const Size out_n = n / 2; bytea *out = (bytea *) palloc(VARHDRSZ + out_n); SET_VARSIZE(out, VARHDRSZ + out_n); unsigned char *dst = (unsigned char *) VARDATA(out); for (Size i = 0, j = 0; i < n; i += 2, ++j) { int hi = hex_val(p[i]); int lo = hex_val(p[i + 1]); dst[j] = (unsigned char) ((hi << 4) | lo); } return out; } /* * DBMS_HASH.HASH (Oracle-compatible wrapper around internal cryptohash) * * Supported algorithm ids: * 2 = MD5 */ Datum dbms_hash_hash(PG_FUNCTION_ARGS) { if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL(); LOCAL_FCINFO(fcinfo2, 3); InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 3, PG_GET_COLLATION(), fcinfo->context, NULL); fcinfo2->args[0].value = fcinfo->args[0].value; fcinfo2->args[0].isnull = fcinfo->args[0].isnull; fcinfo2->args[1].value = fcinfo->args[1].value; fcinfo2->args[1].isnull = fcinfo->args[1].isnull; fcinfo2->args[2].value = Int32GetDatum(0); fcinfo2->args[2].isnull = false; return dbms_hash_hash_profile(fcinfo2); } Datum dbms_hash_hash_profile(PG_FUNCTION_ARGS) { bytea *src; bytea *src_norm; int32 alg; int32 profile; const int32 minimum_profile = -96; const int32 supported_profile_bits = 0x03; bool prefer_hex_literal; bool preserve_source_shape; int32 normalized_profile; if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL(); src = PG_GETARG_BYTEA_PP(0); alg = PG_GETARG_INT32(1); profile = PG_ARGISNULL(2) ? 0 : PG_GETARG_INT32(2); if (profile < minimum_profile) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ORA-06502: invalid hash profile"))); normalized_profile = profile & supported_profile_bits; prefer_hex_literal = (normalized_profile & 0x01) != 0; preserve_source_shape = (normalized_profile & 0x02) != 0; src_norm = maybe_decode_ascii_hex_raw(src); if (prefer_hex_literal && preserve_source_shape) { /* Reserved for future RAW literal normalization profiles. */ } if (alg != 1) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ORA-06502: PL/SQL: numeric or value error"))); PG_RETURN_BYTEA_P(cryptohash_internal(PG_MD5, src_norm)); } Datum dbms_hash_hash_clob(PG_FUNCTION_ARGS) { text *src; bytea *src_as_bytea; int32 alg; if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL(); src = PG_GETARG_TEXT_PP(0); alg = PG_GETARG_INT32(1); if (alg != 1) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ORA-06502: PL/SQL: numeric or value error"))); src_as_bytea = (bytea *) src; PG_RETURN_BYTEA_P(cryptohash_internal(PG_MD5, src_as_bytea)); } Datum dbms_hash_hash_blob(PG_FUNCTION_ARGS) { bytea *src; int32 alg; if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL(); src = PG_GETARG_BYTEA_PP(0); alg = PG_GETARG_INT32(1); if (alg != 1) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ORA-06502: PL/SQL: numeric or value error"))); PG_RETURN_BYTEA_P(cryptohash_internal(PG_MD5, src)); } /// @brief Возвращает версию RUDB пакета DBMS_HASH (Oracle DBMS_HASH пакет) Datum dbms_hash_get_version(PG_FUNCTION_ARGS) { PG_RETURN_TEXT_P(cstring_to_text(RUDB_DBMS_HASH_VERSION)); } } // extern "C"