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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//Copyright 2018 #UlinProject Денис Котляров

//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.


/*!
Fast secure parsing /proc/cmdline".

# Use:

DefaultIter

```rust
use cluproccmdline::Cmdline;

let mut cmdline = cluproccmdline::this_machine().unwrap();
for (name, value) in cmdline.iter() {
	if let Some(name) = name {
		println!(
			"Cmdline_str: {} {}", 
			String::from_utf8(name.to_vec()).unwrap(), 
			String::from_utf8(value.to_vec()).unwrap()
		);
	}
	// OUTPUT: 
	// IF /proc/cmdline = "BOOT_IMAGE=/boot/vmlinuz-linux-zen nmi_watchdog=0"
	// TO -> "Cmdline_str: BOOT_IMAGE /boot/vmlinuz-linux-zen"
	// TO -> "Cmdline_str: nmi_watchdog 0"
}	
```

```rust
use cluproccmdline::Cmdline;

let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
let mut iter = cmdline.iter();

while let Some((Some(name), value)) = iter.next() {
	println!("{} {}", 
		String::from_utf8(name.to_vec()).unwrap(), 
		String::from_utf8(value.to_vec()).unwrap()
	);
	// OUTPUT:
	// rw
}
```

OneIter

```rust
use cluproccmdline::Cmdline;

let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
for value in cmdline.iter_one() {
	println!("{}", 
		String::from_utf8(value.to_vec()).unwrap()
	);
	// OUTPUT: 
	// rw
}
```
```rust
use cluproccmdline::Cmdline;

let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
let mut iter = cmdline.iter_one();

while let Some(value) = iter.next() {
	println!("{}", 
		String::from_utf8(value.to_vec()).unwrap()
	);
	// OUTPUT:
	// rw
}
```

TwoIter

```rust
use cluproccmdline::Cmdline;

let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
for (value, name) in cmdline.iter_two() {
	println!("{} {}", 
		String::from_utf8(name.to_vec()).unwrap(),
		String::from_utf8(value.to_vec()).unwrap()
	);
	// OUTPUT: 
	// test all
}
```

# Hash proc cmdline
```rust
use cluproccmdline::Cmdline;

let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
assert_eq!(cmdline.cmdline_hash(), 1877887864476248772);
```



# Benchmark

Machine: Intel Core 2 Duo (2000 MHz), 2 Gb DDR2

```none
test tests::bench_oneslice_new ... bench:         101 ns/iter (+/- 14)
test tests::bench_slice_new    ... bench:         105 ns/iter (+/- 23)
```

*/


#![feature(test)]

extern crate test;

//#Ulin Project 1718
//




pub mod structs;
pub mod iter;

use std::path::Path;
use std::fs::File;
use std::io::Read;
//use std::ops::Deref;
use std::fmt::Debug;

use structs::slice::CmdlineSlice;
use structs::buf::CmdlineBuf;
use iter::two::CmdlineTwoIter;
use iter::one::CmdlineOneIter;
use iter::CmdlineIter;

use std::hash::Hash;
use std::hash::Hasher;
use std::collections::hash_map::DefaultHasher;


///Main functions of Cmdline.
pub trait Cmdline: /* Deref<Target = [u8]> + */ AsRef<[u8]> + Hash + Debug + Clone + Eq + PartialEq + PartialOrd {
	///```rust
	///use cluproccmdline::Cmdline;
	///
	///let mut cmdline = cluproccmdline::this_machine().unwrap();
	///for (name, value) in cmdline.iter() {
	///	if let Some(name) = name {
	///		println!(
	///			"Cmdline_str: {} {}", 
	///			String::from_utf8(name.to_vec()).unwrap(), 
	///			String::from_utf8(value.to_vec()).unwrap()
	///		);
	///	}
	///	// OUTPUT: 
	///	// IF /proc/cmdline = "BOOT_IMAGE=/boot/vmlinuz-linux-zen nmi_watchdog=0"
	///	// TO -> "Cmdline_str: BOOT_IMAGE /boot/vmlinuz-linux-zen"
	///	// TO -> "Cmdline_str: nmi_watchdog 0"
	///}	
	///```
	///```rust
	///use cluproccmdline::Cmdline;
	///
	///let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
	///let mut iter = cmdline.iter();
	///
	///while let Some((Some(name), value)) = iter.next() {
	///	println!("{} {}", 
	///		String::from_utf8(name.to_vec()).unwrap(), 
	///		String::from_utf8(value.to_vec()).unwrap()
	///	);
	///	// OUTPUT:
	///	// rw
	///}
	///```
	
	fn iter<'i>(&'i mut self) -> CmdlineIter<'i>;
	
	
	///```rust
	///use cluproccmdline::Cmdline;
	///
	///let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
	///for value in cmdline.iter_one() {
	///	println!("{}", 
	///		String::from_utf8(value.to_vec()).unwrap()
	///	);
	///	// OUTPUT: 
	///	// rw
	///}
	///```
	///```rust
	///use cluproccmdline::Cmdline;
	///
	///let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
	///let mut iter = cmdline.iter_one();
	///
	///while let Some(value) = iter.next() {
	///	println!("{}", 
	///		String::from_utf8(value.to_vec()).unwrap()
	///	);
	///	// OUTPUT:
	///	// rw
	///}
	///```
	fn iter_one<'i>(&'i mut self) -> CmdlineOneIter<'i>;
	
	
	
	///```rust
	///use cluproccmdline::Cmdline;
	///
	///let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
	///for (value, name) in cmdline.iter_two() {
	///	println!("{} {}", 
	///		String::from_utf8(name.to_vec()).unwrap(),
	///		String::from_utf8(value.to_vec()).unwrap()
	///	);
	///	// OUTPUT: 
	///	// test all
	///}
	///```
	fn iter_two<'i>(&'i mut self) -> CmdlineTwoIter<'i>;
	
	///```rust
	///use cluproccmdline::Cmdline;
	///
	///let mut cmdline = cluproccmdline::array_slice(b"test=all rw");
	///assert_eq!(cmdline.cmdline_hash(), 1877887864476248772);
	///```
	fn cmdline_hash(&self) -> u64 {
		let mut hasher = DefaultHasher::new();
		self.hash(&mut hasher);	
		hasher.finish()
	}
}

///Opens the cmdline of the current machine. Equivalent `open_file("/proc/cmdline")`.
///```rust
///use cluproccmdline::Cmdline;
///
///let mut cmdline = cluproccmdline::this_machine().unwrap();
///for (name, value) in cmdline.iter() {
///	if let Some(name) = name {
///		println!(
///			"Cmdline_str: {} {}", 
///			String::from_utf8(name.to_vec()).unwrap(), 
///			String::from_utf8(value.to_vec()).unwrap()
///		);
///	}
///
///	// OUTPUT: 
///	// IF /proc/cmdline = "BOOT_IMAGE=/boot/vmlinuz-linux-zen nmi_watchdog=0"
///	// TO -> "Cmdline_str: BOOT_IMAGE /boot/vmlinuz-linux-zen"
///	// TO -> "Cmdline_str: nmi_watchdog 0"
///}	
///```
#[inline]
pub fn this_machine() -> Result<impl Cmdline, CmdlineErr> {
	open_file("/proc/cmdline")
}

///Opens the cmdline from the file.
pub fn open_file<P: AsRef<Path> >(path: P) -> Result<impl Cmdline, CmdlineErr> {
	match File::open(path) {
		Ok(mut file) => {
			let mut vec = Vec::with_capacity(226);
			match file.read_to_end(&mut vec) {
				Ok(size) => {
					if size == 0 {
						return Err(CmdlineErr::EmptyFile);
					}
					
					return Ok(
						array_buf(vec)
					);
				},
				Err(e) => return Err(CmdlineErr::ReadFile(e)),
			}
		},
		Err(e) => return Err(CmdlineErr::OpenFile(e)),
	}
}


///Creates cmdline from `Vec`. Equivalent `CmdlineBuf::array(array)`.
#[inline]
pub fn array_buf(array: Vec<u8>) -> impl Cmdline {
	CmdlineBuf::array(array)
}

///Creates cmdline from `&[u8]`. Equivalent `CmdlineSlice::array(array)`.
#[inline]
pub fn array_slice<'a>(array: &'a [u8]) -> impl Cmdline + 'a {
	CmdlineSlice::array(array)
}	


///Description of errors
#[derive(Debug)]
pub enum CmdlineErr {
	///Open file err
	OpenFile(::std::io::Error),
	
	///Read file err
	ReadFile(::std::io::Error),
	
	///File empty, size = 0
	EmptyFile,
}



#[cfg(test)]
mod tests {
	use super::*;
	use test::Bencher;
	
	#[test]
	fn test_basic_functional() {
		let mut cmdline = array_slice(b"BOOT_IMAGE=/boot/vmlinuz-linux-zen rw quiet");
		
		{
			//default iter
			let mut iter = cmdline.iter();
			
			assert_eq!(iter.next(), Some((Some(&b"BOOT_IMAGE"[..]), &b"/boot/vmlinuz-linux-zen"[..])));
			assert_eq!(iter.next(), Some((None, &b"rw"[..])));
			assert_eq!(iter.next(), Some((None, &b"quiet"[..])));
			assert_eq!(iter.next(), None);
		}
		
		{
			//one iter
			let mut iter = cmdline.iter_one();
			assert_eq!(iter.next(), Some(&b"rw"[..]));
			assert_eq!(iter.next(), Some(&b"quiet"[..]));
			assert_eq!(iter.next(), None);
		}
		
		{
			//two iter
			let mut iter = cmdline.iter_two();
			assert_eq!(iter.next(), Some((&b"BOOT_IMAGE"[..], &b"/boot/vmlinuz-linux-zen"[..])) );
			assert_eq!(iter.next(), None);
		}
	}
	
	
	#[bench]
	fn bench_slice_new(b: &mut Bencher) {
		let mut cmdline = array_slice(b"BOOT_IMAGE=/boot/vmlinuz-linux-zen rw quiet");
		
		b.iter(|| {			
			for (_a, _v) in cmdline.iter() {
				
			}
		});
	}
	
	#[bench]
	fn bench_oneslice_new(b: &mut Bencher) {
		let mut cmdline = array_slice(b"BOOT_IMAGE=/boot/vmlinuz-linux-zen rw quiet");
		
		b.iter(|| {
			for _n in cmdline.iter_one() {
				
			}
		});
	}
}