Skip to content

Instantly share code, notes, and snippets.

@mitaken
Created November 19, 2011 10:10
Show Gist options
  • Select an option

  • Save mitaken/1378681 to your computer and use it in GitHub Desktop.

Select an option

Save mitaken/1378681 to your computer and use it in GitHub Desktop.
uuidchkuを指定ベンダー毎に出力
<?php
/**
* uuidchku modifier
*
* @version 1.0
* @copyrignt mitaken
* @see http://chakuuta3g2.hp.infoseek.co.jp/uuidchku.html
*
* [USAGE]
* uuidchku.php input_contain_chku.3g2 vendor_id(e.g:KC) [output.3g2]
*
* 第一引数はuuidchkuを含む3g2ファイル
* 第二引数はベンダーコード(User agent like)
* 第三引数は変更を加えたファイルの出力先
* 但し第三引数を省略するとファイルは出力されない
*/
/**
* 文字列をXOR
*
* @param string $binary
* @return string
*/
function bin2xor($binary)
{
$xor = $binary[0];
for ($i = 1, $length = strlen($binary);$i < $length;++$i) {
$xor ^= $binary[$i];
}
return bin2hex($xor);
}
echo "uuidchku modifier [Version 1.0]\n"
. "Copyright (c) 2011 mitaken\n\n";
/**
* 引数の確認
*/
if (!isset($argv[1], $argv[2])) {
die("[USAGE]\n"
. "uuidchku.php input_file.3g2 vendor_id [output_file.3g2]\n");
}
/**
* 入力ファイルの確認
*/
if (!is_file($argv[1]) || ($file = @file_get_contents($argv[1])) === false) {
die("Input file error.\n");
}
/**
* ベンダーコードの確認
*/
$vendorList = array('HI', 'KC', 'SN', 'SA', 'TS', 'ST', 'CA', 'PT', 'SH', 'FJ', 'U1', 'U2', 'U3', 'U4', 'U5', 'U6');
if (!in_array($argv[2], $vendorList)) {
die("Unknown vendor_id\n");
}
/**
* uuidchkuの有無を確認
*/
if (($chkuOffset = strpos($file, "\x00\x00\x00\x9Cuuidchku")) === false) {
die("Cannot find uuidchku\n");
}
/**
* 全体のXORを計算
*/
$chku = substr($file, $chkuOffset + 28, 128);
$totalXor = bin2xor($chku);
/**
* メーカー毎の参照位置を算出
*/
$chkuList = array();
$baseList = array(0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9);
$totalOffset = $baseList[hexdec($totalXor[1])];
for ($i = 0;$i < 16;++$i) {
$vendorOffset = $totalOffset + $i;
$vendorOffset = ($vendorOffset > 15) ? $vendorOffset - 16 : $vendorOffset;
$vendorChku = substr($chku, $vendorOffset * 8, 8);
$chkuList[$vendorList[$i]] = $vendorChku;
printf("Vendor:%s Chku:%s Xor:%s ChkuOffset:%-3d\n", $vendorList[$i], bin2hex($vendorChku), bin2xor($vendorChku), $vendorOffset * 8);
}
/**
* ベンダーコードの位置計算
*/
$vendorXor = bin2xor($chkuList[$argv[2]]);
$insertOffset = $baseList[hexdec($vendorXor[1])] + array_search($argv[2], $vendorList);
$insertOffset = ($insertOffset > 15) ? $insertOffset - 16 : $insertOffset;
/**
* ファイル書き出し
*/
if (isset($argv[3])) {
$blankChku = str_repeat("\x00", 128);
$modifyChku = substr_replace($blankChku, $chkuList[$argv[2]], $insertOffset * 8, 8);
$modifyFile = substr_replace($file, $modifyChku, $chkuOffset + 28, 128);
file_put_contents($argv[3], $modifyFile);
}
/**
* 出力結果
*/
echo "TotalX:{$totalXor}"
. " Chku:" . bin2hex($chkuList[$argv[2]])
. " Xor:{$vendorXor}"
. " ChkuOffset:" . ($insertOffset * 8) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment