1 /* 2 bindbc/zstandard/dynload.d 3 4 Dynamically binds all function 5 6 Licensed under the Boost Software License 7 8 2018 - Laszlo Szeremi 9 */ 10 11 module bindbc.zstandard.dynload; 12 13 version(BindZSTD_Static){ 14 15 }else: 16 import bindbc.loader; 17 import bindbc.zstandard.zstd; 18 import bindbc.zstandard.config; 19 20 private{ 21 SharedLib lib; 22 ZSTDSupport loadedVersion; 23 } 24 /** 25 * Unloads the zstandard library. 26 */ 27 void unloadZSTD(){ 28 if(lib != invalidHandle){ 29 lib.unload(); 30 } 31 } 32 /** 33 * Returns the currently loaded zstandard library version, or an error code. 34 */ 35 ZSTDSupport loadedZSTDVersion(){ 36 return loadedVersion; 37 } 38 /** 39 * Loads the library and ties the function pointers to the library. 40 */ 41 ZSTDSupport loadZstandard(){ 42 version(Windows){ 43 const(char)[][2] libNames = [ 44 "libzstd.dll", 45 "zstd.dll" 46 ]; 47 }else static assert(0, "bindbc-zstandard is not yet supported on this platform."); 48 49 ZSTDSupport result; 50 foreach(name; libNames){ 51 result = loadZstandard(name.ptr); 52 if(result != ZSTDSupport.noLibrary) break; 53 } 54 return result; 55 } 56 ZSTDSupport loadZstandard(const(char)* libName){ 57 lib = load(libName); 58 if(lib == invalidHandle) { 59 return ZSTDSupport.noLibrary; 60 } 61 62 auto errCount = errorCount(); 63 loadedVersion = ZSTDSupport.badLibrary; 64 lib.bindSymbol(cast(void**)&ZSTD_versionNumber, "ZSTD_versionNumber"); 65 lib.bindSymbol(cast(void**)&ZSTD_versionString, "ZSTD_versionString"); 66 lib.bindSymbol(cast(void**)&ZSTD_compress, "ZSTD_compress"); 67 lib.bindSymbol(cast(void**)&ZSTD_decompress, "ZSTD_decompress"); 68 lib.bindSymbol(cast(void**)&ZSTD_getFrameContentSize, "ZSTD_getFrameContentSize"); 69 lib.bindSymbol(cast(void**)&ZSTD_getDecompressedSize, "ZSTD_getDecompressedSize"); 70 lib.bindSymbol(cast(void**)&ZSTD_compressBound, "ZSTD_compressBound"); 71 lib.bindSymbol(cast(void**)&ZSTD_isError, "ZSTD_isError"); 72 lib.bindSymbol(cast(void**)&ZSTD_getErrorName, "ZSTD_getErrorName"); 73 lib.bindSymbol(cast(void**)&ZSTD_maxCLevel, "ZSTD_maxCLevel"); 74 lib.bindSymbol(cast(void**)&ZSTD_createCCtx, "ZSTD_createCCtx"); 75 lib.bindSymbol(cast(void**)&ZSTD_freeCCtx, "ZSTD_freeCCtx"); 76 lib.bindSymbol(cast(void**)&ZSTD_compressCCtx, "ZSTD_compressCCtx"); 77 lib.bindSymbol(cast(void**)&ZSTD_createDCtx, "ZSTD_createDCtx"); 78 lib.bindSymbol(cast(void**)&ZSTD_freeDCtx, "ZSTD_freeDCtx"); 79 lib.bindSymbol(cast(void**)&ZSTD_decompressDCtx, "ZSTD_decompressDCtx"); 80 lib.bindSymbol(cast(void**)&ZSTD_compress_usingDict, "ZSTD_compress_usingDict"); 81 lib.bindSymbol(cast(void**)&ZSTD_decompress_usingDict, "ZSTD_decompress_usingDict"); 82 lib.bindSymbol(cast(void**)&ZSTD_createCDict, "ZSTD_createCDict"); 83 lib.bindSymbol(cast(void**)&ZSTD_freeCDict, "ZSTD_freeCDict"); 84 lib.bindSymbol(cast(void**)&ZSTD_compress_usingCDict, "ZSTD_compress_usingCDict"); 85 lib.bindSymbol(cast(void**)&ZSTD_createDDict, "ZSTD_createDDict"); 86 lib.bindSymbol(cast(void**)&ZSTD_freeDDict, "ZSTD_freeDDict"); 87 lib.bindSymbol(cast(void**)&ZSTD_decompress_usingDDict, "ZSTD_decompress_usingDDict"); 88 lib.bindSymbol(cast(void**)&ZSTD_createCStream, "ZSTD_createCStream"); 89 lib.bindSymbol(cast(void**)&ZSTD_freeCStream, "ZSTD_freeCStream"); 90 lib.bindSymbol(cast(void**)&ZSTD_initCStream, "ZSTD_initCStream"); 91 lib.bindSymbol(cast(void**)&ZSTD_compressStream, "ZSTD_compressStream"); 92 lib.bindSymbol(cast(void**)&ZSTD_flushStream, "ZSTD_flushStream"); 93 lib.bindSymbol(cast(void**)&ZSTD_endStream, "ZSTD_endStream"); 94 lib.bindSymbol(cast(void**)&ZSTD_CStreamInSize, "ZSTD_CStreamInSize"); 95 lib.bindSymbol(cast(void**)&ZSTD_CStreamOutSize, "ZSTD_CStreamOutSize"); 96 lib.bindSymbol(cast(void**)&ZSTD_createDStream, "ZSTD_createDStream"); 97 lib.bindSymbol(cast(void**)&ZSTD_freeDStream, "ZSTD_freeDStream"); 98 lib.bindSymbol(cast(void**)&ZSTD_initDStream, "ZSTD_initDStream"); 99 lib.bindSymbol(cast(void**)&ZSTD_decompressStream, "ZSTD_decompressStream"); 100 lib.bindSymbol(cast(void**)&ZSTD_DStreamInSize, "ZSTD_DStreamInSize"); 101 lib.bindSymbol(cast(void**)&ZSTD_DStreamOutSize, "ZSTD_DStreamOutSize"); 102 loadedVersion = ZSTDSupport.ZSTD1_3; 103 if(errCount != errorCount) 104 return ZSTDSupport.badLibrary; 105 version(zstd1_04){ 106 lib.bindSymbol(cast(void**)&ZSTD_compressStream2, "ZSTD_compressStream2"); 107 108 lib.bindSymbol(cast(void**)&ZSTD_cParam_getBounds, "ZSTD_cParam_getBounds"); 109 lib.bindSymbol(cast(void**)&ZSTD_CCtx_setParameter, "ZSTD_CCtx_setParameter"); 110 lib.bindSymbol(cast(void**)&ZSTD_CCtx_setPledgedSrcSize, "ZSTD_CCtx_setPledgedSrcSize"); 111 lib.bindSymbol(cast(void**)&ZSTD_CCtx_reset, "ZSTD_CCtx_reset"); 112 lib.bindSymbol(cast(void**)&ZSTD_compress2, "ZSTD_compress2"); 113 lib.bindSymbol(cast(void**)&ZSTD_getDictID_fromDict, "ZSTD_getDictID_fromDict"); 114 lib.bindSymbol(cast(void**)&ZSTD_getDictID_fromDDict, "ZSTD_getDictID_fromDDict"); 115 lib.bindSymbol(cast(void**)&ZSTD_CCtx_loadDictionary, "ZSTD_CCtx_loadDictionary"); 116 lib.bindSymbol(cast(void**)&ZSTD_DCtx_reset, "ZSTD_DCtx_reset"); 117 lib.bindSymbol(cast(void**)&ZSTD_CCtx_refCDict, "ZSTD_CCtx_refCDict"); 118 lib.bindSymbol(cast(void**)&ZSTD_CCtx_refPrefix, "ZSTD_CCtx_refPrefix"); 119 lib.bindSymbol(cast(void**)&ZSTD_DCtx_loadDictionary, "ZSTD_DCtx_loadDictionary"); 120 lib.bindSymbol(cast(void**)&ZSTD_DCtx_refDDict, "ZSTD_DCtx_refDDict"); 121 lib.bindSymbol(cast(void**)&ZSTD_DCtx_refPrefix, "ZSTD_DCtx_refPrefix"); 122 lib.bindSymbol(cast(void**)&ZSTD_sizeof_CCtx, "ZSTD_sizeof_CCtx"); 123 lib.bindSymbol(cast(void**)&ZSTD_sizeof_DCtx, "ZSTD_sizeof_DCtx"); 124 lib.bindSymbol(cast(void**)&ZSTD_sizeof_CStream, "ZSTD_sizeof_CStream"); 125 lib.bindSymbol(cast(void**)&ZSTD_sizeof_DStream, "ZSTD_sizeof_DStream"); 126 lib.bindSymbol(cast(void**)&ZSTD_sizeof_CDict, "ZSTD_sizeof_CDict"); 127 lib.bindSymbol(cast(void**)&ZSTD_sizeof_DDict, "ZSTD_sizeof_DDict"); 128 loadedVersion = ZSTDSupport.ZSTD1_4; 129 if(errCount != errorCount) 130 return ZSTDSupport.ZSTD1_3; 131 } 132 133 134 return loadedVersion; 135 }