• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Syntax error: unmatched thing in thing from
16 //! std::nonstd::__map<_Cyrillic, _$$$dollars>const basic_string<epic_mystery,
17 //! mongoose_traits &lt; char>, __default_alloc_<casual_Fridays = maybe>>
18 
19 #![cfg_attr(not(test), no_std)]
20 
21 #[rustfmt::skip]
22 pub mod defs;
23 pub use defs::*;
24 
25 impl EfiGuid {
26     /// Returns a new `[EfiGuid]` using the given data.
new(data1: u32, data2: u16, data3: u16, data4: [u8; 8usize]) -> Self27     pub const fn new(data1: u32, data2: u16, data3: u16, data4: [u8; 8usize]) -> Self {
28         EfiGuid { data1, data2, data3, data4 }
29     }
30 }
31 
32 impl GblEfiPartitionName {
33     /// Decodes the UCS2 GblEfiPartitionName using buffer, and returns &str of UTF8 representation.
34     ///
35     /// Buffer must be big enough to contain UTF8 representation of the UCS2 partition name.
36     ///
37     /// Maximum partition name as UCS2 is PARTITION_NAME_LEN_U16.
38     /// And [PARTITION_NAME_LEN_U8] bytes is maximum buffer size needed for UTF8 representation.
39     ///
40     /// # Result
41     /// Ok(&str) - On success return UTF8 representation of the partition name
42     /// Err(usize) if provided buffer is too small, with the minimum buffer size as the payload.
get_str<'a>(&self, buffer_utf8: &'a mut [u8]) -> Result<&'a str, usize>43     pub fn get_str<'a>(&self, buffer_utf8: &'a mut [u8]) -> Result<&'a str, usize> {
44         let mut index = 0;
45         let chars_iter = char::decode_utf16(self.StrUtf16.iter().copied())
46             .map(|c_res| c_res.unwrap_or(char::REPLACEMENT_CHARACTER))
47             .take_while(|c| *c != '\0');
48         for c in chars_iter.clone() {
49             if c.len_utf8() <= buffer_utf8[index..].len() {
50                 index += c.encode_utf8(&mut buffer_utf8[index..]).len();
51             } else {
52                 let buffer_min_len = chars_iter.clone().map(char::len_utf8).sum();
53                 return Err(buffer_min_len);
54             }
55         }
56         // SAFETY:
57         // _unchecked should be OK here since we wrote each utf8 byte ourselves,
58         // but it's just an optimization, checked version would be fine also.
59         unsafe { Ok(core::str::from_utf8_unchecked(&buffer_utf8[..index])) }
60     }
61 }
62 
63 impl From<&[u16]> for GblEfiPartitionName {
from(value: &[u16]) -> Self64     fn from(value: &[u16]) -> Self {
65         let mut res: GblEfiPartitionName = Default::default();
66         res.StrUtf16[..value.len()].copy_from_slice(value);
67         res
68     }
69 }
70 
71 impl<const N: usize> From<[u16; N]> for GblEfiPartitionName {
from(value: [u16; N]) -> Self72     fn from(value: [u16; N]) -> Self {
73         value[..].into()
74     }
75 }
76