| 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 |
45 |
| 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 |
45 |
| STRLEN blocks = n / PSAML_DEFLATE_MAX_BLOCK + 1; |
| 40 |
45 |
| 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 |
45 |
| PERL_STATIC_INLINE STRLEN psaml_deflate_stored(unsigned char *out, |
| 46 |
|
| const unsigned char *in, STRLEN n) { |
| 47 |
45 |
| unsigned char *o = out; |
| 48 |
45 |
| STRLEN i = 0; |
| 49 |
1 |
| for (;;) { |
| 50 |
46 |
| STRLEN chunk = n - i; |
| 51 |
|
| int final; |
| 52 |
46 |
▶ | if (chunk > PSAML_DEFLATE_MAX_BLOCK) chunk = PSAML_DEFLATE_MAX_BLOCK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (chunk > PSAML_DEFLATE_MAX_BLOCK) chunk = PSAML_DEFLATE_MAX_BLOCK; |
1 |
45 |
46 |
|
| 53 |
46 |
| 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 |
46 |
▶ | *o++ = (unsigned char)(final ? 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| *o++ = (unsigned char)(final ? 1 : 0); |
45 |
1 |
46 |
|
| 59 |
46 |
| *o++ = (unsigned char)( chunk & 0xFF); |
| 60 |
46 |
| *o++ = (unsigned char)((chunk >> 8) & 0xFF); |
| 61 |
46 |
| *o++ = (unsigned char)(~chunk & 0xFF); |
| 62 |
46 |
| *o++ = (unsigned char)((~chunk >> 8) & 0xFF); |
| 63 |
46 |
▶ | if (chunk) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (chunk) { |
45 |
1 |
46 |
|
| 64 |
45 |
| memcpy(o, in + i, chunk); |
| 65 |
45 |
| o += chunk; |
| 66 |
|
| } |
| 67 |
46 |
| i += chunk; |
| 68 |
46 |
▶ | if (final) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (final) break; |
45 |
1 |
46 |
|
| 69 |
|
| } |
| 70 |
45 |
| return (STRLEN)(o - out); |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| #endif /* PSAML_DEFLATE_H */ |