## Coverage Report

#### Created: 2025-03-01 02:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)

    /libfido2/src/blob.c

    Line
    Count
    Source
    1
    /*
    2
     * Copyright (c) 2018 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
     * SPDX-License-Identifier: BSD-2-Clause
    6
     */
    7
    8
    #include "fido.h"
    9
    10
    fido_blob_t *
    11
    fido_blob_new(void)
    12
    223k
    {
    13
    223k
            return calloc(1, sizeof(fido_blob_t));
    14
    223k
    }
    15
    16
    void
    17
    fido_blob_reset(fido_blob_t *b)
    18
    6.89M
    {
    19
    6.89M
            freezero(b->ptr, b->len);
    20
    6.89M
            explicit_bzero(b, sizeof(*b));
    21
    6.89M
    }
    22
    23
    int
    24
    fido_blob_set(fido_blob_t *b, const u_char *ptr, size_t len)
    25
    1.13M
    {
    26
    1.13M
            fido_blob_reset(b);
    27
    28
    1.13M
            if (ptr == NULL || len == 0) {
    29
    70.8k
                    fido_log_debug("%s: ptr=%p, len=%zu", __func__,
    30
    70.8k
                        (const void *)ptr, len);
    31
    70.8k
                    return -1;
    32
    70.8k
            }
    33
    34
    1.06M
            if ((b->ptr = malloc(len)) == NULL) {
    35
    2.82k
                    fido_log_debug("%s: malloc", __func__);
    36
    2.82k
                    return -1;
    37
    2.82k
            }
    38
    39
    1.05M
            memcpy(b->ptr, ptr, len);
    40
    1.05M
            b->len = len;
    41
    42
    1.05M
            return 0;
    43
    1.06M
    }
    44
    45
    int
    46
    fido_blob_append(fido_blob_t *b, const u_char *ptr, size_t len)
    47
    4.16k
    {
    48
    4.16k
            u_char *tmp;
    49
    50
    4.16k
            if (ptr == NULL || len == 0) {
    51
    55
                    fido_log_debug("%s: ptr=%p, len=%zu", __func__,
    52
    55
                        (const void *)ptr, len);
    53
    55
                    return -1;
    54
    55
            }
    55
    4.10k
            if (SIZE_MAX - b->len < len) {
    56
    0
                    fido_log_debug("%s: overflow", __func__);
    57
    0
                    return -1;
    58
    0
            }
    59
    4.10k
            if ((tmp = realloc(b->ptr, b->len + len)) == NULL) {
    60
    19
                    fido_log_debug("%s: realloc", __func__);
    61
    19
                    return -1;
    62
    19
            }
    63
    4.08k
            b->ptr = tmp;
    64
    4.08k
            memcpy(&b->ptr[b->len], ptr, len);
    65
    4.08k
            b->len += len;
    66
    67
    4.08k
            return 0;
    68
    4.10k
    }
    69
    70
    void
    71
    fido_blob_free(fido_blob_t **bp)
    72
    535k
    {
    73
    535k
            fido_blob_t *b;
    74
    75
    535k
            if (bp == NULL || (b = *bp) == NULL)
    76
    312k
                    return;
    77
    78
    222k
            fido_blob_reset(b);
    79
    222k
            free(b);
    80
    222k
            *bp = NULL;
    81
    222k
    }
    82
    83
    void
    84
    fido_free_blob_array(fido_blob_array_t *array)
    85
    515k
    {
    86
    515k
            if (array->ptr == NULL)
    87
    491k
                    return;
    88
    89
    689k
            for (size_t i = 0; i < array->len; i++) {
    90
    664k
                    fido_blob_t *b = &array->ptr[i];
    91
    664k
                    freezero(b->ptr, b->len);
    92
    664k
                    b->ptr = NULL;
    93
    664k
            }
    94
    95
    24.7k
            free(array->ptr);
    96
    24.7k
            array->ptr = NULL;
    97
    24.7k
            array->len = 0;
    98
    24.7k
    }
    99
    100
    cbor_item_t *
    101
    fido_blob_encode(const fido_blob_t *b)
    102
    68.9k
    {
    103
    68.9k
            if (b == NULL || b->ptr == NULL)
    104
    113
                    return NULL;
    105
    106
    68.8k
            return cbor_build_bytestring(b->ptr, b->len);
    107
    68.9k
    }
    108
    109
    int
    110
    fido_blob_decode(const cbor_item_t *item, fido_blob_t *b)
    111
    99.2k
    {
    112
    99.2k
            return cbor_bytestring_copy(item, &b->ptr, &b->len);
    113
    99.2k
    }
    114
    115
    int
    116
    fido_blob_is_empty(const fido_blob_t *b)
    117
    233k
    {
    118
    233k
            return b->ptr == NULL || b->len == 0;
    119
    233k
    }
    120
    121
    int
    122
    fido_blob_serialise(fido_blob_t *b, const cbor_item_t *item)
    123
    5.73k
    {
    124
    5.73k
            size_t alloc;
    125
    126
    5.73k
            if (!fido_blob_is_empty(b))
    127
    0
                    return -1;
    128
    5.73k
            if ((b->len = cbor_serialize_alloc(item, &b->ptr, &alloc)) == 0) {
    129
    18
                    b->ptr = NULL;
    130
    18
                    return -1;
    131
    18
            }
    132
    133
    5.71k
            return 0;
    134
    5.73k
    }