commit | author | age
|
854cb6
|
1 |
#include<stdio.h> |
SP |
2 |
#include<general.h> |
|
3 |
#include<snapshot.h> |
22721d
|
4 |
#include<stdlib.h> |
SP |
5 |
#include<stdarg.h> |
|
6 |
#include <zlib.h> |
f0bcea
|
7 |
#include<inttypes.h> |
11fef8
|
8 |
#include<config.h> |
2e8c7f
|
9 |
#include <time.h> |
f0bcea
|
10 |
|
22721d
|
11 |
ts_uint ts_sprintf(ts_string *str, char *fmt, ...){ |
SP |
12 |
va_list ap; |
|
13 |
va_start(ap,fmt); |
|
14 |
ts_uint n=vsprintf(&(str->string[str->beg]),fmt,ap); |
|
15 |
va_end(ap); |
|
16 |
str->beg+=n; |
|
17 |
return n; |
|
18 |
} |
|
19 |
|
|
20 |
|
854cb6
|
21 |
ts_bool xml_trisurf_data(FILE *fh, ts_vesicle *vesicle){ |
22721d
|
22 |
|
SP |
23 |
ts_string *data=(ts_string *)malloc(sizeof(ts_sprintf)); |
|
24 |
data->string=(char *)malloc(512000*sizeof(char)); /*TODO: warning, can break if the string is to long */ |
|
25 |
data->beg=0; |
|
26 |
|
854cb6
|
27 |
xml_trisurf_header(fh, vesicle); |
22721d
|
28 |
xml_trisurf_tria(data,vesicle->tlist); |
SP |
29 |
xml_trisurf_tria_neigh(data,vesicle->tlist); |
|
30 |
xml_trisurf_vtx_neigh(data,vesicle->vlist); |
|
31 |
xml_trisurf_vtx_tristar(data,vesicle->vlist); |
|
32 |
#ifdef COMPRESSION |
f0bcea
|
33 |
char *compressed; |
588bbb
|
34 |
ts_uint nbytes=ts_compress_string64(data->string, data->beg-1, &compressed); //suppress null character at the end with by substracting 1 |
f0bcea
|
35 |
fwrite(compressed, sizeof(unsigned char), nbytes, fh); |
SP |
36 |
free (compressed); |
22721d
|
37 |
#else |
SP |
38 |
fprintf(fh,"%s", data->string); |
|
39 |
#endif |
|
40 |
free(data->string); |
|
41 |
free(data); |
854cb6
|
42 |
xml_trisurf_footer(fh); |
SP |
43 |
return TS_SUCCESS; |
|
44 |
} |
|
45 |
|
|
46 |
ts_bool xml_trisurf_header(FILE *fh, ts_vesicle *vesicle){ |
2e8c7f
|
47 |
/* format current time */ |
SP |
48 |
time_t current_time; |
|
49 |
char *c_time_string; |
|
50 |
current_time = time(NULL); |
|
51 |
c_time_string = ctime(¤t_time); |
e29756
|
52 |
|
SP |
53 |
fprintf(fh, "<trisurfversion>Trisurf (commit %s), compiled on %s %s</trisurfversion>\n",TS_VERSION, __DATE__, __TIME__); |
2e8c7f
|
54 |
fprintf(fh, "<dumpdate>%s</dumpdate>\n", c_time_string); |
SP |
55 |
//free (c_time_string); |
|
56 |
|
f0bcea
|
57 |
fprintf(fh, "<tape>\n"); |
SP |
58 |
|
|
59 |
fprintf(fh, "</tape>\n"); |
854cb6
|
60 |
fprintf(fh, "<trisurf nvtx=\"%u\" npoly=\"%u\" nfono=\"%u\">\n", vesicle->vlist->n, vesicle->poly_list->n, vesicle->poly_list->poly[0]->vlist->n); |
SP |
61 |
return TS_SUCCESS; |
|
62 |
} |
|
63 |
|
|
64 |
ts_bool xml_trisurf_footer(FILE *fh){ |
|
65 |
fprintf(fh, "</trisurf>\n"); |
|
66 |
return TS_SUCCESS; |
|
67 |
} |
|
68 |
|
22721d
|
69 |
ts_bool xml_trisurf_tria(ts_string *data, ts_triangle_list *tlist){ |
854cb6
|
70 |
ts_uint i; |
588bbb
|
71 |
ts_sprintf(data,"<tria>"); |
854cb6
|
72 |
for(i=0; i<tlist->n;i++){ |
588bbb
|
73 |
ts_sprintf(data,"%u %u %u",tlist->tria[i]->vertex[0]->idx, tlist->tria[i]->vertex[1]->idx, tlist->tria[i]->vertex[2]->idx); |
854cb6
|
74 |
} |
588bbb
|
75 |
ts_sprintf(data,"</tria>"); |
854cb6
|
76 |
return TS_SUCCESS; |
SP |
77 |
} |
|
78 |
|
22721d
|
79 |
ts_bool xml_trisurf_tria_neigh(ts_string *data, ts_triangle_list *tlist){ |
854cb6
|
80 |
ts_uint i; |
22721d
|
81 |
ts_sprintf(data,"<trianeigh>\n"); |
854cb6
|
82 |
for(i=0; i<tlist->n;i++){ |
588bbb
|
83 |
ts_sprintf(data,"%u %u %u",tlist->tria[i]->neigh[0]->idx, tlist->tria[i]->neigh[1]->idx, tlist->tria[i]->neigh[2]->idx); |
854cb6
|
84 |
} |
22721d
|
85 |
ts_sprintf(data,"</trianeigh>\n"); |
854cb6
|
86 |
return TS_SUCCESS; |
SP |
87 |
} |
|
88 |
|
22721d
|
89 |
ts_bool xml_trisurf_vtx_neigh(ts_string *data, ts_vertex_list *vlist){ |
854cb6
|
90 |
ts_uint i,j; |
SP |
91 |
for(i=0;i<vlist->n;i++){ |
22721d
|
92 |
ts_sprintf(data,"<vtxn idx=\"%u\">",vlist->vtx[i]->idx); |
854cb6
|
93 |
for(j=0;j<vlist->vtx[i]->neigh_no;j++){ |
22721d
|
94 |
ts_sprintf(data,"%u ",vlist->vtx[i]->neigh[j]->idx); |
854cb6
|
95 |
} |
588bbb
|
96 |
ts_sprintf(data, "</vtxn>"); |
854cb6
|
97 |
} |
SP |
98 |
return TS_SUCCESS; |
|
99 |
} |
|
100 |
|
22721d
|
101 |
ts_bool xml_trisurf_vtx_tristar(ts_string *data, ts_vertex_list *vlist){ |
854cb6
|
102 |
ts_uint i,j; |
SP |
103 |
for(i=0;i<vlist->n;i++){ |
22721d
|
104 |
ts_sprintf(data,"<tristar idx=\"%u\">",vlist->vtx[i]->idx); |
854cb6
|
105 |
for(j=0;j<vlist->vtx[i]->tristar_no;j++){ |
22721d
|
106 |
ts_sprintf(data,"%u ",vlist->vtx[i]->tristar[j]->idx); |
854cb6
|
107 |
} |
588bbb
|
108 |
ts_sprintf(data, "</tristar>"); |
854cb6
|
109 |
} |
SP |
110 |
return TS_SUCCESS; |
|
111 |
} |
|
112 |
|
f0bcea
|
113 |
|
SP |
114 |
|
|
115 |
|
|
116 |
/* UTILITIES */ |
|
117 |
|
|
118 |
/* zlib compression base64 encoded */ |
|
119 |
/* compressed must not be pre-malloced */ |
8c1bb1
|
120 |
/* taken from https://gist.github.com/arq5x/5315739 */ |
f0bcea
|
121 |
ts_uint ts_compress_string64(char *data, ts_uint data_len, char **compressed){ |
SP |
122 |
z_stream defstream; |
|
123 |
defstream.zalloc = Z_NULL; |
|
124 |
defstream.zfree = Z_NULL; |
|
125 |
defstream.opaque = Z_NULL; |
|
126 |
defstream.avail_in = data_len+1; |
|
127 |
defstream.next_in = (unsigned char *)data; |
|
128 |
char *compr=(char *)malloc(data_len*sizeof(char *)); |
|
129 |
defstream.avail_out = data_len+1; |
|
130 |
defstream.next_out = (unsigned char *)compr; |
|
131 |
deflateInit(&defstream, Z_BEST_COMPRESSION); |
|
132 |
deflate(&defstream, Z_FINISH); |
|
133 |
deflateEnd(&defstream); |
|
134 |
/*base64 encode*/ |
|
135 |
size_t nbase; |
|
136 |
*compressed=base64_encode((unsigned char *)compr,(size_t)defstream.total_out,&nbase); |
|
137 |
//fwrite(base64, sizeof(unsigned char), nbase, fh); |
|
138 |
free(compr); |
|
139 |
return nbase; |
|
140 |
} |
|
141 |
|
8c1bb1
|
142 |
ts_uint ts_decompress_string64(char *b64, ts_uint data_len, char **decompressed){ |
SP |
143 |
return TS_SUCCESS; |
|
144 |
|
|
145 |
} |
f0bcea
|
146 |
|
SP |
147 |
/* base64 encoding, taken from http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c */ |
|
148 |
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
|
149 |
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
|
150 |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
|
151 |
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
|
152 |
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
|
153 |
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
|
154 |
'w', 'x', 'y', 'z', '0', '1', '2', '3', |
|
155 |
'4', '5', '6', '7', '8', '9', '+', '/'}; |
|
156 |
static char *decoding_table = NULL; |
|
157 |
static int mod_table[] = {0, 2, 1}; |
|
158 |
|
|
159 |
|
|
160 |
char *base64_encode(const unsigned char *data, |
|
161 |
size_t input_length, |
|
162 |
size_t *output_length) { |
|
163 |
|
|
164 |
*output_length = 4 * ((input_length + 2) / 3); |
|
165 |
int i,j; |
|
166 |
char *encoded_data = malloc(*output_length); |
|
167 |
if (encoded_data == NULL) return NULL; |
|
168 |
|
|
169 |
for (i = 0, j = 0; i < input_length;) { |
|
170 |
|
|
171 |
uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0; |
|
172 |
uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0; |
|
173 |
uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0; |
|
174 |
|
|
175 |
uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; |
|
176 |
|
|
177 |
encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F]; |
|
178 |
encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F]; |
|
179 |
encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F]; |
|
180 |
encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F]; |
|
181 |
} |
|
182 |
|
|
183 |
for (i = 0; i < mod_table[input_length % 3]; i++) |
|
184 |
encoded_data[*output_length - 1 - i] = '='; |
|
185 |
|
|
186 |
return encoded_data; |
|
187 |
} |
|
188 |
|
|
189 |
|
|
190 |
unsigned char *base64_decode(const char *data, |
|
191 |
size_t input_length, |
|
192 |
size_t *output_length) { |
|
193 |
|
|
194 |
int i,j; |
|
195 |
if (decoding_table == NULL) build_decoding_table(); |
|
196 |
|
|
197 |
if (input_length % 4 != 0) return NULL; |
|
198 |
|
|
199 |
*output_length = input_length / 4 * 3; |
|
200 |
if (data[input_length - 1] == '=') (*output_length)--; |
|
201 |
if (data[input_length - 2] == '=') (*output_length)--; |
|
202 |
|
|
203 |
unsigned char *decoded_data = malloc(*output_length); |
|
204 |
if (decoded_data == NULL) return NULL; |
|
205 |
|
|
206 |
for (i = 0, j = 0; i < input_length;) { |
|
207 |
|
|
208 |
uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]]; |
|
209 |
uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]]; |
|
210 |
uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]]; |
|
211 |
uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]]; |
|
212 |
|
|
213 |
uint32_t triple = (sextet_a << 3 * 6) |
|
214 |
+ (sextet_b << 2 * 6) |
|
215 |
+ (sextet_c << 1 * 6) |
|
216 |
+ (sextet_d << 0 * 6); |
|
217 |
|
|
218 |
if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF; |
|
219 |
if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF; |
|
220 |
if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF; |
|
221 |
} |
8c1bb1
|
222 |
if(decoding_table !=NULL) free(decoding_table); |
f0bcea
|
223 |
return decoded_data; |
SP |
224 |
} |
|
225 |
|
|
226 |
|
|
227 |
void build_decoding_table() { |
|
228 |
|
|
229 |
decoding_table = malloc(256); |
|
230 |
int i; |
|
231 |
for (i = 0; i < 64; i++) |
|
232 |
decoding_table[(unsigned char) encoding_table[i]] = i; |
|
233 |
} |
|
234 |
|
|
235 |
|
|
236 |
void base64_cleanup() { |
|
237 |
free(decoding_table); |
|
238 |
} |
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|