Back to index

enigmail  1.4.3
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 /*
00040  * To compile, do:
00041  *
00042  *   gcc -framework ApplicationServices -l stdc++ -o asencode asencode.cpp
00043  */
00044 
00045 #include <cstdio>
00046 #include <cstdlib>
00047 #include <cstring>
00048 #include <stdlib.h>
00049 #include <sys/types.h>
00050 #include <sys/stat.h>
00051 #include <libgen.h>
00052 #include <Carbon/Carbon.h>
00053 
00054 typedef struct ASFinderInfo
00055 {
00056   FInfo ioFlFndrInfo;     /* PBGetFileInfo() or PBGetCatInfo() */
00057   FXInfo ioFlXFndrInfo;   /* PBGetCatInfo() (HFS only) */
00058 } ASFinderInfo; /* ASFinderInfo */
00059 
00060 #define EXIT_IF_FALSE(x)                                                      \
00061   do {                                                                        \
00062     if (!(x)) {                                                               \
00063       printf("Assertion failure: %s\n"                                        \
00064              "  at %s line %d\n",                                             \
00065              #x, __FILE__, __LINE__);                                         \
00066       exit(1);                                                                \
00067     }                                                                         \
00068   } while (0)
00069 
00070 // encodes a file with data and resource forks into a single
00071 // AppleSingle encoded file..
00072 
00073 static void append_file(FILE* output, const char* input_name)
00074 {
00075   FILE* input = fopen(input_name, "rb");
00076   EXIT_IF_FALSE(input != NULL);
00077 
00078   while (1) {
00079     char buffer[4096];
00080     size_t amount = fread(buffer, 1, sizeof(buffer), input);
00081     if (amount == 0) {
00082       EXIT_IF_FALSE(feof(input) != 0);
00083       break;
00084     }
00085     fwrite(buffer, 1, amount, output);
00086   }
00087   fclose(input);
00088 }
00089 
00090 int main(int argc, char** argv)
00091 {
00092   if (argc < 3) {
00093     printf("usage: %s input output\n", argv[0]);
00094     exit(1);
00095   }
00096 
00097   const char *input_name = argv[1];
00098 
00099   struct stat input_st;
00100   if (stat(input_name, &input_st) != 0) {
00101     printf("%s: can't open file `%s'\n", argv[0], input_name);
00102     exit(2);
00103   }
00104 
00105   if ((input_st.st_mode & S_IFMT) != S_IFREG) {
00106     printf("%s: file `%s' not a regular file\n", argv[0], input_name);
00107     exit(3);
00108   }
00109 
00110   char rez_name[512];
00111   strcpy(rez_name, input_name);
00112   strcat(rez_name, "/rsrc");
00113 
00114   struct stat rez_st;
00115   EXIT_IF_FALSE(stat(rez_name, &rez_st) == 0);
00116 
00117   if (rez_st.st_size == 0) {
00118     printf("%s: no resource fork found on file `%s'\n", argv[0], argv[1]);
00119     exit(4);
00120   }
00121 
00122   FILE* output = fopen(argv[2], "wb");
00123   if (output == NULL) {
00124     printf("%s: can't open file `%s'\n", argv[0], argv[2]);
00125     exit(5);
00126   }
00127 
00128   struct header {
00129     int magic_number;
00130     int version_number;
00131     char filler[16];
00132   } header;
00133 
00134   header.magic_number = 0x00051600;
00135   header.version_number = 0x00020000;
00136 
00137   EXIT_IF_FALSE(fwrite(&header, sizeof(header), 1, output) == 1);
00138 
00139   short entry_count = 5;
00140   EXIT_IF_FALSE(fwrite(&entry_count, sizeof(entry_count), 1, output) == 1);
00141 
00142   struct entry {
00143     unsigned int id;
00144     unsigned int offset;
00145     unsigned int length;
00146   };
00147 
00148   struct dates
00149   {
00150     int create; /* file creation date/time */
00151     int modify; /* last modification date/time */
00152     int backup; /* last backup date/time */
00153     int access; /* last access date/time */
00154   } dates;
00155 
00156   char *name_buf = strdup(input_name);
00157   char *orig_name = basename(name_buf);
00158   int orig_name_len = strlen(orig_name);
00159 
00160   entry entries[entry_count];
00161 
00162   int header_end = sizeof(header) + sizeof(entry_count) + sizeof(entries);
00163 
00164   entries[0].id = 1; // data fork
00165   entries[0].offset = header_end;
00166   entries[0].length = input_st.st_size;
00167 
00168   entries[1].id = 2; // data fork
00169   entries[1].offset = entries[0].offset + entries[0].length;
00170   entries[1].length = rez_st.st_size;
00171 
00172   entries[2].id = 3; // file name
00173   entries[2].offset = entries[1].offset + entries[1].length;
00174   entries[2].length = orig_name_len;
00175 
00176   entries[3].id = 8; // file dates
00177   entries[3].offset = entries[2].offset + entries[2].length;
00178   entries[3].length = sizeof(dates);
00179 
00180   entries[4].id = 9; // finder info
00181   entries[4].offset = entries[3].offset + entries[3].length;
00182   entries[4].length = sizeof(ASFinderInfo);
00183 
00184   EXIT_IF_FALSE(fwrite(entries, sizeof(entry), entry_count, output) ==
00185                 entry_count);
00186 
00187   append_file(output, input_name);
00188   append_file(output, rez_name);
00189 
00190   EXIT_IF_FALSE(fwrite(orig_name, 1, orig_name_len, output) == orig_name_len);
00191 
00192   // Dates in an AppleSingle encoded file should be the number of
00193   // seconds since (or to) 00:00:00, January 1, 2000 UTC
00194 #define Y2K_SECONDS (946710000U)
00195 
00196   dates.create = input_st.st_ctime - Y2K_SECONDS;
00197   dates.modify = input_st.st_mtime - Y2K_SECONDS;
00198   dates.backup = 0x80000000; // earliest possible time
00199   dates.access = input_st.st_atime - Y2K_SECONDS;
00200 
00201   EXIT_IF_FALSE(fwrite(&dates, 1, sizeof(dates), output) == sizeof(dates));
00202 
00203   char abs_input_name[PATH_MAX];
00204   EXIT_IF_FALSE(realpath(input_name, abs_input_name) == abs_input_name);
00205 
00206   FSRef fsref;
00207   EXIT_IF_FALSE(FSPathMakeRef((unsigned char *)abs_input_name, &fsref, 0) == 0);
00208 
00209   FSCatalogInfo cat_info;
00210   memset(&cat_info, 0, sizeof(cat_info));
00211   EXIT_IF_FALSE(FSGetCatalogInfo(&fsref,
00212                                  kFSCatInfoGettableInfo,
00213                                  &cat_info, NULL, NULL, NULL) == 0);
00214 
00215   ASFinderInfo finder_info;
00216   memcpy(&finder_info.ioFlFndrInfo, &cat_info.finderInfo,
00217          sizeof(finder_info.ioFlFndrInfo));
00218   memcpy(&finder_info.ioFlXFndrInfo, &cat_info.extFinderInfo,
00219          sizeof(finder_info.ioFlXFndrInfo));
00220 
00221   EXIT_IF_FALSE(fwrite(&finder_info, 1, sizeof(finder_info), output) ==
00222                 sizeof(finder_info));
00223 
00224   fclose(output);
00225 
00226   return 0;
00227 }