| 1 |
(* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* Simple interface to zstd library |
| 3 |
* |
| 4 |
* Copyright (C) 2019-2023 Alexander Koblov ([email protected]) |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 7 |
* a copy of this software and associated documentation files (the |
| 8 |
* "Software"), to deal in the Software without restriction, including |
| 9 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 10 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 11 |
* permit persons to whom the Software is furnished to do so, subject to |
| 12 |
* the following conditions: |
| 13 |
|
| 14 |
* The above copyright notice and this permission notice shall be included |
| 15 |
* in all copies or substantial portions of the Software. |
| 16 |
|
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
* |
| 25 |
* ***** END LICENSE BLOCK ***** *) |
| 26 |
|
| 27 |
{**********************************************************************} |
| 28 |
{* ABBREVIA: AbZstd.pas *} |
| 29 |
{**********************************************************************} |
| 30 |
{* ABBREVIA: TZstdCompressionStream, TZstdDecompressionStream classes *} |
| 31 |
{**********************************************************************} |
| 32 |
|
| 33 |
unit AbZstd; |
| 34 |
|
| 35 |
{$mode delphi} |
| 36 |
{$packrecords c} |
| 37 |
|
| 38 |
interface |
| 39 |
|
| 40 |
uses |
| 41 |
Classes, SysUtils, CTypes; |
| 42 |
|
| 43 |
const |
| 44 |
ZSTD_FRAMEHEADERSIZE_MAX = 18; |
| 45 |
ZSTD_CONTENTSIZE_UNKNOWN = UInt64(-1); |
| 46 |
ZSTD_CONTENTSIZE_ERROR = UInt64(-2); |
| 47 |
|
| 48 |
type |
| 49 |
PZSTD_CCtx = ^TZSTD_CCtx; |
| 50 |
TZSTD_CCtx = record end; |
| 51 |
|
| 52 |
PZSTD_DCtx = ^ZSTD_DCtx; |
| 53 |
ZSTD_DCtx = record end; |
| 54 |
|
| 55 |
PZSTD_inBuffer = ^ZSTD_inBuffer; |
| 56 |
ZSTD_inBuffer = record |
| 57 |
src: pcuint8; |
| 58 |
size: csize_t; |
| 59 |
pos: csize_t; |
| 60 |
end; |
| 61 |
|
| 62 |
PZSTD_outBuffer = ^ZSTD_outBuffer; |
| 63 |
ZSTD_outBuffer = record |
| 64 |
dst: pcuint8; |
| 65 |
size: csize_t; |
| 66 |
pos: csize_t; |
| 67 |
end; |
| 68 |
|
| 69 |
EZstdError = class(Exception); |
| 70 |
|
| 71 |
{ TZstdCompressionStream } |
| 72 |
|
| 73 |
TZstdCompressionStream = class(TStream) |
| 74 |
private |
| 75 |
FStream: TStream; |
| 76 |
FContext: PZSTD_CCtx; |
| 77 |
FBufferOut: ZSTD_outBuffer; |
| 78 |
public |
| 79 |
constructor Create(OutStream: TStream; ALevel: Integer; InSize: UInt64 = ZSTD_CONTENTSIZE_UNKNOWN); |
| 80 |
destructor Destroy; override; |
| 81 |
function Write(const Buffer; Count: Longint): Longint; override; |
| 82 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
| 83 |
end; |
| 84 |
|
| 85 |
{ TZstdDecompressionStream } |
| 86 |
|
| 87 |
TZstdDecompressionStream = class(TStream) |
| 88 |
private |
| 89 |
FStream: TStream; |
| 90 |
FContext: PZSTD_DCtx; |
| 91 |
FBufferIn: ZSTD_inBuffer; |
| 92 |
FBufferInSize: UIntPtr; |
| 93 |
FBufferOut: ZSTD_outBuffer; |
| 94 |
FBufferOutPos: UIntPtr; |
| 95 |
public |
| 96 |
constructor Create(InStream: TStream); |
| 97 |
destructor Destroy; override; |
| 98 |
function Read(var Buffer; Count: Longint): Longint; override; |
| 99 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
| 100 |
end; |
| 101 |
|
| 102 |
function ZSTD_FileSize(const InStream: TStream): UInt64; |
| 103 |
|
| 104 |
implementation |
| 105 |
|
| 106 |
uses |
| 107 |
DynLibs; |
| 108 |
|
| 109 |
const |
| 110 |
libzstd = {$IF DEFINED(MSWINDOWS)} |
| 111 |
'libzstd.dll' |
| 112 |
{$ELSEIF DEFINED(DARWIN)} |
| 113 |
'libzstd.dylib' |
| 114 |
{$ELSEIF DEFINED(UNIX)} |
| 115 |
'libzstd.so.1' |
| 116 |
{$IFEND}; |
| 117 |
|
| 118 |
type |
| 119 |
ZSTD_cParameter = ( |
| 120 |
ZSTD_c_compressionLevel = 100, |
| 121 |
ZSTD_c_checksumFlag = 201 |
| 122 |
); |
| 123 |
|
| 124 |
ZSTD_EndDirective = ( |
| 125 |
ZSTD_e_continue = 0, |
| 126 |
ZSTD_e_flush = 1, |
| 127 |
ZSTD_e_end = 2 |
| 128 |
); |
| 129 |
|
| 130 |
var |
| 131 |
ZSTD_CStreamInSize: function(): csize_t; cdecl; |
| 132 |
ZSTD_CStreamOutSize: function(): csize_t; cdecl; |
| 133 |
|
| 134 |
ZSTD_createCCtx: function(): PZSTD_CCtx; cdecl; |
| 135 |
ZSTD_freeCCtx: function(cctx: PZSTD_CCtx): csize_t; cdecl; |
| 136 |
|
| 137 |
ZSTD_CCtx_setParameter: function(cctx: PZSTD_CCtx; param: ZSTD_cParameter; value: cint): csize_t; cdecl; |
| 138 |
|
| 139 |
ZSTD_compressStream2: function(cctx: PZSTD_CCtx; |
| 140 |
output: PZSTD_outBuffer; |
| 141 |
input: PZSTD_inBuffer; |
| 142 |
endOp: ZSTD_EndDirective): csize_t; cdecl; |
| 143 |
|
| 144 |
ZSTD_DStreamInSize: function(): csize_t; cdecl; |
| 145 |
ZSTD_DStreamOutSize: function(): csize_t; cdecl; |
| 146 |
|
| 147 |
ZSTD_createDCtx: function(): PZSTD_DCtx; cdecl; |
| 148 |
ZSTD_freeDCtx: function(dctx: PZSTD_DCtx): csize_t; cdecl; |
| 149 |
|
| 150 |
ZSTD_DCtx_setMaxWindowSize: function(dctx: PZSTD_DCtx; maxWindowSize: csize_t): csize_t; cdecl; |
| 151 |
|
| 152 |
ZSTD_decompressStream: function(zds: PZSTD_DCtx; output: PZSTD_outBuffer; input: PZSTD_inBuffer): csize_t; cdecl; |
| 153 |
|
| 154 |
ZSTD_isError: function(code: csize_t): cuint; cdecl; |
| 155 |
ZSTD_getErrorName: function(code: csize_t): PAnsiChar; cdecl; |
| 156 |
|
| 157 |
ZSTD_getFrameContentSize: function(src: pcuint8; srcSize: csize_t): cuint64; cdecl; |
| 158 |
ZSTD_CCtx_setPledgedSrcSize: function(cctx: PZSTD_CCtx; pledgedSrcSize: cuint64): csize_t; cdecl; |
| 159 |
|
| 160 |
var |
| 161 |
hZstd: TLibHandle = NilHandle; |
| 162 |
|
| 163 |
procedure Initialize; |
| 164 |
begin |
| 165 |
if hZstd <> NilHandle then Exit; |
| 166 |
|
| 167 |
hZstd:= LoadLibrary(libzstd); |
| 168 |
|
| 169 |
if hZstd = NilHandle then |
| 170 |
raise EZstdError.Create('Zstd shared library not found'); |
| 171 |
|
| 172 |
@ZSTD_CStreamInSize:= GetProcAddress(hZstd, 'ZSTD_CStreamInSize'); |
| 173 |
@ZSTD_CStreamOutSize:= GetProcAddress(hZstd, 'ZSTD_CStreamOutSize'); |
| 174 |
|
| 175 |
@ZSTD_createCCtx:= GetProcAddress(hZstd, 'ZSTD_createCCtx'); |
| 176 |
@ZSTD_freeCCtx:= GetProcAddress(hZstd, 'ZSTD_freeCCtx'); |
| 177 |
|
| 178 |
@ZSTD_CCtx_setParameter:= GetProcAddress(hZstd, 'ZSTD_CCtx_setParameter'); |
| 179 |
@ZSTD_compressStream2:= GetProcAddress(hZstd, 'ZSTD_compressStream2'); |
| 180 |
|
| 181 |
@ZSTD_DStreamInSize:= GetProcAddress(hZstd, 'ZSTD_DStreamInSize'); |
| 182 |
@ZSTD_DStreamOutSize:= GetProcAddress(hZstd, 'ZSTD_DStreamOutSize'); |
| 183 |
|
| 184 |
@ZSTD_createDCtx:= GetProcAddress(hZstd, 'ZSTD_createDCtx'); |
| 185 |
@ZSTD_freeDCtx:= GetProcAddress(hZstd, 'ZSTD_freeDCtx'); |
| 186 |
|
| 187 |
@ZSTD_DCtx_setMaxWindowSize:= GetProcAddress(hZstd, 'ZSTD_DCtx_setMaxWindowSize'); |
| 188 |
@ZSTD_decompressStream:= GetProcAddress(hZstd, 'ZSTD_decompressStream'); |
| 189 |
|
| 190 |
@ZSTD_isError:= GetProcAddress(hZstd, 'ZSTD_isError'); |
| 191 |
@ZSTD_getErrorName:= GetProcAddress(hZstd, 'ZSTD_getErrorName'); |
| 192 |
|
| 193 |
@ZSTD_getFrameContentSize:= GetProcAddress(hZstd, 'ZSTD_getFrameContentSize'); |
| 194 |
@ZSTD_CCtx_setPledgedSrcSize:= GetProcAddress(hZstd, 'ZSTD_CCtx_setPledgedSrcSize'); |
| 195 |
end; |
| 196 |
|
| 197 |
function ZSTD_Check(code: csize_t): csize_t; |
| 198 |
begin |
| 199 |
Result:= code; |
| 200 |
if (ZSTD_isError(code) <> 0) then |
| 201 |
raise EZstdError.Create(ZSTD_getErrorName(code)) |
| 202 |
end; |
| 203 |
|
| 204 |
function ZSTD_FileSize(const InStream: TStream): UInt64; |
| 205 |
var |
| 206 |
APosition: Int64; |
| 207 |
ABuffer: array[1..ZSTD_FRAMEHEADERSIZE_MAX] of Byte; |
| 208 |
begin |
| 209 |
Initialize; |
| 210 |
|
| 211 |
APosition:= InStream.Position; |
| 212 |
InStream.Seek(0, soBeginning); |
| 213 |
InStream.Read(ABuffer[1], ZSTD_FRAMEHEADERSIZE_MAX); |
| 214 |
InStream.Seek(APosition, soBeginning); |
| 215 |
|
| 216 |
Result:= ZSTD_getFrameContentSize(@ABuffer[1], ZSTD_FRAMEHEADERSIZE_MAX); |
| 217 |
|
| 218 |
if (Result = ZSTD_CONTENTSIZE_UNKNOWN) or (Result = ZSTD_CONTENTSIZE_ERROR) then |
| 219 |
Result:= 0; |
| 220 |
end; |
| 221 |
|
| 222 |
{ TZstdDecompressionStream } |
| 223 |
|
| 224 |
constructor TZstdDecompressionStream.Create(InStream: TStream); |
| 225 |
const |
| 226 |
{$IFDEF CPU32} |
| 227 |
ZSTD_MAXWINDOWSIZE = (1 shl 30); |
| 228 |
{$ELSE} |
| 229 |
ZSTD_MAXWINDOWSIZE = (1 shl 31); |
| 230 |
{$ENDIF} |
| 231 |
begin |
| 232 |
Initialize; |
| 233 |
|
| 234 |
FStream:= InStream; |
| 235 |
FContext:= ZSTD_createDCtx(); |
| 236 |
|
| 237 |
FBufferInSize:= ZSTD_DStreamInSize(); |
| 238 |
FBufferIn.size := FBufferInSize; |
| 239 |
FBufferIn.pos:= FBufferInSize; |
| 240 |
FBufferIn.src:= GetMem(FBufferIn.size); |
| 241 |
|
| 242 |
FBufferOut.pos:= 0; |
| 243 |
FBufferOut.size := ZSTD_DStreamOutSize(); |
| 244 |
FBufferOut.dst:= GetMem(FBufferOut.size); |
| 245 |
|
| 246 |
ZSTD_Check(ZSTD_DCtx_setMaxWindowSize(FContext, ZSTD_MAXWINDOWSIZE)); |
| 247 |
end; |
| 248 |
|
| 249 |
destructor TZstdDecompressionStream.Destroy; |
| 250 |
begin |
| 251 |
if Assigned(FContext) then |
| 252 |
begin |
| 253 |
FreeMem(FBufferIn.src); |
| 254 |
FreeMem(FBufferOut.dst); |
| 255 |
ZSTD_freeDCtx(FContext); |
| 256 |
end; |
| 257 |
inherited Destroy; |
| 258 |
end; |
| 259 |
|
| 260 |
function TZstdDecompressionStream.Read(var Buffer; Count: Longint): Longint; |
| 261 |
var |
| 262 |
ABuffer: PByte; |
| 263 |
Available: Integer; |
| 264 |
begin |
| 265 |
Result:= 0; |
| 266 |
ABuffer:= @Buffer; |
| 267 |
|
| 268 |
while (Count > 0) do |
| 269 |
begin |
| 270 |
Available:= FBufferOut.pos - FBufferOutPos; |
| 271 |
|
| 272 |
if (Available > 0) then |
| 273 |
begin |
| 274 |
if Available > Count then |
| 275 |
Available:= Count; |
| 276 |
|
| 277 |
Move(FBufferOut.dst[FBufferOutPos], ABuffer^, Available); |
| 278 |
Inc(Result, Available); |
| 279 |
|
| 280 |
if (Available = Count) then |
| 281 |
begin |
| 282 |
Inc(FBufferOutPos, UIntPtr(Available)); |
| 283 |
Break; |
| 284 |
end; |
| 285 |
|
| 286 |
Inc(ABuffer, Available); |
| 287 |
Dec(Count, Available); |
| 288 |
end; |
| 289 |
|
| 290 |
if (FBufferIn.size > 0) and (FBufferIn.pos = FBufferIn.size) then |
| 291 |
begin |
| 292 |
FBufferIn.pos:= 0; |
| 293 |
FBufferIn.size:= FStream.Read(FBufferIn.src^, FBufferInSize); |
| 294 |
end; |
| 295 |
|
| 296 |
FBufferOutPos:= 0; |
| 297 |
FBufferOut.pos:= 0; |
| 298 |
ZSTD_Check(ZSTD_decompressStream(FContext, @FBufferOut, @FBufferIn)); |
| 299 |
|
| 300 |
if (FBufferOut.pos = 0) and (FBufferIn.size = 0) then Break; |
| 301 |
end; |
| 302 |
end; |
| 303 |
|
| 304 |
function TZstdDecompressionStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 305 |
begin |
| 306 |
Result:= -1; |
| 307 |
end; |
| 308 |
|
| 309 |
{ TZstdCompressionStream } |
| 310 |
|
| 311 |
constructor TZstdCompressionStream.Create(OutStream: TStream; ALevel: Integer; |
| 312 |
InSize: UInt64); |
| 313 |
begin |
| 314 |
Initialize; |
| 315 |
|
| 316 |
FStream:= OutStream; |
| 317 |
FContext:= ZSTD_createCCtx(); |
| 318 |
|
| 319 |
FBufferOut.size:= ZSTD_CStreamOutSize(); |
| 320 |
FBufferOut.dst:= GetMem(FBufferOut.size); |
| 321 |
|
| 322 |
ZSTD_CCtx_setPledgedSrcSize(FContext, InSize); |
| 323 |
ZSTD_Check(ZSTD_CCtx_setParameter(FContext, ZSTD_c_checksumFlag, 1)); |
| 324 |
ZSTD_Check(ZSTD_CCtx_setParameter(FContext, ZSTD_c_compressionLevel, ALevel)); |
| 325 |
end; |
| 326 |
|
| 327 |
destructor TZstdCompressionStream.Destroy; |
| 328 |
var |
| 329 |
ARemaining: csize_t; |
| 330 |
AInput: ZSTD_inBuffer; |
| 331 |
begin |
| 332 |
try |
| 333 |
if Assigned(FContext) then |
| 334 |
try |
| 335 |
FillChar({%H-}AInput, SizeOf(ZSTD_inBuffer), 0); |
| 336 |
|
| 337 |
repeat |
| 338 |
FBufferOut.pos:= 0; |
| 339 |
|
| 340 |
ARemaining:= ZSTD_Check(ZSTD_compressStream2(FContext, @FBufferOut, @AInput, ZSTD_e_end)); |
| 341 |
|
| 342 |
if (FBufferOut.pos > 0) then begin |
| 343 |
FStream.WriteBuffer(FBufferOut.dst^, FBufferOut.pos); |
| 344 |
end; |
| 345 |
until (ARemaining = 0); |
| 346 |
finally |
| 347 |
FreeMem(FBufferOut.dst); |
| 348 |
ZSTD_freeCCtx(FContext); |
| 349 |
end; |
| 350 |
finally |
| 351 |
inherited Destroy; |
| 352 |
end; |
| 353 |
end; |
| 354 |
|
| 355 |
function TZstdCompressionStream.Write(const Buffer; Count: Longint): Longint; |
| 356 |
var |
| 357 |
AInput: ZSTD_inBuffer; |
| 358 |
begin |
| 359 |
AInput.pos:= 0; |
| 360 |
AInput.src:= @Buffer; |
| 361 |
AInput.size:= Count; |
| 362 |
|
| 363 |
while AInput.pos < AInput.size do |
| 364 |
begin |
| 365 |
FBufferOut.pos:= 0; |
| 366 |
|
| 367 |
ZSTD_Check(ZSTD_compressStream2(FContext, @FBufferOut, @AInput, ZSTD_e_continue)); |
| 368 |
|
| 369 |
if (FBufferOut.pos > 0) then |
| 370 |
FStream.WriteBuffer(FBufferOut.dst^, FBufferOut.pos); |
| 371 |
end; |
| 372 |
|
| 373 |
Result:= AInput.pos; |
| 374 |
end; |
| 375 |
|
| 376 |
function TZstdCompressionStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 377 |
begin |
| 378 |
Result:= -1; |
| 379 |
end; |
| 380 |
|
| 381 |
end. |
| 382 |
|