ioapi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* ioapi.c -- IO base function header for compress/uncompress .zip
  2. files using zlib + zip or unzip API
  3. Version 1.01e, February 12th, 2005
  4. Copyright (C) 1998-2005 Gilles Vollant
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "zlib.h"
  10. #include "ioapi.h"
  11. /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
  12. #ifndef SEEK_CUR
  13. #define SEEK_CUR 1
  14. #endif
  15. #ifndef SEEK_END
  16. #define SEEK_END 2
  17. #endif
  18. #ifndef SEEK_SET
  19. #define SEEK_SET 0
  20. #endif
  21. voidpf ZCALLBACK fopen_file_func OF((
  22. voidpf opaque,
  23. const char* filename,
  24. int mode));
  25. uLong ZCALLBACK fread_file_func OF((
  26. voidpf opaque,
  27. voidpf stream,
  28. void* buf,
  29. uLong size));
  30. uLong ZCALLBACK fwrite_file_func OF((
  31. voidpf opaque,
  32. voidpf stream,
  33. const void* buf,
  34. uLong size));
  35. long ZCALLBACK ftell_file_func OF((
  36. voidpf opaque,
  37. voidpf stream));
  38. long ZCALLBACK fseek_file_func OF((
  39. voidpf opaque,
  40. voidpf stream,
  41. uLong offset,
  42. int origin));
  43. int ZCALLBACK fclose_file_func OF((
  44. voidpf opaque,
  45. voidpf stream));
  46. int ZCALLBACK ferror_file_func OF((
  47. voidpf opaque,
  48. voidpf stream));
  49. voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
  50. voidpf opaque;
  51. const char* filename;
  52. int mode;
  53. {
  54. FILE* file = NULL;
  55. const char* mode_fopen = NULL;
  56. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
  57. mode_fopen = "rb";
  58. else
  59. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  60. mode_fopen = "r+b";
  61. else
  62. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  63. mode_fopen = "wb";
  64. if ((filename!=NULL) && (mode_fopen != NULL))
  65. file = fopen(filename, mode_fopen);
  66. return file;
  67. }
  68. uLong ZCALLBACK fread_file_func (opaque, stream, buf, size)
  69. voidpf opaque;
  70. voidpf stream;
  71. void* buf;
  72. uLong size;
  73. {
  74. uLong ret;
  75. ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
  76. return ret;
  77. }
  78. uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size)
  79. voidpf opaque;
  80. voidpf stream;
  81. const void* buf;
  82. uLong size;
  83. {
  84. uLong ret;
  85. ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
  86. return ret;
  87. }
  88. long ZCALLBACK ftell_file_func (opaque, stream)
  89. voidpf opaque;
  90. voidpf stream;
  91. {
  92. long ret;
  93. ret = ftell((FILE *)stream);
  94. return ret;
  95. }
  96. long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
  97. voidpf opaque;
  98. voidpf stream;
  99. uLong offset;
  100. int origin;
  101. {
  102. int fseek_origin=0;
  103. long ret;
  104. switch (origin)
  105. {
  106. case ZLIB_FILEFUNC_SEEK_CUR :
  107. fseek_origin = SEEK_CUR;
  108. break;
  109. case ZLIB_FILEFUNC_SEEK_END :
  110. fseek_origin = SEEK_END;
  111. break;
  112. case ZLIB_FILEFUNC_SEEK_SET :
  113. fseek_origin = SEEK_SET;
  114. break;
  115. default: return -1;
  116. }
  117. ret = 0;
  118. fseek((FILE *)stream, offset, fseek_origin);
  119. return ret;
  120. }
  121. int ZCALLBACK fclose_file_func (opaque, stream)
  122. voidpf opaque;
  123. voidpf stream;
  124. {
  125. int ret;
  126. ret = fclose((FILE *)stream);
  127. return ret;
  128. }
  129. int ZCALLBACK ferror_file_func (opaque, stream)
  130. voidpf opaque;
  131. voidpf stream;
  132. {
  133. int ret;
  134. ret = ferror((FILE *)stream);
  135. return ret;
  136. }
  137. void fill_fopen_filefunc (pzlib_filefunc_def)
  138. zlib_filefunc_def* pzlib_filefunc_def;
  139. {
  140. pzlib_filefunc_def->zopen_file = fopen_file_func;
  141. pzlib_filefunc_def->zread_file = fread_file_func;
  142. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  143. pzlib_filefunc_def->ztell_file = ftell_file_func;
  144. pzlib_filefunc_def->zseek_file = fseek_file_func;
  145. pzlib_filefunc_def->zclose_file = fclose_file_func;
  146. pzlib_filefunc_def->zerror_file = ferror_file_func;
  147. pzlib_filefunc_def->opaque = NULL;
  148. }