Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2020 Yubico AB. All rights reserved. |
3 | | * Use of this source code is governed by a BSD-style |
4 | | * license that can be found in the LICENSE file. |
5 | | */ |
6 | | |
7 | | #include <zlib.h> |
8 | | #include "fido.h" |
9 | | |
10 | 2.05k | #define BOUND (1024UL * 1024UL) |
11 | | |
12 | | static int |
13 | | do_compress(fido_blob_t *out, const fido_blob_t *in, size_t origsiz, int decomp) |
14 | 686 | { |
15 | 686 | u_long ilen, olen; |
16 | 686 | int r; |
17 | | |
18 | 686 | memset(out, 0, sizeof(*out)); |
19 | 686 | if (in->len > ULONG_MAX || (ilen = (u_long)in->len) > BOUND || |
20 | 686 | origsiz > ULONG_MAX || (olen = decomp ? (u_long)origsiz : |
21 | 686 | compressBound(ilen)) > BOUND) |
22 | 686 | return FIDO_ERR_INVALID_ARGUMENT; |
23 | 686 | if ((out->ptr = calloc(1, olen)) == NULL) |
24 | 686 | return FIDO_ERR_INTERNAL; |
25 | 684 | out->len = olen; |
26 | 684 | if (decomp) |
27 | 6 | r = uncompress(out->ptr, &olen, in->ptr, ilen); |
28 | 678 | else |
29 | 678 | r = compress(out->ptr, &olen, in->ptr, ilen); |
30 | 684 | if (r != Z_OK || olen > SIZE_MAX || olen > out->len) { |
31 | 0 | fido_blob_reset(out); |
32 | 0 | return FIDO_ERR_COMPRESS; |
33 | 0 | } |
34 | 684 | out->len = olen; |
35 | | |
36 | 684 | return FIDO_OK; |
37 | 684 | } |
38 | | |
39 | | int |
40 | | fido_compress(fido_blob_t *out, const fido_blob_t *in) |
41 | 679 | { |
42 | 679 | return do_compress(out, in, 0, 0); |
43 | 679 | } |
44 | | |
45 | | int |
46 | | fido_uncompress(fido_blob_t *out, const fido_blob_t *in, size_t origsiz) |
47 | 7 | { |
48 | 7 | return do_compress(out, in, origsiz, 1); |
49 | 7 | } |