Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. open Angstrom
  2.  
  3. type t =
  4. | Nil
  5. | Bool of bool
  6. | Int_fix_pos of int
  7. | Int_fix_neg of int
  8. | Int8 of int
  9. | Int16 of int
  10. | Int32 of int32
  11. | Int64 of int64
  12. | Uint8 of int
  13. | Uint16 of int
  14. | Uint32 of int32
  15. | Uint64 of int64
  16. | Float of float
  17. | Double of float
  18. | Str_fix of string
  19. | Str8 of string
  20. | Str16 of string
  21. | Str32 of string
  22. | Bin8 of string
  23. | Bin16 of string
  24. | Bin32 of string
  25. | Array_fix of t list
  26. | Array16 of t list
  27. | Array32 of t list
  28. | Map_fix of (t * t) list
  29. | Map16 of (t * t) list
  30. | Map32 of (t * t) list
  31. | Ext_fix_1 of int * string
  32. | Ext_fix_2 of int * string
  33. | Ext_fix_4 of int * string
  34. | Ext_fix_8 of int * string
  35. | Ext_fix_16 of int * string
  36. | Ext8 of int * string
  37. | Ext16 of int * string
  38. | Ext32 of int * string
  39.  
  40. module Be = struct
  41. include Be
  42.  
  43. module Es = EndianString.BigEndian
  44.  
  45. let get_float s = Es.get_float s 0
  46. let get_double s = Es.get_double s 0
  47.  
  48. let get_int8 s = Es.get_int8 s 0
  49. let get_int16 s = Es.get_int16 s 0
  50. let get_int32 s = Es.get_int32 s 0
  51. let get_int64 s = Es.get_int64 s 0
  52. let get_uint32_as_int s =
  53. (* Trickery is afoot:
  54. The Int32 module doesn't provide a simple way to convert a Int32.t
  55. value to an int in an unsigned-friendly way. So we're going to fake
  56. it here by padding the value we read into a (theoretically signed)
  57. Int64.t. *)
  58. let v = get_int64 ("\x00\x00\x00\x00" ^ s) in
  59. if Int64.compare v (Int64.of_int max_int) > 0 then (
  60. invalid_arg "msgpack field with length longer than max_int"
  61. );
  62. Int64.to_int v
  63.  
  64. let get_uint8 s = Es.get_uint8 s 0
  65. let get_uint16 s = Es.get_uint16 s 0
  66. let get_uint32 s = Es.get_int32 s 0
  67. let get_uint64 s = Es.get_int64 s 0
  68.  
  69. let make_es size set x =
  70. let buf = Bytes.create size in
  71. set buf 0 x;
  72. Bytes.to_string buf
  73.  
  74. let of_float x =
  75. make_es 4 Es.set_float x
  76. let of_double x =
  77. make_es 8 Es.set_double x
  78.  
  79. let of_int8 x =
  80. make_es 1 Es.set_int8 x
  81. let of_int16 x =
  82. make_es 2 Es.set_int16 x
  83. let of_int32 x =
  84. make_es 4 Es.set_int32 x
  85. let of_int64 x =
  86. make_es 8 Es.set_int64 x
  87.  
  88. let of_uint8 x =
  89. make_es 1 Es.set_int8 x
  90. let of_uint16 x =
  91. make_es 2 Es.set_int16 x
  92. let of_uint32 x =
  93. make_es 4 Es.set_int32 x
  94. let of_uint64 x =
  95. make_es 8 Es.set_int64 x
  96. let of_uint32_as_int x =
  97. (* Trickery is afoot:
  98. The Int32 module doesn't provide a simple way to convert an int value
  99. to an Int32.t in an unsigned-friendly way. So we're going to fake it
  100. here by padding the value we write into a (theoretically signed)
  101. Int64.t. *)
  102. let s = make_es 8 Es.set_int64 (Int64.of_int x) in
  103. String.sub s 4 4
  104. end
  105.  
  106. let byte x = Angstrom.char @@ Char.chr x
  107.  
  108. (* nil *)
  109. let nil_ =
  110. byte 0xc0 *> return Nil
  111.  
  112. let nil = nil_
  113.  
  114. (* bool *)
  115. let false_ =
  116. byte 0xc2 *> return (Bool false)
  117. let true_ =
  118. byte 0xc3 *> return (Bool true)
  119.  
  120. let bool =
  121. choice [false_; true_]
  122.  
  123. (* int *)
  124. let int_fix_pos =
  125. satisfy (fun c -> Char.code c land 0x80 = 0) >>| fun c ->
  126. Int_fix_pos (Be.get_uint8 (String.make 1 c))
  127. let int_fix_neg =
  128. satisfy (fun c -> Char.code c land 0xe0 = 0xe0) >>| fun c ->
  129. Int_fix_neg (Be.get_int8 (String.make 1 c))
  130. let uint8 =
  131. byte 0xcc *> Be.uint8 >>| fun i -> Uint8 i
  132. let uint16 =
  133. byte 0xcd *> Be.uint16 >>| fun i -> Uint16 i
  134. let uint32 =
  135. byte 0xce *> Be.uint32 >>| fun i -> Uint32 i
  136. let uint64 =
  137. byte 0xcf *> Be.uint64 >>| fun i -> Uint64 i
  138. let int8 =
  139. byte 0xd0 *> Be.int8 >>| fun i -> Int8 i
  140. let int16 =
  141. byte 0xd1 *> Be.int16 >>| fun i -> Int16 i
  142. let int32 =
  143. byte 0xd2 *> Be.int32 >>| fun i -> Int32 i
  144. let int64 =
  145. byte 0xd3 *> Be.int64 >>| fun i -> Int64 i
  146.  
  147. let int =
  148. choice [
  149. int_fix_pos;
  150. int_fix_neg;
  151. int8;
  152. int16;
  153. int32;
  154. int64;
  155. uint8;
  156. uint16;
  157. uint32;
  158. uint64;
  159. ]
  160.  
  161. (* float *)
  162. let float =
  163. byte 0xca *> Be.float >>| fun f -> Float f
  164. let double =
  165. byte 0xcb *> Be.double >>| fun d -> Double d
  166.  
  167. let raw8 tag =
  168. byte tag *> Be.uint8 >>= fun length ->
  169. take length
  170. let raw16 tag =
  171. byte tag *> Be.uint16 >>= fun length ->
  172. take length
  173. let raw32 tag =
  174. byte tag *> take 4 >>= fun s ->
  175. let length = Be.get_uint32_as_int s in
  176. take length
  177.  
  178. (* str *)
  179. let str_fix =
  180. satisfy (fun c -> Char.code c land 0xe0 = 0xa0) >>= fun c ->
  181. let length = Be.get_uint8 (String.make 1 c) land 0x1f in
  182. take length >>| fun x -> Str_fix x
  183. let str8 = raw8 0xd9 >>| fun x -> Str8 x
  184. let str16 = raw16 0xda >>| fun x -> Str16 x
  185. let str32 = raw32 0xdb >>| fun x -> Str32 x
  186. let str =
  187. choice [
  188. str_fix;
  189. str8;
  190. str16;
  191. str32;
  192. ]
  193.  
  194. (* bin *)
  195. let bin8 = raw8 0xc4 >>| fun x -> Bin8 x
  196. let bin16 = raw16 0xc5 >>| fun x -> Bin16 x
  197. let bin32 = raw32 0xc6 >>| fun x -> Bin32 x
  198. let bin =
  199. choice [
  200. bin8;
  201. bin16;
  202. bin32;
  203. ]
  204.  
  205. (* array *)
  206. let array_fix any =
  207. satisfy (fun c -> Char.code c land 0xf0 = 0x90) >>= fun c ->
  208. let length = Be.get_uint8 (String.make 1 c) land 0x0f in
  209. count length any >>| fun x -> Array_fix x
  210. let array16 any =
  211. byte 0xdc *> Be.uint16 >>= fun length ->
  212. count length any >>| fun x -> Array16 x
  213. let array32 any =
  214. byte 0xdd *> take 4 >>= fun s ->
  215. let length = Be.get_uint32_as_int s in
  216. count length any >>| fun x -> Array32 x
  217. let array any =
  218. choice [
  219. array_fix any;
  220. array16 any;
  221. array32 any;
  222. ]
  223.  
  224. (* ext *)
  225. let ext_fix_n tag n =
  226. byte tag *> Be.int8 >>= fun typ ->
  227. take n >>= fun content ->
  228. return (typ, content)
  229.  
  230. let ext_fix_1 = ext_fix_n 0xd4 1 >>| fun (typ, x) -> Ext_fix_1 (typ, x)
  231. let ext_fix_2 = ext_fix_n 0xd5 2 >>| fun (typ, x) -> Ext_fix_2 (typ, x)
  232. let ext_fix_4 = ext_fix_n 0xd6 4 >>| fun (typ, x) -> Ext_fix_4 (typ, x)
  233. let ext_fix_8 = ext_fix_n 0xd7 8 >>| fun (typ, x) -> Ext_fix_8 (typ, x)
  234. let ext_fix_16 = ext_fix_n 0xd8 16 >>| fun (typ, x) -> Ext_fix_16 (typ, x)
  235.  
  236. let ext8 =
  237. byte 0xc7 *> Be.uint8 >>= fun length ->
  238. Be.int8 >>= fun typ ->
  239. take length >>= fun content ->
  240. return @@ Ext8 (typ, content)
  241. let ext16 =
  242. byte 0xc8 *> Be.uint16 >>= fun length ->
  243. Be.int8 >>= fun typ ->
  244. take length >>= fun content ->
  245. return @@ Ext16 (typ, content)
  246. let ext32 =
  247. byte 0xc9 *> take 4 >>= fun s ->
  248. let length = Be.get_uint32_as_int s in
  249. Be.int8 >>= fun typ ->
  250. take length >>= fun content ->
  251. return @@ Ext32 (typ, content)
  252.  
  253. let ext =
  254. choice [
  255. ext_fix_1;
  256. ext_fix_2;
  257. ext_fix_4;
  258. ext_fix_8;
  259. ext_fix_16;
  260. ext8;
  261. ext16;
  262. ext32;
  263. ]
  264.  
  265. (* map *)
  266. let tuple a b =
  267. a >>= fun a' ->
  268. b >>= fun b' ->
  269. return (a', b')
  270.  
  271. let map_fix ~key ~value =
  272. satisfy (fun c -> Char.code c land 0xf0 = 0x80) >>= fun c ->
  273. let length = Be.get_uint8 (String.make 1 c) land 0x0f in
  274. count length (tuple key value) >>| fun x -> Map_fix x
  275. let map16 ~key ~value =
  276. byte 0xde *> Be.uint16 >>= fun length ->
  277. count length (tuple key value) >>| fun x -> Map16 x
  278. let map32 ~key ~value =
  279. byte 0xdf *> take 4 >>= fun s ->
  280. let length = Be.get_uint32_as_int s in
  281. count length (tuple key value) >>| fun x -> Map32 x
  282. let map ~key ~value =
  283. choice [
  284. map_fix ~key ~value;
  285. map16 ~key ~value;
  286. map32 ~key ~value;
  287. ]
  288.  
  289. (* One msgpack value *)
  290. let msgpack =
  291. fix (
  292. fun v ->
  293. let map' = map ~key:v ~value:v in
  294. let array' = array v in
  295. choice [
  296. nil;
  297. bool;
  298. int;
  299. float;
  300. double;
  301. str;
  302. bin;
  303. array';
  304. map';
  305. ext;
  306. ]
  307. )
  308.  
  309. (* Many concatenated msgpack values *)
  310. let msgpacks =
  311. many msgpack
  312.  
  313. let of_string s =
  314. parse_only msgpack (`String s)
  315.  
  316. let of_string_exn s =
  317. match of_string s with
  318. | Result.Ok msg -> msg
  319. | Result.Error e -> invalid_arg e
  320.  
  321. let msgs_of_string s =
  322. parse_only msgpacks (`String s)
  323.  
  324. let msgs_of_string_exn s =
  325. match msgs_of_string s with
  326. | Result.Ok msg -> msg
  327. | Result.Error e -> invalid_arg e
  328.  
  329. let to_buffer buf msg =
  330. let bc c = Buffer.add_char buf (Char.chr c) in
  331. let bs s = Buffer.add_string buf s in
  332. let rec add m =
  333. match m with
  334. | Nil -> bc 0xc0
  335. | Bool false -> bc 0xc2
  336. | Bool true -> bc 0xc3
  337. | Int_fix_pos i -> bs (Be.of_uint8 i)
  338. | Int_fix_neg i -> bs (Be.of_int8 i)
  339. | Int8 i -> bc 0xd0; bs (Be.of_int8 i)
  340. | Int16 i -> bc 0xd1; bs (Be.of_int16 i)
  341. | Int32 i -> bc 0xd2; bs (Be.of_int32 i)
  342. | Int64 i -> bc 0xd3; bs (Be.of_int64 i)
  343. | Uint8 i -> bc 0xcc; bs (Be.of_uint8 i)
  344. | Uint16 i -> bc 0xcd; bs (Be.of_uint16 i)
  345. | Uint32 i -> bc 0xce; bs (Be.of_uint32 i)
  346. | Uint64 i -> bc 0xcf; bs (Be.of_uint64 i)
  347. | Float f -> bc 0xca; bs (Be.of_float f)
  348. | Double d -> bc 0xcb; bs (Be.of_double d)
  349. | Str_fix s -> bc (0xa0 lor String.length s); bs s
  350. | Str8 s ->
  351. bc 0xd9;
  352. bs (Be.of_uint8 (String.length s));
  353. bs s
  354. | Str16 s ->
  355. bc 0xda;
  356. bs (Be.of_uint16 (String.length s));
  357. bs s
  358. | Str32 s ->
  359. bc 0xdb;
  360. bs (Be.of_uint32_as_int (String.length s));
  361. bs s
  362. | Bin8 b ->
  363. bc 0xc4;
  364. bs (Be.of_uint8 (String.length b));
  365. bs b
  366. | Bin16 b ->
  367. bc 0xc5;
  368. bs (Be.of_uint16 (String.length b));
  369. bs b
  370. | Bin32 b ->
  371. bc 0xc6;
  372. bs (Be.of_uint32_as_int (String.length b));
  373. bs b
  374. | Array_fix a -> bc (0x90 lor List.length a); List.iter add a
  375. | Array16 a ->
  376. bc 0xdc;
  377. bs (Be.of_uint16 (List.length a));
  378. List.iter add a
  379. | Array32 a ->
  380. bc 0xdd;
  381. bs (Be.of_uint32_as_int (List.length a));
  382. List.iter add a
  383. | Map_fix m ->
  384. bc (0x80 lor List.length m);
  385. List.iter (fun (k, v) -> add k; add v) m
  386. | Map16 m ->
  387. bc 0xde;
  388. bs (Be.of_uint16 (List.length m));
  389. List.iter (fun (k, v) -> add k; add v) m
  390. | Map32 m ->
  391. bc 0xdf;
  392. bs (Be.of_uint32_as_int (List.length m));
  393. List.iter (fun (k, v) -> add k; add v) m
  394. | Ext_fix_1 (typ, s) ->
  395. bc 0xd4;
  396. bs (Be.of_int8 typ);
  397. bs s
  398. | Ext_fix_2 (typ, s) ->
  399. bc 0xd5;
  400. bs (Be.of_int8 typ);
  401. bs s
  402. | Ext_fix_4 (typ, s) ->
  403. bc 0xd6;
  404. bs (Be.of_int8 typ);
  405. bs s
  406. | Ext_fix_8 (typ, s) ->
  407. bc 0xd7;
  408. bs (Be.of_int8 typ);
  409. bs s
  410. | Ext_fix_16 (typ, s) ->
  411. bc 0xd8;
  412. bs (Be.of_int8 typ);
  413. bs s
  414. | Ext8 (typ, s) ->
  415. bc 0xc7;
  416. bs (Be.of_uint8 (String.length s));
  417. bs (Be.of_int8 typ);
  418. bs s
  419. | Ext16 (typ, s) ->
  420. bc 0xc8;
  421. bs (Be.of_uint16 (String.length s));
  422. bs (Be.of_int8 typ);
  423. bs s
  424. | Ext32 (typ, s) ->
  425. bc 0xc9;
  426. bs (Be.of_uint32_as_int (String.length s));
  427. bs (Be.of_int8 typ);
  428. bs s
  429. in
  430. add msg
  431.  
  432. let to_string msg =
  433. let buf = Buffer.create 1_024 in
  434. to_buffer buf msg;
  435. Buffer.contents buf
  436.  
  437. let msgs_to_string msgs =
  438. let buf = Buffer.create 1_024 in
  439. List.iter (to_buffer buf) msgs;
  440. Buffer.contents buf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement