Skip to content
This repository
tag: v3804
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 650 lines (573 sloc) 21.506 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
/*
Copyright © 2011, 2012 MLstate

This file is part of Opa.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* Binary data handling: for any sequence of bytes that is not to be considered
* as text.
*
* @destination PRIVATE
* @stability STABLE
*/

/**
* {1 About this module}
*
* For Node.js Buffer objects are not extensible but this code implements copy-based
* expansion according to a conservative policy.
* For both Node.js and OCaml, therefore, growth is an expensive operation in both
* memory and computation so it pays to get the initial hint correct.
*
* {1 Where should I start?}
*
* You can create directly from strings with [binary_of_string] or an empty binary can
* be created using [Binary.create].
*
* You can fill binary data using the functions provided by the interface.
*
* Insufficient space for added data will cause the data to be expanded, resulting in copying of existing data.
*
* Out-of-range numerical values will result in an exception being raised, as will attempting to read data
* from beyond the end of the binary.
*
* {1 What if I need more?}
*
* The most low-level routines are [add_string] and [get_string], if you can get your
* data into a string, use these to embed/unembed in/from the binary data.
*
*/

/**
* {1 Types defined in this module}
*/

/**
* The type of binary data.
*
* The underlying type is module Buffer for both OCaml and Node.js.
* Remember that these are imperative.
*
**/
@opacapi
@abstract
type binary = external

/**
* {1 Interface}
*/

binary_of_string : string -> binary = Binary.of_string

string_of_binary : binary -> string = Binary.to_string

@opacapi
bin_of_base64 = Binary.of_base64

Binary = {{

  /**
* A generic to_string function.
* @param encoding is wanted encoding
* @param data is binary data to encode
* @returns An encoded string of [data]
*/
  @private
  to_encoding(data, encoding) = %%bslBinary.to_encoding%%(data, encoding)

  /**
* Returns the string content of a binary [data].
* Note: The binary data *must* be valid utf8 sequence else the resulted string
* can be invalid.
* @param data The binary data
* @return The decoded content of [data]
*/
  to_string(data) = to_encoding(data, "utf8")

  /**
* Returns a base64 encoded string of a binary [data].
* @param data The binary data
* @return The base64 encoded content of [data]
*/
  to_base64(data) = to_encoding(data, "base64")

  /**
* Returns a hex encoded string of a binary [data].
* @param data The binary data
* @return The hex encoded content of [data]
*/
  to_hex(data) = to_encoding(data, "hex")

  /**
* A generic of_string function.
* @param encoding is the way where the string is encoded
* @param data is string data to decode to a binary
* @return The decoded binary of [data]
*/
  @private
  of_encoding(data, encoding) = %%bslBinary.of_encoding%%(data, encoding)

  /**
* Convert a (non-encoded) string to its binary representation.
* @param data The string to convert
* @return The binary representation of [data]
*/
  of_string(data) = of_encoding(data, "utf8")

  /**
* Convert a base64 encoded string to its binary representation.
* @param data The string to convert
* @return The binary representation of base64 decoded [data]
*/
  of_base64(data) = of_encoding(data, "base64")

  /**
* Convert a hex encoded string to its binary representation.
* @param data The string to convert
* @return The binary representation of hex decoded [data]
*/
  of_hex(data) = of_encoding(data, "hex")

  /**
* Create binary data of the given size.
*
* @param hint The initial size.
* @return An empty binary value.
*/
  create : int -> binary = %%BslBinary.create%%

  /**
* The number of bytes in the data.
* There may be more empty space after the data.
*
* @param b The binary data.
* @return The size of the binary data.
*/
  length : binary -> int = %%BslBinary.length%%

  /**
* Resize the binary data.
* Smaller means truncate existing data.
* May copy the data.
*
* @param b The binary data.
* @param amount Set the buffer to this size.
* @return void
*/
  resize : binary, int -> void = %%BslBinary.resize%%

  /**
* Clear out any data.
* The data remains the same size.
*
* @param b The binary data.
* @return void
*/
  clear : binary -> void = %%BslBinary.clear%%

  /**
* Set the data size to the size of the defined data.
* May copy the data.
*
* @param b The binary data.
* @return void
*/
  trim : binary -> void = %%BslBinary.trim%%

  /**
* Return any underlying memory and set data size to zero.
*
* @param b The binary data.
* @return void
*/
  reset : binary -> void = %%BslBinary.reset%%

  /**
* Add bytes to end of data.
*
* @param b The binary data.
* @param len The number of bytes to add
* @param v The value of the bytes (0-255)
* @return void
*/
  add_fill : binary, int, int -> void = %%BslBinary.add_fill%%

  /**
* Add a string to the end of the data.
*
* @param b The binary data.
* @param str The string to add.
* @return void
*/
  add_string : binary, string -> void = %%BslBinary.add_string%%
  add_stringr : binary, string -> outcome(void,string) = %%BslBinary.add_stringr%%

  /**
* Add binary data to the end of the data.
*
* @param b The binary data.
* @param nb New binary data to add.
* @return void
*/
  add_binary : binary, binary -> void = %%BslBinary.add_binary%%
  add_binaryr : binary, binary -> outcome(void,string) = %%BslBinary.add_binaryr%%

  /**
* Add a signed 8-bit int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (-128 to 127).
* @return void
*/
  add_int8 : binary, int -> void = %%BslBinary.add_int8%%
  add_int8r : binary, int -> outcome(void,string) = %%BslBinary.add_int8r%%

  /**
* Add an unsigned 8-bit int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (0 to 255).
* @return void
*/
  add_uint8 : binary, int -> void = %%BslBinary.add_uint8%%
  add_uint8r : binary, int -> outcome(void,string) = %%BslBinary.add_uint8r%%

  /**
* Add a signed 16-bit little-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (-32768 to 32767).
* @return void
*/
  add_int16_le : binary, int -> void = %%BslBinary.add_int16_le%%
  add_int16_ler : binary, int -> outcome(void,string) = %%BslBinary.add_int16_ler%%

  /**
* Add an unsigned 16-bit little-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (0 to 65535).
* @return void
*/
  add_uint16_le : binary, int -> void = %%BslBinary.add_uint16_le%%
  add_uint16_ler : binary, int -> outcome(void,string) = %%BslBinary.add_uint16_ler%%

  /**
* Add a signed 16-bit big-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (-32768 to 32767).
* @return void
*/
  add_int16_be : binary, int -> void = %%BslBinary.add_int16_be%%
  add_int16_ber : binary, int -> outcome(void,string) = %%BslBinary.add_int16_ber%%

  /**
* Add an unsigned 16-bit big-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (0 to 65535).
* @return void
*/
  add_uint16_be : binary, int -> void = %%BslBinary.add_uint16_be%%
  add_uint16_ber : binary, int -> outcome(void,string) = %%BslBinary.add_uint16_ber%%

  /**
* Add a signed 32-bit little-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (-2147483648 to 2147483647).
* @return void
*/
  add_int32_le : binary, int -> void = %%BslBinary.add_int32_le%%
  add_int32_ler : binary, int -> outcome(void,string) = %%BslBinary.add_int32_ler%%

  /**
* Add an unsigned 32-bit little-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (0 to 4294967295).
* @return void
*/
  add_uint32_le : binary, int -> void = %%BslBinary.add_uint32_le%%
  add_uint32_ler : binary, int -> outcome(void,string) = %%BslBinary.add_uint32_ler%%

  /**
* Add a signed 32-bit big-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (-2147483648 to 2147483647).
* @return void
*/
  add_int32_be : binary, int -> void = %%BslBinary.add_int32_be%%
  add_int32_ber : binary, int -> outcome(void,string) = %%BslBinary.add_int32_ber%%

  /**
* Add an unsigned 32-bit big-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (0 to 4294967295).
* @return void
*/
  add_uint32_be : binary, int -> void = %%BslBinary.add_uint32_be%%
  add_uint32_ber : binary, int -> outcome(void,string) = %%BslBinary.add_uint32_ber%%

  /**
* Add a signed 53-bit little-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (-9007199254740992 to 9007199254740991).
* @return void
*/
  add_int53_le : binary, int -> void = %%BslBinary.add_int53_le%%
  add_int53_ler : binary, int -> outcome(void,string) = %%BslBinary.add_int53_ler%%

  /**
* Add a signed 53-bit big-endian int to the end of the data.
*
* @param b The binary data.
* @param i Integer to add (-9007199254740992 to 9007199254740991).
* @return void
*/
  add_int53_be : binary, int -> void = %%BslBinary.add_int53_be%%
  add_int53_ber : binary, int -> outcome(void,string) = %%BslBinary.add_int53_ber%%

  /**
* Add an unsigned 64-bit little-endian int to the end of the data.
*
* @param b The binary data.
* @param i Int64 to add (0 to 18446744073709551615).
* @return void
*/
  add_uint64_le : binary, int64 -> void = %%BslBinary.add_uint64_le%%
  add_uint64_ler : binary, int64 -> outcome(void,string) = %%BslBinary.add_uint64_ler%%

  /**
* Add an unsigned 64-bit big-endian int to the end of the data.
*
* @param b The binary data.
* @param i Int64 to add (0 to 18446744073709551615).
* @return void
*/
  add_uint64_be : binary, int64 -> void = %%BslBinary.add_uint64_be%%
  add_uint64_ber : binary, int64 -> outcome(void,string) = %%BslBinary.add_uint64_ber%%

  /**
* Add a 32-bit little-endian float to the end of the data.
* The storage format is not a strict IEEE 32-bit float but a rounded equivalent,
* there may be rounding errors when read back and the ranges shown here are
* not accurate to the last bit.
*
* @param b The binary data.
* @param f Float to add (approx 1.175494351e-38 to 3.402823466e38).
* @return void
*/
  add_float_le : binary, float -> void = %%BslBinary.add_float_le%%
  add_float_ler : binary, float -> outcome(void,string) = %%BslBinary.add_float_ler%%

  /**
* Add a 32-bit big-endian float to the end of the data.
* The storage format is not a strict IEEE 32-bit float but a rounded equivalent,
* there may be rounding errors when read back and the ranges shown here are
* not accurate to the last bit.
*
* @param b The binary data.
* @param f Float to add (approx 1.175494351e-38 to 3.402823466e38).
* @return void
*/
  add_float_be : binary, float -> void = %%BslBinary.add_float_be%%
  add_float_ber : binary, float -> outcome(void,string) = %%BslBinary.add_float_ber%%

  /**
* Add a 64-bit little-endian float to the end of the data.
*
* @param b The binary data.
* @param f Float to add (+/-2.22507385850720138e-308 to +/-1.79769313486231571e+308).
* @return void
*/
  add_double_le : binary, float -> void = %%BslBinary.add_double_le%%
  add_double_ler : binary, float -> outcome(void,string) = %%BslBinary.add_double_ler%%

  /**
* Add a 64-bit big-endian float to the end of the data.
*
* @param b The binary data.
* @param f Float to add (+/-2.22507385850720138e-308 to +/-1.79769313486231571e+308).
* @return void
*/
  add_double_be : binary, float -> void = %%BslBinary.add_double_be%%
  add_double_ber : binary, float -> outcome(void,string) = %%BslBinary.add_double_ber%%

  /**
* Read a section of the buffer back into a string.
*
* Particularly for Node.js, the string read back is not translated in any way so you
* should attempt to keep the data as binary as far as possible, even passing the binary
* data directly to transmission routines which should know about any required translation.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @param length Number of bytes to read.
* @return The data section as a string.
*/
  get_string : binary, int, int -> string = %%BslBinary.get_string%%
  get_stringr : binary, int, int -> outcome(string,string) = %%BslBinary.get_stringr%%

  /**
* Read a section of the buffer back into binary data.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @param length Number of bytes to read.
* @return untranslated binary data
*/
  get_binary : binary, int, int -> binary = %%BslBinary.get_binary%%
  get_binaryr : binary, int, int -> outcome(binary,string) = %%BslBinary.get_binaryr%%

  /**
* Read a signed 8-bit int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (-128 to 127).
*/
  get_int8 : binary, int -> int = %%BslBinary.get_int8%%
  get_int8r : binary, int -> outcome(int,string) = %%BslBinary.get_int8r%%

  /**
* Read an unsigned 8-bit int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (0 to 255).
*/
  get_uint8 : binary, int -> int = %%BslBinary.get_uint8%%
  get_uint8r : binary, int -> outcome(int,string) = %%BslBinary.get_uint8r%%

  /**
* Read a signed 16-bit little-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (-32768 to 32767).
*/
  get_int16_le : binary, int -> int = %%BslBinary.get_int16_le%%
  get_int16_ler : binary, int -> outcome(int,string) = %%BslBinary.get_int16_ler%%

  /**
* Read an unsigned 16-bit little-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (0 to 65535).
*/
  get_uint16_le : binary, int -> int = %%BslBinary.get_uint16_le%%
  get_uint16_ler : binary, int -> outcome(int,string) = %%BslBinary.get_uint16_ler%%

  /**
* Read a signed 16-bit big-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (-32768 to 32767).
*/
  get_int16_be : binary, int -> int = %%BslBinary.get_int16_be%%
  get_int16_ber : binary, int -> outcome(int,string) = %%BslBinary.get_int16_ber%%

  /**
* Read an unsigned 16-bit big-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (0 to 65535).
*/
  get_uint16_be : binary, int -> int = %%BslBinary.get_uint16_be%%
  get_uint16_ber : binary, int -> outcome(int,string) = %%BslBinary.get_uint16_ber%%

  /**
* Read a signed 32-bit little-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (-2147483648 to 2147483647).
*/
  get_int32_le : binary, int -> int = %%BslBinary.get_int32_le%%
  get_int32_ler : binary, int -> outcome(int,string) = %%BslBinary.get_int32_ler%%

  /**
* Read an unsigned 32-bit little-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (0 to 4294967295).
*/
  get_uint32_le : binary, int -> int = %%BslBinary.get_uint32_le%%
  get_uint32_ler : binary, int -> outcome(int,string) = %%BslBinary.get_uint32_ler%%

  /**
* Read a signed 32-bit big-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (-2147483648 to 2147483647).
*/
  get_int32_be : binary, int -> int = %%BslBinary.get_int32_be%%
  get_int32_ber : binary, int -> outcome(int,string) = %%BslBinary.get_int32_ber%%

  /**
* Read an unsigned 32-bit big-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (0 to 4294967295).
*/
  get_uint32_be : binary, int -> int = %%BslBinary.get_uint32_be%%
  get_uint32_ber : binary, int -> outcome(int,string) = %%BslBinary.get_uint32_ber%%

  /**
* Read a signed 53-bit little-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (-9007199254740992 to 9007199254740991).
*/
  get_int53_le : binary, int -> int = %%BslBinary.get_int53_le%%
  get_int53_ler : binary, int -> outcome(int,string) = %%BslBinary.get_int53_ler%%

  /**
* Read a signed 53-bit big-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Integer (-9007199254740992 to 9007199254740991).
*/
  get_int53_be : binary, int -> int = %%BslBinary.get_int53_be%%
  get_int53_ber : binary, int -> outcome(int,string) = %%BslBinary.get_int53_ber%%

  /**
* Read an unsigned 64-bit little-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Int64 (0 to 18446744073709551615).
*/
  get_uint64_le : binary, int -> int64 = %%BslBinary.get_uint64_le%%
  get_uint64_ler : binary, int -> outcome(int64,string) = %%BslBinary.get_uint64_ler%%

  /**
* Read an unsigned 64-bit big-endian int from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Int64 (0 to 18446744073709551615).
*/
  get_uint64_be : binary, int -> int64 = %%BslBinary.get_uint64_be%%
  get_uint64_ber : binary, int -> outcome(int64,string) = %%BslBinary.get_uint64_ber%%

  /**
* Read a 32-bit little-endian float from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Float (approx 1.175494351e-38 to 3.402823466e38).
*/
  get_float_le : binary, int -> float = %%BslBinary.get_float_le%%
  get_float_ler : binary, int -> outcome(float,string) = %%BslBinary.get_float_ler%%

  /**
* Read a 32-bit big-endian float from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Float (approx 1.175494351e-38 to 3.402823466e38).
*/
  get_float_be : binary, int -> float = %%BslBinary.get_float_be%%
  get_float_ber : binary, int -> outcome(float,string) = %%BslBinary.get_float_ber%%

  /**
* Read a 64-bit little-endian float from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Float (+/-2.22507385850720138e-308 to +/-1.79769313486231571e+308).
*/
  get_double_le : binary, int -> float = %%BslBinary.get_double_le%%
  get_double_ler : binary, int -> outcome(float,string) = %%BslBinary.get_double_ler%%

  /**
* Read a 64-bit big-endian float from the buffer.
*
* @param b The binary data.
* @param start The starting position in the buffer.
* @return Float (+/-2.22507385850720138e-308 to +/-1.79769313486231571e+308).
*/
  get_double_be : binary, int -> float = %%BslBinary.get_double_be%%
  get_double_ber : binary, int -> outcome(float,string) = %%BslBinary.get_double_ber%%

  /**
* The default order of binary data.
*/
  order : order(binary, Order.default) = Order.make(ordering)

  /**
* Return an ordering of two binary data.
*/
  ordering(bin1, bin2) =
    len1 = length(bin1)
    len2 = length(bin2)
    match Int.ordering(len1, len2)
    | {eq} ->
      rec aux(i) =
        if i == len1 then {eq}
        else
          match Int.ordering(get_int8(bin1, i), get_int8(bin2, i))
          | {eq} -> aux(i+1)
          | x -> x
      aux(0)
    | x -> x

  /**
* Comparison of two binary data.
*/
  compare(bin1, bin2) = ordering(bin1, bin2) <: Order.comparison

  /**
* Checks the equality of two binary
*/
  equals(bin1, bin2) = ordering(bin1, bin2) == {eq}


}}


Something went wrong with that request. Please try again.