-
Notifications
You must be signed in to change notification settings - Fork 3
/
bsbfix.c
139 lines (118 loc) · 3.61 KB
/
bsbfix.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
/*
* bsbfix.c - Rewrite the index table in the BSB file. Useful after the
* ASCII header has been hand edited to give a valid BSB file.
*
* Copyright (C) 2004 Stuart Cunningham <stuart_hc@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: bsbfix.c,v 1.13 2007/02/05 17:11:15 mikrom Exp $
*
*/
#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#ifdef _WIN32 /* provide a WIN32 replacement for truncate() */
#include <windows.h> /* for CreateFile() et al */
/* truncate() returns 0 on success */
static int truncate(const char *path, long length)
{
HANDLE fh;
int result;
if ((fh = CreateFile(path, GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE)) <= NULL)
{
perror(path);
return 1;
}
SetFilePointer( fh, length, NULL, FILE_BEGIN );
if (! SetEndOfFile(fh))
{
LPTSTR msg;
DWORD err = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &msg, 0, NULL);
fprintf(stderr, "SetEndOfFile error (%08X) %s\n", (int)err, msg);
result = 1;
}
else
result = 0;
CloseHandle(fh);
return result;
}
#else
#include <unistd.h> /* for truncate() */
#endif
#include <bsb.h>
extern int main (int argc, char *argv[])
{
BSBImage image;
int32_t i, arg_idx, *index, delete_only = 0;
uint8_t *buf;
int height;
arg_idx = 1; // points to the first non-option arg
if (argc > 1 && (argv[1][0] == '-' && argv[1][1] == 'd'))
{
delete_only = 1;
arg_idx++;
}
if ((argc - arg_idx) != 1)
{
fprintf(stderr, "Usage:\n\tbsbfix [-d] file.kap\n");
fprintf(stderr, "\n\tRewrite the index table for the specified BSB file, editing it in place\n");
fprintf(stderr, "\n\t-d Delete the index table without restoring a correct index table\n");
exit(1);
}
if (! bsb_open_header(argv[arg_idx], &image))
{
fprintf(stderr, "Failed to open %s\n", argv[arg_idx] );
exit(1);
}
buf = (uint8_t *)malloc(image.width);
if (! buf)
exit(1);
index = (int32_t *)malloc((image.height + 1) * sizeof(int));
/* Read rows from bsb file */
for (i = 0; i < image.height; i++)
{
index[i] = ftell(image.pFile);
bsb_read_row(&image, buf);
}
free(buf);
/* record start-of-index-table file position in the index table */
index[image.height] = ftell(image.pFile);
/* remember for later as we are closing the BSB file */
height = image.height;
bsb_close(&image);
/* delete index table by truncating file */
if (truncate(argv[arg_idx], index[height]) != 0)
{
perror(argv[arg_idx]);
exit(1);
}
/* If specified, do not rewrite a correct index table */
if (delete_only)
{
free(index);
return 0;
}
/* Open the file to write new index table */
if ((image.pFile = fopen(argv[arg_idx], "ab")) == NULL)
{
perror(argv[arg_idx]);
exit(1);
}
bsb_write_index(image.pFile, height, index);
free(index);
return 0;
}