Coverage Report

Created: 2020-12-02 17:02

/libfido2/src/log.c
Line
Count
Source (jump to first uncovered line)
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
 */
6
7
#include <stdarg.h>
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
12
#include "fido.h"
13
14
#ifndef FIDO_NO_DIAGNOSTIC
15
16
#define XXDLEN  32
17
#define XXDROW  128
18
#define LINELEN 256
19
20
#ifndef TLS
21
#define TLS
22
#endif
23
24
static TLS int logging;
25
static TLS fido_log_handler_t *log_handler;
26
27
static void
28
log_on_stderr(const char *str)
29
0
{
30
0
        fprintf(stderr, "%s", str);
31
0
}
32
33
void
34
fido_log_init(void)
35
8.34k
{
36
8.34k
        logging = 1;
37
8.34k
        log_handler = log_on_stderr;
38
8.34k
}
39
40
void
41
fido_log_debug(const char *fmt, ...)
42
1.50M
{
43
1.50M
        char line[LINELEN];
44
1.50M
        va_list ap;
45
1.50M
        int r;
46
1.50M
47
1.50M
        if (!logging || log_handler == NULL)
48
1.50M
                return;
49
1.50M
50
1.50M
        va_start(ap, fmt);
51
1.50M
        r = vsnprintf(line, sizeof(line) - 1, fmt, ap);
52
1.50M
        va_end(ap);
53
1.50M
        if (r < 0 || (size_t)r >= sizeof(line) - 1)
54
427
                return;
55
1.50M
        strlcat(line, "\n", sizeof(line));
56
1.50M
        log_handler(line);
57
1.50M
}
58
59
void
60
fido_log_xxd(const void *buf, size_t count)
61
161k
{
62
161k
        const uint8_t *ptr = buf;
63
161k
        char row[XXDROW];
64
161k
        char xxd[XXDLEN];
65
161k
66
161k
        if (!logging || log_handler == NULL || count == 0)
67
4.45k
                return;
68
156k
69
156k
        *row = '\0';
70
156k
71
16.8M
        for (size_t i = 0; i < count; i++) {
72
16.6M
                *xxd = '\0';
73
16.6M
                if (i % 16 == 0)
74
1.10M
                        snprintf(xxd, sizeof(xxd), "%04zu: %02x", i, *ptr++);
75
15.5M
                else
76
15.5M
                        snprintf(xxd, sizeof(xxd), " %02x", *ptr++);
77
16.6M
                strlcat(row, xxd, sizeof(row));
78
16.6M
                if (i % 16 == 15 || i == count - 1) {
79
1.10M
                        fido_log_debug("%s", row);
80
1.10M
                        *row = '\0';
81
1.10M
                }
82
16.6M
        }
83
156k
}
84
85
void
86
fido_set_log_handler(fido_log_handler_t *handler)
87
8.34k
{
88
8.34k
        if (handler != NULL)
89
8.34k
                log_handler = handler;
90
8.34k
}
91
92
#endif /* !FIDO_NO_DIAGNOSTIC */