changed to typedefs
authorkramm <kramm>
Sat, 6 Jan 2007 22:23:43 +0000 (22:23 +0000)
committerkramm <kramm>
Sat, 6 Jan 2007 22:23:43 +0000 (22:23 +0000)
lib/bitio.c
lib/bitio.h

index 7397604..189dc76 100644 (file)
 
 /* ---------------------------- null reader ------------------------------- */
 
-static int reader_nullread(struct reader_t*r, void* data, int len) 
+static int reader_nullread(reader_t*r, void* data, int len) 
 {
     memset(data, 0, len);
     return len;
 }
-static void reader_nullread_dealloc(struct reader_t*r)
+static void reader_nullread_dealloc(reader_t*r)
 {
-    memset(r, 0, sizeof(struct reader_t));
+    memset(r, 0, sizeof(reader_t));
 }
-void reader_init_nullreader(struct reader_t*r)
+void reader_init_nullreader(reader_t*r)
 {
     r->read = reader_nullread;
     r->dealloc = reader_nullread_dealloc;
@@ -55,14 +55,14 @@ void reader_init_nullreader(struct reader_t*r)
 }
 /* ---------------------------- file reader ------------------------------- */
 
-static int reader_fileread(struct reader_t*reader, void* data, int len) 
+static int reader_fileread(reader_t*reader, void* data, int len) 
 {
     int ret = read((int)reader->internal, data, len);
     if(ret>=0)
        reader->pos += ret;
     return ret;
 }
-void reader_init_filereader(struct reader_t*r, int handle)
+void reader_init_filereader(reader_t*r, int handle)
 {
     r->read = reader_fileread;
     r->internal = (void*)handle;
@@ -79,7 +79,7 @@ struct memread_t
     unsigned char*data;
     int length;
 };
-static int reader_memread(struct reader_t*reader, void* data, int len) 
+static int reader_memread(reader_t*reader, void* data, int len) 
 {
     struct memread_t*mr = (struct memread_t*)reader->internal;
 
@@ -93,13 +93,13 @@ static int reader_memread(struct reader_t*reader, void* data, int len)
        return mr->length - reader->pos;
     }
 }
-static void reader_memread_dealloc(struct reader_t*reader)
+static void reader_memread_dealloc(reader_t*reader)
 {
     if(reader->internal)
        free(reader->internal);
-    memset(reader, 0, sizeof(struct reader_t));
+    memset(reader, 0, sizeof(reader_t));
 }
-void reader_init_memreader(struct reader_t*r, void*newdata, int newlength)
+void reader_init_memreader(reader_t*r, void*newdata, int newlength)
 {
     struct memread_t*mr = malloc(sizeof(struct memread_t));
     mr->data = newdata;
@@ -121,7 +121,7 @@ struct memwrite_t
     int length;
 };
 
-static int writer_memwrite_write(struct writer_t*w, void* data, int len) 
+static int writer_memwrite_write(writer_t*w, void* data, int len) 
 {
     struct memwrite_t*mw = (struct memwrite_t*)w->internal;
     if(mw->length - w->pos > len) {
@@ -134,19 +134,19 @@ static int writer_memwrite_write(struct writer_t*w, void* data, int len)
        return mw->length - w->pos;
     }
 }
-static void writer_memwrite_finish(struct writer_t*w)
+static void writer_memwrite_finish(writer_t*w)
 {
     if(w->internal) 
        free(w->internal);
     w->internal = 0;
 }
-void writer_init_memwriter(struct writer_t*w, void*data, int len)
+void writer_init_memwriter(writer_t*w, void*data, int len)
 {
     struct memwrite_t *mr;
     mr = malloc(sizeof(struct memwrite_t));
     mr->data = data;
     mr->length = len;
-    memset(w, 0, sizeof(struct writer_t));
+    memset(w, 0, sizeof(writer_t));
     w->write = writer_memwrite_write;
     w->finish = writer_memwrite_finish;
     w->internal = (void*)mr;
@@ -162,31 +162,36 @@ struct growmemwrite_t
 {
     unsigned char*data;
     int length;
+    U32 grow;
 };
-static int writer_growmemwrite_write(struct writer_t*w, void* data, int len) 
+static int writer_growmemwrite_write(writer_t*w, void* data, int len) 
 {
     struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
     if(!mw->data) {
        fprintf(stderr, "Illegal write operation: data already given away");
        exit(1);
     }
-    if(mw->length - w->pos <= len) {
+    if(mw->length - w->pos < len) {
        unsigned char*newmem;
        int newlength = mw->length;
        while(newlength - w->pos < len) {
-           newlength += 4096;
+           newlength += mw->grow;
        }
+#ifdef NO_REALLOC
        newmem = malloc(newlength);
        memcpy(newmem, mw->data, mw->length);
        free(mw->data);
        mw->data = newmem;
+#else
+       mw->data = realloc(mw->data, newlength);
+#endif
        mw->length = newlength;
     }
     memcpy(&mw->data[w->pos], data, len);
     w->pos += len;
     return len;
 }
-static void writer_growmemwrite_finish(struct writer_t*w)
+static void writer_growmemwrite_finish(writer_t*w)
 {
     struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
     if(mw->data)
@@ -194,9 +199,9 @@ static void writer_growmemwrite_finish(struct writer_t*w)
     mw->data = 0;
     mw->length = 0;
     free(w->internal);mw=0;
-    memset(w, 0, sizeof(struct writer_t));
+    memset(w, 0, sizeof(writer_t));
 }
-void* writer_growmemwrite_getmem(struct writer_t*w)
+void* writer_growmemwrite_getmem(writer_t*w)
 {
     struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
     void*ret = mw->data;
@@ -206,13 +211,14 @@ void* writer_growmemwrite_getmem(struct writer_t*w)
     mw->data = 0;
     return ret;
 }
-void writer_init_growingmemwriter(struct writer_t*w)
+void writer_init_growingmemwriter(writer_t*w, U32 grow)
 {
-    struct memwrite_t *mr;
-    mr = malloc(sizeof(struct memwrite_t));
+    struct growmemwrite_t *mr;
+    mr = malloc(sizeof(struct growmemwrite_t));
     mr->length = 4096;
     mr->data = malloc(mr->length);
-    memset(w, 0, sizeof(struct writer_t));
+    mr->grow = grow;
+    memset(w, 0, sizeof(writer_t));
     w->write = writer_growmemwrite_write;
     w->finish = writer_growmemwrite_finish;
     w->internal = (void*)mr;
@@ -230,25 +236,25 @@ struct filewrite_t
     char free_handle;
 };
 
-static int writer_filewrite_write(struct writer_t*w, void* data, int len) 
+static int writer_filewrite_write(writer_t*w, void* data, int len) 
 {
     struct filewrite_t * fw= (struct filewrite_t*)w->internal;
     return write(fw->handle, data, len);
 }
-static void writer_filewrite_finish(struct writer_t*w)
+static void writer_filewrite_finish(writer_t*w)
 {
     struct filewrite_t *mr = (struct filewrite_t*)w->internal;
     if(mr->free_handle)
        close(mr->handle);
     free(w->internal);
-    memset(w, 0, sizeof(struct writer_t));
+    memset(w, 0, sizeof(writer_t));
 }
-void writer_init_filewriter(struct writer_t*w, int handle)
+void writer_init_filewriter(writer_t*w, int handle)
 {
     struct filewrite_t *mr = malloc(sizeof(struct filewrite_t));
     mr->handle = handle;
     mr->free_handle = 0;
-    memset(w, 0, sizeof(struct writer_t));
+    memset(w, 0, sizeof(writer_t));
     w->write = writer_filewrite_write;
     w->finish = writer_filewrite_finish;
     w->internal = mr;
@@ -257,7 +263,7 @@ void writer_init_filewriter(struct writer_t*w, int handle)
     w->mybyte = 0;
     w->pos = 0;
 }
-void writer_init_filewriter2(struct writer_t*w, char*filename)
+void writer_init_filewriter2(writer_t*w, char*filename)
 {
     int fi = open("movie.swf",
 #ifdef O_BINARY
@@ -270,18 +276,18 @@ void writer_init_filewriter2(struct writer_t*w, char*filename)
 
 /* ---------------------------- null writer ------------------------------- */
 
-static int writer_nullwrite_write(struct writer_t*w, void* data, int len) 
+static int writer_nullwrite_write(writer_t*w, void* data, int len) 
 {
     w->pos += len;
     return len;
 }
-static void writer_nullwrite_finish(struct writer_t*w)
+static void writer_nullwrite_finish(writer_t*w)
 {
-    memset(w, 0, sizeof(struct writer_t));
+    memset(w, 0, sizeof(writer_t));
 }
-void writer_init_nullwriter(struct writer_t*w)
+void writer_init_nullwriter(writer_t*w)
 {
-    memset(w, 0, sizeof(struct writer_t));
+    memset(w, 0, sizeof(writer_t));
     w->write = writer_nullwrite_write;
     w->finish = writer_nullwrite_finish;
     w->internal = 0;
@@ -296,7 +302,7 @@ struct zlibinflate_t
 {
 #ifdef HAVE_ZLIB
     z_stream zs;
-    struct reader_t*input;
+    reader_t*input;
     unsigned char readbuffer[ZLIB_BUFFER_SIZE];
 #endif
 };
@@ -313,7 +319,7 @@ static void zlib_error(int ret, char* msg, z_stream*zs)
 }
 #endif
 
-static int reader_zlibinflate(struct reader_t*reader, void* data, int len) 
+static int reader_zlibinflate(reader_t*reader, void* data, int len) 
 {
 #ifdef HAVE_ZLIB
     struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
@@ -360,7 +366,7 @@ static int reader_zlibinflate(struct reader_t*reader, void* data, int len)
     exit(1);
 #endif
 }
-static void reader_zlibinflate_dealloc(struct reader_t*reader)
+static void reader_zlibinflate_dealloc(reader_t*reader)
 {
 #ifdef HAVE_ZLIB
     struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
@@ -369,15 +375,15 @@ static void reader_zlibinflate_dealloc(struct reader_t*reader)
        inflateEnd(&z->zs);
        free(reader->internal);
     }
-    memset(reader, 0, sizeof(struct reader_t));
+    memset(reader, 0, sizeof(reader_t));
 #endif
 }
-void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
+void reader_init_zlibinflate(reader_t*r, reader_t*input)
 {
 #ifdef HAVE_ZLIB
     struct zlibinflate_t*z;
     int ret;
-    memset(r, 0, sizeof(struct reader_t));
+    memset(r, 0, sizeof(reader_t));
     z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
     memset(z, 0, sizeof(struct zlibinflate_t));
     r->internal = z;
@@ -405,12 +411,12 @@ struct zlibdeflate_t
 {
 #ifdef HAVE_ZLIB
     z_stream zs;
-    struct writer_t*output;
+    writer_t*output;
     unsigned char writebuffer[ZLIB_BUFFER_SIZE];
 #endif
 };
 
-static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len) 
+static int writer_zlibdeflate_write(writer_t*writer, void* data, int len) 
 {
 #ifdef HAVE_ZLIB
     struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
@@ -451,11 +457,11 @@ static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len)
     exit(1);
 #endif
 }
-static void writer_zlibdeflate_finish(struct writer_t*writer)
+static void writer_zlibdeflate_finish(writer_t*writer)
 {
 #ifdef HAVE_ZLIB
     struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
-    struct writer_t*output;
+    writer_t*output;
     int ret;
     if(writer->type != WRITER_TYPE_ZLIB) {
        fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
@@ -483,19 +489,19 @@ static void writer_zlibdeflate_finish(struct writer_t*writer)
     ret = deflateEnd(&z->zs);
     if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
     free(writer->internal);
-    memset(writer, 0, sizeof(struct writer_t));
+    memset(writer, 0, sizeof(writer_t));
     //output->finish(output); 
 #else
     fprintf(stderr, "Error: swftools was compiled without zlib support");
     exit(1);
 #endif
 }
-void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
+void writer_init_zlibdeflate(writer_t*w, writer_t*output)
 {
 #ifdef HAVE_ZLIB
     struct zlibdeflate_t*z;
     int ret;
-    memset(w, 0, sizeof(struct writer_t));
+    memset(w, 0, sizeof(writer_t));
     z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
     memset(z, 0, sizeof(struct zlibdeflate_t));
     w->internal = z;
@@ -522,7 +528,7 @@ void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
 
 /* ----------------------- bit handling routines -------------------------- */
 
-void writer_writebit(struct writer_t*w, int bit)
+void writer_writebit(writer_t*w, int bit)
 {    
     if(w->bitpos==8) 
     {
@@ -534,7 +540,7 @@ void writer_writebit(struct writer_t*w, int bit)
        w->mybyte |= 1 << (7 - w->bitpos);
     w->bitpos ++;
 }
-void writer_writebits(struct writer_t*w, unsigned int data, int bits)
+void writer_writebits(writer_t*w, unsigned int data, int bits)
 {
     int t;
     for(t=0;t<bits;t++)
@@ -542,7 +548,7 @@ void writer_writebits(struct writer_t*w, unsigned int data, int bits)
        writer_writebit(w, (data >> (bits-t-1))&1);
     }
 }
-void writer_resetbits(struct writer_t*w)
+void writer_resetbits(writer_t*w)
 {
     if(w->bitpos)
        w->write(w, &w->mybyte, 1);
@@ -550,7 +556,7 @@ void writer_resetbits(struct writer_t*w)
     w->mybyte = 0;
 }
  
-unsigned int reader_readbit(struct reader_t*r)
+unsigned int reader_readbit(reader_t*r)
 {
     if(r->bitpos==8) 
     {
@@ -559,7 +565,7 @@ unsigned int reader_readbit(struct reader_t*r)
     }
     return (r->mybyte>>(7-r->bitpos++))&1;
 }
-unsigned int reader_readbits(struct reader_t*r, int num)
+unsigned int reader_readbits(reader_t*r, int num)
 {
     int t;
     int val = 0;
@@ -570,9 +576,134 @@ unsigned int reader_readbits(struct reader_t*r, int num)
     }
     return val;
 }
-void reader_resetbits(struct reader_t*r)
+void reader_resetbits(reader_t*r)
 {
     r->mybyte = 0;
     r->bitpos = 8;
 
 }
+
+U8 reader_readU8(reader_t*r)
+{
+    U8 b;
+    r->read(r, &b, 1);
+    return b;
+}
+U16 reader_readU16(reader_t*r)
+{
+    U8 b1,b2;
+    r->read(r, &b1, 1);
+    r->read(r, &b2, 1);
+    return b1|b2<<8;
+}
+U32 reader_readU32(reader_t*r)
+{
+    U8 b1,b2,b3,b4;
+    r->read(r, &b1, 1);
+    r->read(r, &b2, 1);
+    r->read(r, &b3, 1);
+    r->read(r, &b4, 1);
+    return b1|b2<<8|b3<<16|b4<<24;
+}
+float reader_readFloat(reader_t*r)
+{
+    U8 b1,b2,b3,b4;
+    r->read(r, &b1, 1);
+    r->read(r, &b2, 1);
+    r->read(r, &b3, 1);
+    r->read(r, &b4, 1);
+    U32 w = (b1|b2<<8|b3<<16|b4<<24);
+    return *(float*)&w;
+}
+double reader_readDouble(reader_t*r)
+{
+    double f;
+    r->read(r, &f, 8);
+    return f;
+
+    U8 b[8];
+    r->read(r, b, 8);
+    U64 w = ((U64)b[0]|(U64)b[1]<<8|(U64)b[2]<<16|(U64)b[3]<<24|(U64)b[4]<<32|(U64)b[5]<<40|(U64)b[6]<<48|(U64)b[7]<<56);
+    return *(double*)&w;
+}
+char*reader_readString(reader_t*r)
+{
+    writer_t g;
+    writer_init_growingmemwriter(&g, 16);
+    while(1) {
+       U8 b = reader_readU8(r);
+       writer_writeU8(&g, b);
+       if(!b)
+           break;
+    }
+    char*string = writer_growmemwrite_getmem(&g);
+    writer_growmemwrite_finish(&g);
+    return string;
+}
+
+void writer_writeString(writer_t*w, const char*s)
+{
+    int l = strlen(s);
+    char zero = 0;
+    w->write(w, (void*)s, l);
+    w->write(w, &zero, 1);
+}
+void writer_writeU8(writer_t*w, unsigned char b)
+{
+    w->write(w, &b, 1);
+}
+void writer_writeU16(writer_t*w, unsigned short v)
+{
+    unsigned char b1 = v;
+    unsigned char b2 = v>>8;
+    w->write(w, &b1, 1);
+    w->write(w, &b2, 1);
+}
+void writer_writeU32(writer_t*w, unsigned long v)
+{
+    unsigned char b1 = v;
+    unsigned char b2 = v>>8;
+    unsigned char b3 = v>>16;
+    unsigned char b4 = v>>24;
+    w->write(w, &b1, 1);
+    w->write(w, &b2, 1);
+    w->write(w, &b3, 1);
+    w->write(w, &b4, 1);
+}
+void writer_writeFloat(writer_t*w, float f)
+{
+    unsigned long v = *(unsigned long*)&f;
+    unsigned char b1 = v;
+    unsigned char b2 = v>>8;
+    unsigned char b3 = v>>16;
+    unsigned char b4 = v>>24;
+    w->write(w, &b1, 1);
+    w->write(w, &b2, 1);
+    w->write(w, &b3, 1);
+    w->write(w, &b4, 1);
+}
+void writer_writeDouble(writer_t*w, double f)
+{
+    w->write(w, &f, 8);
+    return;
+
+    unsigned long long v = *(unsigned long long*)&f;
+    unsigned char b1 = v;
+    unsigned char b2 = v>>8;
+    unsigned char b3 = v>>16;
+    unsigned char b4 = v>>24;
+    unsigned char b5 = v>>32;
+    unsigned char b6 = v>>40;
+    unsigned char b7 = v>>48;
+    unsigned char b8 = v>>56;
+    w->write(w, &b1, 1);
+    w->write(w, &b2, 1);
+    w->write(w, &b3, 1);
+    w->write(w, &b4, 1);
+    w->write(w, &b5, 1);
+    w->write(w, &b6, 1);
+    w->write(w, &b7, 1);
+    w->write(w, &b8, 1);
+}
+
+
index fe54471..888a54f 100644 (file)
@@ -20,6 +20,7 @@
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
 #include <stdio.h>
+#include "types.h"
 
 #ifndef __rfxswf_bitio_h__
 #define __rfxswf_bitio_h__
 #define WRITER_TYPE_GROWING_MEM  6
 #define WRITER_TYPE_ZLIB WRITER_TYPE_ZLIB_C
 
-struct reader_t
+typedef struct _reader
 {
-    int (*read)(struct reader_t*, void*data, int len);
-    void (*dealloc)(struct reader_t*);
+    int (*read)(struct _reader*, void*data, int len);
+    void (*dealloc)(struct _reader*);
 
     void *internal;
     int type;
     unsigned char mybyte;
     unsigned char bitpos;
     int pos;
-};
+} reader_t;
 
-struct writer_t
+typedef struct _writer
 {
-    int (*write)(struct writer_t*, void*data, int len);
-    void (*finish)(struct writer_t*);
+    int (*write)(struct _writer*, void*data, int len);
+    void (*finish)(struct _writer*);
 
     void *internal;
     int type;
     unsigned char mybyte;
     unsigned char bitpos;
     int pos;
-};
-
-void reader_resetbits(struct reader_t*r);
-unsigned int reader_readbit(struct reader_t*r);
-unsigned int reader_readbits(struct reader_t*r, int num);
-
-void writer_resetbits(struct writer_t*w);
-void writer_writebit(struct writer_t*w, int bit);
-void writer_writebits(struct writer_t*w, unsigned int data, int bits);
+} writer_t;
+
+void reader_resetbits(reader_t*r);
+unsigned int reader_readbit(reader_t*r);
+unsigned int reader_readbits(reader_t*r, int num);
+
+U8 reader_readU8(reader_t*r);
+U16 reader_readU16(reader_t*r);
+U32 reader_readU32(reader_t*r);
+float reader_readFloat(reader_t*r);
+double reader_readDouble(reader_t*r);
+char*reader_readString(reader_t*r);
+
+void writer_resetbits(writer_t*w);
+void writer_writebit(writer_t*w, int bit);
+void writer_writebits(writer_t*w, unsigned int data, int bits);
+
+void writer_writeU8(writer_t*w, unsigned char b);
+void writer_writeU16(writer_t*w, unsigned short v);
+void writer_writeU32(writer_t*w, unsigned long v);
+void writer_writeFloat(writer_t*w, float f);
+void writer_writeDouble(writer_t*w, double f);
+void writer_writeString(writer_t*w, const char*s);
 
 /* standard readers / writers */
 
-void reader_init_filereader(struct reader_t*r, int handle);
-void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input);
-void reader_init_memreader(struct reader_t*r, void*data, int length);
-void reader_init_nullreader(struct reader_t*r);
+void reader_init_filereader(reader_t*r, int handle);
+void reader_init_zlibinflate(reader_t*r, reader_t*input);
+void reader_init_memreader(reader_t*r, void*data, int length);
+void reader_init_nullreader(reader_t*r);
 
-void writer_init_filewriter(struct writer_t*w, int handle);
-void writer_init_filewriter2(struct writer_t*w, char*filename);
-void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output);
-void writer_init_memwriter(struct writer_t*r, void*data, int length);
-void writer_init_nullwriter(struct writer_t*w);
+void writer_init_filewriter(writer_t*w, int handle);
+void writer_init_filewriter2(writer_t*w, char*filename);
+void writer_init_zlibdeflate(writer_t*w, writer_t*output);
+void writer_init_memwriter(writer_t*r, void*data, int length);
+void writer_init_nullwriter(writer_t*w);
 
-void writer_init_growingmemwriter(struct writer_t*r);
-void* writer_growmemwrite_getmem(struct writer_t*w);
+void writer_init_growingmemwriter(writer_t*r, U32 grow);
+void* writer_growmemwrite_getmem(writer_t*w);
 
 #endif //__rfxswf_bitio_h__