swf.ml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. (*
  2. * This file is part of SwfLib
  3. * Copyright (c)2004 Nicolas Cannasse
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. *)
  19. type float16 = int
  20. type unknown = string
  21. type binary = string
  22. type action_count = int
  23. type rgb = {
  24. cr : int;
  25. cg : int;
  26. cb : int;
  27. }
  28. type rgba = {
  29. r : int;
  30. g : int;
  31. b : int;
  32. a : int;
  33. }
  34. type color =
  35. | ColorRGB of rgb
  36. | ColorRGBA of rgba
  37. type gradient =
  38. | GradientRGB of ((int * rgb) list * int)
  39. | GradientRGBA of ((int * rgba) list * int)
  40. type rect = {
  41. rect_nbits : int;
  42. left : int;
  43. right : int;
  44. top : int;
  45. bottom : int;
  46. }
  47. type big_rect = {
  48. brect_nbits : int;
  49. bleft : int list;
  50. bright : int list;
  51. btop : int list;
  52. bbottom : int list;
  53. }
  54. type matrix_part = {
  55. m_nbits : int;
  56. mx : int;
  57. my : int;
  58. }
  59. type matrix = {
  60. scale : matrix_part option;
  61. rotate : matrix_part option;
  62. trans : matrix_part;
  63. }
  64. type color_transform_alpha = {
  65. cxa_nbits : int;
  66. cxa_add : rgba option;
  67. cxa_mult : rgba option;
  68. }
  69. type function_decl = {
  70. f_name : string;
  71. f_args : string list;
  72. mutable f_codelen : action_count;
  73. }
  74. type func2_flags =
  75. | ThisRegister
  76. | ThisNoVar
  77. | ArgumentsRegister
  78. | ArgumentsNoVar
  79. | SuperRegister
  80. | SuperNoVar
  81. | RootRegister
  82. | ParentRegister
  83. | GlobalRegister
  84. type function_decl2 = {
  85. f2_name : string;
  86. f2_flags : func2_flags list;
  87. f2_args : (int * string) list;
  88. mutable f2_nregs : int;
  89. mutable f2_codelen : action_count;
  90. }
  91. type try_style =
  92. | TryRegister of int
  93. | TryVariable of string
  94. type try_block = {
  95. tr_style : try_style;
  96. mutable tr_trylen : action_count;
  97. mutable tr_catchlen : action_count option;
  98. mutable tr_finallylen : action_count option
  99. }
  100. type push_item =
  101. | PString of string
  102. | PFloat of int32
  103. | PNull
  104. | PUndefined
  105. | PReg of int
  106. | PBool of bool
  107. | PDouble of float
  108. | PInt of int32
  109. | PStack of int
  110. | PStack2 of int
  111. type property =
  112. | PX
  113. | PY
  114. | PXScale
  115. | PYScale
  116. | PCurrentFrame
  117. | PTotalFrames
  118. | PAlpha
  119. | PVisible
  120. | PWidth
  121. | PHeight
  122. | PRotation
  123. | PTarget
  124. | PFramesLoaded
  125. | PName
  126. | PDropTarget
  127. | PUrl
  128. | PHighQuality
  129. | PFocusRect
  130. | PSoundBufTime
  131. | PQuality
  132. | PXMouse
  133. | PYMouse
  134. type action =
  135. | AEnd
  136. | ANextFrame
  137. | APrevFrame
  138. | APlay
  139. | AStop
  140. | AToggleHighQuality
  141. | AStopSounds
  142. | AAddNum
  143. | ASubtract
  144. | AMultiply
  145. | ADivide
  146. | ACompareNum
  147. | AEqualNum
  148. | ALogicalAnd
  149. | ALogicalOr
  150. | ANot
  151. | AStringEqual
  152. | AStringLength
  153. | ASubString
  154. | APop
  155. | AToInt
  156. | AEval
  157. | ASet
  158. | ATellTarget
  159. | AStringAdd
  160. | AGetProperty
  161. | ASetProperty
  162. | ADuplicateMC
  163. | ARemoveMC
  164. | ATrace
  165. | AStartDrag
  166. | AStopDrag
  167. | AThrow
  168. | ACast
  169. | AImplements
  170. | AFSCommand2
  171. | ARandom
  172. | AMBStringLength
  173. | AOrd
  174. | AChr
  175. | AGetTimer
  176. | AMBStringSub
  177. | AMBOrd
  178. | AMBChr
  179. | ADeleteObj
  180. | ADelete
  181. | ALocalAssign
  182. | ACall
  183. | AReturn
  184. | AMod
  185. | ANew
  186. | ALocalVar
  187. | AInitArray
  188. | AObject
  189. | ATypeOf
  190. | ATargetPath
  191. | AEnum
  192. | AAdd
  193. | ACompare
  194. | AEqual
  195. | AToNumber
  196. | AToString
  197. | ADup
  198. | ASwap
  199. | AObjGet
  200. | AObjSet
  201. | AIncrement
  202. | ADecrement
  203. | AObjCall
  204. | ANewMethod
  205. | AInstanceOf
  206. | AEnum2
  207. | AAnd
  208. | AOr
  209. | AXor
  210. | AShl
  211. | AShr
  212. | AAsr
  213. | APhysEqual
  214. | AGreater
  215. | AStringGreater
  216. | AExtends
  217. | AGotoFrame of int
  218. | AGetURL of string * string
  219. | ASetReg of int
  220. | AStringPool of string list
  221. | AWaitForFrame of int * int
  222. | ASetTarget of string
  223. | AGotoLabel of string
  224. | AWaitForFrame2 of int
  225. | AFunction2 of function_decl2
  226. | ATry of try_block
  227. | AWith of int
  228. | APush of push_item list
  229. | AJump of action_count
  230. | AGetURL2 of int
  231. | AFunction of function_decl
  232. | ACondJump of action_count
  233. | ACallFrame (* no data *)
  234. | AGotoFrame2 of bool * int option
  235. | AUnknown of int * unknown
  236. type actions = action DynArray.t
  237. type header = {
  238. mutable h_version : int;
  239. mutable h_size : rect;
  240. mutable h_fps : float16;
  241. mutable h_frame_count : int;
  242. mutable h_compressed : bool;
  243. }
  244. type export = {
  245. mutable exp_id : int;
  246. exp_name : string;
  247. }
  248. type import = {
  249. mutable imp_id : int;
  250. imp_name : string;
  251. }
  252. type do_init_action = {
  253. mutable dia_id : int;
  254. dia_actions : actions;
  255. }
  256. type sound = {
  257. mutable so_id : int;
  258. so_flags : int;
  259. so_samples : int;
  260. so_data : unknown;
  261. }
  262. type start_sound = {
  263. mutable sts_id : int;
  264. sts_data : unknown;
  265. }
  266. type sfs_bitmap = {
  267. sfb_repeat : bool;
  268. sfb_smooth : bool;
  269. mutable sfb_cid : int;
  270. sfb_mpos : matrix;
  271. }
  272. type shape_fill_style =
  273. | SFSSolid of rgb
  274. | SFSSolid3 of rgba
  275. | SFSLinearGradient of matrix * gradient
  276. | SFSRadialGradient of matrix * gradient * int option
  277. | SFSBitmap of sfs_bitmap
  278. type shape_line_style = {
  279. sls_width : int;
  280. sls_color : color;
  281. sls_flags : int option;
  282. sls_fill : shape_fill_style option;
  283. sls_miter : int option;
  284. }
  285. type shape_new_styles = {
  286. sns_fill_styles : shape_fill_style list;
  287. sns_line_styles : shape_line_style list;
  288. sns_nlbits : int;
  289. sns_nfbits : int;
  290. }
  291. type shape_change_style_record = {
  292. scsr_move : (int * int * int) option;
  293. scsr_fs0 : int option;
  294. scsr_fs1 : int option;
  295. scsr_ls : int option;
  296. scsr_new_styles : shape_new_styles option;
  297. }
  298. type shape_curved_edge_record = {
  299. scer_nbits : int;
  300. scer_cx : int;
  301. scer_cy : int;
  302. scer_ax : int;
  303. scer_ay : int;
  304. }
  305. type shape_straight_edge_record = {
  306. sser_nbits : int;
  307. sser_line : int option * int option;
  308. }
  309. type shape_record =
  310. | SRStyleChange of shape_change_style_record
  311. | SRCurvedEdge of shape_curved_edge_record
  312. | SRStraightEdge of shape_straight_edge_record
  313. type shape_records = {
  314. srs_nlbits : int;
  315. srs_nfbits : int;
  316. srs_records : shape_record list;
  317. }
  318. type shape_with_style = {
  319. sws_fill_styles : shape_fill_style list;
  320. sws_line_styles : shape_line_style list;
  321. sws_records : shape_records;
  322. }
  323. type shape = {
  324. mutable sh_id : int;
  325. sh_bounds : rect;
  326. sh_bounds2 : (rect * int) option;
  327. sh_style : shape_with_style;
  328. }
  329. type filter_gradient = {
  330. fgr_colors : (rgba * int) list;
  331. fgr_data : unknown;
  332. }
  333. type filter =
  334. | FDropShadow of unknown
  335. | FBlur of unknown
  336. | FGlow of unknown
  337. | FBevel of unknown
  338. | FGradientGlow of filter_gradient
  339. | FAdjustColor of unknown
  340. | FGradientBevel of filter_gradient
  341. type bitmap_jpg = {
  342. mutable jpg_id : int;
  343. jpg_data : binary;
  344. }
  345. type bitmap_data = {
  346. mutable bd_id : int;
  347. bd_table : binary option;
  348. bd_data : binary;
  349. bd_alpha : binary option;
  350. bd_deblock : int option;
  351. }
  352. type bitmap_lossless = {
  353. mutable bll_id : int;
  354. bll_format : int;
  355. bll_width : int;
  356. bll_height : int;
  357. bll_data : unknown;
  358. }
  359. type morph_shape = {
  360. mutable msh_id : int;
  361. msh_start_bounds : rect;
  362. msh_end_bounds : rect;
  363. msh_data : unknown;
  364. }
  365. type cid_data = {
  366. mutable cd_id : int;
  367. cd_data : binary;
  368. }
  369. type text_glyph = {
  370. txg_index : int;
  371. txg_advanced : int;
  372. }
  373. type text_record = {
  374. mutable txr_font : (int * int) option;
  375. txr_color : color option;
  376. txr_dx : int option;
  377. txr_dy : int option;
  378. txr_glyphs : text_glyph list;
  379. }
  380. type text = {
  381. mutable txt_id : int;
  382. txt_bounds : big_rect;
  383. txt_matrix : matrix;
  384. txt_ngbits : int;
  385. txt_nabits : int;
  386. txt_records : text_record list;
  387. }
  388. type button_record = {
  389. btr_flags : int;
  390. mutable btr_cid : int;
  391. btr_depth : int;
  392. btr_mpos : matrix;
  393. btr_color : color_transform_alpha option;
  394. btr_filters : filter list option;
  395. btr_blendmode : int option;
  396. }
  397. type button_action = {
  398. bta_flags : int;
  399. bta_actions : actions;
  400. }
  401. type button2 = {
  402. mutable bt2_id : int;
  403. bt2_track_as_menu : bool;
  404. bt2_records : button_record list;
  405. bt2_actions : button_action list;
  406. }
  407. type remove_object = {
  408. mutable rmo_id : int;
  409. rmo_depth : int;
  410. }
  411. type edit_text_layout = {
  412. edtl_align : int;
  413. edtl_left_margin : int;
  414. edtl_right_margin : int;
  415. edtl_indent : int;
  416. edtl_leading : int;
  417. }
  418. type edit_text = {
  419. mutable edt_id : int;
  420. edt_bounds : rect;
  421. mutable edt_font : (int * int) option;
  422. edt_color : rgba option;
  423. edt_maxlen : int option;
  424. edt_layout : edit_text_layout option;
  425. edt_variable : string;
  426. edt_text : string option;
  427. edt_wordwrap : bool;
  428. edt_multiline : bool;
  429. edt_password : bool;
  430. edt_readonly : bool;
  431. edt_autosize : bool;
  432. edt_noselect : bool;
  433. edt_border : bool;
  434. edt_html : bool;
  435. edt_outlines : bool;
  436. }
  437. type f9class = {
  438. mutable f9_cid : int option;
  439. f9_classname : string;
  440. }
  441. type files_attrib = {
  442. fa_network : bool;
  443. fa_as3 : bool;
  444. fa_metadata : bool;
  445. fa_gpu : bool;
  446. fa_direct_blt : bool;
  447. }
  448. type tag_data =
  449. | TEnd
  450. | TShowFrame
  451. | TShape of shape
  452. | TRemoveObject of remove_object
  453. | TBitsJPEG of bitmap_jpg
  454. | TJPEGTables of binary
  455. | TSetBgColor of rgb
  456. | TFont of cid_data
  457. | TText of text
  458. | TDoAction of actions
  459. | TFontInfo of cid_data
  460. | TSound of sound
  461. | TStartSound of start_sound
  462. | TBitsLossless of bitmap_lossless
  463. | TBitsJPEG2 of bitmap_data
  464. | TShape2 of shape
  465. | TProtect
  466. | TPlaceObject2 of place_object
  467. | TRemoveObject2 of int
  468. | TShape3 of shape
  469. | TText2 of text
  470. | TButton2 of button2
  471. | TBitsJPEG3 of bitmap_data
  472. | TBitsLossless2 of bitmap_lossless
  473. | TEditText of edit_text
  474. | TClip of clip
  475. | TProductInfo of unknown
  476. | TFrameLabel of string * char option
  477. | TSoundStreamHead2 of unknown
  478. | TMorphShape of morph_shape
  479. | TFont2 of cid_data
  480. | TExport of export list
  481. | TImport of string * import list
  482. | TDoInitAction of do_init_action
  483. | TVideoStream of cid_data
  484. | TVideoFrame of cid_data
  485. | TFontInfo2 of cid_data
  486. | TDebugID of unknown
  487. | TEnableDebugger2 of int * string
  488. | TScriptLimits of int * int
  489. | TFilesAttributes of files_attrib
  490. | TPlaceObject3 of place_object
  491. | TImport2 of string * import list
  492. | TFontAlignZones of cid_data
  493. | TCSMSettings of cid_data
  494. | TFont3 of cid_data
  495. | TF9Classes of f9class list
  496. | TMetaData of string
  497. | TScale9 of int * rect
  498. | TActionScript3 of (int * string) option * As3.as3_tag
  499. | TShape4 of shape
  500. | TMorphShape2 of morph_shape
  501. | TScenes of (int * string) list * (int * string) list
  502. | TBinaryData of int * binary
  503. | TBigBinaryData of int * binary list
  504. | TFontName of cid_data
  505. | TBitsJPEG4 of bitmap_data
  506. | TFont4 of cid_data
  507. | TUnknown of int * unknown
  508. and tag = {
  509. mutable tid : int;
  510. mutable textended : bool;
  511. mutable tdata : tag_data;
  512. }
  513. and clip_event = {
  514. cle_events : int;
  515. cle_key : char option;
  516. cle_actions : actions;
  517. }
  518. and place_object = {
  519. po_depth : int;
  520. po_move : bool;
  521. mutable po_cid : int option;
  522. po_matrix : matrix option;
  523. po_color : color_transform_alpha option;
  524. po_ratio : float16 option;
  525. po_inst_name : string option;
  526. po_clip_depth : int option;
  527. po_events : clip_event list option;
  528. po_filters : filter list option;
  529. po_blend : int option;
  530. po_bcache : int option;
  531. }
  532. and clip = {
  533. mutable c_id : int;
  534. c_frame_count : int;
  535. c_tags : tag list;
  536. }
  537. type font_language_code =
  538. | LCNone (*0*)
  539. | LCLatin (*1*)
  540. | LCJapanese (*2*)
  541. | LCKorean (*3*)
  542. | LCSimplifiedChinese (*4*)
  543. | LCTraditionalChinese (*5*)
  544. type font_glyph_data = {
  545. font_char_code: int;
  546. font_shape: shape_records;
  547. }
  548. type font_layout_glyph_data = {
  549. font_advance: int;
  550. font_bounds: rect;
  551. }
  552. type font_kerning_data = {
  553. font_char_code1: int;
  554. font_char_code2: int;
  555. font_adjust: int;
  556. }
  557. type font_layout_data = {
  558. font_ascent: int;
  559. font_descent: int;
  560. font_leading: int;
  561. font_glyphs_layout: font_layout_glyph_data array;
  562. font_kerning: font_kerning_data list;
  563. }
  564. type font2_data = {
  565. font_shift_jis: bool;
  566. font_is_small: bool;
  567. font_is_ansi: bool;
  568. font_wide_codes: bool;
  569. font_wide_offsets: bool;
  570. font_is_italic: bool;
  571. font_is_bold: bool;
  572. font_language: font_language_code;
  573. font_name: string;
  574. font_glyphs: font_glyph_data array;
  575. font_layout: font_layout_data;
  576. }
  577. type swf = header * tag list
  578. let __deflate = ref (fun (_:unit IO.output) -> assert false)
  579. let __inflate = ref (fun _ -> assert false)
  580. let __parser = ref (fun _ -> assert false)
  581. let __printer = ref (fun (_:unit IO.output) _ -> ())
  582. exception Error of string
  583. let error msg = raise (Error msg)
  584. let warnings = ref true
  585. let to_float16 f =
  586. let sign , f = (if f < 0. then true , 0. -. f else false , f) in
  587. let high = int_of_float f in
  588. let low = int_of_float ((f -. (float high)) *. 256.) in
  589. if high > 127 then failwith "to_float16";
  590. (high lsl 8) lor (if sign then low lor (1 lsl 15) else low)
  591. let parse (ch : IO.input) =
  592. (!__parser ch : swf)
  593. let write (ch : 'a IO.output) (data : swf) =
  594. !__printer (Obj.magic ch) data
  595. let deflate (ch : 'a IO.output) =
  596. (Obj.magic (!__deflate (Obj.magic ch) : unit IO.output) : 'a IO.output)
  597. let inflate (ch : IO.input) =
  598. (!__inflate ch : IO.input)