Add -Wno-narrowing to support building on GCC-4.7 (no bug, r=me).
7 void *mf_open(const char *name);
8 void mf_close(void *handle);
9 size_t mf_write(const void *buf, size_t size, size_t count, void *handle);
10 size_t mf_read(void *buf, size_t size, size_t count, void *handle);
11 size_t mf_getpos(void *handle);
12 int mf_setpos(void *handle, size_t pos);
14 sp_writefuncs_t cstd_funcs =
24 sp_file_t *spfw_create(const char *name, sp_writefuncs_t *optfuncs)
31 optfuncs = &cstd_funcs;
34 file.handle = optfuncs->fnOpen(name);
40 pFile = (sp_file_t *)malloc(sizeof(sp_file_t));
42 pFile->handle = file.handle;
43 memcpy(&pFile->funcs, optfuncs, sizeof(sp_writefuncs_t));
45 pFile->header.magic = SPFILE_MAGIC;
46 pFile->header.sections = 0;
47 pFile->header.stringtab = 0;
48 pFile->header.version = SPFILE_VERSION;
49 pFile->header.imagesize = 0;
50 pFile->header.disksize = 0;
51 pFile->header.compression = SPFILE_COMPRESSION_NONE;
52 pFile->header.dataoffs = 0;
53 pFile->lastsection = 0;
54 pFile->offsets = NULL;
55 pFile->sections = NULL;
57 pFile->nametab = NULL;
58 pFile->nametab_idx = 0;
63 void spfw_destroy(sp_file_t *spf)
68 spf->funcs.fnClose(spf->handle);
72 uint8_t spfw_add_section(sp_file_t *spf, const char *name)
81 namelen = strlen(name) + 1;
83 if (spf->header.sections == 0)
85 /** allocate for first section */
86 spf->sections = (sp_file_section_t *)malloc(sizeof(sp_file_section_t));
87 spf->offsets = (size_t *)malloc(sizeof(size_t));
88 spf->nametab = (char *)malloc(namelen);
90 uint16_t num = spf->header.sections + 1;
91 spf->sections = (sp_file_section_t *)realloc(spf->sections, sizeof(sp_file_section_t) * num);
92 spf->offsets = (size_t *)realloc(spf->offsets, sizeof(size_t) * num);
93 spf->nametab = (char *)realloc(spf->nametab, spf->nametab_idx + namelen);
96 s = spf->header.sections;
98 spf->sections[s].nameoffs = spf->nametab_idx;
100 * "fix" offset will be the second uint2 slot, which is after the previous sections after the header.
102 spf->offsets[s] = sizeof(spf->header) + (sizeof(sp_file_section_t) * spf->header.sections) + sizeof(uint32_t);
103 strcpy(&spf->nametab[spf->nametab_idx], name);
104 spf->nametab_idx += namelen;
106 return ++spf->header.sections;
109 int spfw_finalize_header(sp_file_t *spf)
112 if (spf->state != -1)
117 size = sizeof(sp_file_section_t) * spf->header.sections;
119 spf->header.stringtab = sizeof(spf->header) + size;
120 spf->header.dataoffs = spf->header.stringtab + spf->nametab_idx;
121 if (spf->funcs.fnWrite(&spf->header, sizeof(spf->header), 1, spf->handle) != 1)
125 if (spf->funcs.fnWrite(spf->sections, sizeof(sp_file_section_t), spf->header.sections, spf->handle) !=
126 spf->header.sections)
130 if (spf->funcs.fnWrite(spf->nametab, sizeof(char), spf->nametab_idx, spf->handle) != spf->nametab_idx)
134 spf->curoffs = spf->funcs.fnGetPos(spf->handle);
135 spf->lastsection = spf->curoffs;
141 int spfw_next_section(sp_file_t *spf)
146 if (spf->state < 0 || spf->state > spf->header.sections)
151 if (spf->state == (int)spf->header.sections)
156 s = (uint8_t)spf->state;
158 spf->curoffs = spf->funcs.fnGetPos(spf->handle);
159 spf->funcs.fnSetPos(spf->handle, spf->offsets[s]);
161 rest[0] = spf->lastsection;
162 rest[1] = spf->curoffs - spf->lastsection;
163 if (spf->funcs.fnWrite(rest, sizeof(uint32_t), 2, spf->handle) != 2)
168 spf->funcs.fnSetPos(spf->handle, spf->curoffs);
169 spf->lastsection = spf->curoffs;
176 int spfw_finalize_all(sp_file_t *spf)
180 if (spf->state < spf->header.sections)
185 offs = offsetof(sp_file_hdr_t, imagesize);
186 spf->header.disksize = spf->funcs.fnGetPos(spf->handle);
187 spf->header.imagesize = spf->funcs.fnGetPos(spf->handle);
188 spf->funcs.fnSetPos(spf->handle, offs);
189 spf->funcs.fnWrite(&spf->header.imagesize, sizeof(uint32_t), 1, spf->handle);
190 spf->funcs.fnSetPos(spf->handle, spf->header.imagesize);
196 * More memory file operations
199 void *mf_open(const char *name)
201 return memfile_creat(name, 1024);
204 void mf_close(void *handle)
206 memfile_destroy((memfile_t *)handle);
209 size_t mf_write(const void *buf, size_t size, size_t count, void *handle)
216 if (memfile_write((memfile_t *)handle, buf, size*count))
224 size_t mf_read(void *buf, size_t size, size_t count, void *handle)
226 return memfile_read((memfile_t *)handle, buf, size*count) / count;
229 size_t mf_getpos(void *handle)
231 return (long)memfile_tell((memfile_t *)handle);
234 int mf_setpos(void *handle, size_t pos)
236 memfile_seek((memfile_t *)handle, (long)pos);
243 * Default file operations...
244 * Based on C standard library calls.
247 void *fp_open(const char *name)
249 return fopen(name, "wb");
252 void fp_close(void *handle)
254 fclose((FILE *)handle);
257 size_t fp_write(const void *buf, size_t size, size_t count, void *handle)
259 return fwrite(buf, size, count, (FILE *)handle);
262 size_t fp_read(void *buf, size_t size, size_t count, void *handle)
264 return fread(buf, size, count, (FILE *)handle);
267 size_t fp_getpos(void *handle)
269 return (size_t)ftell((FILE *)handle);
272 int fp_setpos(void *handle, size_t pos)
274 return fseek((FILE *)handle, (long)pos, SEEK_SET);