aboutsummaryrefslogtreecommitdiff
path: root/project/src/lib-pack.c
blob: 4d1736df757209fbaef5d66179458e2e258f0054 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

/***************************************************************************
 *                         _______ _______ _______                         *
 *                        |  ___  |____   |  ___  |                        *
 *                        | |   |_|    / /| |   |_|                        *
 *                        | |_____    / / | |_____                         *
 *                        |_____  |  / /  |_____  |                        *
 *                         _    | | / /    _    | |                        *
 *                        | |___| |/ /____| |___| |                        *
 *                        |_______|_______|_______|                        *
 *                                                                         *
 *                            Wiimms SZS Tools                             *
 *                          https://szs.wiimm.de/                          *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   This file is part of the SZS project.                                 *
 *   Visit https://szs.wiimm.de/ for project details and sources.          *
 *                                                                         *
 *   Copyright (c) 2011-2023 by Dirk Clemens <wiimm@wiimm.de>              *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
 *                                                                         *
 ***************************************************************************/

#include "lib-pack.h"

//
///////////////////////////////////////////////////////////////////////////////
///////////////			IterateFilesPACK()		///////////////
///////////////////////////////////////////////////////////////////////////////

int IterateFilesPACK
(
    struct szs_iterator_t	*it,	// iterator struct with all infos
    bool			term	// true: termination hint
)
{
    if (term)
	return 0;

    DASSERT(it);
    DASSERT(it->func_it);
    szs_file_t * szs = it->szs;
    DASSERT(szs);

    if ( !szs->data || szs->size <= sizeof(pack_header_t) )
	return -1;

    //--- analyze file header

    const pack_header_t * pack	= (pack_header_t*)szs->data;
    const uint size		= ntohl(pack->file_size);
    const uint n_files		= ntohl(pack->n_files);
    const uint offset_metric	= ntohl(pack->offset_metric);

    if
    (	ntohl(pack->magic) != PACK_MAGIC_NUM
	|| size < szs->size
	|| offset_metric < 0x20
	|| offset_metric + n_files * sizeof(pack_metric_t) >= size
    )
    return -1;


    //--- cut file header

    if (it->itpar.cut_files)
    {
	it->index	= 0;
	it->fst_item	= 0;
	it->is_dir	= 0;

	it->off		= 0;
	it->size	= sizeof(pack_header_t);
	StringCopyS(it->path,sizeof(it->path),".PACK.header");
	it->func_it(it,false);

	it->off		= sizeof(pack_header_t);
	it->size	= offset_metric;
	StringCopyS(it->path,sizeof(it->path),".PACK.filenames");
	it->func_it(it,false);

	it->off		= offset_metric;
	it->size	= offset_metric + n_files * sizeof(pack_metric_t);
	StringCopyS(it->path,sizeof(it->path),".PACK.metric");
	it->func_it(it,false);
    }


    //--- sub files

    const pack_metric_t *metric = (pack_metric_t*)(szs->data+offset_metric);
    ccp name = pack->name_pool;

    uint idx;
    for ( idx = 0; idx < n_files; idx++, metric++ )
    {
	while ( *name == '/' ) // skip leading slashes (security)
	    name++;
	StringCopyS(it->path,sizeof(it->path),name);
	name += strlen(name) + 1;

	it->index	= idx+1;
	it->fst_item	= 0;
	it->is_dir	= 0;

	it->off	= ntohl(metric->offset);
	if ( it->off >= size )
	{
	    it->off = size;
	    it->size = 0;
	}
	else
	{
	    it->size = ntohl(metric->size);
	    if ( it->size > size - it->off )
		 it->size = size - it->off;
	}

	it->func_it(it,false);
    }

    return 0;
}

///////////////////////////////////////////////////////////////////////////////

enumError CreatePACK
(
    szs_file_t		* szs,		// valid szs
    ccp			source_dir,	// NULL or path to source dir
    u8			* source_data,	// NULL or source data
    u32			total_fsize,	// total file size
    SetupParam_t	* setup_param	// NULL or setup parameters
)
{
    DASSERT(szs);
    PRINT("CreatePACK() ndir=%d, totfsize=%x, align=%x,%x\n",
		szs->subfile.used, total_fsize, opt_align_breff, opt_align_pack );

    //--- setup

    enumError max_err = ERR_OK;
    const endian_func_t	* endian = &be_func;
    szs->endian = endian;

    szs->fform_arch	= FF_PACK;
    szs->ff_attrib	= GetAttribFF(szs->fform_arch);

    SortSubFilesSZS(szs,SORT_AUTO);

    if ( logging >= 2 )
    {
	printf("\nsorted file list:\n");
	int i;
	for ( i = 0; i < szs->subfile.used; i++ )
	{
	    szs_subfile_t * f = szs->subfile.list + i;
	    printf("%3d.: %u %6x %6x %s\n", i, f->is_dir, f->offset, f->size, f->path );
	}
    }

    //--- calc segment sizes and alloc data

    u32 spool_size = 0, n_files = 0;

    int i;
    szs_subfile_t * fptr;
    for ( i = 0, fptr = szs->subfile.list; i < szs->subfile.used; i++, fptr++ )
    {
	if (!fptr->is_dir)
	{
	    n_files++;
	    spool_size += strlen(fptr->path) + 1;
	}
    }

    spool_size = ALIGN32(spool_size,PACK_METRIC_ALIGN);
    const u32 metric_offset = sizeof(pack_header_t) + spool_size;
    const u32 metric_size
	= ALIGN32( n_files * sizeof(pack_metric_t),
		opt_align_pack > PACK_METRIC_ALIGN ? opt_align_pack : PACK_METRIC_ALIGN );

    total_fsize = ALIGN32(total_fsize,opt_align_pack);
    u32 file_offset = ALIGN32(metric_offset+metric_size,opt_align_pack);

    szs->size = file_offset + total_fsize;
    szs->data = CALLOC(1,szs->size);

    noPRINT(">>> SIZE=%zu=%#zx, N=%u,%u\n",
		szs->size, szs->size, n_files, szs->subfile.used );


    //--- setup header

    pack_header_t *ph	= (pack_header_t*)szs->data;
    ph->magic		= htonl(PACK_MAGIC_NUM);
    ph->file_size	= htonl(szs->size);
    ph->n_files		= htonl(n_files);
    ph->offset_metric	= htonl(metric_offset);


    //--- copy files

    pack_metric_t *metric = (pack_metric_t*)(szs->data+metric_offset);
    char *name = ph->name_pool;
    char *name_end = (char*)metric;

    for ( i = 0, fptr = szs->subfile.list; i < szs->subfile.used; i++, fptr++ )
    {
	MARK_USED(name,name_end);
	if (!fptr->is_dir)
	{
	    name = StringCopyE(name,name_end,fptr->path);
	    name++;

	    metric->offset = htonl(file_offset);
	    metric->size   = htonl(fptr->size);
	    metric++;

	    if (fptr->data)
		memcpy(szs->data + file_offset, fptr->data, fptr->size );
	    else
	    {
		PRINT("LOAD: %x+%x %s\n",file_offset,fptr->size,fptr->path);
		enumError err = LoadFILE( source_dir, fptr->path, 0,
				szs->data + file_offset, fptr->size, 0, &szs->fatt, true );
		if ( max_err < err )
		     max_err = err;
	    }
	    file_offset += ALIGN32(fptr->size,opt_align_pack);
	}
    }

    return max_err;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    END				///////////////
///////////////////////////////////////////////////////////////////////////////