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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#[allow(clippy::useless_attribute)]
#[allow(unused)]
#[allow(deprecated)]
use std::ascii::AsciiExt;
use std::borrow::Borrow;
use std::cmp::{Ordering, PartialEq};
use std::fmt::{self, Debug, Display, Formatter, Write};
use std::hash::{Hash, Hasher};
use std::sync::Arc as Rc;
use idna;
use log::debug;
use crate::error::*;
const WILDCARD: &[u8] = b"*";
const IDNA_PREFIX: &[u8] = b"xn--";
#[derive(Clone, Eq)]
pub struct Label(Rc<[u8]>);
impl Label {
pub fn from_raw_bytes(bytes: &[u8]) -> ProtoResult<Self> {
if bytes.len() > 63 {
return Err(format!("Label exceeds maximum length 63: {}", bytes.len()).into());
};
Ok(Label(Rc::from(bytes)))
}
pub fn from_utf8(s: &str) -> ProtoResult<Self> {
if s.as_bytes() == WILDCARD {
return Ok(Label::wildcard());
}
if s.starts_with('_') {
return Self::from_ascii(s);
}
match idna::Config::default()
.use_std3_ascii_rules(true)
.transitional_processing(true)
.verify_dns_length(true)
.to_ascii(s)
{
Ok(puny) => Self::from_ascii(&puny),
e => Err(format!("Label contains invalid characters: {:?}", e).into()),
}
}
pub fn from_ascii(s: &str) -> ProtoResult<Self> {
if s.as_bytes() == WILDCARD {
return Ok(Label::wildcard());
}
if !s.is_empty()
&& s.is_ascii()
&& s.chars().take(1).all(|c| is_safe_ascii(c, true, false))
&& s.chars().skip(1).all(|c| is_safe_ascii(c, false, false))
{
Label::from_raw_bytes(s.as_bytes())
} else {
Err(format!("Malformed label: {}", s).into())
}
}
pub fn wildcard() -> Self {
Label(Rc::from(WILDCARD.to_vec()))
}
pub fn to_lowercase(&self) -> Self {
if let Some((idx, _)) = self
.0
.iter()
.enumerate()
.find(|&(_, c)| *c != c.to_ascii_lowercase())
{
let mut lower_label: Vec<u8> = self.0.to_vec();
lower_label[idx..].make_ascii_lowercase();
Label(Rc::from(lower_label))
} else {
self.clone()
}
}
pub fn is_wildcard(&self) -> bool {
self.as_bytes() == WILDCARD
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
self.0.eq_ignore_ascii_case(&other.0)
}
pub fn cmp_with_f<F: LabelCmp>(&self, other: &Self) -> Ordering {
let s = self.0.iter();
let o = other.0.iter();
for (s, o) in s.zip(o) {
match F::cmp_u8(*s, *o) {
Ordering::Equal => continue,
not_eq => return not_eq,
}
}
self.0.len().cmp(&other.0.len())
}
pub fn to_utf8(&self) -> String {
format!("{}", self)
}
pub fn to_ascii(&self) -> String {
let mut ascii = String::with_capacity(self.as_bytes().len());
self.write_ascii(&mut ascii)
.expect("should never fail to write a new string");
ascii
}
pub fn write_ascii<W: Write>(&self, f: &mut W) -> Result<(), fmt::Error> {
fn escape_non_ascii<W: Write>(
byte: u8,
f: &mut W,
is_first: bool,
) -> Result<(), fmt::Error> {
let to_triple_escape = |ch: u8| format!("\\{:03o}", ch);
let to_single_escape = |ch: char| format!("\\{}", ch);
match char::from(byte) {
c if is_safe_ascii(c, is_first, true) => f.write_char(c)?,
c if byte > b'\x20' && byte < b'\x7f' => f.write_str(&to_single_escape(c))?,
_ => f.write_str(&to_triple_escape(byte))?,
}
Ok(())
}
let mut chars = self.as_bytes().iter();
if let Some(ch) = chars.next() {
escape_non_ascii(*ch, f, true)?;
}
for ch in chars {
escape_non_ascii(*ch, f, false)?;
}
Ok(())
}
}
impl AsRef<[u8]> for Label {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl Borrow<[u8]> for Label {
fn borrow(&self) -> &[u8] {
&self.0
}
}
fn is_safe_ascii(c: char, is_first: bool, for_encoding: bool) -> bool {
match c {
c if !c.is_ascii() => false,
c if c.is_alphanumeric() => true,
'-' if !is_first => true,
'_' => true,
'*' if is_first => true,
'.' if !for_encoding => true,
_ => false,
}
}
impl Display for Label {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
if self.as_bytes().starts_with(IDNA_PREFIX) {
let label = String::from_utf8_lossy(self.borrow());
let (label, e) = idna::Config::default()
.use_std3_ascii_rules(false)
.transitional_processing(false)
.verify_dns_length(false)
.to_unicode(&label);
if e.is_ok() {
return f.write_str(&label);
} else {
debug!(
"xn-- prefixed string did not translate via IDNA properly: {:?}",
e
)
}
}
self.write_ascii(f)
}
}
impl Debug for Label {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
let label = String::from_utf8_lossy(self.borrow());
f.write_str(&label)
}
}
impl PartialEq<Label> for Label {
fn eq(&self, other: &Self) -> bool {
self.eq_ignore_ascii_case(other)
}
}
impl PartialOrd<Label> for Label {
fn partial_cmp(&self, other: &Label) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Label {
fn cmp(&self, other: &Self) -> Ordering {
self.cmp_with_f::<CaseInsensitive>(other)
}
}
impl Hash for Label {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
for b in self.borrow() as &[u8] {
state.write_u8(b.to_ascii_lowercase());
}
}
}
pub trait LabelCmp {
fn cmp_u8(l: u8, r: u8) -> Ordering;
}
pub(super) struct CaseSensitive;
impl LabelCmp for CaseSensitive {
fn cmp_u8(l: u8, r: u8) -> Ordering {
l.cmp(&r)
}
}
pub(super) struct CaseInsensitive;
impl LabelCmp for CaseInsensitive {
fn cmp_u8(l: u8, r: u8) -> Ordering {
l.to_ascii_lowercase().cmp(&r.to_ascii_lowercase())
}
}
pub trait IntoLabel: Sized {
fn into_label(self: Self) -> ProtoResult<Label>;
}
impl<'a> IntoLabel for &'a Label {
fn into_label(self: Self) -> ProtoResult<Label> {
Ok(self.clone())
}
}
impl IntoLabel for Label {
fn into_label(self: Self) -> ProtoResult<Label> {
Ok(self)
}
}
impl<'a> IntoLabel for &'a str {
fn into_label(self: Self) -> ProtoResult<Label> {
Label::from_utf8(self)
}
}
impl IntoLabel for String {
fn into_label(self: Self) -> ProtoResult<Label> {
Label::from_utf8(&self)
}
}
impl<'a> IntoLabel for &'a [u8] {
fn into_label(self: Self) -> ProtoResult<Label> {
Label::from_raw_bytes(self)
}
}
impl IntoLabel for Vec<u8> {
fn into_label(self: Self) -> ProtoResult<Label> {
Label::from_raw_bytes(&self)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::dbg_macro, clippy::print_stdout)]
use super::*;
#[test]
fn test_encoding() {
assert_eq!(
Label::from_utf8("abc").unwrap(),
Label::from_raw_bytes(b"abc").unwrap()
);
assert_eq!(
Label::from_utf8("ABC").unwrap(),
Label::from_raw_bytes(b"ABC").unwrap()
);
assert_eq!(
Label::from_utf8("🦀").unwrap(),
Label::from_raw_bytes(b"xn--zs9h").unwrap()
);
assert_eq!(
Label::from_utf8("rust-🦀-icon").unwrap(),
Label::from_raw_bytes(b"xn--rust--icon-9447i").unwrap()
);
assert_eq!(
Label::from_ascii("ben.fry").unwrap(),
Label::from_raw_bytes(b"ben.fry").unwrap()
);
assert_eq!(Label::from_utf8("🦀").unwrap().to_utf8(), "🦀");
assert_eq!(Label::from_utf8("🦀").unwrap().to_ascii(), "xn--zs9h");
}
#[test]
fn test_decoding() {
assert_eq!(Label::from_raw_bytes(b"abc").unwrap().to_string(), "abc");
assert_eq!(
Label::from_raw_bytes(b"xn--zs9h").unwrap().to_string(),
"🦀"
);
assert_eq!(
Label::from_raw_bytes(b"xn--rust--icon-9447i")
.unwrap()
.to_string(),
"rust-🦀-icon"
);
}
#[test]
fn test_to_lowercase() {
assert_ne!(Label::from_ascii("ABC").unwrap().to_string(), "abc");
assert_ne!(Label::from_ascii("abcDEF").unwrap().to_string(), "abcdef");
assert_eq!(
Label::from_ascii("ABC").unwrap().to_lowercase().to_string(),
"abc"
);
assert_eq!(
Label::from_ascii("abcDEF")
.unwrap()
.to_lowercase()
.to_string(),
"abcdef"
);
}
#[test]
fn test_to_cmp_f() {
assert_eq!(
Label::from_ascii("ABC")
.unwrap()
.cmp_with_f::<CaseInsensitive>(&Label::from_ascii("abc").unwrap()),
Ordering::Equal
);
assert_eq!(
Label::from_ascii("abcDEF")
.unwrap()
.cmp_with_f::<CaseInsensitive>(&Label::from_ascii("abcdef").unwrap()),
Ordering::Equal
);
assert_eq!(
Label::from_ascii("ABC")
.unwrap()
.cmp_with_f::<CaseSensitive>(&Label::from_ascii("abc").unwrap()),
Ordering::Less
);
assert_eq!(
Label::from_ascii("abcDEF")
.unwrap()
.cmp_with_f::<CaseSensitive>(&Label::from_ascii("abcdef").unwrap()),
Ordering::Less
);
}
#[test]
fn test_partial_cmp() {
let comparisons: Vec<(Label, Label)> = vec![
(
Label::from_raw_bytes(b"yljkjljk").unwrap(),
Label::from_raw_bytes(b"Z").unwrap(),
),
(
Label::from_raw_bytes(b"Z").unwrap(),
Label::from_raw_bytes(b"zABC").unwrap(),
),
(
Label::from_raw_bytes(&[1]).unwrap(),
Label::from_raw_bytes(b"*").unwrap(),
),
(
Label::from_raw_bytes(b"*").unwrap(),
Label::from_raw_bytes(&[200]).unwrap(),
),
];
for (left, right) in comparisons {
println!("left: {}, right: {}", left, right);
assert_eq!(left.cmp(&right), Ordering::Less);
}
}
#[test]
fn test_is_wildcard() {
assert!(Label::from_raw_bytes(b"*").unwrap().is_wildcard());
assert!(Label::from_ascii("*").unwrap().is_wildcard());
assert!(Label::from_utf8("*").unwrap().is_wildcard());
assert!(!Label::from_raw_bytes(b"abc").unwrap().is_wildcard());
}
#[test]
fn test_ascii_escape() {
assert_eq!(
Label::from_raw_bytes(&[0o200]).unwrap().to_string(),
"\\200"
);
assert_eq!(
Label::from_raw_bytes(&[0o001]).unwrap().to_string(),
"\\001"
);
assert_eq!(Label::from_ascii(".").unwrap().to_ascii(), "\\.");
assert_eq!(
Label::from_ascii("ben.fry").unwrap().to_string(),
"ben\\.fry"
);
assert_eq!(Label::from_raw_bytes(&[0o200]).unwrap().to_ascii(), "\\200");
}
}