UK
apps/cryo-studio / tools / demo / rewrite_paths.php · commit 76aa68d
<?php// Rewrites the strings of a .cryo snapshot (section STRS, see ext-cryo/cryo_format.h) so that the published demo holds// no absolute path: every string that starts with $from becomes $to + the rest. The other sections refer to strings by// index, so they stay as they are. Run by tools/demo/make_demo.sh (64-bit PHP: the section lengths are u64).//   php rewrite_paths.php <file.cryo> <from> <to>[, $file, $from, $to] = $argv + [null, null, null, null];if ($file === null || $from === null || $to === null) {    fwrite(STDERR, "usage: php rewrite_paths.php <file.cryo> <from> <to>\n");    exit(2);}$d = file_get_contents($file);if (substr($d, 0, 4) !== 'CRYO') {    fwrite(STDERR, "$file is not a .cryo file\n");    exit(1);}$out = substr($d, 0, 16);$off = 16;$changed = 0;while ($off + 12 <= strlen($d)) {    $tag = substr($d, $off, 4);    $len = unpack('P', $d, $off + 4)[1];    $payload = substr($d, $off + 12, $len);    if ($tag === 'STRS') {        $n = unpack('V', $payload)[1];        $q = 4;        $strs = '';        for ($i = 0; $i < $n; $i++) {            $l = unpack('V', $payload, $q)[1];            $s = substr($payload, $q + 4, $l);            $q += 4 + $l;            if (str_contains($s, $from)) {                $s = str_replace($from, $to, $s);                $changed++;            }            $strs .= pack('V', strlen($s)) . $s;        }        $payload = pack('V', $n) . $strs;    }    $out .= $tag . pack('P', strlen($payload)) . $payload;    $off += 12 + $len;}file_put_contents($file, $out);echo basename($file), ": $changed strings rewritten\n";