zuncompr.pas 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. unit zuncompr;
  2. { uncompr.c -- decompress 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, zinflate;
  12. { ===========================================================================
  13. Decompresses the source buffer into the destination buffer. sourceLen is
  14. the byte length of the source buffer. Upon entry, destLen is the total
  15. size of the destination buffer, which must be large enough to hold the
  16. entire uncompressed data. (The size of the uncompressed data must have
  17. been saved previously by the compressor and transmitted to the decompressor
  18. by some mechanism outside the scope of this compression library.)
  19. Upon exit, destLen is the actual size of the compressed buffer.
  20. This function can be used to decompress a whole file at once if the
  21. input file is mmap'ed.
  22. uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  23. enough memory, Z_BUF_ERROR if there was not enough room in the output
  24. buffer, or Z_DATA_ERROR if the input data was corrupted.
  25. }
  26. function uncompress (dest : Pbyte;
  27. var destLen : cardinal;
  28. const source : array of byte;
  29. sourceLen : cardinal) : integer;
  30. implementation
  31. function uncompress (dest : Pbyte;
  32. var destLen : cardinal;
  33. const source : array of byte;
  34. sourceLen : cardinal) : integer;
  35. var
  36. stream : z_stream;
  37. err : integer;
  38. begin
  39. stream.next_in := Pbyte(@source);
  40. stream.avail_in := cardinal(sourceLen);
  41. { Check for source > 64K on 16-bit machine: }
  42. if (cardinal(stream.avail_in) <> sourceLen) then
  43. begin
  44. uncompress := Z_BUF_ERROR;
  45. exit;
  46. end;
  47. stream.next_out := dest;
  48. stream.avail_out := cardinal(destLen);
  49. if (cardinal(stream.avail_out) <> destLen) then
  50. begin
  51. uncompress := Z_BUF_ERROR;
  52. exit;
  53. end;
  54. err := inflateInit(stream);
  55. if (err <> Z_OK) then
  56. begin
  57. uncompress := err;
  58. exit;
  59. end;
  60. err := inflate(stream, Z_FINISH);
  61. if (err <> Z_STREAM_END) then
  62. begin
  63. inflateEnd(stream);
  64. if err = Z_OK then
  65. uncompress := Z_BUF_ERROR
  66. else
  67. uncompress := err;
  68. exit;
  69. end;
  70. destLen := stream.total_out;
  71. err := inflateEnd(stream);
  72. uncompress := err;
  73. end;
  74. end.