| [8] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| [151] | 7 | |
|---|
| 8 | |
|---|
| [8] | 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| [151] | 13 | |
|---|
| [8] | 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #define LIB3DS_EXPORT |
|---|
| [1563] | 23 | #include "chunk.h" |
|---|
| 24 | #include "readwrite.h" |
|---|
| 25 | #include "chunktable.h" |
|---|
| [8] | 26 | #include <string.h> |
|---|
| [151] | 27 | #include <stdarg.h> |
|---|
| [8] | 28 | |
|---|
| [151] | 29 | |
|---|
| [8] | 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| [151] | 33 | |
|---|
| [8] | 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| [151] | 40 | |
|---|
| [8] | 41 | static Lib3dsBool enable_dump=LIB3DS_FALSE; |
|---|
| 42 | static Lib3dsBool enable_unknown=LIB3DS_FALSE; |
|---|
| 43 | static char lib3ds_chunk_level[128]=""; |
|---|
| 44 | |
|---|
| [151] | 45 | |
|---|
| [8] | 46 | static void |
|---|
| [576] | 47 | lib3ds_chunk_debug_enter(Lib3dsChunk *) |
|---|
| [8] | 48 | { |
|---|
| [151] | 49 | strcat(lib3ds_chunk_level, " "); |
|---|
| [8] | 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | static void |
|---|
| [576] | 54 | lib3ds_chunk_debug_leave(Lib3dsChunk *) |
|---|
| [8] | 55 | { |
|---|
| [151] | 56 | lib3ds_chunk_level[strlen(lib3ds_chunk_level)-2]=0; |
|---|
| [8] | 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | static void |
|---|
| 61 | lib3ds_chunk_debug_dump(Lib3dsChunk *c) |
|---|
| 62 | { |
|---|
| [151] | 63 | if (enable_dump) { |
|---|
| [5453] | 64 | printf("%s%s (0x%X) size=%u\n", |
|---|
| [151] | 65 | lib3ds_chunk_level, |
|---|
| 66 | lib3ds_chunk_name(c->chunk), |
|---|
| 67 | c->chunk, |
|---|
| 68 | c->size |
|---|
| 69 | ); |
|---|
| 70 | } |
|---|
| [8] | 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | void |
|---|
| 78 | lib3ds_chunk_enable_dump(Lib3dsBool enable, Lib3dsBool unknown) |
|---|
| 79 | { |
|---|
| [151] | 80 | enable_dump=enable; |
|---|
| 81 | enable_unknown=unknown; |
|---|
| [8] | 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | Lib3dsBool |
|---|
| [10076] | 96 | lib3ds_chunk_read(Lib3dsChunk *c, iostream *strm) |
|---|
| [8] | 97 | { |
|---|
| [151] | 98 | ASSERT(c); |
|---|
| [10076] | 99 | ASSERT(strm); |
|---|
| 100 | c->cur=strm->tellg(); |
|---|
| 101 | c->chunk=lib3ds_word_read(strm); |
|---|
| 102 | c->size=lib3ds_dword_read(strm); |
|---|
| [151] | 103 | c->end=c->cur+c->size; |
|---|
| 104 | c->cur+=6; |
|---|
| [10076] | 105 | |
|---|
| 106 | if (strm->fail()||(c->size<6)) { |
|---|
| [151] | 107 | return(LIB3DS_FALSE); |
|---|
| 108 | } |
|---|
| 109 | return(LIB3DS_TRUE); |
|---|
| [8] | 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | Lib3dsBool |
|---|
| [10076] | 117 | lib3ds_chunk_read_start(Lib3dsChunk *c, Lib3dsWord chunk, iostream *strm) |
|---|
| [8] | 118 | { |
|---|
| [151] | 119 | ASSERT(c); |
|---|
| [10076] | 120 | ASSERT(strm); |
|---|
| 121 | if (!lib3ds_chunk_read(c, strm)) { |
|---|
| [151] | 122 | return(LIB3DS_FALSE); |
|---|
| 123 | } |
|---|
| 124 | lib3ds_chunk_debug_enter(c); |
|---|
| 125 | return((chunk==0) || (c->chunk==chunk)); |
|---|
| [8] | 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | void |
|---|
| [10076] | 133 | lib3ds_chunk_read_tell(Lib3dsChunk *c, iostream *strm) |
|---|
| [8] | 134 | { |
|---|
| [10076] | 135 | c->cur=strm->tellg(); |
|---|
| [8] | 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | Lib3dsWord |
|---|
| [10076] | 143 | lib3ds_chunk_read_next(Lib3dsChunk *c, iostream *strm) |
|---|
| [8] | 144 | { |
|---|
| [151] | 145 | Lib3dsChunk d; |
|---|
| [8] | 146 | |
|---|
| [151] | 147 | if (c->cur>=c->end) { |
|---|
| 148 | ASSERT(c->cur==c->end); |
|---|
| 149 | return(0); |
|---|
| 150 | } |
|---|
| [8] | 151 | |
|---|
| [10076] | 152 | strm->seekg((long)c->cur,ios_base::beg); |
|---|
| 153 | d.chunk=lib3ds_word_read(strm); |
|---|
| 154 | d.size=lib3ds_dword_read(strm); |
|---|
| [151] | 155 | lib3ds_chunk_debug_dump(&d); |
|---|
| 156 | c->cur+=d.size; |
|---|
| 157 | return(d.chunk); |
|---|
| [8] | 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | void |
|---|
| [10076] | 165 | lib3ds_chunk_read_reset(Lib3dsChunk *, iostream *strm) |
|---|
| 166 | { |
|---|
| 167 | strm->seekg(-6,ios_base::cur); |
|---|
| [8] | 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | void |
|---|
| [10076] | 175 | lib3ds_chunk_read_end(Lib3dsChunk *c, iostream *strm) |
|---|
| [8] | 176 | { |
|---|
| [151] | 177 | lib3ds_chunk_debug_leave(c); |
|---|
| [10076] | 178 | strm->seekg(c->end,ios_base::beg); |
|---|
| [8] | 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | Lib3dsBool |
|---|
| [10076] | 193 | lib3ds_chunk_write(Lib3dsChunk *c, iostream *strm) |
|---|
| [8] | 194 | { |
|---|
| [151] | 195 | ASSERT(c); |
|---|
| [10076] | 196 | if (!lib3ds_word_write(c->chunk, strm)) { |
|---|
| [151] | 197 | LIB3DS_ERROR_LOG; |
|---|
| 198 | return(LIB3DS_FALSE); |
|---|
| 199 | } |
|---|
| [10076] | 200 | if (!lib3ds_dword_write(c->size, strm)) { |
|---|
| [151] | 201 | LIB3DS_ERROR_LOG; |
|---|
| 202 | return(LIB3DS_FALSE); |
|---|
| 203 | } |
|---|
| 204 | return(LIB3DS_TRUE); |
|---|
| [8] | 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | Lib3dsBool |
|---|
| [10076] | 212 | lib3ds_chunk_write_start(Lib3dsChunk *c, iostream *strm) |
|---|
| [8] | 213 | { |
|---|
| [151] | 214 | ASSERT(c); |
|---|
| 215 | c->size=0; |
|---|
| [10076] | 216 | c->cur=strm->tellp(); |
|---|
| 217 | if (!lib3ds_word_write(c->chunk, strm)) { |
|---|
| [151] | 218 | return(LIB3DS_FALSE); |
|---|
| 219 | } |
|---|
| [10076] | 220 | if (!lib3ds_dword_write(c->size, strm)) { |
|---|
| [151] | 221 | return(LIB3DS_FALSE); |
|---|
| 222 | } |
|---|
| 223 | return(LIB3DS_TRUE); |
|---|
| [8] | 224 | } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | Lib3dsBool |
|---|
| [10076] | 231 | lib3ds_chunk_write_end(Lib3dsChunk *c, iostream *strm) |
|---|
| [8] | 232 | { |
|---|
| [151] | 233 | ASSERT(c); |
|---|
| [10076] | 234 | c->size=(Lib3dsDword)(strm->tellp()) - c->cur; |
|---|
| 235 | strm->seekp(c->cur+2,ios_base::beg); |
|---|
| 236 | if (!lib3ds_dword_write(c->size, strm)) { |
|---|
| [151] | 237 | LIB3DS_ERROR_LOG; |
|---|
| 238 | return(LIB3DS_FALSE); |
|---|
| 239 | } |
|---|
| [8] | 240 | |
|---|
| [151] | 241 | c->cur+=c->size; |
|---|
| [10076] | 242 | strm->seekp(c->cur, ios_base::beg); |
|---|
| 243 | if (strm->fail()) { |
|---|
| [151] | 244 | LIB3DS_ERROR_LOG; |
|---|
| 245 | return(LIB3DS_FALSE); |
|---|
| 246 | } |
|---|
| 247 | return(LIB3DS_TRUE); |
|---|
| [8] | 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | const char* |
|---|
| 255 | lib3ds_chunk_name(Lib3dsWord chunk) |
|---|
| 256 | { |
|---|
| [151] | 257 | Lib3dsChunkTable *p; |
|---|
| [8] | 258 | |
|---|
| [151] | 259 | for (p=lib3ds_chunk_table; p->name!=0; ++p) { |
|---|
| 260 | if (p->chunk==chunk) { |
|---|
| 261 | return(p->name); |
|---|
| [8] | 262 | } |
|---|
| [151] | 263 | } |
|---|
| 264 | return("***UNKNOWN***"); |
|---|
| [8] | 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | void |
|---|
| 272 | lib3ds_chunk_unknown(Lib3dsWord chunk) |
|---|
| 273 | { |
|---|
| [151] | 274 | if (enable_unknown) { |
|---|
| 275 | printf("%s***WARNING*** Unknown Chunk: %s (0x%X)\n", |
|---|
| 276 | lib3ds_chunk_level, |
|---|
| 277 | lib3ds_chunk_name(chunk), |
|---|
| 278 | chunk |
|---|
| 279 | ); |
|---|
| 280 | } |
|---|
| [8] | 281 | } |
|---|
| [151] | 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | void |
|---|
| 288 | lib3ds_chunk_dump_info(const char *format, ...) |
|---|
| 289 | { |
|---|
| 290 | if (enable_dump) { |
|---|
| 291 | char s[1024]; |
|---|
| 292 | va_list marker; |
|---|
| 293 | |
|---|
| 294 | va_start(marker, format); |
|---|
| 295 | vsprintf(s, format, marker); |
|---|
| 296 | va_end(marker); |
|---|
| 297 | |
|---|
| 298 | printf("%s%s\n", lib3ds_chunk_level, s); |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|