The changes for R are all to do with portability:

Only files which are needed for the interface used are included.
File compress.c was renamed to bzcompress.c to avoid clashes.

__inline__ is not portable.  We make use of static inlining in the 
file bzlib.c and decompress.c to avoid issues with semantics of 
'extern inline'.

fdopen is POSIX but not C89 nor C99.
Solaris' cc at one time objected to inlining functions with names 
starting with 'main'.
C character strings are limited to ca 500 chars.
Various casts were missing.
Solaris cc complained about unused code, hence the use of UNREACHED.
The shifts on signed ints are undefined behaviour (this has been
reported upstream with no response).


diff -ru bzip2-1.0.6/blocksort.c bzip2/blocksort.c
--- bzip2-1.0.6/blocksort.c	2010-09-10 23:37:02.000000000 +0100
+++ bzip2/blocksort.c	2014-11-22 22:18:15.000000000 +0000
@@ -28,7 +28,7 @@
 
 /*---------------------------------------------*/
 static 
-__inline__
+R_INLINE
 void fallbackSimpleSort ( UInt32* fmap, 
                           UInt32* eclass, 
                           Int32   lo, 
@@ -202,9 +202,10 @@
       bhtab [ 0 .. 2+(nblock/32) ] destroyed
 */
 
-#define       SET_BH(zz)  bhtab[(zz) >> 5] |= (1 << ((zz) & 31))
-#define     CLEAR_BH(zz)  bhtab[(zz) >> 5] &= ~(1 << ((zz) & 31))
-#define     ISSET_BH(zz)  (bhtab[(zz) >> 5] & (1 << ((zz) & 31)))
+// R_change: these shifts on signed ints are undefined behaviour.
+#define       SET_BH(zz)  bhtab[(zz) >> 5] |= (1U << ((zz) & 31))
+#define     CLEAR_BH(zz)  bhtab[(zz) >> 5] &= ~(1U << ((zz) & 31))
+#define     ISSET_BH(zz)  (bhtab[(zz) >> 5] & (1U << ((zz) & 31)))
 #define      WORD_BH(zz)  bhtab[(zz) >> 5]
 #define UNALIGNED_BH(zz)  ((zz) & 0x01f)
 
@@ -342,8 +343,12 @@
 /*---------------------------------------------*/
 
 /*---------------------------------------------*/
+/* Solaris cc objects to inlining functions whose names start with `main' */
+#ifdef __SUNPRO_C
+# define mainGtU BZmainGtU
+#endif
 static
-__inline__
+R_INLINE
 Bool mainGtU ( UInt32  i1, 
                UInt32  i2,
                UChar*  block, 
@@ -579,7 +584,7 @@
 }
 
 static 
-__inline__
+R_INLINE
 UChar mmed3 ( UChar a, UChar b, UChar c )
 {
    UChar t;
diff -ru bzip2-1.0.6/bzlib.c bzip2/bzlib.c
--- bzip2-1.0.6/bzlib.c	2010-09-10 23:38:23.000000000 +0100
+++ bzip2/bzlib.c	2014-11-23 07:43:39.000000000 +0000
@@ -53,8 +53,9 @@
       BZ2_bzlibVersion()
    );
 
+   /* split up over-long message */
    if (errcode == 1007) {
-   fprintf(stderr,
+       fprintf(stderr, "%s%s%s",
       "\n*** A special note about internal error number 1007 ***\n"
       "\n"
       "Experience suggests that a common cause of i.e. 1007\n"
@@ -62,7 +63,7 @@
       "just happens to cross-check the results of huge numbers of\n"
       "memory reads/writes, and so acts (unintendedly) as a stress\n"
       "test of your memory system.\n"
-      "\n"
+       "\n",
       "I suggest the following: try compressing the file again,\n"
       "possibly monitoring progress in detail with the -vv flag.\n"
       "\n"
@@ -72,7 +73,7 @@
       "  (www.memtest86.com).  At the time of writing it is free (GPLd).\n"
       "  Memtest86 tests memory much more thorougly than your BIOSs\n"
       "  power-on test, and may find failures that the BIOS doesn't.\n"
-      "\n"
+      "\n",
       "* If the error can be repeatably reproduced, this is a bug in\n"
       "  bzip2, and I would very much like to hear about it.  Please\n"
       "  let me know, and, ideally, save a copy of the file causing the\n"
@@ -165,7 +166,7 @@
    if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
    if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
 
-   s = BZALLOC( sizeof(EState) );
+   s = (EState *) BZALLOC( sizeof(EState) );
    if (s == NULL) return BZ_MEM_ERROR;
    s->strm = strm;
 
@@ -174,9 +175,9 @@
    s->ftab = NULL;
 
    n       = 100000 * blockSize100k;
-   s->arr1 = BZALLOC( n                  * sizeof(UInt32) );
-   s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
-   s->ftab = BZALLOC( 65537              * sizeof(UInt32) );
+   s->arr1 = (UInt32 *) BZALLOC( n                  * sizeof(UInt32) );
+   s->arr2 = (UInt32 *) BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
+   s->ftab = (UInt32 *) BZALLOC( 65537              * sizeof(UInt32) );
 
    if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
       if (s->arr1 != NULL) BZFREE(s->arr1);
@@ -362,7 +363,7 @@
 {
    Bool progress_in  = False;
    Bool progress_out = False;
-   EState* s = strm->state;
+   EState* s = (EState *) strm->state;
    
    while (True) {
 
@@ -409,7 +410,7 @@
    Bool progress;
    EState* s;
    if (strm == NULL) return BZ_PARAM_ERROR;
-   s = strm->state;
+   s = (EState *) strm->state;
    if (s == NULL) return BZ_PARAM_ERROR;
    if (s->strm != strm) return BZ_PARAM_ERROR;
 
@@ -469,7 +470,7 @@
 {
    EState* s;
    if (strm == NULL) return BZ_PARAM_ERROR;
-   s = strm->state;
+   s = (EState *) strm->state;
    if (s == NULL) return BZ_PARAM_ERROR;
    if (s->strm != strm) return BZ_PARAM_ERROR;
 
@@ -505,7 +506,7 @@
    if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
    if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
 
-   s = BZALLOC( sizeof(DState) );
+   s = (DState *) BZALLOC( sizeof(DState) );
    if (s == NULL) return BZ_MEM_ERROR;
    s->strm                  = strm;
    strm->state              = s;
@@ -684,7 +685,7 @@
 
 
 /*---------------------------------------------------*/
-__inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
+Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
 {
    Int32 nb, na, mid;
    nb = 0;
@@ -810,7 +811,7 @@
    Bool    corrupt;
    DState* s;
    if (strm == NULL) return BZ_PARAM_ERROR;
-   s = strm->state;
+   s = (DState *) strm->state;
    if (s == NULL) return BZ_PARAM_ERROR;
    if (s->strm != strm) return BZ_PARAM_ERROR;
 
@@ -852,9 +853,11 @@
       }
    }
 
+#ifdef UNREACHED
    AssertH ( 0, 6001 );
 
    return 0;  /*NOTREACHED*/
+#endif
 }
 
 
@@ -863,7 +866,7 @@
 {
    DState* s;
    if (strm == NULL) return BZ_PARAM_ERROR;
-   s = strm->state;
+   s = (DState *) strm->state;
    if (s == NULL) return BZ_PARAM_ERROR;
    if (s->strm != strm) return BZ_PARAM_ERROR;
 
@@ -934,7 +937,7 @@
    if (ferror(f))
       { BZ_SETERR(BZ_IO_ERROR); return NULL; };
 
-   bzf = malloc ( sizeof(bzFile) );
+   bzf = (bzFile *) malloc ( sizeof(bzFile) );
    if (bzf == NULL)
       { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
 
@@ -982,7 +985,7 @@
       { BZ_SETERR(BZ_OK); return; };
 
    bzf->strm.avail_in = len;
-   bzf->strm.next_in  = buf;
+   bzf->strm.next_in  = (char *) buf;
 
    while (True) {
       bzf->strm.avail_out = BZ_MAX_UNUSED;
@@ -1107,7 +1110,7 @@
    if (ferror(f))
       { BZ_SETERR(BZ_IO_ERROR); return NULL; };
 
-   bzf = malloc ( sizeof(bzFile) );
+   bzf = (bzFile *) malloc ( sizeof(bzFile) );
    if (bzf == NULL) 
       { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
 
@@ -1179,7 +1182,7 @@
       { BZ_SETERR(BZ_OK); return 0; };
 
    bzf->strm.avail_out = len;
-   bzf->strm.next_out = buf;
+   bzf->strm.next_out = (char *) buf;
 
    while (True) {
 
@@ -1213,7 +1216,9 @@
       
    }
 
+#ifdef UNREACHED
    return 0; /*not reached*/
+#endif
 }
 
 
@@ -1379,6 +1384,15 @@
 #else
 #   define SET_BINARY_MODE(file)
 #endif
+
+#if !defined(fdopen) && defined(HAVE_FDOPEN)
+  FILE *fdopen(int fildes, const char *mode)
+#ifdef __cplusplus
+	throw ()
+#endif
+ ;
+#endif
+
 static
 BZFILE * bzopen_or_bzdopen
                ( const char *path,   /* no use when bzdopen */
@@ -1425,7 +1439,7 @@
         fp = fopen(path,mode2);
       }
    } else {
-#ifdef BZ_STRICT_ANSI
+#ifndef HAVE_FDOPEN
       fp = NULL;
 #else
       fp = fdopen(fd,mode2);
@@ -1513,11 +1527,11 @@
 /*---------------------------------------------------*/
 void BZ_API(BZ2_bzclose) (BZFILE* b)
 {
+   if (b==NULL) return;
+
    int bzerr;
-   FILE *fp;
+   FILE *fp = ((bzFile *)b)->handle;
    
-   if (b==NULL) {return;}
-   fp = ((bzFile *)b)->handle;
    if(((bzFile*)b)->writing){
       BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL);
       if(bzerr != BZ_OK){
diff -ru bzip2-1.0.6/bzlib_private.h bzip2/bzlib_private.h
--- bzip2-1.0.6/bzlib_private.h	2010-09-10 23:41:55.000000000 +0100
+++ bzip2/bzlib_private.h	2011-03-30 14:00:46.000000000 +0100
@@ -22,6 +22,9 @@
 #ifndef _BZLIB_PRIVATE_H
 #define _BZLIB_PRIVATE_H
 
+#include <config.h> /* for R_INLINE */
+#undef HAVE_FDOPEN  /* we may have, but this re-declares it and
+		       we don't use BZ2_bzdopen */
 #include <stdlib.h>
 
 #ifndef BZ_NO_STDIO
diff -ru bzip2-1.0.6/compress.c bzip2/compress.c
--- bzip2-1.0.6/compress.c	2014-11-21 10:38:37.000000000 +0000
+++ bzip2/compress.c	2011-03-30 14:00:46.000000000 +0100
@@ -69,7 +69,7 @@
 
 /*---------------------------------------------------*/
 static
-__inline__
+R_INLINE
 void bsW ( EState* s, Int32 n, UInt32 v )
 {
    bsNEEDW ( n );
diff -ru bzip2-1.0.6/decompress.c bzip2/decompress.c
--- bzip2-1.0.6/decompress.c	2010-09-10 23:43:17.000000000 +0100
+++ bzip2/decompress.c	2011-03-30 14:00:46.000000000 +0100
@@ -209,13 +209,13 @@
       s->blockSize100k -= BZ_HDR_0;
 
       if (s->smallDecompress) {
-         s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
-         s->ll4  = BZALLOC( 
-                      ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar) 
-                   );
+         s->ll16 = (UInt16*) BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
+         s->ll4  = (UChar *) BZALLOC( 
+		               ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar) 
+		             );
          if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
       } else {
-         s->tt  = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
+         s->tt  = (UInt32 *) BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
          if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
       }
 
