Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "tinyrlibc"
version = "0.3.0"
authors = ["Jonathan 'theJPster' Pallant <github@thejpster.org.uk>"]
edition = "2018"
edition = "2021"
description = "Tiny, incomplete C library for bare-metal targets, written in Stable (but Unsafe) Rust"
license-file = "LICENCES.md"
readme = "README.md"
Expand Down
31 changes: 30 additions & 1 deletion LICENCES.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,33 @@ No contributor can revoke this license.
***As far as the law allows, this software comes as is,
without any warranty or condition, and no contributor
will be liable to anyone for any damages related to this
software or this license, under any kind of legal claim.***
software or this license, under any kind of legal claim.***

# Copyright (c) 1990 Regents of the University of California

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. [rescinded 22 July 1999]
4. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
2 changes: 0 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use cc;

fn main() {
// Build our snprintf substitute (which has to be C as Rust doesn't do varargs)
cc::Build::new()
Expand Down
6 changes: 3 additions & 3 deletions src/atoi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use crate::{strtol, CChar, CInt, CLong};
///
/// ```
/// use tinyrlibc::atoi;
/// assert_eq!(unsafe { atoi(b"123".as_ptr()) }, 123);
/// assert_eq!(unsafe { atoi(b"123x".as_ptr()) }, 123);
/// assert_eq!(unsafe { atoi(b"".as_ptr()) }, 0);
/// assert_eq!(unsafe { atoi(b"123\0".as_ptr()) }, 123);
/// assert_eq!(unsafe { atoi(b"123x\0".as_ptr()) }, 123);
/// assert_eq!(unsafe { atoi(b"\0".as_ptr()) }, 0);
/// ```
#[no_mangle]
pub unsafe extern "C" fn atoi(s: *const CChar) -> CInt {
Expand Down
46 changes: 43 additions & 3 deletions src/snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ extern int32_t itoa(int64_t i, char* s, size_t s_len, uint8_t radix);
*/
extern int32_t utoa(uint64_t i, char* s, size_t s_len, uint8_t radix);

/**
* This is provided by `strtoul.rs`. It converts a string to a long.
*/
extern unsigned long int strtoul(const char* str, const char* restrict* endptr, int base);

/* ======================================================================== *
*
* Public Function Definitions
Expand Down Expand Up @@ -91,7 +96,7 @@ extern int32_t utoa(uint64_t i, char* s, size_t s_len, uint8_t radix);
* - f (decimal floating point)
* - g (the shorter of %e and %f)
* - G (the shorter of %E and %f)
* - qualifiers: L, width, precision, -, +, space-pad, zero-pad, etc
* - qualifiers: L, width, (non-string) precision, -, +, space-pad, zero-pad, etc
*
* @param str the output buffer to write to
* @param size the size of the output buffer
Expand All @@ -107,6 +112,8 @@ int vsnprintf(
bool is_escape = false;
int is_long = 0;
bool is_size_t = false;
unsigned long precision = -1;

while ( *fmt )
{
if ( is_escape )
Expand Down Expand Up @@ -315,12 +322,40 @@ int vsnprintf(
// Render %s
{
const char *s = va_arg( ap, const char* );
for ( const char* p = s; *p != '\0'; p++ )
unsigned long count = precision;

while (count > 0 && *s != '\0')
{
write_output( *p, str, size, &written );
write_output(*s, str, size, &written);

s++;
if (precision != (unsigned long)-1) {
count--;
}
}
}
break;
case '.':
// Render a precision specifier
{
// Next up is either a number or a '*' that signifies that the number is in the arguments list
char next = *++fmt;

if (next == '*')
{
precision = va_arg( ap, int );
}
else
{
precision = strtoul(fmt, &fmt, 10);
// Strtoul sets the fmt pointer to the char after the number,
// however the code expects the char before that.
fmt--;
}

is_escape = true;
}
break;
case '%':
write_output( '%', str, size, &written );
break;
Expand All @@ -329,6 +364,11 @@ int vsnprintf(
break;
}
fmt++;

if (!is_escape) {
// Reset precision if it hasn't just been assigned
precision = -1;
}
}
else
{
Expand Down
52 changes: 49 additions & 3 deletions src/snprintf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ mod test {
let mut buf = [b'\0'; 32];
assert_eq!(
unsafe { snprintf(buf.as_mut_ptr(), buf.len(), "Hi\0".as_ptr()) },
2
2,
"{}",
String::from_utf8_lossy(&buf).escape_debug(),
);
assert_eq!(
unsafe { strcmp(buf.as_ptr() as *const u8, b"Hi\0" as *const u8) },
Expand All @@ -37,7 +39,9 @@ mod test {
"World\0".as_ptr(),
)
},
13
13,
"{}",
String::from_utf8_lossy(&buf).escape_debug(),
);
assert_eq!(
unsafe { strcmp(buf.as_ptr() as *const u8, b"Hello, World!\0" as *const u8) },
Expand Down Expand Up @@ -85,7 +89,9 @@ mod test {
CULongLong::from(0xcafe1234u32),
)
},
53
53,
"{}",
String::from_utf8_lossy(&buf).escape_debug(),
);
assert_eq!(
unsafe {
Expand All @@ -97,4 +103,44 @@ mod test {
0
);
}

#[test]
fn non_null_terminated_with_length() {
let mut buf = [b'\0'; 64];
assert_eq!(
unsafe {
snprintf(
buf.as_mut_ptr(),
buf.len(),
"%.*s\0".as_ptr(),
5,
"01234567890123456789\0".as_ptr(),
)
},
5,
"{}",
String::from_utf8_lossy(&buf).escape_debug(),
);
assert_eq!(
unsafe { strcmp(buf.as_ptr() as *const u8, b"01234\0" as *const u8,) },
0
);
assert_eq!(
unsafe {
snprintf(
buf.as_mut_ptr(),
buf.len(),
"%.10s\0".as_ptr(),
"01234567890123456789\0".as_ptr(),
)
},
10,
"{}",
String::from_utf8_lossy(&buf).escape_debug(),
);
assert_eq!(
unsafe { strcmp(buf.as_ptr() as *const u8, b"0123456789\0" as *const u8,) },
0
);
}
}
2 changes: 1 addition & 1 deletion src/strstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub unsafe extern "C" fn strstr(haystack: *const CChar, needle: *const CChar) ->
}
let mut len = 0;
for (inner_idx, nec) in CStringIter::new(needle).enumerate() {
let hsc = *haystack_trim.offset(inner_idx as isize);
let hsc = *haystack_trim.add(inner_idx);
if hsc != nec {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/strtol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{CChar, CLong, CStringIter};
pub unsafe extern "C" fn strtol(s: *const CChar) -> CLong {
let mut result: CLong = 0;
for c in CStringIter::new(s) {
if c >= b'0' && c <= b'9' {
if (b'0'..=b'9').contains(&c) {
result *= 10;
result += (c - b'0') as CLong;
} else {
Expand Down
Loading