-
Notifications
You must be signed in to change notification settings - Fork 6
/
hashformat.c
168 lines (141 loc) · 4.91 KB
/
hashformat.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
/* $Id: hashformat.c,v 1.2 2005/05/15 13:18:27 harbourn Exp $
* dcfldd - The Enhanced Forensic DD
* By Nicholas Harbour
*/
/* Copyright (C) 85, 90, 91, 1995-2001, 2005 Free Software Foundation, Inc.
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, 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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* GNU dd originally written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
#include "dcfldd.h"
#include "hashformat.h"
#include "log.h"
#include "util.h"
#include "sys2.h"
#include <stdio.h>
#include <sys/types.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
format_t *hashformat = NULL;
format_t *totalhashformat = NULL;
static fmtatom_op_t fmt_string_op;
static fmtatom_op_t fmt_window_start_op;
static fmtatom_op_t fmt_window_end_op;
static fmtatom_op_t fmt_winblk_start_op;
static fmtatom_op_t fmt_winblk_end_op;
static fmtatom_op_t fmt_algorithm_op;
static fmtatom_op_t *fmtatom_op_table[] =
{ /* order must conform to fmtatom_t enum */
fmt_string_op,
fmt_window_start_op,
fmt_window_end_op,
fmt_winblk_start_op,
fmt_winblk_end_op,
fmt_string_op,
fmt_algorithm_op
};
static void add_fmtatom(format_t **, fmtatom_t, void *);
static void add_fmtatom(format_t **format, fmtatom_t atom, void *data)
{
format_t *fmt;
if (*format == NULL) {
*format = malloc(sizeof **format);
fmt = *format;
} else {
/* cycle to the end of the list */
for (fmt = *format; fmt->next != NULL; fmt = fmt->next)
;
fmt->next = malloc(sizeof *fmt);
fmt = fmt->next;
}
fmt->next = NULL;
fmt->type = atom;
fmt->op = fmtatom_op_table[atom];
fmt->data = data;
}
format_t *parse_hashformat(char *str)
{
format_t *fmt = NULL;
int i;
if (str == NULL || strlen(str) == 0)
return NULL;
replace_escapes(str);
if (*str == VARIABLE_HOOK) {
for (i = 1; str[i] != '\0' && str[i] != VARIABLE_HOOK; i++)
;
if (str[i] == '\0')
user_error("invalid variable specifier \"%s\", variables should be terminated with another \'%c\'", str, VARIABLE_HOOK);
else if (i == 1) {
/* if there is two HOOKs with nothing between, remove the second and
* push up all the following chars one position */
for (i = 0; str[i] != '\0'; i++)
str[i] = str[i + 1];
} else {
str[i] = '\0';
str++;
if (STREQ(str, "window_start"))
add_fmtatom(&fmt, FMT_WINDOW_START, NULL);
else if (STREQ(str, "window_end"))
add_fmtatom(&fmt, FMT_WINDOW_END, NULL);
else if (STREQ(str, "block_start"))
add_fmtatom(&fmt, FMT_WINBLK_START, NULL);
else if (STREQ(str, "block_end"))
add_fmtatom(&fmt, FMT_WINBLK_END, NULL);
else if (STREQ(str, "hash"))
add_fmtatom(&fmt, FMT_HASH, NULL);
else if (STREQ(str, "algorithm"))
add_fmtatom(&fmt, FMT_ALGORITHM, NULL);
else
user_error("invalid variable specifier \"%c%s%c\"",
VARIABLE_HOOK, str, VARIABLE_HOOK);
fmt->next = parse_hashformat(&str[i]);
return fmt;
}
}
/* this loop needs to start at 1 so that "$$" will display a '$' properly */
for (i = 1; str[i] != '\0' && str[i] != VARIABLE_HOOK; i++)
;
add_fmtatom(&fmt, FMT_STRING, strndup(str, i));
fmt->next = parse_hashformat(&str[i]);
return fmt;
}
void print_fmt(format_t *fmt, FMTATOMOP_ARGS)
{
for (; fmt != NULL; fmt = fmt->next)
(fmt->op)(stream, wina, winb, blksize, alg, fmt->data == NULL ? data : fmt->data);
fputc('\n', stream);
}
static void fmt_string_op(FMTATOMOP_ARGS)
{
char *str = (char *)data;
fputs(str, stream);
}
static void fmt_window_start_op(FMTATOMOP_ARGS)
{
fprintf(stream, "%llu", (unsigned long long int) wina);
}
static void fmt_window_end_op(FMTATOMOP_ARGS)
{
fprintf(stream, "%llu", (unsigned long long int) winb);
}
static void fmt_winblk_start_op(FMTATOMOP_ARGS)
{
fprintf(stream, "%llu", (unsigned long long int) wina / blksize);
}
static void fmt_winblk_end_op(FMTATOMOP_ARGS)
{
fprintf(stream, "%llu", (unsigned long long int) winb / blksize);
}
static void fmt_algorithm_op(FMTATOMOP_ARGS)
{
fputs(alg, stream);
}