include/psaml/psaml_deflate.h

filter: Statementstmt 100% 21 / 21 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 29 / 29 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Punk/SAML/Time.pm ↑ Summary
1 #ifndef PSAML_DEFLATE_H
2 #define PSAML_DEFLATE_H
3  
4 /* Raw DEFLATE (RFC 1951), stored blocks only.
5 *
6 * The HTTP-Redirect binding carries the AuthnRequest as raw DEFLATE,
7 * base64, then percent-encoded. Nothing in this family links zlib and
8 * this is the only place a compressor is wanted, so rather than take a
9 * dependency for it we emit the one block type that needs no compressor
10 * at all: BTYPE 00, stored, which is a five-byte header and the bytes
11 * themselves.
12 *
13 * That is legal, complete DEFLATE. Every inflater reads it, which is
14 * what matters, because the reader here is somebody else's identity
15 * provider and it must never have to be tolerant of us.
16 *
17 * What it costs is the compression, and the redirect binding is where a
18 * URL length limit actually bites. An AuthnRequest is around 1 KB and
19 * grows by five bytes per 65535 here rather than shrinking by about
20 * two thirds, so the encoded URL is roughly three times what a real
21 * deflater would produce. It stays inside every limit that matters
22 * (Internet Explorer's 2083 characters is the historical floor and no
23 * longer applies; Apache's default is 8190), and the moment this dist
24 * signs something large enough to care, this header is where a fixed
25 * Huffman encoder goes. There is no inflater here: the SP deflates its
26 * own request and never inflates anything, because Responses arrive
27 * over the POST binding uncompressed.
28 *
29 * The caller sizes the output with psaml_deflate_bound. */
30  
31 #include <string.h>
32  
33 #define PSAML_DEFLATE_MAX_BLOCK 65535
34  
35 34 PERL_STATIC_INLINE STRLEN psaml_deflate_bound(STRLEN n) {
36 /* five bytes per block, and a final empty block when n is a multiple
37 * of the block size (including n == 0, which still needs one block to
38 * carry BFINAL) */
39 34 STRLEN blocks = n / PSAML_DEFLATE_MAX_BLOCK + 1;
40 34 return n + blocks * 5;
41 }
42  
43 /* Writes raw DEFLATE into out, which must have psaml_deflate_bound(n)
44 * bytes. Returns the number written. */
45 34 PERL_STATIC_INLINE STRLEN psaml_deflate_stored(unsigned char *out,
46 const unsigned char *in, STRLEN n) {
47 34 unsigned char *o = out;
48 34 STRLEN i = 0;
49 1 for (;;) {
50 35 STRLEN chunk = n - i;
51 int final;
52 35 ▶ if (chunk > PSAML_DEFLATE_MAX_BLOCK) chunk = PSAML_DEFLATE_MAX_BLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if (chunk > PSAML_DEFLATE_MAX_BLOCK) chunk = PSAML_DEFLATE_MAX_BLOCK; 1 34 35
53 35 final = (i + chunk == n);
54 /* BFINAL in bit 0, BTYPE 00 in bits 1-2, then the byte-aligned
55 * LEN/NLEN pair little-endian. A stored block's header is the only
56 * one in DEFLATE that is byte aligned, which is why this needs no
57 * bit writer. */
58 35 ▶ *o++ = (unsigned char)(final ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
*o++ = (unsigned char)(final ? 1 : 0); 34 1 35
59 35 *o++ = (unsigned char)( chunk & 0xFF);
60 35 *o++ = (unsigned char)((chunk >> 8) & 0xFF);
61 35 *o++ = (unsigned char)(~chunk & 0xFF);
62 35 *o++ = (unsigned char)((~chunk >> 8) & 0xFF);
63 35 ▶ if (chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunk) { 34 1 35
64 34 memcpy(o, in + i, chunk);
65 34 o += chunk;
66 }
67 35 i += chunk;
68 35 ▶ if (final) break;
Branch: 1 branch
BranchTrueFalseTotal
if (final) break; 34 1 35
69 }
70 34 return (STRLEN)(o - out);
71 }
72  
73 #endif /* PSAML_DEFLATE_H */
« blib/lib/Punk/SAML/Time.pm ↑ Summary