| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2011, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
#include "noit_check_log_helpers.h" |
|---|
| 35 |
#include <zlib.h> |
|---|
| 36 |
#include "utils/noit_b64.h" |
|---|
| 37 |
#include "utils/noit_log.h" |
|---|
| 38 |
|
|---|
| 39 |
int |
|---|
| 40 |
noit_check_log_bundle_compress_b64(noit_compression_type_t ctype, |
|---|
| 41 |
const char *buf_in, |
|---|
| 42 |
unsigned int len_in, |
|---|
| 43 |
char ** buf_out, |
|---|
| 44 |
unsigned int * len_out) { |
|---|
| 45 |
uLong initial_dlen, dlen; |
|---|
| 46 |
char *compbuff, *b64buff; |
|---|
| 47 |
|
|---|
| 48 |
// Compress saves 25% of space (ex 470 -> 330) |
|---|
| 49 |
switch(ctype) { |
|---|
| 50 |
case NOIT_COMPRESS_ZLIB: |
|---|
| 51 |
/* Compress */ |
|---|
| 52 |
initial_dlen = dlen = compressBound((uLong)len_in); |
|---|
| 53 |
compbuff = malloc(initial_dlen); |
|---|
| 54 |
if(!compbuff) return -1; |
|---|
| 55 |
if(Z_OK != compress2((Bytef *)compbuff, &dlen, |
|---|
| 56 |
(Bytef *)buf_in, len_in, 9)) { |
|---|
| 57 |
noitL(noit_error, "Error compressing bundled metrics.\n"); |
|---|
| 58 |
free(compbuff); |
|---|
| 59 |
return -1; |
|---|
| 60 |
} |
|---|
| 61 |
break; |
|---|
| 62 |
case NOIT_COMPRESS_NONE: |
|---|
| 63 |
// Or don't |
|---|
| 64 |
dlen = (uLong)len_in; |
|---|
| 65 |
compbuff = (char *)buf_in; |
|---|
| 66 |
break; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
/* Encode */ |
|---|
| 70 |
// Problems with the calculation? |
|---|
| 71 |
initial_dlen = ((dlen + 2) / 3 * 4); |
|---|
| 72 |
b64buff = malloc(initial_dlen); |
|---|
| 73 |
if (!b64buff) { |
|---|
| 74 |
if(ctype == NOIT_COMPRESS_ZLIB) free(compbuff); |
|---|
| 75 |
return -1; |
|---|
| 76 |
} |
|---|
| 77 |
dlen = noit_b64_encode((unsigned char *)compbuff, dlen, |
|---|
| 78 |
(char *)b64buff, initial_dlen); |
|---|
| 79 |
if(ctype == NOIT_COMPRESS_ZLIB) free(compbuff); |
|---|
| 80 |
if(dlen == 0) { |
|---|
| 81 |
noitL(noit_error, "Error base64'ing bundled metrics.\n"); |
|---|
| 82 |
free(b64buff); |
|---|
| 83 |
return -1; |
|---|
| 84 |
} |
|---|
| 85 |
*buf_out = b64buff; |
|---|
| 86 |
*len_out = (unsigned int)dlen; |
|---|
| 87 |
return 0; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
int |
|---|
| 91 |
noit_check_log_bundle_decompress_b64(noit_compression_type_t ctype, |
|---|
| 92 |
const char *buf_in, |
|---|
| 93 |
unsigned int len_in, |
|---|
| 94 |
char *buf_out, |
|---|
| 95 |
unsigned int len_out) { |
|---|
| 96 |
int rv; |
|---|
| 97 |
uLong initial_dlen, dlen, rawlen; |
|---|
| 98 |
char *compbuff, *rawbuff; |
|---|
| 99 |
|
|---|
| 100 |
/* Decode */ |
|---|
| 101 |
initial_dlen = (((len_in / 4) * 3) - 2); |
|---|
| 102 |
compbuff = malloc(initial_dlen); |
|---|
| 103 |
if (!compbuff) return -1; |
|---|
| 104 |
dlen = noit_b64_decode((char *)buf_in, len_in, |
|---|
| 105 |
(unsigned char *)compbuff, initial_dlen); |
|---|
| 106 |
if(dlen == 0) { |
|---|
| 107 |
noitL(noit_error, "Error base64'ing bundled metrics.\n"); |
|---|
| 108 |
free(compbuff); |
|---|
| 109 |
return -1; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
switch(ctype) { |
|---|
| 113 |
case NOIT_COMPRESS_ZLIB: |
|---|
| 114 |
/* Decompress */ |
|---|
| 115 |
rawlen = len_out; |
|---|
| 116 |
if(Z_OK != (rv = uncompress((Bytef *)buf_out, &rawlen, |
|---|
| 117 |
(Bytef *)compbuff, dlen)) || |
|---|
| 118 |
rawlen != len_out) { |
|---|
| 119 |
noitL(noit_error, "Error decompressing bundle: %d (%u != %u).\n", |
|---|
| 120 |
rv, (unsigned int)rawlen, (unsigned int)len_out); |
|---|
| 121 |
free(compbuff); |
|---|
| 122 |
return -1; |
|---|
| 123 |
} |
|---|
| 124 |
break; |
|---|
| 125 |
case NOIT_COMPRESS_NONE: |
|---|
| 126 |
// Or don't |
|---|
| 127 |
rawlen = (uLong)dlen; |
|---|
| 128 |
rawbuff = compbuff; |
|---|
| 129 |
if(rawlen != len_out) return -1; |
|---|
| 130 |
memcpy(buf_out, rawbuff, rawlen); |
|---|
| 131 |
break; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
return 0; |
|---|
| 135 |
} |
|---|