1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
13 #include <anna/core/util/ZBlock.hpp>
19 * El bloque de datos retornoado contiene:
20 * <int que indica el tamaño original del buffer> + buffer comprimido.
22 const DataBlock& ZBlock::compress(const DataBlock& data, const Mode::_v mode)
23 throw(RuntimeException) {
25 throw RuntimeException("ZBlock::compress | Source and target object cannot be the same", ANNA_FILE_LOCATION);
30 * Si la fuente de datos está vacía no hay que hacer nada
32 if((originalSize = data.getSize()) == 0) {
38 * Según la documentación el buffer destino debe ser, como mínimo un 0.1% mayor que el original + 12 bytes
39 * Lo incrementamos en un 1% para simplificar
41 uLong maxSize = sizeof(int) + 12 + originalSize + (originalSize * 101) / 100;
42 DataBlock::allocate(maxSize);
43 char* buffer = (char*) DataBlock::getData();
44 int aux = htonl(originalSize);
45 char* w((char*) &aux);
46 /* Establece el tamaño original del buffer */
48 buffer [1] = *(w + 1);
49 buffer [2] = *(w + 2);
50 buffer [3] = *(w + 3);
53 if(mode == Mode::Default)
54 rr = ::compress((Bytef*) buffer + sizeof(int), &maxSize, (Bytef*) data.getData(), originalSize);
56 rr = ::compress2((Bytef*) buffer + sizeof(int), &maxSize, (Bytef*) data.getData(), originalSize, mode);
59 string msg("ZBlock::compress | ");
61 throw RuntimeException(msg, ANNA_FILE_LOCATION);
64 DataBlock::setSize((int) maxSize + sizeof(int));
68 const DataBlock& ZBlock::uncompress(const DataBlock& zdata)
69 throw(RuntimeException) {
71 throw RuntimeException("ZBlock::uncompress | Source and target object cannot be the same", ANNA_FILE_LOCATION);
74 * Si la fuente de datos está vacía no hay que hacer nada
76 if(zdata.getSize() < sizeof(int)) {
81 char* buffer = (char*) zdata.getData();
84 char* w((char*) &aux);
86 *(w + 1) = *(buffer + 1);
87 *(w + 2) = *(buffer + 2);
88 *(w + 3) = *(buffer + 3);
90 DataBlock::allocate((int) size);
91 int rr = ::uncompress((Bytef*) DataBlock::getData(), &size, (const Bytef*) zdata.getData() + sizeof(int), zdata.getSize() - sizeof(int));
94 string msg("ZBlock::uncompress | ");
96 throw RuntimeException(msg, ANNA_FILE_LOCATION);
99 DataBlock::setSize((int) size);