Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Latest encoder

Name: FrozenVoid 2011-10-12 10:32


//====Headers
#include <iso646.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <locale.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include <gmp.h>   //using libgmp.a
#include <mpfr.h>
//======Types
#define u1 unsigned char
#define u2 unsigned short
#define u4 unsigned int
#define u8 unsigned long long
//====Bithacks
#define setb(o,p) (o|=(1<<p))      //byte| 1<< pos  
#define clrb(o,p) (o&=(~(1<<p)))  // byte | 11101111
#define togb(o,p) (o^=(1<<p))
#define chkb(o,p) ((o>>p)&1)
#define bitchar(bitchar_bit)    (48|bitchar_bit) // 48|0 or 1
//====Func
static __inline__ u8 rdtsc(void){u8 x;
 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));     return x;}

u8 fsize(u1* filename){
FILE* tfile=fopen(filename,"rb");
if(!tfile){perror(filename);return 0;}
fseek(tfile,0,SEEK_END);
u8 sz=(u8)(ftell(tfile));fclose(tfile);return sz;}

u1* getcontent(u1*filename){//file to buffer
u8 size=fsize(filename);//
if(!size){perror("Zero length content"); return NULL;}
u1* res=malloc(size);
if(!res){perror("Malloc failure");return NULL;}
FILE* inpfile=fopen(filename,"rb");
if(!inpfile){perror("File cannot be read");return NULL;}
u8 findex=(u8)fread(res,1,size,inpfile);fclose(inpfile);
if(findex!=size){perror("size mismatch");return NULL;}
return res;}


void readcontent(u1* filename,u1* storage){
if(!filename){perror("Invalid filename"); return;}
u8 inpsize=fsize(filename);
if(!inpsize){perror("File empty");return;}
FILE* in=fopen(filename,"rb");
if(!in){perror(filename);return;}
storage=realloc(storage,inpsize);
if(!storage){perror("Malloc failure");return;}
u8 findex=(u8)fread(storage,1,inpsize,in);
fclose(in);
if(findex!=inpsize){perror("Read size mismatch");return;}}

void writecontent(u1* filename,u1* content,u8 size){
FILE* out=fopen(filename,"wb");
if(!out){perror(filename);return;}
u8 findex=(u8)fwrite(content,1,size,out);fclose(out);
if(findex!=size) perror("Write size mismatch");}

void buf2mpz(u1* string,mpz_t num,u8 size){
if(!string){perror("NULL string");return;}
if(!num){perror("Invalid mpz_t");return;}
if(!size){perror("Zero-length string");return;}
mpz_import(num,size, 1,1, -1, 0,string);
}

#define tstruct typedef struct
#define loop(loopx,loopy) for(loopx=0;loopx<loopy;loopx++)

#define fextension(string) strrchr(string,'.')?:".???"
#define isbit(xint,ypos)    ((xint)&(1<<ypos)) 
#define frop(x,filename) FILE* x=fopen(filename,"rb")
#define fwop(x,filename) FILE* x=fopen(filename,"wb")

#define outx(x,fil) fwrite(&x,1,sizeof(x),fil);
#define mpz(x) mpz_t x;mpz_init(x);
#define mpf(x) mpf_t x;mpf_init(x);
//static array
#define mpfr(x) MPFR_DECL_INIT(x, 53)  //returns float
#define DEBUG 1
#define DEBUG2 1
#define DEBUG3 0
#define MAXCHAIN 100000  // more for bigger files/
#define C8 "Output.dat"
#define SCALE 1 //was 8*inpsize
#define fdisp(x) puts("");mpf_out_str(stdout,10, 0,x);
//===========MAIN======================
#define setch(o,ch,pos)  (o|=ch<<(pos<<1))  // pos 0-3,ch=0-3
#define getbb(o,pos) ((o>>(pos<<1))&3) //get 001100>>2 &3
#define getb0(b) (b&3)
#define getb1(b) ((b>>2)&3)
#define getb2(b) ((b>>4)&3)
#define getb3(b) ((b>>6)&3)
 
 //(1<<(pos<<1))
//========MAIN===
u4 int2b(u4 v){  // 32-bit value to find the log2 of
static const unsigned int b[] = {0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0,
                                 0xFF00FF00, 0xFFFF0000};
unsigned int r = (v & b[0]) != 0;
  r |= ((v & b[4]) != 0) << 4;
  r |= ((v & b[3]) != 0) << 3;
  r |= ((v & b[2]) != 0) << 2;
  r |= ((v & b[1]) != 0) << 1;
return r;}


 main(int argc,char**argv){
/*
lx=x&0xf
hx=(x&f0)>>4
if hx^0xf==lx,setbit1

*/
if(!argv[1]){printf("Syntax:cmp inputfile [-d=decode]");exit(1);}
//-----DECODE--Does not work-----
if(argv[2] && !strcmp(argv[2],"-d")){//decode Data+Bitmap
u1* bitfile=(u1*)getcontent("Bitmapx16");
u1* datafile=(u1*)getcontent("Data");
u8 xsize=fsize("Bitmapx16");u8 btsize=fsize("Data");
FILE* result=fopen("Result","wb");
if(!bitfile||!datafile||!result){perror("File error");exit(2);}
//convert into n1 byte nib(avoids corruption)
u1* expbit=malloc(btsize*4);
u8 n8;for(n8=0;n8<btsize;n8++){
expbit[n8*4]=getb0(datafile[n8]);
expbit[n8*4+1]=getb1(datafile[n8]);
expbit[n8*4+2]=getb2(datafile[n8]);
expbit[n8*4+3]=getb3(datafile[n8]);}

u1 bitsave=0,s0,s1,s2,s3;//read output nib
u8 m,dpos=0;u4 a2,cbit;for(m=0;m<xsize;m++){
for(a2=0;a2<8;a2++){//read bit
cbit=chkb(bitfile[m],a2);//current bit:
if(cbit){//read three,write four, nib pos never changes due expbit array
s0=expbit[dpos++];s1=expbit[dpos++];s2=expbit[dpos++];
bitsave=(s0)|(s1<<2)|(s2<<4)|((s0^3)<<6);fputc(bitsave,result);
}else{//read four, write four,
s0=expbit[dpos++];s1=expbit[dpos++];s2=expbit[dpos++];s3=expbit[dpos++];
bitsave=(s0)|(s1<<2)|(s2<<4)|(s3<<6);fputc(bitsave,result);
}
}//iloop end

}

;printf("Decoded to file Result");exit(10);}
//------INPUT--------
u8 inpsize=fsize(argv[1]);if(!inpsize){perror("input size invalid");return;};
u1* input=getcontent(argv[1]);
u1* output=calloc(inpsize/8 +1,1);
if(!input){perror("input invalid");return;}
FILE* bitfile=fopen("Bitmapx16","wb");if(!bitfile){perror("File write failed");return;};
FILE* datafile=fopen("Data","wb");if(!bitfile){perror("File write failed");return;};
u8 c,total=0,opos=0;u4 j;u1 l0,l1,l2,l3,bitsave=0,bitpos=0,startnib=0;

for(c=1;c<inpsize;c++){
l0=getb0(input[c]);
l1=getb1(input[c]);
l2=getb2(input[c]);
l3=getb3(input[c]); //opt
if(l0^3==l3){ total++;
switch(startnib){//
case 0:;bitsave|=(l0);bitsave|=(l1<<2);bitsave|=(l2<<4);;startnib=3;break;
case 1:;bitsave|=(l0<<2);bitsave|=(l1<<4);bitsave|=(l2<<6);
fputc(bitsave,datafile);bitsave=0;startnib=0;break;
case 2:;bitsave|=(l0<<4);bitsave|=(l1<<6);
fputc(bitsave,datafile);bitsave=0;bitsave|=(l2);startnib=1;break;
case 3:;bitsave|=(l0<<6);fputc(bitsave,datafile);bitsave=0;
bitsave|=(l1);bitsave|=(l2<<2);startnib=2;break;;default:break;;}
output[opos]|=(1<<(bitpos));}else{//save all
switch(startnib){//no change in start nib
case 0:;bitsave|=(l0);bitsave|=(l1<<2);bitsave|=(l2<<4);bitsave|=(l3<<6);
fputc(bitsave,datafile);bitsave=0;;break;
case 1:;bitsave|=(l0<<2);bitsave|=(l1<<4);bitsave|=(l2<<6);
fputc(bitsave,datafile);bitsave=0;bitsave|=(l3);;break;
case 2:;bitsave|=(l0<<4);bitsave|=(l1<<6);
fputc(bitsave,datafile);bitsave=0;bitsave|=(l2);bitsave|=(l3<<2);;break;
case 3:;bitsave|=(l0<<6);fputc(bitsave,datafile);bitsave=0;
bitsave|=(l1);bitsave|=(l2<<2);bitsave|=(l2<<4);break;default:break;;;}

}
if(++bitpos==8){bitpos=0;opos++;}
;}

fwrite(output,1,inpsize/8+1,bitfile);
printf("Total bytes saved %llu ",total/4);

//====================================
; }

Name: Anonymous 2011-10-12 10:47

vomitchan.png

Name: FrozenVoid 2011-10-12 11:12

>>2
I formatted it for easy reading, added comments and posted with [code] tags, what is wrong with it?

Name: Anonymous 2011-10-12 11:59

>>3
You forgot the int from main(int argc,char**argv){

It might be valid C-code, but very hard to read.

Name: Anonymous 2011-10-12 12:21

>C89
Why? Why are you stuck in the 80s?

>u1-8
WHY?

Name: FrozenVoid 2011-10-12 12:24

>>4
Its superfluos.
>>5
u8 is easy to type instead of "unsigned long long" its 2 vs 27 chars(a x13 increase in space).
 Its easy to discern:
u8= unsigned 8 bytes= quad
u4= unsigned 4 bytes= Int
u2= unsigned 2 bytes= short
u1= unsigned 1 bytes= char

Name: FrozenVoid 2011-10-12 12:25

Here is the complete type defines from void.h
#define u1 unsigned char
#define u2 unsigned short
#define u4 unsigned int
#define u8 unsigned long long
#define s1 signed char
#define s2 signed short
#define s4 signed int
#define s8 signed long long
#define f2 short float
#define f4 float
#define f8 double
#define f10 long double

Name: Anonymous 2011-10-12 12:30

>>6
Unconventional, unnecessary and wrong.

Unconventional because the numbering typically counts the bits. u32, for example.
Unnecessary because there are uintN_[least/fast/]t in stdint.h already.
Wrong because, for example, unsigned long is not 32-bits in size, but at least 32-bits in size.

Also, if you're using C89, long long is not defined.
Also, why not typedefs instead of macros?

IOW: extremely shitty code.

Name: Anonymous 2011-10-12 12:31

Lacks some defines, here
#define uf10 unsigned long double
#define uf8 unsigned double
#define uf6 unsigned long float
#define uf4 unsigned float
#define uf2 unsigned short float

Name: Anonymous 2011-10-12 12:34

So many invisible posts

Name: sage 2011-10-12 12:36

>>9
#define fuds unsigned signed double float /* some performance haxx */

Name: Anonymous 2011-10-12 12:36

#include <stdio.h>

#define chkb(o,p) ((o>>p)&1)
#define getb0(b) ((b >> 0) & 3)
#define getb1(b) ((b >> 2) & 3)
#define getb2(b) ((b >> 4) & 3)
#define getb3(b) ((b >> 6) & 3)

unsigned long long fsize(unsigned char* filename) {
    FILE* tfile = fopen(filename, "rb");
    if (!tfile) {
        perror(filename);
        return 0;
    }
    fseek(tfile, 0, SEEK_END);
    unsigned long long sz = (unsigned long long)(ftell(tfile));
    fclose(tfile);
    return sz;
}

unsigned char* getcontent(unsigned char*filename) { //file to buffer
    unsigned long long size = fsize(filename); //
    if (!size) {
        perror("Zero length content");
        return NULL;
    }
    unsigned char* res = malloc(size);
    if (!res) {
        perror("Malloc failure");
        return NULL;
    }
    FILE* inpfile = fopen(filename, "rb");
    if (!inpfile) {
        perror("File cannot be read");
        return NULL;
    }
    unsigned long long findex = (unsigned long long)fread(res, 1, size, inpfile);
    fclose(inpfile);
    if (findex != size) {
        perror("size mismatch");
        return NULL;
    }
    return res;
}

unsigned int int2b(unsigned int v) { // 32-bit value to find the log2 of
    static const unsigned int b[] = {0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0,
                                     0xFF00FF00, 0xFFFF0000
                                    };
    unsigned int r = (v & b[0]) != 0;
    r |= ((v & b[4]) != 0) << 4;
    r |= ((v & b[3]) != 0) << 3;
    r |= ((v & b[2]) != 0) << 2;
    r |= ((v & b[1]) != 0) << 1;
    return r;
}

int main(int argc, char**argv) {
    if (!argv[1]) {
        printf("Syntax: cmp inputfile [-d=decode]");
        exit(1);
    }
#if 0 /* Decode: does not work. */
    if (argv[2] && !strcmp(argv[2], "-d")) { // Decode data + bitmap
        unsigned char* bitfile = getcontent("Bitmapx16");
        unsigned char* datafile = getcontent("Data");
        unsigned long long xsize = fsize("Bitmapx16");
        unsigned long long btsize = fsize("Data");
        FILE* result = fopen("Result", "wb");
        if (!bitfile || !datafile || !result) {
            perror("File error");
            exit(2);
        }
        // convert into n1 byte nib(avoids corruption)
        unsigned char* expbit = malloc(btsize * 4);
        unsigned long long n8;
        for (n8 = 0; n8 < btsize; n8++) {
            expbit[n8 * 4] = getb0(datafile[n8]);
            expbit[n8 * 4 + 1] = getb1(datafile[n8]);
            expbit[n8 * 4 + 2] = getb2(datafile[n8]);
            expbit[n8 * 4 + 3] = getb3(datafile[n8]);
        }

        unsigned char bitsave = 0, s0, s1, s2, s3; // read output nib
        unsigned long long m, dpos = 0;
        unsigned int a2, cbit;
        for (m = 0; m < xsize; m++) {
            for (a2 = 0; a2 < 8; a2++) { // read bit
                cbit = chkb(bitfile[m], a2); // current bit:
                if (cbit) { // read three, write four, nib pos never changes due expbit array
                    s0 = expbit[dpos++];
                    s1 = expbit[dpos++];
                    s2 = expbit[dpos++];
                    bitsave = (s0) | (s1 << 2) | (s2 << 4) | ((s0 ^ 3) << 6);
                    fputc(bitsave, result);
                } else { // read four, write four,
                    s0 = expbit[dpos++];
                    s1 = expbit[dpos++];
                    s2 = expbit[dpos++];
                    s3 = expbit[dpos++];
                    bitsave = (s0) | (s1 << 2) | (s2 << 4) | (s3 << 6);
                    fputc(bitsave, result);
                }
            } // iloop end
        }

        printf("Decoded to file Result");
        exit(10);
    }
#endif
    unsigned long long inpsize = fsize(argv[1]);
    if (!inpsize) {
        perror("input size invalid");
        return;
    };
    unsigned char* input = getcontent(argv[1]);
    unsigned char* output = calloc(inpsize / 8 + 1, 1);
    if (!input) {
        perror("input invalid");
        return;
    }
    FILE* bitfile = fopen("Bitmapx16", "wb");
    if (!bitfile) {
        perror("File write failed");
        return;
    };
    FILE* datafile = fopen("Data", "wb");
    if (!bitfile) {
        perror("File write failed");
        return;
    };
    unsigned long long c, total = 0, opos = 0;
    unsigned int j;
    unsigned char l0, l1, l2, l3, bitsave = 0, bitpos = 0, startnib = 0;

    for (c = 1; c < inpsize; c++) {
        l0 = getb0(input[c]);
        l1 = getb1(input[c]);
        l2 = getb2(input[c]);
        l3 = getb3(input[c]); //opt
        if (l0 ^ 3 == l3) {
            total++;
            switch (startnib) { //
            case 0:
                bitsave |= (l0);
                bitsave |= (l1 << 2);
                bitsave |= (l2 << 4);;
                startnib = 3;
                break;
            case 1:
                bitsave |= (l0 << 2);
                bitsave |= (l1 << 4);
                bitsave |= (l2 << 6);
                fputc(bitsave, datafile);
                bitsave = 0;
                startnib = 0;
                break;
            case 2:
                bitsave |= (l0 << 4);
                bitsave |= (l1 << 6);
                fputc(bitsave, datafile);
                bitsave = 0;
                bitsave |= (l2);
                startnib = 1;
                break;
            case 3:
                bitsave |= (l0 << 6);
                fputc(bitsave, datafile);
                bitsave = 0;
                bitsave |= (l1);
                bitsave |= (l2 << 2);
                startnib = 2;
                break;
            default:
                break;
            }
            output[opos] |= (1 << (bitpos));
        } else {
            switch (startnib) {
            case 0:
                bitsave |= (l0);
                bitsave |= (l1 << 2);
                bitsave |= (l2 << 4);
                bitsave |= (l3 << 6);
                fputc(bitsave, datafile);
                bitsave = 0;;
                break;
            case 1:
                bitsave |= (l0 << 2);
                bitsave |= (l1 << 4);
                bitsave |= (l2 << 6);
                fputc(bitsave, datafile);
                bitsave = 0;
                bitsave |= (l3);;
                break;
            case 2:
                bitsave |= (l0 << 4);
                bitsave |= (l1 << 6);
                fputc(bitsave, datafile);
                bitsave = 0;
                bitsave |= (l2);
                bitsave |= (l3 << 2);;
                break;
            case 3:
                bitsave |= (l0 << 6);
                fputc(bitsave, datafile);
                bitsave = 0;
                bitsave |= (l1);
                bitsave |= (l2 << 2);
                bitsave |= (l2 << 4);
                break;
            default:
                break;
            }

        }
        if (++bitpos == 8) {
            bitpos = 0;
            opos++;
        }
    }

    fwrite(output, 1, inpsize / 8 + 1, bitfile);
    printf("Total bytes saved: %llu\n", total / 4);
}


It still doesn't make sense.

Name: FrozenVoid 2011-10-12 12:40

>>8
i want to easily define some quantity: i don't think in terms of bits
bit are not directly addressable via a pointer, only bytes.
I want to allocate variable which can be defined in bits, i don't think such exist: in fact i have solved over a dozen corruption errors which all stem from the fact bits cannot be addressed individually.
There is no uint12bits or whatever nonsense. There bytes, just bytes and ways to address them. To get real 12bits, you have to define a struct with bitfields, which contain byte-level variables.
And in practice, its not worth the abstraction if you can write bithacks for fast and optimized code which uses shifts/masks.

Name: FrozenVoid 2011-10-12 12:46

>>12
Its easy to understand:
1. every byte is split into 4 parts 2 bits each
2. if part0^3==part3 (part3 is part0 inversed)
 save only 3/4 of byte and set 1 bit in bitmap
2.if part0^3!=part3 save the entire byte, set 0 in bitmap.
4.the bitmap can be compressed conventionally for further 20-40% reduction in size. 
5. the data with saved bytes, is a separate file which can be recursively compressed again(about 3-10% reduction in size).

Name: Anonymous 2011-10-12 13:06

pigeonhole principle motherfuckers

Name: FrozenVoid 2011-10-12 13:17

>>15
It does not violate any "pigeonhole principle" it just wouldn't compress some data(that which has unlinked bit parts as in >>14 .

Name: FrozenVoid 2011-10-12 13:29

The idea of compressing single byte is about redudancy of the representation as described in http://dis.4chan.org/read/prog/1318181159/
The xor part is about avoiding relying on static properties of data,
e.g. compressing all 00's or 101's, it shifts the generation on  data context to be more dynamic(if you notice, the "removed" bit parts are essentially random, since they reflect the underlying data structure from which they are reconstructed)

Name: Anonymous 2011-10-12 13:30

>>14
IOW:

Assuming homogenous distribution,
- 25% of the time (when parts match), you save 7 bits per byte;
- 75% of the time (when they don't), you save 9 bits per byte.

That is, on average, 8.5 bits per byte. Awesome compression.

Also, why the fuck do you need to "invert" parts? For "fast and optimized code"?

Seriously, just stop trying. You visibly has no knowledge in either C or information theory.

And to not pose as a completely mean guy, read about Huffman encoding. It has been proven optimal for entropy encoding, and it is easy to implement. Oh, unless your algorithm is rather "specialized" for some "special data format" I'm unaware of.

Name: FrozenVoid 2011-10-12 13:41

>That is, on average, 8.5 bits per byte. Awesome compression.
The filesize is reduced
1. a completely incompressible file of 1 million binary digits is
 now 337kb(with 51kb bitmap which is further compressible since it contains redundancy) instead of 415kb.
http://marknelson.us/2006/06/20/million-digit-challenge/

Name: FrozenVoid 2011-10-12 13:59

2. Since the encoder is context sensitive it does not care if there are
25% of Possible values(that is theoretical freq for static,single value match),it just xors and matches data from stream.
I will demonstrate why your thinking is deficient:
1. the "invert" is xor generation step
2. it generates data from context: if data was the same all the time, such as 00's it shows as no matches. it is meant for more random data.
3. there is a small pool of 4 possible bitstrings for 2 bits:
xor^3 flips all bits in a bitstring: 00^3=11, 01^3=10,10^3=01,11^3=00. this ensures the 2bits of last part
are different and in random data there is distance of 4 bits from part0 to part3 which further makes this more random.
4. The value system is meaningless: there is no static partX which is found and eliminated, it scans for "change",
The bytes are only used as container for end and start: the scheme could be reworked with bitfields and more optimal generation, but the principle remains the same:
Context can generate its own data,as if it were a fractal.

Name: Anonymous 2011-10-12 16:08

Someone just tell this nigger what a fast fourier transform is already.

Name: FrozenVoid 2011-10-12 18:55

>>21
This is about lossless compression. Infinite lossless compression.

Name: Anonymous 2011-10-13 1:39

I still can't discern whether FrozenVoid is trolling with the whole "infinite compression" shit.

Name: FrozenVoid 2011-10-13 5:41

I simplified the encoder by removing startnib and making
 the 2bit values an array of bytes(holding 2bit values).

//====Headers
#include <iso646.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <locale.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include <gmp.h>   //using libgmp.a
#include <mpfr.h>
//======Types
#define u1 unsigned char
#define u2 unsigned short
#define u4 unsigned int
#define u8 unsigned long long
//====Bithacks
#define setb(o,p) (o|=(1<<p))      //byte| 1<< pos  
#define clrb(o,p) (o&=(~(1<<p)))  // byte | 11101111
#define togb(o,p) (o^=(1<<p))
#define chkb(o,p) ((o>>p)&1)
#define bitchar(bitchar_bit)    (48|bitchar_bit) // 48|0 or 1
//====Func
static __inline__ u8 rdtsc(void){u8 x;
 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));     return x;}

u8 fsize(u1* filename){
FILE* tfile=fopen(filename,"rb");
if(!tfile){perror(filename);return 0;}
fseek(tfile,0,SEEK_END);
u8 sz=(u8)(ftell(tfile));fclose(tfile);return sz;}

u1* getcontent(u1*filename){//file to buffer
u8 size=fsize(filename);//
if(!size){perror("Zero length content"); return NULL;}
u1* res=malloc(size);
if(!res){perror("Malloc failure");return NULL;}
FILE* inpfile=fopen(filename,"rb");
if(!inpfile){perror("File cannot be read");return NULL;}
u8 findex=(u8)fread(res,1,size,inpfile);fclose(inpfile);
if(findex!=size){perror("size mismatch");return NULL;}
return res;}


void readcontent(u1* filename,u1* storage){
if(!filename){perror("Invalid filename"); return;}
u8 inpsize=fsize(filename);
if(!inpsize){perror("File empty");return;}
FILE* in=fopen(filename,"rb");
if(!in){perror(filename);return;}
storage=realloc(storage,inpsize);
if(!storage){perror("Malloc failure");return;}
u8 findex=(u8)fread(storage,1,inpsize,in);
fclose(in);
if(findex!=inpsize){perror("Read size mismatch");return;}}

void writecontent(u1* filename,u1* content,u8 size){
FILE* out=fopen(filename,"wb");
if(!out){perror(filename);return;}
u8 findex=(u8)fwrite(content,1,size,out);fclose(out);
if(findex!=size) perror("Write size mismatch");}

void buf2mpz(u1* string,mpz_t num,u8 size){
if(!string){perror("NULL string");return;}
if(!num){perror("Invalid mpz_t");return;}
if(!size){perror("Zero-length string");return;}
mpz_import(num,size, 1,1, -1, 0,string);
}

#define tstruct typedef struct
#define loop(loopx,loopy) for(loopx=0;loopx<loopy;loopx++)

#define fextension(string) strrchr(string,'.')?:".???"
#define isbit(xint,ypos)    ((xint)&(1<<ypos)) 
#define frop(x,filename) FILE* x=fopen(filename,"rb")
#define fwop(x,filename) FILE* x=fopen(filename,"wb")

#define outx(x,fil) fwrite(&x,1,sizeof(x),fil);
#define mpz(x) mpz_t x;mpz_init(x);
#define mpf(x) mpf_t x;mpf_init(x);
//static array
#define mpfr(x) MPFR_DECL_INIT(x, 53)  //returns float
#define DEBUG 1
#define DEBUG2 1
#define DEBUG3 0
#define fdisp(x) puts("");mpf_out_str(stdout,10, 0,x);
//===========MAIN======================
#define setch(o,ch,pos)  (o|=ch<<(pos<<1))  // pos 0-3,ch=0-3
#define getbb(o,pos) ((o>>(pos<<1))&3) //get 001100>>2 &3
#define getb0(b) (b&3)
#define getb1(b) ((b>>2)&3)
#define getb2(b) ((b>>4)&3)
#define getb3(b) ((b>>6)&3)

 //(1<<(pos<<1))
//========MAIN===
u4 int2b(u4 v){  // 32-bit value to find the log2 of
static const unsigned int b[] = {0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0,
                                 0xFF00FF00, 0xFFFF0000};
unsigned int r = (v & b[0]) != 0;
  r |= ((v & b[4]) != 0) << 4;
  r |= ((v & b[3]) != 0) << 3;
  r |= ((v & b[2]) != 0) << 2;
  r |= ((v & b[1]) != 0) << 1;
return r;}


 main(int argc,char**argv){
if(!argv[1]){printf("Syntax:cmp inputfile [-d=decode]");exit(1);}
//-----DECODE--TODO-----
if(argv[2] && !strcmp(argv[2],"-d")){//decode Data+Bitmap
u1* bitfile=(u1*)getcontent("Bitmapx16");
u1* datafile=(u1*)getcontent("Data");
u8 xsize=fsize("Bitmapx16");
u8 btsize=fsize("Data");
FILE* result=fopen("Result","wb");
if(!bitfile||!datafile||!result){perror("File error");exit(2);}
//convert into n1 byte nib(avoids corruption)
u1* expbit=malloc(btsize*4);
u8 n8;for(n8=0;n8<btsize;n8++){
expbit[n8*4]=getb0(datafile[n8]);
expbit[n8*4+1]=getb1(datafile[n8]);
expbit[n8*4+2]=getb2(datafile[n8]);
expbit[n8*4+3]=getb3(datafile[n8]);}

u1 bitsave=0,s0,s1,s2,s3;//read output nib
u8 m,dpos=0;u4 a2,cbit;
for(m=0;m<xsize;m++){//read byte
for(a2=0;a2<8;a2++){//read bit
cbit=chkb(bitfile[m],a2);//current bit:
if(cbit){//read three,write four, nib pos never changes due expbit array
s0=expbit[dpos++];
s1=expbit[dpos++];
s2=expbit[dpos++];
bitsave=(s0)|(s1<<2)|(s2<<4)|((s0^3)<<6);
fputc(bitsave,result);
}else{//read four, write four
s0=expbit[dpos++];
s1=expbit[dpos++];
s2=expbit[dpos++];
s3=expbit[dpos++];
bitsave=((s0)|(s1<<2)|(s2<<4)|(s3<<6));
fputc(bitsave,result);}}//bitloop end
}//byteloop end
printf("Decoded to file Result");exit(10);}
//------INPUT--------
u8 inpsize=fsize(argv[1]);if(!inpsize){perror("input size invalid");return;};
u1* input=getcontent(argv[1]);
u1* output=calloc(inpsize/8 +1,1);
if(!input){perror("input invalid");return;}
FILE* bitfile=fopen("Bitmapx16","wb");if(!bitfile){perror("File write failed");return;};
FILE* datafile=fopen("Data","wb");if(!bitfile){perror("File write failed");return;};
u8 c,total=0,opos=0;u1 bitsave=0,bitpos=0;
u1* quar=calloc(inpsize*4,1);
u1* outquar=calloc(inpsize*4,1);u8 qpos=0;//max
for(c=0;c<inpsize;c++){
quar[0+c*4]=getb0(input[c]);
quar[1+c*4]=getb1(input[c]);
quar[2+c*4]=getb2(input[c]);
quar[3+c*4]=getb3(input[c]);}
for(c=0;c<inpsize;c++){
outquar[qpos++]=quar[0+c*4];
outquar[qpos++]=quar[1+c*4];
outquar[qpos++]=quar[2+c*4];
if(quar[0+c*4]==quar[3+c*4]^3){
total++;output[opos]|=(1<<(bitpos));}else{
outquar[qpos++]=quar[3+c*4];}
if(++bitpos==8){bitpos=0;opos++;}
;}
for(c=0;c<qpos;c+=4){
bitsave=outquar[c]|(outquar[c+1]<<2)|(outquar[c+2]<<4)|(outquar[c+3]<<6);fputc(bitsave,datafile);}
fwrite(output,1,opos,bitfile);
printf("Total bytes saved %llu ",total/4);

//====================================
; }
/* Simplified with outquar array*/

Name: Anonymous 2011-10-13 14:20

Welp hold the prenses, get this man his Anal Turing Award

Don't change these.
Name: Email:
Entire Thread Thread List