2

I'm writing an extension for postgres in C.

This function will receive a bytea from hexstring, convert bytea into char* then do some operations on this char*, finally return a number.

In SQL level, I have something like:

select my_function(decode(**hex_string**, 'hex')) AS result;

In C level, I have:

bytea *newval = PG_GETARG_BYTEA_P(0);
unsigned char *byte_array = (unsigned char *) VARDATA(newval);

With following 2056 bytes size hex_string

484c08020000000045422275433435354222a56424534257334242335285345034366432525334333312464268247645b421352562714488235244462423347733333443333764333276343362522524277251533276542454332725235237251552363532232264325822663374483725224343332535227437229446363344422332634444294363815262622b237457236261312655522255533443745345434495535422423538333446152155443333434234632436332479252464522373412835526433584342352523542366227634847374546642323642365338353653a71533345343364234473144362353389232342332322443413532926224952324553425345433643333432344453432542511426513332244536425546335a42744433d324545464526443425415423242423432323433559342336435334953562673627797554634424254244666543493355436341125323452424245373639334354246513531447741247333542743155323233734558844332447322225324132333354523613553151415435271352453365342254232324221414234778332232464534592347534465231314313638453768343683563556541356633572632333364255442632143431635514553373376436143543346262153643342742436465233322525683423123e446544336357342331433443452462425125224476333412446381334464225594443233924433344244313433444426564663554424346733543374552635355543542333416639317251946583222445742341673644642242643473353543461394347765233337733234533255443633522b452149435433424327422665423347553315144237745265223393433343635476442255435554251430424333343324445425342774632a4228363444444443332434220334343273341663652824153342424336644324311235732525324325158343263572565134424214633234532683342322432457543353333232339522354533464533144363252324332654353345754163727552357534532734753232263535342435343363162269235314322422455675262333482233328346333534693356736533423455a45323753135336353323235354345452546435232233665442454533424552745943451623225384362532345324832445434363334542323424321424234332352253435337244524313527454244474444633325347364262534336751353422335243357463127254335634355164463527514872315633739376433422332335a623432624647452044584223643279344345a3425743333744436342463323433641456553235b5645446363465441b33744423675652357e224654555345451725341234471422742335322195244454532243443554332735324352524324325538362123642444343733429345443555132233422658434323318322224812932523225472542222253824567143533934422355564357556544364143582456552a5723633444343373143233232365313457333335c854303223435555376224527522837373432423636175514265729563262724363277553723345655343543644154222243544584235166433234144427432422353223161443225236552236247233344444673254346533332622536836233436524125642424346312831738524337135282253821395545134317236235463527335424225512342442327435876642613664244d514536635365842545133354453224a166632541354457334124224353536547422331543364273124364322734625292685535624235542432444542683b365353344253434363623634111325142362255346447425653656376413463621664322354624552825434226546553711223344382254339694331a345443733528422333233464224343302562636431323453441155432336355556322342624453332737553545436243222c552322231244334321322725226733475682554c22346442473655521543516465465448334224422467733355535543425333132234323422342327836322421563825334624422443373344482523526345348334543148b533426615333354344536242544344743342472843442258644834363462354424363d24c53424536537434343443526324592341423544a3335341325465964443452334322746a336533326413622533422334367451846143234444154323256a61342334334466a9454235284262536232443314514243344433324137222437532175432446364352554334247435442428743735436344235213249423463132b655133627457443533431352332156453564333554547512335657a515425244222640334341643435944324453337235444752332645133633252423164843422335823421322352752122424354212554467474422534143833552b352253232225224153263363334323633a5471673366411342545233727337439482874525135124343341524462335283445c63736631322124546553433242252263a134123146127422735733344265413547565536122239b35403323442335635232234423424424435332a2363343723354333443433462233823434183456b344444444445442942248425434331243444433634647333634233494464242247143252391342a396443523229233536223563992333355a233342343182435324497332744885233732533375454264833433454273236443214223473333264393232272242443522491583234426323266634631344364326

I just got a 4byte char* in c

byte_array content : HL
byte_array length  : 4

The question is what is the problem with my code and how can I get expected char* (2056 byte) ?

2 Answers 2

1

Short answer: Your bytes don't contain a printable C string.

Long answer: Your bytes obviously include null characters, which are end markers for C strings. Thus your program prints the first characters until the first 0. Since your hex-string contains 4 consecutive zeros it doesn't seem to be a valid C string as well; regardless of possible encodings.

You can convert the bytes to a Postgres text value with encode(bytea_field, 'escape)`, and read it as text from your C code. But because of the zero bytes this will not print a longer string either.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you explaination @clemens!
0

You should use VARSIZE(newval) to determine the size of the bytea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.