summaryrefslogtreecommitdiffstats
path: root/apps/plugins/mikmod/mmio.c
blob: 023c56baef3a1e85bd13326bd10b63b39fa55ad6 (plain)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*	MikMod sound library
	(c) 1998, 1999, 2000 Miodrag Vallat and others - see file AUTHORS for
	complete list.

	This library is free software; you can redistribute it and/or modify
	it under the terms of the GNU Library 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 Library General Public License for more details.

	You should have received a copy of the GNU Library 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.
*/

/*==============================================================================

  Portable file I/O routines

==============================================================================*/

/*

	The way this module works:

	- _mm_fopen will call the errorhandler [see mmerror.c] in addition to
	  setting _mm_errno on exit.
	- _mm_iobase is for internal use.  It is used by Player_LoadFP to
	  ensure that it works properly with wad files.
	- _mm_read_I_* and _mm_read_M_* differ : the first is for reading data
	  written by a little endian (intel) machine, and the second is for reading
	  big endian (Mac, RISC, Alpha) machine data.
	- _mm_write functions work the same as the _mm_read functions.
	- _mm_read_string is for reading binary strings.  It is basically the same
	  as an fread of bytes.

*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif

#include <stdio.h>
#include <string.h>
#include <limits.h>

#include "mikmod_internals.h"

#ifdef SUNOS
extern int fclose(FILE *);
extern int fgetc(FILE *);
extern int fputc(int, FILE *);
extern size_t fread(void *, size_t, size_t, FILE *);
extern int fseek(FILE *, long, int);
extern size_t fwrite(const void *, size_t, size_t, FILE *);
#endif

/* some prototypes */
static int _mm_MemReader_Eof(MREADER* reader);
static int _mm_MemReader_Read(MREADER* reader,void* ptr,size_t size);
static int _mm_MemReader_Get(MREADER* reader);
static int _mm_MemReader_Seek(MREADER* reader,long offset,int whence);
static long _mm_MemReader_Tell(MREADER* reader);

int _mm_fopen(const CHAR* fname, const CHAR* attrib)
{
	int fp;
	(void)attrib;
	//if(!(fp=fopen(fname,attrib))) {
	//      _mm_errno = MMERR_OPENING_FILE;
	//      if(_mm_errorhandler) _mm_errorhandler();
	//}
	fp = open(fname, O_RDONLY);
	if( fp < 0 ) {
		_mm_errno = MMERR_OPENING_FILE;
		if(_mm_errorhandler) _mm_errorhandler();
	}
	return fp;
}

int _mm_FileExists(const CHAR* fname)
{
	int fp;

	//if(!(fp=fopen(fname,"r"))) return 0;
	//fclose(fp);
	fp = open(fname, O_RDONLY);
	if ( fp < 0 ) return 0;
	close(fp);

	return 1;
}

int _mm_fclose(int fp)
{
	//return fclose(fp);
	return close(fp);
}

/* Sets the current file-position as the new iobase */
void _mm_iobase_setcur(MREADER* reader)
{
	reader->prev_iobase=reader->iobase;  /* store old value in case of revert */
	reader->iobase=reader->Tell(reader);
}

/* Reverts to the last known iobase value. */
void _mm_iobase_revert(MREADER* reader)
{
	reader->iobase=reader->prev_iobase;
}

/*========== File Reader */

typedef struct MFILEREADER {
	MREADER core;
	int   file;
} MFILEREADER;

static int _mm_FileReader_Eof(MREADER* reader)
{
	//return feof(((MFILEREADER*)reader)->file);
	int size   = filesize(((MFILEREADER*)reader)->file);
	int offset = lseek(((MFILEREADER*)reader)->file, 0, SEEK_CUR);
	return (size <= 0 || offset < 0 || offset >= size) ? 1 : 0;
}

static int _mm_FileReader_Read(MREADER* reader,void* ptr,size_t size)
{
	return read(((MFILEREADER*)reader)->file, ptr, size);
	//return !!fread(ptr,size,1,((MFILEREADER*)reader)->file);
}

static int _mm_FileReader_Get(MREADER* reader)
{
	//return fgetc(((MFILEREADER*)reader)->file);
	unsigned char c;
	if ( read(((MFILEREADER*)reader)->file, &c, 1) )
		return c;
	else
		return EOF;
}

static int _mm_FileReader_Seek(MREADER* reader,long offset,int whence)
{
	//return fseek(((MFILEREADER*)reader)->file,
	//                       (whence==SEEK_SET)?offset+reader->iobase:offset,whence);
	return lseek(((MFILEREADER*)reader)->file,
				 (whence==SEEK_SET)?offset+reader->iobase:offset,whence);
}

static long _mm_FileReader_Tell(MREADER* reader)
{
	//return ftell(((MFILEREADER*)reader)->file)-reader->iobase;
	return lseek( (((MFILEREADER*)reader)->file)-reader->iobase, 0, SEEK_CUR );
}

MREADER *_mm_new_file_reader(int fp)
{
	MFILEREADER* reader=(MFILEREADER*)MikMod_calloc(1,sizeof(MFILEREADER));
	if (reader) {
		reader->core.Eof =&_mm_FileReader_Eof;
		reader->core.Read=&_mm_FileReader_Read;
		reader->core.Get =&_mm_FileReader_Get;
		reader->core.Seek=&_mm_FileReader_Seek;
		reader->core.Tell=&_mm_FileReader_Tell;
		reader->file=fp;
	}
	return (MREADER*)reader;
}

void _mm_delete_file_reader (MREADER* reader)
{
	MikMod_free(reader);
}

/*========== File Writer */

typedef struct MFILEWRITER {
	MWRITER core;
	int   file;
} MFILEWRITER;

static int _mm_FileWriter_Seek(MWRITER* writer,long offset,int whence)
{
	//return fseek(((MFILEWRITER*)writer)->file,offset,whence);
	return lseek(((MFILEREADER*)writer)->file,offset,whence);
}

static long _mm_FileWriter_Tell(MWRITER* writer)
{
	//return ftell(((MFILEWRITER*)writer)->file);
	return lseek(((MFILEWRITER*)writer)->file, 0, SEEK_CUR);
}

static int _mm_FileWriter_Write(MWRITER* writer, const void* ptr, size_t size)
{
	//return (fwrite(ptr,size,1,((MFILEWRITER*)writer)->file)==size);
	(void)writer;
	(void)ptr;
	(void)size;
	return 0;
}

static int _mm_FileWriter_Put(MWRITER* writer,int value)
{
	(void)writer;
	(void)value;
	//return fputc(value,((MFILEWRITER*)writer)->file);
	return 1; // TODO
}

MWRITER *_mm_new_file_writer(int fp)
{
	MFILEWRITER* writer=(MFILEWRITER*)MikMod_calloc(1,sizeof(MFILEWRITER));
	if (writer) {
		writer->core.Seek =&_mm_FileWriter_Seek;
		writer->core.Tell =&_mm_FileWriter_Tell;
		writer->core.Write=&_mm_FileWriter_Write;
		writer->core.Put  =&_mm_FileWriter_Put;
		writer->file=fp;
	}
	return (MWRITER*) writer;
}

void _mm_delete_file_writer (MWRITER* writer)
{
	MikMod_free (writer);
}

/*========== Memory Reader */

typedef struct MMEMREADER {
	MREADER core;
	const void *buffer;
	long len;
	long pos;
} MMEMREADER;

void _mm_delete_mem_reader(MREADER* reader)
{
	MikMod_free(reader);
}

MREADER *_mm_new_mem_reader(const void *buffer, long len)
{
	MMEMREADER* reader=(MMEMREADER*)MikMod_calloc(1,sizeof(MMEMREADER));
	if (reader)
	{
		reader->core.Eof =&_mm_MemReader_Eof;
		reader->core.Read=&_mm_MemReader_Read;
		reader->core.Get =&_mm_MemReader_Get;
		reader->core.Seek=&_mm_MemReader_Seek;
		reader->core.Tell=&_mm_MemReader_Tell;
		reader->buffer = buffer;
		reader->len = len;
		reader->pos = 0;
	}
	return (MREADER*)reader;
}

static int _mm_MemReader_Eof(MREADER* reader)
{
	MMEMREADER* mr = (MMEMREADER*) reader;
	if (!mr) return 1;
	if (mr->pos >= mr->len) return 1;
	return 0;
}

static int _mm_MemReader_Read(MREADER* reader,void* ptr,size_t size)
{
	unsigned char *d;
	const unsigned char *s;
	MMEMREADER* mr;
	long siz;
	int ret;

	if (!reader || !size || (size > (size_t) LONG_MAX))
		return 0;

	mr = (MMEMREADER*) reader;
	siz = (long) size;
	if (mr->pos >= mr->len) return 0;	/* @ eof */
	if (mr->pos + siz > mr->len) {
		siz = mr->len - mr->pos;
		ret = 0; /* not enough remaining bytes */
	}
	else {
		ret = 1;
	}

	s = (const unsigned char *) mr->buffer;
	s += mr->pos;
	mr->pos += siz;
	d = (unsigned char *) ptr;

	while (siz) {
		*d++ = *s++;
		siz--;
	}

	return ret;
}

static int _mm_MemReader_Get(MREADER* reader)
{
	MMEMREADER* mr;
	int c;

	mr = (MMEMREADER*) reader;
	if (mr->pos >= mr->len) return EOF;
	c = ((const unsigned char*) mr->buffer)[mr->pos];
	mr->pos++;

	return c;
}

static int _mm_MemReader_Seek(MREADER* reader,long offset,int whence)
{
	MMEMREADER* mr;

	if (!reader) return -1;
	mr = (MMEMREADER*) reader;
	switch(whence)
	{
	case SEEK_CUR:
		mr->pos += offset;
		break;
	case SEEK_SET:
		mr->pos = reader->iobase + offset;
		break;
	case SEEK_END:
		mr->pos = mr->len + offset;
		break;
	default: /* invalid */
		return -1;
	}
	if (mr->pos < reader->iobase) {
		mr->pos = mr->core.iobase;
		return -1;
	}
	if (mr->pos > mr->len) {
		mr->pos = mr->len;
	}
	return 0;
}

static long _mm_MemReader_Tell(MREADER* reader)
{
	if (reader) {
		return ((MMEMREADER*)reader)->pos - reader->iobase;
	}
	return 0;
}

/*========== Write functions */

void _mm_write_string(const CHAR* data,MWRITER* writer)
{
	if(data)
		_mm_write_UBYTES(data,strlen(data),writer);
}

void _mm_write_M_UWORD(UWORD data,MWRITER* writer)
{
	_mm_write_UBYTE(data>>8,writer);
	_mm_write_UBYTE(data&0xff,writer);
}

void _mm_write_I_UWORD(UWORD data,MWRITER* writer)
{
	_mm_write_UBYTE(data&0xff,writer);
	_mm_write_UBYTE(data>>8,writer);
}

void _mm_write_M_ULONG(ULONG data,MWRITER* writer)
{
	_mm_write_M_UWORD(data>>16,writer);
	_mm_write_M_UWORD(data&0xffff,writer);
}

void _mm_write_I_ULONG(ULONG data,MWRITER* writer)
{
	_mm_write_I_UWORD(data&0xffff,writer);
	_mm_write_I_UWORD(data>>16,writer);
}

void _mm_write_M_SWORD(SWORD data,MWRITER* writer)
{
	_mm_write_M_UWORD((UWORD)data,writer);
}

void _mm_write_I_SWORD(SWORD data,MWRITER* writer)
{
	_mm_write_I_UWORD((UWORD)data,writer);
}

void _mm_write_M_SLONG(SLONG data,MWRITER* writer)
{
	_mm_write_M_ULONG((ULONG)data,writer);
}

void _mm_write_I_SLONG(SLONG data,MWRITER* writer)
{
	_mm_write_I_ULONG((ULONG)data,writer);
}

void _mm_write_M_SWORDS(SWORD *buffer,int cnt,MWRITER* writer)
{
	while(cnt-- > 0) _mm_write_M_SWORD(*(buffer++),writer);
}

void _mm_write_M_UWORDS(UWORD *buffer,int cnt,MWRITER* writer)
{
	while(cnt-- > 0) _mm_write_M_UWORD(*(buffer++),writer);
}

void _mm_write_I_SWORDS(SWORD *buffer,int cnt,MWRITER* writer)
{
	while(cnt-- > 0) _mm_write_I_SWORD(*(buffer++),writer);
}

void _mm_write_I_UWORDS(UWORD *buffer,int cnt,MWRITER* writer)
{
	while(cnt-- > 0) _mm_write_I_UWORD(*(buffer++),writer);
}

void _mm_write_M_SLONGS(SLONG *buffer,int cnt,MWRITER* writer)
{
	while(cnt-- > 0) _mm_write_M_SLONG(*(buffer++),writer);
}

void _mm_write_M_ULONGS(ULONG *buffer,int cnt,MWRITER* writer)
{
	while(cnt-- > 0) _mm_write_M_ULONG(*(buffer++),writer);
}

void _mm_write_I_SLONGS(SLONG *buffer,int cnt,MWRITER* writer)
{
	while(cnt-- > 0) _mm_write_I_SLONG(*(buffer++),writer);
}

void _mm_write_I_ULONGS(ULONG *buffer,int cnt,MWRITER* writer)
{
	while(cnt-- > 0) _mm_write_I_ULONG(*(buffer++),writer);
}

/*========== Read functions */

int _mm_read_string(CHAR* buffer,int cnt,MREADER* reader)
{
	return reader->Read(reader,buffer,cnt);
}

UWORD _mm_read_M_UWORD(MREADER* reader)
{
	UWORD result=((UWORD)_mm_read_UBYTE(reader))<<8;
	result|=_mm_read_UBYTE(reader);
	return result;
}

UWORD _mm_read_I_UWORD(MREADER* reader)
{
	UWORD result=_mm_read_UBYTE(reader);
	result|=((UWORD)_mm_read_UBYTE(reader))<<8;
	return result;
}

ULONG _mm_read_M_ULONG(MREADER* reader)
{
	ULONG result=((ULONG)_mm_read_M_UWORD(reader))<<16;
	result|=_mm_read_M_UWORD(reader);
	return result;
}

ULONG _mm_read_I_ULONG(MREADER* reader)
{
	ULONG result=_mm_read_I_UWORD(reader);
	result|=((ULONG)_mm_read_I_UWORD(reader))<<16;
	return result;
}

SWORD _mm_read_M_SWORD(MREADER* reader)
{
	return((SWORD)_mm_read_M_UWORD(reader));
}

SWORD _mm_read_I_SWORD(MREADER* reader)
{
	return((SWORD)_mm_read_I_UWORD(reader));
}

SLONG _mm_read_M_SLONG(MREADER* reader)
{
	return((SLONG)_mm_read_M_ULONG(reader));
}

SLONG _mm_read_I_SLONG(MREADER* reader)
{
	return((SLONG)_mm_read_I_ULONG(reader));
}

int _mm_read_M_SWORDS(SWORD *buffer,int cnt,MREADER* reader)
{
	while(cnt-- > 0) *(buffer++)=_mm_read_M_SWORD(reader);
	return !reader->Eof(reader);
}

int _mm_read_M_UWORDS(UWORD *buffer,int cnt,MREADER* reader)
{
	while(cnt-- > 0) *(buffer++)=_mm_read_M_UWORD(reader);
	return !reader->Eof(reader);
}

int _mm_read_I_SWORDS(SWORD *buffer,int cnt,MREADER* reader)
{
	while(cnt-- > 0) *(buffer++)=_mm_read_I_SWORD(reader);
	return !reader->Eof(reader);
}

int _mm_read_I_UWORDS(UWORD *buffer,int cnt,MREADER* reader)
{
	while(cnt-- > 0) *(buffer++)=_mm_read_I_UWORD(reader);
	return !reader->Eof(reader);
}

int _mm_read_M_SLONGS(SLONG *buffer,int cnt,MREADER* reader)
{
	while(cnt-- > 0) *(buffer++)=_mm_read_M_SLONG(reader);
	return !reader->Eof(reader);
}

int _mm_read_M_ULONGS(ULONG *buffer,int cnt,MREADER* reader)
{
	while(cnt-- > 0) *(buffer++)=_mm_read_M_ULONG(reader);
	return !reader->Eof(reader);
}

int _mm_read_I_SLONGS(SLONG *buffer,int cnt,MREADER* reader)
{
	while(cnt-- > 0) *(buffer++)=_mm_read_I_SLONG(reader);
	return !reader->Eof(reader);
}

int _mm_read_I_ULONGS(ULONG *buffer,int cnt,MREADER* reader)
{
	while(cnt-- > 0) *(buffer++)=_mm_read_I_ULONG(reader);
	return !reader->Eof(reader);
}

/* ex:set ts=4: */