-
Notifications
You must be signed in to change notification settings - Fork 4
/
dump.c
207 lines (184 loc) · 5.62 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Disktest
* Copyright (c) International Business Machines Corp., 2001
*
*
* 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.
*
* 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.
*
* Please send e-mail to [email protected] if you have
* questions or comments.
*
* Project Website: TBD
*
* $Id: dump.c,v 1.9 2007/02/28 06:52:21 yardleyb Exp $
*
*/
#include <stdio.h> /* *printf() */
#include <string.h> /* memset(), strn***() */
#include <ctype.h> /* isprint() */
#include <stdlib.h> /* malloc(), free() */
#include "defs.h"
#include "io.h"
#include "sfunc.h"
#include "dump.h"
int format_str(size_t iBytes, const unsigned char *ibuff, size_t ibuff_siz, unsigned char *obuff, size_t obuff_siz)
{
unsigned int i;
unsigned char buff[10];
static size_t TotalBytes = 0;
if((iBytes == 0) &&
(ibuff == NULL) && (ibuff_siz == 0) &&
(obuff == NULL) && (obuff_siz == 0)) {
TotalBytes = 0;
return 0;
}
if((ibuff == NULL) || (obuff == NULL) || (iBytes < 1)) return -1;
memset(obuff, 0, obuff_siz);
sprintf(buff,"%08lX", (long)TotalBytes);
strncat(obuff, buff, (obuff_siz-1)-strlen(obuff));
for(i=0;i<iBytes;i++) {
if((i%4) == 0) strncat(obuff, " ", (obuff_siz-1)-strlen(obuff));
if((i%8) == 0) strncat(obuff, " ", (obuff_siz-1)-strlen(obuff));
sprintf(buff,"%02X ", *(ibuff+i));
strncat(obuff, buff, (obuff_siz-1)-strlen(obuff));
}
for(;i<ibuff_siz;i++) {
if((i%4) == 0) strncat(obuff, " ", (obuff_siz-1)-strlen(obuff));
if((i%8) == 0) strncat(obuff, " ", (obuff_siz-1)-strlen(obuff));
strncat(obuff, " ", (obuff_siz-1)-strlen(obuff));
}
strncat(obuff, " ", (obuff_siz-1)-strlen(obuff));
for(i=0;i<iBytes;i++) {
sprintf(buff, "%c", (isprint(*(ibuff+i))) ? *(ibuff+i) : '.');
strncat(obuff, buff, (obuff_siz-1)-strlen(obuff));
}
TotalBytes += iBytes;
return 0;
}
int format_raw(size_t iBytes, const unsigned char *ibuff, unsigned char *obuff, size_t obuff_siz)
{
unsigned int i;
unsigned char buff[10];
static size_t TotalBytes = 0;
if((iBytes == 0) && (ibuff == NULL) &&
(obuff == NULL) && (obuff_siz == 0)) {
TotalBytes = 0;
return 0;
}
if((ibuff == NULL) || (obuff == NULL) || (iBytes < 1)) return -1;
memset(obuff, 0, obuff_siz);
sprintf(buff,"%08lX ", (long)TotalBytes);
strncat(obuff, buff, (obuff_siz-1)-strlen(obuff));
for(i=0;i<iBytes;i++) {
sprintf(buff,"%02X", *(ibuff+i));
strncat(obuff, buff, (obuff_siz-1)-strlen(obuff));
}
TotalBytes += iBytes;
return 0;
}
int dump_data(FILE *stream, const unsigned char *buff, const size_t buff_siz, const size_t ofd_siz, const size_t offset, const int format)
{
size_t TotalRemainingBytes, NumBytes, ibuff_siz, obuff_siz;
unsigned char *ibuff, *obuff, *buff_curr;
buff_curr = (unsigned char *) buff;
buff_curr += offset;
TotalRemainingBytes = buff_siz;
NumBytes = 0;
ibuff_siz = ofd_siz;
obuff_siz = 12+(3*ibuff_siz)+(ibuff_siz/4)+(ibuff_siz/8)+ibuff_siz;
switch(format) {
case FMT_STR:
format_str(0, NULL, 0, NULL, 0);
break;
case FMT_RAW:
format_raw(0, NULL, NULL, 0);
break;
default:
return(-1);
}
if((ibuff = (unsigned char *) ALLOC(ibuff_siz)) == NULL) {
fprintf(stderr, "Can't allocate ibuff\n");
return(-1);
}
if((obuff = (unsigned char *) ALLOC(obuff_siz)) == NULL) {
FREE(ibuff);
fprintf(stderr, "Can't allocate obuff\n");
return(-1);
}
while(TotalRemainingBytes > 0) {
if(TotalRemainingBytes >= ibuff_siz) {
memcpy(ibuff, buff_curr, ibuff_siz);
TotalRemainingBytes -= ibuff_siz;
NumBytes = ibuff_siz;
buff_curr += NumBytes;
} else {
memcpy(ibuff, buff_curr, TotalRemainingBytes);
NumBytes = TotalRemainingBytes;
TotalRemainingBytes = 0;
}
switch(format) {
case FMT_STR:
format_str(NumBytes, ibuff, ibuff_siz, obuff, obuff_siz);
fprintf(stream, "%s\n", obuff);
break;
case FMT_RAW:
format_raw(NumBytes, ibuff, obuff, obuff_siz);
fprintf(stream, "%s\n", obuff);
break;
default:
return(-1);
}
}
FREE(ibuff);
FREE(obuff);
return(0);
}
int do_dump(child_args_t *args)
{
size_t NumBytes = 0;
OFF_T TargetLBA, TotalBytes = 0;
unsigned char *buff;
fd_t fd;
if((buff = (unsigned char *) ALLOC(args->htrsiz*BLK_SIZE)) == NULL) {
fprintf(stderr, "Can't allocate buffer\n");
return(-1);
}
memset(buff, 0, args->htrsiz*BLK_SIZE);
fd = Open(args->device, args->flags | CLD_FLG_R);
if(INVALID_FD(fd)) {
pMsg(ERR, args, "could not open %s.\n",args->device);
pMsg(ERR, args, "%s: Error = %u\n",args->device, GETLASTERROR());
return(-1);
}
TargetLBA = Seek(fd, args->start_lba*BLK_SIZE);
if(TargetLBA != (args->start_lba * (OFF_T) BLK_SIZE)) {
pMsg(ERR, args, "Could not seek to start position.\n");
CLOSE(fd);
return(-1);
}
do {
NumBytes = Read(fd, buff, args->htrsiz*BLK_SIZE);
if((NumBytes > args->htrsiz*BLK_SIZE) || (NumBytes < 0)) {
pMsg(ERR, args, "Failure reading %s\n", args->device);
pMsg(ERR, args, "Last Error was %lu\n", GETLASTERROR());
break;
}
dump_data(stdout, buff, NumBytes, 16, 0, FMT_STR);
TotalBytes += (OFF_T) NumBytes;
} while((TotalBytes < (args->htrsiz*BLK_SIZE)) && (NumBytes > 0));
FREE(buff);
CLOSE(fd);
return 0;
}