-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.c
More file actions
122 lines (93 loc) · 2.33 KB
/
util.c
File metadata and controls
122 lines (93 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* util.c - Utility functions
*
* Copyright (C) 2021 Linzhi Ltd.
*
* This work is licensed under the terms of the MIT License.
* A copy of the license can be found in the file COPYING.txt
*/
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "util.h"
/* ----- Debug dumps --------------------------------------------------------*/
/*
* Decimal dump, for easy compatbility with printing results from ethash.py
*/
static void dump(const char *s, const void *p, unsigned bytes,
const char *fmt, unsigned cols)
{
unsigned i;
if (s)
printf("--- %s (%u words) ---\n", s, bytes / 4);
for (i = 0; i != bytes; i += 4)
printf(fmt, *(const uint32_t *) (p + i),
((i / 4) % cols) == cols - 1 ? '\n' : ' ');
if ((bytes / 4) % cols)
putchar('\n');
}
void dump_hex(const char *s, const void *p, unsigned bytes)
{
dump(s, p, bytes, "%08x%c", 8);
}
void dump_decimal(const char *s, const void *p, unsigned bytes)
{
dump(s, p, bytes, "%10u%c", 4);
}
void dump_blob(const char *s, const void *p, unsigned bytes)
{
unsigned i;
if (s)
printf("--- %s (%u bytes) ---\n", s, bytes);
for (i = 0; i != bytes; i++)
printf("%02x%s", *(const uint8_t *) p++,
(i & 15) == 15 ? "\n" : (i & 7) == 7 ? " " : " ");
if (bytes & 15)
putchar('\n');
}
/* ----- Hex decoding ------------------------------------------------------ */
void hex_decode_big_endian(uint8_t *res, const char *s, unsigned bytes)
{
const char *t;
unsigned v;
bool first = 1;
if (!strncmp(s, "0x", 2))
s += 2;
if (strlen(s) != bytes * 2) {
fprintf(stderr,
"expected %u instead of %u characters: \"%s\"\n",
bytes * 2, (unsigned) strlen(s), s);
exit(1);
}
for (t = s; *t; t++) {
v = *t <= '9' ? *t - '0' :
10 + (*t < 'a' ? *t - 'A' : *t - 'a');
if (v > 15) {
fprintf(stderr, "non-digit '%c' in \"%s\"\n", *t, s);
exit(1);
}
if (first) {
*res = v << 4;
} else {
*res++ |= v;
}
first = !first;
}
}
/* ----- Performance measurements ------------------------------------------ */
static struct timeval t0;
void t_start(void)
{
gettimeofday(&t0, NULL);
}
void t_print(const char *s)
{
struct timeval t;
gettimeofday(&t, NULL);
printf("%s: %g s\n", s,
t.tv_sec - t0.tv_sec + 1e-6 * (t.tv_usec - t0.tv_usec));
}