Pg: Add "$hex" pseudo-type

This commit is contained in:
Yorhel 2025-02-28 13:41:48 +01:00
parent baf0f90bd5
commit 15954f4ad5
6 changed files with 63 additions and 15 deletions

View file

@ -156,6 +156,33 @@ SENDFN(bytea) {
fustr_write(out, buf, len);
}
RECVFN(hex) {
SV *r = newSV(len ? len * 2 : 1);
SvPOK_only(r);
char *out = SvPVX(r);
const unsigned char *in = (const unsigned char *)buf;
int i;
for (i=0; i<len; i++) {
*out++ = PL_hexdigit[(in[i] >> 4) & 0x0f];
*out++ = PL_hexdigit[in[i] & 0x0f];
}
SvCUR_set(r, len * 2);
return r;
}
SENDFN(hex) {
STRLEN len;
const char *in = SvPV(val, len);
const char *end = in + len;
if (len % 2) SERR("Invalid hex string");
while (in < end) {
int v = (fu_hexdig(*in)<<4) + fu_hexdig(in[1]);
if (v > 0xff) SERR("Invalid hex string");
fustr_write_ch(out, v);
in += 2;
}
}
RECVFN(char) {
RLEN(1);
return newSVpvn(buf, len);
@ -515,7 +542,7 @@ RECVFN(uuid) {
RLEN(16);
char tmp[64];
char *out = tmp;
unsigned char *in = (unsigned char *)buf;
const unsigned char *in = (const unsigned char *)buf;
int i;
for (i=0; i<16; i++) {
if (i == 4 || i == 6 || i == 8 || i == 10) *out++ = '-';
@ -534,9 +561,8 @@ SENDFN(uuid) {
for (; *in; in++) {
if (*in == '}') break;
if (dig == 0x10 && *in == '-') continue;
unsigned char x = *in;
x = x >= '0' && x <= '9' ? x-'0' : x >= 'A' && x <= 'F' ? x-'A'+10 : x >= 'a' && x <= 'f' ? x-'a'+10 : 0x10;
if (x == 0x10) SERR("invalid UUID");
int x = fu_hexdig(*in);
if (x > 0x10) SERR("invalid UUID");
if (bytes >= 16) SERR("invalid UUID");
if (dig == 0x10) dig = x;
else {
@ -794,7 +820,8 @@ static const fupg_type fupg_builtin[] = {
/* List of special types for use with set_type() */
#define SPECIALS\
T("$date_str", date_str)
T("$date_str", date_str)\
T("$hex", hex )
static const fupg_type fupg_specials[] = {
#define T(name, fun) { 0, 0, {name"\0"}, fupg_send_##fun, fupg_recv_##fun },