title: Opensubtitles hash function fails for large files
body: <p>I am using the function below to calculate the opensubtitles.org hash for movie files. It works mostly but with large files I get the following error.</p>
  
  <p>I dont really understand it because there should always be data available. </p>
  
  <p>Can anyonep point me in the right direction?</p>
  
  <blockquote>
    <p>PHP Warning:  unpack(): Type v: not enough input, need 2, have 0 in
    file.php on line 169</p>
  </blockquote>
  
  <pre><code>function OpenSubtitlesHash($file)
  {
      $handle = fopen($file, "rb");
      $fsize = filesize($file);
  
      $hash = array(3 =&gt; 0, 
                    2 =&gt; 0, 
                    1 =&gt; ($fsize &gt;&gt; 16) &amp; 0xFFFF, 
                    0 =&gt; $fsize &amp; 0xFFFF);
  
      for ($i = 0; $i &lt; 8192; $i++)
      {
          $tmp = ReadUINT64($handle);
          $hash = AddUINT64($hash, $tmp);
      }
  
      $offset = $fsize - 65536;
      fseek($handle, $offset &gt; 0 ? $offset : 0, SEEK_SET);
  
      for ($i = 0; $i &lt; 8192; $i++)
      {
          $tmp = ReadUINT64($handle);
          $hash = AddUINT64($hash, $tmp);         
      }
  
      fclose($handle);
          return UINT64FormatHex($hash);
  }
  
  function ReadUINT64($handle)
  {
      $u = unpack("va/vb/vc/vd", fread($handle, 8));
      return array(0 =&gt; $u["a"], 1 =&gt; $u["b"], 2 =&gt; $u["c"], 3 =&gt; $u["d"]);
  }
  
  function AddUINT64($a, $b)
  {
      $o = array(0 =&gt; 0, 1 =&gt; 0, 2 =&gt; 0, 3 =&gt; 0);
  
      $carry = 0;
      for ($i = 0; $i &lt; 4; $i++) 
      {
          if (($a[$i] + $b[$i] + $carry) &gt; 0xffff ) 
          {
              $o[$i] += ($a[$i] + $b[$i] + $carry) &amp; 0xffff;
              $carry = 1;
          }
          else 
          {
              $o[$i] += ($a[$i] + $b[$i] + $carry);
              $carry = 0;
          }
      }
  
      return $o;   
  }
  
  function UINT64FormatHex($n)
  {   
      return sprintf("%04x%04x%04x%04x", $n[3], $n[2], $n[1], $n[0]);
  }
  </code></pre>