44open Angstrom
55
66let defer finally = Fun. protect ~finally
7+ let arch32 = match Sys. word_size with 32 -> true | _ -> false
78
89(* * redefine & as bitwise operation, && is still logical and *)
910let ( & ) = Int. logand
@@ -138,6 +139,8 @@ type value =
138139 | Enum of int
139140 | String of string
140141 | Int of int
142+ | Int32 of Int32 .t
143+ | Int64 of Int64 .t
141144 | Float of float
142145 | Unknown
143146
@@ -146,6 +149,8 @@ let to_string value =
146149 | Enum d -> sprintf " enum(%d)" d
147150 | String s -> sprintf " string(%s)" s
148151 | Int i -> sprintf " int(%d)" i
152+ | Int32 i -> sprintf " int(%ld)" i
153+ | Int64 i -> sprintf " int(%Ld)" i
149154 | Float f -> sprintf " float(%f)" f
150155 | Unknown -> " unknown"
151156
@@ -163,6 +168,7 @@ type t = { header : header; records : record list }
163168 [arch] is known as well *)
164169let base arch ty =
165170 let ff = - 1 in
171+ let ff' = Int32. minus_one in
166172 let float = function
167173 | x when Float. is_nan x -> return Unknown
168174 | x when x = Float. infinity -> return Unknown
@@ -179,12 +185,16 @@ let base arch ty =
179185 return (if x = unk then Unknown else Int x)
180186 in
181187 let int32 ukn x =
182- let x = Int32. to_int x in
183- return (if x = ukn then Unknown else Int x)
188+ match arch32 with
189+ | _ when x = ukn -> return Unknown
190+ | true -> return (Int32 x)
191+ | false -> return (Int (Int32. to_int x))
184192 in
185193 let uint32 unk x =
186- let x = Int32. to_int x land 0xffff_ffff in
187- return (if x = unk then Unknown else Int x)
194+ match arch32 with
195+ | _ when x = unk -> return Unknown
196+ | true -> return (Int64 Int64. (of_int32 x |> logand 0xFFFF_FFFF_L ))
197+ | false -> return (Int Int32. (to_int x land 0xffff_ffff ))
188198 in
189199 let value =
190200 match (arch, ty.Type. ty) with
@@ -194,18 +204,18 @@ let base arch ty =
194204 | __ , Type. Int (Signed, 8 , FF) -> any_int8 >> = int ff
195205 | BE , Type. Int (Signed, 16 , FF) -> BE. any_int16 >> = int ff
196206 | LE , Type. Int (Signed, 16 , FF) -> LE. any_int16 >> = int ff
197- | BE , Type. Int (Signed, 32 , FF) -> BE. any_int32 >> = int32 ff
198- | LE , Type. Int (Signed, 32 , FF) -> LE. any_int32 >> = int32 ff
207+ | BE , Type. Int (Signed, 32 , FF) -> BE. any_int32 >> = int32 ff'
208+ | LE , Type. Int (Signed, 32 , FF) -> LE. any_int32 >> = int32 ff'
199209 | __ , Type. Int (Unsigned, 8 , ZZ) -> any_uint8 >> = uint8 0
200210 | LE , Type. Int (Unsigned, 16 , ZZ) -> LE. any_uint16 >> = uint16 0
201211 | BE , Type. Int (Unsigned, 16 , ZZ) -> BE. any_uint16 >> = uint16 0
202- | LE , Type. Int (Unsigned, 32 , ZZ) -> LE. any_int32 >> = uint32 0
203- | BE , Type. Int (Unsigned, 32 , ZZ) -> BE. any_int32 >> = uint32 0
212+ | LE , Type. Int (Unsigned, 32 , ZZ) -> LE. any_int32 >> = uint32 0l
213+ | BE , Type. Int (Unsigned, 32 , ZZ) -> BE. any_int32 >> = uint32 0l
204214 | __ , Type. Int (Unsigned, 8 , FF) -> any_uint8 >> = uint8 0xff
205215 | LE , Type. Int (Unsigned, 16 , FF) -> LE. any_uint16 >> = uint16 0xffff
206216 | BE , Type. Int (Unsigned, 16 , FF) -> BE. any_uint16 >> = uint16 0xffff
207- | LE , Type. Int (Unsigned, 32 , FF) -> LE. any_int32 >> = uint32 0xffff_ffff
208- | BE , Type. Int (Unsigned, 32 , FF) -> BE. any_int32 >> = uint32 0xffff_ffff
217+ | LE , Type. Int (Unsigned, 32 , FF) -> LE. any_int32 >> = uint32 0xffff_ffff_l
218+ | BE , Type. Int (Unsigned, 32 , FF) -> BE. any_int32 >> = uint32 0xffff_ffff_l
209219 | BE , Type. Float 32 -> BE. any_float >> = float
210220 | LE , Type. Float 32 -> LE. any_float >> = float
211221 | BE , Type. Float 64 -> BE. any_double >> = float
@@ -426,6 +436,8 @@ module JSON = struct
426436 | _ , _ , Enum n -> (string_of_int pos, `Int n)
427437 | _ , _ , String s -> (string_of_int pos, `String s)
428438 | _ , _ , Int i -> (string_of_int pos, `Int i)
439+ | _ , _ , Int32 i -> (string_of_int pos, `Float (Int32. to_float i))
440+ | _ , _ , Int64 i -> (string_of_int pos, `Float (Int64. to_float i))
429441 | _ , _ , Float f when Float. is_nan f -> (string_of_int pos, `Null )
430442 | _ , _ , Float f -> (string_of_int pos, `Float f)
431443 | _ , _ , Unknown -> (string_of_int pos, `Null )
0 commit comments