Last active
April 19, 2017 17:24
-
-
Save edp1096/a791c659b0da4473c4139a9297f77dab to your computer and use it in GitHub Desktop.
A simple example code using LZ4.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* LZ4 Study. | |
* (c) Robert Sung wook Shin All rights reserved. | |
* http://enjoytools.net, [email protected] | |
* | |
*/ | |
#include "stdafx.h" // empty except '#pragma once' | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include "lz4.c" | |
#define ENCODED_HEADER "\tLZ_ENJOYTOOLS\t" | |
#define ENCODED_HEADER_LEN 15 | |
int main(int argc, char* argv[]) { | |
FILE *fp_in = NULL; | |
FILE *fp_out = NULL; | |
size_t B = 0; | |
char buf[ENCODED_HEADER_LEN + 1] = { 0, }; | |
char *result_buffer; | |
char *result; | |
size_t buf_len; | |
uint32_t result_len; | |
size_t extract_len; | |
char compress_filename[256]; | |
char decompress_filename[256]; | |
char password[256] = "This is a sample."; | |
/* Compress */ | |
// 파일 준비 | |
if (argc != 2) { | |
fprintf(stderr, "Usage: filename.\n"); | |
exit(0); | |
} | |
fp_in = fopen((char *)argv[1], "rb"); | |
if (fp_in == NULL) { | |
fprintf(stderr, "File not found(%s)\n", argv[1]); | |
exit(0); | |
} | |
sprintf(compress_filename, "%s.enc_tmp", argv[1]); | |
fp_out = fopen(compress_filename, "wb"); | |
if (fp_out == NULL) { | |
exit(0); | |
} | |
fseek(fp_in, 0, SEEK_END); | |
buf_len = ftell(fp_in); | |
rewind(fp_in); | |
if (buf_len < 20) { | |
printf("Data is small.(must be > 20)\n"); | |
fclose(fp_in); | |
fclose(fp_out); | |
exit(0); | |
} | |
result_buffer = (char*)malloc(buf_len + 1); | |
memset(result_buffer, 0, sizeof(char)*buf_len); | |
result = (char*)malloc(buf_len + 1); | |
memset(result, 0, sizeof(char)*buf_len); | |
B = fread(result_buffer, (size_t)sizeof(char), (size_t)(buf_len), fp_in); | |
//printf("data: %s/\n", result_buffer); | |
result_len = LZ4_compress_default(result_buffer, result, (int)buf_len, LZ4_compressBound((int)buf_len)); | |
fwrite(ENCODED_HEADER, ENCODED_HEADER_LEN, 1, fp_out); | |
fwrite(&buf_len, sizeof(size_t), 1, fp_out); | |
fwrite(result, result_len, 1, fp_out); | |
printf("plain size: %d, %x\n", (uint32_t*)buf_len, (uint32_t*)buf_len); | |
fclose(fp_in); | |
fclose(fp_out); | |
free(result); | |
free(result_buffer); | |
/* Decompress */ | |
result_len = 0; | |
fp_in = fopen(compress_filename, "rb"); | |
if (fp_in == NULL) { | |
fprintf(stderr, "File not found(%s)\n", argv[1]); | |
exit(0); | |
} | |
sprintf(decompress_filename, "%s.dec_tmp", argv[1]); | |
fp_out = fopen(decompress_filename, "wb"); | |
if (fp_out == NULL) { | |
exit(0); | |
} | |
fseek(fp_in, 0, SEEK_END); | |
buf_len = ftell(fp_in) - (ENCODED_HEADER_LEN + sizeof(size_t)); // 헤더, plain size 값 제외 | |
rewind(fp_in); | |
result_buffer = (char*)malloc(buf_len+sizeof(size_t)); | |
memset(result_buffer, 0, sizeof(char)*(buf_len+sizeof(size_t))); | |
extract_len = (size_t)malloc(sizeof(size_t)); | |
memset(&extract_len, 0, sizeof(uint32_t)); | |
fread(buf, ENCODED_HEADER_LEN, 1, fp_in); | |
B = fread(result_buffer, (size_t)sizeof(char), (size_t)(buf_len+8), fp_in); // 8바이트 늘려야 된다. 이유는 모르겠다. | |
memcpy(&extract_len, result_buffer, sizeof(size_t)); | |
printf("compressed size: %d, %x\n", (uint32_t*)buf_len, (uint32_t*)buf_len); | |
printf("written (extract) size: %d, %x\n", extract_len, extract_len); | |
result = (char*)malloc(extract_len); | |
memset(result, 0, sizeof(char)*extract_len); | |
result_len = LZ4_decompress_safe(result_buffer+sizeof(size_t), result, (int)buf_len, (int)extract_len); | |
printf("decompressed size: %d, %x\n", result_len, result_len); | |
fwrite(result, result_len, 1, fp_out); | |
fclose(fp_in); | |
fclose(fp_out); | |
extract_len = 0; | |
free(result); | |
free(result_buffer); | |
free((void*)extract_len); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LZ4 example 보다가 이해가 안되서 해 봄.
작성 시점 기준 LZ4 버전은 1.7.5
압축 해제시 파일 뒤쪽 8byte를 짤라먹는다. - 일단, 해결
약 15자 이내로 들어가니까 오류 발생
압축 풀 때 fread로 압축파일을 직접 열지 않고, 다른 무언가에 붙여서 굴리려면 압축파일 처음에 붙이는 원본 파일의 용량 정보를 먼저 제거한 다음에 압축된 내용만을 전달해야 됨.
원본 파일의 용량 정보에 널문자가 들어가기 때문에 문자열 짤림.