Home | API | File List | Examples | Download

lib3ds_chunk.c

00001 /*
00002     Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
00003     All rights reserved.
00004     
00005     This program is free  software: you can redistribute it and/or modify 
00006     it under the terms of the GNU Lesser General Public License as published 
00007     by the Free Software Foundation, either version 2.1 of the License, or 
00008     (at your option) any later version.
00009 
00010     Thisprogram  is  distributed in the hope that it will be useful, 
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
00013     GNU Lesser General Public License for more details.
00014     
00015     You should  have received a copy of the GNU Lesser General Public License
00016     along with  this program; If not, see <http://www.gnu.org/licenses/>. 
00017 */
00018 #include "lib3ds_impl.h"
00019 
00020 
00021 /*#define LIB3DS_CHUNK_DEBUG*/
00022 /*#define LIB3DS_CHUNK_WARNING*/
00023 
00024 
00033 void
00034 lib3ds_chunk_read(Lib3dsChunk *c, Lib3dsIo *io) {
00035     assert(c);
00036     assert(io);
00037     c->cur = lib3ds_io_tell(io);
00038     c->chunk = lib3ds_io_read_word(io);
00039     c->size = lib3ds_io_read_dword(io);
00040     c->end = c->cur + c->size;
00041     c->cur += 6;
00042     if (c->size < 6) {
00043         lib3ds_io_log(io, LIB3DS_LOG_ERROR, "Invalid chunk header.");
00044     }
00045 
00046 }
00047 
00048 
00049 void
00050 lib3ds_chunk_read_start(Lib3dsChunk *c, uint16_t chunk, Lib3dsIo *io) {
00051     assert(c);
00052     assert(io);
00053     lib3ds_chunk_read(c, io);
00054     if ((chunk != 0) && (c->chunk != chunk)) {
00055         lib3ds_io_log(io, LIB3DS_LOG_ERROR, "Unexpected chunk found.");
00056     }
00057     ((Lib3dsIoImpl*)io->impl)->log_indent++;
00058 }
00059 
00060 
00061 void
00062 lib3ds_chunk_read_tell(Lib3dsChunk *c, Lib3dsIo *io) {
00063     c->cur = lib3ds_io_tell(io);
00064 }
00065 
00066 
00067 uint16_t
00068 lib3ds_chunk_read_next(Lib3dsChunk *c, Lib3dsIo *io) {
00069     Lib3dsChunk d;
00070 
00071     if (c->cur >= c->end) {
00072         assert(c->cur == c->end);
00073         return 0;
00074     }
00075 
00076     lib3ds_io_seek(io, (long)c->cur, LIB3DS_SEEK_SET);
00077     d.chunk = lib3ds_io_read_word(io);
00078     d.size = lib3ds_io_read_dword(io);
00079     c->cur += d.size;
00080 
00081     if (io->log_func) {
00082         lib3ds_io_log(io, LIB3DS_LOG_INFO, "%s (0x%X) size=%lu", lib3ds_chunk_name(d.chunk), d.chunk, d.size);
00083     }
00084 
00085     return d.chunk;
00086 }
00087 
00088 
00089 void
00090 lib3ds_chunk_read_reset(Lib3dsChunk *c, Lib3dsIo *io) {
00091     lib3ds_io_seek(io, -6, LIB3DS_SEEK_CUR);
00092 }
00093 
00094 
00095 void
00096 lib3ds_chunk_read_end(Lib3dsChunk *c, Lib3dsIo *io) {
00097     ((Lib3dsIoImpl*)io->impl)->log_indent--;
00098     lib3ds_io_seek(io, c->end, LIB3DS_SEEK_SET);
00099 }
00100 
00101 
00110 void
00111 lib3ds_chunk_write(Lib3dsChunk *c, Lib3dsIo *io) {
00112     assert(c);
00113     lib3ds_io_write_word(io, c->chunk);
00114     lib3ds_io_write_dword(io, c->size);
00115 }
00116 
00117 
00118 void
00119 lib3ds_chunk_write_start(Lib3dsChunk *c, Lib3dsIo *io) {
00120     assert(c);
00121     c->size = 0;
00122     c->cur = lib3ds_io_tell(io);
00123     lib3ds_io_write_word(io, c->chunk);
00124     lib3ds_io_write_dword(io, c->size);
00125 }
00126 
00127 
00128 void
00129 lib3ds_chunk_write_end(Lib3dsChunk *c, Lib3dsIo *io) {
00130     assert(c);
00131     c->size = lib3ds_io_tell(io) - c->cur;
00132     lib3ds_io_seek(io, c->cur + 2, LIB3DS_SEEK_SET);
00133     lib3ds_io_write_dword(io, c->size);
00134     c->cur += c->size;
00135     lib3ds_io_seek(io, c->cur, LIB3DS_SEEK_SET);
00136 }
00137 
00138 
00139 void
00140 lib3ds_chunk_unknown(uint16_t chunk, Lib3dsIo *io) {
00141     if (io->log_func) {
00142         lib3ds_io_log(io, LIB3DS_LOG_WARN, "Unknown Chunk: %s (0x%X)", lib3ds_chunk_name(chunk), chunk);
00143     }
00144 }
00145 
00146