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 * Johnny Stenback <jst@mozilla.org> (Original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 * gcc -framework ApplicationServices -l stdc++ -o asencode asencode.cpp
49 #include <sys/types.h>
52 #include <Carbon/Carbon.h>
54 typedef struct ASFinderInfo
56 FInfo ioFlFndrInfo
; /* PBGetFileInfo() or PBGetCatInfo() */
57 FXInfo ioFlXFndrInfo
; /* PBGetCatInfo() (HFS only) */
58 } ASFinderInfo
; /* ASFinderInfo */
60 #define EXIT_IF_FALSE(x) \
63 printf("Assertion failure: %s\n" \
65 #x, __FILE__, __LINE__); \
70 // encodes a file with data and resource forks into a single
71 // AppleSingle encoded file..
73 static void append_file(FILE* output
, const char* input_name
)
75 FILE* input
= fopen(input_name
, "rb");
76 EXIT_IF_FALSE(input
!= NULL
);
80 size_t amount
= fread(buffer
, 1, sizeof(buffer
), input
);
82 EXIT_IF_FALSE(feof(input
) != 0);
85 fwrite(buffer
, 1, amount
, output
);
90 int main(int argc
, char** argv
)
93 printf("usage: %s input output\n", argv
[0]);
97 const char *input_name
= argv
[1];
100 if (stat(input_name
, &input_st
) != 0) {
101 printf("%s: can't open file `%s'\n", argv
[0], input_name
);
105 if ((input_st
.st_mode
& S_IFMT
) != S_IFREG
) {
106 printf("%s: file `%s' not a regular file\n", argv
[0], input_name
);
111 strcpy(rez_name
, input_name
);
112 strcat(rez_name
, "/rsrc");
115 EXIT_IF_FALSE(stat(rez_name
, &rez_st
) == 0);
117 if (rez_st
.st_size
== 0) {
118 printf("%s: no resource fork found on file `%s'\n", argv
[0], argv
[1]);
122 FILE* output
= fopen(argv
[2], "wb");
123 if (output
== NULL
) {
124 printf("%s: can't open file `%s'\n", argv
[0], argv
[2]);
134 header
.magic_number
= 0x00051600;
135 header
.version_number
= 0x00020000;
137 EXIT_IF_FALSE(fwrite(&header
, sizeof(header
), 1, output
) == 1);
139 short entry_count
= 5;
140 EXIT_IF_FALSE(fwrite(&entry_count
, sizeof(entry_count
), 1, output
) == 1);
150 int create
; /* file creation date/time */
151 int modify
; /* last modification date/time */
152 int backup
; /* last backup date/time */
153 int access
; /* last access date/time */
156 char *name_buf
= strdup(input_name
);
157 char *orig_name
= basename(name_buf
);
158 int orig_name_len
= strlen(orig_name
);
160 entry entries
[entry_count
];
162 int header_end
= sizeof(header
) + sizeof(entry_count
) + sizeof(entries
);
164 entries
[0].id
= 1; // data fork
165 entries
[0].offset
= header_end
;
166 entries
[0].length
= input_st
.st_size
;
168 entries
[1].id
= 2; // data fork
169 entries
[1].offset
= entries
[0].offset
+ entries
[0].length
;
170 entries
[1].length
= rez_st
.st_size
;
172 entries
[2].id
= 3; // file name
173 entries
[2].offset
= entries
[1].offset
+ entries
[1].length
;
174 entries
[2].length
= orig_name_len
;
176 entries
[3].id
= 8; // file dates
177 entries
[3].offset
= entries
[2].offset
+ entries
[2].length
;
178 entries
[3].length
= sizeof(dates
);
180 entries
[4].id
= 9; // finder info
181 entries
[4].offset
= entries
[3].offset
+ entries
[3].length
;
182 entries
[4].length
= sizeof(ASFinderInfo
);
184 EXIT_IF_FALSE(fwrite(entries
, sizeof(entry
), entry_count
, output
) ==
187 append_file(output
, input_name
);
188 append_file(output
, rez_name
);
190 EXIT_IF_FALSE(fwrite(orig_name
, 1, orig_name_len
, output
) == orig_name_len
);
192 // Dates in an AppleSingle encoded file should be the number of
193 // seconds since (or to) 00:00:00, January 1, 2000 UTC
194 #define Y2K_SECONDS (946710000U)
196 dates
.create
= input_st
.st_ctime
- Y2K_SECONDS
;
197 dates
.modify
= input_st
.st_mtime
- Y2K_SECONDS
;
198 dates
.backup
= 0x80000000; // earliest possible time
199 dates
.access
= input_st
.st_atime
- Y2K_SECONDS
;
201 EXIT_IF_FALSE(fwrite(&dates
, 1, sizeof(dates
), output
) == sizeof(dates
));
203 char abs_input_name
[PATH_MAX
];
204 EXIT_IF_FALSE(realpath(input_name
, abs_input_name
) == abs_input_name
);
207 EXIT_IF_FALSE(FSPathMakeRef((unsigned char *)abs_input_name
, &fsref
, 0) == 0);
209 FSCatalogInfo cat_info
;
210 memset(&cat_info
, 0, sizeof(cat_info
));
211 EXIT_IF_FALSE(FSGetCatalogInfo(&fsref
,
212 kFSCatInfoGettableInfo
,
213 &cat_info
, NULL
, NULL
, NULL
) == 0);
215 ASFinderInfo finder_info
;
216 memcpy(&finder_info
.ioFlFndrInfo
, &cat_info
.finderInfo
,
217 sizeof(finder_info
.ioFlFndrInfo
));
218 memcpy(&finder_info
.ioFlXFndrInfo
, &cat_info
.extFinderInfo
,
219 sizeof(finder_info
.ioFlXFndrInfo
));
221 EXIT_IF_FALSE(fwrite(&finder_info
, 1, sizeof(finder_info
), output
) ==
222 sizeof(finder_info
));