Index: /trunk/Mars/mcore/huffman.h
===================================================================
--- /trunk/Mars/mcore/huffman.h	(revision 16441)
+++ /trunk/Mars/mcore/huffman.h	(revision 16442)
@@ -85,19 +85,28 @@
         Code lut[1<<16];
 
-        void CreateEncoder(const TreeNode *n, size_t bits=0, uint8_t nbits=0)
+        bool CreateEncoder(const TreeNode *n, size_t bits=0, uint8_t nbits=0)
         {
             if (n->isLeaf)
             {
+#ifdef __EXCEPTIONS
                 if (nbits>sizeof(size_t)*8)
                     throw std::runtime_error("Too many different symbols - this should not happen!");
-
+#else
+                if (nbits>sizeof(size_t)*8)
+                {
+                    count = 0;
+                    return false;
+                }
+#endif
                 lut[n->symbol].bits    = bits;
                 lut[n->symbol].numbits = nbits==0 ? 1 : nbits;
                 count++;
-                return;
-            }
-
-            CreateEncoder(n->zero, bits,              nbits+1);
-            CreateEncoder(n->one,  bits | (1<<nbits), nbits+1);
+                return true;
+            }
+
+            return
+                CreateEncoder(n->zero, bits,              nbits+1) &&
+                CreateEncoder(n->one,  bits | (1<<nbits), nbits+1);
+
         }
 
@@ -296,6 +305,12 @@
                 const uint8_t curbyte = (*two >> curbit);
 
+#ifdef __EXCEPTIONS
                 if (!p->lut)
                     throw std::runtime_error("Unknown bitcode in stream!");
+#else
+                if (!p->lut)
+                    return NULL;
+#endif
+
                 p = p->lut + curbyte;
                 if (!p->isLeaf)
@@ -321,5 +336,5 @@
         }
 
-        Decoder(const uint8_t* bufin, unsigned int &pindex) : isLeaf(false), lut(NULL)
+        Decoder(const uint8_t* bufin, int64_t &pindex) : isLeaf(false), lut(NULL)
         {
             // FIXME: Sanity check for size missing....
@@ -348,7 +363,15 @@
 
                 const uint8_t numbytes = numbytes_from_numbits(numbits);
+
+#ifdef __EXCEPTIONS
                 if (numbytes>sizeof(size_t))
                     throw std::runtime_error("Number of bytes for a single symbol exceeds maximum.");
-
+#else
+                if (numbytes>sizeof(size_t))
+                {
+                    pindex = -1;
+                    return;
+                }
+#endif
                 size_t bits=0;
                 memcpy(&bits, bufin+pindex, numbytes);
@@ -360,18 +383,25 @@
     };
 
-    void Encode(std::string &bufout, const uint16_t *bufin, size_t bufinlen)
-    {
+    bool Encode(std::string &bufout, const uint16_t *bufin, size_t bufinlen)
+    {
+        const Encoder encoder(bufin, bufinlen);
+
+#ifndef __EXCEPTIONS
+        if (encoder.count==0)
+            return false;
+#endif
+
         bufout.append((char*)&bufinlen, sizeof(size_t));
-
-        const Encoder encoder(bufin, bufinlen);
         encoder.WriteCodeTable(bufout);
         encoder.Encode(bufout, bufin, bufinlen);
+
+        return true;
     }
 
-    int Decode(const uint8_t *bufin,
+    int64_t Decode(const uint8_t *bufin,
                size_t         bufinlen,
                std::vector<uint16_t> &pbufout)
     {
-        unsigned int i = 0;
+        int64_t i = 0;
 
         // Read the number of data bytes this encoding represents.
@@ -385,8 +415,18 @@
         const Decoder decoder(bufin, i);
 
+#ifndef __EXCEPTIONS
+        if (i==-1)
+            return -1;
+#endif
+
         const uint8_t *in_ptr =
             decoder.Decode(bufin+i, bufin+bufinlen,
                            pbufout.data(), pbufout.data()+data_count);
 
+#ifndef __EXCEPTIONS
+        if (!in_ptr)
+            return -1;
+#endif
+
         return in_ptr-bufin;
     }
