Coverage Report

Created: 2020-09-01 07:05

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