예제 #1
0
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;
}
예제 #2
0
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;
}