/* * As a "hack" to avoid extra allocation and complications by using PQunescapeBytea() * we instead unescape the buffer retrieved via PQgetvalue 'in-place'. This should * be safe as unescape will only modify internal bytes in the buffer and not change * the buffer pointer. See also unescape_bytea() above. */ const void *PostgresqlResultSet_getBlob(T R, int columnIndex, int *size) { assert(R); int i = checkAndSetColumnIndex(columnIndex, R->columnCount); if (PQgetisnull(R->res, R->currentRow, i)) return NULL; return unescape_bytea((uchar_t*)PQgetvalue(R->res, R->currentRow, i), PQgetlength(R->res, R->currentRow, i), size); }
static char *file_export(DBG_Struct *dbg, char *id, char *socket) { static char *filename; char *str; json_object *obj; NETFILE *fp; ValueStruct *ret, *value; ValueStruct *retval; ret = monblob_export(dbg, id); obj = json_object_new_object(); if (!ret) { fprintf(stderr, "[%s] is not registered\n", id); json_object_object_add(obj, "status", json_object_new_int(404)); retval = NULL; } else { value = GetItemLongName(ret, "file_data"); retval = unescape_bytea(dbg, value); char *id; id = ValueToString(GetItemLongName(ret, "id"), dbg->coding); json_object_object_add(obj, "id", json_object_new_string(id)); filename = ValueToString(GetItemLongName(ret, "filename"), dbg->coding); json_object_object_add(obj, "filename", json_object_new_string(filename)); char *content_type; content_type = ValueToString(GetItemLongName(ret, "content_type"), dbg->coding); if (content_type == NULL || strlen(content_type) == 0) { json_object_object_add( obj, "content-type", json_object_new_string("application/octet-stream")); } else { json_object_object_add(obj, "content-type", json_object_new_string(content_type)); } json_object_object_add(obj, "status", json_object_new_int(200)); } str = (char *)json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN); fp = ConnectBlobAPI(socket); SendString(fp, str); SendValue(fp, retval); FreeValueStruct(retval); Flush(fp); DisconnectBlobAPI(fp); FreeValueStruct(ret); return filename; }
CAMLprim value PQgetescval_stub(value v_res, value v_tup_num, value v_field_num) { CAMLparam1(v_res); value v_str; PGresult *res = get_res(v_res); size_t field_num = Long_val(v_field_num); size_t tup_num = Long_val(v_tup_num); char *str = PQgetvalue(res, tup_num, field_num); if (PQfformat(res, field_num) == 0) { if (str != NULL && str[0] == '\\' && str[1] == 'x') v_str = unescape_bytea_9x(str + 2); else v_str = unescape_bytea(str); } else { /* Assume binary format! */ size_t len = PQgetlength(res, tup_num, field_num); v_str = len ? caml_alloc_string(len) : v_empty_string; memcpy(String_val(v_str), str, len); } CAMLreturn(v_str); }
CAMLprim value PQunescapeBytea_stub(value v_from) { return unescape_bytea(String_val(v_from)); }