include/psaml/psaml_time.h

filter: Statementstmt 99.0% 99 / 100 Branchbran 66.0% 99 / 150 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.2% 198 / 250 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/psaml/psaml_compat.h ↑ Summary xs/request.xs »
1 #ifndef PSAML_TIME_H
2 #define PSAML_TIME_H
3  
4 /* xs:dateTime, both ways, UTC only.
5 *
6 * Every timestamp SAML compares is an xs:dateTime: NotBefore,
7 * NotOnOrAfter, IssueInstant, SessionNotOnOrAfter. The comparisons decide
8 * whether a login is accepted, so the parse has to be exact and has to
9 * refuse what it does not understand rather than approximate it.
10 *
11 * timegm is not portable: it is a GNU/BSD extension, absent on Windows
12 * and not in any C standard. The alternative usually reached for is
13 * mktime with TZ forced to UTC, which mutates process-global state and
14 * is not thread-safe. So the conversion is arithmetic, using the
15 * days-from-civil algorithm: exact for every year in range, no tables,
16 * no locale, no globals.
17 *
18 * Accepted: [-]YYYY-MM-DDThh:mm:ss[.fraction][Z|(+|-)hh:mm]
19 * Refused: a missing timezone (xs:dateTime allows it and calls the value
20 * local, which is not a thing a protocol timestamp may be), a 24:00
21 * end-of-day, and anything out of range. Leap seconds (ss == 60) are
22 * refused: no identity provider emits one and accepting it would mean
23 * deciding what instant it names.
24 *
25 * The fraction is parsed and discarded. Assertion lifetimes are minutes;
26 * sub-second precision has no bearing on any comparison here, and
27 * carrying it would only invite a rounding difference between the two
28 * sides of one. */
29  
30 #include <string.h>
31  
32 /* Days from 1970-01-01 to y-m-d, proleptic Gregorian. Howard Hinnant's
33 * days_from_civil: exact for the whole range, and the shift to a
34 * March-based year is what removes the leap-day special case. */
35 591 PERL_STATIC_INLINE IV psaml_days_from_civil(IV y, unsigned m, unsigned d) {
36 IV era, doe, yoe, doy, eyr;
37 591 y -= (m <= 2);
38 591 ▶ eyr = (y >= 0 ? y : y - 399);
Branch: 1 branch
BranchTrueFalseTotal
eyr = (y >= 0 ? y : y - 399); 0 591 591
39 591 era = eyr / 400;
40 591 yoe = y - era * 400; /* [0, 399] */
41 591 ▶ doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; /* [0, 365] */
Branch: 1 branch
BranchTrueFalseTotal
doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; /* [0, 365] */ 588 3 591
42 591 doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; /* [0, 146096] */
43 591 return era * 146097 + doe - 719468;
44 }
45  
46 /* The inverse, civil_from_days. */
47 35 PERL_STATIC_INLINE void psaml_civil_from_days(IV z, IV *y, unsigned *m, unsigned *d) {
48 IV era, doe, yoe, doy, mp, yr;
49 35 z += 719468;
50 35 ▶ era = (z >= 0 ? z : z - 146096) / 146097;
Branch: 1 branch
BranchTrueFalseTotal
era = (z >= 0 ? z : z - 146096) / 146097; 0 35 35
51 35 doe = z - era * 146097; /* [0, 146096] */
52 35 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
53 35 yr = yoe + era * 400;
54 35 doy = doe - (365 * yoe + yoe / 4 - yoe / 100); /* [0, 365] */
55 35 mp = (5 * doy + 2) / 153; /* [0, 11] */
56 35 *d = (unsigned)(doy - (153 * mp + 2) / 5 + 1); /* [1, 31] */
57 35 ▶ *m = (unsigned)(mp + (mp < 10 ? 3 : -9)); /* [1, 12] */
Branch: 1 branch
BranchTrueFalseTotal
*m = (unsigned)(mp + (mp < 10 ? 3 : -9)); /* [1, 12] */ 35 0 35
58 35 *y = yr + (*m <= 2);
59 35 }
60  
61 3 PERL_STATIC_INLINE int psaml_is_leap(IV y) {
62 3 ▶ return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
Branch: 3 branches
BranchTrueFalseTotal
return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; 1 2 3
return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; 0 1 1
return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; 0 2 2
63 }
64  
65 596 PERL_STATIC_INLINE unsigned psaml_days_in_month(IV y, unsigned m) {
66 static const unsigned len[13] =
67 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
68 596 ▶ if (m == 2 && psaml_is_leap(y)) return 29;
Branch: 2 branches
BranchTrueFalseTotal
if (m == 2 && psaml_is_leap(y)) return 29; 3 593 596
if (m == 2 && psaml_is_leap(y)) return 29; 1 2 3
69 595 ▶ return (m >= 1 && m <= 12) ? len[m] : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (m >= 1 && m <= 12) ? len[m] : 0; 595 0 595
return (m >= 1 && m <= 12) ? len[m] : 0; 595 0 595
70 }
71  
72 3622 PERL_STATIC_INLINE int psaml_digits(const char *p, STRLEN n, IV *out) {
73 3622 IV v = 0;
74 STRLEN i;
75 12072 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8450 3622 12072
76 8450 ▶ if (p[i] < '0' || p[i] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] < '0' || p[i] > '9') return 0; 8450 0 8450
if (p[i] < '0' || p[i] > '9') return 0; 0 8450 8450
77 8450 v = v * 10 + (p[i] - '0');
78 }
79 3622 *out = v;
80 3622 return 1;
81 }
82  
83 /* Parses into seconds since the epoch. Returns 1 on success, 0 on any
84 * refusal, and never partially fills *out. */
85 607 PERL_STATIC_INLINE int psaml_time_parse(const char *s, STRLEN len, IV *out) {
86 IV y, mo, da, ho, mi, se, days;
87 607 IV offset = 0;
88 607 STRLEN i = 0;
89 607 int neg_year = 0;
90  
91 607 ▶ if (!s) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return 0; 0 607 607
92 607 ▶ if (i < len && s[i] == '-') { neg_year = 1; i++; }
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == '-') { neg_year = 1; i++; } 606 1 607
if (i < len && s[i] == '-') { neg_year = 1; i++; } 0 606 606
93 /* the year is at least four digits and may be longer, but not padded */
94 {
95 607 STRLEN start = i;
96 3022 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') i++; 3021 1 3022
while (i < len && s[i] >= '0' && s[i] <= '9') i++; 2417 604 3021
while (i < len && s[i] >= '0' && s[i] <= '9') i++; 2415 2 2417
97 607 ▶ if (i - start < 4) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i - start < 4) return 0; 4 603 607
98 603 ▶ if (i - start > 4 && s[start] == '0') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i - start > 4 && s[start] == '0') return 0; 0 603 603
if (i - start > 4 && s[start] == '0') return 0; 0 0 0
99 603 ▶ if (!psaml_digits(s + start, i - start, &y)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_digits(s + start, i - start, &y)) return 0; 0 603 603
100 }
101 603 ▶ if (neg_year) y = -y;
Branch: 1 branch
BranchTrueFalseTotal
if (neg_year) y = -y; 0 603 603
102 603 ▶ if (i + 1 > len || s[i] != '-') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != '-') return 0; 603 0 603
if (i + 1 > len || s[i] != '-') return 0; 0 603 603
103 603 i++;
104 603 ▶ if (i + 2 > len || !psaml_digits(s + i, 2, &mo)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 2 > len || !psaml_digits(s + i, 2, &mo)) return 0; 603 0 603
if (i + 2 > len || !psaml_digits(s + i, 2, &mo)) return 0; 0 603 603
105 603 i += 2;
106 603 ▶ if (i + 1 > len || s[i] != '-') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != '-') return 0; 603 0 603
if (i + 1 > len || s[i] != '-') return 0; 0 603 603
107 603 i++;
108 603 ▶ if (i + 2 > len || !psaml_digits(s + i, 2, &da)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 2 > len || !psaml_digits(s + i, 2, &da)) return 0; 603 0 603
if (i + 2 > len || !psaml_digits(s + i, 2, &da)) return 0; 0 603 603
109 603 i += 2;
110 603 ▶ if (i + 1 > len || s[i] != 'T') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != 'T') return 0; 602 1 603
if (i + 1 > len || s[i] != 'T') return 0; 0 602 602
111 602 i++;
112 602 ▶ if (i + 2 > len || !psaml_digits(s + i, 2, &ho)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 2 > len || !psaml_digits(s + i, 2, &ho)) return 0; 602 0 602
if (i + 2 > len || !psaml_digits(s + i, 2, &ho)) return 0; 0 602 602
113 602 i += 2;
114 602 ▶ if (i + 1 > len || s[i] != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != ':') return 0; 602 0 602
if (i + 1 > len || s[i] != ':') return 0; 0 602 602
115 602 i++;
116 602 ▶ if (i + 2 > len || !psaml_digits(s + i, 2, &mi)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 2 > len || !psaml_digits(s + i, 2, &mi)) return 0; 602 0 602
if (i + 2 > len || !psaml_digits(s + i, 2, &mi)) return 0; 0 602 602
117 602 i += 2;
118 602 ▶ if (i + 1 > len || s[i] != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != ':') return 0; 602 0 602
if (i + 1 > len || s[i] != ':') return 0; 0 602 602
119 602 i++;
120 602 ▶ if (i + 2 > len || !psaml_digits(s + i, 2, &se)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 2 > len || !psaml_digits(s + i, 2, &se)) return 0; 602 0 602
if (i + 2 > len || !psaml_digits(s + i, 2, &se)) return 0; 0 602 602
121 602 i += 2;
122  
123 602 ▶ if (i < len && s[i] == '.') { /* parsed and discarded */
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == '.') { /* parsed and discarded */ 601 1 602
if (i < len && s[i] == '.') { /* parsed and discarded */ 2 599 601
124 2 STRLEN start = ++i;
125 8 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') i++; 8 0 8
while (i < len && s[i] >= '0' && s[i] <= '9') i++; 8 0 8
while (i < len && s[i] >= '0' && s[i] <= '9') i++; 6 2 8
126 2 ▶ if (i == start) return 0; /* a '.' with no digits */
Branch: 1 branch
BranchTrueFalseTotal
if (i == start) return 0; /* a '.' with no digits */ 1 1 2
127 }
128  
129 601 ▶ if (i >= len) return 0; /* no timezone: refused */
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) return 0; /* no timezone: refused */ 1 600 601
130 600 ▶ if (s[i] == 'Z') {
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == 'Z') { 596 4 600
131 596 i++;
132 }
133 6 ▶ else if (s[i] == '+' || s[i] == '-') {
Branch: 2 branches
BranchTrueFalseTotal
else if (s[i] == '+' || s[i] == '-') { 1 3 4
else if (s[i] == '+' || s[i] == '-') { 1 0 1
134 IV oh, om;
135 4 int minus = (s[i] == '-');
136 4 i++;
137 6 ▶ if (i + 2 > len || !psaml_digits(s + i, 2, &oh)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 2 > len || !psaml_digits(s + i, 2, &oh)) return 0; 4 0 4
if (i + 2 > len || !psaml_digits(s + i, 2, &oh)) return 0; 0 4 4
138 4 i += 2;
139 4 ▶ if (i + 1 > len || s[i] != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != ':') return 0; 4 0 4
if (i + 1 > len || s[i] != ':') return 0; 1 3 4
140 3 i++;
141 3 ▶ if (i + 2 > len || !psaml_digits(s + i, 2, &om)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 2 > len || !psaml_digits(s + i, 2, &om)) return 0; 3 0 3
if (i + 2 > len || !psaml_digits(s + i, 2, &om)) return 0; 0 3 3
142 3 i += 2;
143 3 ▶ if (oh > 14 || om > 59 || (oh == 14 && om != 0)) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (oh > 14 || om > 59 || (oh == 14 && om != 0)) return 0; 2 1 3
if (oh > 14 || om > 59 || (oh == 14 && om != 0)) return 0; 2 0 2
if (oh > 14 || om > 59 || (oh == 14 && om != 0)) return 0; 0 2 2
if (oh > 14 || om > 59 || (oh == 14 && om != 0)) return 0; 0 0 0
144 2 offset = oh * 3600 + om * 60;
145 2 ▶ if (minus) offset = -offset;
Branch: 1 branch
BranchTrueFalseTotal
if (minus) offset = -offset; 1 1 2
146 }
147 else {
148 0 return 0;
149 }
150 598 ▶ if (i != len) return 0; /* trailing bytes */
Branch: 1 branch
BranchTrueFalseTotal
if (i != len) return 0; /* trailing bytes */ 1 597 598
151  
152 597 ▶ if (mo < 1 || mo > 12) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (mo < 1 || mo > 12) return 0; 597 0 597
if (mo < 1 || mo > 12) return 0; 1 596 597
153 596 ▶ if (da < 1 || (unsigned)da > psaml_days_in_month(y, (unsigned)mo)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (da < 1 || (unsigned)da > psaml_days_in_month(y, (unsigned)mo)) return 0; 596 0 596
if (da < 1 || (unsigned)da > psaml_days_in_month(y, (unsigned)mo)) return 0; 3 593 596
154 593 ▶ if (ho > 23 || mi > 59 || se > 59) return 0; /* 24:00 and :60 refused */
Branch: 3 branches
BranchTrueFalseTotal
if (ho > 23 || mi > 59 || se > 59) return 0; /* 24:00 and :60 refused */ 592 1 593
if (ho > 23 || mi > 59 || se > 59) return 0; /* 24:00 and :60 refused */ 592 0 592
if (ho > 23 || mi > 59 || se > 59) return 0; /* 24:00 and :60 refused */ 1 591 592
155  
156 591 days = psaml_days_from_civil(y, (unsigned)mo, (unsigned)da);
157 591 *out = days * 86400 + ho * 3600 + mi * 60 + se - offset;
158 591 return 1;
159 }
160  
161 /* Formats as YYYY-MM-DDThh:mm:ssZ. Writes at most 40 bytes including the
162 * NUL and returns the length written. UTC always: an identity provider
163 * that received a local time with an offset would be entitled to read it
164 * correctly and we would still have said something we did not mean. */
165 35 PERL_STATIC_INLINE STRLEN psaml_time_format(char *buf, STRLEN cap, IV t) {
166 35 IV days = t / 86400;
167 35 IV secs = t % 86400;
168 IV y;
169 unsigned mo, da;
170 int n;
171 35 ▶ if (secs < 0) { secs += 86400; days -= 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (secs < 0) { secs += 86400; days -= 1; } 0 35 35
172 35 psaml_civil_from_days(days, &y, &mo, &da);
173 35 n = my_snprintf(buf, (int)cap, "%04" IVdf "-%02u-%02uT%02u:%02u:%02uZ",
174 y, mo, da,
175 35 (unsigned)(secs / 3600), (unsigned)((secs / 60) % 60),
176 35 (unsigned)(secs % 60));
177 35 return n > 0 ? (STRLEN)n : 0;
178 }
179  
180 #endif /* PSAML_TIME_H */
« include/psaml/psaml_compat.h ↑ Summary xs/request.xs »