-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlumped.c
More file actions
442 lines (419 loc) · 11 KB
/
lumped.c
File metadata and controls
442 lines (419 loc) · 11 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#if 0
gcc $CFLAGS -c -Wno-unused-result lumped.c `sdl-config --cflags`
exit
#endif
#include "common.h"
#include <fnmatch.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
typedef struct {
char name[16]; // name in uppercase (null-terminated)
Uint32 offset; // offset of beginning of data; if zero, then it is a new lump
Uint32 length;
Uint8*data; // if changed or in use
Uint32 refcount;
Uint8 flag; // bit0=changed bit1=writing
} Lump;
char*world_name;
Uint32 lump_size;
static FILE*worldfile;
static Lump*lumps;
static int nlumps;
typedef struct {
char name[16];
Uint32 length;
Uint32 wlength;
Uint8*data;
Uint32 pos;
Uint32 id;
Uint8 writing;
} LumpCookie;
static void convert_lump_name(const char*in,char*out) {
int i;
for(i=0;i<15;i++) {
out[i]=in[i];
if(!in[i]) break;
if(in[i]>='a' && in[i]<='z') out[i]+='A'-'a';
}
out[15]=0;
}
static int compare_lump_name(const void*a,const void*b) {
const Lump*x=a;
const Lump*y=b;
return strcmp(x->name,y->name);
}
static ssize_t lump_read(void*cookie,char*buf,size_t size) {
LumpCookie*co=cookie;
if(co->pos>=co->length || !size) return 0;
if(size>co->length-co->pos) size=co->length-co->pos;
memcpy(buf,co->data+co->pos,size);
co->pos+=size;
return size;
}
static ssize_t lump_write(void*cookie,const char*buf,size_t size) {
LumpCookie*co=cookie;
if(!size) return 0;
if(co->pos+size>co->length) co->length=co->pos+size;
if(co->pos+size>co->wlength) {
if(co->wlength>=1024) co->wlength+=co->wlength>>1; else co->wlength=1024;
if(co->pos+size>co->wlength) co->wlength=co->pos+size;
co->data=realloc(co->data,co->wlength);
if(!co->data) err(1,"Allocation failed");
}
memcpy(co->data+co->pos,buf,size);
co->pos+=size;
return size;
}
static int lump_seek(void*cookie,off64_t*offset,int whence) {
LumpCookie*co=cookie;
switch(whence) {
case SEEK_SET: co->pos=*offset; break;
case SEEK_CUR: co->pos+=*offset; break;
case SEEK_END: co->pos=co->length+*offset; break;
default: return -1;
}
*offset=co->pos;
return 0;
}
static int lump_close(void*cookie) {
Lump*obj;
LumpCookie*co=cookie;
Lump key;
if(co->id<nlumps && !strcmp(lumps[co->id].name,co->name)) {
obj=lumps+co->id;
} else {
memcpy(key.name,co->name,16);
obj=bsearch(&key,lumps,nlumps,sizeof(Lump),compare_lump_name);
if(!obj) errx(1,"Unexpected error in lump_close");
}
--obj->refcount;
if(co->writing) {
obj->flag&=~2;
obj->data=co->data;
obj->length=co->length;
}
if(!obj->refcount && !obj->flag) {
free(obj->data);
obj->data=0;
}
free(co);
return 0;
}
FILE*open_lump(const char*name,const char*mode) {
static const cookie_io_functions_t cf={
.read=lump_read,
.write=lump_write,
.seek=lump_seek,
.close=lump_close,
};
LumpCookie*co;
FILE*fp;
Lump key;
Lump*obj;
lump_size=0;
convert_lump_name(name,key.name);
if(strchr(mode,'x')) {
if(obj=bsearch(&key,lumps,nlumps,sizeof(Lump),compare_lump_name)) {
if(!obj->length) goto found;
return 0;
}
goto notfound;
}
try_again:
obj=bsearch(&key,lumps,nlumps,sizeof(Lump),compare_lump_name);
if(!obj) {
notfound:
if(*mode=='r') {
if(!mode[1] && config.extra_lump_name && !strcmp(config.extra_lump_name,key.name)) return fopen(config.extra_lump_file,"r");
return 0;
}
lumps=realloc(lumps,(nlumps+1)*sizeof(Lump));
if(!lumps) err(1,"Allocation failed");
lumps[nlumps]=key;
lumps[nlumps].offset=lumps[nlumps].length=lumps[nlumps].refcount=0;
lumps[nlumps].data=0;
lumps[nlumps].flag=1;
++nlumps;
qsort(lumps,nlumps,sizeof(Lump),compare_lump_name);
goto try_again;
}
found:
if(obj->flag&2) return 0;
if(obj->refcount && (*mode!='r' || strchr(mode,'+'))) return 0;
++obj->refcount;
co=malloc(sizeof(LumpCookie));
if(!co) err(1,"Allocation failed");
memcpy(co->name,obj->name,16);
if(*mode=='w') {
obj->length=0;
free(obj->data);
obj->data=0;
}
lump_size=co->length=obj->length;
co->data=obj->data;
co->pos=co->wlength=0;
co->id=obj-lumps;
co->writing=0;
if(obj->length && !obj->data) {
fseek(worldfile,obj->offset,SEEK_SET);
co->data=obj->data=malloc(obj->length);
if(!co->data) err(1,"Allocation failed");
fread(co->data,1,obj->length,worldfile);
}
if(*mode!='r' || strchr(mode,'+')) co->writing=obj->flag|=3;
fp=fopencookie(co,mode,cf);
if(!fp) errx(1,"Unable to open lump cookie stream");
return fp;
}
FILE*open_lump_by_number(Uint16 id,const char*ext,const char*mode) {
static char name[16];
snprintf(name,15,"%04X.%s",id,ext);
return open_lump(name,mode);
}
void revert_lump(const char*name) {
Lump key;
Lump*obj;
convert_lump_name(name,key.name);
if((obj=bsearch(&key,lumps,nlumps,sizeof(Lump),compare_lump_name)) && obj->flag&1) {
if((obj->flag&2) || obj->refcount) errx(1,"Trying to revert a lump which is in use");
free(obj->data);
obj->data=0;
obj->flag=0;
if(obj->offset && worldfile) {
fseek(worldfile,obj->offset-4,SEEK_SET);
obj->length=fgetc(worldfile)<<16;
obj->length|=fgetc(worldfile)<<24;
obj->length|=fgetc(worldfile)<<0;
obj->length|=fgetc(worldfile)<<8;
} else {
obj->length=0;
}
}
}
void revert_lump_by_number(Uint16 id,const char*ext) {
static char name[16];
snprintf(name,15,"%04X.%s",id,ext);
revert_lump(name);
}
int open_world(const char*name) {
FILE*fp=fopen(name,"r");
Uint32 n;
int c,i;
if(!fp) {
warn("Cannot open file '%s'",name);
return -1;
}
if(flock(fileno(fp),LOCK_SH|LOCK_NB)) {
warn("Cannot lock file '%s'",name);
fclose(fp);
return -1;
}
if(name!=world_name) {
free(world_name);
world_name=strdup(name);
if(!world_name) err(1,"Allocation failed");
}
if(worldfile) fclose(worldfile);
worldfile=fp;
for(n=0;n<nlumps;n++) free(lumps[n].data);
free(lumps);
nlumps=0;
for(;;) {
while((c=fgetc(fp))>0);
if(c<0) break;
++nlumps;
n=fgetc(fp)<<16;
n|=fgetc(fp)<<24;
n|=fgetc(fp);
n|=fgetc(fp)<<8;
fseek(fp,n,SEEK_CUR);
}
rewind(fp);
if(!nlumps) return 0;
lumps=calloc(nlumps,sizeof(Lump));
if(!lumps) err(1,"Allocation failed");
for(n=0;n<nlumps;n++) {
i=0;
while((c=fgetc(fp))>0) {
if(i<16) lumps[n].name[i++]=c+(c>='a' && c<='z'?'A'-'a':0); else lumps[n].name[0]=0;
}
lumps[n].length=fgetc(fp)<<16;
lumps[n].length|=fgetc(fp)<<24;
lumps[n].length|=fgetc(fp);
lumps[n].length|=fgetc(fp)<<8;
lumps[n].offset=ftell(fp);
fseek(fp,lumps[n].length,SEEK_CUR);
if(!lumps[n].name[0]) --n,--nlumps;
}
qsort(lumps,nlumps,sizeof(Lump),compare_lump_name);
return 0;
}
void close_world(void) {
Uint32 n;
free(world_name);
world_name=0;
if(worldfile) fclose(worldfile);
worldfile=0;
for(n=0;n<nlumps;n++) free(lumps[n].data);
free(lumps);
nlumps=0;
}
int save_world(const char*name) {
Uint8 buf[1024];
FILE*fp=name?fopen(name,"wx"):fopen(config.temporary_file_1,"w");
Uint32 n,o;
if(!name && !worldfile) {
if(!world_name) errx(1,"Internal confusion in save_world");
worldfile=fmemopen("",1,"rb");
}
if(!fp) {
warn("Cannot save world");
return -1;
}
if(!name && flock(fileno(worldfile),LOCK_EX|LOCK_NB)) {
warn("Cannot lock world");
fclose(fp);
return -1;
}
if(flock(fileno(fp),LOCK_EX)) {
warn("Cannot lock new world file");
fclose(fp);
if(!name) flock(fileno(worldfile),LOCK_SH);
return -1;
}
for(n=0;n<nlumps;n++) {
if(lumps[n].flag&2) errx(1,"Trying to save world while lump '%s' is open for writing",lumps[n].name);
if(!name) lumps[n].flag=0;
o=lumps[n].length;
if(!o) continue; // omit empty lumps
fwrite(lumps[n].name,1,strlen(lumps[n].name)+1,fp);
fputc(o>>16,fp); fputc(o>>24,fp); fputc(o>>0,fp); fputc(o>>8,fp);
o=lumps[n].offset;
if(!name) lumps[n].offset=ftell(fp);
if(lumps[n].data) {
fwrite(lumps[n].data,1,lumps[n].length,fp);
} else {
fseek(worldfile,o,SEEK_SET);
o=lumps[n].length;
while(o>1024) {
fread(buf,1,1024,worldfile);
fwrite(buf,1,1024,fp);
o-=1024;
}
if(o) {
fread(buf,1,o,worldfile);
fwrite(buf,1,o,fp);
}
}
}
n=ferror(fp);
fclose(fp);
if(n) {
if(!name) err(1,"Error writing to new world file");
warn("Error writing to new world file");
return -1;
}
if(!name) {
if(rename(config.temporary_file_1,world_name)) err(1,"Cannot replace world file (the new file is now called %s)",config.temporary_file_1);
flock(fileno(worldfile),LOCK_UN);
fclose(worldfile);
worldfile=fopen(world_name,"r");
if(!worldfile) err(1,"Cannot reopen world file '%s'",name);
if(flock(fileno(worldfile),LOCK_SH|LOCK_NB)) {
warn("Cannot lock file '%s'",world_name);
fclose(worldfile);
worldfile=0;
return -1;
}
}
return 0;
}
int save_game(FILE*fp) {
FILE*f;
Uint32 n,o;
for(n=0;n<nlumps;n++) if(lumps[n].flag&1) {
if(lumps[n].flag&2) errx(1,"Trying to save game while lump '%s' is open for writing",lumps[n].name);
fwrite(lumps[n].name,1,strlen(lumps[n].name)+1,fp);
o=lumps[n].data?lumps[n].length:0;
fputc(o>>16,fp); fputc(o>>24,fp); fputc(o>>0,fp); fputc(o>>8,fp);
if(o) fwrite(lumps[n].data,1,o,fp);
}
return 0;
}
int restore_game(FILE*fp) {
Uint8 buf[1024];
Uint32 n,o;
FILE*f;
int c;
for(n=0;n<nlumps;n++) {
if((lumps[n].flag&2) || lumps[n].refcount) errx(1,"Trying to restore a saved game while lump '%s' is in use",lumps[n].name);
if(lumps[n].flag&1) {
lumps[n].flag=0;
free(lumps[n].data);
lumps[n].data=0;
if(lumps[n].offset && worldfile) {
fseek(worldfile,lumps[n].offset-4,SEEK_SET);
lumps[n].length=fgetc(worldfile)<<16;
lumps[n].length|=fgetc(worldfile)<<24;
lumps[n].length|=fgetc(worldfile)<<0;
lumps[n].length|=fgetc(worldfile)<<8;
} else {
lumps[n].length=0;
}
}
}
for(;;) {
o=0;
while((c=fgetc(fp))>0) if(o<16) buf[o++]=c;
if(c<0) break;
buf[o]=0;
o=fgetc(fp)<<16;
o|=fgetc(fp)<<24;
o|=fgetc(fp);
o|=fgetc(fp)<<8;
f=open_lump(buf,"w");
if(!f) errx(1,"Error opening lump for writing");
while(o>1024) {
fread(buf,1,1024,fp);
fwrite(buf,1,1024,f);
o-=1024;
}
if(o) {
fread(buf,1,o,fp);
fwrite(buf,1,o,f);
}
fclose(f);
}
return 0;
}
size_t copy_stream(FILE*in,FILE*out,size_t len) {
char buf[0x2000];
size_t t=0;
size_t n;
size_t s;
while(len) {
if(len>0x2000) n=0x2000; else n=len;
if(s=fread(buf,1,n,in)) fwrite(buf,1,s,out);
if(s<n) return s+t;
len-=n;
t+=s;
}
return t;
}
void list_lumps(const char*pat,const char***list,int*count) {
int n;
size_t s=0;
char*c=0;
const char*x;
FILE*f=open_memstream(&c,&s);
*list=0;
*count=0;
if(!f) return;
for(n=0;n<nlumps;n++) if(lumps[n].length && !fnmatch(pat,x=lumps[n].name,FNM_NOESCAPE|FNM_CASEFOLD)) {
fwrite(&x,1,sizeof(const char*),f);
++*count;
}
fclose(f);
*list=(void*)c;
}