diff --git a/Codec/Compression/BZip/Stream.hsc b/Codec/Compression/BZip/Stream.hsc
--- a/Codec/Compression/BZip/Stream.hsc
+++ b/Codec/Compression/BZip/Stream.hsc
@@ -58,10 +58,14 @@
 import Foreign
          ( Word8, Ptr, nullPtr, plusPtr, peekByteOff, pokeByteOff, mallocBytes
          , ForeignPtr, FinalizerPtr, newForeignPtr_, addForeignPtrFinalizer
-         , finalizeForeignPtr, withForeignPtr, touchForeignPtr
-         , unsafeForeignPtrToPtr, unsafePerformIO )
+         , finalizeForeignPtr, withForeignPtr, touchForeignPtr )
+#if MIN_VERSION_base(4,4,0)
+import Foreign.ForeignPtr.Unsafe ( unsafeForeignPtrToPtr )
+import System.IO.Unsafe          ( unsafePerformIO )
+#else
+import Foreign ( unsafeForeignPtrToPtr, unsafePerformIO )
+#endif
 import Foreign.C
-         ( CInt, CUInt )
 import Data.ByteString.Internal (nullForeignPtr)
 import System.IO.Unsafe (unsafeInterleaveIO)
 import System.IO (hPutStrLn, stderr)
diff --git a/bunzip2.hs b/bunzip2.hs
deleted file mode 100644
--- a/bunzip2.hs
+++ /dev/null
@@ -1,6 +0,0 @@
-module Main where
-
-import qualified Data.ByteString.Lazy as B
-import qualified Codec.Compression.BZip as BZip
-
-main = B.interact BZip.decompress
diff --git a/bzip2.hs b/bzip2.hs
deleted file mode 100644
--- a/bzip2.hs
+++ /dev/null
@@ -1,6 +0,0 @@
-module Main where
-
-import qualified Data.ByteString.Lazy as B
-import qualified Codec.Compression.BZip as BZip
-
-main = B.interact BZip.compress
diff --git a/bzlib.cabal b/bzlib.cabal
--- a/bzlib.cabal
+++ b/bzlib.cabal
@@ -1,6 +1,6 @@
 name:            bzlib
-version:         0.5.0.1
-copyright:       (c) 2006-2008 Duncan Coutts
+version:         0.5.0.2
+copyright:       (c) 2006-2011 Duncan Coutts
 license:         BSD3
 license-file:    LICENSE
 author:          Duncan Coutts <duncan@community.haskell.org>
@@ -19,7 +19,7 @@
 cabal-version:   >= 1.6
 extra-source-files: cbits/bzlib_private.h cbits/LICENSE
                     -- demo programs:
-                    bzip2.hs bunzip2.hs
+                    examples/bzip2.hs examples/bunzip2.hs
 
 source-repository head
   type: darcs
@@ -39,7 +39,7 @@
     extra-libraries: bz2
   else
     -- However for the benefit of users of Windows (which does not have zlib
-    -- by default) we bundle a complete copy of the C sources of bzip2-1.0.5
+    -- by default) we bundle a complete copy of the C sources of bzip2-1.0.6
     c-sources:     cbits/blocksort.c cbits/bzlib.c cbits/compress.c
                    cbits/crctable.c cbits/decompress.c cbits/huffman.c
                    cbits/randtable.c
diff --git a/cbits/LICENSE b/cbits/LICENSE
--- a/cbits/LICENSE
+++ b/cbits/LICENSE
@@ -2,7 +2,7 @@
 --------------------------------------------------------------------------
 
 This program, "bzip2", the associated library "libbzip2", and all
-documentation, are copyright (C) 1996-2007 Julian R Seward.  All
+documentation, are copyright (C) 1996-2010 Julian R Seward.  All
 rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -37,6 +37,6 @@
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 Julian Seward, jseward@bzip.org
-bzip2/libbzip2 version 1.0.5 of 10 December 2007
+bzip2/libbzip2 version 1.0.6 of 6 September 2010
 
 --------------------------------------------------------------------------
diff --git a/cbits/blocksort.c b/cbits/blocksort.c
--- a/cbits/blocksort.c
+++ b/cbits/blocksort.c
@@ -8,8 +8,8 @@
    This file is part of bzip2/libbzip2, a program and library for
    lossless, block-sorting data compression.
 
-   bzip2/libbzip2 version 1.0.5 of 10 December 2007
-   Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
+   bzip2/libbzip2 version 1.0.6 of 6 September 2010
+   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
 
    Please read the WARNING, DISCLAIMER and PATENTS sections in the 
    README file.
diff --git a/cbits/bzlib.c b/cbits/bzlib.c
--- a/cbits/bzlib.c
+++ b/cbits/bzlib.c
@@ -8,8 +8,8 @@
    This file is part of bzip2/libbzip2, a program and library for
    lossless, block-sorting data compression.
 
-   bzip2/libbzip2 version 1.0.5 of 10 December 2007
-   Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
+   bzip2/libbzip2 version 1.0.6 of 6 September 2010
+   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
 
    Please read the WARNING, DISCLAIMER and PATENTS sections in the 
    README file.
diff --git a/cbits/bzlib.h b/cbits/bzlib.h
--- a/cbits/bzlib.h
+++ b/cbits/bzlib.h
@@ -8,8 +8,8 @@
    This file is part of bzip2/libbzip2, a program and library for
    lossless, block-sorting data compression.
 
-   bzip2/libbzip2 version 1.0.5 of 10 December 2007
-   Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
+   bzip2/libbzip2 version 1.0.6 of 6 September 2010
+   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
 
    Please read the WARNING, DISCLAIMER and PATENTS sections in the 
    README file.
@@ -94,6 +94,12 @@
 #   define BZ_EXTERN extern
 #endif
 
+/* Using WINAPI causes the function to be exported using the 'stdcall'
+ * calling convention. We'd rather use the ordinary calling convention
+ * so we redefine BZ_API to do nothing.
+ */
+#undef BZ_API
+#define BZ_API(func) func
 
 /*-- Core (low-level) library functions --*/
 
diff --git a/cbits/bzlib_private.h b/cbits/bzlib_private.h
--- a/cbits/bzlib_private.h
+++ b/cbits/bzlib_private.h
@@ -8,8 +8,8 @@
    This file is part of bzip2/libbzip2, a program and library for
    lossless, block-sorting data compression.
 
-   bzip2/libbzip2 version 1.0.5 of 10 December 2007
-   Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
+   bzip2/libbzip2 version 1.0.6 of 6 September 2010
+   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
 
    Please read the WARNING, DISCLAIMER and PATENTS sections in the 
    README file.
@@ -36,7 +36,7 @@
 
 /*-- General stuff. --*/
 
-#define BZ_VERSION  "1.0.5, 10-Dec-2007"
+#define BZ_VERSION  "1.0.6, 6-Sept-2010"
 
 typedef char            Char;
 typedef unsigned char   Bool;
diff --git a/cbits/compress.c b/cbits/compress.c
--- a/cbits/compress.c
+++ b/cbits/compress.c
@@ -8,8 +8,8 @@
    This file is part of bzip2/libbzip2, a program and library for
    lossless, block-sorting data compression.
 
-   bzip2/libbzip2 version 1.0.5 of 10 December 2007
-   Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
+   bzip2/libbzip2 version 1.0.6 of 6 September 2010
+   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
 
    Please read the WARNING, DISCLAIMER and PATENTS sections in the 
    README file.
diff --git a/cbits/crctable.c b/cbits/crctable.c
--- a/cbits/crctable.c
+++ b/cbits/crctable.c
@@ -8,8 +8,8 @@
    This file is part of bzip2/libbzip2, a program and library for
    lossless, block-sorting data compression.
 
-   bzip2/libbzip2 version 1.0.5 of 10 December 2007
-   Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
+   bzip2/libbzip2 version 1.0.6 of 6 September 2010
+   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
 
    Please read the WARNING, DISCLAIMER and PATENTS sections in the 
    README file.
diff --git a/cbits/decompress.c b/cbits/decompress.c
--- a/cbits/decompress.c
+++ b/cbits/decompress.c
@@ -8,8 +8,8 @@
    This file is part of bzip2/libbzip2, a program and library for
    lossless, block-sorting data compression.
 
-   bzip2/libbzip2 version 1.0.5 of 10 December 2007
-   Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
+   bzip2/libbzip2 version 1.0.6 of 6 September 2010
+   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
 
    Please read the WARNING, DISCLAIMER and PATENTS sections in the 
    README file.
@@ -381,6 +381,13 @@
             es = -1;
             N = 1;
             do {
+               /* Check that N doesn't get too big, so that es doesn't
+                  go negative.  The maximum value that can be
+                  RUNA/RUNB encoded is equal to the block size (post
+                  the initial RLE), viz, 900k, so bounding N at 2
+                  million should guard against overflow without
+                  rejecting any legitimate inputs. */
+               if (N >= 2*1024*1024) RETURN(BZ_DATA_ERROR);
                if (nextSym == BZ_RUNA) es = es + (0+1) * N; else
                if (nextSym == BZ_RUNB) es = es + (1+1) * N;
                N = N * 2;
@@ -485,12 +492,25 @@
          RETURN(BZ_DATA_ERROR);
 
       /*-- Set up cftab to facilitate generation of T^(-1) --*/
+      /* Check: unzftab entries in range. */
+      for (i = 0; i <= 255; i++) {
+         if (s->unzftab[i] < 0 || s->unzftab[i] > nblock)
+            RETURN(BZ_DATA_ERROR);
+      }
+      /* Actually generate cftab. */
       s->cftab[0] = 0;
       for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1];
       for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1];
+      /* Check: cftab entries in range. */
       for (i = 0; i <= 256; i++) {
          if (s->cftab[i] < 0 || s->cftab[i] > nblock) {
             /* s->cftab[i] can legitimately be == nblock */
+            RETURN(BZ_DATA_ERROR);
+         }
+      }
+      /* Check: cftab entries non-descending. */
+      for (i = 1; i <= 256; i++) {
+         if (s->cftab[i-1] > s->cftab[i]) {
             RETURN(BZ_DATA_ERROR);
          }
       }
diff --git a/cbits/huffman.c b/cbits/huffman.c
--- a/cbits/huffman.c
+++ b/cbits/huffman.c
@@ -8,8 +8,8 @@
    This file is part of bzip2/libbzip2, a program and library for
    lossless, block-sorting data compression.
 
-   bzip2/libbzip2 version 1.0.5 of 10 December 2007
-   Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
+   bzip2/libbzip2 version 1.0.6 of 6 September 2010
+   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
 
    Please read the WARNING, DISCLAIMER and PATENTS sections in the 
    README file.
diff --git a/cbits/randtable.c b/cbits/randtable.c
--- a/cbits/randtable.c
+++ b/cbits/randtable.c
@@ -8,8 +8,8 @@
    This file is part of bzip2/libbzip2, a program and library for
    lossless, block-sorting data compression.
 
-   bzip2/libbzip2 version 1.0.5 of 10 December 2007
-   Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
+   bzip2/libbzip2 version 1.0.6 of 6 September 2010
+   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
 
    Please read the WARNING, DISCLAIMER and PATENTS sections in the 
    README file.
diff --git a/examples/bunzip2.hs b/examples/bunzip2.hs
new file mode 100644
--- /dev/null
+++ b/examples/bunzip2.hs
@@ -0,0 +1,6 @@
+module Main where
+
+import qualified Data.ByteString.Lazy as B
+import qualified Codec.Compression.BZip as BZip
+
+main = B.interact BZip.decompress
diff --git a/examples/bzip2.hs b/examples/bzip2.hs
new file mode 100644
--- /dev/null
+++ b/examples/bzip2.hs
@@ -0,0 +1,6 @@
+module Main where
+
+import qualified Data.ByteString.Lazy as B
+import qualified Codec.Compression.BZip as BZip
+
+main = B.interact BZip.compress
