summaryrefslogtreecommitdiff
path: root/trunk/TAO/tao/Compression/zlib/ZlibCompressor.cpp
blob: 4af3ab9ed72af079d9a9219e4fd60dfa8642dbec (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
#include "ZlibCompressor.h"

ACE_RCSID (ZLIB,
           ZlibCompressor,
           "$Id$")

#include "zlib.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

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

void
ZlibCompressor::compress (
    const ::CORBA::OctetSeq & source,
    ::CORBA::OctetSeq & target
  )
  ACE_THROW_SPEC ((
    ::CORBA::SystemException,
    ::Compression::CompressionException
  ))
{
  ::CORBA::ULong max_length = static_cast <CORBA::ULong> (source.length () * 1.1) + 12;
  target.length (max_length);

  int const retval = ::compress2 (reinterpret_cast <Bytef*>(target.get_buffer ()),
                                  reinterpret_cast <uLongf *>(&max_length),
                                  reinterpret_cast <const Bytef*>(source.get_buffer ()),
                                  source.length (),
                                  this->compression_level ());

  if (retval != Z_OK)
    {
      ACE_THROW (::Compression::CompressionException ());
    }
  else
    {
      target.length (max_length);
    }
}

void
ZlibCompressor::decompress (
  const ::CORBA::OctetSeq & source,
  ::CORBA::OctetSeq & target)
        ACE_THROW_SPEC ((
          ::CORBA::SystemException,
          ::Compression::CompressionException
        ))
{
  CORBA::ULong max_length = target.length ();
  int const retval = uncompress (reinterpret_cast <Bytef*>(target.get_buffer ()),
                                 reinterpret_cast <uLongf *>(&max_length),
                                 reinterpret_cast <const Bytef*>(source.get_buffer ()),
                                 source.length ());

  if (retval != Z_OK)
    {
      ACE_THROW (::Compression::CompressionException ());
    }
  else
    {
      target.length (max_length);
    }
}
}

TAO_END_VERSIONED_NAMESPACE_DECL