mfs_file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. --------------------------------------------------------------------------------
  3. - Module : mem_file.c
  4. - Description : A general purpose library for manipulating a memory area
  5. - as if it were a file.
  6. - mfs_ stands for memory file system.
  7. - Author : Mike Johnson - Banctec AB 03/07/96
  8. -
  9. --------------------------------------------------------------------------------
  10. */
  11. /*
  12. Copyright (c) 1996 Mike Johnson
  13. Copyright (c) 1996 BancTec AB
  14. Permission to use, copy, modify, distribute, and sell this software
  15. for any purpose is hereby granted without fee, provided
  16. that (i) the above copyright notices and this permission notice appear in
  17. all copies of the software and related documentation, and (ii) the names of
  18. Mike Johnson and BancTec may not be used in any advertising or
  19. publicity relating to the software.
  20. THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  21. EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  22. WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  23. IN NO EVENT SHALL MIKE JOHNSON OR BANCTEC BE LIABLE FOR
  24. ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  25. OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  26. WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  27. LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  28. OF THIS SOFTWARE.
  29. */
  30. /*
  31. --------------------------------------------------------------------------------
  32. - Includes
  33. --------------------------------------------------------------------------------
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. /*
  39. --------------------------------------------------------------------------------
  40. - Definitions
  41. --------------------------------------------------------------------------------
  42. */
  43. #define MAX_BUFFS 20
  44. #define FALSE 0
  45. #define TRUE 1
  46. /*
  47. --------------------------------------------------------------------------------
  48. - Globals
  49. --------------------------------------------------------------------------------
  50. */
  51. static char *buf[MAX_BUFFS]; /* Memory for each open buf */
  52. static long buf_off[MAX_BUFFS]; /* File pointer for each buf */
  53. static long buf_size[MAX_BUFFS]; /* Count of bytes allocated for each buf */
  54. static long fds[MAX_BUFFS]; /* File descriptor status */
  55. static int buf_mode[MAX_BUFFS]; /* Mode of buffer (r, w, a) */
  56. static int library_init_done = FALSE;
  57. /*
  58. --------------------------------------------------------------------------------
  59. - Function prototypes
  60. --------------------------------------------------------------------------------
  61. */
  62. int mfs_open (void *ptr, int size, char *mode);
  63. int mfs_lseek (int fd, int offset, int whence);
  64. int mfs_read (int fd, void *buf, int size);
  65. int mfs_write (int fd, void *buf, int size);
  66. int mfs_size (int fd);
  67. int mfs_map (int fd, char **addr, size_t *len);
  68. int mfs_unmap (int fd);
  69. int mfs_close (int fd);
  70. static int extend_mem_file (int fd, int size);
  71. static void mem_init ();
  72. /*
  73. --------------------------------------------------------------------------------
  74. - Function code
  75. --------------------------------------------------------------------------------
  76. */
  77. /*
  78. --------------------------------------------------------------------------------
  79. - Function : mfs_open ()
  80. -
  81. - Arguments : Pointer to allocated buffer, initial size of buffer,
  82. - mode spec (r, w, a)
  83. -
  84. - Returns : File descriptor or -1 if error.
  85. -
  86. - Description : Register this area of memory (which has been allocated
  87. - and has a file read into it) under the mem_file library.
  88. - A file descriptor is returned which can the be passed
  89. - back to TIFFClientOpen and used as if it was a disk
  90. - based fd.
  91. - If the call is for mode 'w' then pass (void *)NULL as
  92. - the buffer and zero size and the library will
  93. - allocate memory for you.
  94. - If the mode is append then pass (void *)NULL and size
  95. - zero or with a valid address.
  96. -
  97. --------------------------------------------------------------------------------
  98. */
  99. int mfs_open (void *buffer, int size, char *mode)
  100. {
  101. int ret, i;
  102. void *tmp;
  103. if (library_init_done == FALSE)
  104. {
  105. mem_init ();
  106. library_init_done = TRUE;
  107. }
  108. ret = -1;
  109. /* Find a free fd */
  110. for (i = 0; i < MAX_BUFFS; i++)
  111. {
  112. if (fds[i] == -1)
  113. {
  114. ret = i;
  115. break;
  116. }
  117. }
  118. if (i == MAX_BUFFS) /* No more free descriptors */
  119. {
  120. ret = -1;
  121. errno = EMFILE;
  122. }
  123. if (ret >= 0 && *mode == 'r')
  124. {
  125. if (buffer == (void *)NULL)
  126. {
  127. ret = -1;
  128. errno = EINVAL;
  129. }
  130. else
  131. {
  132. buf[ret] = (char *)buffer;
  133. buf_size[ret] = size;
  134. buf_off[ret] = 0;
  135. }
  136. }
  137. else if (ret >= 0 && *mode == 'w')
  138. {
  139. if (buffer != (void *)NULL)
  140. {
  141. ret = -1;
  142. errno = EINVAL;
  143. }
  144. else
  145. {
  146. tmp = malloc (0); /* Get a pointer */
  147. if (tmp == (void *)NULL)
  148. {
  149. ret = -1;
  150. errno = EDQUOT;
  151. }
  152. else
  153. {
  154. buf[ret] = (char *)tmp;
  155. buf_size[ret] = 0;
  156. buf_off[ret] = 0;
  157. }
  158. }
  159. }
  160. else if (ret >= 0 && *mode == 'a')
  161. {
  162. if (buffer == (void *) NULL) /* Create space for client */
  163. {
  164. tmp = malloc (0); /* Get a pointer */
  165. if (tmp == (void *)NULL)
  166. {
  167. ret = -1;
  168. errno = EDQUOT;
  169. }
  170. else
  171. {
  172. buf[ret] = (char *)tmp;
  173. buf_size[ret] = 0;
  174. buf_off[ret] = 0;
  175. }
  176. }
  177. else /* Client has file read in already */
  178. {
  179. buf[ret] = (char *)buffer;
  180. buf_size[ret] = size;
  181. buf_off[ret] = 0;
  182. }
  183. }
  184. else /* Some other invalid combination of parameters */
  185. {
  186. ret = -1;
  187. errno = EINVAL;
  188. }
  189. if (ret != -1)
  190. {
  191. fds[ret] = 0;
  192. buf_mode[ret] = *mode;
  193. }
  194. return (ret);
  195. }
  196. /*
  197. --------------------------------------------------------------------------------
  198. - Function : mfs_lseek ()
  199. -
  200. - Arguments : File descriptor, offset, whence
  201. -
  202. - Returns : as per man lseek (2)
  203. -
  204. - Description : Does the same as lseek (2) except on a memory based file.
  205. - Note: the memory area will be extended if the caller
  206. - attempts to seek past the current end of file (memory).
  207. -
  208. --------------------------------------------------------------------------------
  209. */
  210. int mfs_lseek (int fd, int offset, int whence)
  211. {
  212. int ret;
  213. long test_off;
  214. if (fds[fd] == -1) /* Not open */
  215. {
  216. ret = -1;
  217. errno = EBADF;
  218. }
  219. else if (offset < 0 && whence == SEEK_SET)
  220. {
  221. ret = -1;
  222. errno = EINVAL;
  223. }
  224. else
  225. {
  226. switch (whence)
  227. {
  228. case SEEK_SET:
  229. if (offset > buf_size[fd])
  230. extend_mem_file (fd, offset);
  231. buf_off[fd] = offset;
  232. ret = offset;
  233. break;
  234. case SEEK_CUR:
  235. test_off = buf_off[fd] + offset;
  236. if (test_off < 0)
  237. {
  238. ret = -1;
  239. errno = EINVAL;
  240. }
  241. else
  242. {
  243. if (test_off > buf_size[fd])
  244. extend_mem_file (fd, test_off);
  245. buf_off[fd] = test_off;
  246. ret = test_off;
  247. }
  248. break;
  249. case SEEK_END:
  250. test_off = buf_size[fd] + offset;
  251. if (test_off < 0)
  252. {
  253. ret = -1;
  254. errno = EINVAL;
  255. }
  256. else
  257. {
  258. if (test_off > buf_size[fd])
  259. extend_mem_file (fd, test_off);
  260. buf_off[fd] = test_off;
  261. ret = test_off;
  262. }
  263. break;
  264. default:
  265. errno = EINVAL;
  266. ret = -1;
  267. break;
  268. }
  269. }
  270. return (ret);
  271. }
  272. /*
  273. --------------------------------------------------------------------------------
  274. - Function : mfs_read ()
  275. -
  276. - Arguments : File descriptor, buffer, size
  277. -
  278. - Returns : as per man read (2)
  279. -
  280. - Description : Does the same as read (2) except on a memory based file.
  281. - Note: An attempt to read past the end of memory currently
  282. - allocated to the file will return 0 (End Of File)
  283. -
  284. --------------------------------------------------------------------------------
  285. */
  286. int mfs_read (int fd, void *clnt_buf, int size)
  287. {
  288. int ret;
  289. if (fds[fd] == -1 || buf_mode[fd] != 'r')
  290. {
  291. /* File is either not open, or not opened for read */
  292. ret = -1;
  293. errno = EBADF;
  294. }
  295. else if (buf_off[fd] + size > buf_size[fd])
  296. {
  297. ret = 0; /* EOF */
  298. }
  299. else
  300. {
  301. memcpy (clnt_buf, (void *) (buf[fd] + buf_off[fd]), size);
  302. buf_off[fd] = buf_off[fd] + size;
  303. ret = size;
  304. }
  305. return (ret);
  306. }
  307. /*
  308. --------------------------------------------------------------------------------
  309. - Function : mfs_write ()
  310. -
  311. - Arguments : File descriptor, buffer, size
  312. -
  313. - Returns : as per man write (2)
  314. -
  315. - Description : Does the same as write (2) except on a memory based file.
  316. - Note: the memory area will be extended if the caller
  317. - attempts to write past the current end of file (memory).
  318. -
  319. --------------------------------------------------------------------------------
  320. */
  321. int mfs_write (int fd, void *clnt_buf, int size)
  322. {
  323. int ret;
  324. if (fds[fd] == -1 || buf_mode[fd] == 'r')
  325. {
  326. /* Either the file is not open or it is opened for reading only */
  327. ret = -1;
  328. errno = EBADF;
  329. }
  330. else if (buf_mode[fd] == 'w')
  331. {
  332. /* Write */
  333. if (buf_off[fd] + size > buf_size[fd])
  334. {
  335. extend_mem_file (fd, buf_off[fd] + size);
  336. buf_size[fd] = (buf_off[fd] + size);
  337. }
  338. memcpy ((buf[fd] + buf_off[fd]), clnt_buf, size);
  339. buf_off[fd] = buf_off[fd] + size;
  340. ret = size;
  341. }
  342. else
  343. {
  344. /* Append */
  345. if (buf_off[fd] != buf_size[fd])
  346. buf_off[fd] = buf_size[fd];
  347. extend_mem_file (fd, buf_off[fd] + size);
  348. buf_size[fd] += size;
  349. memcpy ((buf[fd] + buf_off[fd]), clnt_buf, size);
  350. buf_off[fd] = buf_off[fd] + size;
  351. ret = size;
  352. }
  353. return (ret);
  354. }
  355. /*
  356. --------------------------------------------------------------------------------
  357. - Function : mfs_size ()
  358. -
  359. - Arguments : File descriptor
  360. -
  361. - Returns : integer file size
  362. -
  363. - Description : This function returns the current size of the file in bytes.
  364. -
  365. --------------------------------------------------------------------------------
  366. */
  367. int mfs_size (int fd)
  368. {
  369. int ret;
  370. if (fds[fd] == -1) /* Not open */
  371. {
  372. ret = -1;
  373. errno = EBADF;
  374. }
  375. else
  376. ret = buf_size[fd];
  377. return (ret);
  378. }
  379. /*
  380. --------------------------------------------------------------------------------
  381. - Function : mfs_map ()
  382. -
  383. - Arguments : File descriptor, ptr to address, ptr to length
  384. -
  385. - Returns : Map status (succeeded or otherwise)
  386. -
  387. - Description : This function tells the client where the file is mapped
  388. - in memory and what size the mapped area is. It is provided
  389. - to satisfy the MapProc function in libtiff. It pretends
  390. - that the file has been mmap (2)ped.
  391. -
  392. --------------------------------------------------------------------------------
  393. */
  394. int mfs_map (int fd, char **addr, size_t *len)
  395. {
  396. int ret;
  397. if (fds[fd] == -1) /* Not open */
  398. {
  399. ret = -1;
  400. errno = EBADF;
  401. }
  402. else
  403. {
  404. *addr = buf[fd];
  405. *len = buf_size[fd];
  406. ret = 0;
  407. }
  408. return (ret);
  409. }
  410. /*
  411. --------------------------------------------------------------------------------
  412. - Function : mfs_unmap ()
  413. -
  414. - Arguments : File descriptor
  415. -
  416. - Returns : UnMap status (succeeded or otherwise)
  417. -
  418. - Description : This function does nothing as the file is always
  419. - in memory.
  420. -
  421. --------------------------------------------------------------------------------
  422. */
  423. int mfs_unmap (int fd)
  424. {
  425. return (0);
  426. }
  427. /*
  428. --------------------------------------------------------------------------------
  429. - Function : mfs_close ()
  430. -
  431. - Arguments : File descriptor
  432. -
  433. - Returns : close status (succeeded or otherwise)
  434. -
  435. - Description : Close the open memory file. (Make fd available again.)
  436. -
  437. --------------------------------------------------------------------------------
  438. */
  439. int mfs_close (int fd)
  440. {
  441. int ret;
  442. if (fds[fd] == -1) /* Not open */
  443. {
  444. ret = -1;
  445. errno = EBADF;
  446. }
  447. else
  448. {
  449. fds[fd] = -1;
  450. ret = 0;
  451. }
  452. return (ret);
  453. }
  454. /*
  455. --------------------------------------------------------------------------------
  456. - Function : extend_mem_file ()
  457. -
  458. - Arguments : File descriptor, length to extend to.
  459. -
  460. - Returns : 0 - All OK, -1 - realloc () failed.
  461. -
  462. - Description : Increase the amount of memory allocated to a file.
  463. -
  464. --------------------------------------------------------------------------------
  465. */
  466. static int extend_mem_file (int fd, int size)
  467. {
  468. void *new_mem;
  469. int ret;
  470. if ((new_mem = realloc (buf[fd], size)) == (void *) NULL)
  471. ret = -1;
  472. else
  473. {
  474. buf[fd] = (char *) new_mem;
  475. ret = 0;
  476. }
  477. return (ret);
  478. }
  479. /*
  480. --------------------------------------------------------------------------------
  481. - Function : mem_init ()
  482. -
  483. - Arguments : None
  484. -
  485. - Returns : void
  486. -
  487. - Description : Initialise the library.
  488. -
  489. --------------------------------------------------------------------------------
  490. */
  491. static void mem_init ()
  492. {
  493. int i;
  494. for (i = 0; i < MAX_BUFFS; i++)
  495. {
  496. fds[i] = -1;
  497. buf[i] = (char *)NULL;
  498. buf_size[i] = 0;
  499. buf_off[i] = 0;
  500. }
  501. }
  502. /*
  503. * Local Variables:
  504. * mode: c
  505. * c-basic-offset: 8
  506. * fill-column: 78
  507. * End:
  508. */