Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

429 lines
10 KiB

  1. /*
  2. * @DEC_COPYRIGHT@
  3. */
  4. /*
  5. * HISTORY
  6. * $Log: sc_file.c,v $
  7. * Revision 1.1.8.6 1996/12/12 20:54:43 Hans_Graves
  8. * Fix some NT warnings (when linking statically).
  9. * [1996/12/12 20:07:58 Hans_Graves]
  10. *
  11. * Revision 1.1.8.5 1996/11/04 22:38:38 Hans_Graves
  12. * Fixed open/closes under NT. File closes weren't always happening.
  13. * [1996/11/04 22:29:53 Hans_Graves]
  14. *
  15. * Revision 1.1.8.4 1996/10/28 17:32:18 Hans_Graves
  16. * Replace longs with dwords for NT portability.
  17. * [1996/10/28 16:54:46 Hans_Graves]
  18. *
  19. * Revision 1.1.8.3 1996/09/18 23:45:38 Hans_Graves
  20. * Added ScFileClose() for portability
  21. * [1996/09/18 21:53:20 Hans_Graves]
  22. *
  23. * Revision 1.1.8.2 1996/05/07 19:55:45 Hans_Graves
  24. * Fix file creation under NT.
  25. * [1996/05/07 17:11:18 Hans_Graves]
  26. *
  27. * Revision 1.1.6.2 1996/04/01 16:23:08 Hans_Graves
  28. * Added ScFileOpen and ScFileRead/Write functions for portability
  29. * [1996/04/01 16:11:56 Hans_Graves]
  30. *
  31. * Revision 1.1.4.3 1996/02/07 23:23:48 Hans_Graves
  32. * Added ScFileSeek().
  33. * [1996/02/07 23:21:55 Hans_Graves]
  34. *
  35. * Revision 1.1.4.2 1996/01/02 18:30:51 Bjorn_Engberg
  36. * Got rid of compiler warnings: Added include files for NT.
  37. * [1996/01/02 15:25:02 Bjorn_Engberg]
  38. *
  39. * Revision 1.1.2.5 1995/09/20 14:59:32 Bjorn_Engberg
  40. * Port to NT
  41. * [1995/09/20 14:41:12 Bjorn_Engberg]
  42. *
  43. * Revision 1.1.2.4 1995/07/12 19:48:22 Hans_Graves
  44. * Added H261 recognition to ScFileType().
  45. * [1995/07/12 19:33:48 Hans_Graves]
  46. *
  47. * Revision 1.1.2.3 1995/06/22 21:36:00 Hans_Graves
  48. * Moved ScGetFileType() from sv_gentoc.c. Added some Audio file types.
  49. * [1995/06/22 21:33:05 Hans_Graves]
  50. *
  51. * Revision 1.1.2.2 1995/05/31 18:07:49 Hans_Graves
  52. * Inclusion in new SLIB location.
  53. * [1995/05/31 16:13:00 Hans_Graves]
  54. *
  55. * Revision 1.1.2.3 1995/04/07 18:55:36 Hans_Graves
  56. * Added FileExists()
  57. * [1995/04/07 18:55:13 Hans_Graves]
  58. *
  59. * Revision 1.1.2.2 1995/04/07 18:34:21 Hans_Graves
  60. * Inclusion in SLIB's Su library
  61. * [1995/04/07 18:33:26 Hans_Graves]
  62. *
  63. * $EndLog$
  64. */
  65. /*****************************************************************************
  66. ** Copyright (c) Digital Equipment Corporation, 1995 **
  67. ** **
  68. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  69. ** laws of the United States. **
  70. ** **
  71. ** The software contained on this media is proprietary to and embodies **
  72. ** the confidential technology of Digital Equipment Corporation. **
  73. ** Possession, use, duplication or dissemination of the software and **
  74. ** media is authorized only pursuant to a valid written license from **
  75. ** Digital Equipment Corporation. **
  76. ** **
  77. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  78. ** Government is subject to restrictions as set forth in Subparagraph **
  79. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  80. ******************************************************************************/
  81. #include <fcntl.h>
  82. #include <sys/types.h>
  83. #ifndef WIN32
  84. #include <sys/mman.h>
  85. #endif /* WIN32 */
  86. #include <sys/stat.h>
  87. #include "SC.h"
  88. #include "SC_err.h"
  89. #ifdef WIN32
  90. #include <string.h>
  91. #include <sys/stat.h>
  92. #include <io.h>
  93. #include <stdio.h>
  94. #include <windows.h>
  95. #endif
  96. /*
  97. ** Name: ScFileExists
  98. ** Purpose: Does this file exist?
  99. **
  100. */
  101. ScBoolean_t ScFileExists(char *filename)
  102. {
  103. #ifdef WIN32
  104. struct _stat stat_buf;
  105. if (_stat(filename, &stat_buf))
  106. #else
  107. struct stat stat_buf;
  108. if (stat(filename, &stat_buf))
  109. #endif
  110. return(FALSE);
  111. else
  112. return(TRUE);
  113. }
  114. /*
  115. ** Name: ScFileOpenForReading
  116. ** Purpose: Open a file for reading.
  117. ** Returns: Handle to file.
  118. ** -1 if error
  119. */
  120. int ScFileOpenForReading(char *filename)
  121. {
  122. if (!filename)
  123. return(-1);
  124. #ifdef WIN32
  125. return((int)_open(filename, _O_RDONLY|_O_BINARY));
  126. #else /* OSF */
  127. return((int)open(filename, O_RDONLY));
  128. #endif
  129. }
  130. /*
  131. ** Name: ScFileOpenForWriting
  132. ** Purpose: Open a file for writing. Creates it if it doesn't already exist.
  133. ** Returns: Handle to file.
  134. ** -1 if error
  135. */
  136. int ScFileOpenForWriting(char *filename, ScBoolean_t truncate)
  137. {
  138. if (!filename)
  139. return(-1);
  140. #ifdef WIN32
  141. if (truncate)
  142. return((int)_open(filename, _O_WRONLY|_O_CREAT|_O_TRUNC|_O_BINARY,
  143. _S_IREAD|_S_IWRITE));
  144. else
  145. return((int)_open(filename, _O_WRONLY|_O_CREAT|_O_BINARY,
  146. _S_IREAD|_S_IWRITE));
  147. #else
  148. if (truncate)
  149. return((int)open(filename, O_WRONLY|O_CREAT|O_TRUNC,
  150. S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH));
  151. else
  152. return((int)open(filename, O_WRONLY|O_CREAT,
  153. S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH));
  154. #endif
  155. }
  156. /*
  157. ** Name: ScFileSize
  158. ** Purpose: Get the size of a file in bytes
  159. */
  160. ScStatus_t ScFileSize(char *filename, unsigned qword *size)
  161. {
  162. #ifdef WIN32
  163. struct _stat stat_buf;
  164. #else
  165. struct stat stat_buf;
  166. #endif
  167. if (!filename || !size)
  168. return(ScErrorBadArgument);
  169. #ifdef WIN32
  170. if (_stat(filename, &stat_buf) < 0)
  171. #else
  172. if (stat(filename, &stat_buf) < 0)
  173. #endif
  174. {
  175. *size=0;
  176. return(ScErrorFile);
  177. }
  178. *size=(unsigned qword)stat_buf.st_size;
  179. return(NoErrors);
  180. }
  181. /*
  182. ** Name: ScFileRead
  183. ** Purpose: Read a number of bytes from a file into a buffer
  184. ** Return: Number of bytes read
  185. ** -1 if EOF
  186. */
  187. dword ScFileRead(int fd, void *buffer, unsigned dword bytes)
  188. {
  189. #ifdef __VMS
  190. return((long)fread(buffer, 1, bytes, fd));
  191. #elif defined(WIN32)
  192. return((long)_read(fd, buffer, bytes));
  193. #else /* UNIX */
  194. return((long)read(fd, buffer, bytes));
  195. #endif
  196. }
  197. /*
  198. ** Name: ScFileWrite
  199. ** Purpose: Write a number of bytes from a buffer to a file
  200. ** Return: Number of bytes written
  201. ** 0 if error
  202. */
  203. dword ScFileWrite(int fd, void *buffer, unsigned dword bytes)
  204. {
  205. #ifdef __VMS
  206. return((dword)fwrite(buffer, 1, bytes, fd));
  207. #elif defined(WIN32)
  208. return((dword)_write(fd, buffer, bytes));
  209. #else /* UNIX */
  210. return((dword)write(fd, buffer, bytes));
  211. #endif
  212. }
  213. /*
  214. ** Name: ScFileSeek
  215. ** Purpose: Seek to a specific position is a file
  216. */
  217. ScStatus_t ScFileSeek(int fd, qword bytepos)
  218. {
  219. #ifdef __VMS
  220. if (fseek(fd,bytepos,SEEK_SET)<0)
  221. #elif defined(WIN32)
  222. if (_lseek(fd,(long)bytepos,SEEK_SET)<0)
  223. #else
  224. if (lseek(fd,(long)bytepos,SEEK_SET)<0)
  225. #endif
  226. return(ScErrorFile);
  227. else
  228. return(NoErrors);
  229. }
  230. /*
  231. ** Name: ScFileClose
  232. ** Purpose: Close an opened file
  233. */
  234. void ScFileClose(int fd)
  235. {
  236. if (fd>=0)
  237. {
  238. #ifdef WIN32
  239. _close(fd);
  240. #else /* UNIX or VMS */
  241. close(fd);
  242. #endif
  243. }
  244. }
  245. /*
  246. ** Name: ScFileMap
  247. ** Purpose: Map an entire file to memory
  248. ** if fd<0 then the filename is opened for reading
  249. ** Returns: buffer = memory pointer to the mapped file
  250. ** size = size of the buffer (file)
  251. */
  252. ScStatus_t ScFileMap(char *filename, int *pfd, u_char **buffer,
  253. unsigned qword *size)
  254. {
  255. #ifdef WIN32
  256. /*
  257. * Mapping of files can be supported on NT,
  258. * but for now return an error and implement
  259. * file mapping later - BE.
  260. */
  261. return(ScErrorMapFile);
  262. #else /* !WIN32 */
  263. if (!pfd || !filename || !buffer || !size)
  264. return(ScErrorBadArgument);
  265. if (ScFileSize(filename, size)!=NoErrors)
  266. return(ScErrorFile);
  267. if (*pfd<0)
  268. {
  269. if ((*pfd = open (filename, O_RDONLY)) < 0)
  270. return(ScErrorFile);
  271. }
  272. *buffer= (unsigned char *)mmap(0, *size, PROT_READ,
  273. MAP_FILE | MAP_VARIABLE | MAP_PRIVATE, *pfd, 0);
  274. if (*buffer==(u_char *)-1L)
  275. {
  276. *buffer=NULL;
  277. *size=0;
  278. return(ScErrorMapFile);
  279. }
  280. #endif /* !WIN32 */
  281. return(NoErrors);
  282. }
  283. /*
  284. ** Name: ScFileUnMap
  285. ** Purpose: UnMap a file mapped to memory
  286. ** if fd>=0 then the file is closed
  287. */
  288. ScStatus_t ScFileUnMap(int fd, u_char *buffer, unsigned int size)
  289. {
  290. if (!buffer || !size)
  291. return(ScErrorBadArgument);
  292. #ifndef WIN32
  293. if (munmap(buffer, size)<0)
  294. #endif /* !WIN32 */
  295. return(ScErrorMapFile);
  296. if (fd>=0)
  297. ScFileClose(fd);
  298. return(NoErrors);
  299. }
  300. /*
  301. ** Name: ScGetFileType
  302. ** Purpose: Find out the type of a multmedia file.
  303. ** Returns: UNKNOWN_FILE, AVI_FILE, JFIF_FILE, QUICKTIME_JPEG_FILE
  304. ** MPEG_VIDEO_FILE, MPEG_AUDIO_FILE, MPEG_SYSTEM_FILE,
  305. ** GSM_FILE
  306. */
  307. int ScGetFileType(char *filename)
  308. {
  309. int fd;
  310. u_char buf[20];
  311. char *fileext;
  312. if ((fd = ScFileOpenForReading(filename)) < 0)
  313. return(ScErrorDevOpen);
  314. ScFileRead(fd, buf, 11);
  315. /*
  316. ** MPEG video file
  317. */
  318. if ((buf[0] == 0) &&
  319. (buf[1] == 0) &&
  320. (buf[2] == 1) &&
  321. (buf[3] == 0xb3)) {
  322. ScFileClose(fd);
  323. return(MPEG_VIDEO_FILE);
  324. }
  325. /*
  326. ** MPEG system file
  327. */
  328. if ((buf[0] == 0x00) &&
  329. (buf[1] == 0x00) &&
  330. (buf[2] == 0x01) &&
  331. (buf[3] == 0xba)) {
  332. ScFileClose(fd);
  333. return(MPEG_SYSTEM_FILE);
  334. }
  335. /*
  336. ** H261 video stream file
  337. */
  338. if ((buf[0] == 0x00) &&
  339. (buf[1] == 0x01) &&
  340. (buf[2] == 0x00) &&
  341. (buf[3] == 0x88)) {
  342. ScFileClose(fd);
  343. return(H261_FILE);
  344. }
  345. /*
  346. ** JFIF file (ffd8 = Start-Of-Image marker)
  347. */
  348. if ((buf[0] == 0xff) &&
  349. (buf[1] == 0xd8)) {
  350. ScFileClose(fd);
  351. return(JFIF_FILE);
  352. }
  353. /*
  354. ** QUICKTIME JPEG file (4 ignored bytes, "mdat", ff, d8, ff)
  355. */
  356. if ((strncmp(&buf[4], "mdat", 4) == 0 ) &&
  357. (buf[8] == 0xff) &&
  358. (buf[9] == 0xd8) &&
  359. (buf[10] == 0xff)) {
  360. ScFileClose(fd);
  361. return(QUICKTIME_JPEG_FILE);
  362. }
  363. /******* use the file's extension to help guess the type ********/
  364. for (fileext=filename; *fileext; fileext++)
  365. if (*fileext=='.' && *(fileext+1)!='.')
  366. {
  367. fileext++;
  368. if (strncmp(fileext, "p64", 3)==0)
  369. {
  370. ScFileClose(fd);
  371. return(H261_FILE);
  372. }
  373. if (strncmp(fileext, "gsm", 3)==0)
  374. {
  375. ScFileClose(fd);
  376. return(GSM_FILE);
  377. }
  378. if (strncmp(fileext, "pcm", 3)==0)
  379. {
  380. ScFileClose(fd);
  381. return(PCM_FILE);
  382. }
  383. if (strncmp(fileext, "wav", 3)==0 && strncmp(buf, "RIFF", 4)==0)
  384. {
  385. ScFileClose(fd);
  386. return(WAVE_FILE);
  387. }
  388. if (strncmp(fileext, "mp", 2)==0 && buf[0]==0xFF)
  389. {
  390. ScFileClose(fd);
  391. return(MPEG_AUDIO_FILE);
  392. }
  393. break;
  394. }
  395. /*
  396. ** AVI RIFF file
  397. */
  398. if ( strncmp(buf, "RIFF", 4) == 0 ) {
  399. ScFileClose(fd);
  400. return(AVI_FILE);
  401. }
  402. ScFileClose(fd);
  403. return(UNKNOWN_FILE);
  404. }