| 1 | /* |
| 2 | * Copyright (C) 2002-2024 D2X Project |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <cstring> // for mem* functions |
| 19 | |
| 20 | #include "mvelib.h" |
| 21 | |
| 22 | static const char MVE_HEADER[] = "Interplay MVE File\x1A"; |
| 23 | static const short MVE_HDRCONST1 = 0x001A; |
| 24 | static const short MVE_HDRCONST2 = 0x0100; |
| 25 | static const short MVE_HDRCONST3 = 0x1133; |
| 26 | |
| 27 | mve_cb_Read mve_read; |
| 28 | mve_cb_Alloc mve_alloc; |
| 29 | mve_cb_Free mve_free; |
| 30 | mve_cb_ShowFrame mve_showframe; |
| 31 | mve_cb_SetPalette mve_setpalette; |
| 32 | |
| 33 | /* |
| 34 | * private utility functions |
| 35 | */ |
| 36 | static short _mve_get_short(unsigned char *data); |
| 37 | static unsigned short _mve_get_ushort(unsigned char *data); |
| 38 | |
| 39 | /* |
| 40 | * private functions for mvefile |
| 41 | */ |
| 42 | static MVEFILE *_mvefile_alloc(); |
| 43 | static void _mvefile_free(MVEFILE *movie); |
| 44 | static int _mvefile_open(MVEFILE *movie, void *stream); |
| 45 | static void _mvefile_reset(MVEFILE *movie); |
| 46 | static int _mvefile_read_header(MVEFILE *movie); |
| 47 | static void _mvefile_set_buffer_size(MVEFILE *movie, int buf_size); |
| 48 | static int _mvefile_fetch_next_chunk(MVEFILE *movie); |
| 49 | |
| 50 | /* |
| 51 | * private functions for mvestream |
| 52 | */ |
| 53 | static MVESTREAM *_mvestream_alloc(); |
| 54 | static void _mvestream_free(MVESTREAM *movie); |
| 55 | static int _mvestream_open(MVESTREAM *movie, void *stream); |
| 56 | static void _mvestream_reset(MVESTREAM *movie); |
| 57 | |
| 58 | /************************************************************ |
| 59 | * public MVEFILE functions |
| 60 | ************************************************************/ |
| 61 | |
| 62 | /* |
| 63 | * open an MVE file |
| 64 | */ |
| 65 | MVEFILE *mvefile_open(void *stream) { |
| 66 | MVEFILE *file; |
| 67 | |
| 68 | /* create the file */ |
| 69 | file = _mvefile_alloc(); |
| 70 | if (!_mvefile_open(file, stream)) { |
| 71 | _mvefile_free(file); |
| 72 | return nullptr; |
| 73 | } |
| 74 | |
| 75 | /* initialize the file */ |
| 76 | _mvefile_set_buffer_size(file, 1024); |
| 77 | |
| 78 | /* verify the file's header */ |
| 79 | if (!_mvefile_read_header(file)) { |
| 80 | _mvefile_free(file); |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
| 84 | /* now, prefetch the next chunk */ |
| 85 | _mvefile_fetch_next_chunk(file); |
| 86 | |
| 87 | return file; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * close a MVE file |
| 92 | */ |
| 93 | void mvefile_close(MVEFILE *movie) { _mvefile_free(movie); } |
| 94 | |
| 95 | /* |
| 96 | * reset a MVE file |
| 97 | */ |
| 98 | void mvefile_reset(MVEFILE *file) { |
| 99 | _mvefile_reset(file); |
| 100 | |
| 101 | /* initialize the file */ |
| 102 | _mvefile_set_buffer_size(file, 1024); |
| 103 | |
| 104 | /* verify the file's header */ |
| 105 | if (!_mvefile_read_header(file)) { |
| 106 | _mvefile_free(file); |
| 107 | // return NULL; |
| 108 | } |
| 109 | |
| 110 | /* now, prefetch the next chunk */ |
| 111 | _mvefile_fetch_next_chunk(file); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * get the size of the next segment |
| 116 | */ |
| 117 | int mvefile_get_next_segment_size(MVEFILE *movie) { |
| 118 | /* if nothing is cached, fail */ |
| 119 | if (movie->cur_chunk == nullptr || movie->next_segment >= movie->cur_fill) |
| 120 | return -1; |
| 121 | |
| 122 | /* if we don't have enough data to get a segment, fail */ |
| 123 | if (movie->cur_fill - movie->next_segment < 4) |
| 124 | return -1; |
| 125 | |
| 126 | /* otherwise, get the data length */ |
| 127 | return _mve_get_short(movie->cur_chunk + movie->next_segment); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * get type of next segment in chunk (0xff if no more segments in chunk) |
| 132 | */ |
| 133 | unsigned char mvefile_get_next_segment_major(MVEFILE *movie) { |
| 134 | /* if nothing is cached, fail */ |
| 135 | if (movie->cur_chunk == nullptr || movie->next_segment >= movie->cur_fill) |
| 136 | return 0xff; |
| 137 | |
| 138 | /* if we don't have enough data to get a segment, fail */ |
| 139 | if (movie->cur_fill - movie->next_segment < 4) |
| 140 | return 0xff; |
| 141 | |
| 142 | /* otherwise, get the data length */ |
| 143 | return movie->cur_chunk[movie->next_segment + 2]; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * get subtype (version) of next segment in chunk (0xff if no more segments in |
| 148 | * chunk) |
| 149 | */ |
| 150 | unsigned char mvefile_get_next_segment_minor(MVEFILE *movie) { |
| 151 | /* if nothing is cached, fail */ |
| 152 | if (movie->cur_chunk == nullptr || movie->next_segment >= movie->cur_fill) |
| 153 | return 0xff; |
| 154 | |
| 155 | /* if we don't have enough data to get a segment, fail */ |
| 156 | if (movie->cur_fill - movie->next_segment < 4) |
| 157 | return 0xff; |
| 158 | |
| 159 | /* otherwise, get the data length */ |
| 160 | return movie->cur_chunk[movie->next_segment + 3]; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * see next segment (return NULL if no next segment) |
| 165 | */ |
| 166 | unsigned char *mvefile_get_next_segment(MVEFILE *movie) { |
| 167 | /* if nothing is cached, fail */ |
| 168 | if (movie->cur_chunk == nullptr || movie->next_segment >= movie->cur_fill) |
| 169 | return nullptr; |
| 170 | |
| 171 | /* if we don't have enough data to get a segment, fail */ |
| 172 | if (movie->cur_fill - movie->next_segment < 4) |
| 173 | return nullptr; |
| 174 | |
| 175 | /* otherwise, get the data length */ |
| 176 | return movie->cur_chunk + movie->next_segment + 4; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * advance to next segment |
| 181 | */ |
| 182 | void mvefile_advance_segment(MVEFILE *movie) { |
| 183 | /* if nothing is cached, fail */ |
| 184 | if (movie->cur_chunk == nullptr || movie->next_segment >= movie->cur_fill) |
| 185 | return; |
| 186 | |
| 187 | /* if we don't have enough data to get a segment, fail */ |
| 188 | if (movie->cur_fill - movie->next_segment < 4) |
| 189 | return; |
| 190 | |
| 191 | /* else, advance to next segment */ |
| 192 | movie->next_segment += (4 + _mve_get_ushort(movie->cur_chunk + movie->next_segment)); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * fetch the next chunk (return 0 if at end of stream) |
| 197 | */ |
| 198 | int mvefile_fetch_next_chunk(MVEFILE *movie) { return _mvefile_fetch_next_chunk(movie); } |
| 199 | |
| 200 | /************************************************************ |
| 201 | * public MVESTREAM functions |
| 202 | ************************************************************/ |
| 203 | |
| 204 | /* |
| 205 | * open an MVE stream |
| 206 | */ |
| 207 | MVESTREAM *mve_open(void *stream) { |
| 208 | MVESTREAM *movie; |
| 209 | |
| 210 | /* allocate */ |
| 211 | movie = _mvestream_alloc(); |
| 212 | |
| 213 | /* open */ |
| 214 | if (!_mvestream_open(movie, stream)) { |
| 215 | _mvestream_free(movie); |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
| 219 | return movie; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * close an MVE stream |
| 224 | */ |
| 225 | void mve_close(MVESTREAM *movie) { _mvestream_free(movie); } |
| 226 | |
| 227 | /* |
| 228 | * reset an MVE stream |
| 229 | */ |
| 230 | void mve_reset(MVESTREAM *movie) { _mvestream_reset(movie); } |
| 231 | |
| 232 | /* |
| 233 | * set segment type handler |
| 234 | */ |
| 235 | void mve_set_handler(MVESTREAM *movie, unsigned char major, MVESEGMENTHANDLER handler) { |
| 236 | if (major < 32) |
| 237 | movie->handlers[major] = handler; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * set segment handler context |
| 242 | */ |
| 243 | void mve_set_handler_context(MVESTREAM *movie, void *context) { movie->context = context; } |
| 244 | |
| 245 | /* |
| 246 | * play next chunk |
| 247 | */ |
| 248 | int mve_play_next_chunk(MVESTREAM *movie) { |
| 249 | unsigned char major, minor; |
| 250 | unsigned char *data; |
| 251 | int len; |
| 252 | |
| 253 | /* loop over segments */ |
| 254 | major = mvefile_get_next_segment_major(movie->movie); |
| 255 | while (major != 0xff) { |
| 256 | /* check whether to handle the segment */ |
| 257 | if (major < 32 && movie->handlers[major] != nullptr) { |
| 258 | minor = mvefile_get_next_segment_minor(movie->movie); |
| 259 | len = mvefile_get_next_segment_size(movie->movie); |
| 260 | data = mvefile_get_next_segment(movie->movie); |
| 261 | |
| 262 | if (!movie->handlers[major](major, minor, data, len, movie->context)) |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /* advance to next segment */ |
| 267 | mvefile_advance_segment(movie->movie); |
| 268 | major = mvefile_get_next_segment_major(movie->movie); |
| 269 | } |
| 270 | |
| 271 | if (!mvefile_fetch_next_chunk(movie->movie)) |
| 272 | return 0; |
| 273 | |
| 274 | /* return status */ |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | /************************************************************ |
| 279 | * private functions |
| 280 | ************************************************************/ |
| 281 | |
| 282 | /* |
| 283 | * allocate an MVEFILE |
| 284 | */ |
| 285 | static MVEFILE *_mvefile_alloc() { |
| 286 | MVEFILE *file = (MVEFILE *)mve_alloc(sizeof(MVEFILE)); |
| 287 | file->stream = nullptr; |
| 288 | file->cur_chunk = nullptr; |
| 289 | file->buf_size = 0; |
| 290 | file->cur_fill = 0; |
| 291 | file->next_segment = 0; |
| 292 | |
| 293 | return file; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * free an MVE file |
| 298 | */ |
| 299 | static void _mvefile_free(MVEFILE *movie) { |
| 300 | /* free the stream */ |
| 301 | movie->stream = nullptr; |
| 302 | |
| 303 | /* free the buffer */ |
| 304 | if (movie->cur_chunk) |
| 305 | mve_free(movie->cur_chunk); |
| 306 | movie->cur_chunk = nullptr; |
| 307 | |
| 308 | /* not strictly necessary */ |
| 309 | movie->buf_size = 0; |
| 310 | movie->cur_fill = 0; |
| 311 | movie->next_segment = 0; |
| 312 | |
| 313 | /* free the struct */ |
| 314 | mve_free(movie); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * open the file stream in thie object |
| 319 | */ |
| 320 | static int _mvefile_open(MVEFILE *file, void *stream) { |
| 321 | file->stream = stream; |
| 322 | if (!file->stream) |
| 323 | return 0; |
| 324 | |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * allocate an MVEFILE |
| 330 | */ |
| 331 | static void _mvefile_reset(MVEFILE *file) { |
| 332 | fseek((FILE *)file->stream, 0, SEEK_SET); |
| 333 | file->cur_chunk = nullptr; |
| 334 | file->buf_size = 0; |
| 335 | file->cur_fill = 0; |
| 336 | file->next_segment = 0; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * read and verify the header of the recently opened file |
| 341 | */ |
| 342 | static int _mvefile_read_header(MVEFILE *movie) { |
| 343 | unsigned char buffer[26]; |
| 344 | |
| 345 | /* check the file is open */ |
| 346 | if (!movie->stream) |
| 347 | return 0; |
| 348 | |
| 349 | /* check the file is long enough */ |
| 350 | if (!mve_read(movie->stream, buffer, 26)) |
| 351 | return 0; |
| 352 | |
| 353 | /* check the signature */ |
| 354 | if (memcmp(buffer, MVE_HEADER, 20)) |
| 355 | return 0; |
| 356 | |
| 357 | /* check the hard-coded constants */ |
| 358 | if (_mve_get_short(buffer + 20) != MVE_HDRCONST1) |
| 359 | return 0; |
| 360 | if (_mve_get_short(buffer + 22) != MVE_HDRCONST2) |
| 361 | return 0; |
| 362 | if (_mve_get_short(buffer + 24) != MVE_HDRCONST3) |
| 363 | return 0; |
| 364 | |
| 365 | return 1; |
| 366 | } |
| 367 | |
| 368 | static void _mvefile_set_buffer_size(MVEFILE *movie, int buf_size) { |
| 369 | unsigned char *new_buffer; |
| 370 | int new_len; |
| 371 | |
| 372 | /* check if this would be a redundant operation */ |
| 373 | if (buf_size <= movie->buf_size) |
| 374 | return; |
| 375 | |
| 376 | /* allocate new buffer */ |
| 377 | new_len = 100 + buf_size; |
| 378 | new_buffer = (unsigned char *)mve_alloc(new_len); |
| 379 | |
| 380 | /* copy old data */ |
| 381 | if (movie->cur_chunk && movie->cur_fill) |
| 382 | memcpy(new_buffer, movie->cur_chunk, movie->cur_fill); |
| 383 | |
| 384 | /* free old buffer */ |
| 385 | if (movie->cur_chunk) { |
| 386 | mve_free(movie->cur_chunk); |
| 387 | movie->cur_chunk = 0; |
| 388 | } |
| 389 | |
| 390 | /* install new buffer */ |
| 391 | movie->cur_chunk = new_buffer; |
| 392 | movie->buf_size = new_len; |
| 393 | } |
| 394 | |
| 395 | static int _mvefile_fetch_next_chunk(MVEFILE *movie) { |
| 396 | unsigned char buffer[4]; |
| 397 | unsigned short length; |
| 398 | |
| 399 | /* fail if not open */ |
| 400 | if (!movie->stream) |
| 401 | return 0; |
| 402 | |
| 403 | /* fail if we can't read the next segment descriptor */ |
| 404 | if (!mve_read(movie->stream, buffer, 4)) |
| 405 | return 0; |
| 406 | |
| 407 | /* pull out the next length */ |
| 408 | length = _mve_get_short(buffer); |
| 409 | |
| 410 | /* make sure we've got sufficient space */ |
| 411 | _mvefile_set_buffer_size(movie, length); |
| 412 | |
| 413 | /* read the chunk */ |
| 414 | if (!mve_read(movie->stream, movie->cur_chunk, length)) |
| 415 | return 0; |
| 416 | movie->cur_fill = length; |
| 417 | movie->next_segment = 0; |
| 418 | |
| 419 | return 1; |
| 420 | } |
| 421 | |
| 422 | static short _mve_get_short(unsigned char *data) { |
| 423 | short value; |
| 424 | value = data[0] | (data[1] << 8); |
| 425 | return value; |
| 426 | } |
| 427 | |
| 428 | static unsigned short _mve_get_ushort(unsigned char *data) { |
| 429 | unsigned short value; |
| 430 | value = data[0] | (data[1] << 8); |
| 431 | return value; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * allocate an MVESTREAM |
| 436 | */ |
| 437 | static MVESTREAM *_mvestream_alloc() { |
| 438 | MVESTREAM *movie; |
| 439 | |
| 440 | /* allocate and zero-initialize everything */ |
| 441 | movie = (MVESTREAM *)mve_alloc(sizeof(MVESTREAM)); |
| 442 | movie->movie = nullptr; |
| 443 | movie->context = 0; |
| 444 | memset(movie->handlers, 0, sizeof(movie->handlers)); |
| 445 | |
| 446 | return movie; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * free an MVESTREAM |
| 451 | */ |
| 452 | static void _mvestream_free(MVESTREAM *movie) { |
| 453 | /* close MVEFILE */ |
| 454 | if (movie == nullptr) |
| 455 | return; |
| 456 | if (movie->movie != nullptr) |
| 457 | mvefile_close(movie->movie); |
| 458 | movie->movie = nullptr; |
| 459 | |
| 460 | /* clear context and handlers */ |
| 461 | movie->context = nullptr; |
| 462 | memset(movie->handlers, 0, sizeof(movie->handlers)); |
| 463 | |
| 464 | /* free the struct */ |
| 465 | mve_free(movie); |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * open an MVESTREAM object |
| 470 | */ |
| 471 | static int _mvestream_open(MVESTREAM *movie, void *stream) { |
| 472 | movie->movie = mvefile_open(stream); |
| 473 | |
| 474 | return (movie->movie == nullptr) ? 0 : 1; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * reset an MVESTREAM |
| 479 | */ |
| 480 | static void _mvestream_reset(MVESTREAM *movie) { mvefile_reset(movie->movie); } |