|
| 1 | +/* Original work: |
| 2 | + * Copyright (c) 2016 - David Rouyer [email protected] Copyright (c) 2011 - 2014 Robert Važan, Google Inc. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 5 | + * |
| 6 | + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 7 | + * |
| 8 | + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. |
| 9 | + * |
| 10 | + * * Neither the name of Robert Važan nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 11 | + * |
| 12 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 13 | + * |
| 14 | + * Modified work: |
| 15 | + * Copyright 2020–present MongoDB Inc. |
| 16 | + * |
| 17 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 18 | + * you may not use this file except in compliance with the License. |
| 19 | + * You may obtain a copy of the License at |
| 20 | + * |
| 21 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | + * |
| 23 | + * Unless required by applicable law or agreed to in writing, software |
| 24 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 25 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | + * See the License for the specific language governing permissions and |
| 27 | + * limitations under the License. |
| 28 | + */ |
| 29 | + |
| 30 | +using System; |
| 31 | +using System.Runtime.InteropServices; |
| 32 | +using MongoDB.Driver.Core.NativeLibraryLoader; |
| 33 | + |
| 34 | +namespace MongoDB.Driver.Core.Compression.Snappy |
| 35 | +{ |
| 36 | + internal static class Snappy32NativeMethods |
| 37 | + { |
| 38 | + // private static fields |
| 39 | + private static readonly Lazy<LibraryLoader> __libraryLoader; |
| 40 | + private static readonly Lazy<Delegates32.snappy_compress> __snappy_compress; |
| 41 | + private static readonly Lazy<Delegates32.snappy_max_compressed_length> __snappy_max_compressed_length; |
| 42 | + private static readonly Lazy<Delegates32.snappy_uncompress> __snappy_uncompress; |
| 43 | + private static readonly Lazy<Delegates32.snappy_uncompressed_length> __snappy_uncompressed_length; |
| 44 | + private static readonly Lazy<Delegates32.snappy_validate_compressed_buffer> __snappy_validate_compressed_buffer; |
| 45 | + |
| 46 | + // static constructor |
| 47 | + static Snappy32NativeMethods() |
| 48 | + { |
| 49 | + var snappyLocator = new SnappyLocator(); |
| 50 | + __libraryLoader = new Lazy<LibraryLoader>(() => new LibraryLoader(snappyLocator), isThreadSafe: true); |
| 51 | + |
| 52 | + __snappy_compress = CreateLazyForDelegate<Delegates32.snappy_compress>(nameof(snappy_compress)); |
| 53 | + __snappy_max_compressed_length = CreateLazyForDelegate<Delegates32.snappy_max_compressed_length>(nameof(snappy_max_compressed_length)); |
| 54 | + __snappy_uncompress = CreateLazyForDelegate<Delegates32.snappy_uncompress>(nameof(snappy_uncompress)); |
| 55 | + __snappy_uncompressed_length = CreateLazyForDelegate<Delegates32.snappy_uncompressed_length>(nameof(snappy_uncompressed_length)); |
| 56 | + __snappy_validate_compressed_buffer = CreateLazyForDelegate<Delegates32.snappy_validate_compressed_buffer>(nameof(snappy_validate_compressed_buffer)); |
| 57 | + } |
| 58 | + |
| 59 | + // public static methods |
| 60 | + public static SnappyStatus snappy_compress(IntPtr input, uint input_length, IntPtr output, ref uint output_length) |
| 61 | + { |
| 62 | + return __snappy_compress.Value(input, input_length, output, ref output_length); |
| 63 | + } |
| 64 | + |
| 65 | + public static uint snappy_max_compressed_length(uint input_length) |
| 66 | + { |
| 67 | + return __snappy_max_compressed_length.Value(input_length); |
| 68 | + } |
| 69 | + |
| 70 | + public static SnappyStatus snappy_uncompress(IntPtr input, uint input_length, IntPtr output, ref uint output_length) |
| 71 | + { |
| 72 | + return __snappy_uncompress.Value(input, input_length, output, ref output_length); |
| 73 | + } |
| 74 | + |
| 75 | + public static SnappyStatus snappy_uncompressed_length(IntPtr input, uint input_length, out uint output_length) |
| 76 | + { |
| 77 | + return __snappy_uncompressed_length.Value(input, input_length, out output_length); |
| 78 | + } |
| 79 | + |
| 80 | + public static SnappyStatus snappy_validate_compressed_buffer(IntPtr input, uint input_length) |
| 81 | + { |
| 82 | + return __snappy_validate_compressed_buffer.Value(input, input_length); |
| 83 | + } |
| 84 | + |
| 85 | + // private static methods |
| 86 | + private static Lazy<TDelegate> CreateLazyForDelegate<TDelegate>(string name) |
| 87 | + { |
| 88 | + return new Lazy<TDelegate>(() => __libraryLoader.Value.GetDelegate<TDelegate>(name), isThreadSafe: true); |
| 89 | + } |
| 90 | + |
| 91 | + // nested types |
| 92 | + private class Delegates32 |
| 93 | + { |
| 94 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 95 | + public delegate SnappyStatus snappy_compress(IntPtr input, uint input_length, IntPtr output, ref uint output_length); |
| 96 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 97 | + public delegate uint snappy_max_compressed_length(uint input_length); |
| 98 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 99 | + public delegate SnappyStatus snappy_uncompress(IntPtr input, uint input_length, IntPtr output, ref uint output_length); |
| 100 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 101 | + public delegate SnappyStatus snappy_uncompressed_length(IntPtr input, uint input_length, out uint output_length); |
| 102 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 103 | + public delegate SnappyStatus snappy_validate_compressed_buffer(IntPtr input, uint input_length); |
| 104 | + } |
| 105 | + |
| 106 | + private class SnappyLocator : RelativeLibraryLocatorBase |
| 107 | + { |
| 108 | + public override string GetLibraryRelativePath(SupportedPlatform currentPlatform) |
| 109 | + { |
| 110 | + switch (currentPlatform) |
| 111 | + { |
| 112 | + case SupportedPlatform.Windows: |
| 113 | + return @"runtimes\win\native\snappy32.dll"; |
| 114 | + case SupportedPlatform.Linux: // TODO: add support for Linux and MacOS later |
| 115 | + case SupportedPlatform.MacOS: |
| 116 | + default: |
| 117 | + throw new InvalidOperationException($"Snappy is not supported on the current platform: {currentPlatform}."); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments