zip_io.h
1 /*************************************************************************/
2 /* zip_io.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef ZIP_IO_H
30 #define ZIP_IO_H
31 
32 #include "io/zip.h"
33 #include "io/unzip.h"
34 #include "os/file_access.h"
35 #include "os/copymem.h"
36 
37 
38 static void* zipio_open(void* data, const char* p_fname, int mode) {
39 
40  FileAccess *&f = *(FileAccess**)data;
41 
42  String fname;
43  fname.parse_utf8(p_fname);
44 
45  if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
46  f = FileAccess::open(fname,FileAccess::WRITE);
47  } else {
48 
49  f = FileAccess::open(fname,FileAccess::READ);
50  }
51 
52  if (!f)
53  return NULL;
54 
55  return data;
56 
57 };
58 
59 static uLong zipio_read(void* data, void* fdata, void* buf, uLong size) {
60 
61  FileAccess* f = *(FileAccess**)data;
62  return f->get_buffer((uint8_t*)buf, size);
63 
64 };
65 
66 static uLong zipio_write(voidpf opaque, voidpf stream, const void* buf, uLong size) {
67 
68  FileAccess* f = *(FileAccess**)opaque;
69  f->store_buffer((uint8_t*)buf, size);
70  return size;
71 };
72 
73 
74 static long zipio_tell (voidpf opaque, voidpf stream) {
75 
76  FileAccess* f = *(FileAccess**)opaque;
77  return f->get_pos();
78 };
79 
80 static long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
81 
82  FileAccess* f = *(FileAccess**)opaque;
83 
84  int pos = offset;
85  switch (origin) {
86 
87  case ZLIB_FILEFUNC_SEEK_CUR:
88  pos = f->get_pos() + offset;
89  break;
90  case ZLIB_FILEFUNC_SEEK_END:
91  pos = f->get_len() + offset;
92  break;
93  default:
94  break;
95  };
96 
97  f->seek(pos);
98  return 0;
99 };
100 
101 static int zipio_close(voidpf opaque, voidpf stream) {
102 
103  FileAccess*& f = *(FileAccess**)opaque;
104  if (f) {
105  f->close();
106  f=NULL;
107  }
108  return 0;
109 };
110 
111 static int zipio_testerror(voidpf opaque, voidpf stream) {
112 
113  FileAccess* f = *(FileAccess**)opaque;
114  return (f && f->get_error()!=OK)?1:0;
115 };
116 
117 
118 
119 static voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) {
120 
121  voidpf ptr =memalloc(items*size);
122  zeromem(ptr,items*size);
123  return ptr;
124 }
125 
126 
127 static void zipio_free(voidpf opaque, voidpf address) {
128 
129  memfree(address);
130 }
131 
132 
133 static zlib_filefunc_def zipio_create_io_from_file(FileAccess **p_file) {
134 
136  io.opaque = p_file;
137  io.zopen_file = zipio_open;
138  io.zread_file = zipio_read;
139  io.zwrite_file = zipio_write;
140  io.ztell_file = zipio_tell;
141  io.zseek_file = zipio_seek;
142  io.zclose_file = zipio_close;
143  io.zerror_file = zipio_testerror;
144  io.alloc_mem=zipio_alloc;
145  io.free_mem=zipio_free;
146  return io;
147 }
148 
149 
150 
151 #endif // ZIP_IO_H
virtual size_t get_len() const =0
get size of the file
Definition: ioapi.h:136
virtual size_t get_pos() const =0
get position in the file
virtual void store_buffer(const uint8_t *p_src, int p_length)
store an array of bytes
Definition: file_access.cpp:465
virtual void seek(size_t p_position)=0
seek to a given position
Definition: ustring.h:64
Definition: file_access.h:40
virtual int get_buffer(uint8_t *p_dst, int p_length) const
get an array of bytes
Definition: file_access.cpp:335
virtual void close()=0
close a file
virtual Error get_error() const =0
get last error