summaryrefslogtreecommitdiff
path: root/TAO/tao/Compression/lzo/LzoCompressor.cpp
blob: 4d714773b68b3b0483c6383c1177d7a51af4352d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// $Id$

#include "LzoCompressor.h"
#include <lzo1x.h>
#include <lzoutil.h>

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{
LzoCompressor::LzoCompressor (
  ::Compression::CompressorFactory_ptr compressor_factory,
  ::Compression::CompressionLevel compression_level) :
    BaseCompressor (compressor_factory, compression_level)
{
}

void
LzoCompressor::compress (
    const ::Compression::Buffer & source,
    ::Compression::Buffer & target)
{
  void* wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_MEM_COMPRESS);
  // Ensure maximum is at least a bit bigger than input length.
  target.length (static_cast <CORBA::ULong> ((source.length () * 1.1) + 12));
  lzo_uint max_length = static_cast <lzo_uint> (target.maximum ());

  int const retval = ::lzo1x_1_compress (
            reinterpret_cast <const unsigned char*>(source.get_buffer ()),
            source.length (),
            reinterpret_cast <unsigned char*>(target.get_buffer ()),
            &max_length,
            wrkmem);

  lzo_free(wrkmem);

  if (retval != LZO_E_OK)
    {
      throw ::Compression::CompressionException (retval, "");
    }
  else
    {
      target.length (static_cast <CORBA::ULong> (max_length));
    }

  // Update statistics for this compressor
  this->update_stats (source.length (), target.length ());
}

void
LzoCompressor::decompress (
  const ::Compression::Buffer & source,
  ::Compression::Buffer & target)
{
  lzo_uint max_length = static_cast <lzo_uint> (target.maximum ());

  int const retval = ::lzo1x_decompress (
                                 reinterpret_cast <const unsigned char*>(source.get_buffer ()),
                                 source.length (),
                                 reinterpret_cast <unsigned char*>(target.get_buffer ()),
                                 &max_length,
                                 0);

  if (retval != LZO_E_OK)
    {
      throw ::Compression::CompressionException (retval, "");
    }
  else
    {
      target.length (static_cast  <CORBA::ULong> (max_length));
    }
}
}

TAO_END_VERSIONED_NAMESPACE_DECL