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
|
/*************************************************************************
*
* HTCd - Copyright (C) 1998-2006 Henrik Rydberg
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <hdb/db.h>
static fp_stream mout(stdout),merr(stderr);
main(int argc,char* argv[]) try
{
if(argc<2) {
mout<<"Usage: "<<argv[0]<<" [-s] <path>\n";
return -1;
}
mstring path; int silent;
if(argc>2) { silent=1; path=argv[2]; }
else { silent=0; path=argv[1]; }
mstring map; if(map.load(path)<0) THROW("hdbfix: could not open "<<path);
filemap::head_t head;
mstring version=First(sref(head.ident,sizeof(head.ident)));
memcpy(&head,map.data(),sizeof(head));
mstring cver=First(sref(head.ident,sizeof(head.ident)));
mstring answer;
int save=0;
if(version!=cver) {
merr<<path<<": Not a "<<version<<" file\n";
return -1;
}
else {
int top=head.frame.get(),last=0;
while(top<head.filetop.get()&&top<map.size()) {
filemap::rec_t* rec=(filemap::rec_t*)(map.data()+top);
if(rec->mem.get()<=0) {
if(!silent&&rec->mem.get()<0)
mout<<"ending mem: "<<rec->mem.get()<<"\n";
break;
}
last=top;
top+=rec->mem.get();
}
if(top==head.filetop.get()&&head.filesize.get()==map.size()) {
if(!silent) mout<<path<<": OK\n";
}
else {
mout<<path<<": CORRUPTED\n";
if(silent) return 0;
mout<<"\n";
mout<<"Head info:\n";
mout<<"Size: "<<head.filesize.get()<<"\n";
mout<<"Frame: "<<head.frame.get()<<"\n";
mout<<"Top: "<<head.filetop.get()<<"\n";
mout<<"\n";
mout<<"Actual sizes:\n";
mout<<"Size: "<<map.size()<<"\n";
mout<<"Last: "<<last<<"\n";
mout<<"End: "<<top<<"\n";
mout<<"\n";
if(map.size()!=head.filesize.get()) {
mout<<"Filesizes differ - do you want to set the filesize to "
<<head.filesize.get()<<"?\n";
getline(answer,stdin); answer.tolower();
if(*answer=='y') {
map.resize(head.filesize.get());
save=1;
mout<<"Done.\n";
}
else {
mout<<"No change - quitting.\n";
return 0;
}
}
int nend=0,end=head.filetop.get();
for(int p=head.filetop.get()-4;p>=last;p-=4){
int val=ntohl(*(long*)(map.data()+p));
if(p+val==end) { nend++; end=p; }
}
int gap=end-top;
mout<<"GAP DATA\n";
mout<<"==============================\n";
for(int i=top;i<end;i++) {
if(cset_printable.test(map[i])) mout.put(map[i]);
else mout<<"["<<(int)map[i]<<"]";
}
mout<<"\n==============================\n";
mout<<"\n";
mout<<"At-end sequence:\n";
mout<<"Start: "<<end<<"\n";
mout<<"Gap: "<<gap<<"\n";
mout<<"\n";
if(gap>0) {
mout<<"Do you want to mark the gap as deleted?\n";
getline(answer,stdin); answer.tolower();
if(*answer=='y') {
filemap::rec_t* rec=(filemap::rec_t*)(map.data()+top);
rec->mem.set(gap);
rec->caps.set(0);
save=1;
mout<<"Done.\n";
}
else {
mout<<"No change - quitting.\n";
return 0;
}
}
}
}
if(save) map.save(path);
return 0;
}
catch(const merror_t& e) {
merr<<e.desc<<"\n";
return -1;
}
catch(...) {
merr<<"FATAL ERROR\n";
return -1;
}
|