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
//! Internal helpers. Nothing in this module is public.
//!
//! It holds pseudo-random ids for the PDF `/ID` entry and PDF date strings.
use std::sync::atomic::{AtomicUsize, Ordering};
use crate::date::OffsetDateTime;
/// Counter that feeds [`random_number`]. File identifiers need no cryptographic security, so a
/// xorshift generator replaces a dependency on a random number crate.
static RAND_SEED: AtomicUsize = AtomicUsize::new(2100);
/// Next number of the xorshift generator. Every call advances a global counter, so the result
/// differs from call to call and is not cryptographically secure.
pub(crate) fn random_number() -> usize {
let mut x = RAND_SEED.fetch_add(21, Ordering::SeqCst);
#[cfg(target_pointer_width = "64")]
{
x ^= x << 21;
x ^= x >> 35;
x ^= x << 4;
x
}
#[cfg(target_pointer_width = "32")]
{
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
x
}
}
/// 32 pseudo-random characters from `A` to `J` (the digits of [`random_number`], mapped to letters).
pub(crate) fn random_character_string_32() -> String {
const MAX_CHARS: usize = 32;
let mut final_string = String::with_capacity(MAX_CHARS);
let mut char_pos = 0;
'outer: while char_pos < MAX_CHARS {
let rand = format!("{}", crate::utils::random_number());
// The decimal digits of a number, so `to_digit` always has an answer; each pass of the
// outer loop adds at least one character.
for digit in rand.chars().filter_map(|ch| ch.to_digit(10)) {
if char_pos < MAX_CHARS {
final_string.push(u8_to_char(digit as u8));
char_pos += 1;
} else {
break 'outer;
}
}
}
final_string
}
/// Formats `date` as a PDF date string such as `D:20170505150224+00'00'`. The time zone is always
/// written as UTC. On wasm the result is the Unix epoch.
#[cfg(target_family = "wasm")]
pub(crate) fn to_pdf_time_stamp_metadata(_date: &OffsetDateTime) -> String {
"D:19700101000000+00'00'".to_string()
}
#[cfg(not(target_family = "wasm"))]
pub(crate) fn to_pdf_time_stamp_metadata(date: &OffsetDateTime) -> String {
format!(
"D:{:04}{:02}{:02}{:02}{:02}{:02}+00'00'",
date.year(),
u8::from(date.month()),
date.day(),
date.hour(),
date.minute(),
date.second(),
)
}
/// Formats `date` as an XMP date (ISO 8601) such as `2018-09-19T10:05:05+00:00`. On wasm the
/// result is the Unix epoch. The XMP specification does not allow the `D:` prefix of PDF dates.
#[cfg(target_family = "wasm")]
pub(crate) fn to_pdf_xmp_date(_date: &OffsetDateTime) -> String {
"1970-01-01T00:00:00+00:00".to_string()
}
#[cfg(not(target_family = "wasm"))]
pub(crate) fn to_pdf_xmp_date(date: &OffsetDateTime) -> String {
// Since the time is in UTC, the offset is always +00:00.
format!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}+00:00",
date.year(),
// `u8`, not the `Month` enum: Month's Display ignores the `{:02}` width, so months 1 to 9
// would have one digit, a date that veraPDF rejects.
u8::from(date.month()),
date.day(),
date.hour(),
date.minute(),
date.second(),
)
}
/// `0 => A`, `1 => B`, and so on
#[inline(always)]
fn u8_to_char(input: u8) -> char {
b'A'.saturating_add(input) as char
}
#[cfg(test)]
mod tests {
#[cfg(not(target_family = "wasm"))]
#[test]
fn xmp_date_zero_pads_single_digit_month() {
use crate::date::{Date, DateTime, Offset, Time};
let d = DateTime::new_in_offset(
Date {
year: 2018,
month: 9,
day: 3,
},
Time {
hour: 1,
minute: 2,
second: 3,
millisecond: 0,
},
Offset {
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0,
},
);
assert_eq!(super::to_pdf_xmp_date(&d), "2018-09-03T01:02:03+00:00");
}
}