-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWFWriter.cpp
171 lines (140 loc) · 3.24 KB
/
SWFWriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* File: SWFWriter.cpp
* Author: brucewang
*
* Created on October 27, 2009, 1:37 AM
*/
#include "SWFWriter.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
SWFWriter::SWFWriter() {
mBuffer = (byte*)malloc(SWFWRITER_BUFSIZE);
memset( mBuffer, 0, SWFWRITER_BUFSIZE );
mBytePos = mBitPos = 0;
}
SWFWriter::~SWFWriter() {
free(mBuffer);
}
#if 0
bool SWFWriter::pushByte(byte b){
if(mBitPos>0) mBytePos++;
mBuffer[mBytePos] = b;
mBytePos++;
mBitPos=0;
return true;
}
bool SWFWriter::pushBit(bool b){
if(mBitPos>7){
mBytePos++;
mBitPos=0;
}
if(b){
byte currentb = mBuffer[mBytePos];
byte bnew = (byte)( 1 << (7-mBitPos) );
mBuffer[mBytePos] = currentb | bnew;
}
mBitPos++;
return true;
}
#else
bool SWFWriter::pushByte(byte b){
if(mBitPos>0) mBytePos++;
mBuffer[mBytePos] = b;
mBytePos++;
mBitPos=0;
return true;
}
bool SWFWriter::pushBit(bool b){
if(b){
byte currentb = mBuffer[mBytePos];
byte bnew = (byte)( 1 << (7-mBitPos) );
mBuffer[mBytePos] = currentb | bnew;
}
mBitPos++;
if(mBitPos>7){
mBytePos++;
mBitPos=0;
}
return true;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// WRITE
////////////////////////////////////////////////////////////////////////////////
void SWFWriter::WriteByte(byte data){
pushByte(data);
}
void SWFWriter::WriteUI8(byte data){
WriteByte(data);
}
void SWFWriter::WriteUI8(byte* data, int n){
for( int i=0; i<n; i++ ){
WriteByte(data[i]);
}
}
void SWFWriter::WriteSI8(sbyte data){
return WriteByte(data);
}
void SWFWriter::WriteUI16(UInt16 data){
byte b;
#if 0 //for BigEndian CPUs
b = (byte)(data>>8); WriteByte(b);
b = (byte)(data & 0x00ff); WriteByte(b);
#else
b = (byte)(data & 0x00ff); WriteByte(b);
b = (byte)(data>>8); WriteByte(b);
#endif
}
void SWFWriter::WriteSI16(Int16 data){
WriteUI16(data);
}
void SWFWriter::WriteUI32(UInt32 data){
byte b;
#if 0 //for BigEndian CPUs
b = (byte)(data>>24); WriteByte(b);
b = (byte)(data>>16); WriteByte(b);
b = (byte)(data>>8); WriteByte(b);
b = (byte)(data & 0x000000ff); WriteByte(b);
#else
b = (byte)(data & 0x000000ff); WriteByte(b);
b = (byte)(data>>8); WriteByte(b);
b = (byte)(data>>16); WriteByte(b);
b = (byte)(data>>24); WriteByte(b);
#endif
}
void SWFWriter::WriteSI32(UInt32 data){
return WriteUI32(data);
}
void SWFWriter::WriteSTRING(const szstring data){
int iwritten=0;
while( data[iwritten]!= 0 ){
WriteByte( data[iwritten] );
iwritten++;
}
// Terminating NULL
WriteByte(0);
}
void SWFWriter::WriteUB(UInt32 data, int nBits){
bool b;
for(int i=nBits-1; i>-1; i--){
b = 0x00000001 & (data>>i);
this->pushBit(b);
}
}
void SWFWriter::WriteSB(Int32 data, int nBits) {
if( data<0 )
data = (1<<nBits)+data;
return WriteUB(data, nBits);
}
bool SWFWriter::SaveFile(char* file){
bool bReturn=false;
FILE* fp = fopen(file, "wb");
if(fp){
fwrite(mBuffer, 1, mBytePos, fp);
fclose(fp);
bReturn = true;
}
return bReturn;
}