File: utils.cpp

package info (click to toggle)
scummvm-tools 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,940 kB
  • sloc: cpp: 76,819; python: 6,550; sh: 4,661; perl: 1,530; ansic: 646; makefile: 360
file content (95 lines) | stat: -rw-r--r-- 3,085 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* ScummVM Tools
 *
 * ScummVM Tools is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "engines/mohawk/utils.h"

#include "common/endian.h"
#include "common/util.h"

#define ATOM_STCO MKID_BE('stco')
#define ATOM_MOOV MKID_BE('moov')
#define ATOM_TRAK MKID_BE('trak')
#define ATOM_MDIA MKID_BE('mdia')
#define ATOM_MINF MKID_BE('minf')
#define ATOM_STBL MKID_BE('stbl')

#define BUF_SIZE 8192

void copyBytes(Common::File *src, Common::File *outputFile, uint32 bytesToCopy) {
	byte tempBuffer[BUF_SIZE];

	if (src->pos() + bytesToCopy > src->size()) {
		error("Unexpected end of stream (pos: %d + bytesToCopy: %d > totalSize: %d",
		      src->pos(), bytesToCopy, src->size());
	}

	while (bytesToCopy > 0) {
		size_t bytesCopied = src->read_noThrow(tempBuffer, MIN<uint32>(bytesToCopy, BUF_SIZE));
		outputFile->write(tempBuffer, bytesCopied);
		bytesToCopy -= bytesCopied;
	}
}

void adjustQuickTimeAtomOffsets(Common::File *src, uint32 parentSize, int32 offset, Common::File *outputFile) {
	static const uint32 kAtomHeaderSize = sizeof(uint32) + sizeof(uint32); // size, type
	uint32 totalSize = 0;

	while (totalSize + kAtomHeaderSize < parentSize) {
		uint32 atomSize = src->readUint32BE();
		uint32 atomType = src->readUint32BE();
		outputFile->writeUint32BE(atomSize);
		outputFile->writeUint32BE(atomType);

		if (atomSize == 1) {
			error("64 bit extended size is not supported in QuickTime");
		}

		if (atomSize < kAtomHeaderSize) {
			error("Invalid atom size %d", atomSize);
		}

		if (atomType == ATOM_MOOV || atomType == ATOM_TRAK
		    || atomType == ATOM_MDIA || atomType == ATOM_MINF
		    || atomType == ATOM_STBL) {
			// Recurse into container atom types
			adjustQuickTimeAtomOffsets(src, atomSize - kAtomHeaderSize, offset, outputFile);

		} else if (atomType == ATOM_STCO) {
			copyBytes(src, outputFile, 4); // Copy flags

			uint32 chunkCount = src->readUint32BE();
			outputFile->writeUint32BE(chunkCount);

			for (uint32 i = 0; i < chunkCount; i++) {
				uint32 chunkOffset = src->readUint32BE() + offset;
				outputFile->writeUint32BE(chunkOffset);
			}

		} else {
			copyBytes(src, outputFile, atomSize - kAtomHeaderSize);
		}

		totalSize += atomSize;
	}

	if (totalSize != parentSize)
		error("Unexpected position in atom %d (expected %d)", totalSize, parentSize);
}