summaryrefslogtreecommitdiffstats
path: root/src/c/string.rs
blob: 3d991dcc0c820c4ab968b964f4ca7b191b3b3fe9 (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use super::posix;
use core::{mem, ptr, slice};
use core::ops::{Deref, DerefMut};

fn must_succeed<T>(p: *mut T) -> *mut T {
	assert!(!p.is_null(), "allocation failure");
	p
}

fn zst<T>(count: usize) -> bool {
	mem::size_of::<T>() == 0 || count == 0
}

fn alloc<T>(count: usize) -> *mut T {
	if zst::<T>(count) {
		return ptr::null_mut();
	}
	let size = count * mem::size_of::<T>();
	let align = mem::align_of::<T>();
	must_succeed(
		unsafe {
			libc::memalign(align as libc::size_t, size as libc::size_t) as *mut T
		}
	)
}

fn dangling<T>() -> *mut T {
	mem::align_of::<T>() as *mut T
}

fn check_ptr<T>(p: *const T, len: usize) {
	debug_assert!((p as usize) % mem::align_of::<T>() == 0, "unaligned ptr");
	assert!(zst::<T>(len) || !p.is_null(), "NULL ptr");
}

fn slice_len<T>(p: *const [T]) -> usize {
	unsafe { mem::transmute::<*const [T], [usize; 2]>(p)[1] }
}

pub struct CBox<T: ?Sized>(*mut T);

impl<T: ?Sized> CBox<T> {
	pub unsafe fn from_raw_unchecked(p: *mut T) -> CBox<T> {
		CBox(p)
	}

	pub fn into_raw(self) -> *mut T {
		let p = self.0;
		mem::forget(self);
		p
	}

	pub fn as_ptr(&self) -> *const T {
		self.0
	}

	pub fn as_mut_ptr(&mut self) -> *mut T {
		self.0
	}
}

impl<T> CBox<T> {
	pub fn new(value: T) -> CBox<T> {
		let p = alloc(1);
		unsafe {
			ptr::write(p, value);
			CBox::from_raw_unchecked(p)
		}
	}

	pub unsafe fn from_raw(p: *mut T) -> CBox<T> {
		check_ptr(p, 1);
		CBox(p)
	}

	pub unsafe fn slice_from_raw_parts_unchecked(p: *mut T, len: usize) -> CBox<[T]> {
		CBox(ptr::slice_from_raw_parts_mut(p, len))
	}

	pub unsafe fn slice_from_raw_parts(p: *mut T, len: usize) -> CBox<[T]> {
		check_ptr(p, len);
		CBox::slice_from_raw_parts_unchecked(p, len)
	}

	fn safe_ptr(&self) -> *mut T {
		if self.0.is_null() {
			debug_assert!(zst::<T>(1), "NULL ptr");
			return dangling();
		}

		self.0
	}
}

impl<T> CBox<[T]> {
	fn safe_ptr(&self) -> *mut [T] {
		if self.0.is_null() {
			let len = slice_len(self.0);
			debug_assert!(zst::<T>(len), "NULL ptr");
			return ptr::slice_from_raw_parts_mut(dangling(), len);
		}

		self.0
	}
}

impl<T: ?Sized> Drop for CBox<T> {
	fn drop(&mut self) {
		unsafe {
			ptr::drop_in_place(self.0);
			libc::free(self.0 as *mut libc::c_void);
		}
	}
}

impl<T> Deref for CBox<T> {
	type Target = T;

	fn deref(&self) -> &T {
		unsafe { &*self.safe_ptr() }
	}
}

impl<T> DerefMut for CBox<T> {
	fn deref_mut(&mut self) -> &mut T {
		unsafe { &mut *self.safe_ptr() }
	}
}

impl<T> Deref for CBox<[T]> {
	type Target = [T];

	fn deref(&self) -> &[T] {
		unsafe { &*self.safe_ptr() }
	}
}

impl<T> DerefMut for CBox<[T]> {
	fn deref_mut(&mut self) -> &mut [T] {
		unsafe { &mut *self.safe_ptr() }
	}
}

//pub struct FromBytesWithNulError {}

pub struct CStr { inner: libc::c_char }

impl CStr {
	pub unsafe fn from_ptr_unchecked<'a>(p: *const libc::c_char) -> &'a CStr {
		&*(p as *const CStr)
	}

	pub unsafe fn from_ptr<'a>(p: *const libc::c_char) -> &'a CStr {
		check_ptr(p, 1);
		CStr::from_ptr_unchecked(p)
	}

	pub unsafe fn from_mut_ptr_unchecked<'a>(p: *mut libc::c_char) -> &'a mut CStr {
		&mut *(p as *mut CStr)
	}

	pub unsafe fn from_mut_ptr<'a>(p: *mut libc::c_char) -> &'a mut CStr {
		check_ptr(p, 1);
		CStr::from_mut_ptr_unchecked(p)
	}

	pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
		CStr::from_ptr_unchecked(bytes.as_ptr() as *const libc::c_char)
	}

	// TODO
	//pub fn from_bytes_with_nul(bytes: &[u8]) -> Result<&CStr, FromBytesWithNulError> {
	//}

	pub fn len(&self) -> usize {
		unsafe { libc::strlen(self.as_ptr()) as usize }
	}

	pub const fn as_ptr(&self) -> *const libc::c_char {
		&self.inner
	}

	pub fn as_mut_ptr(&mut self) -> *mut libc::c_char {
		&mut self.inner
	}

	pub fn as_bytes(&self) -> &[u8] {
		unsafe { slice::from_raw_parts(
			self.as_ptr() as *const u8,
			self.len(),
		) }
	}

	pub fn as_mut_bytes(&mut self) -> &mut [u8] {
		unsafe { slice::from_raw_parts_mut(
			self.as_mut_ptr() as *mut u8,
			self.len(),
		) }
	}

	pub fn to_owned(self: &CStr) -> CString {
		CString::from(self)
	}
}

#[macro_export]
macro_rules! cstr {
	($s:expr) => (
		unsafe { $crate::c::string::CStr::from_bytes_with_nul_unchecked(concat!($s, "\0").as_bytes()) }
	)
}

pub struct CString { inner: CBox<libc::c_char> }

impl CString {
	pub unsafe fn from_raw_unchecked(p: *mut libc::c_char) -> CString {
		CString { inner: CBox::from_raw_unchecked(p) }
	}

	pub unsafe fn from_raw(p: *mut libc::c_char) -> CString {
		CString { inner: CBox::from_raw(p) }
	}

	pub fn into_raw(self) -> *mut libc::c_char {
		self.inner.into_raw()
	}
}

impl Deref for CString {
	type Target = CStr;

	fn deref(&self) -> &CStr {
		unsafe { CStr::from_ptr(&*self.inner) }
	}
}

impl DerefMut for CString {
	fn deref_mut(&mut self) -> &mut CStr {
		unsafe { CStr::from_mut_ptr(&mut *self.inner) }
	}
}

impl From<&[u8]> for CString {
	fn from(s: &[u8]) -> CString {
		unsafe {
			CString::from_raw_unchecked(must_succeed(posix::strndup(
				s.as_ptr() as *const libc::c_char,
				s.len() as libc::size_t,
			)))
		}
	}
}

impl From<&str> for CString {
	fn from(s: &str) -> CString {
		CString::from(s.as_bytes())
	}
}

impl From<&CStr> for CString {
	fn from(s: &CStr) -> CString {
		unsafe {
			CString::from_raw_unchecked(must_succeed(libc::strdup(s.as_ptr())))
		}
	}
}