zcompres.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Unit zCompres;
  2. { compress.c -- compress a memory buffer
  3. Copyright (C) 1995-1998 Jean-loup Gailly.
  4. Pascal tranlastion
  5. Copyright (C) 1998 by Jacques Nomssi Nzali
  6. For conditions of distribution and use, see copyright notice in readme.txt
  7. }
  8. interface
  9. {$I zconf.inc}
  10. uses
  11. zbase, zdeflate;
  12. { utility functions }
  13. {EXPORT}
  14. function compress (dest : Pbyte;
  15. var destLen : cardinal;
  16. const source : array of Byte;
  17. sourceLen : cardinal) : integer;
  18. { Compresses the source buffer into the destination buffer. sourceLen is
  19. the byte length of the source buffer. Upon entry, destLen is the total
  20. size of the destination buffer, which must be at least 0.1% larger than
  21. sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
  22. compressed buffer.
  23. This function can be used to compress a whole file at once if the
  24. input file is mmap'ed.
  25. compress returns Z_OK if success, Z_MEM_ERROR if there was not
  26. enough memory, Z_BUF_ERROR if there was not enough room in the output
  27. buffer. }
  28. {EXPORT}
  29. function compress2 (dest : Pbyte;
  30. var destLen : cardinal;
  31. const source : array of byte;
  32. sourceLen : cardinal;
  33. level : integer) : integer;
  34. { Compresses the source buffer into the destination buffer. The level
  35. parameter has the same meaning as in deflateInit. sourceLen is the byte
  36. length of the source buffer. Upon entry, destLen is the total size of the
  37. destination buffer, which must be at least 0.1% larger than sourceLen plus
  38. 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
  39. compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  40. memory, Z_BUF_ERROR if there was not enough room in the output buffer,
  41. Z_STREAM_ERROR if the level parameter is invalid. }
  42. implementation
  43. { ===========================================================================
  44. }
  45. function compress2 (dest : Pbyte;
  46. var destLen : cardinal;
  47. const source : array of byte;
  48. sourceLen : cardinal;
  49. level : integer) : integer;
  50. var
  51. stream : z_stream;
  52. err : integer;
  53. begin
  54. stream.next_in := Pbyte(@source);
  55. stream.avail_in := cardinal(sourceLen);
  56. {$ifdef MAXSEG_64K}
  57. { Check for source > 64K on 16-bit machine: }
  58. if (cardinal(stream.avail_in) <> sourceLen) then
  59. begin
  60. compress2 := Z_BUF_ERROR;
  61. exit;
  62. end;
  63. {$endif}
  64. stream.next_out := dest;
  65. stream.avail_out := cardinal(destLen);
  66. if (cardinal(stream.avail_out) <> destLen) then
  67. begin
  68. compress2 := Z_BUF_ERROR;
  69. exit;
  70. end;
  71. err := deflateInit(stream, level);
  72. if (err <> Z_OK) then
  73. begin
  74. compress2 := err;
  75. exit;
  76. end;
  77. err := deflate(stream, Z_FINISH);
  78. if (err <> Z_STREAM_END) then
  79. begin
  80. deflateEnd(stream);
  81. if err = Z_OK then
  82. compress2 := Z_BUF_ERROR
  83. else
  84. compress2 := err;
  85. exit;
  86. end;
  87. destLen := stream.total_out;
  88. err := deflateEnd(stream);
  89. compress2 := err;
  90. end;
  91. { ===========================================================================
  92. }
  93. function compress (dest : Pbyte;
  94. var destLen : cardinal;
  95. const source : array of Byte;
  96. sourceLen : cardinal) : integer;
  97. begin
  98. compress := compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
  99. end;
  100. end.