Coverage Report

Created: 2020-03-07 10:10

/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
19.9k
{
36
19.9k
        logging = 1;
37
19.9k
        log_handler = log_on_stderr;
38
19.9k
}
39
40
void
41
fido_log_debug(const char *fmt, ...)
42
4.00M
{
43
4.00M
        char line[LINELEN];
44
4.00M
        va_list ap;
45
4.00M
        int r;
46
4.00M
47
4.00M
        if (!logging || log_handler == NULL)
48
4.00M
                return;
49
4.00M
50
4.00M
        va_start(ap, fmt);
51
4.00M
        r = vsnprintf(line, sizeof(line) - 1, fmt, ap);
52
4.00M
        va_end(ap);
53
4.00M
        if (r < 0 || (size_t)r >= sizeof(line) - 1)
54
1.13k
                return;
55
4.00M
        strlcat(line, "\n", sizeof(line));
56
4.00M
        log_handler(line);
57
4.00M
}
58
59
void
60
fido_log_xxd(const void *buf, size_t count)
61
360k
{
62
360k
        const uint8_t *ptr = buf;
63
360k
        char row[XXDROW];
64
360k
        char xxd[XXDLEN];
65
360k
66
360k
        if (!logging || log_handler == NULL || count == 0)
67
8.48k
                return;
68
351k
69
351k
        *row = '\0';
70
351k
71
46.7M
        for (size_t i = 0; i < count; i++) {
72
46.4M
                *xxd = '\0';
73
46.4M
                if (i % 16 == 0)
74
3.02M
                        snprintf(xxd, sizeof(xxd), "%04zu: %02x", i, *ptr++);
75
43.3M
                else
76
43.3M
                        snprintf(xxd, sizeof(xxd), " %02x", *ptr++);
77
46.4M
                strlcat(row, xxd, sizeof(row));
78
46.4M
                if (i % 16 == 15 || i == count - 1) {
79
3.02M
                        fido_log_debug("%s", row);
80
3.02M
                        *row = '\0';
81
3.02M
                }
82
46.4M
        }
83
351k
}
84
85
void
86
fido_set_log_handler(fido_log_handler_t *handler)
87
19.9k
{
88
19.9k
        if (handler != NULL)
89
19.9k
                log_handler = handler;
90
19.9k
}
91
92
#endif /* !FIDO_NO_DIAGNOSTIC */