Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
xrle
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CFLAGS := -Wall -Wextra -std=c99 -pedantic -Og -g3

all: xrle
xrle: xrle.o main.o
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)
95 changes: 95 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <stdio.h>
#include <stdlib.h>
#include "xrle.h"

void *read_file(const char *file, size_t *sz)
{
FILE *f;
void *ret = NULL;

f = fopen(file, "rb");
if(f == NULL)
goto out;

fseek(f, 0, SEEK_END);
*sz = ftell(f);
fseek(f, 0, SEEK_SET);

ret = malloc(*sz);
if(ret != NULL)
fread(ret, 1, *sz, f);

fclose(f);

out:
return ret;
}


int main(int argc, char *argv[])
{
const char *file;
char *in, *out;
size_t insz, outsz;

if(argc != 3 || argv[1][0] != '-')
{
fprintf(stderr, "Usage: xrle [-c|-d] file > out\n");
return 1;
}

if(argv[1][1] != 'c' && argv[1][1] != 'd')
{
fprintf(stderr, "Unknown option '%s'\n", argv[1]);
return 1;
}

file = argv[2];
in = read_file(file, &insz);
if(in == NULL)
{
fprintf(stderr, "An error occured\n");
return 1;
}

if(argv[1][1] == 'c')
{
out = malloc(xrle_max_out(insz));
if(out == NULL)
{
fprintf(stderr, "Unable to allocate memory\n");
return 1;
}

outsz = xrle_compress(out, in, insz);
for(size_t i = 0; i < outsz; i++)
putc(out[i], stdout);

fflush(stdout);
free(out);

}
else
{
outsz = xrle_getsz(in);
out = malloc(outsz + 8);
if(out == NULL)
{
fprintf(stderr, "Unable to allocate %lu bytes of memory\n", outsz);
return 1;
}

outsz = xrle_decompress(out, in, insz);
for(size_t i = 0; i < outsz; i++)
putc(out[i], stdout);

fflush(stdout);
free(out);

}

fprintf(stderr, "%lu --> %lu, %f%%\n", insz, outsz, ((float)outsz/(float)insz)*100.0f);
free(in);

return 0;
}
24 changes: 22 additions & 2 deletions xrle.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
*/

/*
* Files created with this tool are not endian agnostic.
*
* Header:
* 32 bits
* +--------------+
* | size of file |
* +--------------+
*
* Format:
* 32 bits 32 bits 64 bits
* +----------------+---------------+----------+----------------+
Expand All @@ -36,15 +44,18 @@
#include <stdint.h>
#include <string.h>

#define U64 uint64_t
#define U32 uint32_t
typedef uint64_t U64;
typedef uint32_t U32;

size_t xrle_compress(void * out,const void * in,size_t in_size)
{
U64 *in_pos = (U64 *) in,*out_pos = (U64 *) out;
U32 tail = in_size & 7,*descr,repeat;
U64 *in_limit = (U64 *)((char *)in + in_size - tail),previous,next;

*out_pos = in_size;
out_pos++;

if(in_size < 16){
memcpy(out,in,in_size);
return in_size;
Expand Down Expand Up @@ -85,13 +96,22 @@ size_t xrle_compress(void * out,const void * in,size_t in_size)
return (char *)out_pos - (char *)out + tail;
}

U32 xrle_getsz(const void * in)
{
U32 expected;
memcpy(&expected, in, sizeof(expected));
return expected;
}

size_t xrle_decompress(void * out,const void * in,size_t in_size)
{

U64 *in_pos = (U64 *) in,*out_pos = (U64 *) out,word,tmp;
U32 tail = in_size & 7,lit_len,repeat,i;
U64 *in_limit = (U64 *)((char *)in + in_size - tail);

in_pos++;

if(in_size < 16){
memcpy(out,in,in_size);
return in_size;
Expand Down
4 changes: 3 additions & 1 deletion xrle.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
size_t xrle_compress(void * out,const void * in,size_t in_size);

size_t xrle_getsz(const void * in);

/*
* in: buffer to decompress
* out: buffer to store decompressed data
Expand All @@ -53,4 +55,4 @@ size_t xrle_compress(void * out,const void * in,size_t in_size);
size_t xrle_decompress(void * out,const void * in,size_t in_size);

/* This macro returns the maximum size of compressed data in bytes */
#define xrle_max_out(A) ((A) + 8)
#define xrle_max_out(A) ((A) + 8 + 4)