1diff --git a/bzip2.c b/bzip2.c
2index d95d280..7852cc4 100644
3--- a/bzip2.c
4+++ b/bzip2.c
5@@ -1070,7 +1070,11 @@ void applySavedFileAttrToOutputFile ( IntNative fd )
6    retVal = fchmod ( fd, fileMetaInfo.st_mode );
7    ERROR_IF_NOT_ZERO ( retVal );
8
9-   (void) fchown ( fd, fileMetaInfo.st_uid, fileMetaInfo.st_gid );
10+#if __GNUC__
11+   int unused __attribute__((unused));
12+   unused =
13+#endif
14+   fchown ( fd, fileMetaInfo.st_uid, fileMetaInfo.st_gid );
15    /* chown() will in many cases return with EPERM, which can
16       be safely ignored.
17    */
18diff --git a/bzip2recover.c b/bzip2recover.c
19index a8131e0..0925048 100644
20--- a/bzip2recover.c
21+++ b/bzip2recover.c
22@@ -153,7 +153,7 @@ typedef
23 /*---------------------------------------------*/
24 static BitStream* bsOpenReadStream ( FILE* stream )
25 {
26-   BitStream *bs = malloc ( sizeof(BitStream) );
27+   BitStream *bs = (BitStream *) malloc ( sizeof(BitStream) );
28    if (bs == NULL) mallocFail ( sizeof(BitStream) );
29    bs->handle = stream;
30    bs->buffer = 0;
31@@ -166,7 +166,7 @@ static BitStream* bsOpenReadStream ( FILE* stream )
32 /*---------------------------------------------*/
33 static BitStream* bsOpenWriteStream ( FILE* stream )
34 {
35-   BitStream *bs = malloc ( sizeof(BitStream) );
36+   BitStream *bs = (BitStream *) malloc ( sizeof(BitStream) );
37    if (bs == NULL) mallocFail ( sizeof(BitStream) );
38    bs->handle = stream;
39    bs->buffer = 0;
40diff --git a/bzlib.c b/bzlib.c
41index 2178655..aaf1b40 100644
42--- a/bzlib.c
43+++ b/bzlib.c
44@@ -165,7 +165,7 @@ int BZ_API(BZ2_bzCompressInit)
45    if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
46    if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
47
48-   s = BZALLOC( sizeof(EState) );
49+   s = (EState*) BZALLOC( sizeof(EState) );
50    if (s == NULL) return BZ_MEM_ERROR;
51    s->strm = strm;
52
53@@ -174,9 +174,9 @@ int BZ_API(BZ2_bzCompressInit)
54    s->ftab = NULL;
55
56    n       = 100000 * blockSize100k;
57-   s->arr1 = BZALLOC( n                  * sizeof(UInt32) );
58-   s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
59-   s->ftab = BZALLOC( 65537              * sizeof(UInt32) );
60+   s->arr1 = (UInt32*) BZALLOC( n                  * sizeof(UInt32) );
61+   s->arr2 = (UInt32*) BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
62+   s->ftab = (UInt32*) BZALLOC( 65537              * sizeof(UInt32) );
63
64    if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
65       if (s->arr1 != NULL) BZFREE(s->arr1);
66@@ -362,7 +362,7 @@ Bool handle_compress ( bz_stream* strm )
67 {
68    Bool progress_in  = False;
69    Bool progress_out = False;
70-   EState* s = strm->state;
71+   EState* s = (EState*) strm->state;
72
73    while (True) {
74
75@@ -409,7 +409,7 @@ int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
76    Bool progress;
77    EState* s;
78    if (strm == NULL) return BZ_PARAM_ERROR;
79-   s = strm->state;
80+   s = (EState*) strm->state;
81    if (s == NULL) return BZ_PARAM_ERROR;
82    if (s->strm != strm) return BZ_PARAM_ERROR;
83
84@@ -469,7 +469,7 @@ int BZ_API(BZ2_bzCompressEnd)  ( bz_stream *strm )
85 {
86    EState* s;
87    if (strm == NULL) return BZ_PARAM_ERROR;
88-   s = strm->state;
89+   s = (EState*) strm->state;
90    if (s == NULL) return BZ_PARAM_ERROR;
91    if (s->strm != strm) return BZ_PARAM_ERROR;
92
93@@ -505,7 +505,7 @@ int BZ_API(BZ2_bzDecompressInit)
94    if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
95    if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
96
97-   s = BZALLOC( sizeof(DState) );
98+   s = (DState*) BZALLOC( sizeof(DState) );
99    if (s == NULL) return BZ_MEM_ERROR;
100    s->strm                  = strm;
101    strm->state              = s;
102@@ -684,7 +684,10 @@ Bool unRLE_obuf_to_output_FAST ( DState* s )
103
104
105 /*---------------------------------------------------*/
106-__inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
107+#ifndef __cplusplus
108+__inline__
109+#endif
110+Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
111 {
112    Int32 nb, na, mid;
113    nb = 0;
114@@ -810,7 +813,7 @@ int BZ_API(BZ2_bzDecompress) ( bz_stream *strm )
115    Bool    corrupt;
116    DState* s;
117    if (strm == NULL) return BZ_PARAM_ERROR;
118-   s = strm->state;
119+   s = (DState*) strm->state;
120    if (s == NULL) return BZ_PARAM_ERROR;
121    if (s->strm != strm) return BZ_PARAM_ERROR;
122
123@@ -863,7 +866,7 @@ int BZ_API(BZ2_bzDecompressEnd)  ( bz_stream *strm )
124 {
125    DState* s;
126    if (strm == NULL) return BZ_PARAM_ERROR;
127-   s = strm->state;
128+   s = (DState*) strm->state;
129    if (s == NULL) return BZ_PARAM_ERROR;
130    if (s->strm != strm) return BZ_PARAM_ERROR;
131
132@@ -934,7 +937,7 @@ BZFILE* BZ_API(BZ2_bzWriteOpen)
133    if (ferror(f))
134       { BZ_SETERR(BZ_IO_ERROR); return NULL; };
135
136-   bzf = malloc ( sizeof(bzFile) );
137+   bzf = (bzFile*) malloc ( sizeof(bzFile) );
138    if (bzf == NULL)
139       { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
140
141@@ -982,7 +985,7 @@ void BZ_API(BZ2_bzWrite)
142       { BZ_SETERR(BZ_OK); return; };
143
144    bzf->strm.avail_in = len;
145-   bzf->strm.next_in  = buf;
146+   bzf->strm.next_in  = (char*)buf;
147
148    while (True) {
149       bzf->strm.avail_out = BZ_MAX_UNUSED;
150@@ -1107,7 +1110,7 @@ BZFILE* BZ_API(BZ2_bzReadOpen)
151    if (ferror(f))
152       { BZ_SETERR(BZ_IO_ERROR); return NULL; };
153
154-   bzf = malloc ( sizeof(bzFile) );
155+   bzf = (bzFile*) malloc ( sizeof(bzFile) );
156    if (bzf == NULL)
157       { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
158
159@@ -1179,7 +1182,7 @@ int BZ_API(BZ2_bzRead)
160       { BZ_SETERR(BZ_OK); return 0; };
161
162    bzf->strm.avail_out = len;
163-   bzf->strm.next_out = buf;
164+   bzf->strm.next_out = (char*) buf;
165
166    while (True) {
167
168diff --git a/bzlib_private.h b/bzlib_private.h
169index 3755a6f..2578c2d 100644
170--- a/bzlib_private.h
171+++ b/bzlib_private.h
172@@ -128,7 +128,7 @@ extern void bz_internal_error ( int errcode );
173
174 /*-- Stuff for randomising repetitive blocks. --*/
175
176-extern Int32 BZ2_rNums[512];
177+extern const Int32 BZ2_rNums[512];
178
179 #define BZ_RAND_DECLS                          \
180    Int32 rNToGo;                               \
181@@ -152,7 +152,7 @@ extern Int32 BZ2_rNums[512];
182
183 /*-- Stuff for doing CRCs. --*/
184
185-extern UInt32 BZ2_crc32Table[256];
186+extern const UInt32 BZ2_crc32Table[256];
187
188 #define BZ_INITIALISE_CRC(crcVar)              \
189 {                                              \
190diff --git a/crctable.c b/crctable.c
191index 2b33c25..a9212db 100644
192--- a/crctable.c
193+++ b/crctable.c
194@@ -28,7 +28,7 @@
195   comp.compression FAQ.
196 --*/
197
198-UInt32 BZ2_crc32Table[256] = {
199+const UInt32 BZ2_crc32Table[256] = {
200
201    /*-- Ugly, innit? --*/
202
203diff --git a/decompress.c b/decompress.c
204index a1a0bac..5afd651 100644
205--- a/decompress.c
206+++ b/decompress.c
207@@ -209,13 +209,13 @@ Int32 BZ2_decompress ( DState* s )
208       s->blockSize100k -= BZ_HDR_0;
209
210       if (s->smallDecompress) {
211-         s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
212-         s->ll4  = BZALLOC(
213+         s->ll16 = (UInt16*) BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
214+         s->ll4  = (UChar*) BZALLOC(
215                       ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar)
216                    );
217          if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
218       } else {
219-         s->tt  = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
220+         s->tt  = (UInt32*) BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
221          if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
222       }
223
224diff --git a/randtable.c b/randtable.c
225index bdc6d4a..70666a1 100644
226--- a/randtable.c
227+++ b/randtable.c
228@@ -23,7 +23,7 @@
229
230
231 /*---------------------------------------------*/
232-Int32 BZ2_rNums[512] = {
233+const Int32 BZ2_rNums[512] = {
234    619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
235    985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
236    733, 859, 335, 708, 621, 574, 73, 654, 730, 472,
237