-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswfparse.cpp
3675 lines (2972 loc) · 97.2 KB
/
swfparse.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SWF file parser.
//
// I uploaded this file *as is* while I was in the middle
// of debugging something (so its a bit of a mess) but still,
// it fixes most (all?) of the bugs in the original parser
// and includes quite a bit more functionality.
//
// It also uses the Zlib stuff in ParseDefineBitsLossless()
// so you can either comment that out or grab the Zlib source
// and link it in.
//
// Cheers, David.
//
// Change History:
//
// 99.07.12: Uploaded first version (as is)
// 99.07.22: Added code to parse Flash 4 ActionScripts,
// and switches to dump image/sound/tag data
// Thanks to Jason Schuchert!
// 99.07.28: More flash 4 actionscript.
// Added GetUrl2 (thanks Olivier)
// Fixed callFrame (there's no arguments to print)
// Added WaitForFrameExpression
// - Jason Schuchert
// 99.10.21: Now parses streaming sound and MP3 headers
// - David Michie
// 99.10.28: Now parses DefineMorphShape and correctly
// interprets FLOAT values with ActionPush
// - David Michie
// 99.11.02: Now parses DefineFont2 and DefineEditText
// - David Michie
//
//////////////////////////////////////////////////////////////////////
//!!@ dump sound data
#include <stdio.h>
#include <string.h>
void usage( void )
{
fprintf(stderr, "usage: swfdump [options] inputFile\n");
fprintf(stderr, " -a dumps all data (tag, image, sound)\n" );
fprintf(stderr, " -i dumps image data\n" );
fprintf(stderr, " -s dumps sound data\n" );
fprintf(stderr, " -t dumps tag data\n" );
}
extern "C"
{
#include "zlib.h"
}
#if defined(_DEBUG) && defined(_WIN32)
#include <windows.h>
#define DEBUG_ASSERT DebugBreak()
#else
#define DEBUG_ASSERT
typedef unsigned long BOOL;
#endif
// Global Types
typedef unsigned long U32, *P_U32, **PP_U32;
typedef signed long S32, *P_S32, **PP_S32;
typedef unsigned short U16, *P_U16, **PP_U16;
typedef signed short S16, *P_S16, **PP_S16;
typedef unsigned char U8, *P_U8, **PP_U8;
typedef signed char S8, *P_S8, **PP_S8;
typedef signed long SFIXED, *P_SFIXED;
typedef signed long SCOORD, *P_SCOORD;
typedef struct SPOINT
{
SCOORD x;
SCOORD y;
} SPOINT, *P_SPOINT;
typedef struct SRECT
{
SCOORD xmin;
SCOORD xmax;
SCOORD ymin;
SCOORD ymax;
} SRECT, *P_SRECT;
// Start Sound Flags
enum {
soundHasInPoint = 0x01,
soundHasOutPoint = 0x02,
soundHasLoops = 0x04,
soundHasEnvelope = 0x08
// the upper 4 bits are reserved for synchronization flags
};
enum {
fillGradient = 0x10,
fillLinearGradient = 0x10,
fillRadialGradient = 0x12,
fillMaxGradientColors = 8,
// Texture/bitmap fills
fillBits = 0x40 // if this bit is set, must be a bitmap pattern
};
// Flags for defining a shape character
enum {
// These flag codes are used for state changes - and as return values from ShapeParser::GetEdge()
eflagsMoveTo = 0x01,
eflagsFill0 = 0x02,
eflagsFill1 = 0x04,
eflagsLine = 0x08,
eflagsNewStyles = 0x10,
eflagsEnd = 0x80 // a state change with no change marks the end
};
enum FontFlags
{
fontUnicode = 0x20,
fontShiftJIS = 0x10,
fontANSI = 0x08,
fontItalic = 0x04,
fontBold = 0x02,
fontWideCodes = 0x01
};
// Edit text field flags
enum
{
sfontFlagsBold = 0x01,
sfontFlagsItalic = 0x02,
sfontFlagsWideCodes = 0x04,
sfontFlagsWideOffsets = 0x08,
sfontFlagsANSI = 0x10,
sfontFlagsUnicode = 0x20,
sfontFlagsShiftJIS = 0x40,
sfontFlagsHasLayout = 0x80
};
// Edit Text Flags
enum {
seditTextFlagsHasFont = 0x0001,
seditTextFlagsHasMaxLength = 0x0002,
seditTextFlagsHasTextColor = 0x0004,
seditTextFlagsReadOnly = 0x0008,
seditTextFlagsPassword = 0x0010,
seditTextFlagsMultiline = 0x0020,
seditTextFlagsWordWrap = 0x0040,
seditTextFlagsHasText = 0x0080,
seditTextFlagsUseOutlines = 0x0100,
seditTextFlagsBorder = 0x0800,
seditTextFlagsNoSelect = 0x1000,
seditTextFlagsHasLayout = 0x2000
};
enum TextFlags
{
isTextControl = 0x80,
textHasFont = 0x08,
textHasColor = 0x04,
textHasYOffset= 0x02,
textHasXOffset= 0x01
};
typedef struct MATRIX
{
SFIXED a;
SFIXED b;
SFIXED c;
SFIXED d;
SCOORD tx;
SCOORD ty;
} MATRIX, *P_MATRIX;
typedef struct CXFORM
{
/*
S32 flags;
enum
{
needA=0x1, // Set if we need the multiply terms.
needB=0x2 // Set if we need the constant terms.
};
*/
S16 aa, ab; // a is multiply factor, b is addition factor
S16 ra, rb;
S16 ga, gb;
S16 ba, bb;
}
CXFORM, *P_CXFORM;
#ifndef NULL
#define NULL 0
#endif
// Tag values that represent actions or data in a Flash script.
enum
{
stagEnd = 0,
stagShowFrame = 1,
stagDefineShape = 2,
stagFreeCharacter = 3,
stagPlaceObject = 4,
stagRemoveObject = 5,
stagDefineBits = 6,
stagDefineButton = 7,
stagJPEGTables = 8,
stagSetBackgroundColor = 9,
stagDefineFont = 10,
stagDefineText = 11,
stagDoAction = 12,
stagDefineFontInfo = 13,
stagDefineSound = 14, // Event sound tags.
stagStartSound = 15,
stagDefineButtonSound = 17,
stagSoundStreamHead = 18,
stagSoundStreamBlock = 19,
stagDefineBitsLossless = 20, // A bitmap using lossless zlib compression.
stagDefineBitsJPEG2 = 21, // A bitmap using an internal JPEG compression table.
stagDefineShape2 = 22,
stagDefineButtonCxform = 23,
stagProtect = 24, // This file should not be importable for editing.
// These are the new tags for Flash 3.
stagPlaceObject2 = 26, // The new style place w/ alpha color transform and name.
stagRemoveObject2 = 28, // A more compact remove object that omits the character tag (just depth).
stagDefineShape3 = 32, // A shape V3 includes alpha values.
stagDefineText2 = 33, // A text V2 includes alpha values.
stagDefineButton2 = 34, // A button V2 includes color transform, alpha and multiple actions
stagDefineBitsJPEG3 = 35, // A JPEG bitmap with alpha info.
stagDefineBitsLossless2 = 36, // A lossless bitmap with alpha info.
stagDefineEditText = 37, // An editable Text Field
stagDefineSprite = 39, // Define a sequence of tags that describe the behavior of a sprite.
stagNameCharacter = 40, // Name a character definition, character id and a string, (used for buttons, bitmaps, sprites and sounds).
stagFrameLabel = 43, // A string label for the current frame.
stagSoundStreamHead2 = 45, // For lossless streaming sound, should not have needed this...
stagDefineMorphShape = 46, // A morph shape definition
stagDefineFont2 = 48, //
};
// PlaceObject2 Flags
enum
{
splaceMove = 0x01, // this place moves an exisiting object
splaceCharacter = 0x02, // there is a character tag (if no tag, must be a move)
splaceMatrix = 0x04, // there is a matrix (matrix)
splaceColorTransform= 0x08, // there is a color transform (cxform with alpha)
splaceRatio = 0x10, // there is a blend ratio (word)
splaceName = 0x20, // there is an object name (string)
splaceDefineClip = 0x40 // this shape should open or close a clipping bracket (character != 0 to open, character == 0 to close)
// one bit left for expansion
};
// Action codes
enum
{
sactionNone = 0x00,
sactionNextFrame = 0x04,
sactionPrevFrame = 0x05,
sactionPlay = 0x06,
sactionStop = 0x07,
sactionToggleQuality = 0x08,
sactionStopSounds = 0x09,
sactionAdd = 0x0A,
sactionSubtract = 0x0B,
sactionMultiply = 0x0C,
sactionDivide = 0x0D,
sactionEqual = 0x0E,
sactionLessThan = 0x0F,
sactionLogicalAnd = 0x10,
sactionLogicalOr = 0x11,
sactionLogicalNot = 0x12,
sactionStringEqual = 0x13,
sactionStringLength = 0x14,
sactionSubString = 0x15,
sactionInt = 0x18,
sactionEval = 0x1C,
sactionSetVariable = 0x1D,
sactionSetTargetExpression = 0x20,
sactionStringConcat = 0x21,
sactionGetProperty = 0x22,
sactionSetProperty = 0x23,
sactionDuplicateClip = 0x24,
sactionRemoveClip = 0x25,
sactionTrace = 0x26,
sactionStartDragMovie = 0x27,
sactionStopDragMovie = 0x28,
sactionStringLessThan = 0x29,
sactionRandom = 0x30,
sactionMBLength = 0x31,
sactionOrd = 0x32,
sactionChr = 0x33,
sactionGetTimer = 0x34,
sactionMBSubString = 0x35,
sactionMBOrd = 0x36,
sactionMBChr = 0x37,
sactionHasLength = 0x80,
sactionGotoFrame = 0x81, // frame num (WORD)
sactionGetURL = 0x83, // url (STR), window (STR)
sactionWaitForFrame = 0x8A, // frame needed (WORD),
// actions to skip (BYTE)
sactionSetTarget = 0x8B, // name (STR)
sactionGotoLabel = 0x8C, // name (STR)
sactionWaitForFrameExpression = 0x8D, // frame needed on stack,
// actions to skip (BYTE)
sactionPushData = 0x96,
sactionBranchAlways = 0x99,
sactionGetURL2 = 0x9A,
sactionBranchIfTrue = 0x9D,
sactionCallFrame = 0x9E,
sactionGotoExpression = 0x9F
};
//////////////////////////////////////////////////////////////////////
// Input script object definition.
//////////////////////////////////////////////////////////////////////
// An input script object. This object represents a script created from
// an external file that is meant to be inserted into an output script.
struct CInputScript
{
// Pointer to file contents buffer.
U8 *m_fileBuf;
// File state information.
U32 m_filePos;
U32 m_fileSize;
U32 m_fileStart;
U16 m_fileVersion;
// Bit Handling
S32 m_bitPos;
U32 m_bitBuf;
// Tag parsing information.
U32 m_tagStart;
U32 m_tagZero;
U32 m_tagEnd;
U32 m_tagLen;
// Parsing information.
S32 m_nFillBits;
S32 m_nLineBits;
// Set to true if we wish to dump all contents long form
U32 m_dumpAll;
// if set to true will dump image guts (i.e. jpeg, zlib, etc. data)
U32 m_dumpGuts;
// if set to true will dump sound guts
U32 m_dumpSoundGuts;
// Handle to output file.
FILE *m_outputFile;
// Font glyph counts (gotta save it somewhere!)
int m_iGlyphCounts[256];
U8* m_srcAdpcm;
U32 m_bitBufAdpcm; // this should always contain at least 24 bits of data
S32 m_bitPosAdpcm;
U32 m_nSamplesAdpcm; // number of samples decompressed so far
// Streaming sound info from SoundStreamHead tag
int m_iStreamCompression;
int m_iStreamSampleRate;
int m_iStreamSampleSize;
int m_iStreamStereoMono;
int m_nStreamSamples;
// Constructor/destructor.
CInputScript();
~CInputScript();
// Tag scanning methods.
U16 GetTag(void);
void SkipBytes(int n);
U8 GetByte(void);
U16 GetWord(void);
U32 GetDWord(void);
void GetRect(SRECT *r);
void GetMatrix(MATRIX *matrix);
void GetCxform(CXFORM *cxform, BOOL hasAlpha);
void PrintCxform(char* str, CXFORM *cxform);
char *GetString(void);
U32 GetColor(BOOL fWithAlpha=false);
void PrintMatrix(MATRIX matrix, char* str);
void PrintRect(SRECT rect, char* str);
// Routines for reading arbitrary sized bit fields from the stream.
// Always call start bits before gettings bits and do not intermix
// these calls with GetByte, etc...
void InitBits();
S32 GetSBits(S32 n);
U32 GetBits(S32 n);
void where();
// Tag subcomponent parsing methods
// For shapes
void ParseShapeStyle(char *str, BOOL fWithAlpha=false);
BOOL ParseShapeRecord(char *str, int& xLast, int& yLast, BOOL fWithAlpha=false);
void ParseButtonRecord(char *str, U32 byte, BOOL fGetColorMatrix=true);
BOOL ParseTextRecord(char* str, int nGlyphBits, int nAdvanceBits);
// Parsing methods.
void ParseEnd(char *str); // 00: stagEnd
void ParseShowFrame(char *str, U32 frame, U32 offset); // 01: stagShowFrame
void ParseDefineShape(char *str, BOOL fWithAlpha=false);// 02: stagDefineShape
void ParseFreeCharacter(char *str); // 03: stagFreeCharacter
void ParsePlaceObject(char *str); // 04: stagPlaceObject
void ParseRemoveObject(char *str); // 05: stagRemoveObject
void ParseDefineBits(char *str); // 06: stagDefineBits
void ParseDefineButton(char *str); //x 07: stagDefineButton
void ParseJPEGTables(char *str); // 08: stagJPEGTables
void ParseSetBackgroundColor(char *str); // 09: stagSetBackgroundColor
void ParseDefineFont(char *str); //x 10: stagDefineFont
void ParseDefineText(char *str); //x 11: stagDefineText
void ParseDoAction(char *str, BOOL fPrintTag=true); // 12: stagDoAction
void ParseDefineFontInfo(char *str); //x 13: stagDefineFontInfo
void ParseDefineSound(char *str); // 14: stagDefineSound
void ParseStartSound(char *str); // 15: stagStartSound
void ParseStopSound(char *str); // 16: stagStopSound
void ParseDefineButtonSound(char *str); // 17: stagDefineButtonSound
void ParseSoundStreamHead(char *str); // 18: stagSoundStreamHead
void ParseSoundStreamBlock(char *str); // 19: stagSoundStreamBlock
void ParseDefineBitsLossless(char *str); // 20: stagDefineBitsLossless
void ParseDefineBitsJPEG2(char *str); // 21: stagDefineBitsJPEG2
void ParseDefineShape2(char *str); //x 22: stagDefineShape2
void ParseDefineButtonCxform(char *str); // 23: stagDefineButtonCxform
void ParseProtect(char *str); // 24: stagProtect
void ParsePlaceObject2(char *str); // 26: stagPlaceObject2
void ParseRemoveObject2(char *str); // 28: stagRemoveObject2
void ParseDefineShape3(char *str); //x 32: stagDefineShape3
void ParseDefineText2(char *str); //x 33: stagDefineText2
void ParseDefineButton2(char *str); //x 34: stagDefineButton2
void ParseDefineBitsJPEG3(char *str); // 35: stagDefineBitsJPEG3
void ParseDefineBitsLossless2(char *str); // 36: stagDefineBitsLossless2
void ParseDefineEditText(char *str); // 37: stagDefineEditText
void ParseDefineMouseTarget(char *str); // 38: stagDefineMouseTarget
void ParseDefineSprite(char *str); //x 39: stagDefineSprite
void ParseNameCharacter(char *str); // 40: stagNameCharacter
void ParseFrameLabel(char *str); // 43: stagFrameLabel
void ParseSoundStreamHead2(char *str, BOOL fIsHead2=true); // 45: stagSoundStreamHead2
void ParseDefineMorphShape(char *str); //x 46: stagDefineMorphShape
void ParseDefineFont2(char *str); //x 48: stagDefineFont2
void ParseUnknown(char *str, U16 code);
void ParseTags(BOOL sprite, U32 tabs);
BOOL ParseFile(char * pInput);
void S_DumpImageGuts(char *str);
// ADPCM stuff
void AdpcmFillBuffer();
long AdpcmGetBits(int n);
long AdpcmGetSBits(int n);
void AdpcmDecompress(long n, long stereo, int if16bit, U8 *dst=NULL);
// MP3 stuff
void DecodeMp3Headers(char* str, int iSamplesPerFrame);
void DecodeMp3Frame(U8* pbFrame, int iEncodedSize, int iDecodedSize);
};
#define INDENT printf(" ");
//////////////////////////////////////////////////////////////////////
// Inline input script object methods.
//////////////////////////////////////////////////////////////////////
//
// Inlines to parse a Flash file.
//
inline void CInputScript::SkipBytes(int n)
{
m_filePos += n;
}
inline U8 CInputScript::GetByte(void)
{
//printf("GetByte: filePos: %02x [%02x]\n", m_filePos, m_fileBuf[m_filePos]);
InitBits();
return m_fileBuf[m_filePos++];
}
inline U16 CInputScript::GetWord(void)
{
//printf("GetWord: filePos: %02x\n", m_filePos);
U8* s = m_fileBuf + m_filePos;
m_filePos += 2;
InitBits();
return (U16) s[0] | ((U16) s[1] << 8);
}
inline U32 CInputScript::GetDWord(void)
{
//printf("GetDWord: filePos: %02x\n", m_filePos);
U8 * s = m_fileBuf + m_filePos;
m_filePos += 4;
InitBits();
return (U32) s[0] | ((U32) s[1] << 8) | ((U32) s[2] << 16) | ((U32) s [3] << 24);
}
void CInputScript::PrintMatrix(MATRIX matrix, char* str)
{
printf("%s\t[%5.3f %5.3f]\n", str, (double)matrix.a/65536.0, (double)matrix.b/65536.0);
printf("%s\t[%5.3f %5.3f]\n", str, (double)matrix.c/65536.0, (double)matrix.d/65536.0);
printf("%s\t[%5.3f %5.3f]\n", str, (double)matrix.tx/20.0, (double)matrix.ty/20.0);
/*
printf("%s\t[%08x %08x]\n", str, matrix.a, matrix.b);
printf("%s\t[%08x %08x]\n", str, matrix.c, matrix.d);
printf("%s\t[%d %d]\n", str, matrix.tx, matrix.ty);
*/
}
void CInputScript::PrintRect(SRECT rect, char* str)
{
printf("\t%s(%g, %g)[%g x %g]\n", str,
(double)rect.xmin / 20.0, (double)rect.ymin / 20.0,
(double)(rect.xmax - rect.xmin) / 20.0,
(double)(rect.ymax - rect.ymin) / 20.0);
}
void CInputScript::where()
{
printf("where: %04x [%02x]\n", m_filePos, m_fileBuf[m_filePos]);
}
//////////////////////////////////////////////////////////////////////
// Input script object methods.
//////////////////////////////////////////////////////////////////////
CInputScript::CInputScript(void)
// Class constructor.
{
// Initialize the input pointer.
m_fileBuf = NULL;
// Initialize the file information.
m_filePos = 0;
m_fileSize = 0;
m_fileStart = 0;
m_fileVersion = 0;
// Initialize the bit position and buffer.
m_bitPos = 0;
m_bitBuf = 0;
// Initialize the output file.
m_outputFile = NULL;
// Set to true if we wish to dump all contents long form
m_dumpAll = false;
// if set to true will dump image guts (i.e. jpeg, zlib, etc. data)
m_dumpGuts = false;
// if set to true will dump sound guts
m_dumpSoundGuts = false;
return;
}
CInputScript::~CInputScript(void)
// Class destructor.
{
// Free the buffer if it is there.
if (m_fileBuf)
{
delete m_fileBuf;
m_fileBuf = NULL;
m_fileSize = 0;
}
}
U16 CInputScript::GetTag(void)
{
// Save the start of the tag.
m_tagStart = m_filePos;
m_tagZero = m_tagStart;
// Get the combined code and length of the tag.
U16 wRawCode = GetWord();
U16 code = wRawCode;
// The length is encoded in the tag.
U32 len = code & 0x3f;
// Remove the length from the code.
code = code >> 6;
// Determine if another long word must be read to get the length.
if (len == 0x3f)
{
len = (U32) GetDWord();
//printf("\nGetTag: long tag: raw-code: %04x len: 0x%08x\n", wRawCode, len);
m_tagZero += 4;
}
//printf("->> GetTag: code:%04x len:%08x\n", code, len);
// Determine the end position of the tag.
m_tagEnd = m_filePos + (U32) len;
m_tagLen = (U32) len;
return code;
}
void CInputScript::GetRect (SRECT * r)
{
InitBits();
int nBits = (int) GetBits(5);
r->xmin = GetSBits(nBits);
r->xmax = GetSBits(nBits);
r->ymin = GetSBits(nBits);
r->ymax = GetSBits(nBits);
}
void CInputScript::GetMatrix(MATRIX* mat)
{
InitBits();
// Scale terms
if (GetBits(1))
{
int nBits = (int) GetBits(5);
mat->a = GetSBits(nBits);
mat->d = GetSBits(nBits);
}
else
{
mat->a = mat->d = 0x00010000L;
}
// Rotate/skew terms
if (GetBits(1))
{
int nBits = (int)GetBits(5);
mat->b = GetSBits(nBits);
mat->c = GetSBits(nBits);
}
else
{
mat->b = mat->c = 0;
}
// Translate terms
int nBits = (int) GetBits(5);
mat->tx = GetSBits(nBits);
mat->ty = GetSBits(nBits);
}
void CInputScript::GetCxform(CXFORM* cx, BOOL hasAlpha)
{
InitBits();
// !!! The spec has these bits reversed !!!
BOOL fNeedAdd = (GetBits(1) != 0);
BOOL fNeedMul = (GetBits(1) != 0);
// !!! The spec has these bits reversed !!!
//printf("fNeedMul:%d fNeedAdd:%d\n", fNeedMul, fNeedAdd);
int nBits = (int) GetBits(4);
cx->aa = 256; cx->ab = 0;
if (fNeedMul)
{
cx->ra = (S16) GetSBits(nBits);
cx->ga = (S16) GetSBits(nBits);
cx->ba = (S16) GetSBits(nBits);
if (hasAlpha) cx->aa = (S16) GetSBits(nBits);
}
else
{
cx->ra = cx->ga = cx->ba = 256;
}
if (fNeedAdd)
{
cx->rb = (S16) GetSBits(nBits);
cx->gb = (S16) GetSBits(nBits);
cx->bb = (S16) GetSBits(nBits);
if (hasAlpha) cx->ab = (S16) GetSBits(nBits);
}
else
{
cx->rb = cx->gb = cx->bb = 0;
}
}
void CInputScript::PrintCxform(char* str, CXFORM* pCxform)
{
printf("%sCXFORM:\n", str);
printf("%sAlpha: mul:%04u add:%04u\n",str, pCxform->aa, pCxform->ab);
printf("%sRed: mul:%04u add:%04u\n",str, pCxform->ra, pCxform->rb);
printf("%sGreen: mul:%04u add:%04u\n",str, pCxform->ga, pCxform->gb);
printf("%sBlue: mul:%04u add:%04u\n",str, pCxform->ba, pCxform->bb);
}
char *CInputScript::GetString(void)
{
// Point to the string.
char *str = (char *) &m_fileBuf[m_filePos];
// Skip over the string.
while (GetByte());
return str;
}
U32 CInputScript::GetColor(BOOL fWithAlpha)
{
U32 r = GetByte();
U32 g = GetByte();
U32 b = GetByte();
U32 a = 0xff;
if (fWithAlpha)
a = GetByte();
return (a << 24) | (r << 16) | (g << 8) | b;
}
void CInputScript::InitBits(void)
{
// Reset the bit position and buffer.
m_bitPos = 0;
m_bitBuf = 0;
}
S32 CInputScript::GetSBits(S32 n)
// Get n bits from the string with sign extension.
{
// Get the number as an unsigned value.
S32 v = (S32) GetBits(n);
// Is the number negative?
if (v & (1L << (n - 1)))
{
// Yes. Extend the sign.
v |= -1L << n;
}
return v;
}
U32 CInputScript::GetBits (S32 n)
// Get n bits from the stream.
{
U32 v = 0;
while (true)
{
//if (m_bitPos == 0)
// printf("bitPos is ZERO: m_bitBuf: %02x\n", m_bitBuf);
S32 s = n - m_bitPos;
if (s > 0)
{
// Consume the entire buffer
v |= m_bitBuf << s;
n -= m_bitPos;
// Get the next buffer
m_bitBuf = GetByte();
m_bitPos = 8;
}
else
{
// Consume a portion of the buffer
v |= m_bitBuf >> -s;
m_bitPos -= n;
m_bitBuf &= 0xff >> (8 - m_bitPos); // mask off the consumed bits
//printf("GetBits: nBitsToRead:%d m_bitPos:%d m_bitBuf:%02x v:%d\n", nBitsToRead, m_bitPos, m_bitBuf, v);
return v;
}
}
}
void CInputScript::ParseEnd(char *str)
{
printf("%stagEnd\n", str);
}
void CInputScript::ParseShowFrame(char *str, U32 frame, U32 offset)
{
printf("%stagShowFrame\n", str);
printf("\n%s<----- dumping frame %d file offset 0x%04x ----->\n", str, frame + 1, offset);
}
void CInputScript::ParseFreeCharacter(char *str)
{
U32 tagid = (U32) GetWord();
printf("%stagFreeCharacter \ttagid %-5u\n", str, tagid);
}
void CInputScript::ParsePlaceObject(char *str)
{
U32 tagid = (U32) GetWord();
U32 depth = (U32) GetWord();
printf("%stagPlaceObject \ttagid %-5u depth %-5u\n", str, tagid, depth);
if (!m_dumpAll)
return;
MATRIX matrix;
GetMatrix(&matrix);
PrintMatrix(matrix, str);
if (m_filePos < m_tagEnd)
{
CXFORM cxform;
GetCxform(&cxform, false);
PrintCxform(str, &cxform);
}
}
void CInputScript::ParsePlaceObject2(char *str)
{
U8 flags = GetByte();
U32 depth = GetWord();
printf("%stagPlaceObject2 \tflags %-5u depth %-5u ", str, (int) flags, (int) depth);
if ( flags & splaceMove )
printf("move ");
// Get the tag if specified.
if (flags & splaceCharacter)
{
U32 tag = GetWord();
printf("tag %-5u\n", tag);
}
else
{
printf("\n");
}
// Get the matrix if specified.
if (flags & splaceMatrix)
{
// this one gets called
MATRIX matrix;
GetMatrix(&matrix);
PrintMatrix(matrix, str);
}
// Get the color transform if specified.
if (flags & splaceColorTransform)
{
CXFORM cxform;
GetCxform(&cxform, true);
PrintCxform(str, &cxform);
}
// Get the ratio if specified.
if (flags & splaceRatio)
{
U32 ratio = GetWord();
INDENT;
printf("%ratio %u\n", ratio);
}
// Get the clipdepth if specified.
if (flags & splaceDefineClip)
{
U32 clipDepth = GetWord();
INDENT;
printf("clipDepth %i\n", clipDepth);
}
// Get the instance name
if (flags & splaceName)
{
char* pszName = GetString();
INDENT;
printf("instance name %s\n", pszName);
}
if (!m_dumpAll)
return;
}
void CInputScript::ParseRemoveObject(char *str)
{
U32 tagid = (U32) GetWord();
U32 depth = (U32) GetWord();
printf("%stagRemoveObject \ttagid %-5u depth %-5u\n", str, tagid, depth);
}
void CInputScript::ParseRemoveObject2(char *str)
{
U32 depth = (U32) GetWord();
printf("%stagRemoveObject2 depth %-5u\n", str, depth);
}
void CInputScript::ParseSetBackgroundColor(char *str)
{
U32 r = GetByte();
U32 g = GetByte();
U32 b = GetByte();
U32 color = (r << 16) | (g << 8) | b;
printf("%stagSetBackgroundColor \tRGB_HEX %06x\n", str, color);
}
void CInputScript::ParseStartSound(char *str)
{
U32 tagid = (U32) GetWord();
printf("%stagStartSound \ttagid %-5u\n", str, tagid);
if (!m_dumpAll)
return;
U32 code = GetByte();
INDENT;
printf("%scode %-3u", str, code);
if ( code & soundHasInPoint )
printf(" inpoint %u ", GetDWord());
if ( code & soundHasOutPoint )
printf(" oupoint %u", GetDWord());
if ( code & soundHasLoops )
printf(" loops %u", GetDWord());
printf("\n");
if ( code & soundHasEnvelope )
{
int points = GetByte();
for ( int i = 0; i < points; i++ )
{
printf("\n");
INDENT;
printf("%smark44 %u", str, GetDWord());
printf(" left chanel %u", GetWord());
printf(" right chanel %u", GetWord());
printf("\n");
}
}
}
void CInputScript::ParseStopSound(char *str)
{
printf("%stagStopSound\n", str);
}
void CInputScript::ParseProtect(char *str)
{
printf("%stagProtect\n", str);
}
BOOL CInputScript::ParseShapeRecord(char *str, int& xLast, int& yLast, BOOL fWithAlpha)
{
// Determine if this is an edge.
BOOL isEdge = (BOOL) GetBits(1);
if (!isEdge)
{