Remove dynamic exceptions
[anna.git] / source / core / util / ZBlock.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <arpa/inet.h>
10
11 #include <zlib.h>
12
13 #include <anna/core/util/ZBlock.hpp>
14
15 using namespace std;
16 using namespace anna;
17
18 /*
19  * El bloque de datos retornoado contiene:
20  * <int que indica el tamaño original del buffer> + buffer comprimido.
21  */
22 const DataBlock& ZBlock::compress(const DataBlock& data, const Mode::_v mode)
23 noexcept(false) {
24   if(&data == this)
25     throw RuntimeException("ZBlock::compress | Source and target object cannot be the same", ANNA_FILE_LOCATION);
26
27   int originalSize;
28
29   /*
30    * Si la fuente de datos está vacía no hay que hacer nada
31    */
32   if((originalSize = data.getSize()) == 0) {
33     DataBlock::clear();
34     return *this;
35   }
36
37   /*
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
40    */
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 */
47   buffer [0] = *w;
48   buffer [1] = *(w + 1);
49   buffer [2] = *(w + 2);
50   buffer [3] = *(w + 3);
51   int rr;
52
53   if(mode == Mode::Default)
54     rr = ::compress((Bytef*) buffer + sizeof(int), &maxSize, (Bytef*) data.getData(), originalSize);
55   else
56     rr = ::compress2((Bytef*) buffer + sizeof(int), &maxSize, (Bytef*) data.getData(), originalSize, mode);
57
58   if(rr != Z_OK) {
59     string msg("ZBlock::compress | ");
60     msg += zError(rr);
61     throw RuntimeException(msg, ANNA_FILE_LOCATION);
62   }
63
64   DataBlock::setSize((int) maxSize + sizeof(int));
65   return *this;
66 }
67
68 const DataBlock& ZBlock::uncompress(const DataBlock& zdata)
69 noexcept(false) {
70   if(&zdata == this)
71     throw RuntimeException("ZBlock::uncompress | Source and target object cannot be the same", ANNA_FILE_LOCATION);
72
73   /*
74    * Si la fuente de datos está vacía no hay que hacer nada
75    */
76   if(zdata.getSize() < sizeof(int)) {
77     DataBlock::clear();
78     return *this;
79   }
80
81   char* buffer = (char*) zdata.getData();
82   int aux;
83   uLong size;
84   char* w((char*) &aux);
85   *w  = *buffer;
86   *(w + 1) = *(buffer + 1);
87   *(w + 2) = *(buffer + 2);
88   *(w + 3) = *(buffer + 3);
89   size = ntohl(aux);
90   DataBlock::allocate((int) size);
91   int rr = ::uncompress((Bytef*) DataBlock::getData(), &size, (const Bytef*) zdata.getData() + sizeof(int), zdata.getSize() - sizeof(int));
92
93   if(rr != Z_OK) {
94     string msg("ZBlock::uncompress | ");
95     msg += zError(rr);
96     throw RuntimeException(msg, ANNA_FILE_LOCATION);
97   }
98
99   DataBlock::setSize((int) size);
100   return *this;
101 }