Import 'vsprintf' crate v2.0.0
Request Document: go/android-rust-importing-crates
For CL Reviewers: go/android3p#cl-review
For Build Team: go/ab-third-party-imports
Bug: 360132300
Bug: 359646531
Test: none
Change-Id: Iaaaf071145beefc6e3472ae00b196557d9b0232b
Signed-off-by: Neill Kapron <nkapron@google.com>
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
new file mode 100644
index 0000000..7c54ef9
--- /dev/null
+++ b/.cargo_vcs_info.json
@@ -0,0 +1,5 @@
+{
+ "git": {
+ "sha1": "0f8635b8045f3a60f899d4d3b2fd64c455542d2b"
+ }
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4308d82
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+target/
+**/*.rs.bk
+Cargo.lock
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..81be97b
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,27 @@
+# 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]
+edition = "2018"
+name = "vsprintf"
+version = "2.0.0"
+authors = ["Dylan McKay <me@dylanmckay.io>"]
+description = "Rust bindings to the libc vsprintf function"
+documentation = "https://docs.rs/vsprintf"
+readme = "README.md"
+keywords = ["vsprintf", "sprintf", "printf", "va_list", "va_arg"]
+license = "MIT"
+repository = "https://github.com/dylanmckay/vsprintf"
+[dependencies.libc]
+version = "0.2"
+[build-dependencies.cc]
+version = "1.0"
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
new file mode 100644
index 0000000..7a02a4f
--- /dev/null
+++ b/Cargo.toml.orig
@@ -0,0 +1,19 @@
+[package]
+name = "vsprintf"
+version = "2.0.0"
+authors = ["Dylan McKay <me@dylanmckay.io>"]
+edition = "2018"
+
+description = "Rust bindings to the libc vsprintf function"
+license = "MIT"
+documentation = "https://docs.rs/vsprintf"
+repository = "https://github.com/dylanmckay/vsprintf"
+readme = "README.md"
+
+keywords = ["vsprintf", "sprintf", "printf", "va_list", "va_arg"]
+
+[dependencies]
+libc = "0.2"
+
+[build-dependencies]
+cc = "1.0"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..258a887
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,8 @@
+The MIT License (MIT)
+Copyright (c) 2017 Dylan McKay
+
+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/METADATA b/METADATA
new file mode 100644
index 0000000..0dea670
--- /dev/null
+++ b/METADATA
@@ -0,0 +1,20 @@
+name: "vsprintf"
+description: "Rust bindings to the libc vsprintf function"
+third_party {
+ identifier {
+ type: "crates.io"
+ value: "vsprintf"
+ }
+ identifier {
+ type: "Archive"
+ value: "https://static.crates.io/crates/vsprintf/vsprintf-2.0.0.crate"
+ primary_source: true
+ }
+ version: "2.0.0"
+ license_type: NOTICE
+ last_upgrade_date {
+ year: 2024
+ month: 8
+ day: 22
+ }
+}
diff --git a/MODULE_LICENSE_MIT b/MODULE_LICENSE_MIT
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/MODULE_LICENSE_MIT
diff --git a/OWNERS b/OWNERS
new file mode 100644
index 0000000..48bea6e
--- /dev/null
+++ b/OWNERS
@@ -0,0 +1,2 @@
+# Bug component: 688011
+include platform/prebuilts/rust:main:/OWNERS
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..083bf3f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,6 @@
+# vsprintf
+
+Convert a format string and vararg list to a Rust string.
+
+[Documentation](https://docs.rs/vsprintf)
+
diff --git a/build.rs b/build.rs
new file mode 100644
index 0000000..c32e4e9
--- /dev/null
+++ b/build.rs
@@ -0,0 +1,8 @@
+fn main() {
+ println!("cargo:rerun-if-changed=src/lib.c");
+
+ cc::Build::new()
+ .file("src/lib.c")
+ .compile("libvsprintf.a");
+}
+
diff --git a/src/headers.h b/src/headers.h
new file mode 100644
index 0000000..10b222c
--- /dev/null
+++ b/src/headers.h
@@ -0,0 +1,2 @@
+#include <stdio.h>
+
diff --git a/src/lib.c b/src/lib.c
new file mode 100644
index 0000000..fcbd823
--- /dev/null
+++ b/src/lib.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <errno.h>
+
+// C function that calls `vsnprintf` with a POINTER to a va_list.
+//
+// We have to write this in C because there is no way to know
+// the size of a va_list in Rust and so we couldn't pass it
+// by-value as required by vsnprintf.
+int vsnprintf_wrapper(char *buffer,
+ size_t size,
+ const char *format,
+ va_list orig_list) {
+ va_list list;
+ va_copy(list, orig_list);
+
+ // C does not require vsprintf to set errno, but POSIX does.
+ // Here we clear the errno and so we know that if this function
+ // fails AND there is an error set, then it must have been triggered
+ // by the sprintf.
+ errno = 0;
+ return vsnprintf(buffer, size, format, list);
+}
+
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..97d99de
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,75 @@
+//! Convert C format strings to Rust.
+
+extern crate libc;
+
+use libc::size_t;
+use std::iter::repeat;
+use std::io::Error;
+use std::os::raw::*;
+
+const INITIAL_BUFFER_SIZE: usize = 512;
+
+/// The result of a vsprintf call.
+pub type Result<T> = ::std::result::Result<T, Error>;
+
+/// Prints a format string into a Rust string.
+pub unsafe fn vsprintf<V>(format: *const c_char,
+ va_list: *mut V) -> Result<String> {
+ vsprintf_raw(format, va_list).map(|bytes| {
+ String::from_utf8(bytes).expect("vsprintf result is not valid utf-8")
+ })
+}
+
+/// Prints a format string into a list of raw bytes that form
+/// a null-terminated C string.
+pub unsafe fn vsprintf_raw<V>(format: *const c_char,
+ va_list: *mut V) -> Result<Vec<u8>> {
+ let list_ptr = va_list as *mut c_void;
+
+ let mut buffer = Vec::new();
+ buffer.extend([0u8; INITIAL_BUFFER_SIZE].iter().cloned());
+
+ loop {
+ let character_count = vsnprintf_wrapper(
+ buffer.as_mut_ptr(), buffer.len(), format, list_ptr
+ );
+
+ // Check for errors.
+ if character_count == -1 {
+ // C does not require vsprintf to set errno, but POSIX does.
+ //
+ // Default handling will just generate an 'unknown' IO error
+ // if no errno is set.
+ return Err(Error::last_os_error());
+ } else {
+ assert!(character_count >= 0);
+ let character_count = character_count as usize;
+
+ let current_max = buffer.len() - 1;
+
+ // Check if we had enough room in the buffer to fit everything.
+ if character_count > current_max {
+ let extra_space_required = character_count - current_max;
+
+ // Reserve enough space and try again.
+ buffer.extend(repeat(0).take(extra_space_required as usize));
+ continue;
+ } else { // We fit everything into the buffer.
+ // Truncate the buffer up until the null terminator.
+ buffer = buffer.into_iter()
+ .take_while(|&b| b != 0)
+ .collect();
+ break;
+ }
+ }
+ }
+
+ Ok(buffer)
+}
+
+extern {
+ fn vsnprintf_wrapper(buffer: *mut u8,
+ size: size_t,
+ format: *const c_char,
+ va_list: *mut c_void) -> libc::c_int;
+}