| | |
| | | free(compr); |
| | | |
| | | ts_uint header[4]={1, data_len, data_len, number_of_compressed_bytes}; |
| | | fprintf(stderr,"Header before encoding: %d, %d, %d, %d\n", header[0],header[1],header[2], header[3]); |
| | | char *encoded_header=(char *)base64_encode((unsigned char *)header, 4*sizeof(ts_uint), &nbase2); |
| | | char *return_value=malloc((nbase1+nbase2+1)*sizeof(char)); |
| | | strncpy(return_value,encoded_header,nbase2); |
| | |
| | | free(encoded_header); |
| | | |
| | | //test decoding and decompression |
| | | char *test_data = ts_decompress((unsigned char *)return_value, nbase1+nbase2, data_len); |
| | | free(test_data); |
| | | //char *test_data = ts_decompress((unsigned char *)return_value, nbase1+nbase2, data_len); |
| | | //for(int i=0;i<data_len;i++){ |
| | | // if(data[i]!=test_data[i]){ |
| | | // fprintf(stderr,"(DE)COMPRESSION ERROR!\n"); |
| | | // } |
| | | //} |
| | | //free(test_data); |
| | | return return_value; |
| | | } |
| | | |
| | | char *ts_decompress(unsigned char *compressed_data, unsigned int data_len, unsigned int result_len){ |
| | | char *ts_decompress(unsigned char *compressed_data, unsigned int data_len, size_t *decompressed_length){ |
| | | const size_t encoded_header_len=24; |
| | | ts_uint *header; |
| | | size_t nbase1=0,nbase2=0; |
| | | header=(ts_uint *)base64_decode((const char *)compressed_data, encoded_header_len, &nbase2); |
| | | if(header==NULL) fprintf(stderr, "Error decoding header\n"); |
| | | fprintf(stderr,"Header=%d, %d, %d, %d\n", header[0],header[1], header[2], header[3]); |
| | | //fprintf(stderr,"Header=%d, %d, %d, %d\n", header[0],header[1], header[2], header[3]); |
| | | unsigned long result_len=header[2]; |
| | | unsigned char *decoded_data=base64_decode((const char *)&compressed_data[encoded_header_len], data_len-encoded_header_len, &nbase1); |
| | | if(decoded_data==NULL) fprintf(stderr, "Error decoding compressed data\n"); |
| | | unsigned char *return_value=(unsigned char *)malloc(result_len*sizeof(char)); |
| | | uncompress(return_value, (unsigned long *)&result_len, decoded_data, header[3]); |
| | | // fprintf(stderr,"RESULT_LEN=%d\n",result_len); |
| | | uncompress(return_value, &result_len, decoded_data, header[3]); |
| | | *decompressed_length=result_len; |
| | | free(decoded_data); |
| | | free(header); |
| | | return (char *)return_value; |