1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#![allow(non_camel_case_types)]
type size_t = usize;
type c_char = u8;
#[cfg(any(target_arch = "avr", target_arch = "msp430"))]
pub type c_int = i16;
#[cfg(any(target_arch = "avr", target_arch = "msp430"))]
pub type c_uint = u16;
#[cfg(not(any(target_arch = "avr", target_arch = "msp430")))]
pub type c_int = i32;
#[cfg(not(any(target_arch = "avr", target_arch = "msp430")))]
pub type c_uint = u32;
extern "C" {
/// Gets the length of a C-style string by finding the first nul byte after the given pointer.
pub fn strlen(s: *const c_char) -> size_t;
}
pub mod minimal_cstr {
extern crate core;
use super::{c_char, strlen};
import! {
{cmp::PartialEq, marker::PhantomData}
}
// TODO: make this a full-fledged CStr implementation so no need for to_stdlib
/// A minimal CStr implementation for use in place of `core::ffi::CStr` (unstable before 1.64)
/// and `std::ffi::CStr` (requires `std`).
///
/// To do anything meaningful with this, you must convert it to the standard library's fully
/// implemented version via [`to_stdlib`](CStr::to_stdlib).
///
/// Do not call `to_stdlib` more than once, as every call runs `strlen` to determine the length
/// of the `CStr`.
#[repr(transparent)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CStr<'a> {
inner: *const c_char,
_marker: PhantomData<&'a [c_char]>
}
#[cfg(feature = "to_core_cstr")]
/// Alias for stdlib's CStr.
pub type StdCStr = core::ffi::CStr;
#[cfg(all(not(feature = "to_core_cstr"), feature = "std"))]
/// Alias for stdlib's CStr.
pub type StdCStr = ::std::ffi::CStr;
impl PartialEq<*const u8> for CStr<'_> {
#[allow(clippy::inline_always)]
#[inline(always)]
fn eq(&self, other: &*const u8) -> bool {
self.inner == *other
}
}
impl<'a> PartialEq<CStr<'a>> for *const u8 {
#[allow(clippy::inline_always)]
#[inline(always)]
fn eq(&self, other: &CStr<'a>) -> bool {
*self == other.inner
}
}
#[allow(clippy::inline_always)]
#[allow(clippy::len_without_is_empty)]
impl<'a> CStr<'a> {
/// Gets a pointer to the start of this `CStr`.
///
/// # Examples
///
/// ```
/// // this is necessary for the test to not cause UB with miri; this shouldn't be necessary
/// // in normal code.
/// const ARGS: [*const u8; 1] = ["argument\0".as_ptr()];
/// unsafe { snailx::direct::set_argc_argv(ARGS.len() as u32, ARGS.as_ptr()) };
///
/// let first = snailx::Args::new().as_slice().get(0).copied();
/// if let Some(c) = first {
/// let p = c.as_ptr();
/// assert!(!p.is_null());
/// }
/// ```
#[must_use]
#[inline(always)]
pub const fn as_ptr(&self) -> *const u8 {
self.inner.cast()
}
/// Gets the length of this `CStr`.
///
/// Avoid calling this function more than once.
///
/// # Examples
///
/// ```
/// // this is necessary for the test to not cause UB with miri; this shouldn't be necessary
/// // in normal code.
/// const ARGS: [*const u8; 1] = ["argument\0".as_ptr()];
/// unsafe { snailx::direct::set_argc_argv(ARGS.len() as u32, ARGS.as_ptr()) };
///
/// let first = snailx::Args::new().as_slice().get(0).copied();
/// if let Some(c) = first {
/// let _l = c.len();
/// }
/// ```
#[must_use]
#[inline(always)]
pub fn len(&self) -> usize {
unsafe { strlen(self.inner) }
}
#[cfg(all(feature = "std", not(feature = "to_core_cstr")))]
/// Converts this value into the `std` equivalent.
///
/// # Examples
///
/// ```
/// // this is necessary for the test to not cause UB with miri; this shouldn't be necessary
/// // in normal code.
/// const ARGS: [*const u8; 1] = ["argument\0".as_ptr()];
/// unsafe { snailx::direct::set_argc_argv(ARGS.len() as u32, ARGS.as_ptr()) };
///
/// let first = snailx::Args::new().as_slice().get(0).copied();
/// if let Some(c) = first {
/// let s = c.to_stdlib().to_string_lossy();
/// let _ = s.len();
/// }
/// ```
#[must_use]
#[inline(always)]
pub fn to_stdlib(&self) -> &'a ::std::ffi::CStr {
// SAFETY: from_ptr requires that the pointer is a valid CStr
unsafe {
assume!(!self.inner.is_null());
let bytes =
switch!(core::slice::from_raw_parts(self.inner, strlen(self.inner.cast()) + 1));
assume!(
!bytes.is_empty() && bytes[bytes.len() - 1] == 0,
"CStr does not end with null byte"
);
&*(bytes as *const [u8] as *const ::std::ffi::CStr)
}
}
#[cfg(feature = "to_core_cstr")]
/// Converts this value into the `core` equivalent.
///
/// # Examples
///
/// ```
/// // this is necessary for the test to not cause UB with miri; this shouldn't be necessary
/// // in normal code.
/// const ARGS: [*const u8; 1] = ["argument\0".as_ptr()];
/// unsafe { snailx::direct::set_argc_argv(ARGS.len() as u32, ARGS.as_ptr()) };
///
/// let first = snailx::Args::new().as_slice().get(0).copied();
/// if let Some(c) = first {
/// let core_cstr = c.to_stdlib();
/// // Access the raw bytes (without the terminating nul):
/// let _ = core_cstr.to_bytes().len();
/// }
/// ```
#[must_use]
#[inline(always)]
pub fn to_stdlib(&self) -> &'a core::ffi::CStr {
// SAFETY: from_ptr requires that the pointer is a valid CStr
unsafe {
assume!(!self.inner.is_null());
let bytes =
switch!(core::slice::from_raw_parts(self.inner, strlen(self.inner.cast()) + 1));
assume!(
!bytes.is_empty() && bytes[bytes.len() - 1] == 0,
"CStr does not end with null byte"
);
&*(bytes as *const [u8] as *const core::ffi::CStr)
}
}
/// Creates a `CStr` from a pointer to its first byte.
///
/// # Examples
///
/// ```
/// // this is necessary for the test to not cause UB with miri; this shouldn't be necessary
/// // in normal code.
/// const ARGS: [*const u8; 1] = ["argument\0".as_ptr()];
/// unsafe { snailx::direct::set_argc_argv(ARGS.len() as u32, ARGS.as_ptr()) };
///
/// let first = snailx::Args::new().as_slice().get(0).copied();
/// if let Some(c) = first {
/// let p = c.as_ptr();
/// // SAFETY: Using a pointer provided by the OS for argv is valid here.
/// // Note that this operation is redundant and only an example; `first` was already a
/// // CStr.
/// let rebuilt = unsafe { snailx::CStr::from_ptr(p) };
/// assert_eq!(rebuilt.as_ptr(), p);
/// }
/// ```
///
/// # Safety
///
/// - The memory pointed to by `ptr` must contain a valid nul terminator at the end of the
/// string.
/// - `ptr` must be valid for reads of bytes up to and including the nul terminator. This
/// means in particular:
/// - The entire memory range of this `CStr` must be contained within a single
/// allocation!
/// - `ptr` must be non-null even for a zero-length cstr.
/// - The memory referenced by the returned `CStr` must not be mutated for the duration of
/// lifetime `'a`.
/// - The nul terminator must be within `isize::MAX` bytes from `ptr`
#[must_use]
#[inline(always)]
pub unsafe fn from_ptr(p: *const u8) -> CStr<'a> {
assume!(
dbg,
{
let len = strlen(p.cast());
let bytes = switch!(core::slice::from_raw_parts(p, len + 1));
!bytes.is_empty() && bytes[len] == 0
},
"CStr does not end with null byte"
);
CStr { inner: p, _marker: PhantomData }
}
}
#[cfg(all(feature = "std", not(feature = "to_core_cstr")))]
#[allow(unused_qualifications)]
impl<'a> core::convert::From<CStr<'a>> for &'a ::std::ffi::CStr {
fn from(c: CStr<'a>) -> &'a ::std::ffi::CStr {
c.to_stdlib()
}
}
#[cfg(feature = "to_core_cstr")]
#[allow(unused_qualifications)]
impl<'a> core::convert::From<CStr<'a>> for &'a core::ffi::CStr {
fn from(c: CStr<'a>) -> &'a core::ffi::CStr {
c.to_stdlib()
}
}
}