/*------------------------------------------------------------------------- * * checksum_utility.c * Generic data and text checksum helpers * *------------------------------------------------------------------------- */ #include "postgres.h" #include "fmgr.h" #include "access/htup_details.h" #include "utils/builtins.h" #include "checksum_internal.h" /*------------------------------------------------------------------------- * UTILITY FUNCTIONS *------------------------------------------------------------------------- */ /* * pg_data_checksum - Generic data checksum utility function * * Computes a checksum for arbitrary binary data. Useful for application * developers who want to checksum their own data using the same algorithm. * * Parameters: * data: Binary data to checksum (bytea) * seed: Initial hash value (can be used for different contexts) */ PG_FUNCTION_INFO_V1(pg_data_checksum); Datum pg_data_checksum(PG_FUNCTION_ARGS) { bytea *data; uint32 seed; uint32 checksum; if (PG_ARGISNULL(0)) PG_RETURN_NULL(); data = PG_GETARG_BYTEA_PP(0); seed = PG_GETARG_INT32(1); checksum = pg_checksum_data_custom(VARDATA_ANY(data), VARSIZE_ANY_EXHDR(data), seed); PG_RETURN_INT32((int32)checksum); } /* * pg_text_checksum - Text data checksum utility function * * Convenience wrapper for checksumming text data. Useful for string * comparison and text content verification. */ PG_FUNCTION_INFO_V1(pg_text_checksum); Datum pg_text_checksum(PG_FUNCTION_ARGS) { text *input_text; uint32 seed; uint32 checksum; if (PG_ARGISNULL(0)) PG_RETURN_NULL(); input_text = PG_GETARG_TEXT_PP(0); seed = PG_GETARG_INT32(1); checksum = pg_checksum_data_custom(VARDATA_ANY(input_text), VARSIZE_ANY_EXHDR(input_text), seed); PG_RETURN_INT32((int32)checksum); }