-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.c
168 lines (147 loc) · 4.43 KB
/
dump.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (C) 2002 John Todd Larason <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "rtv.h"
#include "dump.h"
#include <ctype.h>
#include <stdio.h>
#include <time.h>
static FILE * fp;
static char spaces[] = " ";
static char * leader = spaces + sizeof(spaces) - 1;
void dump_set_file(FILE * out)
{
fp = out;
}
void dump_group_start(char const * groupname)
{
fprintf(fp, "%s%s {\n", leader, groupname);
leader -= 2;
}
void dump_group_end()
{
leader += 2;
fprintf(fp, "%s}\n", leader);
}
void dump_mapping(char const * tag, u32 value, struct mapping * map)
{
int i;
for (i = 0; map[i].name; i++) {
if (map[i].value == value) {
fprintf(fp, "%s%-24s\t%lu %s\n",
leader, tag, (unsigned long)value, map[i].name);
return;
}
}
fprintf(fp, "%s%-24s\t%lu Unrecognized item\n",
leader, tag, (unsigned long)value);
}
void dump_bitmapping(char const * tag, u32 value, struct mapping * map)
{
int i;
int first = 1;
fprintf(fp, "%s%-24s\t%-8lx ", leader, tag, (unsigned long)value);
for (i = 0; map[i].name; i++) {
if (value & map[i].value) {
fprintf(fp, "%s%s", first ? "" : "; ", map[i].name);
first = 0;
value &= ~map[i].value;
if (value == 0) {
break;
}
}
}
if (value == 0) {
fprintf(fp, "\n");
return;
}
fprintf(fp, "& %02lx\n", (unsigned long) value);
}
void dump_time(char const * tag, u32 value)
{
char buffer[32];
struct tm * tm;
tm = localtime(&value);
strftime(buffer, sizeof buffer, "%Y-%m-%d %H:%M:%S", tm);
fprintf(fp, "%s%-24s\t%10lu %s\n",
leader, tag, (unsigned long)value, buffer);
}
void dump_string(char const * tag, char const * value)
{
fprintf(fp, "%s%-24s\t%s\n", leader, tag, value);
}
void dump_u8(char const * tag, u8 value)
{
fprintf(fp, "%s%-24s\t 0x%02x %12u\n", leader, tag, value, value);
}
void dump_u16(char const * tag, u16 value)
{
fprintf(fp, "%s%-24s\t 0x%04x %12u\n", leader, tag, value, value);
}
void dump_u32(char const * tag, u32 value)
{
if (value > 0x80000000)
fprintf(fp, "%s%-24s\t0x%08lx %12lu %12lu\n", leader, tag,
(unsigned long)value, (unsigned long)value,
(unsigned long)value - 0x80000000);
else
fprintf(fp, "%s%-24s\t0x%08lx %12lu\n", leader, tag,
(unsigned long)value, (unsigned long)value);
}
void dump_u64(char const * tag, u64 value)
{
fprintf(fp, "%s%-24s\t0x%016"U64F"x %24"U64F"u\n",
leader, tag, value, value);
}
void dump_buffer(char const * tag, u8 const * buffer, size_t len)
{
unsigned int row, column, byte;
#define COLUMNS 16
fprintf(fp, "%s%-24s\t", leader, tag);
for (row = 0; row < (len + COLUMNS - 1)/COLUMNS; row++) {
if (row > 0)
fprintf(fp, "%s \t", leader);
for (column = 0, byte = row * COLUMNS;
column < COLUMNS;
column++, byte++) {
if (byte < len) {
fprintf(fp, "%02x", buffer[byte]);
} else {
fprintf(fp, " ");
}
if (byte % 4 == 3)
fprintf(fp, " ");
}
for (column = 0, byte = row * COLUMNS;
column < COLUMNS;
column++, byte++) {
if (byte < len) {
fprintf(fp, "%c",
isascii(buffer[byte]) && isprint(buffer[byte]) ?
buffer[byte] : '.');
} else {
fprintf(fp, " ");
}
}
fprintf(fp, "\n");
}
#undef COLUMNS
}
char const * lookup_mapping(u32 value, struct mapping * map)
{
int i;
for (i = 0; map[i].name; i++)
if (map[i].value == value)
return map[i].name;
return NULL;
}