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 include/psaml/psaml_clos.h »
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 508 PERL_STATIC_INLINE IV psaml_days_from_civil(IV y, unsigned m, unsigned d) {
36 IV era, doe, yoe, doy, eyr;
37 508 y -= (m <= 2);
38 508 ▶ eyr = (y >= 0 ? y : y - 399);
Branch: 1 branch
BranchTrueFalseTotal
eyr = (y >= 0 ? y : y - 399); 0 508 508
39 508 era = eyr / 400;
40 508 yoe = y - era * 400; /* [0, 399] */
41 508 ▶ 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] */ 505 3 508
42 508 doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; /* [0, 146096] */
43 508 return era * 146097 + doe - 719468;
44 }
45  
46 /* The inverse, civil_from_days. */
47 24 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 24 z += 719468;
50 24 ▶ era = (z >= 0 ? z : z - 146096) / 146097;
Branch: 1 branch
BranchTrueFalseTotal
era = (z >= 0 ? z : z - 146096) / 146097; 0 24 24
51 24 doe = z - era * 146097; /* [0, 146096] */
52 24 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
53 24 yr = yoe + era * 400;
54 24 doy = doe - (365 * yoe + yoe / 4 - yoe / 100); /* [0, 365] */
55 24 mp = (5 * doy + 2) / 153; /* [0, 11] */
56 24 *d = (unsigned)(doy - (153 * mp + 2) / 5 + 1); /* [1, 31] */
57 24 ▶ *m = (unsigned)(mp + (mp < 10 ? 3 : -9)); /* [1, 12] */
Branch: 1 branch
BranchTrueFalseTotal
*m = (unsigned)(mp + (mp < 10 ? 3 : -9)); /* [1, 12] */ 24 0 24
58 24 *y = yr + (*m <= 2);
59 24 }
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 513 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 513 ▶ if (m == 2 && psaml_is_leap(y)) return 29;
Branch: 2 branches
BranchTrueFalseTotal
if (m == 2 && psaml_is_leap(y)) return 29; 3 510 513
if (m == 2 && psaml_is_leap(y)) return 29; 1 2 3
69 512 ▶ return (m >= 1 && m <= 12) ? len[m] : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (m >= 1 && m <= 12) ? len[m] : 0; 512 0 512
return (m >= 1 && m <= 12) ? len[m] : 0; 512 0 512
70 }
71  
72 3124 PERL_STATIC_INLINE int psaml_digits(const char *p, STRLEN n, IV *out) {
73 3124 IV v = 0;
74 STRLEN i;
75 10412 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7288 3124 10412
76 7288 ▶ if (p[i] < '0' || p[i] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] < '0' || p[i] > '9') return 0; 7288 0 7288
if (p[i] < '0' || p[i] > '9') return 0; 0 7288 7288
77 7288 v = v * 10 + (p[i] - '0');
78 }
79 3124 *out = v;
80 3124 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 524 PERL_STATIC_INLINE int psaml_time_parse(const char *s, STRLEN len, IV *out) {
86 IV y, mo, da, ho, mi, se, days;
87 524 IV offset = 0;
88 524 STRLEN i = 0;
89 524 int neg_year = 0;
90  
91 524 ▶ if (!s) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return 0; 0 524 524
92 524 ▶ if (i < len && s[i] == '-') { neg_year = 1; i++; }
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == '-') { neg_year = 1; i++; } 523 1 524
if (i < len && s[i] == '-') { neg_year = 1; i++; } 0 523 523
93 /* the year is at least four digits and may be longer, but not padded */
94 {
95 524 STRLEN start = i;
96 2607 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') i++; 2606 1 2607
while (i < len && s[i] >= '0' && s[i] <= '9') i++; 2085 521 2606
while (i < len && s[i] >= '0' && s[i] <= '9') i++; 2083 2 2085
97 524 ▶ if (i - start < 4) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i - start < 4) return 0; 4 520 524
98 520 ▶ if (i - start > 4 && s[start] == '0') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i - start > 4 && s[start] == '0') return 0; 0 520 520
if (i - start > 4 && s[start] == '0') return 0; 0 0 0
99 520 ▶ 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 520 520
100 }
101 520 ▶ if (neg_year) y = -y;
Branch: 1 branch
BranchTrueFalseTotal
if (neg_year) y = -y; 0 520 520
102 520 ▶ if (i + 1 > len || s[i] != '-') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != '-') return 0; 520 0 520
if (i + 1 > len || s[i] != '-') return 0; 0 520 520
103 520 i++;
104 520 ▶ 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; 520 0 520
if (i + 2 > len || !psaml_digits(s + i, 2, &mo)) return 0; 0 520 520
105 520 i += 2;
106 520 ▶ if (i + 1 > len || s[i] != '-') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != '-') return 0; 520 0 520
if (i + 1 > len || s[i] != '-') return 0; 0 520 520
107 520 i++;
108 520 ▶ 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; 520 0 520
if (i + 2 > len || !psaml_digits(s + i, 2, &da)) return 0; 0 520 520
109 520 i += 2;
110 520 ▶ if (i + 1 > len || s[i] != 'T') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != 'T') return 0; 519 1 520
if (i + 1 > len || s[i] != 'T') return 0; 0 519 519
111 519 i++;
112 519 ▶ 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; 519 0 519
if (i + 2 > len || !psaml_digits(s + i, 2, &ho)) return 0; 0 519 519
113 519 i += 2;
114 519 ▶ if (i + 1 > len || s[i] != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != ':') return 0; 519 0 519
if (i + 1 > len || s[i] != ':') return 0; 0 519 519
115 519 i++;
116 519 ▶ 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; 519 0 519
if (i + 2 > len || !psaml_digits(s + i, 2, &mi)) return 0; 0 519 519
117 519 i += 2;
118 519 ▶ if (i + 1 > len || s[i] != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 > len || s[i] != ':') return 0; 519 0 519
if (i + 1 > len || s[i] != ':') return 0; 0 519 519
119 519 i++;
120 519 ▶ 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; 519 0 519
if (i + 2 > len || !psaml_digits(s + i, 2, &se)) return 0; 0 519 519
121 519 i += 2;
122  
123 519 ▶ if (i < len && s[i] == '.') { /* parsed and discarded */
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == '.') { /* parsed and discarded */ 518 1 519
if (i < len && s[i] == '.') { /* parsed and discarded */ 2 516 518
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 518 ▶ if (i >= len) return 0; /* no timezone: refused */
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) return 0; /* no timezone: refused */ 1 517 518
130 517 ▶ if (s[i] == 'Z') {
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == 'Z') { 513 4 517
131 513 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 515 ▶ if (i != len) return 0; /* trailing bytes */
Branch: 1 branch
BranchTrueFalseTotal
if (i != len) return 0; /* trailing bytes */ 1 514 515
151  
152 514 ▶ if (mo < 1 || mo > 12) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (mo < 1 || mo > 12) return 0; 514 0 514
if (mo < 1 || mo > 12) return 0; 1 513 514
153 513 ▶ 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; 513 0 513
if (da < 1 || (unsigned)da > psaml_days_in_month(y, (unsigned)mo)) return 0; 3 510 513
154 510 ▶ 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 */ 509 1 510
if (ho > 23 || mi > 59 || se > 59) return 0; /* 24:00 and :60 refused */ 509 0 509
if (ho > 23 || mi > 59 || se > 59) return 0; /* 24:00 and :60 refused */ 1 508 509
155  
156 508 days = psaml_days_from_civil(y, (unsigned)mo, (unsigned)da);
157 508 *out = days * 86400 + ho * 3600 + mi * 60 + se - offset;
158 508 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 24 PERL_STATIC_INLINE STRLEN psaml_time_format(char *buf, STRLEN cap, IV t) {
166 24 IV days = t / 86400;
167 24 IV secs = t % 86400;
168 IV y;
169 unsigned mo, da;
170 int n;
171 24 ▶ if (secs < 0) { secs += 86400; days -= 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (secs < 0) { secs += 86400; days -= 1; } 0 24 24
172 24 psaml_civil_from_days(days, &y, &mo, &da);
173 24 n = my_snprintf(buf, (int)cap, "%04" IVdf "-%02u-%02uT%02u:%02u:%02uZ",
174 y, mo, da,
175 24 (unsigned)(secs / 3600), (unsigned)((secs / 60) % 60),
176 24 (unsigned)(secs % 60));
177 24 return n > 0 ? (STRLEN)n : 0;
178 }
179  
180 #endif /* PSAML_TIME_H */
« include/psaml/psaml_compat.h ↑ Summary include/psaml/psaml_clos.h »