Checkout’s DB sometimes uses bytea data columns to save some data.
From what I see, some of those are settings data, mostly in the preference table.
Checkout encodes the data before saving it to table, here’s an example:
\\200\\002}q\\001X\\006\\000\\000\\000youngiq\\002J\\315\\215jLs.
The past setting in preference should be holding Enstore’s store name “youngi” and the last sync time.
I found a bytea.py file in checkout’s root which contains few python functions about this encoding and decoding, but I am trying to translate it to PHP in order to use it. (it is a http://python.projects.postgresql.org 2005 project that I can’t find online now)
here’s the content of the file :
'PostgreSQL bytea encodings'
def bchr(ord):
if not (32 < ord < 126):
return "\\" + oct(ord).lstrip('0').rjust(3, '0')
elif ord == 92:
return r'\\'
else:
return chr(ord)
def encode(data):
return ''.join([bchr(ord(x)) for x in data])
def decode(data):
diter = iter(data)
output = []
next = diter.next
for x in diter:
if x == "\\":
try:
y = next()
except StopIteration:
raise ValueError, "incomplete backslash sequence"
if y == "\\":
pass
elif y.isdigit():
try:
os = ''.join((y, next(), next()))
except StopIteration:
raise ValueError, "incomplete backslash sequence"
try:
x = chr(int(os, base = 8))
except ValueError:
raise ValueError, "invalid bytea octal sequence '%s'" %(os,)
else:
raise ValueError, "invalid backslash follow '%s'" %(y,)
output.append(x)
return ''.join(output)
I tried hard to translate it to PHP, got close but still no success. What I guess is that the input is JSON, so when decoding I expect a JSON output, or at least some nodes and strings.
If anyone has any clue, share it please, coz this is quite important to decode some of the orders info in the DB.
my trial is like follows:
public function bchr($ord)
{
if (!((32<$ord)&&($ord<126)))
{
$result = "\\\\". str_pad(ltrim(decoct($ord),0),3,0, STR_PAD_LEFT);
Mage::log('1='.$result);
return $result;
}
elseif ($ord==92)
{
$result = '\\\\';
Mage::log('2='.$result);
return $result;
}
else
{
$result = chr($ord);
Mage::log('3='.$result);
return $result;
}
}
public function encode($data)
{
$joined = '';
$pregData = preg_split('//', $data, -1, PREG_SPLIT_NO_EMPTY);
foreach ($pregData as $x)
{
$ord = ord($x);
Mage::log('x='.$x);
Mage::log('ord='.$ord);
$joined .= $this->bchr($ord);
}
return $joined;
}
public function decode($data)
{
$pregData = preg_split('//', $data, -1, PREG_SPLIT_NO_EMPTY);
$output = '';
foreach ($pregData as $x)
{
if ($x == '\\\\')
{
$y = next($pregData);
//Mage::log($y);
if ($y == '\\\\')
{
continue;
}
elseif (is_int($y))
{
$os = $y.next($pregData).next($pregData);
$x = chr(intval($os, 8));
Mage::log($x);
}
else
{
//Mage::throwException('Invalid Backslash Follow $y');
}
}
$output .= $x;
}
return $output;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment