2

In PostgreSQL's libpq library (the C API), I'm trying to convert from a bytea field that is returned in text representation, to a raw binary string.

For example, for a single newline character, the text representation is "\\x0a" (i.e. the characters '\\', 'x', '0' and 'a', terminated with a null byte).

According to the documentation this text representation can be converted back to a binary representation by using PQunescapeBytea(). However, when I use that, I just get a char * that is exactly the same, but with the leading slash removed "x0a". What am I doing wrong?

char * value  = PGgetvalue(res, row, col);
size_t length = 0;
char * bytes  = PQunescapeBytea(value, &length);
// inspection of 'bytes' for length shows it's the same, with the '\' removed
d11wtq
  • 34,788
  • 19
  • 120
  • 195

1 Answers1

1

Well, the machine I'm developing on has PostgreSQL 8.4, while the server has 9.1, which is doing hex representation of bytea, not understood by the client. I need to do:

SET bytea_output = escape

However, since I don't want to tamper with the user settings (which may even change from beneath me), I opted to provide a fallback implementation and by-pass libpq when the string looks like the new format. Namespace prefix removed for brevity. Not that I'm returning a Ruby String, hence the rb_* and VALUE stuff in my code.

/** Predicate to test of string is of the form \x0afe... */
#define NEW_HEX_P(s, len) (len > 2 && s[0] == '\\' && s[1] == 'x')

/** Lookup table for fast conversion of bytea hex strings to binary data */
static char * HexLookup;

/** Cast from a bytea to a String according to the new (PG 9.0) hex format */
static VALUE cast_bytea_hex(char * hex, size_t len) {
  if ((len % 2) != 0) {
    rb_raise(rb_eRuntimeError,
        "Bad hex value provided for bytea (length not divisible by 2)");
  }

  size_t   buflen = (len - 2) / 2;
  char   * buffer = malloc(sizeof(char) * buflen);
  char   * s      = hex + 2;
  char   * b      = buffer;

  if (buffer == NULL) {
    rb_raise(rb_eRuntimeError,
        "Failed to allocate %ld bytes for bytea conversion", buflen);
  }

  for (; *s; s += 2, ++b)
    *b = (HexLookup[*s] << 4) + (HexLookup[*(s + 1)]);

  VALUE str = rb_str_new(buffer, buflen);
  free(buffer);

  return str;
}

/** Cast from a bytea to a String according to a regular escape format */
static VALUE cast_bytea_escape(char * escaped, size_t len) {
  unsigned char * buffer  = PQunescapeBytea(escaped, &len);

  if (buffer == NULL) {
    rb_raise(rb_eRuntimeError,
        "Failed to allocate memory for PQunescapeBytea() conversion");
  }

  VALUE str = rb_str_new(buffer, len);
  PQfreemem(buffer);

  return str;
}

/** Get the value as a ruby type */
VALUE cast_value(PGresult * res, int row, int col) {
  if (PQgetisnull(res, row, col)) {
    return Qnil;
  }

  char * value  = PQgetvalue(res, row, col);
  int    length = PQgetlength(res, row, col);

  switch (PQftype(res, col)) {
    case INT2OID:
    case INT4OID:
    case INT8OID:
      return rb_cstr2inum(value, 10);

    case BOOLOID:
      return (value[0] == 't') ? Qtrue : Qfalse;

    case BYTEAOID:
      if (NEW_HEX_P(value, length)) {
        return cast_bytea_hex(value, length);
      } else {
        return cast_bytea_escape(value, length);
      }

    default:
      return rb_str_new(value, length);
  }
}

/* Initialize hex decoding lookup table. Must be invoked once, before use. */
void Init_casts(void) {
  HexLookup = malloc(sizeof(char) * 128);

  if (HexLookup == NULL) {
    rb_raise(rb_eRuntimeError,
        "Failed to allocate 128 bytes for internal lookup table");
  }

  char c;

  for (c = '\0'; c < '\x7f'; ++c)
    HexLookup[c] = 0; // Default to NULLs so we don't crash. May be a bad idea.

  for (c = '0'; c <= '9'; ++c)
    HexLookup[c] = c - '0';

  for (c = 'a'; c <= 'f'; ++c)
    HexLookup[c] = 10 + c - 'a';

  for (c = 'A'; c <= 'F'; ++c)
    HexLookup[c] = 10 + c - 'A';
}

So basically when I try and cast a value that has the OID of BYTEAOID, I first check if it looks like the new hex format, and if it does, I perform a decode against a lookup table, otherwise I just go via PQunescapeBytea() as normal. Kinda sucks, but it's the lesser of two evils.

Init_casts();

VALUE rubyval = cast_value(res, 1, 6); // Get the ruby type in row 1, column 6
d11wtq
  • 34,788
  • 19
  • 120
  • 195