Exemplo n.º 1
0
 function import_from_reader($reader)
 {
     $endian_string = MO::get_byteorder($reader->readint32());
     if (FALSE === $endian_string) {
         return FALSE;
     }
     $reader->setEndian($endian_string);
     $endian = 'big' == $endian_string ? 'N' : 'V';
     $header = $reader->read(24);
     if ($reader->strlen($header) != 24) {
         return FALSE;
     }
     // parse header
     $header = unpack("{$endian}revision/{$endian}total/{$endian}originals_lenghts_addr/{$endian}translations_lenghts_addr/{$endian}hash_length/{$endian}hash_addr", $header);
     if (!is_array($header)) {
         return FALSE;
     }
     extract($header);
     // support revision 0 of MO format specs, only
     if ($revision != 0) {
         return FALSE;
     }
     // seek to data blocks
     $reader->seekto($originals_lenghts_addr);
     // read originals' indices
     $originals_lengths_length = $translations_lenghts_addr - $originals_lenghts_addr;
     if ($originals_lengths_length != $total * 8) {
         return FALSE;
     }
     $originals = $reader->read($originals_lengths_length);
     if ($reader->strlen($originals) != $originals_lengths_length) {
         return FALSE;
     }
     // read translations' indices
     $translations_lenghts_length = $hash_addr - $translations_lenghts_addr;
     if ($translations_lenghts_length != $total * 8) {
         return FALSE;
     }
     $translations = $reader->read($translations_lenghts_length);
     if ($reader->strlen($translations) != $translations_lenghts_length) {
         return FALSE;
     }
     // transform raw data into set of indices
     $originals = $reader->str_split($originals, 8);
     $translations = $reader->str_split($translations, 8);
     // skip hash table
     $strings_addr = $hash_addr + $hash_length * 4;
     $reader->seekto($strings_addr);
     $strings = $reader->read_all();
     $reader->close();
     for ($i = 0; $i < $total; $i++) {
         $o = unpack("{$endian}length/{$endian}pos", $originals[$i]);
         $t = unpack("{$endian}length/{$endian}pos", $translations[$i]);
         if (!$o || !$t) {
             return FALSE;
         }
         // adjust offset due to reading strings to separate space before
         $o['pos'] -= $strings_addr;
         $t['pos'] -= $strings_addr;
         $original = $reader->substr($strings, $o['pos'], $o['length']);
         $translation = $reader->substr($strings, $t['pos'], $t['length']);
         if ('' === $original) {
             $this->set_headers($this->make_headers($translation));
         } else {
             $entry =& $this->make_entry($original, $translation);
             $this->entries[$entry->key()] =& $entry;
         }
     }
     return TRUE;
 }