PHP code to unescape data retrieved from a bytea column

Lists: pgsql-general
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
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 ;).


From: "Alexey V(dot) Borzov" <borz_off(at)rdw(dot)ru>
To: huongch(at)bigfoot(dot)com ((Flancer))
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: PHP code to unescape data retrieved from a bytea column
Date: 2001-10-24 19:44:48
Message-ID: 1688164379.20011024234448@rdw.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello Flancer,

Wednesday, October 24, 2001, 1:18:30 PM, you wrote:

F> If anyone out there can improve this code or already has a better
F> code.. please let me know ;).
Well, I have better code. Sorta. ;]
http://www.php.net/manual/en/function.stripcslashes.php

Of course, you'd have to pass the value thru stripslashes() first
if you have "black magic quotes" enabled.

--
Yours, Alexey V. Borzov, webmaster of RDW.ru