1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
namespace IceInternal
{
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
internal static class SafeNativeMethods
{
[DllImport("bzip2.dll", EntryPoint="BZ2_bzlibVersion", ExactSpelling=true)]
internal static extern IntPtr windowsBZ2_bzlibVersion();
[DllImport("bzip2.dll", EntryPoint="BZ2_bzBuffToBuffCompress", ExactSpelling=true)]
internal static extern int windowsBZ2_bzBuffToBuffCompress(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int blockSize100k,
int verbosity,
int workFactor);
[DllImport("bzip2.dll", EntryPoint="BZ2_bzBuffToBuffDecompress", ExactSpelling=true)]
internal static extern int windowsBZ2_bzBuffToBuffDecompress(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int small,
int verbosity);
[DllImport("libbz2.so.1", EntryPoint="BZ2_bzlibVersion", ExactSpelling=true)]
internal static extern IntPtr unixBZ2_1_bzlibVersion();
[DllImport("libbz2.so.1", EntryPoint="BZ2_bzBuffToBuffCompress", ExactSpelling=true)]
internal static extern int unixBZ2_1_bzBuffToBuffCompress(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int blockSize100k,
int verbosity,
int workFactor);
[DllImport("libbz2.so.1", EntryPoint="BZ2_bzBuffToBuffDecompress", ExactSpelling=true)]
internal static extern int unixBZ2_1_bzBuffToBuffDecompress(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int small,
int verbosity);
[DllImport("libbz2.so.1.0", EntryPoint="BZ2_bzlibVersion", ExactSpelling=true)]
internal static extern IntPtr unixBZ2_10_bzlibVersion();
[DllImport("libbz2.so.1.0", EntryPoint="BZ2_bzBuffToBuffCompress", ExactSpelling=true)]
internal static extern int unixBZ2_10_bzBuffToBuffCompress(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int blockSize100k,
int verbosity,
int workFactor);
[DllImport("libbz2.so.1.0", EntryPoint="BZ2_bzBuffToBuffDecompress", ExactSpelling=true)]
internal static extern int unixBZ2_10_bzBuffToBuffDecompress(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int small,
int verbosity);
[DllImport("libbz2.dylib", EntryPoint="BZ2_bzlibVersion", ExactSpelling=true)]
internal static extern IntPtr macOSBZ2_bzlibVersion();
[DllImport("libbz2.dylib", EntryPoint="BZ2_bzBuffToBuffCompress", ExactSpelling=true)]
internal static extern int macOSBZ2_bzBuffToBuffCompress(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int blockSize100k,
int verbosity,
int workFactor);
[DllImport("libbz2.dylib", EntryPoint="BZ2_bzBuffToBuffDecompress", ExactSpelling=true)]
internal static extern int macOSBZ2_bzBuffToBuffDecompress(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int small,
int verbosity);
}
delegate int CompressBuffer(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int blockSize100k,
int verbosity,
int workFactor);
delegate int DecompressBuffer(byte[] dest,
ref int destLen,
byte[] source,
int sourceLen,
int small,
int verbosity);
public class BZip2
{
static BZip2()
{
//
// Simple trick to find out whether bzip2 is installed: Call the BZ2_bzlibVersion() function in the
// library. If we get an exception, the library is not available.
//
_bzlibInstalled = false;
_bzlibName = "";
try
{
if(AssemblyUtil.isWindows)
{
_bzlibName = "bzip2.dll";
SafeNativeMethods.windowsBZ2_bzlibVersion();
}
else if(AssemblyUtil.isMacOS)
{
_bzlibName = "libbz2.dylib";
SafeNativeMethods.macOSBZ2_bzlibVersion();
}
else
{
try
{
_bzlibName = "libbz2.so.1.0";
SafeNativeMethods.unixBZ2_10_bzlibVersion();
}
catch(TypeLoadException)
{
_bzlibName = "libbz2.so.1";
SafeNativeMethods.unixBZ2_1_bzlibVersion();
}
}
_bzlibInstalled = true;
}
catch(EntryPointNotFoundException)
{
Console.Error.WriteLine("warning: found " + _bzlibName + " but entry point BZ2_bzlibVersion is missing.");
}
catch(TypeLoadException)
{
// Expected -- bzip2 lib not installed or not in PATH.
}
catch(BadImageFormatException ex)
{
string lib = _bzlibName;
if(!String.IsNullOrEmpty(ex.FileName))
{
lib = ex.FileName; // Future-proof: we'll do the right thing if the FileName member is non-empty.
}
Console.Error.Write("warning: " + lib + " could not be loaded (likely due to 32/64-bit mismatch).");
if(IntPtr.Size == 8)
{
Console.Error.Write(" Make sure the directory containing the 64-bit " + lib + " is in your PATH.");
}
Console.Error.WriteLine();
}
if(AssemblyUtil.isWindows)
{
_compressBuffer = (byte[] dest, ref int destLen, byte[] source, int sourceLen, int blockSize100k,
int verbosity, int workFactor) =>
{
return SafeNativeMethods.windowsBZ2_bzBuffToBuffCompress(dest, ref destLen, source, sourceLen,
blockSize100k, verbosity, workFactor);
};
_decompressBuffer = (byte[] dest, ref int destLen, byte[] source, int sourceLen, int small,
int verbosity) =>
{
return SafeNativeMethods.windowsBZ2_bzBuffToBuffDecompress(dest, ref destLen, source, sourceLen,
small, verbosity);
};
}
else if(AssemblyUtil.isMacOS)
{
_compressBuffer = (byte[] dest, ref int destLen, byte[] source, int sourceLen, int blockSize100k,
int verbosity, int workFactor) =>
{
return SafeNativeMethods.macOSBZ2_bzBuffToBuffCompress(dest, ref destLen, source, sourceLen,
blockSize100k, verbosity, workFactor);
};
_decompressBuffer = (byte[] dest, ref int destLen, byte[] source, int sourceLen, int small,
int verbosity) =>
{
return SafeNativeMethods.macOSBZ2_bzBuffToBuffDecompress(dest, ref destLen, source, sourceLen,
small, verbosity);
};
}
else
{
if(_bzlibName == "libbz2.so.1.0")
{
_compressBuffer = (byte[] dest, ref int destLen, byte[] source, int sourceLen, int blockSize100k,
int verbosity, int workFactor) =>
{
return SafeNativeMethods.unixBZ2_10_bzBuffToBuffCompress(dest, ref destLen, source,
sourceLen, blockSize100k,
verbosity, workFactor);
};
_decompressBuffer = (byte[] dest, ref int destLen, byte[] source, int sourceLen, int small,
int verbosity) =>
{
return SafeNativeMethods.unixBZ2_10_bzBuffToBuffDecompress(dest, ref destLen, source,
sourceLen, small, verbosity);
};
}
else
{
_compressBuffer = (byte[] dest, ref int destLen, byte[] source, int sourceLen, int blockSize100k,
int verbosity, int workFactor) =>
{
return SafeNativeMethods.unixBZ2_1_bzBuffToBuffCompress(dest, ref destLen, source,
sourceLen, blockSize100k, verbosity,
workFactor);
};
_decompressBuffer = (byte[] dest, ref int destLen, byte[] source, int sourceLen, int small,
int verbosity) =>
{
return SafeNativeMethods.unixBZ2_1_bzBuffToBuffDecompress(dest, ref destLen, source,
sourceLen, small, verbosity);
};
}
}
}
static string getBZ2Error(int error)
{
string rc;
switch(error)
{
case BZ_SEQUENCE_ERROR:
{
rc = "BZ_SEQUENCE_ERROR";
break;
}
case BZ_PARAM_ERROR:
{
rc = "BZ_PARAM_ERROR";
break;
}
case BZ_MEM_ERROR:
{
rc = "BZ_MEM_ERROR";
break;
}
case BZ_DATA_ERROR:
{
rc = "BZ_DATA_ERROR";
break;
}
case BZ_DATA_ERROR_MAGIC:
{
rc = "BZ_DATA_ERROR_MAGIC";
break;
}
case BZ_IO_ERROR:
{
rc = "BZ_IO_ERROR";
break;
}
case BZ_UNEXPECTED_EOF:
{
rc = "BZ_UNEXPECTED_EOF";
break;
}
case BZ_OUTBUFF_FULL:
{
rc = "BZ_OUTBUFF_FULL";
break;
}
case BZ_CONFIG_ERROR:
{
rc = "BZ_CONFIG_ERROR";
break;
}
default:
{
rc = "Unknown bzip2 error: " + error;
break;
}
}
return rc;
}
public static bool supported()
{
return _bzlibInstalled;
}
public static Buffer compress(Buffer buf, int headerSize, int compressionLevel)
{
Debug.Assert(supported());
//
// Compress the message body, but not the header.
//
int uncompressedLen = buf.size() - headerSize;
byte[] data = buf.b.rawBytes(headerSize, uncompressedLen);
int compressedLen = (int)(uncompressedLen * 1.01 + 600);
byte[] compressed = new byte[compressedLen];
int rc = _compressBuffer(compressed, ref compressedLen, data, uncompressedLen, compressionLevel, 0, 0);
if(rc == BZ_OUTBUFF_FULL)
{
return null;
}
else if(rc < 0)
{
Ice.CompressionException ex = new Ice.CompressionException("BZ2_bzBuffToBuffCompress failed");
ex.reason = getBZ2Error(rc);
throw ex;
}
//
// Don't bother if the compressed data is larger than the
// uncompressed data.
//
if(compressedLen >= uncompressedLen)
{
return null;
}
Buffer r = new Buffer();
r.resize(headerSize + 4 + compressedLen, false);
r.b.position(0);
//
// Copy the header from the uncompressed stream to the
// compressed one.
//
r.b.put(buf.b.rawBytes(0, headerSize));
//
// Add the size of the uncompressed stream before the
// message body.
//
r.b.putInt(buf.size());
//
// Add the compressed message body.
//
r.b.put(compressed, 0, compressedLen);
return r;
}
public static Buffer uncompress(Buffer buf, int headerSize, int messageSizeMax)
{
Debug.Assert(supported());
buf.b.position(headerSize);
int uncompressedSize = buf.b.getInt();
if(uncompressedSize <= headerSize)
{
throw new Ice.IllegalMessageSizeException("compressed size <= header size");
}
if(uncompressedSize > messageSizeMax)
{
IceInternal.Ex.throwMemoryLimitException(uncompressedSize, messageSizeMax);
}
int compressedLen = buf.size() - headerSize - 4;
byte[] compressed = buf.b.rawBytes(headerSize + 4, compressedLen);
int uncompressedLen = uncompressedSize - headerSize;
byte[] uncompressed = new byte[uncompressedLen];
int rc = _decompressBuffer(uncompressed, ref uncompressedLen, compressed, compressedLen, 0, 0);
if(rc < 0)
{
Ice.CompressionException ex = new Ice.CompressionException("BZ2_bzBuffToBuffDecompress failed");
ex.reason = getBZ2Error(rc);
throw ex;
}
Buffer r = new Buffer();
r.resize(uncompressedSize, false);
//
// Copy the header from the compressed buffer to the uncompressed one.
//
r.b.position(0);
r.b.put(buf.b.rawBytes(), 0, headerSize);
r.b.put(uncompressed);
return r;
}
private static bool _bzlibInstalled;
private static string _bzlibName;
private static CompressBuffer _compressBuffer;
private static DecompressBuffer _decompressBuffer;
const int BZ_SEQUENCE_ERROR = -1;
const int BZ_PARAM_ERROR = -2;
const int BZ_MEM_ERROR = -3;
const int BZ_DATA_ERROR = -4;
const int BZ_DATA_ERROR_MAGIC = -5;
const int BZ_IO_ERROR = -6;
const int BZ_UNEXPECTED_EOF = -7;
const int BZ_OUTBUFF_FULL = -8;
const int BZ_CONFIG_ERROR = -9;
}
}
|