Coverage Report

Created: 2021-03-26 11:35

/libfido2/src/compress.c
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
1.72k
#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
576
{
15
576
        u_long ilen, olen;
16
576
        int r;
17
576
18
576
        memset(out, 0, sizeof(*out));
19
576
        if (in->len > ULONG_MAX || (ilen = (u_long)in->len) > BOUND ||
20
576
            origsiz > ULONG_MAX || (olen = decomp ? (u_long)origsiz :
21
576
            compressBound(ilen)) > BOUND)
22
576
                return FIDO_ERR_INVALID_ARGUMENT;
23
576
        if ((out->ptr = calloc(1, olen)) == NULL)
24
576
                return FIDO_ERR_INTERNAL;
25
572
        out->len = olen;
26
572
        if (decomp)
27
7
                r = uncompress(out->ptr, &olen, in->ptr, ilen);
28
565
        else
29
565
                r = compress(out->ptr, &olen, in->ptr, ilen);
30
572
        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
572
        out->len = olen;
35
572
36
572
        return FIDO_OK;
37
572
}
38
39
int
40
fido_compress(fido_blob_t *out, const fido_blob_t *in)
41
567
{
42
567
        return do_compress(out, in, 0, 0);
43
567
}
44
45
int
46
fido_uncompress(fido_blob_t *out, const fido_blob_t *in, size_t origsiz)
47
9
{
48
9
        return do_compress(out, in, origsiz, 1);
49
9
}