<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -30,13 +30,11 @@ class MIME
         {
             $this-&gt;literal_size = 8;
             $this-&gt;glob_size = 8;
-            $this-&gt;suffix_tree_node_size = 16;
         }
         else if ($this-&gt;version == '1.1')
         {
             $this-&gt;literal_size = 12;
             $this-&gt;glob_size = 12;
-            $this-&gt;suffix_tree_node_size = 12;
         }
         else
             throw new Exception('unsupported mime.cache version '.$this-&gt;version);
@@ -73,27 +71,65 @@ class MIME
 
     protected function suffix($filename)
     {
-        list($n, $pos) = $this-&gt;nuint32_at($this-&gt;uint32_at(16), 2);
-        $suffix = '';
-
-        for ($i = 0; $i &lt; $n;)
+        if ($this-&gt;version == '1.0')
         {
-            $c = $this-&gt;uint32_at($pos);
-            if ($c == 0 || $filename{strlen($filename)-strlen($suffix)-1} == chr($c))
+            /* forward suffix tree */
+            $queue = array(array('', $this-&gt;uint32_at(16)));
+
+            while (!empty($queue))
             {
-                $type_off = $this-&gt;uint32_at($pos+4);
-                if (($this-&gt;version == '1.0' &amp;&amp; $type_off) || $c == 0)
-                    return $this-&gt;string_at($type_off);
-                $suffix = chr($c).$suffix; /* FIXME: make unicode-aware? */
-                if (strlen($suffix) &gt;= strlen($filename))
-                    break;
-                list($n, $pos) = $this-&gt;nuint32_at($pos+$this-&gt;suffix_tree_node_size-8, 2);
-                $i = 0;
+                list($suffix, $pos) = array_shift($queue);
+                list($n, $pos) = $this-&gt;nuint32_at($pos, 2);
+
+                for ($i = 0; $i &lt; $n; $i++, $pos += 16)
+                {
+                    $c = $this-&gt;uint32_at($pos);
+                    $cur = $suffix . chr($c);
+
+                    $idx = strrpos($filename, $cur);
+                    if ($idx === FALSE)
+                        continue;
+                    else if ($idx + strlen($cur) == strlen($filename))
+                    {
+                        $type_off = $this-&gt;uint32_at($pos+4);
+                        if ($type_off == 0)
+                            continue;
+                        return $this-&gt;string_at($type_off);
+                    }
+                    else
+                        $queue[] = array($cur, $pos+8);
+
+                    $pos += 16;
+                }
             }
-            else
+        }
+        else
+        {
+            /* reverse suffix tree */
+            list($n, $pos) = $this-&gt;nuint32_at($this-&gt;uint32_at(16), 2);
+            $suffix = '';
+
+            for ($i = 0; $i &lt; $n;)
             {
-                $i++;
-                $pos += $this-&gt;suffix_tree_node_size;
+                $c = $this-&gt;uint32_at($pos);
+                if ($c == 0)
+                {
+                    $type_off = $this-&gt;uint32_at($pos+4);
+                    return $this-&gt;string_at($type_off);
+                }
+                else if ($filename{strlen($filename)-strlen($suffix)-1} == chr($c))
+                {
+                    $suffix = chr($c).$suffix; /* FIXME: make unicode-aware? */
+                    if (strlen($suffix) &gt;= strlen($filename))
+                        break;
+                    list($n, $pos) = $this-&gt;nuint32_at($pos+4, 2);
+                    $i = 0;
+                }
+                else
+                {
+                    $i++;
+                    $pos += 12;
+                }
             }
         }
         return NULL;</diff>
      <filename>include/mime.class.php</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>bf7011aba780ed885440821d1256e86a983a498a</id>
    </parent>
  </parents>
  <author>
    <name>Patrik Fimml</name>
    <email>patrik@fimml.at</email>
  </author>
  <url>http://github.com/soult/ewiki/commit/5bc2b0d9e22859cc8864b41f4b243d8638ea7256</url>
  <id>5bc2b0d9e22859cc8864b41f4b243d8638ea7256</id>
  <committed-date>2009-06-21T06:34:17-07:00</committed-date>
  <authored-date>2009-06-21T06:34:17-07:00</authored-date>
  <message>MIME: Make suffix tree lookup in mime.cache 1.0 work again</message>
  <tree>25240e630b2deacd50388e5240a3d83c0e42f1c7</tree>
  <committer>
    <name>Patrik Fimml</name>
    <email>patrik@fimml.at</email>
  </committer>
</commit>
