file.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Copyright (c) 2010, Florian Reuter
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Florian Reuter nor the names of its contributors
  14. may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27. OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <opc/file.h>
  30. #include <stdio.h>
  31. #include <libxml/xmlmemory.h>
  32. #include <libxml/globals.h>
  33. #include <plib/plib.h>
  34. static void *opcFileOpen(const xmlChar *filename, int flags) {
  35. char mode[5];
  36. int mode_ofs=0;
  37. mode[mode_ofs++]='r';
  38. mode[mode_ofs++]='b';
  39. mode[mode_ofs++]='\0';
  40. FILE *file=fopen((const char*)filename, mode); // try to open in READ mode...
  41. if (flags & OPC_FILE_WRITE) {
  42. if (NULL!=file && flags & OPC_FILE_TRUNC) {
  43. fclose(file); file=NULL; // force creating of new file..
  44. }
  45. if (NULL==file) {
  46. // try to create one
  47. mode_ofs=0;
  48. mode[mode_ofs++]='w';
  49. mode[mode_ofs++]='+';
  50. mode[mode_ofs++]='b';
  51. mode[mode_ofs++]='\0';
  52. file=fopen((const char *)filename, mode); // try to open new file
  53. } else {
  54. fclose(file); // close the read handle...
  55. mode_ofs=0;
  56. mode[mode_ofs++]='r';
  57. mode[mode_ofs++]='+';
  58. mode[mode_ofs++]='b';
  59. mode[mode_ofs++]='\0';
  60. file=fopen((const char *)filename, mode); // try to open existing for read/write
  61. }
  62. }
  63. return file;
  64. }
  65. static int opcFileClose(void *iocontext) {
  66. return fclose((FILE*)iocontext);
  67. }
  68. static int opcFileRead(void *iocontext, char *buffer, int len) {
  69. return fread(buffer, sizeof(char), len, (FILE*)iocontext);
  70. }
  71. static int opcFileWrite(void *iocontext, const char *buffer, int len) {
  72. return fwrite(buffer, sizeof(char), len, (FILE*)iocontext);
  73. }
  74. static opc_ofs_t opcFileSeek(void *iocontext, opc_ofs_t ofs) {
  75. int ret=fseek((FILE*)iocontext, ofs, SEEK_SET);
  76. if (ret>=0) {
  77. return ftell((FILE*)iocontext);
  78. } else {
  79. return ret;
  80. }
  81. }
  82. static int opcFileTrim(void *iocontext, opc_ofs_t new_size) {
  83. #ifdef WIN32
  84. return _chsize(fileno((FILE*)iocontext), new_size);
  85. #else
  86. return ftruncate(fileno((FILE*)iocontext), new_size);
  87. #endif
  88. }
  89. static int opcFileFlush(void *iocontext) {
  90. return fflush((FILE*)iocontext);
  91. }
  92. static opc_uint32_t opcFileLength(void *iocontext) {
  93. opc_ofs_t current=ftell((FILE*)iocontext);
  94. OPC_ENSURE(fseek((FILE*)iocontext, 0, SEEK_END)>=0);
  95. opc_ofs_t length=ftell((FILE*)iocontext);
  96. OPC_ENSURE(fseek((FILE*)iocontext, current, SEEK_SET)>=0);
  97. OPC_ASSERT(current==ftell((FILE*)iocontext));
  98. return length;
  99. }
  100. struct __opcZipMemContext {
  101. const opc_uint8_t *data;
  102. opc_uint32_t data_len;
  103. opc_uint32_t data_pos;
  104. };
  105. static void *opcMemOpen(const opc_uint8_t *data, opc_uint32_t data_len) {
  106. struct __opcZipMemContext *mem=(struct __opcZipMemContext *)xmlMalloc(sizeof(struct __opcZipMemContext));
  107. memset(mem, 0, sizeof(*mem));
  108. mem->data_len=data_len;
  109. mem->data_pos=0;
  110. mem->data=data;
  111. return mem;
  112. }
  113. static int opcMemClose(void *iocontext) {
  114. struct __opcZipMemContext *mem=(struct __opcZipMemContext*)iocontext;
  115. xmlFree(mem);
  116. return 0;
  117. }
  118. static int opcMemRead(void *iocontext, char *buffer, int len) {
  119. struct __opcZipMemContext *mem=(struct __opcZipMemContext*)iocontext;
  120. opc_uint32_t max=(mem->data_pos+len<=mem->data_len?len:mem->data_len-mem->data_pos);
  121. OPC_ASSERT(max>=0 && mem->data_pos+max<=mem->data_len);
  122. memcpy(buffer, mem->data+mem->data_pos, max);
  123. mem->data_pos+=max;
  124. return max;
  125. }
  126. static int opcMemWrite(void *iocontext, const char *buffer, int len) {
  127. OPC_ASSERT(0); // not valid for mem
  128. return -1;
  129. }
  130. static opc_ofs_t opcMemSeek(void *iocontext, opc_ofs_t ofs) {
  131. struct __opcZipMemContext *mem=(struct __opcZipMemContext*)iocontext;
  132. if (ofs<=mem->data_len) {
  133. mem->data_pos=ofs;
  134. } else {
  135. mem->data_pos=mem->data_len;
  136. }
  137. return mem->data_pos;
  138. }
  139. static int opcMemTrim(void *iocontext, opc_ofs_t new_size) {
  140. OPC_ASSERT(0); // not valid for mem
  141. return -1;
  142. }
  143. static int opcMemFlush(void *iocontext) {
  144. return 0;
  145. }
  146. opc_error_t opcFileInitIO(opcIO_t *io,
  147. opcFileReadCallback *ioread,
  148. opcFileWriteCallback *iowrite,
  149. opcFileCloseCallback *ioclose,
  150. opcFileSeekCallback *ioseek,
  151. opcFileTrimCallback *iotrim,
  152. opcFileFlushCallback *ioflush,
  153. void *iocontext,
  154. pofs_t file_size,
  155. int flags) {
  156. opc_bzero_mem(io, sizeof(*io));
  157. io->_ioread=ioread;
  158. io->_iowrite=iowrite;
  159. io->_ioclose=ioclose;
  160. io->_ioseek=ioseek;
  161. io->_iotrim=iotrim;
  162. io->_ioflush=ioflush;
  163. io->iocontext=iocontext;
  164. io->file_size=file_size;
  165. io->flags=flags;
  166. return OPC_ERROR_NONE;
  167. }
  168. opc_error_t opcFileInitIOFile(opcIO_t *io, const xmlChar *filename, int flags) {
  169. opc_error_t ret=OPC_ERROR_NONE;
  170. void *iocontext=opcFileOpen(filename, flags);
  171. if (iocontext!=NULL) {
  172. ret=opcFileInitIO(io,
  173. opcFileRead,
  174. opcFileWrite,
  175. opcFileClose,
  176. opcFileSeek,
  177. opcFileTrim,
  178. opcFileFlush,
  179. iocontext,
  180. opcFileLength(iocontext),
  181. flags);
  182. } else {
  183. ret=OPC_ERROR_STREAM;
  184. }
  185. if (OPC_ERROR_NONE!=ret && OPC_ERROR_NONE==io->state.err) io->state.err=ret; // propagate error to stream
  186. return ret;
  187. }
  188. opc_error_t opcFileInitIOMemory(opcIO_t *io, const opc_uint8_t *data, opc_uint32_t data_len, int flags) {
  189. opc_error_t ret=OPC_ERROR_NONE;
  190. void *iocontext=opcMemOpen(data, data_len);
  191. if (iocontext!=NULL) {
  192. ret=opcFileInitIO(io,
  193. opcMemRead,
  194. opcMemWrite,
  195. opcMemClose,
  196. opcMemSeek,
  197. opcMemTrim,
  198. opcMemFlush,
  199. iocontext,
  200. data_len,
  201. flags);
  202. } else {
  203. ret=OPC_ERROR_STREAM;
  204. }
  205. if (OPC_ERROR_NONE!=ret && OPC_ERROR_NONE==io->state.err) io->state.err=ret; // propagate error to stream
  206. return ret;
  207. }
  208. opc_error_t opcFileCleanupIO(opcIO_t *io) {
  209. if (NULL!=io->iocontext) {
  210. io->_ioclose(io->iocontext);
  211. io->iocontext=NULL;
  212. }
  213. return OPC_ERROR_NONE;
  214. }