wadread.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Emacs style mode select -*- C++ -*-
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id: wadread.c,v 1.3 1997/01/30 19:54:23 b1 Exp $
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. //
  18. // $Log: wadread.c,v $
  19. // Revision 1.3 1997/01/30 19:54:23 b1
  20. // Final reformatting run. All the remains (ST, W, WI, Z).
  21. //
  22. // Revision 1.2 1997/01/21 19:00:10 b1
  23. // First formatting run:
  24. // using Emacs cc-mode.el indentation for C++ now.
  25. //
  26. // Revision 1.1 1997/01/19 17:22:51 b1
  27. // Initial check in DOOM sources as of Jan. 10th, 1997
  28. //
  29. //
  30. // DESCRIPTION:
  31. // WAD and Lump I/O, the second.
  32. // This time for soundserver only.
  33. // Welcome to Department of Redundancy Department. Again :-).
  34. //
  35. //-----------------------------------------------------------------------------
  36. static const char rcsid[] = "$Id: wadread.c,v 1.3 1997/01/30 19:54:23 b1 Exp $";
  37. #include <malloc.h>
  38. #include <fcntl.h>
  39. #include <sys/stat.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <ctype.h>
  44. #include <unistd.h>
  45. #include "soundsrv.h"
  46. #include "wadread.h"
  47. int* sfxlengths;
  48. typedef struct wadinfo_struct
  49. {
  50. char identification[4];
  51. int numlumps;
  52. int infotableofs;
  53. } wadinfo_t;
  54. typedef struct filelump_struct
  55. {
  56. int filepos;
  57. int size;
  58. char name[8];
  59. } filelump_t;
  60. typedef struct lumpinfo_struct
  61. {
  62. int handle;
  63. int filepos;
  64. int size;
  65. char name[8];
  66. } lumpinfo_t;
  67. lumpinfo_t* lumpinfo;
  68. int numlumps;
  69. void** lumpcache;
  70. #define strcmpi strcasecmp
  71. //
  72. // Something new.
  73. // This version of w_wad.c does handle endianess.
  74. //
  75. #ifndef __BIG_ENDIAN__
  76. #define LONG(x) (x)
  77. #define SHORT(x) (x)
  78. #else
  79. #define LONG(x) ((long)SwapLONG((unsigned long) (x)))
  80. #define SHORT(x) ((short)SwapSHORT((unsigned short) (x)))
  81. unsigned long SwapLONG(unsigned long x)
  82. {
  83. return
  84. (x>>24)
  85. | ((x>>8) & 0xff00)
  86. | ((x<<8) & 0xff0000)
  87. | (x<<24);
  88. }
  89. unsigned short SwapSHORT(unsigned short x)
  90. {
  91. return
  92. (x>>8) | (x<<8);
  93. }
  94. #endif
  95. // Way too many of those...
  96. static void derror(char* msg)
  97. {
  98. fprintf(stderr, "\nwadread error: %s\n", msg);
  99. exit(-1);
  100. }
  101. void strupr (char *s)
  102. {
  103. while (*s)
  104. *s++ = toupper(*s);
  105. }
  106. int filelength (int handle)
  107. {
  108. struct stat fileinfo;
  109. if (fstat (handle,&fileinfo) == -1)
  110. fprintf (stderr, "Error fstating\n");
  111. return fileinfo.st_size;
  112. }
  113. void openwad(char* wadname)
  114. {
  115. int wadfile;
  116. int tableoffset;
  117. int tablelength;
  118. int tablefilelength;
  119. int i;
  120. wadinfo_t header;
  121. filelump_t* filetable;
  122. // open and read the wadfile header
  123. wadfile = open(wadname, O_RDONLY);
  124. if (wadfile < 0)
  125. derror("Could not open wadfile");
  126. read(wadfile, &header, sizeof header);
  127. if (strncmp(header.identification, "IWAD", 4))
  128. derror("wadfile has weirdo header");
  129. numlumps = LONG(header.numlumps);
  130. tableoffset = LONG(header.infotableofs);
  131. tablelength = numlumps * sizeof(lumpinfo_t);
  132. tablefilelength = numlumps * sizeof(filelump_t);
  133. lumpinfo = (lumpinfo_t *) malloc(tablelength);
  134. filetable = (filelump_t *) ((char*)lumpinfo + tablelength - tablefilelength);
  135. // get the lumpinfo table
  136. lseek(wadfile, tableoffset, SEEK_SET);
  137. read(wadfile, filetable, tablefilelength);
  138. // process the table to make the endianness right and shift it down
  139. for (i=0 ; i<numlumps ; i++)
  140. {
  141. strncpy(lumpinfo[i].name, filetable[i].name, 8);
  142. lumpinfo[i].handle = wadfile;
  143. lumpinfo[i].filepos = LONG(filetable[i].filepos);
  144. lumpinfo[i].size = LONG(filetable[i].size);
  145. // fprintf(stderr, "lump [%.8s] exists\n", lumpinfo[i].name);
  146. }
  147. }
  148. void*
  149. loadlump
  150. ( char* lumpname,
  151. int* size )
  152. {
  153. int i;
  154. void* lump;
  155. for (i=0 ; i<numlumps ; i++)
  156. {
  157. if (!strncasecmp(lumpinfo[i].name, lumpname, 8))
  158. break;
  159. }
  160. if (i == numlumps)
  161. {
  162. // fprintf(stderr,
  163. // "Could not find lumpname [%s]\n", lumpname);
  164. lump = 0;
  165. }
  166. else
  167. {
  168. lump = (void *) malloc(lumpinfo[i].size);
  169. lseek(lumpinfo[i].handle, lumpinfo[i].filepos, SEEK_SET);
  170. read(lumpinfo[i].handle, lump, lumpinfo[i].size);
  171. *size = lumpinfo[i].size;
  172. }
  173. return lump;
  174. }
  175. void*
  176. getsfx
  177. ( char* sfxname,
  178. int* len )
  179. {
  180. unsigned char* sfx;
  181. unsigned char* paddedsfx;
  182. int i;
  183. int size;
  184. int paddedsize;
  185. char name[20];
  186. sprintf(name, "ds%s", sfxname);
  187. sfx = (unsigned char *) loadlump(name, &size);
  188. // pad the sound effect out to the mixing buffer size
  189. paddedsize = ((size-8 + (SAMPLECOUNT-1)) / SAMPLECOUNT) * SAMPLECOUNT;
  190. paddedsfx = (unsigned char *) realloc(sfx, paddedsize+8);
  191. for (i=size ; i<paddedsize+8 ; i++)
  192. paddedsfx[i] = 128;
  193. *len = paddedsize;
  194. return (void *) (paddedsfx + 8);
  195. }