ea-jdlz/main-compress.c
2024-10-22 18:15:18 +02:00

177 lines
4.4 KiB
C

/*
MIT License
Copyright (c) 2024
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include "compress.c"
// This is kinda slow
unsigned char * read_stdin(size_t * length) {
unsigned char* buffer;
unsigned char* tmp;
size_t realsize = 0;
size_t bufsize = 0;
int c;
while ((c = getchar()) != EOF) {
realsize += 1;
if (realsize > bufsize) {
bufsize += 4096;
tmp = realloc(buffer, bufsize);
if (!tmp) { // Check if realloc failed
free(buffer);
return NULL;
}
buffer = tmp;
}
buffer[realsize - 1] = (unsigned char)c;
}
if (realsize == 0) {
return NULL;
}
tmp = realloc(buffer, realsize);
if (!tmp) { // Check if realloc failed
free(buffer);
return NULL;
}
buffer = tmp;
*length = realsize;
return buffer;
}
unsigned char * read_file(char * path,size_t * length) {
int fd = open(path,O_RDONLY);
if (fd == -1) { goto fail; }
size_t size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
unsigned char * data = malloc(size);
if (data == NULL) { goto fail_file; }
if (read(fd,data,size) != size) { goto fail_data; }
close(fd);
*length = size;
return data;
fail_data:;
free(data);
fail_file:;
close(fd);
fail:;
return NULL;
}
int file_create_from_data(char * path,unsigned char * data,size_t length) {
int fd = open(path,O_CREAT | O_WRONLY | O_TRUNC,0644);
if (fd == -1) { goto fail; }
if (write(fd, data, length) != length) { goto fail_file; }
close(fd);
return 0;
fail_file:;
close(fd);
fail:;
return -1;
}
void write_little_endian(uint32_t nr, unsigned char * out, size_t bt) {
for(int i = 0; i < bt; i++) {
out[i] = (nr & 0xFF);
nr >>= 8;
}
}
void write_big_endian(uint32_t nr, unsigned char * out, size_t bt) {
for(int i = 0; i < bt; i++) {
out[i] = ((nr >> (8 * (bt - 1))) & 0xFF);
nr <<= 8;
}
}
int main(int argc, char* argv[]) {
if (argc != 3) {
fprintf(stderr,"Syntax: %s <input> <output>\n",argv[0]);
fprintf(stderr,"You may use \"-\" for stdin/stdout\n");
return 1;
}
// INPUT
char * input_data;
size_t input_length;
if (strcmp(argv[1],"-") == 0) { // stdin
input_data = read_stdin(&input_length);
if (input_data == NULL) {
goto fail_mem;
}
} else { // file
input_data = read_file(argv[1],&input_length);
if (input_data == NULL) {
goto fail_mem;
}
}
// OUTPUT
unsigned char * output_data = malloc(16 + (input_length * 2));
if (output_data == NULL) {
goto fail_mem;
}
int output_length = ea_jdlz_compress(input_data,output_data + 16,input_length);
const char HEADER[] = "JDLZ\x02\x10\x00\x00";
uint32_t header_unc_length = input_length;
uint32_t header_cmp_length = output_length;
memcpy(output_data, HEADER, 8);
write_little_endian(header_unc_length,output_data + 8,4);
write_little_endian(header_cmp_length,output_data + 12,4);
//memcpy(output_data + 8, &header_unc_length,4);
//memcpy(output_data + 12, &header_cmp_length,4);
if (strcmp(argv[2],"-") == 0) {
if (write(STDOUT_FILENO, output_data, output_length + 16) != output_length + 16) {
goto fail_file;
}
} else {
if (file_create_from_data(argv[2], output_data, output_length + 16) == -1) {
goto fail_file;
}
}
return 0;
fail_mem:;
fprintf(stderr,"Could not allocate memory: %s\n",strerror(errno));
return 1;
fail_file:;
fprintf(stderr,"Creating file failed: %s\n",strerror(errno));
return 2;
}