commit | author | age
|
dba66b
|
1 |
#include<stdio.h> |
SP |
2 |
#include<stdlib.h> |
|
3 |
#include<general.h> |
|
4 |
#include<string.h> |
|
5 |
#include<zlib.h> |
|
6 |
#include<inttypes.h> |
|
7 |
#include<b64zlib_compression.h> |
|
8 |
|
|
9 |
|
|
10 |
/* zlib compression base64 encoded */ |
|
11 |
/* compressed must not be pre-malloced */ |
|
12 |
/* taken from https://gist.github.com/arq5x/5315739 */ |
|
13 |
/* This function is not to be used in compressing paraview data. See below. */ |
|
14 |
ts_uint ts_compress_data(char *data, ts_uint data_len, char **compressed){ |
|
15 |
z_stream defstream; |
|
16 |
defstream.zalloc = Z_NULL; |
|
17 |
defstream.zfree = Z_NULL; |
|
18 |
defstream.opaque = Z_NULL; |
|
19 |
defstream.avail_in = data_len+1; |
|
20 |
defstream.next_in = (unsigned char *)data; |
|
21 |
char *compr=(char *)malloc(data_len*sizeof(char)); |
|
22 |
defstream.avail_out = data_len+1; |
|
23 |
defstream.next_out = (unsigned char *)compr; |
|
24 |
deflateInit(&defstream, 6); |
|
25 |
// deflateInit(&defstream, Z_BEST_COMPRESSION); |
|
26 |
deflate(&defstream, Z_FINISH); |
|
27 |
deflateEnd(&defstream); |
|
28 |
*compressed=compr; |
|
29 |
return defstream.total_out; |
|
30 |
} |
|
31 |
|
|
32 |
ts_uint ts_compress_string64(char *data, ts_uint data_len, char **encoded_compressed){ |
|
33 |
size_t nbase; |
|
34 |
char *compr; |
|
35 |
size_t number_of_compressed_bytes=ts_compress_data(data, data_len, &compr); |
|
36 |
*encoded_compressed=base64_encode((unsigned char *)compr,number_of_compressed_bytes,&nbase); |
|
37 |
free(compr); |
|
38 |
return nbase; |
|
39 |
} |
|
40 |
|
|
41 |
char *ts_compress(char *data, ts_uint data_len){ |
|
42 |
size_t nbase1, nbase2; |
|
43 |
unsigned char *compr=(unsigned char *)malloc(data_len); |
|
44 |
size_t number_of_compressed_bytes=data_len; |
|
45 |
|
|
46 |
compress(compr,&number_of_compressed_bytes, (unsigned char *)data, data_len); |
|
47 |
// printf("Compressdion error code=%d, Z_OK=%d, Z_BUF_ERROR=%d", errcode, Z_OK, Z_BUF_ERROR); |
|
48 |
|
|
49 |
char *encoded_compressed=base64_encode((unsigned char *)compr,number_of_compressed_bytes,&nbase1); |
|
50 |
|
|
51 |
free(compr); |
|
52 |
|
|
53 |
ts_uint header[4]={1, data_len, data_len, number_of_compressed_bytes}; |
|
54 |
|
|
55 |
char *encoded_header=(char *)base64_encode((unsigned char *)header, 4*sizeof(ts_uint), &nbase2); |
|
56 |
char *return_value=malloc((nbase1+nbase2+1)*sizeof(char)); |
|
57 |
strncpy(return_value,encoded_header,nbase2); |
|
58 |
strncpy(return_value+nbase2,encoded_compressed,nbase1); |
|
59 |
*(return_value+nbase1+nbase2)=0; |
|
60 |
|
|
61 |
// printf("Compressed size in bytes= %ld, size of encoded header= %ld, size of encoded compressed= %ld.\n",number_of_compressed_bytes, nbase2, nbase1); |
|
62 |
free(encoded_compressed); |
|
63 |
free(encoded_header); |
|
64 |
return return_value; |
|
65 |
} |
|
66 |
|
|
67 |
ts_uint ts_decompress_string64(char *b64, ts_uint data_len, char **decompressed){ |
|
68 |
return TS_SUCCESS; |
|
69 |
|
|
70 |
} |
|
71 |
|
|
72 |
/* base64 encoding, taken from http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c */ |
|
73 |
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
|
74 |
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
|
75 |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
|
76 |
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
|
77 |
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
|
78 |
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
|
79 |
'w', 'x', 'y', 'z', '0', '1', '2', '3', |
|
80 |
'4', '5', '6', '7', '8', '9', '+', '/'}; |
|
81 |
static char *decoding_table = NULL; |
|
82 |
static int mod_table[] = {0, 2, 1}; |
|
83 |
|
|
84 |
|
|
85 |
char *base64_encode(const unsigned char *data, size_t input_length, size_t *output_length) { |
|
86 |
*output_length = 4 * ((input_length + 2) / 3); |
|
87 |
int i,j; |
|
88 |
char *encoded_data = malloc(*output_length); |
|
89 |
if (encoded_data == NULL) return NULL; |
|
90 |
|
|
91 |
for (i = 0, j = 0; i < input_length;) { |
|
92 |
|
|
93 |
uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0; |
|
94 |
uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0; |
|
95 |
uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0; |
|
96 |
uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; |
|
97 |
|
|
98 |
encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F]; |
|
99 |
encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F]; |
|
100 |
encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F]; |
|
101 |
encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F]; |
|
102 |
} |
|
103 |
|
|
104 |
for (i = 0; i < mod_table[input_length % 3]; i++) |
|
105 |
encoded_data[*output_length - 1 - i] = '='; |
|
106 |
|
|
107 |
return encoded_data; |
|
108 |
} |
|
109 |
|
|
110 |
|
|
111 |
unsigned char *base64_decode(const char *data, size_t input_length, size_t *output_length) { |
|
112 |
int i,j; |
|
113 |
if (decoding_table == NULL) build_decoding_table(); |
|
114 |
|
|
115 |
if (input_length % 4 != 0) return NULL; |
|
116 |
|
|
117 |
*output_length = input_length / 4 * 3; |
|
118 |
if (data[input_length - 1] == '=') (*output_length)--; |
|
119 |
if (data[input_length - 2] == '=') (*output_length)--; |
|
120 |
|
|
121 |
unsigned char *decoded_data = malloc(*output_length); |
|
122 |
if (decoded_data == NULL) return NULL; |
|
123 |
|
|
124 |
for (i = 0, j = 0; i < input_length;) { |
|
125 |
uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]]; |
|
126 |
uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]]; |
|
127 |
uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]]; |
|
128 |
uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]]; |
|
129 |
|
|
130 |
uint32_t triple = (sextet_a << 3 * 6) |
|
131 |
+ (sextet_b << 2 * 6) |
|
132 |
+ (sextet_c << 1 * 6) |
|
133 |
+ (sextet_d << 0 * 6); |
|
134 |
|
|
135 |
if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF; |
|
136 |
if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF; |
|
137 |
if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF; |
|
138 |
} |
|
139 |
if(decoding_table !=NULL) free(decoding_table); |
|
140 |
return decoded_data; |
|
141 |
} |
|
142 |
|
|
143 |
|
|
144 |
void build_decoding_table() { |
|
145 |
|
|
146 |
decoding_table = malloc(256); |
|
147 |
int i; |
|
148 |
for (i = 0; i < 64; i++) |
|
149 |
decoding_table[(unsigned char) encoding_table[i]] = i; |
|
150 |
} |
|
151 |
|
|
152 |
|
|
153 |
void base64_cleanup() { |
|
154 |
free(decoding_table); |
|
155 |
} |
|
156 |
|
|
157 |
|
|
158 |
|