aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sisfilerecord.cpp
blob: a483b80ee87e7238d207b251acbd6266b36d11f9 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/** -*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 2002 Daniel Brahneborg <basic.chello@se>
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "sisfilerecord.h"
#include "sisfile.h"
#include "plpintl.h"

#include <stdio.h>

SisRC
SISFileRecord::fillFrom(uint8_t* buf, int* base, off_t len, SISFile* sisFile)
{
	if (*base + 28 + 4 * 2 > len)
		return SIS_TRUNCATED;

	SisRC rc = SIS_OK;
	m_buf = buf;
	m_len = len;
	uint8_t* p = buf + *base;
	int size = 0;
	m_flags = read32(p);
	if (logLevel >= 2)
		printf(_("Got flags %d\n"), m_flags);
	m_fileType = read32(p + 4);
	if (logLevel >= 2)
		printf(_("Got file type %d\n"), m_fileType);
	m_fileDetails = read32(p + 8);
	if (logLevel >= 2)
		printf(_("Got file details %d\n"), m_fileDetails);
	m_sourceLength = read32(p + 12);
	m_sourcePtr = read32(p + 16);
//	printf(_("Got source length = %d, source name ptr = %d\n"),
//		   m_sourceLength, m_sourcePtr);
	if (logLevel >= 2)
		if (m_sourceLength > 0)
			printf(_("Got source name %.*s\n"), m_sourceLength, buf + m_sourcePtr);
	m_destLength = read32(p + 20);
	m_destPtr = read32(p + 24);
//	printf(_("Got dest length = %d, dest name ptr = %d\n"),
//		   m_destLength, m_destPtr);
	if (logLevel >= 2)
		printf(_("Got destination name %.*s\n"), m_destLength, buf + m_destPtr);
	size = 28;
	switch (m_flags)
		{
		case 0: // Only one file.
			m_fileLengths = new uint32_t[1];
			m_filePtrs = new uint32_t[1];
			m_fileLengths[0] = read32(p + size);
			m_filePtrs[0] = read32(p + size + 4);
			size += 8;
			if (logLevel >= 2)
				printf(_("File is %d bytes long (at %d) (to %d)\n"),
					   m_fileLengths[0], m_filePtrs[0],
					   m_fileLengths[0] + m_filePtrs[0]);
			if (logLevel >= 1)
				printf(_("%d .. %d (%d bytes): Single file record type %d, %.*s\n"),
					   m_filePtrs[0],
					   m_filePtrs[0] + m_fileLengths[0],
					   m_fileLengths[0],
					   m_fileType,
					   m_destLength, buf + m_destPtr);
			break;

		case 1: // One file per language.
			{
			int n = sisFile->m_header.m_nlangs;
			m_fileLengths = new uint32_t[n];
			m_filePtrs = new uint32_t[n];
			if (*base + size + n * 8 > len)
				return SIS_TRUNCATED;
			for (int i = 0; i < n; ++i)
				{
				m_fileLengths[i] = read32(p + size);
//				if (m_fileLengths[i] > len)
//					rc = SIS_TRUNCATEDDATA;
				size += 4;
				}
			for (int i = 0; i < n; ++i)
				{
				m_filePtrs[i] = read32(p + size);
				int fileLen = m_fileLengths[i];
//				if (m_filePtrs[i] + fileLen > len)
//					rc = SIS_TRUNCATEDDATA;
				size += 4;
				if (logLevel >= 2)
					printf(_("File %d (for %s) is %d bytes long (at %d)\n"),
						   i,
						   sisFile->getLanguage(i)->m_name,
						   fileLen,
						   m_filePtrs[i]);
				if (logLevel >= 1)
					printf(_("%d .. %d (%d bytes): File record (%s) for %.*s\n"),
						   m_filePtrs[i],
						   m_filePtrs[i] + fileLen,
						   fileLen,
						   sisFile->getLanguage(i)->m_name,
						   m_destLength, buf + m_destPtr);
				}
			break;
			}

		default:
			if (logLevel >= 2)
				printf(_("Unknown file flags %d\n"), m_flags);
		}
	*base += size;
	return rc;
}

uint8_t*
SISFileRecord::getFilePtr(int fileNo)
{
	if (fileNo < 0)
		return 0;
	if (m_filePtrs[fileNo] >= m_len)
		return 0;
	return &m_buf[m_filePtrs[fileNo]];
}

void
SISFileRecord::setMainDrive(char drive)
{
	if (m_buf[m_destPtr] == '!')
		m_buf[m_destPtr] = drive;
}