| 1 |
// Copyright 2018-2019 Mozilla |
| 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 |
use std::{ |
| 16 |
cmp::Ordering, |
| 17 |
fmt, |
| 18 |
hash::{Hash, Hasher}, |
| 19 |
ops, str, |
| 20 |
}; |
| 21 |
|
| 22 |
use crate::error::{ErrorKind, Result}; |
| 23 |
|
| 24 |
/// A GUID for an item in a bookmark tree. |
| 25 |
#[derive(Clone)] |
| 26 |
pub struct Guid(Repr); |
| 27 |
|
| 28 |
/// Indicates if the GUID is valid. Implemented for byte slices and GUIDs. |
| 29 |
pub trait IsValidGuid { |
| 30 |
fn is_valid_guid(&self) -> bool; |
| 31 |
} |
| 32 |
|
| 33 |
/// The internal representation of a GUID. Valid GUIDs are 12 bytes, and contain |
| 34 |
/// only Base64url characters; we can store them on the stack without a heap |
| 35 |
/// allocation. However, both local and remote items might have invalid GUIDs, |
| 36 |
/// in which case we fall back to a heap-allocated string. |
| 37 |
#[derive(Clone)] |
| 38 |
enum Repr { |
| 39 |
Valid([u8; 12]), |
| 40 |
Invalid(Box<str>), |
| 41 |
} |
| 42 |
|
| 43 |
/// The Places root GUID, used to root all items in a bookmark tree. |
| 44 |
pub const ROOT_GUID: Guid = Guid(Repr::Valid(*b"root________")); |
| 45 |
|
| 46 |
/// The bookmarks toolbar GUID. |
| 47 |
pub const TOOLBAR_GUID: Guid = Guid(Repr::Valid(*b"toolbar_____")); |
| 48 |
|
| 49 |
/// The bookmarks menu GUID. |
| 50 |
pub const MENU_GUID: Guid = Guid(Repr::Valid(*b"menu________")); |
| 51 |
|
| 52 |
/// The "Other Bookmarks" GUID, used to hold items without a parent. |
| 53 |
pub const UNFILED_GUID: Guid = Guid(Repr::Valid(*b"unfiled_____")); |
| 54 |
|
| 55 |
/// The mobile bookmarks GUID. |
| 56 |
pub const MOBILE_GUID: Guid = Guid(Repr::Valid(*b"mobile______")); |
| 57 |
|
| 58 |
/// The tags root GUID. |
| 59 |
pub const TAGS_GUID: Guid = Guid(Repr::Valid(*b"tags________")); |
| 60 |
|
| 61 |
const VALID_GUID_BYTES: [u8; 255] = [ |
| 62 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 63 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, |
| 64 |
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| 65 |
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
| 66 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 67 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 68 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 69 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 70 |
]; |
| 71 |
|
| 72 |
impl Guid { |
| 73 |
/// Converts a UTF-8 byte slice to a GUID. |
| 74 |
pub fn from_utf8(b: &[u8]) -> Result<Guid> { |
| 75 |
let repr = if b.is_valid_guid() { |
| 76 |
let mut bytes = [0u8; 12]; |
| 77 |
bytes.copy_from_slice(b); |
| 78 |
Repr::Valid(bytes) |
| 79 |
} else { |
| 80 |
match str::from_utf8(b) { |
| 81 |
Ok(s) => Repr::Invalid(s.into()), |
| 82 |
Err(err) => return Err(err.into()), |
| 83 |
} |
| 84 |
}; |
| 85 |
Ok(Guid(repr)) |
| 86 |
} |
| 87 |
|
| 88 |
/// Converts a UTF-16 byte slice to a GUID. |
| 89 |
pub fn from_utf16(b: &[u16]) -> Result<Guid> { |
| 90 |
let repr = if b.is_valid_guid() { |
| 91 |
let mut bytes = [0u8; 12]; |
| 92 |
for (index, &byte) in b.iter().enumerate() { |
| 93 |
if byte > u16::from(u8::max_value()) { |
| 94 |
return Err(ErrorKind::InvalidByte(byte).into()); |
| 95 |
} |
| 96 |
bytes[index] = byte as u8; |
| 97 |
} |
| 98 |
Repr::Valid(bytes) |
| 99 |
} else { |
| 100 |
match String::from_utf16(b) { |
| 101 |
Ok(s) => Repr::Invalid(s.into()), |
| 102 |
Err(err) => return Err(err.into()), |
| 103 |
} |
| 104 |
}; |
| 105 |
Ok(Guid(repr)) |
| 106 |
} |
| 107 |
|
| 108 |
/// Returns the GUID as a byte slice. |
| 109 |
#[inline] |
| 110 |
pub fn as_bytes(&self) -> &[u8] { |
| 111 |
match self.0 { |
| 112 |
Repr::Valid(ref bytes) => bytes, |
| 113 |
Repr::Invalid(ref s) => s.as_bytes(), |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/// Returns the GUID as a string slice. |
| 118 |
#[inline] |
| 119 |
pub fn as_str(&self) -> &str { |
| 120 |
// We actually could use from_utf8_unchecked here, and depending on how |
| 121 |
// often we end up doing this, it's arguable that we should. We know |
| 122 |
// already this is valid utf8, since we know that we only ever create |
| 123 |
// these while respecting is_valid (and moreover, we assert that |
| 124 |
// `s.is_char_boundary(12)` in `Guid::from`). |
| 125 |
match self.0 { |
| 126 |
Repr::Valid(ref bytes) => str::from_utf8(bytes).unwrap(), |
| 127 |
Repr::Invalid(ref s) => s, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/// Indicates if the GUID is one of the five Places built-in roots, |
| 132 |
/// including the user content roots and the tags root. |
| 133 |
#[inline] |
| 134 |
pub fn is_built_in_root(&self) -> bool { |
| 135 |
self == TOOLBAR_GUID |
| 136 |
|| self == MENU_GUID |
| 137 |
|| self == UNFILED_GUID |
| 138 |
|| self == MOBILE_GUID |
| 139 |
|| self == TAGS_GUID |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
impl IsValidGuid for Guid { |
| 144 |
#[inline] |
| 145 |
fn is_valid_guid(&self) -> bool { |
| 146 |
match self.0 { |
| 147 |
Repr::Valid(_) => true, |
| 148 |
Repr::Invalid(_) => false, |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl<T: Copy + Into<usize>> IsValidGuid for [T] { |
| 154 |
/// Equivalent to `PlacesUtils.isValidGuid`. |
| 155 |
#[inline] |
| 156 |
fn is_valid_guid(&self) -> bool { |
| 157 |
self.len() == 12 |
| 158 |
&& self |
| 159 |
.iter() |
| 160 |
.all(|&byte| VALID_GUID_BYTES.get(byte.into()).map_or(false, |&b| b == 1)) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
impl From<String> for Guid { |
| 165 |
#[inline] |
| 166 |
fn from(s: String) -> Guid { |
| 167 |
Guid::from(s.as_str()) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
impl<'a> From<&'a str> for Guid { |
| 172 |
#[inline] |
| 173 |
fn from(s: &'a str) -> Guid { |
| 174 |
let repr = if s.as_bytes().is_valid_guid() { |
| 175 |
assert!(s.is_char_boundary(12)); |
| 176 |
let mut bytes = [0u8; 12]; |
| 177 |
bytes.copy_from_slice(s.as_bytes()); |
| 178 |
Repr::Valid(bytes) |
| 179 |
} else { |
| 180 |
Repr::Invalid(s.into()) |
| 181 |
}; |
| 182 |
Guid(repr) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
impl AsRef<str> for Guid { |
| 187 |
#[inline] |
| 188 |
fn as_ref(&self) -> &str { |
| 189 |
self.as_str() |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
impl AsRef<[u8]> for Guid { |
| 194 |
#[inline] |
| 195 |
fn as_ref(&self) -> &[u8] { |
| 196 |
self.as_bytes() |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
impl ops::Deref for Guid { |
| 201 |
type Target = str; |
| 202 |
|
| 203 |
#[inline] |
| 204 |
fn deref(&self) -> &str { |
| 205 |
self.as_str() |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
impl Ord for Guid { |
| 210 |
fn cmp(&self, other: &Guid) -> Ordering { |
| 211 |
self.as_bytes().cmp(other.as_bytes()) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
impl PartialOrd for Guid { |
| 216 |
fn partial_cmp(&self, other: &Guid) -> Option<Ordering> { |
| 217 |
Some(self.cmp(other)) |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// Allow direct comparison with str |
| 222 |
impl PartialEq<str> for Guid { |
| 223 |
#[inline] |
| 224 |
fn eq(&self, other: &str) -> bool { |
| 225 |
self.as_bytes() == other.as_bytes() |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
impl<'a> PartialEq<&'a str> for Guid { |
| 230 |
#[inline] |
| 231 |
fn eq(&self, other: &&'a str) -> bool { |
| 232 |
self == *other |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
impl PartialEq for Guid { |
| 237 |
#[inline] |
| 238 |
fn eq(&self, other: &Guid) -> bool { |
| 239 |
self.as_bytes() == other.as_bytes() |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
impl<'a> PartialEq<Guid> for &'a Guid { |
| 244 |
#[inline] |
| 245 |
fn eq(&self, other: &Guid) -> bool { |
| 246 |
*self == other |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
impl Eq for Guid {} |
| 251 |
|
| 252 |
impl Hash for Guid { |
| 253 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 254 |
self.as_bytes().hash(state); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// The default Debug impl is pretty unhelpful here. |
| 259 |
impl fmt::Debug for Guid { |
| 260 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 261 |
write!(f, "Guid({:?})", self.as_str()) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
impl fmt::Display for Guid { |
| 266 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 267 |
f.write_str(self.as_str()) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
#[cfg(test)] |
| 272 |
mod tests { |
| 273 |
use super::*; |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn is_valid() { |
| 277 |
let valid_guids = &[ |
| 278 |
"bookmarkAAAA", |
| 279 |
"menu________", |
| 280 |
"__folderBB__", |
| 281 |
"queryAAAAAAA", |
| 282 |
]; |
| 283 |
for s in valid_guids { |
| 284 |
assert!(s.as_bytes().is_valid_guid(), "{:?} should validate", s); |
| 285 |
assert!(Guid::from(*s).is_valid_guid()); |
| 286 |
} |
| 287 |
|
| 288 |
let invalid_guids = &["bookmarkAAA", "folder!", "b@dgu1d!"]; |
| 289 |
for s in invalid_guids { |
| 290 |
assert!(!s.as_bytes().is_valid_guid(), "{:?} should not validate", s); |
| 291 |
assert!(!Guid::from(*s).is_valid_guid()); |
| 292 |
} |
| 293 |
|
| 294 |
let invalid_guid_bytes: &[[u8; 12]] = |
| 295 |
&[[113, 117, 101, 114, 121, 65, 225, 193, 65, 65, 65, 65]]; |
| 296 |
for bytes in invalid_guid_bytes { |
| 297 |
assert!(!bytes.is_valid_guid(), "{:?} should not validate", bytes); |
| 298 |
Guid::from_utf8(bytes).expect_err("Should not make GUID from invalid UTF-8"); |
| 299 |
} |
| 300 |
} |
| 301 |
} |