Skip to content

Commit 1d59f86

Browse files
committed
FEAT: very minimal SWF file format codec
1 parent cd4c62a commit 1d59f86

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

src/mezz/boot-files.r

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ REBOL [
5353
%codec-crt.r
5454
%codec-gzip.r
5555
%codec-xml.r
56+
%codec-swf.r
5657
]
5758

5859
;-- protocols:

src/mezz/codec-swf.r

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
REBOL [
2+
title: "REBOL 3 codec for SWF file format"
3+
name: 'codec-SWF
4+
author: "Oldes"
5+
version: 0.1.0
6+
date: 11-Oct-2018
7+
history: [
8+
0.1.0 11-Oct-2018 "Oldes" {
9+
Initial version with DECODE and IDENTIFY functions.
10+
Not all chunks are parsed.
11+
}
12+
]
13+
]
14+
15+
import module [
16+
Title: "SWF file format related functions"
17+
Name: SWF
18+
Version: 0.1.0
19+
;Exports: []
20+
][
21+
22+
readRECT: func[bin /local n rect][
23+
binary/read bin [n: UB 5]
24+
rect: make object! [xMin: xMax: yMin: yMax: 0]
25+
set rect binary/read bin [SB :n SB :n SB :n SB :n]
26+
rect
27+
]
28+
29+
register-codec [
30+
name: 'SWF
31+
title: "ShockWave Flash"
32+
suffixes: [%.swf]
33+
34+
decode: function [
35+
data [binary!]
36+
][
37+
if verbose > 0 [
38+
print ["^[[1;32mDecode SWF data^[[m (^[[1m" length? data "^[[mbytes )"]
39+
]
40+
41+
swf: make object! [
42+
header: none
43+
tags: none
44+
]
45+
46+
bin: binary data ; initializes Bincode streams
47+
binary/read bin [
48+
compression: UI8
49+
signature: UI16
50+
version: UI8
51+
]
52+
unless all [
53+
signature = 22355 ; "WS"
54+
find [67 70 90] compression ; C F Z
55+
][
56+
; not a SWF file
57+
return none
58+
]
59+
fileSize: binary/read bin 'UI32LE
60+
if verbose > 0 [
61+
print [
62+
"SWF file version:" version
63+
select [
64+
67 "compressed using deflate"
65+
70 "uncompressed"
66+
90 "compressed using LZMA"
67+
] compression
68+
lf
69+
"Data size:" fileSize - 8 "bytes"
70+
]
71+
]
72+
73+
switch compression [
74+
67 [
75+
binary/init bin decompress/zlib/size bin/buffer fileSize - 8
76+
]
77+
90 [
78+
packed: binary/read bin 'UI32LE
79+
binary/init bin decompress/lzma/size bin/buffer fileSize - 8
80+
]
81+
]
82+
83+
binary/read bin [n: UB 5]
84+
frame-size: binary/read bin [SB :n SB :n SB :n SB :n ALIGN]
85+
;? bin
86+
binary/read bin [frame-rate: UI16 frame-count: UI16LE]
87+
if verbose > 0 [
88+
print ["^[[32mframe-size: ^[[0;1m" frame-size "^[[m"]
89+
print ["^[[32mframe-rate: ^[[0;1m" frame-rate "^[[m"]
90+
print ["^[[32mframe-count: ^[[0;1m" frame-count "^[[m"]
91+
]
92+
swf/header: object compose [
93+
version: (version)
94+
bounds: (frame-size)
95+
rate: (frame-rate)
96+
frames: (frame-count)
97+
]
98+
swf/tags: make block! (2 * frame-count) + 100
99+
while [not tail? bin/buffer][
100+
tag: binary/read bin 'UI16LE
101+
tagId: (65472 and tag) >> 6
102+
tagLength: tag and 63
103+
if tagLength = 63 [tagLength: binary/read bin 'UI32LE]
104+
either tagLength > 0 [
105+
binary/read bin [tagData: BYTES :tagLength]
106+
][ tagData: none ]
107+
repend swf/tags [tagId tagData]
108+
]
109+
110+
new-line/all/skip swf/tags true 2
111+
return swf
112+
]
113+
114+
decode-tag: function[id [integer!] data [binary!]][
115+
116+
]
117+
118+
identify: func [
119+
"Returns TRUE if binary looks like SWF data"
120+
data [binary!]
121+
][
122+
parse/case data [[#"C" | #"F" | #"Z"] "WS" to end]
123+
]
124+
125+
verbose: 0
126+
]
127+
]

src/tests/units/codecs-test.r3

+14
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,18 @@ if find system/codecs 'crt [
5959
system/codecs/crt/verbose: 0
6060
]
6161

62+
if find system/codecs 'swf [
63+
system/codecs/swf/verbose: 3
64+
===start-group=== "SWF codec"
65+
66+
--test-- "Load SWF file"
67+
--assert object? swf1: load %units/files/test1-deflate.swf
68+
--assert object? swf2: load %units/files/test2-lzma.swf
69+
--assert swf1/tags = swf2/tags
70+
--assert swf1/header/frames = 25
71+
72+
===end-group===
73+
system/codecs/swf/verbose: 0
74+
]
75+
6276
~~~end-file~~~
262 Bytes
Binary file not shown.

src/tests/units/files/test2-lzma.swf

252 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)