1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the AppleSingle decoder.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 2002
20
* the Initial Developer. All Rights Reserved.
23
* Patrick Beard <beard@netscape.com> (Original author)
24
* Brian Ryner <bryner@brianryner.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the MPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the MPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
45
#define EXIT_IF_FALSE(x) \
48
printf("Assertion failure: %s\n" \
50
#x, __FILE__, __LINE__); \
55
// decodes a file into data and resource forks.
57
static int read_int(FILE* f)
60
EXIT_IF_FALSE(fread(&result, sizeof(result), 1, f) == 1);
64
static void copy_range(FILE* input, size_t offset, size_t length,
65
const char* output_name)
67
FILE* output = fopen(output_name, "wb");
68
EXIT_IF_FALSE(output != NULL);
69
fseek(input, offset, SEEK_SET);
72
size_t amount = (length > sizeof(buffer) ? sizeof(buffer) : length);
73
EXIT_IF_FALSE(fread(buffer, 1, amount, input) == amount);
74
fwrite(buffer, 1, amount, output);
80
int main(int argc, char** argv)
83
printf("usage: %s input output\n", argv[0]);
87
FILE* input = fopen(argv[1], "rb");
89
printf("%s: can't open file `%s'\n", argv[0], argv[1]);
99
EXIT_IF_FALSE(fread(&header, sizeof(header), 1, input) == 1);
100
EXIT_IF_FALSE(header.magic_number == 0x00051600);
101
EXIT_IF_FALSE(header.version_number <= 0x00020000);
102
// printf("sizeof(header) == %d\n", sizeof(header));
105
EXIT_IF_FALSE(fread(&entry_count, sizeof(entry_count), 1, input) == 1);
113
entry* entries = new entry[entry_count];
114
EXIT_IF_FALSE(fread(entries, sizeof(entry), entry_count, input) == entry_count);
116
entry* data_entry = NULL;
117
entry* rez_entry = NULL;
119
for (int i = 0; i < entry_count; i++) {
120
entry& entry = entries[i];
132
const char* data_name = argv[2];
133
if (data_entry && data_entry->length) {
134
copy_range(input, data_entry->offset, data_entry->length, data_name);
136
// always create the data fork, even if the file doesn't have one.
137
FILE* tmp = fopen(data_name, "wb");
142
if (rez_entry && rez_entry->length) {
144
strcpy(rez_name, data_name);
145
strcat(rez_name, "/rsrc");
146
copy_range(input, rez_entry->offset, rez_entry->length, rez_name);