PHP code to unescape data retrieved from a bytea column

From: huongch(at)bigfoot(dot)com (Flancer)
To: pgsql-general(at)postgresql(dot)org
Subject: PHP code to unescape data retrieved from a bytea column
Date: 2001-10-24 09:18:30
Message-ID: 8c8c68a1.0110240118.441559a@posting.google.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

To those who has been asking for it.. here it is.. pieced it togather
within a few hours so please excuse my clumsy syntaxes or algorithm or
my speed (for those who can actually code it a lot faster.... erm then
again I guess you won't be reading this post anyway).

function checkalphanum($ct,$i)
{
//find if next 3 characters are digits or not
for ($digitcount=0,$j = $i+1,$actual_count=1; ($actual_count <= 3) &&
($j < strlen($ct));++$actual_count, ++$j)
{
if ((ord($ct[$j]) >= 48) && (ord($ct[$j]) <= 57))
{
++$digitcount;
}
}
return $digitcount;
}

function unescape($ct,$i)
{
$buff = "";
for ($j = $i,$actual_count=1; ($actual_count <= 3) && ($j <
strlen($ct));++$actual_count, ++$j)
{
$buff .= $ct[$j];
}
return chr(octdec(intval($buff)));
}

function decode($ct)
{
$buf = "";
for ($i = 0; $i < strlen($ct); $i++)
{
if ($ct[$i] == "\\")
{
if ($ct[$i + 1] == "\\")
{
if (checkalphanum($ct,$i+1) == 3)
{
$i +=2;
$buf .= unescape($ct,$i);
$i +=2;
}
else
{
$buf .=$ct[$i];
}
}
else
{
if (checkalphanum($ct,$i) == 3)
{
++$i;
$buf .= unescape($ct,$i);
$i +=2;
}
}
}
else
{
$buf .= $ct[$i];
}
}
return $buf;
}

I have tested it using data compressed using the bzcompress.. and
voila it bzdecompres perfectly using my function ;).
To use it just call something like

print decode($rawdatafrombytea);

So far I have only tested on data that was encoded using another PHP
function done by Joe Conway.

You can find that code here
http://groups.google.com/groups?q=bytea&hl=ms&group=comp.databases.postgresql.general&rnum=7&selm=027601c12c08%2454576a20%2448d210ac%40jecw2k1

If anyone out there can improve this code or already has a better
code.. please let me know ;).

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ben-Nes Michael 2001-10-24 09:23:49 Disable Transaction - plans ?
Previous Message James Szabadics 2001-10-24 08:31:57 Installation problems