OSDN Git Service

add zlib
authormzp <mzpppp@gmail.com>
Sun, 1 Nov 2009 23:53:56 +0000 (08:53 +0900)
committermzp <mzpppp@gmail.com>
Sun, 1 Nov 2009 23:53:56 +0000 (08:53 +0900)
link/OMakefile
swflib/OMakefile
swflib/swf.ml
swflib/swfIn.ml
swflib/swfInTest.ml

index 0fa8a95..d1a2dd1 100644 (file)
@@ -5,6 +5,7 @@
 OCAMLPACKS[] =
        extlib
        oUnit
+       gz
 
 UseCamlp4(pa_oo pa_field)
 
index 2e3cef9..67fb6c5 100644 (file)
@@ -4,6 +4,7 @@ OCAMLPACKS[] =
        xml-light
        oUnit
        str
+       gz
 
 FILES[] =
        label
@@ -58,7 +59,7 @@ OUnitTest(revList  , revList)
 OUnitTest(cpool    , cpool revList)
 
 OUnitTest(swfOut, swfOut swfBaseOut bitsOut)
-OUnitTest(swfIn, swfIn swfBaseIn bitsIn swfOut swfBaseOut bitsOut)
+OUnitTest(swfIn, swfIn swfBaseIn bitsIn swfOut swfBaseOut bitsOut bytesIn)
 OUnitTest(tagOut, tagOut tagType)
 OUnitTest(tagIn, tagIn swfBaseIn bitsIn tagType swfBaseOut bitsOut)
 OUnitTest(swfBaseOut, swfBaseOut bitsOut)
index 0852c1e..6067e7b 100644 (file)
@@ -4,6 +4,7 @@ open SwfOut
 open TagOut
 
 type t = Abc.t TagType.t SwfType.t
+exception SwfError of string
 
 module Writer = SwfOut.Make(TagOut.Make(Abc))
 module Reader = SwfIn.Make(TagIn.Make(Abc))
@@ -14,6 +15,9 @@ let write ch swf =
   +> List.iter (output_byte ch)
 
 let read ch =
-  BytesIn.of_channel ch
-  +> Reader.read
+  try
+    BytesIn.of_channel ch
+    +> Reader.read
+  with e ->
+    raise (SwfError (Printexc.to_string e))
 
index f5c5010..ce4ed18 100644 (file)
@@ -50,9 +50,8 @@ module Make(Tag:TagType) = struct
       [< (tag,size) = tag_and_size; body = repeat size ui8 >] ->
        Tag.read tag @@ Stream.of_list body
 
-  let read = parser
-      [< _ = char 'F'; _ = char 'W'; _ = char 'S';
-        version = ui8; _ = ui32; (left,right,top,bottom) = rect;
+  let swf = parser
+      [< version = ui8; _ = ui32; (left,right,top,bottom) = rect;
         frame_rate = fixed8; frame_count = ui16; tags = many to_tag >] ->
        {
          version;
@@ -61,4 +60,35 @@ module Make(Tag:TagType) = struct
          frame_count;
          tags
        }
+
+  let string_of_stream stream =
+    let buf =
+      Buffer.create 0 in
+      Stream.iter (Buffer.add_char buf $ char_of_int) stream;
+      Buffer.contents buf
+
+  let stream_of_string s =
+    let n =
+      String.length s in
+      Stream.from begin fun i ->
+       if i < n then
+         Some (int_of_char s.[i])
+       else
+         None
+      end
+
+  let uncompress s =
+    String.iter (fun c -> Printf.printf "[%x]\n" (int_of_char c)) s(*;
+    Gz.uncompress s ~pos:0 ~len:(String.length s)*)
+      ;s
+
+  let read stream =
+    match stream with parser
+       [< _ = char 'F'; _ = char 'W'; _ = char 'S' >] ->
+         swf stream
+      | [< _ = char 'C'; _ = char 'W'; _ = char 'S' >] ->
+         string_of_stream stream
+         +> uncompress
+         +> stream_of_string
+         +> swf
 end
index 5165ff8..9fc9706 100644 (file)
@@ -30,6 +30,11 @@ let ok_b ?msg f x y =
   assert_equal ?msg (SwfBaseOut.to_list y) (SwfBaseOut.to_list (f x))
 
 let _ = begin "swfIn.ml" >::: [
+  "compress" >:: begin fun () ->
+    open_in_with "compress.swf" begin fun ch ->
+      ignore @@ M.read @@ BytesIn.of_channel ch
+    end
+  end;
   "header" >:: begin fun () ->
     let swf = {
       version     = 9;