Back to index

lightning-sunbird  0.9+nobinonly
asencode.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the AppleSingle decoder.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Johnny Stenback <jst@mozilla.org>  (Original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <cstdio>
00040 #include <cstdlib>
00041 #include <cstring>
00042 #include <stdlib.h>
00043 #include <sys/types.h>
00044 #include <sys/stat.h>
00045 #include <libgen.h>
00046 #include <Carbon/Carbon.h>
00047 
00048 typedef struct ASFinderInfo
00049 {
00050   FInfo ioFlFndrInfo;     /* PBGetFileInfo() or PBGetCatInfo() */
00051   FXInfo ioFlXFndrInfo;   /* PBGetCatInfo() (HFS only) */
00052 } ASFinderInfo; /* ASFinderInfo */
00053 
00054 #define EXIT_IF_FALSE(x)                                                      \
00055   do {                                                                        \
00056     if (!(x)) {                                                               \
00057       printf("Assertion failure: %s\n"                                        \
00058              "  at %s line %d\n",                                             \
00059              #x, __FILE__, __LINE__);                                         \
00060       exit(1);                                                                \
00061     }                                                                         \
00062   } while (0)
00063 
00064 // encodes a file with data and resource forks into a single
00065 // AppleSingle encoded file..
00066 
00067 static void append_file(FILE* output, const char* input_name)
00068 {
00069   FILE* input = fopen(input_name, "rb");
00070   EXIT_IF_FALSE(input != NULL);
00071 
00072   while (1) {
00073     char buffer[4096];
00074     size_t amount = fread(buffer, 1, sizeof(buffer), input);
00075     if (amount == 0) {
00076       EXIT_IF_FALSE(feof(input) != 0);
00077       break;
00078     }
00079     fwrite(buffer, 1, amount, output);
00080   }
00081   fclose(input);
00082 }
00083 
00084 int main(int argc, char** argv)
00085 {
00086   if (argc < 3) {
00087     printf("usage: %s input output\n", argv[0]);
00088     exit(1);
00089   }
00090 
00091   const char *input_name = argv[1];
00092 
00093   struct stat input_st;
00094   if (stat(input_name, &input_st) != 0) {
00095     printf("%s: can't open file `%s'\n", argv[0], input_name);
00096     exit(2);
00097   }
00098 
00099   if ((input_st.st_mode & S_IFMT) != S_IFREG) {
00100     printf("%s: file `%s' not a regular file\n", argv[0], input_name);
00101     exit(3);
00102   }
00103 
00104   char rez_name[512];
00105   strcpy(rez_name, input_name);
00106   strcat(rez_name, "/rsrc");
00107 
00108   struct stat rez_st;
00109   EXIT_IF_FALSE(stat(rez_name, &rez_st) == 0);
00110 
00111   if (rez_st.st_size == 0) {
00112     printf("%s: no resource fork found on file `%s'\n", argv[0], argv[1]);
00113     exit(4);
00114   }
00115 
00116   FILE* output = fopen(argv[2], "wb");
00117   if (output == NULL) {
00118     printf("%s: can't open file `%s'\n", argv[0], argv[2]);
00119     exit(5);
00120   }
00121 
00122   struct header {
00123     int magic_number;
00124     int version_number;
00125     char filler[16];
00126   } header;
00127 
00128   header.magic_number = 0x00051600;
00129   header.version_number = 0x00020000;
00130 
00131   EXIT_IF_FALSE(fwrite(&header, sizeof(header), 1, output) == 1);
00132 
00133   short entry_count = 5;
00134   EXIT_IF_FALSE(fwrite(&entry_count, sizeof(entry_count), 1, output) == 1);
00135 
00136   struct entry {
00137     unsigned int id;
00138     unsigned int offset;
00139     unsigned int length;
00140   };
00141 
00142   struct dates
00143   {
00144     int create; /* file creation date/time */
00145     int modify; /* last modification date/time */
00146     int backup; /* last backup date/time */
00147     int access; /* last access date/time */
00148   } dates;
00149 
00150   char *name_buf = strdup(input_name);
00151   char *orig_name = basename(name_buf);
00152   int orig_name_len = strlen(orig_name);
00153 
00154   entry entries[entry_count];
00155 
00156   int header_end = sizeof(header) + sizeof(entry_count) + sizeof(entries);
00157 
00158   entries[0].id = 1; // data fork
00159   entries[0].offset = header_end;
00160   entries[0].length = input_st.st_size;
00161 
00162   entries[1].id = 2; // data fork
00163   entries[1].offset = entries[0].offset + entries[0].length;
00164   entries[1].length = rez_st.st_size;
00165 
00166   entries[2].id = 3; // file name
00167   entries[2].offset = entries[1].offset + entries[1].length;
00168   entries[2].length = orig_name_len;
00169 
00170   entries[3].id = 8; // file dates
00171   entries[3].offset = entries[2].offset + entries[2].length;
00172   entries[3].length = sizeof(dates);
00173 
00174   entries[4].id = 9; // finder info
00175   entries[4].offset = entries[3].offset + entries[3].length;
00176   entries[4].length = sizeof(ASFinderInfo);
00177 
00178   EXIT_IF_FALSE(fwrite(entries, sizeof(entry), entry_count, output) ==
00179                 entry_count);
00180 
00181   append_file(output, input_name);
00182   append_file(output, rez_name);
00183 
00184   EXIT_IF_FALSE(fwrite(orig_name, 1, orig_name_len, output) == orig_name_len);
00185 
00186   // Dates in an AppleSingle encoded file should be the number of
00187   // seconds since (or to) 00:00:00, January 1, 2000 UTC
00188 #define Y2K_SECONDS (946710000U)
00189 
00190   dates.create = input_st.st_ctime - Y2K_SECONDS;
00191   dates.modify = input_st.st_mtime - Y2K_SECONDS;
00192   dates.backup = 0x80000000; // earliest possible time
00193   dates.access = input_st.st_atime - Y2K_SECONDS;
00194 
00195   EXIT_IF_FALSE(fwrite(&dates, 1, sizeof(dates), output) == sizeof(dates));
00196 
00197   char abs_input_name[PATH_MAX];
00198   EXIT_IF_FALSE(realpath(input_name, abs_input_name) == abs_input_name);
00199 
00200   FSRef fsref;
00201   EXIT_IF_FALSE(FSPathMakeRef((unsigned char *)abs_input_name, &fsref, 0) == 0);
00202 
00203   FSCatalogInfo cat_info;
00204   memset(&cat_info, 0, sizeof(cat_info));
00205   EXIT_IF_FALSE(FSGetCatalogInfo(&fsref,
00206                                  kFSCatInfoGettableInfo,
00207                                  &cat_info, NULL, NULL, NULL) == 0);
00208 
00209   ASFinderInfo finder_info;
00210   memcpy(&finder_info.ioFlFndrInfo, &cat_info.finderInfo,
00211          sizeof(finder_info.ioFlFndrInfo));
00212   memcpy(&finder_info.ioFlXFndrInfo, &cat_info.extFinderInfo,
00213          sizeof(finder_info.ioFlXFndrInfo));
00214 
00215   EXIT_IF_FALSE(fwrite(&finder_info, 1, sizeof(finder_info), output) ==
00216                 sizeof(finder_info));
00217 
00218   fclose(output);
00219 
00220   return 0;
00221 }