Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / config / asencode.cpp
blob98f1f010b9032d5463f71e2bd43383126fdbf6c4
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
40 * To compile, do:
42 * gcc -framework ApplicationServices -l stdc++ -o asencode asencode.cpp
45 #include <cstdio>
46 #include <cstdlib>
47 #include <cstring>
48 #include <stdlib.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <libgen.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) \
61 do { \
62 if (!(x)) { \
63 printf("Assertion failure: %s\n" \
64 " at %s line %d\n", \
65 #x, __FILE__, __LINE__); \
66 exit(1); \
67 } \
68 } while (0)
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);
78 while (1) {
79 char buffer[4096];
80 size_t amount = fread(buffer, 1, sizeof(buffer), input);
81 if (amount == 0) {
82 EXIT_IF_FALSE(feof(input) != 0);
83 break;
85 fwrite(buffer, 1, amount, output);
87 fclose(input);
90 int main(int argc, char** argv)
92 if (argc < 3) {
93 printf("usage: %s input output\n", argv[0]);
94 exit(1);
97 const char *input_name = argv[1];
99 struct stat input_st;
100 if (stat(input_name, &input_st) != 0) {
101 printf("%s: can't open file `%s'\n", argv[0], input_name);
102 exit(2);
105 if ((input_st.st_mode & S_IFMT) != S_IFREG) {
106 printf("%s: file `%s' not a regular file\n", argv[0], input_name);
107 exit(3);
110 char rez_name[512];
111 strcpy(rez_name, input_name);
112 strcat(rez_name, "/rsrc");
114 struct stat rez_st;
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]);
119 exit(4);
122 FILE* output = fopen(argv[2], "wb");
123 if (output == NULL) {
124 printf("%s: can't open file `%s'\n", argv[0], argv[2]);
125 exit(5);
128 struct header {
129 int magic_number;
130 int version_number;
131 char filler[16];
132 } header;
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);
142 struct entry {
143 unsigned int id;
144 unsigned int offset;
145 unsigned int length;
148 struct dates
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 */
154 } dates;
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) ==
185 entry_count);
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);
206 FSRef fsref;
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));
224 fclose(output);
226 return 0;