swfmill 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +23 -0
- data/.swfmill +240 -0
- data/LICENSE +340 -0
- data/README.rdoc +37 -0
- data/Rakefile +59 -0
- data/VERSION +1 -0
- data/ext/.gitignore +5 -0
- data/ext/extconf.rb +86 -0
- data/ext/swfmill/.gitignore +27 -0
- data/ext/swfmill/AUTHORS +9 -0
- data/ext/swfmill/COPYING +340 -0
- data/ext/swfmill/Makefile.am +14 -0
- data/ext/swfmill/Makefile.in +635 -0
- data/ext/swfmill/NEWS +189 -0
- data/ext/swfmill/README +170 -0
- data/ext/swfmill/TODO +33 -0
- data/ext/swfmill/aclocal.m4 +7712 -0
- data/ext/swfmill/autogen.sh +7 -0
- data/ext/swfmill/compile +142 -0
- data/ext/swfmill/config.guess +1516 -0
- data/ext/swfmill/config.sub +1626 -0
- data/ext/swfmill/configure +21868 -0
- data/ext/swfmill/configure.ac +55 -0
- data/ext/swfmill/depcomp +589 -0
- data/ext/swfmill/install-sh +519 -0
- data/ext/swfmill/ltmain.sh +6964 -0
- data/ext/swfmill/missing +367 -0
- data/ext/swfmill/src/Geom.cpp +107 -0
- data/ext/swfmill/src/Geom.h +100 -0
- data/ext/swfmill/src/Makefile.am +86 -0
- data/ext/swfmill/src/Makefile.in +1025 -0
- data/ext/swfmill/src/SWF.h +11941 -0
- data/ext/swfmill/src/SWFAction.cpp +41 -0
- data/ext/swfmill/src/SWFAction.h +19 -0
- data/ext/swfmill/src/SWFBasic.h +7 -0
- data/ext/swfmill/src/SWFFile.cpp +406 -0
- data/ext/swfmill/src/SWFFile.h +34 -0
- data/ext/swfmill/src/SWFFilter.cpp +25 -0
- data/ext/swfmill/src/SWFFilter.h +15 -0
- data/ext/swfmill/src/SWFGlyphList.cpp +262 -0
- data/ext/swfmill/src/SWFGlyphList.h +34 -0
- data/ext/swfmill/src/SWFIdItem.h +85 -0
- data/ext/swfmill/src/SWFIdItems.h +16 -0
- data/ext/swfmill/src/SWFItem.cpp +235 -0
- data/ext/swfmill/src/SWFItem.h +60 -0
- data/ext/swfmill/src/SWFList.h +179 -0
- data/ext/swfmill/src/SWFReader.cpp +352 -0
- data/ext/swfmill/src/SWFReader.h +73 -0
- data/ext/swfmill/src/SWFShapeItem.cpp +220 -0
- data/ext/swfmill/src/SWFShapeItem.h +45 -0
- data/ext/swfmill/src/SWFShapeMaker.cpp +401 -0
- data/ext/swfmill/src/SWFShapeMaker.h +128 -0
- data/ext/swfmill/src/SWFTag.cpp +49 -0
- data/ext/swfmill/src/SWFTag.h +20 -0
- data/ext/swfmill/src/SWFTrait.cpp +35 -0
- data/ext/swfmill/src/SWFTrait.h +22 -0
- data/ext/swfmill/src/SWFWriter.cpp +312 -0
- data/ext/swfmill/src/SWFWriter.h +84 -0
- data/ext/swfmill/src/base64.c +110 -0
- data/ext/swfmill/src/base64.h +15 -0
- data/ext/swfmill/src/codegen/Makefile.am +15 -0
- data/ext/swfmill/src/codegen/Makefile.in +346 -0
- data/ext/swfmill/src/codegen/basic.xsl +45 -0
- data/ext/swfmill/src/codegen/basics.xsl +235 -0
- data/ext/swfmill/src/codegen/dumper.xsl +109 -0
- data/ext/swfmill/src/codegen/header.xsl +131 -0
- data/ext/swfmill/src/codegen/mk.xsl +26 -0
- data/ext/swfmill/src/codegen/parser.xsl +196 -0
- data/ext/swfmill/src/codegen/parsexml.xsl +312 -0
- data/ext/swfmill/src/codegen/size.xsl +189 -0
- data/ext/swfmill/src/codegen/source.xml +2197 -0
- data/ext/swfmill/src/codegen/writer.xsl +190 -0
- data/ext/swfmill/src/codegen/writexml.xsl +138 -0
- data/ext/swfmill/src/swfmill.cpp +482 -0
- data/ext/swfmill/src/swft/Makefile.am +55 -0
- data/ext/swfmill/src/swft/Makefile.in +717 -0
- data/ext/swfmill/src/swft/Parser.cpp +76 -0
- data/ext/swfmill/src/swft/Parser.h +37 -0
- data/ext/swfmill/src/swft/SVGAttributeParser.cpp +78 -0
- data/ext/swfmill/src/swft/SVGAttributeParser.h +35 -0
- data/ext/swfmill/src/swft/SVGColor.cpp +116 -0
- data/ext/swfmill/src/swft/SVGColor.h +37 -0
- data/ext/swfmill/src/swft/SVGColors.h +167 -0
- data/ext/swfmill/src/swft/SVGGradient.cpp +258 -0
- data/ext/swfmill/src/swft/SVGGradient.h +81 -0
- data/ext/swfmill/src/swft/SVGPathParser.cpp +155 -0
- data/ext/swfmill/src/swft/SVGPathParser.h +126 -0
- data/ext/swfmill/src/swft/SVGPointsParser.cpp +51 -0
- data/ext/swfmill/src/swft/SVGPointsParser.h +25 -0
- data/ext/swfmill/src/swft/SVGStyle.cpp +181 -0
- data/ext/swfmill/src/swft/SVGStyle.h +80 -0
- data/ext/swfmill/src/swft/SVGTransformParser.cpp +72 -0
- data/ext/swfmill/src/swft/SVGTransformParser.h +32 -0
- data/ext/swfmill/src/swft/readpng.c +305 -0
- data/ext/swfmill/src/swft/readpng.h +92 -0
- data/ext/swfmill/src/swft/swft.cpp +251 -0
- data/ext/swfmill/src/swft/swft.h +64 -0
- data/ext/swfmill/src/swft/swft_document.cpp +57 -0
- data/ext/swfmill/src/swft/swft_import.cpp +38 -0
- data/ext/swfmill/src/swft/swft_import_binary.cpp +82 -0
- data/ext/swfmill/src/swft/swft_import_jpeg.cpp +255 -0
- data/ext/swfmill/src/swft/swft_import_mp3.cpp +268 -0
- data/ext/swfmill/src/swft/swft_import_png.cpp +231 -0
- data/ext/swfmill/src/swft/swft_import_ttf.cpp +519 -0
- data/ext/swfmill/src/swft/swft_import_wav.cpp +255 -0
- data/ext/swfmill/src/swft/swft_path.cpp +178 -0
- data/ext/swfmill/src/xslt/Makefile.am +51 -0
- data/ext/swfmill/src/xslt/Makefile.in +536 -0
- data/ext/swfmill/src/xslt/README +19 -0
- data/ext/swfmill/src/xslt/assemble.xsl +38 -0
- data/ext/swfmill/src/xslt/simple-deprecated.xslt +62 -0
- data/ext/swfmill/src/xslt/simple-elements.xslt +627 -0
- data/ext/swfmill/src/xslt/simple-import.xslt +565 -0
- data/ext/swfmill/src/xslt/simple-svg.xslt +383 -0
- data/ext/swfmill/src/xslt/simple-tools.xslt +255 -0
- data/ext/swfmill/src/xslt/simple.cpp +1686 -0
- data/ext/swfmill/src/xslt/simple.xml +7 -0
- data/ext/swfmill/src/xslt/xslt.h +7 -0
- data/ext/swfmill/test/Makefile.am +1 -0
- data/ext/swfmill/test/Makefile.in +490 -0
- data/ext/swfmill/test/xml/Makefile.am +20 -0
- data/ext/swfmill/test/xml/Makefile.in +353 -0
- data/ext/swfmill/test/xml/test-xml +21 -0
- data/ext/swfmill_ext.cc +375 -0
- data/lib/swfmill.rb +30 -0
- data/spec/data/swfmill-banner1.swf +0 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/swfmill_spec.rb +125 -0
- metadata +206 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
#include <SWFTag.h>
|
2
|
+
#include "SWF.h"
|
3
|
+
#include <cstring>
|
4
|
+
|
5
|
+
namespace SWF {
|
6
|
+
|
7
|
+
Tag *Tag::get( Reader *r, int end, Context *ctx ) {
|
8
|
+
uint16_t h = r->getWord();
|
9
|
+
int type = h>>6;
|
10
|
+
int len = h&0x3F;
|
11
|
+
if( len == 0x3F ) { // long size
|
12
|
+
len = r->getInt();
|
13
|
+
}
|
14
|
+
|
15
|
+
// sanity check
|
16
|
+
if( len > 100000000 ) {
|
17
|
+
fprintf(stderr,"ridiculous tag size: %i, ignoring.\n", len );
|
18
|
+
// r->setEOF();
|
19
|
+
return NULL;
|
20
|
+
}
|
21
|
+
|
22
|
+
Tag *ret = getByType( type );
|
23
|
+
|
24
|
+
if( !ret ) {
|
25
|
+
ret = new UnknownTag;
|
26
|
+
}
|
27
|
+
|
28
|
+
ret->setType( type );
|
29
|
+
ret->setLength( len );
|
30
|
+
ret->parse( r, r->getPosition()+len, ctx );
|
31
|
+
|
32
|
+
return ret;
|
33
|
+
}
|
34
|
+
|
35
|
+
void Tag::writeHeader( Writer *w, Context *ctx, size_t len ) {
|
36
|
+
// printf("write tag %02X, len %i, recorded %i(+header, 2 or 6), position %i\n", type, len, this->len, w->getPosition() );
|
37
|
+
// shouldnt this be 0x3f - macromedia flash seems to use long length for > 1F...
|
38
|
+
len -= (len >= SWF_LONG_TAG + 6) ? 6 : 2;
|
39
|
+
uint16_t h = type<<6;
|
40
|
+
if( len >= SWF_LONG_TAG ) {
|
41
|
+
h |= 0x3F;
|
42
|
+
w->putWord(h);
|
43
|
+
w->putInt(len);
|
44
|
+
} else {
|
45
|
+
h |= len;
|
46
|
+
w->putWord(h);
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#ifndef SWF_TAG_H
|
2
|
+
#define SWF_TAG_H
|
3
|
+
|
4
|
+
#include <SWFIdItem.h>
|
5
|
+
|
6
|
+
namespace SWF {
|
7
|
+
|
8
|
+
#define SWF_LONG_TAG 8
|
9
|
+
class Tag : public IdItem<Tag> {
|
10
|
+
protected:
|
11
|
+
int getHeaderSize( int size ) { return (size/8)>=SWF_LONG_TAG ? (6*8) : (2*8); }
|
12
|
+
void writeHeader( Writer *w, Context *ctx, size_t len );
|
13
|
+
|
14
|
+
// static stuff
|
15
|
+
public:
|
16
|
+
static Tag* get( Reader *r, int end, Context *ctx );
|
17
|
+
};
|
18
|
+
|
19
|
+
}
|
20
|
+
#endif
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#include <SWFAction.h>
|
2
|
+
#include "SWF.h"
|
3
|
+
#include <cstring>
|
4
|
+
|
5
|
+
namespace SWF {
|
6
|
+
|
7
|
+
Trait *Trait::get( Reader *r, int end, Context *ctx ) {
|
8
|
+
int type = r->getNBitInt( 4 );
|
9
|
+
int len = end - r->getPosition();
|
10
|
+
|
11
|
+
Trait* ret = getByType( type );
|
12
|
+
|
13
|
+
if( !ret ) {
|
14
|
+
ret = handleError( type );
|
15
|
+
}
|
16
|
+
|
17
|
+
if( ret ) {
|
18
|
+
ret->setType( type );
|
19
|
+
ret->setLength( len );
|
20
|
+
ret->parse( r, end, ctx );
|
21
|
+
}
|
22
|
+
|
23
|
+
return ret;
|
24
|
+
}
|
25
|
+
|
26
|
+
void Trait::writeHeader( Writer *w, Context *ctx, size_t len ) {
|
27
|
+
w->putNBitInt( type, 4 );
|
28
|
+
}
|
29
|
+
|
30
|
+
Trait* Trait::handleError( int type ) {
|
31
|
+
fprintf(stderr,"WARNING: unknown trait type 0x%02X\n", type );
|
32
|
+
return NULL;
|
33
|
+
}
|
34
|
+
|
35
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#ifndef SWF_TRAIT_H
|
2
|
+
#define SWF_TRAIT_H
|
3
|
+
|
4
|
+
#include <SWFIdItem.h>
|
5
|
+
|
6
|
+
namespace SWF {
|
7
|
+
|
8
|
+
class Trait : public IdItem<Trait> {
|
9
|
+
protected:
|
10
|
+
int getHeaderSize( int size ) { return 4; }
|
11
|
+
void writeHeader( Writer *w, Context *ctx, size_t len );
|
12
|
+
|
13
|
+
// static stuff
|
14
|
+
public:
|
15
|
+
static Trait* get( Reader *r, int end, Context *ctx );
|
16
|
+
|
17
|
+
protected:
|
18
|
+
static Trait* handleError( int type );
|
19
|
+
};
|
20
|
+
|
21
|
+
}
|
22
|
+
#endif
|
@@ -0,0 +1,312 @@
|
|
1
|
+
#include "SWFWriter.h"
|
2
|
+
#include <cstring>
|
3
|
+
#include <cstdlib>
|
4
|
+
|
5
|
+
namespace SWF {
|
6
|
+
|
7
|
+
Writer::Writer( unsigned char *d, size_t l ) {
|
8
|
+
data = d;
|
9
|
+
length = l;
|
10
|
+
pos = 0;
|
11
|
+
buf = bits = 0;
|
12
|
+
err = 0;
|
13
|
+
}
|
14
|
+
|
15
|
+
Writer::Writer( size_t l ) {
|
16
|
+
data = new unsigned char[l];
|
17
|
+
length = l;
|
18
|
+
pos = 0;
|
19
|
+
buf = bits = 0;
|
20
|
+
err = 0;
|
21
|
+
}
|
22
|
+
|
23
|
+
void Writer::putData( void *src, size_t len ) {
|
24
|
+
byteAlign();
|
25
|
+
if( !assure( len ) ) return;
|
26
|
+
memcpy( &data[pos], src, len );
|
27
|
+
pos += len;
|
28
|
+
}
|
29
|
+
|
30
|
+
void Writer::putByte( uint8_t value ) {
|
31
|
+
byteAlign();
|
32
|
+
writeByte(value);
|
33
|
+
}
|
34
|
+
|
35
|
+
void Writer::putWord( uint16_t value ) {
|
36
|
+
byteAlign();
|
37
|
+
if( !assure( 2 ) ) return;
|
38
|
+
// FIXME x86-centric?
|
39
|
+
data[pos++] = value&0xFF;
|
40
|
+
data[pos++] = value>>8;
|
41
|
+
}
|
42
|
+
|
43
|
+
void Writer::putInt( uint32_t value ) {
|
44
|
+
byteAlign();
|
45
|
+
if( !assure( 4 ) ) return;
|
46
|
+
// FIXME x86-centric?
|
47
|
+
data[pos++] = value&0xFF;
|
48
|
+
data[pos++] = (value>>8)&0xFF;
|
49
|
+
data[pos++] = (value>>16)&0xFF;
|
50
|
+
data[pos++] = (value>>24)&0xFF;
|
51
|
+
}
|
52
|
+
|
53
|
+
void Writer::putInt64( uint64_t value ) {
|
54
|
+
byteAlign();
|
55
|
+
if( !assure( 8 ) ) return;
|
56
|
+
|
57
|
+
putInt( value >> 32 );
|
58
|
+
putInt( value & 0xffffffff );
|
59
|
+
}
|
60
|
+
|
61
|
+
void Writer::putU30( uint32_t value ) {
|
62
|
+
byteAlign();
|
63
|
+
|
64
|
+
int bytes = 1;
|
65
|
+
uint32_t limit = 0x80;
|
66
|
+
for(; value > limit - 1; limit *= 0x80) {
|
67
|
+
bytes++;
|
68
|
+
}
|
69
|
+
|
70
|
+
if( !assure( bytes ) ) return;
|
71
|
+
// FIXME x86-centric?
|
72
|
+
|
73
|
+
for(; bytes > 0; bytes--) {
|
74
|
+
data[pos] = value & 0x7F;
|
75
|
+
|
76
|
+
if(bytes > 1) {
|
77
|
+
value = value >> 7;
|
78
|
+
data[pos] |= 0x80;
|
79
|
+
}
|
80
|
+
pos++;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
void Writer::putS24( int value ) {
|
85
|
+
byteAlign();
|
86
|
+
if( !assure( 3 ) ) return;
|
87
|
+
// FIXME x86-centric?
|
88
|
+
value = (unsigned int)value;
|
89
|
+
data[pos++] = value&0xFF;
|
90
|
+
data[pos++] = (value>>8)&0xFF;
|
91
|
+
data[pos++] = (value>>16)&0xFF;
|
92
|
+
}
|
93
|
+
|
94
|
+
void Writer::putEncodedU32(unsigned int value) {
|
95
|
+
for (size_t i = 0; i < 5; ++i) {
|
96
|
+
if (value < 0x80) {
|
97
|
+
data[pos++] = value;
|
98
|
+
return;
|
99
|
+
}
|
100
|
+
data[pos++] = (value & 0x7f) | 0x80;
|
101
|
+
value >>= 7;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
void Writer::putFloat( float v ) {
|
106
|
+
/*
|
107
|
+
byteAlign();
|
108
|
+
if( !assure( 4 ) ) return;
|
109
|
+
|
110
|
+
// FIXME x86-centric?
|
111
|
+
int value = *((int*)&v);
|
112
|
+
data[pos++] = value&0xFF;
|
113
|
+
data[pos++] = (value>>8)&0xFF;
|
114
|
+
data[pos++] = (value>>16)&0xFF;
|
115
|
+
data[pos++] = (value>>24)&0xFF;
|
116
|
+
*/
|
117
|
+
union {
|
118
|
+
float f;
|
119
|
+
uint32_t ul;
|
120
|
+
} u;
|
121
|
+
u.f = v;
|
122
|
+
putInt(u.ul);
|
123
|
+
}
|
124
|
+
|
125
|
+
void Writer::putDouble( double v ) {
|
126
|
+
union {
|
127
|
+
double d;
|
128
|
+
uint64_t ull;
|
129
|
+
} u;
|
130
|
+
u.d = v;
|
131
|
+
putInt64(u.ull);
|
132
|
+
}
|
133
|
+
|
134
|
+
void Writer::putDouble2( double v ) {
|
135
|
+
union {
|
136
|
+
double d;
|
137
|
+
char c[8];
|
138
|
+
} u;
|
139
|
+
u.d = v;
|
140
|
+
for( int i=0; i < 8; i++ ) {
|
141
|
+
data[pos++] = u.c[i];
|
142
|
+
}
|
143
|
+
}
|
144
|
+
|
145
|
+
void Writer::putHalf( float v ) {
|
146
|
+
union {
|
147
|
+
float f;
|
148
|
+
uint32_t ul;
|
149
|
+
} u;
|
150
|
+
u.f = v;
|
151
|
+
|
152
|
+
int sign = u.ul >> 31;
|
153
|
+
int exp = (u.ul >> 23) & 0xFF;
|
154
|
+
int man = (u.ul & 0x007FFFFF);
|
155
|
+
|
156
|
+
if( exp == 0 ) {
|
157
|
+
man = 0;
|
158
|
+
} else if( exp == 0xFF ) {
|
159
|
+
exp = 0x1F;
|
160
|
+
if( man != 0 ) {
|
161
|
+
man = 1;
|
162
|
+
}
|
163
|
+
} else {
|
164
|
+
exp -= 127;
|
165
|
+
if( exp < -14 ) {
|
166
|
+
if( exp >= -24 ) {
|
167
|
+
man = (man | 0x800000) >> (-exp - 14 + 13);
|
168
|
+
} else {
|
169
|
+
man = 0;
|
170
|
+
}
|
171
|
+
exp = 0;
|
172
|
+
} else if( exp > 15 ) {
|
173
|
+
exp = 0x1F;
|
174
|
+
man = 0;
|
175
|
+
} else {
|
176
|
+
exp += 15;
|
177
|
+
man >>= 13;
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
putWord((sign << 15) | (exp << 10) | man);
|
182
|
+
}
|
183
|
+
|
184
|
+
void Writer::putFixed( double value, int bytesize, int exp ) {
|
185
|
+
/* putFixed/getFixed are deprecated: they implicitly to byteAlign */
|
186
|
+
|
187
|
+
if( !assure( bytesize ) ) return;
|
188
|
+
|
189
|
+
value *= ((double)(1<<exp));
|
190
|
+
uint64_t v = (uint64_t)value;
|
191
|
+
for( int i=0; i<bytesize; i++ ) {
|
192
|
+
data[pos++] = v>>(8*i);
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
void Writer::putString( const char *value ) {
|
197
|
+
byteAlign();
|
198
|
+
int len = strlen(value);
|
199
|
+
if( !assure( len+1 ) ) return;
|
200
|
+
memcpy( &data[pos], value, len+1 );
|
201
|
+
pos += len+1;
|
202
|
+
}
|
203
|
+
|
204
|
+
void Writer::putPString( const char *value ) {
|
205
|
+
byteAlign();
|
206
|
+
int len = strlen(value);
|
207
|
+
if( !assure( len+1 ) ) return;
|
208
|
+
putByte(len);
|
209
|
+
memcpy( &data[pos], value, len );
|
210
|
+
pos += len;
|
211
|
+
}
|
212
|
+
|
213
|
+
void Writer::putPStringU30( const char *value ) {
|
214
|
+
byteAlign();
|
215
|
+
int len = strlen(value);
|
216
|
+
if( !assure( len+1 ) ) return;
|
217
|
+
putU30(len);
|
218
|
+
memcpy( &data[pos], value, len );
|
219
|
+
pos += len;
|
220
|
+
}
|
221
|
+
|
222
|
+
void Writer::putNBitInt( int64_t value, int n, bool is_signed ) {
|
223
|
+
// FIXME: unefficient, maybe incorrect!
|
224
|
+
if( !assure( ((n+bits)/8)+1 ) ) return;
|
225
|
+
|
226
|
+
if( is_signed ) {
|
227
|
+
uint64_t *v = (uint64_t*)&value;
|
228
|
+
uint64_t sign = (1<<(n-1));
|
229
|
+
if( *v & sign ) {
|
230
|
+
value = (*v&(sign-1)) | (1<<(n-1));
|
231
|
+
}
|
232
|
+
}
|
233
|
+
|
234
|
+
// sanity check
|
235
|
+
if( !is_signed || value >= 0 ) {
|
236
|
+
if( value >= (uint64_t)1<<(n+(is_signed?2:1) ) ) {
|
237
|
+
fprintf(stderr,"WARNING: (%ssigned) value %i is too big to be represented in %i bits (max %i)\n", is_signed?"":"un", value, n, 1<<(n+(is_signed?1:0) ) );
|
238
|
+
}
|
239
|
+
} else {
|
240
|
+
if( value < ((uint64_t)-1)<<(n+(is_signed?2:1) ) ) {
|
241
|
+
fprintf(stderr,"WARNING: (%ssigned) value %i is too small to be represented in %i bits (max %i)\n", is_signed?"":"un", value, n, 1<<(n+(is_signed?1:0) ) );
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
if( n==(8-bits) ) {
|
246
|
+
writeByte( buf | value );
|
247
|
+
buf = bits = 0;
|
248
|
+
return;
|
249
|
+
}
|
250
|
+
|
251
|
+
if( n > (8-bits) ) {
|
252
|
+
int bs = n-(8-bits);
|
253
|
+
writeByte( buf | value>>bs );
|
254
|
+
n-=(8-bits);
|
255
|
+
|
256
|
+
while( n>=8 ) {
|
257
|
+
bs-=8;
|
258
|
+
writeByte( value >> bs );
|
259
|
+
n-=8;
|
260
|
+
}
|
261
|
+
|
262
|
+
if( n>0 ) {
|
263
|
+
bits = n;
|
264
|
+
buf = value << (8-bits);
|
265
|
+
} else {
|
266
|
+
bits = buf = 0;
|
267
|
+
}
|
268
|
+
return;
|
269
|
+
}
|
270
|
+
|
271
|
+
// n < (8-bits)
|
272
|
+
bits += n;
|
273
|
+
buf |= value << (8-bits);
|
274
|
+
return;
|
275
|
+
}
|
276
|
+
|
277
|
+
void Writer::putNBitFixed( double v, int n, int m, bool is_signed ) {
|
278
|
+
v *= ((double)(1<<m));
|
279
|
+
putNBitInt( (int64_t)v, n, is_signed );
|
280
|
+
}
|
281
|
+
|
282
|
+
void Writer::putNBitFixed2( double v, int n, int m, bool is_signed ) {
|
283
|
+
if( n%8 != 0 ) {
|
284
|
+
fprintf(stderr,"WARNING: fixedpoint2 needs a size that is divisible by 8");
|
285
|
+
return;
|
286
|
+
}
|
287
|
+
putFixed( v, n / 8, m );
|
288
|
+
}
|
289
|
+
|
290
|
+
void Writer::writeByte( uint8_t value ) {
|
291
|
+
data[pos++] = value;
|
292
|
+
}
|
293
|
+
|
294
|
+
void Writer::dump( int len, int ofs ) {
|
295
|
+
const unsigned char *d = &data[pos+ofs];
|
296
|
+
|
297
|
+
int i=0;
|
298
|
+
printf("DumpingWriter %i - %i\n", pos+ofs, pos+ofs+len );
|
299
|
+
while( i<len ) {
|
300
|
+
for( int n=0; n<4 && i<len; n++ ) {
|
301
|
+
for( int b=7; b>=0; b-- ) {
|
302
|
+
printf( d[i] & (1<<b) ? "1" : "0" );
|
303
|
+
}
|
304
|
+
printf(" 0x%02X %03i ", d[i], d[i] );
|
305
|
+
printf(" ");
|
306
|
+
i++;
|
307
|
+
}
|
308
|
+
printf("\n");
|
309
|
+
}
|
310
|
+
}
|
311
|
+
|
312
|
+
}
|
@@ -0,0 +1,84 @@
|
|
1
|
+
#ifndef SWF_Writer_H
|
2
|
+
#define SWF_Writer_H
|
3
|
+
|
4
|
+
#include <cstdio>
|
5
|
+
#include <stdint.h>
|
6
|
+
#include "SWFBasic.h"
|
7
|
+
|
8
|
+
namespace SWF {
|
9
|
+
|
10
|
+
#define SWFW_OK 0
|
11
|
+
#define SWFW_ERROR 1
|
12
|
+
#define SWFW_FULL 2
|
13
|
+
|
14
|
+
class Writer {
|
15
|
+
public:
|
16
|
+
Writer( unsigned char *data, size_t length );
|
17
|
+
Writer( size_t length );
|
18
|
+
|
19
|
+
// these implicitly do byteAlign()
|
20
|
+
void putData( void *src, size_t length );
|
21
|
+
void putByte(uint8_t);
|
22
|
+
void putWord(uint16_t);
|
23
|
+
void putInt(uint32_t);
|
24
|
+
void putInt64(uint64_t);
|
25
|
+
void putU30(uint32_t);
|
26
|
+
void putS24(int);
|
27
|
+
void putFloat(float);
|
28
|
+
void putDouble(double);
|
29
|
+
void putDouble2(double);
|
30
|
+
void putHalf(float);
|
31
|
+
void putFixed( double v, int bytesize, int decimals );
|
32
|
+
void putString( const char * );
|
33
|
+
void putPString( const char * );
|
34
|
+
void putPStringU30( const char * );
|
35
|
+
void putEncodedU32(unsigned int);
|
36
|
+
|
37
|
+
// this one cares about the current bit position
|
38
|
+
void putNBitInt( int64_t v, int n, bool is_signed = false );
|
39
|
+
void putNBitFixed( double v, int n, int m, bool is_signed = false );
|
40
|
+
void putNBitFixed2( double v, int n, int m, bool is_signed = false );
|
41
|
+
|
42
|
+
// set bit position to next byte.
|
43
|
+
void byteAlign() {
|
44
|
+
if( bits ) writeByte( buf );
|
45
|
+
buf = 0; bits=0;
|
46
|
+
}
|
47
|
+
|
48
|
+
int getError() { return err; }
|
49
|
+
|
50
|
+
void dump( int n, int ofs ); // dumps n bytes from current position to stdout
|
51
|
+
bool assure( int l, int before=-1 ) {
|
52
|
+
int b = (before==-1?length:before);
|
53
|
+
if( pos+l > b ) {
|
54
|
+
fprintf(stderr,"Write buffer full (%i+%i >= %i)\n", pos, l, b );
|
55
|
+
err = SWFW_FULL;
|
56
|
+
return false;
|
57
|
+
}
|
58
|
+
return true;
|
59
|
+
}
|
60
|
+
|
61
|
+
size_t getLength() { return length; }
|
62
|
+
unsigned char *getData() { return data; }
|
63
|
+
int getPosition() { return pos; }
|
64
|
+
int getBits() { return bits; }
|
65
|
+
int getBitPosition() { return (pos*8)+( bits ); }
|
66
|
+
|
67
|
+
protected:
|
68
|
+
void writeByte( uint8_t value );
|
69
|
+
|
70
|
+
// the uncompressed swf data and our position within
|
71
|
+
unsigned char *data;
|
72
|
+
int pos, length;
|
73
|
+
|
74
|
+
// buf is the current byte when in "NBit mode"
|
75
|
+
// bits is the number of bits represented in buf.
|
76
|
+
uint8_t buf, bits;
|
77
|
+
|
78
|
+
int err;
|
79
|
+
|
80
|
+
bool write( void *dst, size_t n );
|
81
|
+
};
|
82
|
+
|
83
|
+
}
|
84
|
+
#endif
|