summaryrefslogtreecommitdiffstats
path: root/apps/plugins/mikmod/mmio.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/mikmod/mmio.c')
-rw-r--r--apps/plugins/mikmod/mmio.c304
1 files changed, 173 insertions, 131 deletions
diff --git a/apps/plugins/mikmod/mmio.c b/apps/plugins/mikmod/mmio.c
index 0ffecffd2f..023c56baef 100644
--- a/apps/plugins/mikmod/mmio.c
+++ b/apps/plugins/mikmod/mmio.c
@@ -6,12 +6,12 @@
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
@@ -20,8 +20,6 @@
/*==============================================================================
- $Id: mmio.c,v 1.3 2005/03/30 19:10:58 realtech Exp $
-
Portable file I/O routines
==============================================================================*/
@@ -51,10 +49,14 @@
#include <unistd.h>
#endif
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
+
#include <stdio.h>
#include <string.h>
+#include <limits.h>
-#include "mikmod.h"
#include "mikmod_internals.h"
#ifdef SUNOS
@@ -66,8 +68,6 @@ extern int fseek(FILE *, long, int);
extern size_t fwrite(const void *, size_t, size_t, FILE *);
#endif
-#define COPY_BUFSIZE 1024
-
/* some prototypes */
static int _mm_MemReader_Eof(MREADER* reader);
static int _mm_MemReader_Read(MREADER* reader,void* ptr,size_t size);
@@ -75,16 +75,13 @@ 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);
-//static long _mm_iobase=0,temp_iobase=0;
-
-int _mm_fopen(CHAR* fname,CHAR* attrib)
+int _mm_fopen(const CHAR* fname, const CHAR* attrib)
{
int fp;
- (void)attrib;
-
+ (void)attrib;
//if(!(fp=fopen(fname,attrib))) {
- // _mm_errno = MMERR_OPENING_FILE;
- // if(_mm_errorhandler) _mm_errorhandler();
+ // _mm_errno = MMERR_OPENING_FILE;
+ // if(_mm_errorhandler) _mm_errorhandler();
//}
fp = open(fname, O_RDONLY);
if( fp < 0 ) {
@@ -94,7 +91,7 @@ int _mm_fopen(CHAR* fname,CHAR* attrib)
return fp;
}
-int _mm_FileExists(CHAR* fname)
+int _mm_FileExists(const CHAR* fname)
{
int fp;
@@ -103,7 +100,7 @@ int _mm_FileExists(CHAR* fname)
fp = open(fname, O_RDONLY);
if ( fp < 0 ) return 0;
close(fp);
-
+
return 1;
}
@@ -130,12 +127,12 @@ void _mm_iobase_revert(MREADER* reader)
typedef struct MFILEREADER {
MREADER core;
- int file;
+ int file;
} MFILEREADER;
static int _mm_FileReader_Eof(MREADER* reader)
{
- //return feof(((MFILEREADER*)reader)->file);
+ //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;
@@ -143,8 +140,8 @@ static int _mm_FileReader_Eof(MREADER* reader)
static int _mm_FileReader_Read(MREADER* reader,void* ptr,size_t size)
{
- //return !!fread(ptr,size,1,((MFILEREADER*)reader)->file);
return read(((MFILEREADER*)reader)->file, ptr, size);
+ //return !!fread(ptr,size,1,((MFILEREADER*)reader)->file);
}
static int _mm_FileReader_Get(MREADER* reader)
@@ -160,7 +157,7 @@ static int _mm_FileReader_Get(MREADER* reader)
static int _mm_FileReader_Seek(MREADER* reader,long offset,int whence)
{
//return fseek(((MFILEREADER*)reader)->file,
- // (whence==SEEK_SET)?offset+reader->iobase:offset,whence);
+ // (whence==SEEK_SET)?offset+reader->iobase:offset,whence);
return lseek(((MFILEREADER*)reader)->file,
(whence==SEEK_SET)?offset+reader->iobase:offset,whence);
}
@@ -173,7 +170,7 @@ static long _mm_FileReader_Tell(MREADER* reader)
MREADER *_mm_new_file_reader(int fp)
{
- MFILEREADER* reader=(MFILEREADER*)MikMod_malloc(sizeof(MFILEREADER));
+ MFILEREADER* reader=(MFILEREADER*)MikMod_calloc(1,sizeof(MFILEREADER));
if (reader) {
reader->core.Eof =&_mm_FileReader_Eof;
reader->core.Read=&_mm_FileReader_Read;
@@ -187,14 +184,14 @@ MREADER *_mm_new_file_reader(int fp)
void _mm_delete_file_reader (MREADER* reader)
{
- if(reader) MikMod_free(reader);
+ MikMod_free(reader);
}
/*========== File Writer */
typedef struct MFILEWRITER {
MWRITER core;
- int file;
+ int file;
} MFILEWRITER;
static int _mm_FileWriter_Seek(MWRITER* writer,long offset,int whence)
@@ -209,27 +206,26 @@ static long _mm_FileWriter_Tell(MWRITER* writer)
return lseek(((MFILEWRITER*)writer)->file, 0, SEEK_CUR);
}
-static int _mm_FileWriter_Write(MWRITER* writer,void* ptr,size_t size)
+static int _mm_FileWriter_Write(MWRITER* writer, const void* ptr, size_t size)
{
//return (fwrite(ptr,size,1,((MFILEWRITER*)writer)->file)==size);
- //return (write(ptr,size,((MFILEWRITER*)writer)->file)==(int)size);
- (void)writer;
- (void)ptr;
- (void)size;
- return 0;
+ (void)writer;
+ (void)ptr;
+ (void)size;
+ return 0;
}
static int _mm_FileWriter_Put(MWRITER* writer,int value)
{
- (void)writer;
- (void)value;
+ (void)writer;
+ (void)value;
//return fputc(value,((MFILEWRITER*)writer)->file);
- return 1; // TODO
+ return 1; // TODO
}
MWRITER *_mm_new_file_writer(int fp)
{
- MFILEWRITER* writer=(MFILEWRITER*)MikMod_malloc(sizeof(MFILEWRITER));
+ MFILEWRITER* writer=(MFILEWRITER*)MikMod_calloc(1,sizeof(MFILEWRITER));
if (writer) {
writer->core.Seek =&_mm_FileWriter_Seek;
writer->core.Tell =&_mm_FileWriter_Tell;
@@ -242,12 +238,11 @@ MWRITER *_mm_new_file_writer(int fp)
void _mm_delete_file_writer (MWRITER* writer)
{
- if(writer) MikMod_free (writer);
+ MikMod_free (writer);
}
/*========== Memory Reader */
-
typedef struct MMEMREADER {
MREADER core;
const void *buffer;
@@ -257,12 +252,12 @@ typedef struct MMEMREADER {
void _mm_delete_mem_reader(MREADER* reader)
{
- if (reader) { MikMod_free(reader); }
+ MikMod_free(reader);
}
-MREADER *_mm_new_mem_reader(const void *buffer, int len)
+MREADER *_mm_new_mem_reader(const void *buffer, long len)
{
- MMEMREADER* reader=(MMEMREADER*)MikMod_malloc(sizeof(MMEMREADER));
+ MMEMREADER* reader=(MMEMREADER*)MikMod_calloc(1,sizeof(MMEMREADER));
if (reader)
{
reader->core.Eof =&_mm_MemReader_Eof;
@@ -279,74 +274,86 @@ MREADER *_mm_new_mem_reader(const void *buffer, int len)
static int _mm_MemReader_Eof(MREADER* reader)
{
- if (!reader) { return 1; }
- if ( ((MMEMREADER*)reader)->pos > ((MMEMREADER*)reader)->len ) {
- return 1;
- }
+ 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=ptr;
+ unsigned char *d;
const unsigned char *s;
-
- if (!reader) { return 0; }
-
- if (reader->Eof(reader)) { return 0; }
-
- s = ((MMEMREADER*)reader)->buffer;
- s += ((MMEMREADER*)reader)->pos;
-
- if ( ((MMEMREADER*)reader)->pos + (long)size > ((MMEMREADER*)reader)->len)
- {
- ((MMEMREADER*)reader)->pos = ((MMEMREADER*)reader)->len;
- return 0; /* not enough remaining bytes */
+ 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;
}
- ((MMEMREADER*)reader)->pos += (long)size;
+ s = (const unsigned char *) mr->buffer;
+ s += mr->pos;
+ mr->pos += siz;
+ d = (unsigned char *) ptr;
- while (size--)
- {
- *d = *s;
- s++;
- d++;
+ while (siz) {
+ *d++ = *s++;
+ siz--;
}
-
- return 1;
+
+ return ret;
}
static int _mm_MemReader_Get(MREADER* reader)
{
- int pos;
+ MMEMREADER* mr;
+ int c;
- if (reader->Eof(reader)) { return 0; }
-
- pos = ((MMEMREADER*)reader)->pos;
- ((MMEMREADER*)reader)->pos++;
+ mr = (MMEMREADER*) reader;
+ if (mr->pos >= mr->len) return EOF;
+ c = ((const unsigned char*) mr->buffer)[mr->pos];
+ mr->pos++;
- return ((unsigned char*)(((MMEMREADER*)reader)->buffer))[pos];
+ return c;
}
static int _mm_MemReader_Seek(MREADER* reader,long offset,int whence)
{
- if (!reader) { return -1; }
-
+ MMEMREADER* mr;
+
+ if (!reader) return -1;
+ mr = (MMEMREADER*) reader;
switch(whence)
{
- case SEEK_CUR:
- ((MMEMREADER*)reader)->pos += offset;
- break;
- case SEEK_SET:
- ((MMEMREADER*)reader)->pos = offset;
- break;
- case SEEK_END:
- ((MMEMREADER*)reader)->pos = ((MMEMREADER*)reader)->len - offset - 1;
- break;
+ 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 ( ((MMEMREADER*)reader)->pos < 0) { ((MMEMREADER*)reader)->pos = 0; }
- if ( ((MMEMREADER*)reader)->pos > ((MMEMREADER*)reader)->len ) {
- ((MMEMREADER*)reader)->pos = ((MMEMREADER*)reader)->len;
+ if (mr->pos < reader->iobase) {
+ mr->pos = mr->core.iobase;
+ return -1;
+ }
+ if (mr->pos > mr->len) {
+ mr->pos = mr->len;
}
return 0;
}
@@ -354,13 +361,14 @@ static int _mm_MemReader_Seek(MREADER* reader,long offset,int whence)
static long _mm_MemReader_Tell(MREADER* reader)
{
if (reader) {
- return ((MMEMREADER*)reader)->pos;
+ return ((MMEMREADER*)reader)->pos - reader->iobase;
}
return 0;
}
/*========== Write functions */
-void _mm_write_string(CHAR* data,MWRITER* writer)
+
+void _mm_write_string(const CHAR* data,MWRITER* writer)
{
if(data)
_mm_write_UBYTES(data,strlen(data),writer);
@@ -410,37 +418,51 @@ void _mm_write_I_SLONG(SLONG data,MWRITER* writer)
_mm_write_I_ULONG((ULONG)data,writer);
}
-#if defined __STDC__ || defined _MSC_VER || defined MPW_C
-#define DEFINE_MULTIPLE_WRITE_FUNCTION(type_name,type) \
-void _mm_write_##type_name##S (type *buffer,int number,MWRITER* writer) \
-{ \
- while(number-->0) \
- _mm_write_##type_name(*(buffer++),writer); \
+void _mm_write_M_SWORDS(SWORD *buffer,int cnt,MWRITER* writer)
+{
+ while(cnt-- > 0) _mm_write_M_SWORD(*(buffer++),writer);
}
-#else
-#define DEFINE_MULTIPLE_WRITE_FUNCTION(type_name,type) \
-void _mm_write_/**/type_name/**/S (type *buffer,int number,MWRITER* writer) \
-{ \
- while(number-->0) \
- _mm_write_/**/type_name(*(buffer++),writer); \
+
+void _mm_write_M_UWORDS(UWORD *buffer,int cnt,MWRITER* writer)
+{
+ while(cnt-- > 0) _mm_write_M_UWORD(*(buffer++),writer);
}
-#endif
-DEFINE_MULTIPLE_WRITE_FUNCTION(M_SWORD,SWORD)
-DEFINE_MULTIPLE_WRITE_FUNCTION(M_UWORD,UWORD)
-DEFINE_MULTIPLE_WRITE_FUNCTION(I_SWORD,SWORD)
-DEFINE_MULTIPLE_WRITE_FUNCTION(I_UWORD,UWORD)
+void _mm_write_I_SWORDS(SWORD *buffer,int cnt,MWRITER* writer)
+{
+ while(cnt-- > 0) _mm_write_I_SWORD(*(buffer++),writer);
+}
-DEFINE_MULTIPLE_WRITE_FUNCTION(M_SLONG,SLONG)
-DEFINE_MULTIPLE_WRITE_FUNCTION(M_ULONG,ULONG)
-DEFINE_MULTIPLE_WRITE_FUNCTION(I_SLONG,SLONG)
-DEFINE_MULTIPLE_WRITE_FUNCTION(I_ULONG,ULONG)
+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 number,MREADER* reader)
+int _mm_read_string(CHAR* buffer,int cnt,MREADER* reader)
{
- return reader->Read(reader,buffer,number);
+ return reader->Read(reader,buffer,cnt);
}
UWORD _mm_read_M_UWORD(MREADER* reader)
@@ -491,32 +513,52 @@ SLONG _mm_read_I_SLONG(MREADER* reader)
return((SLONG)_mm_read_I_ULONG(reader));
}
-#if defined __STDC__ || defined _MSC_VER || defined MPW_C
-#define DEFINE_MULTIPLE_READ_FUNCTION(type_name,type) \
-int _mm_read_##type_name##S (type *buffer,int number,MREADER* reader) \
-{ \
- while(number-->0) \
- *(buffer++)=_mm_read_##type_name(reader); \
- return !reader->Eof(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);
}
-#else
-#define DEFINE_MULTIPLE_READ_FUNCTION(type_name,type) \
-int _mm_read_/**/type_name/**/S (type *buffer,int number,MREADER* reader) \
-{ \
- while(number-->0) \
- *(buffer++)=_mm_read_/**/type_name(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);
}
-#endif
-DEFINE_MULTIPLE_READ_FUNCTION(M_SWORD,SWORD)
-DEFINE_MULTIPLE_READ_FUNCTION(M_UWORD,UWORD)
-DEFINE_MULTIPLE_READ_FUNCTION(I_SWORD,SWORD)
-DEFINE_MULTIPLE_READ_FUNCTION(I_UWORD,UWORD)
+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);
+}
-DEFINE_MULTIPLE_READ_FUNCTION(M_SLONG,SLONG)
-DEFINE_MULTIPLE_READ_FUNCTION(M_ULONG,ULONG)
-DEFINE_MULTIPLE_READ_FUNCTION(I_SLONG,SLONG)
-DEFINE_MULTIPLE_READ_FUNCTION(I_ULONG,ULONG)
+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: */