leb128-0.2.5/.cargo_vcs_info.json0000644000000001110000000000100120260ustar { "git": { "sha1": "34ce0fce88281a6c7e7bf4e51505ea29ef2b712f" } }leb128-0.2.5/.github/dependabot.yml000064400000000000000000000001770072674642500150520ustar 00000000000000version: 2 updates: - package-ecosystem: cargo directory: "/" schedule: interval: daily open-pull-requests-limit: 10 leb128-0.2.5/.gitignore000064400000000000000000000000300072674642500126360ustar 00000000000000target Cargo.lock **.bk leb128-0.2.5/Cargo.lock0000644000000143710000000000100100160ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "aho-corasick" version = "0.7.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" dependencies = [ "memchr", ] [[package]] name = "autocfg" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cloudabi" version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" dependencies = [ "bitflags", ] [[package]] name = "env_logger" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3" dependencies = [ "log", "regex", ] [[package]] name = "fuchsia-cprng" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" [[package]] name = "leb128" version = "0.2.5" dependencies = [ "quickcheck", ] [[package]] name = "libc" version = "0.2.103" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6" [[package]] name = "log" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ "cfg-if", ] [[package]] name = "memchr" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "quickcheck" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c35d9c36a562f37eca96e79f66d5fd56eefbc22560dacc4a864cabd2d277456" dependencies = [ "env_logger", "log", "rand", "rand_core 0.4.2", ] [[package]] name = "rand" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" dependencies = [ "autocfg", "libc", "rand_chacha", "rand_core 0.4.2", "rand_hc", "rand_isaac", "rand_jitter", "rand_os", "rand_pcg", "rand_xorshift", "winapi", ] [[package]] name = "rand_chacha" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" dependencies = [ "autocfg", "rand_core 0.3.1", ] [[package]] name = "rand_core" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" dependencies = [ "rand_core 0.4.2", ] [[package]] name = "rand_core" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" [[package]] name = "rand_hc" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" dependencies = [ "rand_core 0.3.1", ] [[package]] name = "rand_isaac" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" dependencies = [ "rand_core 0.3.1", ] [[package]] name = "rand_jitter" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" dependencies = [ "libc", "rand_core 0.4.2", "winapi", ] [[package]] name = "rand_os" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" dependencies = [ "cloudabi", "fuchsia-cprng", "libc", "rand_core 0.4.2", "rdrand", "winapi", ] [[package]] name = "rand_pcg" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" dependencies = [ "autocfg", "rand_core 0.4.2", ] [[package]] name = "rand_xorshift" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" dependencies = [ "rand_core 0.3.1", ] [[package]] name = "rdrand" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" dependencies = [ "rand_core 0.3.1", ] [[package]] name = "regex" version = "1.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" leb128-0.2.5/Cargo.toml0000644000000020700000000000100100320ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "leb128" version = "0.2.5" authors = ["Nick Fitzgerald ", "Philip Craig "] exclude = ["/.travis.yml", "/.coveralls.yml", "/format"] 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 = [] leb128-0.2.5/Cargo.toml.orig000064400000000000000000000011400072674642500135400ustar 00000000000000[package] authors = ["Nick Fitzgerald ", "Philip Craig "] name = "leb128" version = "0.2.5" edition = "2018" description = "Read and write DWARF's \"Little Endian Base 128\" (LEB128) variable length integer encoding." license = "Apache-2.0/MIT" readme = "./README.md" keywords = ["LEB128", "DWARF", "variable", "length", "encoding"] documentation = "https://docs.rs/leb128" repository = "https://github.com/gimli-rs/leb128" exclude = ["/.travis.yml", "/.coveralls.yml", "/format"] [dependencies] [dev-dependencies] quickcheck = "0.8.0" [features] nightly = [] leb128-0.2.5/LICENSE-APACHE000064400000000000000000000251370072674642500126110ustar 00000000000000 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. leb128-0.2.5/LICENSE-MIT000064400000000000000000000020570072674642500123150ustar 00000000000000Copyright (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. leb128-0.2.5/README.md000064400000000000000000000046720072674642500121450ustar 00000000000000# `leb128` [![](https://img.shields.io/crates/v/leb128.svg) ![](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](https://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 https://www.apache.org/licenses/LICENSE-2.0) * MIT license ([`LICENSE-MIT`](./LICENSE-MIT) or https://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. leb128-0.2.5/benches/bench.rs000064400000000000000000000026100072674642500137100ustar 00000000000000#![feature(test)] extern crate test; use 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()); } }); } leb128-0.2.5/src/bin/leb128-repl.rs000064400000000000000000000047420072674642500145260ustar 00000000000000use 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"); } } leb128-0.2.5/src/lib.rs000064400000000000000000000435600072674642500125700ustar 00000000000000//! 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 LEB128-encoded signed and unsigned integers. pub mod read { use super::{low_bits_of_byte, CONTINUATION_BIT, SIGN_BIT}; use std::fmt; use std::io; /// An error type for reading LEB128-encoded values. #[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 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> { match *self { Error::IoError(ref e) => e.fmt(f), Error::Overflow => { write!(f, "The number being read is larger than can be represented") } } } } impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match *self { Error::IoError(ref e) => Some(e), Error::Overflow => None, } } } /// Read an unsigned LEB128-encoded number from the `std::io::Read` stream /// `r`. /// /// On success, return the number. pub fn unsigned(r: &mut R) -> Result where R: ?Sized + io::Read, { let mut result = 0; let mut shift = 0; loop { let mut buf = [0]; r.read_exact(&mut buf)?; if shift == 63 && buf[0] != 0x00 && buf[0] != 0x01 { while buf[0] & CONTINUATION_BIT != 0 { r.read_exact(&mut buf)?; } 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-encoded number from the `std::io::Read` stream `r`. /// /// On success, return the number. pub fn signed(r: &mut R) -> Result where R: ?Sized + io::Read, { let mut result = 0; let mut shift = 0; let size = 64; let mut byte; loop { let mut buf = [0]; r.read_exact(&mut buf)?; byte = buf[0]; if shift == 63 && byte != 0x00 && byte != 0x7f { while buf[0] & CONTINUATION_BIT != 0 { r.read_exact(&mut buf)?; } 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 LEB128-encoded signed and unsigned integers. pub mod write { use super::{low_bits_of_u64, CONTINUATION_BIT}; use std::io; /// Write `val` to the `std::io::Write` stream `w` as an unsigned LEB128 value. /// /// On success, return the number of bytes written to `w`. pub fn unsigned(w: &mut W, mut val: u64) -> Result 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]; w.write_all(&buf)?; bytes_written += 1; if val == 0 { return Ok(bytes_written); } } } /// Write `val` to the `std::io::Write` stream `w` as a signed LEB128 value. /// /// On success, return the number of bytes written to `w`. pub fn signed(w: &mut W, mut val: i64) -> Result 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]; 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") ); } #[test] fn test_read_unsigned_thru_dyn_trait() { fn read(r: &mut dyn io::Read) -> u64 { read::unsigned(r).expect("Should read number") } let buf = [0u8]; let mut readable = &buf[..]; assert_eq!(0, read(&mut readable)); let mut readable = io::Cursor::new(buf); assert_eq!(0, read(&mut readable)); } // 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_thru_dyn_trait() { fn read(r: &mut dyn io::Read) -> i64 { read::signed(r).expect("Should read number") } let buf = [0u8]; let mut readable = &buf[..]; assert_eq!(0, read(&mut readable)); let mut readable = io::Cursor::new(buf); assert_eq!(0, read(&mut readable)); } #[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 test_write_unsigned_thru_dyn_trait() { fn write(w: &mut dyn io::Write, val: u64) -> usize { write::unsigned(w, val).expect("Should write number") } let mut buf = [0u8; 1]; let mut writable = &mut buf[..]; assert_eq!(write(&mut writable, 0), 1); assert_eq!(buf[0], 0); let mut writable = Vec::from(&buf[..]); assert_eq!(write(&mut writable, 0), 1); assert_eq!(buf[0], 0); } #[test] fn test_write_signed_thru_dyn_trait() { fn write(w: &mut dyn io::Write, val: i64) -> usize { write::signed(w, val).expect("Should write number") } let mut buf = [0u8; 1]; let mut writable = &mut buf[..]; assert_eq!(write(&mut writable, 0), 1); assert_eq!(buf[0], 0); let mut writable = Vec::from(&buf[..]); assert_eq!(write(&mut writable, 0), 1); assert_eq!(buf[0], 0); } #[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 ); } #[test] fn test_read_multiple_with_overflow() { let buf = [ 0b1111_1111, 0b1111_1111, 0b1111_1111, 0b1111_1111, 0b1111_1111, 0b1111_1111, 0b1111_1111, 0b1111_1111, 0b1111_1111, 0b1111_1111, 0b0111_1111, // Overflow! 0b1110_0100, 0b1110_0000, 0b0000_0010, // 45156 ]; let mut readable = &buf[..]; assert!(if let read::Error::Overflow = read::unsigned(&mut readable).expect_err("Should fail with Error::Overflow") { true } else { false }); assert_eq!( read::unsigned(&mut readable).expect("Should succeed with correct value"), 45156 ); } } leb128-0.2.5/tests/quickchecks.rs000064400000000000000000000023320072674642500146620ustar 00000000000000use leb128; use 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 { 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); } #[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 { 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); }