Created
December 3, 2012 04:10
-
-
Save cafuego/4192641 to your computer and use it in GitHub Desktop.
check serialized data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add yer string! | |
$data = ''; | |
define('PATTERN', '/s:(\d+):"(.*?)";([Obis]):((\d+):)?([^;]+);/'); | |
// Care not for arrays. | |
$blob = preg_replace('/a:\d+:{/', "\n", $data); | |
// Can't cope with objects. | |
$blob = preg_replace('/":\d+:{/', "\";\n", $blob); | |
Tidy up cleaned array and object closing braces. | |
$blob = preg_replace('/}/', "\n", $blob); | |
$blob = trim($blob); | |
$blob = implode('', explode("\n", $blob)); | |
function variable_type($type) { | |
switch($type) { | |
case 'O': | |
return 'Object'; | |
case 'a': | |
return 'Array'; | |
case 'b': | |
return 'Boolean'; | |
case 'i': | |
return 'Integer'; | |
case 's': | |
return 'String'; | |
} | |
return 'Unknown ('. $type .')'; | |
} | |
$errors = array(); | |
$count = preg_match_all(PATTERN, $blob, $matches, PREG_SET_ORDER); | |
foreach ($matches as $match) { | |
$type = variable_type($match[3]); | |
// Key name is in $matches[2] and its length is in $matches[1]. | |
if (strlen($match[2]) != $match[1]) { | |
$errors[] = sprintf ("%s variable name `%s' has incorrectly serialized name. Length is %d but should be %d.", $type, $match[2], $match[1], strlen($match[2])); | |
} | |
// Value $matches[6] and length in $matches[5]. | |
switch ($match[3]) { | |
case 'O': | |
$errors[] = sprintf("I know nothing about objects, but I will check variables inside `%s() %s'.", trim($match[6], '"'), $match[2]); | |
break; | |
case 'a': | |
$errors[] = sprintf("I know nothing about arrays. Whatever `%s' might be ...", $match[2]); | |
break; | |
case 'b': | |
if ($match[6] < 0 || $match[6] > 1) { | |
$errors[] = sprintf("%s variable `%s' contains junk data: %s.", $type, $match[2], $match[6]); | |
} | |
break; | |
case 'i': | |
if (!is_numeric($match[6])) { | |
$errors[] = sprintf("%s variable `%s' contains junk data: `%s'.", $type, $match[2], trim($match[6])); | |
} | |
break; | |
case 's': | |
$text = trim($match[6], '"'); | |
if (strlen($text) != $match[5]) { | |
$errors[] = sprintf ("%s variable `%s' has incorrectly serialized value `%s'. Length is %d but should be %d.", $type, $match[2], $text, $match[5], strlen($text)); | |
} | |
break; | |
default: | |
$errors[] = sprintf("Junk for %s variable `%s'.", $type, $match[2]); | |
break; | |
} | |
} | |
if (!empty($errors)) { | |
printf("I detected %d problems:\n%s\n", count($errors), implode("\n", $errors)); | |
return count($errors); | |
} else { | |
printf("Well, that all looks fine to me, though I did not check for array sizes.\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment