sk1
This commit is contained in:
parent
6c4d7d0fef
commit
f191d7b7b4
@ -88,7 +88,7 @@ unsigned char* decompress_1_buffer(const unsigned char* data, size_t size, size_
|
||||
int decompress_1(const char* input_file_name, const char* output_file_name) {
|
||||
size_t size;
|
||||
unsigned char* data;
|
||||
if (read_file(input_file_name, &data, &size) != 0) return -1;
|
||||
if (read_file(output_file_name, &data, &size) != 0) return -1;
|
||||
|
||||
size_t out_size;
|
||||
unsigned char* decompressed = decompress_1_buffer(data, size, &out_size);
|
||||
@ -96,7 +96,7 @@ int decompress_1(const char* input_file_name, const char* output_file_name) {
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
int result = write_file(output_file_name, decompressed, out_size);
|
||||
int result = write_file(input_file_name, decompressed, out_size);
|
||||
free(data);
|
||||
free(decompressed);
|
||||
return result == 0 ? (int)out_size : -1;
|
||||
@ -109,17 +109,21 @@ int compress_2(const char* input_file_name, const char* output_file_name) {
|
||||
if (read_file(input_file_name, &data, &size) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE* output = fopen(output_file_name, "w");
|
||||
if (!output) {
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char* dictionary[MAX_DICT_SIZE] = { NULL };
|
||||
size_t dict_size = 0;
|
||||
|
||||
size_t current_index = 0;
|
||||
while (current_index < size) {
|
||||
size_t match_index = 0;
|
||||
size_t match_length = 0;
|
||||
|
||||
for (size_t i = 0; i < dict_size; i++) {
|
||||
size_t len = 0;
|
||||
while (data[current_index + len] != '\0' &&
|
||||
@ -132,9 +136,11 @@ int compress_2(const char* input_file_name, const char* output_file_name) {
|
||||
match_index = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_index + match_length < size) {
|
||||
fprintf(output, "%zu%c", match_index, data[current_index + match_length]);
|
||||
}
|
||||
|
||||
if (dict_size < MAX_DICT_SIZE) {
|
||||
dictionary[dict_size] = malloc(match_length + 2);
|
||||
if (!dictionary[dict_size]) {
|
||||
@ -152,6 +158,7 @@ int compress_2(const char* input_file_name, const char* output_file_name) {
|
||||
dictionary[dict_size][match_length + 1] = '\0';
|
||||
dict_size++;
|
||||
}
|
||||
|
||||
current_index += match_length + 1;
|
||||
}
|
||||
|
||||
@ -159,40 +166,47 @@ int compress_2(const char* input_file_name, const char* output_file_name) {
|
||||
free(data);
|
||||
for (size_t i = 0; i < dict_size; i++) {
|
||||
free(dictionary[i]);
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int decompress_2(const char* input_file_name, const char* output_file_name) {
|
||||
unsigned char* data;
|
||||
size_t size;
|
||||
if (read_file(input_file_name, &data, &size) != 0) {
|
||||
if (read_file(output_file_name, &data, &size) != 0) {
|
||||
return -1;
|
||||
}
|
||||
FILE* output = fopen(output_file_name, "w");
|
||||
|
||||
FILE* output = fopen(input_file_name, "w");
|
||||
if (!output) {
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char* dictionary[MAX_DICT_SIZE] = { NULL };
|
||||
size_t dict_size = 0;
|
||||
|
||||
size_t current_index = 0;
|
||||
while (current_index < size) {
|
||||
unsigned short index = 0;
|
||||
unsigned char ch = '\0';
|
||||
|
||||
while (current_index < size && isdigit(data[current_index])) {
|
||||
index = index * 10 + (data[current_index] - '0');
|
||||
current_index++;
|
||||
}
|
||||
|
||||
if (current_index < size) {
|
||||
ch = data[current_index++];
|
||||
}
|
||||
|
||||
if (index == 0) {
|
||||
fputc(ch, output);
|
||||
} else {
|
||||
fwrite(dictionary[index - 1], 1, strlen((char*)dictionary[index - 1]), output);
|
||||
fputc(ch, output);
|
||||
}
|
||||
|
||||
if (dict_size < MAX_DICT_SIZE) {
|
||||
size_t length = (index > 0) ? strlen((char*)dictionary[index - 1]) : 0;
|
||||
dictionary[dict_size] = malloc(length + 2);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user