| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Reflection; |
| | 8 | | using System.Runtime.ExceptionServices; |
| | 9 | | using System.Runtime.InteropServices; |
| | 10 | | using System.Text; |
| | 11 | |
|
| | 12 | | namespace Ice.Internal; |
| | 13 | |
|
| | 14 | | // Map Bzip2 bz_stream struct to a C# struct for using with the Bzip2 low level API. |
| | 15 | | [StructLayout(LayoutKind.Sequential)] |
| | 16 | | internal struct BZStream(IntPtr nextOut, uint availOut) |
| | 17 | | { |
| | 18 | | internal IntPtr NextIn = IntPtr.Zero; |
| | 19 | | internal uint AvailIn = 0; |
| | 20 | | internal uint TotalInLo32 = 0; |
| | 21 | | internal uint TotalInHi32 = 0; |
| | 22 | |
|
| | 23 | | internal IntPtr NextOut = nextOut; |
| | 24 | | internal uint AvailOut = availOut; |
| | 25 | | internal uint TotalOutLo32 = 0; |
| | 26 | | internal uint TotalOutHi32 = 0; |
| | 27 | |
|
| | 28 | | internal IntPtr State = IntPtr.Zero; |
| | 29 | | internal IntPtr BzAlloc = IntPtr.Zero; |
| | 30 | | internal IntPtr BzFree = IntPtr.Zero; |
| | 31 | | internal IntPtr Opaque = IntPtr.Zero; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public static class BZip2 |
| | 35 | | { |
| | 36 | | private enum BzAction |
| | 37 | | { |
| | 38 | | Run = 0, |
| | 39 | | Flush = 1, |
| | 40 | | Finish = 2 |
| | 41 | | } |
| | 42 | |
|
| | 43 | | private enum BzStatus |
| | 44 | | { |
| | 45 | | Ok = 0, |
| | 46 | | RunOk = 1, |
| | 47 | | FinishOk = 3, |
| | 48 | | StreamEnd = 4, |
| | 49 | |
|
| | 50 | | SequenceError = -1, |
| | 51 | | ParamError = -2, |
| | 52 | | MemError = -3, |
| | 53 | | DataError = -4, |
| | 54 | | DataErrorMagic = -5, |
| | 55 | | IoError = -6, |
| | 56 | | UnexpectedEof = -7, |
| | 57 | | OutbuffFull = -8, |
| | 58 | | ConfigError = -9 |
| | 59 | | } |
| | 60 | |
|
| 1 | 61 | | private static readonly object _mutex = new object(); |
| | 62 | | private static bool? _isLoaded; |
| | 63 | |
|
| | 64 | | public static bool isLoaded(Ice.Logger logger) |
| | 65 | | { |
| 1 | 66 | | lock (_mutex) |
| | 67 | | { |
| 1 | 68 | | if (_isLoaded is null) |
| | 69 | | { |
| | 70 | | // Register a delegate to load native libraries used by Ice assembly. |
| 1 | 71 | | NativeLibrary.SetDllImportResolver(Assembly.GetAssembly(typeof(BZip2))!, dllImportResolver); |
| 1 | 72 | | string libNames = string.Join(", ", getPlatformNativeLibraryNames()).TrimEnd(); |
| 1 | 73 | | bool loaded = false; |
| | 74 | | try |
| | 75 | | { |
| 1 | 76 | | BZ2_bzLibVersion(); |
| 1 | 77 | | loaded = true; |
| 1 | 78 | | } |
| 0 | 79 | | catch (EntryPointNotFoundException) |
| | 80 | | { |
| 0 | 81 | | logger.warning($"found {libNames} but entry point BZ2_bzlibVersion is missing."); |
| 0 | 82 | | } |
| 0 | 83 | | catch (TypeLoadException) |
| | 84 | | { |
| | 85 | | // Expected -- bzip2 lib not installed or not in PATH. |
| 0 | 86 | | } |
| 0 | 87 | | catch (BadImageFormatException) |
| | 88 | | { |
| 0 | 89 | | var msg = new StringBuilder(); |
| 0 | 90 | | msg.AppendLine( |
| 0 | 91 | | CultureInfo.InvariantCulture, |
| 0 | 92 | | $"{libNames} could not be loaded (likely due to 32/64-bit mismatch)."); |
| 0 | 93 | | if (IntPtr.Size == 8) |
| | 94 | | { |
| 0 | 95 | | msg.AppendLine( |
| 0 | 96 | | CultureInfo.InvariantCulture, |
| 0 | 97 | | $"Make sure the directory containing the 64-bit {libNames} is in your PATH."); |
| | 98 | | } |
| 0 | 99 | | logger.warning(msg.ToString()); |
| 0 | 100 | | } |
| 1 | 101 | | _isLoaded = loaded; |
| | 102 | | } |
| 1 | 103 | | return _isLoaded.Value; |
| | 104 | | } |
| 1 | 105 | | } |
| | 106 | |
|
| | 107 | | internal static Buffer? compress(Buffer buf, int headerSize, int compressionLevel) |
| | 108 | | { |
| | 109 | | Debug.Assert(_isLoaded is true); |
| | 110 | | // In the worst case, the compressed buffer will be 1% larger than the decompressed buffer plus 600 bytes |
| | 111 | | // for the bzip2 header, plus 4 bytes for the decompressed size added by Ice protocol. |
| 1 | 112 | | int compressedLenMax = (int)((buf.size() * 1.01) + 600 + 4); |
| | 113 | |
|
| 1 | 114 | | byte[] compressed = new byte[compressedLenMax]; |
| | 115 | |
|
| | 116 | | // Prevent GC from moving the byte array, this allow to take the object address and pass it to bzip2 calls. |
| 1 | 117 | | var compressedHandle = GCHandle.Alloc(compressed, GCHandleType.Pinned); |
| 1 | 118 | | var bzStream = new BZStream( |
| 1 | 119 | | compressedHandle.AddrOfPinnedObject() + headerSize + 4, |
| 1 | 120 | | (uint)(compressed.Length - headerSize - 4)); |
| | 121 | |
|
| | 122 | | BzStatus rc; |
| | 123 | | try |
| | 124 | | { |
| 1 | 125 | | rc = (BzStatus)BZ2_bzCompressInit(ref bzStream, compressionLevel, 0, 0); |
| 1 | 126 | | if (rc != BzStatus.Ok) |
| | 127 | | { |
| 0 | 128 | | throw new ProtocolException($"Bzip2 compress init failed {rc}"); |
| | 129 | | } |
| | 130 | |
|
| | 131 | | // Compress the message body, but not the header. |
| 1 | 132 | | int decompressedLen = buf.size() - headerSize; |
| 1 | 133 | | byte[] decompressed = buf.b.rawBytes(); |
| 1 | 134 | | var payload = new ArraySegment<byte>(decompressed, headerSize, decompressedLen); |
| | 135 | |
|
| 1 | 136 | | var payloadHandle = GCHandle.Alloc(payload.Array, GCHandleType.Pinned); |
| 1 | 137 | | bzStream.NextIn = payloadHandle.AddrOfPinnedObject() + payload.Offset; |
| 1 | 138 | | bzStream.AvailIn = (uint)payload.Count; |
| | 139 | | Debug.Assert(bzStream.AvailIn > 0); |
| | 140 | |
|
| | 141 | | do |
| | 142 | | { |
| 1 | 143 | | rc = (BzStatus)BZ2_bzCompress(ref bzStream, (int)BzAction.Run); |
| | 144 | | } |
| 1 | 145 | | while (rc == BzStatus.RunOk && bzStream.AvailIn > 0); |
| 1 | 146 | | payloadHandle.Free(); |
| | 147 | |
|
| 1 | 148 | | if (rc != BzStatus.RunOk) |
| | 149 | | { |
| 0 | 150 | | throw new ProtocolException($"Bzip2 compress run failed {rc}"); |
| | 151 | | } |
| | 152 | |
|
| | 153 | | do |
| | 154 | | { |
| 1 | 155 | | rc = (BzStatus)BZ2_bzCompress(ref bzStream, (int)BzAction.Finish); |
| | 156 | | } |
| 1 | 157 | | while (rc == BzStatus.FinishOk); |
| | 158 | |
|
| 1 | 159 | | if (rc != BzStatus.StreamEnd) |
| | 160 | | { |
| 0 | 161 | | throw new ProtocolException($"Bzip2 compress finish failed {rc}"); |
| | 162 | | } |
| | 163 | |
|
| 1 | 164 | | int compressedLen = compressed.Length - (int)bzStream.AvailOut; |
| | 165 | |
|
| | 166 | | // Don't bother if the compressed data is larger than the decompressed data. |
| 1 | 167 | | if (compressedLen >= decompressedLen) |
| | 168 | | { |
| 1 | 169 | | return null; |
| | 170 | | } |
| | 171 | |
|
| 1 | 172 | | var r = new Buffer(compressed); |
| 1 | 173 | | r.resize(compressedLen, false); |
| 1 | 174 | | r.b.position(0); |
| | 175 | |
|
| | 176 | | // Copy the header from the decompressed buffer to the compressed one. |
| 1 | 177 | | r.b.put(decompressed, 0, headerSize); |
| | 178 | |
|
| | 179 | | // Add the size of the decompressed stream before the message body |
| 1 | 180 | | r.b.putInt(buf.size()); |
| | 181 | |
|
| 1 | 182 | | r.b.position(compressedLen); |
| 1 | 183 | | return r; |
| | 184 | | } |
| | 185 | | finally |
| | 186 | | { |
| 1 | 187 | | rc = (BzStatus)BZ2_bzCompressEnd(ref bzStream); |
| | 188 | | Debug.Assert(rc == BzStatus.Ok); |
| 1 | 189 | | compressedHandle.Free(); |
| 1 | 190 | | } |
| 1 | 191 | | } |
| | 192 | |
|
| | 193 | | internal static Buffer decompress(Buffer buf, int headerSize, int messageSizeMax) |
| | 194 | | { |
| | 195 | | Debug.Assert(_isLoaded is true); |
| | 196 | |
|
| 1 | 197 | | buf.b.position(headerSize); |
| 1 | 198 | | int decompressedSize = buf.b.getInt(); |
| 1 | 199 | | if (decompressedSize <= headerSize) |
| | 200 | | { |
| 0 | 201 | | throw new MarshalException("compressed size <= header size"); |
| | 202 | | } |
| 1 | 203 | | if (decompressedSize > messageSizeMax) |
| | 204 | | { |
| 1 | 205 | | Ex.throwMemoryLimitException(decompressedSize, messageSizeMax); |
| | 206 | | } |
| | 207 | |
|
| 1 | 208 | | byte[] compressed = buf.b.rawBytes(); |
| | 209 | |
|
| 1 | 210 | | byte[] decompressed = new byte[decompressedSize]; |
| | 211 | | // Prevent GC from moving the byte array, this allow to take the object address and pass it to bzip2 calls. |
| 1 | 212 | | var decompressedHandle = GCHandle.Alloc(decompressed, GCHandleType.Pinned); |
| 1 | 213 | | var compressedHandle = GCHandle.Alloc(compressed, GCHandleType.Pinned); |
| 1 | 214 | | var bzStream = new BZStream( |
| 1 | 215 | | decompressedHandle.AddrOfPinnedObject() + headerSize, |
| 1 | 216 | | (uint)(decompressedSize - headerSize)); |
| | 217 | |
|
| | 218 | | BzStatus rc; |
| | 219 | | try |
| | 220 | | { |
| 1 | 221 | | rc = (BzStatus)BZ2_bzDecompressInit(ref bzStream, 0, 0); |
| 1 | 222 | | if (rc != BzStatus.Ok) |
| | 223 | | { |
| 0 | 224 | | throw new ProtocolException($"bzip2 decompression failed: {rc}"); |
| | 225 | | } |
| | 226 | |
|
| 1 | 227 | | bzStream.NextIn = compressedHandle.AddrOfPinnedObject() + headerSize + 4; |
| 1 | 228 | | bzStream.AvailIn = (uint)(compressed.Length - headerSize - 4); |
| 1 | 229 | | rc = (BzStatus)BZ2_bzDecompress(ref bzStream); |
| 1 | 230 | | if (rc != BzStatus.StreamEnd) |
| | 231 | | { |
| 0 | 232 | | throw new ProtocolException($"bzip2 decompression failed: {rc}"); |
| | 233 | | } |
| 1 | 234 | | } |
| | 235 | | finally |
| | 236 | | { |
| 1 | 237 | | rc = (BzStatus)BZ2_bzDecompressEnd(ref bzStream); |
| | 238 | | Debug.Assert(rc == BzStatus.Ok); |
| 1 | 239 | | decompressedHandle.Free(); |
| 1 | 240 | | compressedHandle.Free(); |
| 1 | 241 | | } |
| 1 | 242 | | compressed.AsSpan(0, headerSize).CopyTo(decompressed); |
| 1 | 243 | | return new Buffer(decompressed); |
| | 244 | | } |
| | 245 | |
|
| | 246 | | [DllImport("bzip2", EntryPoint = "BZ2_bzlibVersion", ExactSpelling = true)] |
| | 247 | | private static extern IntPtr BZ2_bzLibVersion(); |
| | 248 | |
|
| | 249 | | [DllImport("bzip2", EntryPoint = "BZ2_bzCompressInit", ExactSpelling = true)] |
| | 250 | | private static extern int BZ2_bzCompressInit( |
| | 251 | | ref BZStream stream, |
| | 252 | | int blockSize100k, |
| | 253 | | int verbosity, |
| | 254 | | int workFactor); |
| | 255 | |
|
| | 256 | | [DllImport("bzip2", EntryPoint = "BZ2_bzCompress", ExactSpelling = true)] |
| | 257 | | private static extern int BZ2_bzCompress(ref BZStream stream, int action); |
| | 258 | |
|
| | 259 | | [DllImport("bzip2", EntryPoint = "BZ2_bzCompressEnd", ExactSpelling = true)] |
| | 260 | | private static extern int BZ2_bzCompressEnd(ref BZStream stream); |
| | 261 | |
|
| | 262 | | [DllImport("bzip2", EntryPoint = "BZ2_bzDecompressInit", ExactSpelling = true)] |
| | 263 | | private static extern int BZ2_bzDecompressInit(ref BZStream stream, int verbosity, int small); |
| | 264 | |
|
| | 265 | | [DllImport("bzip2", EntryPoint = "BZ2_bzDecompress", ExactSpelling = true)] |
| | 266 | | private static extern int BZ2_bzDecompress(ref BZStream stream); |
| | 267 | |
|
| | 268 | | [DllImport("bzip2", EntryPoint = "BZ2_bzDecompressEnd", ExactSpelling = true)] |
| | 269 | | private static extern int BZ2_bzDecompressEnd(ref BZStream stream); |
| | 270 | |
|
| | 271 | | private static IntPtr dllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) |
| | 272 | | { |
| 1 | 273 | | DllNotFoundException? failure = null; |
| | 274 | | Debug.Assert(libraryName == "bzip2"); |
| 1 | 275 | | foreach (string name in getPlatformNativeLibraryNames()) |
| | 276 | | { |
| | 277 | | try |
| | 278 | | { |
| 1 | 279 | | return NativeLibrary.Load(name, assembly, searchPath); |
| | 280 | | } |
| 0 | 281 | | catch (DllNotFoundException ex) |
| | 282 | | { |
| 0 | 283 | | failure = ex; |
| 0 | 284 | | } |
| | 285 | | } |
| | 286 | | Debug.Assert(failure is not null); |
| 0 | 287 | | ExceptionDispatchInfo.Throw(failure); |
| | 288 | | Debug.Assert(false); |
| 0 | 289 | | return IntPtr.Zero; |
| 1 | 290 | | } |
| | 291 | |
|
| | 292 | | private static string[] getPlatformNativeLibraryNames() |
| | 293 | | { |
| 1 | 294 | | if (AssemblyUtil.isWindows) |
| | 295 | | { |
| 0 | 296 | | return ["bzip2.dll", "bzip2d.dll"]; |
| | 297 | | } |
| 1 | 298 | | else if (AssemblyUtil.isMacOS) |
| | 299 | | { |
| 0 | 300 | | return ["libbz2.dylib"]; |
| | 301 | | } |
| | 302 | | else |
| | 303 | | { |
| 1 | 304 | | return ["libbz2.so.1.0", "libbz2.so.1", "libbz2.so"]; |
| | 305 | | } |
| | 306 | | } |
| | 307 | | } |