diff options
Diffstat (limited to 'third_party/rust/leb128')
-rw-r--r-- | third_party/rust/leb128/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/leb128/Cargo.toml | 29 | ||||
-rw-r--r-- | third_party/rust/leb128/LICENSE-APACHE | 201 | ||||
-rw-r--r-- | third_party/rust/leb128/LICENSE-MIT | 25 | ||||
-rw-r--r-- | third_party/rust/leb128/README.md | 113 | ||||
-rwxr-xr-x | third_party/rust/leb128/benches/bench.rs | 66 | ||||
-rwxr-xr-x | third_party/rust/leb128/format | 33 | ||||
-rw-r--r-- | third_party/rust/leb128/src/bin/leb128-repl.rs | 93 | ||||
-rw-r--r-- | third_party/rust/leb128/src/lib.rs | 556 | ||||
-rw-r--r-- | third_party/rust/leb128/tests/quickchecks.rs | 48 |
10 files changed, 1165 insertions, 0 deletions
diff --git a/third_party/rust/leb128/.cargo-checksum.json b/third_party/rust/leb128/.cargo-checksum.json new file mode 100644 index 0000000000..7539459f1c --- /dev/null +++ b/third_party/rust/leb128/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"d70d0cbbc1e8b4ea23752c9447e3aae913c4a0d90150de56f3b24f078c24fc33","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"7b63ecd5f1902af1b63729947373683c32745c16a10e8e6292e2e2dcd7e90ae0","README.md":"6421616fb4573642b0b6caa712662a75e265aa97225747f01944a37ec763684f","benches/bench.rs":"466c4373071e85e206eeb0be08fe90dfa13631778a9ef9dd5c2d46dbad04d587","format":"365c1f23817e16ddcaffa2bf013cf2367c22b4a0fad1fd0ab66580b0eddd95e7","src/bin/leb128-repl.rs":"ed77faac736e087079689967b1916131d1ea7d1d98cb83e3231b62b3992f1389","src/lib.rs":"849e30f6b1d9f8aeb748c64228cce01a1478dfb12f7e0448a94af21b8b0b617b","tests/quickchecks.rs":"14e318ffcec1ed9baf940561a0a78a91a56e15f3db8c9cc375f1d2f9c0377f6d"},"package":"3576a87f2ba00f6f106fdfcd16db1d698d648a26ad8e0573cad8537c3c362d2a"}
\ No newline at end of file diff --git a/third_party/rust/leb128/Cargo.toml b/third_party/rust/leb128/Cargo.toml new file mode 100644 index 0000000000..fc84257445 --- /dev/null +++ b/third_party/rust/leb128/Cargo.toml @@ -0,0 +1,29 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "leb128" +version = "0.2.4" +authors = ["Nick Fitzgerald <fitzgen@gmail.com>", "Philip Craig <philipjcraig@gmail.com>"] +description = "Read and write DWARF's \"Little Endian Base 128\" (LEB128) variable length integer encoding." +documentation = "https://docs.rs/leb128" +readme = "./README.md" +keywords = ["LEB128", "DWARF", "variable", "length", "encoding"] +license = "Apache-2.0/MIT" +repository = "https://github.com/gimli-rs/leb128" + +[dependencies] +[dev-dependencies.quickcheck] +version = "0.8.0" + +[features] +nightly = [] diff --git a/third_party/rust/leb128/LICENSE-APACHE b/third_party/rust/leb128/LICENSE-APACHE new file mode 100644 index 0000000000..16fe87b06e --- /dev/null +++ b/third_party/rust/leb128/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/third_party/rust/leb128/LICENSE-MIT b/third_party/rust/leb128/LICENSE-MIT new file mode 100644 index 0000000000..e69282e381 --- /dev/null +++ b/third_party/rust/leb128/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2015 The Rust Project Developers + +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. diff --git a/third_party/rust/leb128/README.md b/third_party/rust/leb128/README.md new file mode 100644 index 0000000000..2632f5787c --- /dev/null +++ b/third_party/rust/leb128/README.md @@ -0,0 +1,113 @@ +# `leb128` + +[![](http://meritbadge.herokuapp.com/leb128) ![](https://img.shields.io/crates/d/leb128.png)](https://crates.io/crates/leb128) [![Build Status](https://travis-ci.org/gimli-rs/leb128.png?branch=master)](https://travis-ci.org/gimli-rs/leb128) [![Coverage Status](https://coveralls.io/repos/github/gimli-rs/leb128/badge.svg?branch=master)](https://coveralls.io/github/gimli-rs/leb128?branch=master) + +Read and write DWARF's "Little Endian Base 128" (LEB128) variable length integer +encoding. + +The implementation is a direct translation of the pseudocode in the DWARF 4 +standard's appendix C. + +## Install + +Either + + $ cargo add leb128 + +or add this to your `Cargo.toml`: + + [dependencies] + leb128 = "0.2.1" + +## Example + +```rust +use leb128; + +let mut buf = [0; 1024]; + +// Write to anything that implements `std::io::Write`. +{ + let mut writable = &mut buf[..]; + leb128::write::signed(&mut writable, -12345).expect("Should write number"); +} + +// Read from anything that implements `std::io::Read`. +let mut readable = &buf[..]; +let val = leb128::read::signed(&mut readable).expect("Should read number"); +assert_eq!(val, -12345); +``` + +## Documentation + +[Documentation](http://gimli-rs.github.io/leb128/leb128/index.html) + +## Read-Eval-Print-Loop for LEB128 + +This crate comes with a `leb128-repl` program that you can use after `cargo +install leb128` or by running `cargo run` in clone of this repository. + +``` +$ leb128-repl +LEB128 Read-Eval-Print-Loop! + +Converts numbers to signed and unsigned LEB128 and displays the results in +base-10, hex, and binary. + +> 42 +# unsigned LEB128 +[42] +[2a] +[00101010] + +# signed LEB128 +[42] +[2a] +[00101010] + +> -42 +# unsigned LEB128 +error + +# signed LEB128 +[86] +[56] +[01010110] + +> 9001 +# unsigned LEB128 +[169, 70] +[a9, 46] +[10101001, 01000110] + +# signed LEB128 +[169, 198, 0] +[a9, c6, 0] +[10101001, 11000110, 00000000] + +> -9001 +# unsigned LEB128 +error + +# signed LEB128 +[215, 185, 127] +[d7, b9, 7f] +[11010111, 10111001, 01111111] + +> +``` + +## License + +Licensed under either of + + * Apache License, Version 2.0 ([`LICENSE-APACHE`](./LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([`LICENSE-MIT`](./LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +## Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions. diff --git a/third_party/rust/leb128/benches/bench.rs b/third_party/rust/leb128/benches/bench.rs new file mode 100755 index 0000000000..afd4297ec8 --- /dev/null +++ b/third_party/rust/leb128/benches/bench.rs @@ -0,0 +1,66 @@ +#![feature(test)] + +extern crate test; +extern crate leb128; + +#[bench] +fn write_signed(b: &mut test::Bencher) { + let mut buf = [0; 4096]; + + b.iter(|| { + let mut writable = &mut buf[..]; + for i in -1025..1025 { + test::black_box(leb128::write::signed(&mut writable, i).unwrap()); + } + }); +} + +#[bench] +fn write_unsigned(b: &mut test::Bencher) { + let mut buf = [0; 4096]; + + b.iter(|| { + let mut writable = &mut buf[..]; + for i in 0..2050 { + test::black_box(leb128::write::unsigned(&mut writable, i).unwrap()); + } + }); +} + +#[bench] +fn read_signed(b: &mut test::Bencher) { + let mut buf = [0; 4096]; + + { + let mut writable = &mut buf[..]; + for i in -1025..1025 { + leb128::write::signed(&mut writable, i).unwrap(); + } + } + + b.iter(|| { + let mut readable = &buf[..]; + for _ in -1025..1025 { + test::black_box(leb128::read::signed(&mut readable).unwrap()); + } + }); +} + +#[bench] +fn read_unsigned(b: &mut test::Bencher) { + let mut buf = [0; 4096]; + + { + let mut writable = &mut buf[..]; + for i in 0..2050 { + leb128::write::unsigned(&mut writable, i).unwrap(); + } + } + + b.iter(|| { + let mut readable = &buf[..]; + for _ in 0..2050 { + test::black_box(leb128::read::unsigned(&mut readable).unwrap()); + } + }); +} diff --git a/third_party/rust/leb128/format b/third_party/rust/leb128/format new file mode 100755 index 0000000000..7221f10c74 --- /dev/null +++ b/third_party/rust/leb128/format @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# Usage: +# ./format +# +# Run `rustfmt` on all the Rust source files in this project, modifying them in +# place. Usually, one would use `cargo fmt` for this task, but that seems to be +# broken for me at the moment. + +RUSTFMT="" + +function maybe_set() { + if [[ $RUSTFMT == "" && -f "$1" ]]; then + RUSTFMT="$1" + fi +} + +maybe_set ~/.multirust/toolchains/nightly/cargo/bin/rustfmt +maybe_set ~/.cargo/bin/rustfmt +maybe_set $(which rustfmt) + +if [[ $RUSTFMT == "" ]]; then + echo "$0: error: could not find an appropriate rustfmt binary!" + exit 1 +fi + +echo "$0: using rustfmt=$RUSTFMT" + +cd $(dirname $0) +RUST_FILES=$(find src -type f -name '*.rs') + +$RUSTFMT -v --write-mode replace $RUST_FILES 2>&1 \ + | xargs -I '{}' echo "rustfmt: {}" diff --git a/third_party/rust/leb128/src/bin/leb128-repl.rs b/third_party/rust/leb128/src/bin/leb128-repl.rs new file mode 100644 index 0000000000..22b6e87e6d --- /dev/null +++ b/third_party/rust/leb128/src/bin/leb128-repl.rs @@ -0,0 +1,93 @@ +extern crate leb128; + +use std::io::{self, BufRead, Write}; +use std::str; + +fn display(bytes: &[u8]) -> String { + let mut s = vec![]; + + // Base 10. + write!(&mut s, "{:?}\n", bytes).unwrap(); + + // Hex. + write!(&mut s, "[").unwrap(); + for (i, b) in bytes.iter().enumerate() { + if i != 0 { + write!(&mut s, ", ").unwrap(); + } + write!(&mut s, "{:0x}", b).unwrap(); + } + writeln!(&mut s, "]").unwrap(); + + // Binary. + write!(&mut s, "[").unwrap(); + for (i, b) in bytes.iter().enumerate() { + if i != 0 { + write!(&mut s, ", ").unwrap(); + } + write!(&mut s, "{:08b}", b).unwrap(); + } + writeln!(&mut s, "]").unwrap(); + + String::from_utf8(s).unwrap() +} + +fn main() { + println!( + " +LEB128 Read-Eval-Print-Loop! + +Converts numbers to signed and unsigned LEB128 and displays the results in +base-10, hex, and binary. +" ); + + let mut stdin = io::BufReader::new(io::stdin()); + let mut stdout = io::stdout(); + + let mut buf = vec![]; + + loop { + stdout.write_all(b"> ").expect("failed to write to stdout"); + stdout.flush().expect("failed to flush stdout"); + + buf.clear(); + let n = stdin + .read_until(b'\n', &mut buf) + .expect("failed to read line from stdin"); + if n == 0 { + break; + } + + let uleb = str::from_utf8(&buf) + .ok() + .and_then(|s| s.trim().parse().ok()) + .and_then(|n: u64| { + let mut s = vec![]; + leb128::write::unsigned(&mut s, n).ok()?; + Some(display(&s)) + }) + .unwrap_or_else(|| "error\n".into()); + stdout + .write_all(b"# unsigned LEB128\n") + .and_then(|_| stdout.write_all(uleb.as_bytes())) + .and_then(|_| stdout.write_all(b"\n")) + .expect("failed to write to stdout"); + + let leb = str::from_utf8(&buf) + .ok() + .and_then(|s| s.trim().parse().ok()) + .and_then(|n: i64| { + let mut s = vec![]; + leb128::write::signed(&mut s, n).ok()?; + Some(display(&s)) + }) + .unwrap_or_else(|| "error\n".into()); + stdout + .write_all(b"# signed LEB128\n") + .and_then(|_| stdout.write_all(leb.as_bytes())) + .and_then(|_| stdout.write_all(b"\n")) + .expect("failed to write to stdout"); + + stdout.flush().expect("failed to flush stdout"); + } +} diff --git a/third_party/rust/leb128/src/lib.rs b/third_party/rust/leb128/src/lib.rs new file mode 100644 index 0000000000..cc8f2cfd6c --- /dev/null +++ b/third_party/rust/leb128/src/lib.rs @@ -0,0 +1,556 @@ +//! Read and write DWARF's "Little Endian Base 128" (LEB128) variable length +//! integer encoding. +//! +//! The implementation is a direct translation of the psuedocode in the DWARF 4 +//! standard's appendix C. +//! +//! Read and write signed integers: +//! +//! ``` +//! use leb128; +//! +//! let mut buf = [0; 1024]; +//! +//! // Write to anything that implements `std::io::Write`. +//! { +//! let mut writable = &mut buf[..]; +//! leb128::write::signed(&mut writable, -12345).expect("Should write number"); +//! } +//! +//! // Read from anything that implements `std::io::Read`. +//! let mut readable = &buf[..]; +//! let val = leb128::read::signed(&mut readable).expect("Should read number"); +//! assert_eq!(val, -12345); +//! ``` +//! +//! Or read and write unsigned integers: +//! +//! ``` +//! use leb128; +//! +//! let mut buf = [0; 1024]; +//! +//! { +//! let mut writable = &mut buf[..]; +//! leb128::write::unsigned(&mut writable, 98765).expect("Should write number"); +//! } +//! +//! let mut readable = &buf[..]; +//! let val = leb128::read::unsigned(&mut readable).expect("Should read number"); +//! assert_eq!(val, 98765); +//! ``` + +#![deny(missing_docs)] + +#[doc(hidden)] +pub const CONTINUATION_BIT: u8 = 1 << 7; +#[doc(hidden)] +pub const SIGN_BIT: u8 = 1 << 6; + +#[doc(hidden)] +#[inline] +pub fn low_bits_of_byte(byte: u8) -> u8 { + byte & !CONTINUATION_BIT +} + +#[doc(hidden)] +#[inline] +pub fn low_bits_of_u64(val: u64) -> u8 { + let byte = val & (std::u8::MAX as u64); + low_bits_of_byte(byte as u8) +} + +/// A module for reading signed and unsigned integers that have been LEB128 +/// encoded. +pub mod read { + use super::{low_bits_of_byte, CONTINUATION_BIT, SIGN_BIT}; + use std::fmt; + use std::io; + + /// An enumeration of the possible errors that can occur when reading a + /// number encoded with LEB128. + #[derive(Debug)] + pub enum Error { + /// There was an underlying IO error. + IoError(io::Error), + /// The number being read is larger than can be represented. + Overflow, + } + + impl From<io::Error> for Error { + fn from(e: io::Error) -> Self { + Error::IoError(e) + } + } + + impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!( + f, + "leb128::read::Error: {}", + ::std::error::Error::description(self) + ) + } + } + + impl ::std::error::Error for Error { + fn description(&self) -> &str { + match *self { + Error::IoError(ref e) => e.description(), + Error::Overflow => "The number being read is larger than can be represented", + } + } + + fn cause(&self) -> Option<&::std::error::Error> { + match *self { + Error::IoError(ref e) => Some(e), + Error::Overflow => None, + } + } + } + + /// Read an unsigned LEB128 number from the given `std::io::Read`able and + /// return it or an error if reading failed. + pub fn unsigned<R>(r: &mut R) -> Result<u64, Error> + where + R: io::Read, + { + let mut result = 0; + let mut shift = 0; + + loop { + let mut buf = [0]; + try!(r.read_exact(&mut buf)); + + if shift == 63 && buf[0] != 0x00 && buf[0] != 0x01 { + return Err(Error::Overflow); + } + + let low_bits = low_bits_of_byte(buf[0]) as u64; + result |= low_bits << shift; + + if buf[0] & CONTINUATION_BIT == 0 { + return Ok(result); + } + + shift += 7; + } + } + + /// Read a signed LEB128 number from the given `std::io::Read`able and + /// return it or an error if reading failed. + pub fn signed<R>(r: &mut R) -> Result<i64, Error> + where + R: io::Read, + { + let mut result = 0; + let mut shift = 0; + let size = 64; + let mut byte; + + loop { + let mut buf = [0]; + try!(r.read_exact(&mut buf)); + + byte = buf[0]; + if shift == 63 && byte != 0x00 && byte != 0x7f { + return Err(Error::Overflow); + } + + let low_bits = low_bits_of_byte(byte) as i64; + result |= low_bits << shift; + shift += 7; + + if byte & CONTINUATION_BIT == 0 { + break; + } + } + + if shift < size && (SIGN_BIT & byte) == SIGN_BIT { + // Sign extend the result. + result |= !0 << shift; + } + + Ok(result) + } +} + +/// A module for writing integers encoded as LEB128. +pub mod write { + use super::{low_bits_of_u64, CONTINUATION_BIT}; + use std::io; + + /// Write the given unsigned number using the LEB128 encoding to the given + /// `std::io::Write`able. Returns the number of bytes written to `w`, or an + /// error if writing failed. + pub fn unsigned<W>(w: &mut W, mut val: u64) -> Result<usize, io::Error> + where + W: ?Sized + io::Write, + { + let mut bytes_written = 0; + loop { + let mut byte = low_bits_of_u64(val); + val >>= 7; + if val != 0 { + // More bytes to come, so set the continuation bit. + byte |= CONTINUATION_BIT; + } + + let buf = [byte]; + try!(w.write_all(&buf)); + bytes_written += 1; + + if val == 0 { + return Ok(bytes_written); + } + } + } + + /// Write the given signed number using the LEB128 encoding to the given + /// `std::io::Write`able. Returns the number of bytes written to `w`, or an + /// error if writing failed. + pub fn signed<W>(w: &mut W, mut val: i64) -> Result<usize, io::Error> + where + W: ?Sized + io::Write, + { + let mut bytes_written = 0; + loop { + let mut byte = val as u8; + // Keep the sign bit for testing + val >>= 6; + let done = val == 0 || val == -1; + if done { + byte &= !CONTINUATION_BIT; + } else { + // Remove the sign bit + val >>= 1; + // More bytes to come, so set the continuation bit. + byte |= CONTINUATION_BIT; + } + + let buf = [byte]; + try!(w.write_all(&buf)); + bytes_written += 1; + + if done { + return Ok(bytes_written); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std; + use std::io; + + #[test] + fn test_low_bits_of_byte() { + for i in 0..127 { + assert_eq!(i, low_bits_of_byte(i)); + assert_eq!(i, low_bits_of_byte(i | CONTINUATION_BIT)); + } + } + + #[test] + fn test_low_bits_of_u64() { + for i in 0u64..127 { + assert_eq!(i as u8, low_bits_of_u64(1 << 16 | i)); + assert_eq!( + i as u8, + low_bits_of_u64(i << 16 | i | (CONTINUATION_BIT as u64)) + ); + } + } + + // Examples from the DWARF 4 standard, section 7.6, figure 22. + #[test] + fn test_read_unsigned() { + let buf = [2u8]; + let mut readable = &buf[..]; + assert_eq!( + 2, + read::unsigned(&mut readable).expect("Should read number") + ); + + let buf = [127u8]; + let mut readable = &buf[..]; + assert_eq!( + 127, + read::unsigned(&mut readable).expect("Should read number") + ); + + let buf = [CONTINUATION_BIT, 1]; + let mut readable = &buf[..]; + assert_eq!( + 128, + read::unsigned(&mut readable).expect("Should read number") + ); + + let buf = [1u8 | CONTINUATION_BIT, 1]; + let mut readable = &buf[..]; + assert_eq!( + 129, + read::unsigned(&mut readable).expect("Should read number") + ); + + let buf = [2u8 | CONTINUATION_BIT, 1]; + let mut readable = &buf[..]; + assert_eq!( + 130, + read::unsigned(&mut readable).expect("Should read number") + ); + + let buf = [57u8 | CONTINUATION_BIT, 100]; + let mut readable = &buf[..]; + assert_eq!( + 12857, + read::unsigned(&mut readable).expect("Should read number") + ); + } + + // Examples from the DWARF 4 standard, section 7.6, figure 23. + #[test] + fn test_read_signed() { + let buf = [2u8]; + let mut readable = &buf[..]; + assert_eq!(2, read::signed(&mut readable).expect("Should read number")); + + let buf = [0x7eu8]; + let mut readable = &buf[..]; + assert_eq!(-2, read::signed(&mut readable).expect("Should read number")); + + let buf = [127u8 | CONTINUATION_BIT, 0]; + let mut readable = &buf[..]; + assert_eq!( + 127, + read::signed(&mut readable).expect("Should read number") + ); + + let buf = [1u8 | CONTINUATION_BIT, 0x7f]; + let mut readable = &buf[..]; + assert_eq!( + -127, + read::signed(&mut readable).expect("Should read number") + ); + + let buf = [CONTINUATION_BIT, 1]; + let mut readable = &buf[..]; + assert_eq!( + 128, + read::signed(&mut readable).expect("Should read number") + ); + + let buf = [CONTINUATION_BIT, 0x7f]; + let mut readable = &buf[..]; + assert_eq!( + -128, + read::signed(&mut readable).expect("Should read number") + ); + + let buf = [1u8 | CONTINUATION_BIT, 1]; + let mut readable = &buf[..]; + assert_eq!( + 129, + read::signed(&mut readable).expect("Should read number") + ); + + let buf = [0x7fu8 | CONTINUATION_BIT, 0x7e]; + let mut readable = &buf[..]; + assert_eq!( + -129, + read::signed(&mut readable).expect("Should read number") + ); + } + + #[test] + fn test_read_signed_63_bits() { + let buf = [ + CONTINUATION_BIT, + CONTINUATION_BIT, + CONTINUATION_BIT, + CONTINUATION_BIT, + CONTINUATION_BIT, + CONTINUATION_BIT, + CONTINUATION_BIT, + CONTINUATION_BIT, + 0x40, + ]; + let mut readable = &buf[..]; + assert_eq!( + -0x4000000000000000, + read::signed(&mut readable).expect("Should read number") + ); + } + + #[test] + fn test_read_unsigned_not_enough_data() { + let buf = [CONTINUATION_BIT]; + let mut readable = &buf[..]; + match read::unsigned(&mut readable) { + Err(read::Error::IoError(e)) => assert_eq!(e.kind(), io::ErrorKind::UnexpectedEof), + otherwise => panic!("Unexpected: {:?}", otherwise), + } + } + + #[test] + fn test_read_signed_not_enough_data() { + let buf = [CONTINUATION_BIT]; + let mut readable = &buf[..]; + match read::signed(&mut readable) { + Err(read::Error::IoError(e)) => assert_eq!(e.kind(), io::ErrorKind::UnexpectedEof), + otherwise => panic!("Unexpected: {:?}", otherwise), + } + } + + #[test] + fn test_write_unsigned_not_enough_space() { + let mut buf = [0; 1]; + let mut writable = &mut buf[..]; + match write::unsigned(&mut writable, 128) { + Err(e) => assert_eq!(e.kind(), io::ErrorKind::WriteZero), + otherwise => panic!("Unexpected: {:?}", otherwise), + } + } + + #[test] + fn test_write_signed_not_enough_space() { + let mut buf = [0; 1]; + let mut writable = &mut buf[..]; + match write::signed(&mut writable, 128) { + Err(e) => assert_eq!(e.kind(), io::ErrorKind::WriteZero), + otherwise => panic!("Unexpected: {:?}", otherwise), + } + } + + #[test] + fn dogfood_signed() { + fn inner(i: i64) { + let mut buf = [0u8; 1024]; + + { + let mut writable = &mut buf[..]; + write::signed(&mut writable, i).expect("Should write signed number"); + } + + let mut readable = &buf[..]; + let result = read::signed(&mut readable).expect("Should be able to read it back again"); + assert_eq!(i, result); + } + for i in -513..513 { + inner(i); + } + inner(std::i64::MIN); + } + + #[test] + fn dogfood_unsigned() { + for i in 0..1025 { + let mut buf = [0u8; 1024]; + + { + let mut writable = &mut buf[..]; + write::unsigned(&mut writable, i).expect("Should write signed number"); + } + + let mut readable = &buf[..]; + let result = + read::unsigned(&mut readable).expect("Should be able to read it back again"); + assert_eq!(i, result); + } + } + + #[test] + fn test_read_unsigned_overflow() { + let buf = [ + 2u8 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 1, + ]; + let mut readable = &buf[..]; + assert!(read::unsigned(&mut readable).is_err()); + } + + #[test] + fn test_read_signed_overflow() { + let buf = [ + 2u8 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 2 | CONTINUATION_BIT, + 1, + ]; + let mut readable = &buf[..]; + assert!(read::signed(&mut readable).is_err()); + } + + #[test] + fn test_read_multiple() { + let buf = [2u8 | CONTINUATION_BIT, 1u8, 1u8]; + + let mut readable = &buf[..]; + assert_eq!( + read::unsigned(&mut readable).expect("Should read first number"), + 130u64 + ); + assert_eq!( + read::unsigned(&mut readable).expect("Should read first number"), + 1u64 + ); + } +} diff --git a/third_party/rust/leb128/tests/quickchecks.rs b/third_party/rust/leb128/tests/quickchecks.rs new file mode 100644 index 0000000000..ac6752f761 --- /dev/null +++ b/third_party/rust/leb128/tests/quickchecks.rs @@ -0,0 +1,48 @@ +extern crate leb128; +extern crate quickcheck; + +use std::io; + +#[test] +fn can_write_any_unsigned_int() { + fn f(x: u64) -> io::Result<()> { + let mut v = vec![]; + leb128::write::unsigned(&mut v, x)?; + Ok(()) + } + quickcheck::quickcheck(f as fn(u64) -> io::Result<()>); +} + +#[test] +fn can_round_trip_any_unsigned_int() { + fn f(x: u64) -> io::Result<bool> { + let mut v = vec![]; + leb128::write::unsigned(&mut v, x)?; + let y = leb128::read::unsigned(&mut &v[..]) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + Ok(x == y) + } + quickcheck::quickcheck(f as fn(u64) -> io::Result<bool>); +} + +#[test] +fn can_write_any_signed_int() { + fn f(x: i64) -> io::Result<()> { + let mut v = vec![]; + leb128::write::signed(&mut v, x)?; + Ok(()) + } + quickcheck::quickcheck(f as fn(i64) -> io::Result<()>); +} + +#[test] +fn can_round_trip_any_signed_int() { + fn f(x: i64) -> io::Result<bool> { + let mut v = vec![]; + leb128::write::signed(&mut v, x)?; + let y = leb128::read::signed(&mut &v[..]) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + Ok(x == y) + } + quickcheck::quickcheck(f as fn(i64) -> io::Result<bool>); +} |