stoneharry

Untitled

Jan 22nd, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <zlib.hpp>
  2.  
  3. #include "zlib.h"
  4. #include <stdexcept>
  5.  
  6. namespace
  7. {
  8.   int wrap_inflate ( unsigned char* output,
  9.       unsigned int size_output ,
  10.       const unsigned char* input,
  11.       unsigned int size_input ,
  12.       unsigned long* written_output )
  13.   {
  14.     z_stream stream;
  15.  
  16.     stream.zalloc = NULL;
  17.     stream.zfree = NULL;
  18.     stream.next_in = const_cast<Bytef*> (input);
  19.     stream.avail_in = size_input;
  20.     stream.next_out = output;
  21.     stream.avail_out = size_output;
  22.  
  23.     int result = inflateInit (&stream);
  24.     if (result)
  25.     {
  26.       return result;
  27.     }
  28.     int inflateResult = inflate (&stream, 4);
  29.     if (inflateResult == 1)
  30.     {
  31.       *written_output = stream.total_out;
  32.       return inflateEnd (&stream);
  33.     }
  34.     else
  35.     {
  36.       inflateEnd (&stream);
  37.       if (inflateResult != 2 && (inflateResult != -5 || stream.avail_in))
  38.       {
  39.         return inflateResult;
  40.       }
  41.       return -3;
  42.     }
  43.   }
  44.  
  45.   int wrap_deflate ( unsigned char* output, int size_output
  46.                    , const unsigned char* input, int size_input
  47.                    , unsigned int* written_output
  48.                    )
  49.   {
  50.     z_stream stream;
  51.  
  52.     stream.zalloc = NULL;
  53.     stream.zfree = NULL;
  54.     stream.next_in = const_cast<Bytef*> (input);
  55.     stream.avail_in = size_input;
  56.     stream.next_out = output;
  57.     stream.avail_out = size_output;
  58.  
  59.     int result = deflateInit (&stream, Z_DEFAULT_COMPRESSION);
  60.     if (result)
  61.     {
  62.       return result;
  63.     }
  64.     int deflateResult = deflate (&stream, 4);
  65.     if (deflateResult != Z_STREAM_ERROR)
  66.     {
  67.       *written_output = size_output - stream.avail_out;
  68.       return deflateEnd (&stream);
  69.     }
  70.     else
  71.     {
  72.       deflateEnd (&stream);
  73.       if (deflateResult != 2 && (deflateResult != -5 || stream.avail_in))
  74.       {
  75.         return deflateResult;
  76.       }
  77.       return -3;
  78.     }
  79.   }
  80. }
  81.  
  82. namespace zlib
  83. {
  84.   void inflate (const std::vector<unsigned char>& input, std::vector<unsigned char>* output)
  85.   {
  86.     unsigned long inflated;
  87.     const int res (wrap_inflate (&(*output)[0], output->size(), &input[0], input.size(), &inflated));
  88.     if (res || inflated != output->size())
  89.     {
  90.       throw std::runtime_error ("error: not inflated correctly");
  91.     }
  92.   }
  93.  
  94.   std::vector<unsigned char> deflate (const std::vector<unsigned char>& data)
  95.   {
  96.     std::vector<unsigned char> output (data.size());
  97.     unsigned int deflated_size (0);
  98.     if (wrap_deflate (&output[0], output.size(), &data[0], data.size(), &deflated_size))
  99.     {
  100.       throw std::runtime_error ("deflating failed");
  101.     }
  102.     output.resize (deflated_size);
  103.     return output;
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment