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.
 
 
 
 
 
 

260 lines
5.3 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2006-2007 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains functions for compressed file I/O.
  16. */
  17. #include "config.h"
  18. #if !defined __KERNEL__
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. # if defined HAVE_ZLIB_H
  23. # include <zlib.h>
  24. # define READSIZE 128 /* Read buffer size */
  25. # define WRITESIZE 128 /* Inflate buffer size */
  26. # endif
  27. #endif
  28. #include "cucul.h"
  29. #include "cucul_internals.h"
  30. #if !defined __KERNEL__ && defined HAVE_ZLIB_H
  31. static int zipread(cucul_file_t *, void *, unsigned int);
  32. #endif
  33. #if !defined __KERNEL__
  34. struct cucul_file
  35. {
  36. # if defined HAVE_ZLIB_H
  37. uint8_t read_buffer[READSIZE];
  38. z_stream stream;
  39. gzFile gz;
  40. int eof, zip;
  41. # endif
  42. FILE *f;
  43. int readonly;
  44. };
  45. #endif
  46. cucul_file_t *cucul_file_open(char const *path, const char *mode)
  47. {
  48. #if defined __KERNEL__
  49. return NULL;
  50. #else
  51. cucul_file_t *fp = malloc(sizeof(*fp));
  52. fp->readonly = !strchr(mode, 'r');
  53. # if defined HAVE_ZLIB_H
  54. uint8_t buf[4];
  55. unsigned int skip_size = 0;
  56. fp->gz = gzopen(path, fp->readonly ? "rb" : "wb");
  57. if(!fp->gz)
  58. {
  59. free(fp);
  60. return NULL;
  61. }
  62. fp->eof = 0;
  63. fp->zip = 0;
  64. if(fp->readonly)
  65. {
  66. /* Parse ZIP file and go to start of first file */
  67. gzread(fp->gz, buf, 4);
  68. if(memcmp(buf, "PK\3\4", 4))
  69. {
  70. gzseek(fp->gz, 0, SEEK_SET);
  71. return fp;
  72. }
  73. fp->zip = 1;
  74. gzseek(fp->gz, 22, SEEK_CUR);
  75. gzread(fp->gz, buf, 2); /* Filename size */
  76. skip_size += (uint16_t)buf[0] | ((uint16_t)buf[1] << 8);
  77. gzread(fp->gz, buf, 2); /* Extra field size */
  78. skip_size += (uint16_t)buf[0] | ((uint16_t)buf[1] << 8);
  79. gzseek(fp->gz, skip_size, SEEK_CUR);
  80. /* Initialise inflate stream */
  81. fp->stream.total_out = 0;
  82. fp->stream.zalloc = NULL;
  83. fp->stream.zfree = NULL;
  84. fp->stream.opaque = NULL;
  85. fp->stream.next_in = NULL;
  86. fp->stream.avail_in = 0;
  87. if(inflateInit2(&fp->stream, -MAX_WBITS))
  88. {
  89. free(fp);
  90. gzclose(fp->gz);
  91. return NULL;
  92. }
  93. }
  94. # else
  95. fp->f = fopen(path, fmode);
  96. if(!fp->f)
  97. {
  98. free(fp);
  99. return NULL;
  100. }
  101. # endif
  102. return fp;
  103. #endif
  104. }
  105. int cucul_file_close(cucul_file_t *fp)
  106. {
  107. #if defined __KERNEL__
  108. return 0;
  109. #elif defined HAVE_ZLIB_H
  110. gzFile gz = fp->gz;
  111. if(fp->zip)
  112. inflateEnd(&fp->stream);
  113. free(fp);
  114. return gzclose(gz);
  115. #else
  116. FILE *f = fp->f;
  117. free(fp);
  118. return fclose(f);
  119. #endif
  120. }
  121. size_t cucul_file_read(cucul_file_t *fp, void *ptr, size_t size)
  122. {
  123. #if defined __KERNEL__
  124. return 0;
  125. #elif defined HAVE_ZLIB_H
  126. if(fp->zip)
  127. return zipread(fp, ptr, size);
  128. return gzread(fp->gz, ptr, size);
  129. #else
  130. return fread(ptr, 1, size, fp->f);
  131. #endif
  132. }
  133. size_t cucul_file_write(cucul_file_t *fp, const void *ptr, size_t size)
  134. {
  135. if(fp->readonly)
  136. return 0;
  137. #if defined __KERNEL__
  138. return 0;
  139. #elif defined HAVE_ZLIB_H
  140. return gzwrite(fp->gz, ptr, size);
  141. #else
  142. return fwrite(ptr, 1, size, fp->f);
  143. #endif
  144. }
  145. char *cucul_file_gets(cucul_file_t *fp, char *s, int size)
  146. {
  147. #if defined __KERNEL__
  148. return NULL;
  149. #elif defined HAVE_ZLIB_H
  150. if(fp->zip)
  151. {
  152. int i;
  153. for(i = 0; i < size; i++)
  154. {
  155. int ret = zipread(fp, s + i, 1);
  156. if(ret < 0)
  157. return NULL;
  158. if(ret == 0 || s[i] == '\n')
  159. {
  160. if(i + 1 < size)
  161. s[i + 1] = '\0';
  162. return s;
  163. }
  164. }
  165. return s;
  166. }
  167. return gzgets(fp->gz, s, size);
  168. #else
  169. return fgets(s, size, fp->f);
  170. #endif
  171. }
  172. int cucul_file_eof(cucul_file_t *fp)
  173. {
  174. #if defined __KERNEL__
  175. return 1;
  176. #elif defined HAVE_ZLIB_H
  177. return fp->zip ? fp->eof : gzeof(fp->gz);
  178. #else
  179. return feof(fp->f);
  180. #endif
  181. }
  182. #if !defined __KERNEL__ && defined HAVE_ZLIB_H
  183. static int zipread(cucul_file_t *fp, void *buf, unsigned int len)
  184. {
  185. unsigned int total_read = 0;
  186. if(len == 0)
  187. return 0;
  188. fp->stream.next_out = buf;
  189. fp->stream.avail_out = len;
  190. while(fp->stream.avail_out > 0)
  191. {
  192. unsigned int tmp;
  193. int ret = 0;
  194. if(fp->stream.avail_in == 0 && !gzeof(fp->gz))
  195. {
  196. int bytes_read;
  197. bytes_read = gzread(fp->gz, fp->read_buffer, READSIZE);
  198. if(bytes_read < 0)
  199. return -1;
  200. fp->stream.next_in = fp->read_buffer;
  201. fp->stream.avail_in = bytes_read;
  202. }
  203. tmp = fp->stream.total_out;
  204. ret = inflate(&fp->stream, Z_SYNC_FLUSH);
  205. total_read += fp->stream.total_out - tmp;
  206. if(ret == Z_STREAM_END)
  207. {
  208. fp->eof = 1;
  209. return total_read;
  210. }
  211. if(ret != Z_OK)
  212. return ret;
  213. }
  214. return total_read;
  215. }
  216. #endif