From 4c6cd64194bda548e10c5b808b90adb3b10dba02 Mon Sep 17 00:00:00 2001 From: BLM Date: Wed, 22 Jul 2015 14:41:05 -0500 Subject: [PATCH] Fixed more issues with binary I/O --- pg_libphonenumber.cpp | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/pg_libphonenumber.cpp b/pg_libphonenumber.cpp index f1451f7..2f2cf0e 100755 --- a/pg_libphonenumber.cpp +++ b/pg_libphonenumber.cpp @@ -159,26 +159,39 @@ extern "C" { phone_number_recv(PG_FUNCTION_ARGS) { try { StringInfo buf = (StringInfo)PG_GETARG_POINTER(0); - ShortPhoneNumber* result; + ShortPhoneNumber* number; - result = (ShortPhoneNumber*)palloc(sizeof(ShortPhoneNumber)); - //TODO: make portable. - *result = reinterpret_castpq_getmsgint64(buf); - PG_RETURN_POINTER(result); + number = (ShortPhoneNumber*)palloc(sizeof(ShortPhoneNumber)); + //TODO: make portable (fix endianness issues, etc.). + pq_copymsgbytes(buf, (char*)number, sizeof(ShortPhoneNumber)); + PG_RETURN_POINTER(number); + } catch(const std::bad_alloc& e) { + reportOutOfMemory(); + } catch (const std::exception& e) { + reportGenericError(e); + } + + PG_RETURN_NULL(); } PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_send); PGDLLEXPORT Datum - phone_number_send(PG_FUNCTION_ARGS) - { - ShortPhoneNumber *number = (ShortPhoneNumber*)PG_GETARG_POINTER(0); - StringInfoData buf; + phone_number_send(PG_FUNCTION_ARGS) { + try { + const ShortPhoneNumber *number = (ShortPhoneNumber*)PG_GETARG_POINTER(0); + StringInfoData buf; - pq_begintypsend(&buf); - //TODO: make portable. - pq_sendint64(&buf, reinterpret_cast*number);; - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + pq_begintypsend(&buf); + pq_sendbytes(&buf, (const char*)number, sizeof(ShortPhoneNumber)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } catch(const std::bad_alloc& e) { + reportOutOfMemory(); + } catch (const std::exception& e) { + reportGenericError(e); + } + + PG_RETURN_NULL(); } }