buffer_demo.adb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ----------------------------------------------------------------
  2. -- ZLib for Ada thick binding. --
  3. -- --
  4. -- Copyright (C) 2002-2004 Dmitriy Anisimkov --
  5. -- --
  6. -- Open source license information is in the zlib.ads file. --
  7. ----------------------------------------------------------------
  8. --
  9. -- $Id: buffer_demo.adb,v 1.3 2004/09/06 06:55:35 vagul Exp $
  10. -- This demo program provided by Dr Steve Sangwine <sjs@essex.ac.uk>
  11. --
  12. -- Demonstration of a problem with Zlib-Ada (already fixed) when a buffer
  13. -- of exactly the correct size is used for decompressed data, and the last
  14. -- few bytes passed in to Zlib are checksum bytes.
  15. -- This program compresses a string of text, and then decompresses the
  16. -- compressed text into a buffer of the same size as the original text.
  17. with Ada.Streams; use Ada.Streams;
  18. with Ada.Text_IO;
  19. with ZLib; use ZLib;
  20. procedure Buffer_Demo is
  21. EOL : Character renames ASCII.LF;
  22. Text : constant String
  23. := "Four score and seven years ago our fathers brought forth," & EOL &
  24. "upon this continent, a new nation, conceived in liberty," & EOL &
  25. "and dedicated to the proposition that `all men are created equal'.";
  26. Source : Stream_Element_Array (1 .. Text'Length);
  27. for Source'Address use Text'Address;
  28. begin
  29. Ada.Text_IO.Put (Text);
  30. Ada.Text_IO.New_Line;
  31. Ada.Text_IO.Put_Line
  32. ("Uncompressed size : " & Positive'Image (Text'Length) & " bytes");
  33. declare
  34. Compressed_Data : Stream_Element_Array (1 .. Text'Length);
  35. L : Stream_Element_Offset;
  36. begin
  37. Compress : declare
  38. Compressor : Filter_Type;
  39. I : Stream_Element_Offset;
  40. begin
  41. Deflate_Init (Compressor);
  42. -- Compress the whole of T at once.
  43. Translate (Compressor, Source, I, Compressed_Data, L, Finish);
  44. pragma Assert (I = Source'Last);
  45. Close (Compressor);
  46. Ada.Text_IO.Put_Line
  47. ("Compressed size : "
  48. & Stream_Element_Offset'Image (L) & " bytes");
  49. end Compress;
  50. -- Now we decompress the data, passing short blocks of data to Zlib
  51. -- (because this demonstrates the problem - the last block passed will
  52. -- contain checksum information and there will be no output, only a
  53. -- check inside Zlib that the checksum is correct).
  54. Decompress : declare
  55. Decompressor : Filter_Type;
  56. Uncompressed_Data : Stream_Element_Array (1 .. Text'Length);
  57. Block_Size : constant := 4;
  58. -- This makes sure that the last block contains
  59. -- only Adler checksum data.
  60. P : Stream_Element_Offset := Compressed_Data'First - 1;
  61. O : Stream_Element_Offset;
  62. begin
  63. Inflate_Init (Decompressor);
  64. loop
  65. Translate
  66. (Decompressor,
  67. Compressed_Data
  68. (P + 1 .. Stream_Element_Offset'Min (P + Block_Size, L)),
  69. P,
  70. Uncompressed_Data
  71. (Total_Out (Decompressor) + 1 .. Uncompressed_Data'Last),
  72. O,
  73. No_Flush);
  74. Ada.Text_IO.Put_Line
  75. ("Total in : " & Count'Image (Total_In (Decompressor)) &
  76. ", out : " & Count'Image (Total_Out (Decompressor)));
  77. exit when P = L;
  78. end loop;
  79. Ada.Text_IO.New_Line;
  80. Ada.Text_IO.Put_Line
  81. ("Decompressed text matches original text : "
  82. & Boolean'Image (Uncompressed_Data = Source));
  83. end Decompress;
  84. end;
  85. end Buffer_Demo;