Input Data:
The set of SQL sripts defining the tables which I ported:
The notices which I created when porting:
#include "postgres.h" #include "executor/spi.h" #include "funcapi.h" #include "utils/lsyscache.h" #include "fmgr.h" typedef struct { int4 length; unsigned char data[1]; } image; PG_FUNCTION_INFO_V1(image_in); Datum image_in(PG_FUNCTION_ARGS) { unsigned char *in = PG_GETARG_CSTRING(0);//"AABBCCDDEE1122";// int i = 0, out_len; const unsigned char convert[256] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //elog(WARNING,"IN_STRING: %s", in); /* Compute the length */ while (in[++i]); out_len = i / 2; //elog(WARNING,"IN_LENGTH: %d", i); image *im = (image *) palloc(VARHDRSZ + out_len); memset(im, 0, VARHDRSZ + out_len); unsigned char *out_data = im->data; im->length = out_len + VARHDRSZ; for(i = 0; i < out_len; i++) { out_data[i] = convert[in[i * 2]] * ((unsigned char) 16) + convert[in[i * 2 + 1]]; // elog(WARNING,"%d",out_data[i]); } PG_RETURN_POINTER(im); } PG_FUNCTION_INFO_V1(image_out); Datum image_out(PG_FUNCTION_ARGS) { const char convert[] = {48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70}; int i; image *im = (image *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); unsigned char *data_in = im->data; int len = im->length - VARHDRSZ; //elog(WARNING, "OUT_LENGTH: %d", len); unsigned char *data_out = palloc(2 * len * sizeof(char) + 1); for(i = 0; i < len ; i++) { data_out[i * 2] = convert[data_in[i] / 16]; data_out[i * 2 + 1] = convert[data_in[i] % 16]; } data_out[2 * len ] = 0; PG_FREE_IF_COPY(im, 0); PG_RETURN_CSTRING(data_out); }
The image type SQL definition:
DROP TYPE image CASCADE; CREATE or REPLACE FUNCTION image_in(cstring) RETURNS image AS '/home/math/sdss_db/image_type/image_type.so' LANGUAGE C IMMUTABLE STRICT; CREATE OR REPLACE FUNCTION image_out(image) RETURNS cstring AS '/home/math/sdss_db/image_type/image_type.so' LANGUAGE C IMMUTABLE STRICT; CREATE TYPE image ( INPUT = image_in, OUTPUT = image_out, INTERNALLENGTH = -1, STORAGE = external );