-#define READER_TYPE_FILE 0
-#define READER_TYPE_MEM 1
-#define READER_TYPE_ZLIB 2
-#define WRITER_TYPE_FILE 0
-#define WRITER_TYPE_MEM 1
-#define WRITER_TYPE_ZLIB 2
-
-struct reader_t
+/* bitio.c
+ implementation of bitio.h.
+
+ Part of the swftools package.
+
+ Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
+
+ This file is distributed under the GPL, see file COPYING for details */#include <stdio.h>
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <zlib.h>
+#include "./bitio.h"
+
+#define ZLIB_BUFFER_SIZE 16384
+
+struct memread_t
{
- int (*read)(struct reader_t*, void*data, int len);
- void *internal;
- int type;
- unsigned char mybyte;
- unsigned char bitpos;
+ unsigned char*data;
+ int pos;
+ int length;
};
-struct writer_t
+
+struct memwrite_t
{
- int (*write)(struct writer_t*, void*data, int len);
- void (*finish)(struct writer_t*);
- void *internal;
- int type;
- unsigned char mybyte;
- unsigned char bitpos;
+ unsigned char*data;
+ int pos;
+ int length;
};
-static int reader_fileread(struct reader_t*reader, void* data, int len)
+
+struct zlibinflate_t
{
- return read((int)reader->internal, data, len);
-}
-static void reader_resetbits(struct reader_t*r)
+ z_stream zs;
+ struct reader_t*input;
+ unsigned char readbuffer[ZLIB_BUFFER_SIZE];
+};
+
+struct zlibdeflate_t
+{
+ z_stream zs;
+ struct writer_t*output;
+ unsigned char writebuffer[ZLIB_BUFFER_SIZE];
+};
+
+void reader_resetbits(struct reader_t*r)
{
r->mybyte = 0;
r->bitpos = 8;
+
}
-static void reader_init_filereader(struct reader_t*r, int handle)
+
+static int reader_zlibinflate(struct reader_t*reader, void* data, int len);
+static int reader_fileread(struct reader_t*reader, void* data, int len);
+static int reader_memread(struct reader_t*reader, void* data, int len);
+static void zlib_error(int ret, char* msg, z_stream*zs);
+
+void reader_init_filereader(struct reader_t*r, int handle)
{
r->read = reader_fileread;
r->internal = (void*)handle;
r->mybyte = 0;
r->bitpos = 8;
}
-#define ZLIB_BUFFER_SIZE 16384
-struct zlibinflate_t
-{
- z_stream zs;
- struct reader_t*input;
- U8 readbuffer[ZLIB_BUFFER_SIZE];
-};
-static void zlib_error(int ret, char* msg, z_stream*zs)
+
+void reader_init_memreader(struct reader_t*r, void*newdata, int newlength)
{
- fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
- msg,
- ret,
- zs->msg?zs->msg:"unknown");
- perror("errno:");
- exit(1);
+ struct memread_t*mr = malloc(sizeof(struct memread_t));
+ mr->data = newdata;
+ mr->length = newlength;
+ r->read = reader_memread;
+ r->internal = (void*)mr;
+ r->type = READER_TYPE_MEM;
+ r->mybyte = 0;
+ r->bitpos = 8;
}
-static int reader_zlibinflate(struct reader_t*reader, void* data, int len);
-static void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
+
+void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
{
struct zlibinflate_t*z;
int ret;
if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
reader_resetbits(r);
}
+
+static void zlib_error(int ret, char* msg, z_stream*zs)
+{
+ fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
+ msg,
+ ret,
+ zs->msg?zs->msg:"unknown");
+ perror("errno:");
+ exit(1);
+}
+
+static int reader_fileread(struct reader_t*reader, void* data, int len)
+{
+ return read((int)reader->internal, data, len);
+}
+
+static int reader_memread(struct reader_t*reader, void* data, int len)
+{
+ struct memread_t*mr = (struct memread_t*)reader->internal;
+
+ if(mr->length - mr->pos > len) {
+ memcpy(data, &mr->data[mr->pos], len);
+ mr->pos += len;
+ return len;
+ } else {
+ memcpy(data, &mr->data[mr->pos], mr->length - mr->pos);
+ mr->pos = mr->length;
+ return mr->length - mr->pos;
+ }
+}
+
static int reader_zlibinflate(struct reader_t*reader, void* data, int len)
{
struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
}
return len;
}
-static unsigned int reader_readbit(struct reader_t*r)
+unsigned int reader_readbit(struct reader_t*r)
{
if(r->bitpos==8)
{
}
return (r->mybyte>>(7-r->bitpos++))&1;
}
-static unsigned int reader_readbits(struct reader_t*r, int num)
+unsigned int reader_readbits(struct reader_t*r, int num)
{
int t;
int val = 0;
}
return val;
}
+
+static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len);
+static void writer_zlibdeflate_finish(struct writer_t*writer);
+static int writer_filewrite_write(struct writer_t*w, void* data, int len);
+static void writer_filewrite_finish(struct writer_t*w);
+
static int writer_filewrite_write(struct writer_t*w, void* data, int len)
{
return write((int)w->internal, data, len);
static void writer_filewrite_finish(struct writer_t*w)
{
}
-static void writer_resetbits(struct writer_t*w)
+
+static int writer_memwrite_write(struct writer_t*w, void* data, int len)
+{
+ struct memread_t*mw = (struct memread_t*)w->internal;
+ if(mw->length - mw->pos > len) {
+ memcpy(&mw->data[mw->pos], data, len);
+ mw->pos += len;
+ return len;
+ } else {
+ memcpy(&mw->data[mw->pos], data, mw->length - mw->pos);
+ mw->pos = mw->length;
+ return mw->length - mw->pos;
+ }
+}
+static void writer_memwrite_finish(struct writer_t*w)
+{
+ free(w->internal);
+}
+
+void writer_resetbits(struct writer_t*w)
{
if(w->bitpos)
w->write(w, &w->mybyte, 1);
w->bitpos = 0;
w->mybyte = 0;
}
-static void writer_init_filewriter(struct writer_t*w, int handle)
+void writer_init_filewriter(struct writer_t*w, int handle)
{
memset(w, 0, sizeof(struct writer_t));
w->write = writer_filewrite_write;
w->bitpos = 0;
w->mybyte = 0;
}
-struct zlibdeflate_t
+void writer_init_memwriter(struct writer_t*w, void*data, int len)
{
- z_stream zs;
- struct writer_t*output;
- U8 writebuffer[ZLIB_BUFFER_SIZE];
-};
-static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len);
-static void writer_zlibdeflate_finish(struct writer_t*writer);
-static void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
+ struct memwrite_t *mr;
+ mr = malloc(sizeof(struct memwrite_t));
+ mr->data = data;
+ mr->length = len;
+ memset(w, 0, sizeof(struct writer_t));
+ w->write = writer_memwrite_write;
+ w->finish = writer_memwrite_finish;
+ w->internal = (void*)mr;
+ w->type = WRITER_TYPE_FILE;
+ w->bitpos = 0;
+ w->mybyte = 0;
+}
+
+void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
{
struct zlibdeflate_t*z;
int ret;
output->finish(output);
}
-static void writer_writebit(struct writer_t*w, int bit)
+void writer_writebit(struct writer_t*w, int bit)
{
if(w->bitpos==8)
{
w->mybyte |= 1 << (7 - w->bitpos);
w->bitpos ++;
}
-static void writer_writebits(struct writer_t*w, U32 data, int bits)
+void writer_writebits(struct writer_t*w, unsigned int data, int bits)
{
int t;
for(t=0;t<bits;t++)
#endif // HAVE_ZLIB_H
#endif // HAVE_LIBZ
+#include "./bitio.h"
+
// internal constants
#define MALLOC_SIZE 128
{
#ifdef DEBUG_RFXSWF
fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
+ *(int*)0=0;
#endif
return 0;
}
return res;
}
-/* reader/writer stuff - from ../src/bitio.c */
-#include "./bitio.c"
-
S32 swf_GetSBits(TAG * t,int nbits)
{ U32 res = swf_GetBits(t,nbits);
if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
return swf_ReadSWF2(&reader, swf);
}
-int swf_WriteSWF2(struct writer_t*writer, SWF * swf, bool compress) // Writes SWF to file, returns length or <0 if fails
+int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
{ U32 len;
TAG * t;
int frameCount=0;
swf->frameCount = frameCount;
}
- if(compress) {
+ if(swf->compressed) {
char*id = "CWS";
writer->write(writer, id, 3);
}
PUT32(b4, swf->fileSize);
writer->write(writer, b4, 4);
- if(compress) {
+ if(swf->compressed) {
writer_init_zlibdeflate(&zwriter, writer);
writer = &zwriter;
}
int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
{
struct writer_t writer;
+ swf->compressed = 0;
if(handle<0)
- return swf_WriteSWF2(&writer, swf, FALSE);
+ return swf_WriteSWF2(&writer, swf);
writer_init_filewriter(&writer, handle);
- return swf_WriteSWF2(&writer, swf, FALSE);
+ return swf_WriteSWF2(&writer, swf);
}
int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
{
struct writer_t writer;
+ swf->compressed = 1;
if(handle<0)
- return swf_WriteSWF2(&writer, swf, TRUE);
+ return swf_WriteSWF2(&writer, swf);
writer_init_filewriter(&writer, handle);
- return swf_WriteSWF2(&writer, swf, TRUE);
+ return swf_WriteSWF2(&writer, swf);
}
+int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
+{
+ SWF myswf;
+ memcpy(&myswf,swf,sizeof(SWF));
+ myswf.firstTag = 0;
+ return swf_WriteSWF2(writer, &myswf);
+}
int swf_WriteHeader(int handle,SWF * swf)
{
#include <fcntl.h>
#include <ctype.h>
#include "../config.h"
+#include "./bitio.h"
#define LAME
#include "lame/lame.h"
// Basic Functions
+int swf_ReadSWF2(struct reader_t*reader, SWF * swf); // Reads SWF via callback
int swf_ReadSWF(int handle,SWF * swf); // Reads SWF to memory (malloc'ed), returns length or <0 if fails
+int swf_WriteSWF2(struct writer_t*writer, SWF * swf); // Writes SWF via callback, returns length or <0 if fails
int swf_WriteSWF(int handle,SWF * swf); // Writes SWF to file, returns length or <0 if fails
-int swf_WriteSWC(int handle, SWF * swf); // Write Compressed SWF to file, returns length or <0 if fails
+int swf_WriteSWC(int handle, SWF * swf); // for convenience, equal to swf->compressed=1;swf_WriteSWF(..)
int swf_WriteCGI(SWF * swf); // Outputs SWF with valid CGI header to stdout
void swf_FreeTags(SWF * swf); // Frees all malloc'ed memory for swf
// for streaming:
int swf_WriteHeader(int handle,SWF * swf); // Writes Header of swf to file
+int swf_WriteHeader2(struct writer_t*writer,SWF * swf); // Writes Header of swf to file
int swf_WriteTag(int handle,TAG * tag); // Writes TAG to file
+int swf_WriteTag2(struct writer_t*writer, TAG * t); //Write TAG via callback
+
+int swf_ReadHeader(struct reader_t*reader, SWF * swf); // Reads SWF Header via callback
// folding/unfolding:
U8 swf_isPseudoDefiningTag(TAG * t);
U8 swf_isAllowedSpriteTag(TAG * t);
U16 swf_GetDefineID(TAG * t);
+void swf_SetDefineID(TAG * t, U16 newid);
U16 swf_GetPlaceID(TAG * t); //PLACEOBJECT, PLACEOBJECT2 (sometimes), REMOVEOBJECT
U16 swf_GetDepth(TAG * t); //PLACEOBJECT,PLACEOBJECT2,REMOVEOBJECT,REMOVEOBJECT2
char* swf_GetName(TAG * t); //PLACEOBJECT2, FRAMELABEL
int x0,int y0,int x1,int y1,int x2,int y2);
int swf_GetNumUsedIDs(TAG * t);
void swf_GetUsedIDs(TAG * t, int * positions);
+void swf_Relocate(SWF*swf, char*bitmap); // bitmap is 65536 bytes, bitmap[a]==0 means id a is free
// swfcgi.c