function parse_dbc($file, $string_fields=array(), $id_idx=1){ $fh = fopen($file, 'r'); $sig = fread($fh, 4); $records = read_int($fh); $fields = read_int($fh); $record_size = read_int($fh); $strings_size = read_int($fh); if ($fields * 4 != $record_size){ die("unexpected field size! ($fields fields = $record_size bytes)"); } $rows = array(); for ($i=0; $i<$records; $i++){ $row = array(); for ($j=0; $j<$fields; $j++){ $row[$j+1] = read_int($fh); } if ($id_idx){ $rows[$row[$id_idx]] = $row; }else{ $rows[] = $row; } } $string_data = fread($fh, $strings_size); foreach ($rows as &$row){ foreach ($string_fields as $f){ $p = $row[$f]; if ($p >= $strings_size){ $row[$f] = "LOC? ($p)"; }else{ $idx = strpos($string_data, "\0", $p); if ($idx !== false){ $row[$f] = substr($string_data, $p, $idx-$p); }else{ $row[$f] = "OVER ($p)"; } } } } return $rows; }
function init_mysql_conf() { $conf_filename = sprintf("%s/etc/judge.conf", $GLOBALS["JUDGE_HOME"]); $fp = fopen($conf_filename, "r") or die("Unable to read OJ conf file"); while (!feof($fp)) { $buf = fgets($fp); read_buf($buf, "OJ_HOST_NAME", $host_name); read_buf($buf, "OJ_USER_NAME", $user_name); read_buf($buf, "OJ_PASSWORD", $password); read_buf($buf, "OJ_DB_NAME", $db_name); read_int($buf, "OJ_PORT_NUMBER", $port_number); } fclose($fp); $GLOBALS["DB_HOST"] = $host_name; $GLOBALS["DB_USER"] = $user_name; $GLOBALS["DB_PASS"] = $password; $GLOBALS["DB_NAME"] = $db_name; $GLOBALS["DB_PORT"] = $port_number; }
function extract_areas($file, $x, $y, &$zones){ $fh = fopen($file, 'r'); # get positions of all chunks $offset = 0; fseek($fh, 0); $chunks = array(); while (!feof($fh)){ $type = fread($fh, 4); if (strlen($type) < 4) break; $type = strrev($type); $size = read_int($fh); fseek($fh, $size, SEEK_CUR); if ($type == 'MCNK') $chunks[] = $offset; $offset += 8 + $size; } $n = 0; foreach ($chunks as $off){ fseek($fh, $off + 0x3C); $id = read_int($fh); $sx = $n % 16; $sy = floor($n/16); $xp = ($x*256)+($sx*16); $yp = ($y*256)+($sy*16); $zones[$id][] = array($xp, $yp); $n++; } }
function extract_mogi($filename){ $fh = fopen($filename, 'r'); seek_to_chunk($fh, 'MOHD'); fseek($fh, 4, SEEK_CUR); $groups = read_int($fh); seek_to_chunk($fh, 'MOHD'); fseek($fh, 0x24, SEEK_CUR); $floats = fread($fh, 24); $box = unpack('f6', $floats); #echo "root\t\t: ".format_box($box)."\n"; $chunks = array(); seek_to_chunk($fh, 'MOGI'); for ($i=0; $i<$groups; $i++){ $flags = read_int($fh); $floats = fread($fh, 24); $box = unpack('f6', $floats); $name = read_int($fh); $chunks[$i] = array($box[1], 0-$box[2], $box[3], $box[4], 0-$box[5], $box[6]); #echo "group $i \t: ".format_box($box)."\n"; #print_r($box); } #echo "found $groups groups\n"; #exit; fclose($fh); return $chunks; }
} else { if (isset($INSTALL['siteshortname'])) { $siteshortname = $INSTALL['siteshortname']; } } if ($interactive == CLI_FULL || $interactive == CLI_SEMI && !isset($INSTALL['sitesummary'])) { console_write('sitesummary'); $sitesummary = read(); } else { if (isset($INSTALL['sitesummary'])) { $sitesummary = $INSTALL['sitesummary']; } } if ($interactive == CLI_FULL || $interactive == CLI_SEMI && !isset($INSTALL['sitenewsitems'])) { console_write('sitenewsitems'); $sitenewsitems = read_int(); } else { if (isset($INSTALL['sitenewsitems'])) { $sitenewsitems = $INSTALL['sitenewsitems']; } } if (!isset($sitefullname)) { $sitefullname = $DEFAULT['sitefullname']; } if (!isset($siteshortname)) { $siteshortname = $DEFAULT['siteshortname']; } if (!isset($sitesummary)) { $sitesummary = $DEFAULT['sitesummary']; } if (!isset($sitenewsitems)) {
/** * Read and integer value within range * * @param int $start * @param int $end * @return int */ function read_int_range($start, $end) { $input = read_int(); if (valid_int_range($input, $start, $end)) { return $input; } else { console_write_error('invalidintrange'); console_write("\n", '', false); } }
echo "Error ", $file['error']; } } //if ($_SERVER['HTTP_USER_AGENT'] != "CUETools 205") { // echo "user agent ", $_SERVER['HTTP_USER_AGENT'], " is not allowed"; // return; //} $tmpname = $file['tmp_name']; $size = @file_exists($tmpname) ? filesize($tmpname) : 0; if ($size == 0) { echo "no file uploaded"; return; } $fp = fopen($tmpname, 'rb'); $head = read_chunk($fp, 0, $size, 'CTDB.HEAD', 20); $npar = read_int($fp, 0, $size, 'CTDB.DISC.NPAR'); $version = BigEndian2Int(substr($head, 0, 4)); $disccount = BigEndian2Int(substr($head, 4, 4)); $total = BigEndian2Int(substr($head, 8, 4)); printf("npar=%d, disccount=%d, total=%d,", $npar, $disccount, $total); fclose($fp); $id = $_POST['id']; $err = sscanf($id, "%03d-%04x%04x-%04x%04x-%04x%04x", $tracks, $id1a, $id1b, $id2a, $id2b, $cddbida, $cddbidb); $parsedid = sprintf("%03d-%04x%04x-%04x%04x-%04x%04x", $tracks, $id1a, $id1b, $id2a, $id2b, $cddbida, $cddbidb); if ($id != $parsedid) { echo "bad id ", $id; return; } $target_path = sprintf("parity/%x/%x/%x", $id1b & 15, $id1b >> 4 & 15, $id1b >> 8 & 15); $target_file = sprintf("%s/dBCT-%s.bin", $target_path, $parsedid); @mkdir($target_path, 0777, true);
function extract_mogi($filename){ $fh = fopen($filename, 'r'); seek_to_chunk($fh, 'MOHD'); fseek($fh, 4, SEEK_CUR); $groups = read_int($fh); seek_to_chunk($fh, 'MOHD'); fseek($fh, 0x24, SEEK_CUR); $floats = fread($fh, 24); $box = unpack('f6', $floats); echo "root\t\t: ".format_box($box)."\n"; seek_to_chunk($fh, 'MOGI'); for ($i=0; $i<$groups; $i++){ $flags = read_int($fh); $floats = fread($fh, 24); $box = unpack('f6', $floats); $name = read_int($fh); echo "group $i \t: ".format_box($box)."\n"; #print_r($box); } #echo "found $groups groups\n"; #exit; }
function iterate_list($callback, $rec, $start_from, $desc, $fh, $hash_entry_ptr, $index_list) { $ptr = $hash_entry_ptr; //if (TRACE) print("============ get_list_entry_pos\n"); while ($ptr) { //if (TRACE) printf("\$ptr: 0x%X, count(\$index_list): %d\n", $ptr, count($index_list)); if (count($index_list)) { $search_subhash = 1; $index = array_shift($index_list); } else { $search_subhash = 0; $index = NULL; } $is_first_round = 1; $prev_rec_ptr = $ptr; while ($ptr = $prev_rec_ptr) { $rec_desc = read_int(1, $fh, SEEK_SET, $ptr); $field_qt = $rec_desc & FIELD_QT_READ_MASK; $ref_size = ($rec_desc & REF_SIZE_READ_MASK) >> REF_SIZE_SHIFT; //if (TRACE) printf("\$rec_desc: 0x%X, \$search_subhash: %d, \$index: %d\n", $rec_desc, $search_subhash, $index); //if (TRACE) printf("\$is_first_round: %d, \$field_qt: %d\n", $is_first_round, $field_qt); if ($is_first_round && !$field_qt) { $list_entry_pos = ftell($fh); //if (TRACE) printf("\$list_entry_pos: 0x%X, \$search_subhash: %d, \$index: %d\n", $list_entry_pos, $search_subhash, $index); if (!$search_subhash) { //if (TRACE) printf("BINGO: \$list_entry_pos: 0x%X\n", $list_entry_pos); $pos = $list_entry_pos; //if (TRACE) printf("\$start_from: 0x%X\n", $start_from); $level = 0; $max = 0xff; $mask = 0xff; $stack = array(); //$stack[] = array('pos' => $pos + 4, 'level' => $level); //$next_pos; //$go_up = 1;//$go_up = $start_from > $max; //if (TRACE) printf("\$pos: 0x%X, \$start_from: %d, \$level: %d; \$max: 0x%X, \$mask: 0x%X, \$next_pos: 0x%X, \$go_up: %d\n", $pos, $start_from, $level, $max,$mask, $next_pos, $go_up); if (DEBUG) { print '<pre>'; } if ($desc) { while ($pos) { if ($start_from > $max) { $stack[] = array('pos' => $pos + 4, 'level' => $level); $level++; $mask <<= 8; $max = $max << 8 | 0xff; } else { $start = ($start_from & $mask) >> 8 * $level; if (count($stack)) { $top =& $stack[count($stack) - 1]; } $is_topper = !count($stack) || $level > $top['level']; $stack[] = array('pos' => $pos + ($is_topper ? 4 : 0), 'level' => $level, 'start' => $start, 'index_base' => $is_topper ? 0 : $top['index_base'] + $top['start'] << 8); if (!$level) { break; } $pos += $start * 4; $mask >>= 8; $level--; } $pos = read_int(4, $fh, SEEK_SET, $pos); } //if (DEBUG) print_r($stack); while (count($stack)) { $item = array_pop($stack); $delta = count($stack) && $stack[count($stack) - 1]['level'] > $item['level'] ? 1 : 0; $level =& $item['level']; if (!array_key_exists('start', $item)) { $item['start'] = 255; } elseif ($level) { $item['start']--; } //if (DEBUG) printf("\$item['start']: " . $item['start'] . ", \$delta: $delta, \$level: $level; \$item['start'] >= (\$delta ? 0 : 1): %d\n", $item['start'] >= ($delta ? 0 : 1)); if ($item['start'] < ($delta ? 0 : 1)) { continue; } if (!array_key_exists('ptrs', $item)) { $qt = $item['start'] + $delta; $item['ptrs'] = unpack("N{$qt}", read_buf($qt * 4, $fh, SEEK_SET, $item['pos'])); //if (DEBUG) if ($level) print_r($item); } $ptrs =& $item['ptrs']; $index_base =& $item['index_base']; //if (DEBUG) printf("\$item['start'] + \$delta: %d, \$index_base: %d\n", $item['start'] + $delta, $index_base); for ($i = $item['start'] + $delta; $i >= 1; $i--) { //if (DEBUG) if ($level) printf("\$ptrs[$i]: 0x%X\n", $ptrs[$i]); if (!$ptrs[$i]) { continue; } if ($level) { $item['start'] = $i - $delta; //if (DEBUG) if ($level) printf("\$item['start']: %d\n", $item['start']); $stack[] = $item; $stack[] = array('pos' => $ptrs[$i], 'level' => $item['level'] - 1, 'index_base' => $index_base + $item['start'] << 8); break; } else { $rec->entry_ptr = $ptrs[$i]; if ($callback($index_base + $i - $delta, $rec) === FALSE) { return TRUE; } } } //if (DEBUG) if (!$level) printf('%d - %d' . "\n", $index_base + count($ptrs) - $delta, $index_base + 1 - $delta); } } else { while ($pos) { if ($start_from > $max) { //$stack[] = array('pos' => $pos + 4, 'level' => $level); $level++; $mask <<= 8; $max = $max << 8 | 0xff; $pos = read_int(4, $fh, SEEK_SET, $pos); } else { $start = ($start_from & $mask) >> 8 * $level; if (count($stack)) { $top =& $stack[count($stack) - 1]; } //$is_topper = !count($stack) || $level > $top['level']; $stack[] = array('ptrs' => unpack('N256', read_buf(1024, $fh, SEEK_SET, $pos)), 'level' => $level, 'start' => $start, 'index_base' => !count($stack) ? 0 : $top['index_base'] + $top['start'] << 8); if (!$level) { break; } $pos = $stack[count($stack) - 1]['ptrs'][$start + 1]; //$pos += $start * 4; $mask >>= 8; $level--; } } if ($start_from > $max) { return TRUE; } //if (DEBUG) print_r($stack); while (count($stack)) { $item = array_pop($stack); //$delta = count($stack) && $stack[count($stack) - 1]['level'] > $item['level'] ? 1 : 0; $level =& $item['level']; if (!array_key_exists('start', $item)) { $item['start'] = 0; } elseif ($level) { $item['start']++; } //if (DEBUG) printf("\$item['start']: " . $item['start'] . ", \$level: $level; \$item['start'] >= 0: %d\n", $item['start'] >= 0)); if ($item['start'] > 255) { continue; } /* if (!array_key_exists('ptrs', $item)) { $qt = $item['start'] + $delta; $item['ptrs'] = unpack("N$qt", read_buf($qt * 4, $fh, SEEK_SET, $item['pos'])); if (DEBUG) if ($level) print_r($item); } */ $ptrs =& $item['ptrs']; $index_base =& $item['index_base']; //if (DEBUG) printf("\$item['start']: %d, \$index_base: %d\n", $item['start'], $index_base); for ($i = $item['start'] + 1; $i <= 256; $i++) { //if (DEBUG) if ($level) printf("\$ptrs[$i]: 0x%X\n", $ptrs[$i]); if (!$ptrs[$i]) { continue; } if ($level) { $item['start'] = $i - 1; //if (DEBUG) printf("\$item['start']: %d\n", $item['start']); $stack[] = $item; $stack[] = array('ptrs' => unpack('N256', read_buf(1024, $fh, SEEK_SET, $ptrs[$i])), 'level' => $item['level'] - 1, 'index_base' => $index_base + $item['start'] << 8); //if (DEBUG) print_r($stack); break; } else { $rec->entry_ptr = $ptrs[$i]; if ($callback($index_base + $i - 1, $rec) === FALSE) { return TRUE; } } } //if (DEBUG) if (!$level) printf('%d - %d' . "\n", $index_base + $item['start'], $index_base + count($ptrs) - 1); //if (DEBUG) if (!count($stack)) print_r($item); if (count($stack)) { continue; } if (!$ptrs[1]) { break; } $stack[] = array('ptrs' => unpack('N256', read_buf(1024, $fh, SEEK_SET, $ptrs[1])), 'level' => $item['level'] + 1, 'index_base' => 0, 'start' => 0); //if (DEBUG) print_r($stack); } } return TRUE; } elseif ($index > 0) { $pos = $list_entry_pos; $i = $index; $level = 0; $max = 0xff; $mask = 0xff; $next_pos; $go_up = $i > $max; if (TRACE) { printf("\$pos: 0x%X, \$i: %d, \$level: %d; \$max: 0x%X, \$mask: 0x%X, \$next_pos: 0x%X, \$go_up: %d\n", $pos, $i, $level, $max, $mask, $next_pos, $go_up); } while ($go_up && $i > $max || !$go_up && $mask != 0xff) { if ($go_up) { $level++; $mask <<= 8; $max = $max << 8 | 0xff; } else { $pos += (($i & $mask) >> 8 * $level) * 4; $mask >>= 8; $level--; } $next_pos = read_int(4, $fh, SEEK_SET, $pos); if (TRACE) { printf("\$pos: 0x%X, \$i: %d, \$level: %d; \$max: 0x%X, \$mask: 0x%X, \$next_pos: 0x%X, \$go_up: %d\n", $pos, $i, $level, $max, $mask, $next_pos, $go_up); } if (!$next_pos) { return FALSE; } $pos = $next_pos; $go_up = $go_up && $i > $max; } $list_item_addr = $pos + ($i & 0xff) * 4; if (TRACE) { printf("\$list_item_addr: 0x%X\n", $list_item_addr); } $ptr = read_int(4, $fh, SEEK_SET, $list_item_addr); break; } else { $is_first_round = 0; $prev_rec_ptr = read_int(4, $fh, SEEK_SET, $list_entry_pos + 1024); } if (TRACE) { printf("\$prev_rec_ptr: 0x%X\n", $prev_rec_ptr); } } elseif (!$is_first_round && !$field_qt) { $ref = !$ref_size ? 0 : read_int($ref_size, $fh, SEEK_CUR, 1024 + 4); $prev_rec_ptr = !$ref ? 0 : $ptr - $ref; if (TRACE) { printf("\$prev_rec_ptr: 0x%X\n", $prev_rec_ptr); } } else { if ($is_first_round) { if (!$search_subhash) { return FALSE; } $is_first_round = 0; } if (!$search_subhash) { throw new Exception('ASSERTION'); } if ($field_qt == 31) { // 0b11111 $field_qt = read_int(1, $fh, SEEK_CUR, 0) + 1; } $field_defs_len = $field_qt * 2; $buf = read_buf($field_defs_len + $ref_size, $fh, SEEK_CUR, 0); $field_defs = unpack("C{$field_defs_len}", $buf); $skip_len = 0; $ptr = NULL; for ($i = 1; $i <= $field_defs_len; $i += 2) { $def =& $field_defs[$i + 1]; if ($index != -$field_defs[$i]) { $skip_len += ($def & SSS_READ_MASK) >> SSS_SHIFT; } else { if ($def != HASH_TYPE) { return FALSE; } $ptr = read_int(4, $fh, SEEK_CUR, $skip_len); break; } } if (!is_null($ptr)) { break; } $ref = !$ref_size ? 0 : unpack_int($ref_size, substr($buf, -$ref_size, $ref_size)); $prev_rec_ptr = !$ref ? 0 : $ptr - $ref; } } } return FALSE; }