Fix stdin/stdout
This commit is contained in:
parent
53aa6f211b
commit
31443a2b12
@ -31,40 +31,41 @@ THE SOFTWARE.
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "compress.c"
|
#include "compress.c"
|
||||||
|
|
||||||
// This is kinda slow
|
#ifdef _WIN32
|
||||||
unsigned char * read_stdin(size_t * length) {
|
# include <io.h>
|
||||||
unsigned char* buffer;
|
# include <fcntl.h>
|
||||||
unsigned char* tmp;
|
# define SET_BINARY_MODE(handle) setmode(handle, O_BINARY)
|
||||||
size_t realsize = 0;
|
#else
|
||||||
size_t bufsize = 0;
|
# define SET_BINARY_MODE(handle) ((void)0)
|
||||||
int c;
|
#endif
|
||||||
|
|
||||||
while ((c = getchar()) != EOF) {
|
// Read the entirety of the given file into memory, returning a pointer to
|
||||||
realsize += 1;
|
// said memory. You will need to free the pointer. The size of the data is
|
||||||
if (realsize > bufsize) {
|
// returned in the "outsize" parameter.
|
||||||
bufsize += 4096;
|
#define RACHUNKSIZE 65536
|
||||||
tmp = realloc(buffer, bufsize);
|
unsigned char *read_all(FILE *f, size_t *outsize) {
|
||||||
if (!tmp) { // Check if realloc failed
|
// Initial allocation
|
||||||
free(buffer);
|
*outsize = 0;
|
||||||
|
unsigned char *buffer = malloc(RACHUNKSIZE);
|
||||||
|
unsigned char *tmp;
|
||||||
|
if (buffer == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
// Continue until we're at eof (this works for stdin too)
|
||||||
|
while (!feof(f)) {
|
||||||
|
int readbytes = fread(buffer + *outsize, 1, RACHUNKSIZE, f);
|
||||||
|
*outsize += readbytes;
|
||||||
|
if (readbytes) { // Only realloc if we read something
|
||||||
|
// To make code simpler, always keep a full empty chunk at the end
|
||||||
|
tmp = realloc(buffer, *outsize + RACHUNKSIZE);
|
||||||
|
if (tmp == NULL) {
|
||||||
|
free(buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
buffer = tmp;
|
buffer = tmp;
|
||||||
}
|
}
|
||||||
buffer[realsize - 1] = (unsigned char)c;
|
|
||||||
}
|
}
|
||||||
|
// outsize is already correct at loop exit
|
||||||
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;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,6 +118,8 @@ void write_big_endian(uint32_t nr, unsigned char * out, size_t bt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
SET_BINARY_MODE(STDIN_FILENO);
|
||||||
|
SET_BINARY_MODE(STDOUT_FILENO);
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
fprintf(stderr,"Syntax: %s <input> <output>\n",argv[0]);
|
fprintf(stderr,"Syntax: %s <input> <output>\n",argv[0]);
|
||||||
fprintf(stderr,"You may use \"-\" for stdin/stdout\n");
|
fprintf(stderr,"You may use \"-\" for stdin/stdout\n");
|
||||||
@ -127,7 +130,7 @@ int main(int argc, char* argv[]) {
|
|||||||
char * input_data;
|
char * input_data;
|
||||||
size_t input_length;
|
size_t input_length;
|
||||||
if (strcmp(argv[1],"-") == 0) { // stdin
|
if (strcmp(argv[1],"-") == 0) { // stdin
|
||||||
input_data = read_stdin(&input_length);
|
input_data = read_all(stdin,&input_length);
|
||||||
if (input_data == NULL) {
|
if (input_data == NULL) {
|
||||||
goto fail_mem;
|
goto fail_mem;
|
||||||
}
|
}
|
||||||
@ -156,7 +159,7 @@ int main(int argc, char* argv[]) {
|
|||||||
//memcpy(output_data + 12, &header_cmp_length,4);
|
//memcpy(output_data + 12, &header_cmp_length,4);
|
||||||
|
|
||||||
if (strcmp(argv[2],"-") == 0) {
|
if (strcmp(argv[2],"-") == 0) {
|
||||||
if (write(STDOUT_FILENO, output_data, output_length + 16) != output_length + 16) {
|
if (fwrite(output_data, output_length + 16, 1, stdout) < 1) {
|
||||||
goto fail_file;
|
goto fail_file;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -31,40 +31,41 @@ THE SOFTWARE.
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "decompress.c"
|
#include "decompress.c"
|
||||||
|
|
||||||
// This is kinda slow
|
#ifdef _WIN32
|
||||||
unsigned char * read_stdin(size_t * length) {
|
# include <io.h>
|
||||||
unsigned char* buffer;
|
# include <fcntl.h>
|
||||||
unsigned char* tmp;
|
# define SET_BINARY_MODE(handle) setmode(handle, O_BINARY)
|
||||||
size_t realsize = 0;
|
#else
|
||||||
size_t bufsize = 0;
|
# define SET_BINARY_MODE(handle) ((void)0)
|
||||||
int c;
|
#endif
|
||||||
|
|
||||||
while ((c = getchar()) != EOF) {
|
// Read the entirety of the given file into memory, returning a pointer to
|
||||||
realsize += 1;
|
// said memory. You will need to free the pointer. The size of the data is
|
||||||
if (realsize > bufsize) {
|
// returned in the "outsize" parameter.
|
||||||
bufsize += 4096;
|
#define RACHUNKSIZE 65536
|
||||||
tmp = realloc(buffer, bufsize);
|
unsigned char *read_all(FILE *f, size_t *outsize) {
|
||||||
if (!tmp) { // Check if realloc failed
|
// Initial allocation
|
||||||
free(buffer);
|
*outsize = 0;
|
||||||
|
unsigned char *buffer = malloc(RACHUNKSIZE);
|
||||||
|
unsigned char *tmp;
|
||||||
|
if (buffer == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
// Continue until we're at eof (this works for stdin too)
|
||||||
|
while (!feof(f)) {
|
||||||
|
int readbytes = fread(buffer + *outsize, 1, RACHUNKSIZE, f);
|
||||||
|
*outsize += readbytes;
|
||||||
|
if (readbytes) { // Only realloc if we read something
|
||||||
|
// To make code simpler, always keep a full empty chunk at the end
|
||||||
|
tmp = realloc(buffer, *outsize + RACHUNKSIZE);
|
||||||
|
if (tmp == NULL) {
|
||||||
|
free(buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
buffer = tmp;
|
buffer = tmp;
|
||||||
}
|
}
|
||||||
buffer[realsize - 1] = (unsigned char)c;
|
|
||||||
}
|
}
|
||||||
|
// outsize is already correct at loop exit
|
||||||
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;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,6 +118,8 @@ void write_big_endian(uint32_t nr, unsigned char * out, size_t bt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
SET_BINARY_MODE(STDIN_FILENO);
|
||||||
|
SET_BINARY_MODE(STDOUT_FILENO);
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
fprintf(stderr,"Syntax: %s <input> <output>\n",argv[0]);
|
fprintf(stderr,"Syntax: %s <input> <output>\n",argv[0]);
|
||||||
fprintf(stderr,"You may use \"-\" for stdin/stdout\n");
|
fprintf(stderr,"You may use \"-\" for stdin/stdout\n");
|
||||||
@ -127,7 +130,7 @@ int main(int argc, char* argv[]) {
|
|||||||
unsigned char * input_data;
|
unsigned char * input_data;
|
||||||
size_t input_length;
|
size_t input_length;
|
||||||
if (strcmp(argv[1],"-") == 0) { // stdin
|
if (strcmp(argv[1],"-") == 0) { // stdin
|
||||||
input_data = read_stdin(&input_length);
|
input_data = read_all(stdin,&input_length);
|
||||||
if (input_data == NULL) {
|
if (input_data == NULL) {
|
||||||
goto fail_mem;
|
goto fail_mem;
|
||||||
}
|
}
|
||||||
@ -152,7 +155,7 @@ int main(int argc, char* argv[]) {
|
|||||||
ea_jdlz_decompress(input_data + 16,input_length - 16,output_data,header_unc_length);
|
ea_jdlz_decompress(input_data + 16,input_length - 16,output_data,header_unc_length);
|
||||||
|
|
||||||
if (strcmp(argv[2],"-") == 0) {
|
if (strcmp(argv[2],"-") == 0) {
|
||||||
if (write(STDOUT_FILENO, output_data, header_unc_length) != header_unc_length) {
|
if (fwrite(output_data, header_unc_length, 1, stdout) < 1) {
|
||||||
goto fail_file;
|
goto fail_file;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user