~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to mozilla/config/asencode.cpp

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* This Source Code Form is subject to the terms of the Mozilla Public
 
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
5
 
 
6
/*
 
7
 * To compile, do:
 
8
 *
 
9
 *   gcc -framework ApplicationServices -l stdc++ -o asencode asencode.cpp
 
10
 */
 
11
 
 
12
#include <cstdio>
 
13
#include <cstdlib>
 
14
#include <cstring>
 
15
#include <stdlib.h>
 
16
#include <sys/types.h>
 
17
#include <sys/stat.h>
 
18
#include <libgen.h>
 
19
#include <Carbon/Carbon.h>
 
20
 
 
21
typedef struct ASFinderInfo
 
22
{
 
23
  FInfo ioFlFndrInfo;     /* PBGetFileInfo() or PBGetCatInfo() */
 
24
  FXInfo ioFlXFndrInfo;   /* PBGetCatInfo() (HFS only) */
 
25
} ASFinderInfo; /* ASFinderInfo */
 
26
 
 
27
#define EXIT_IF_FALSE(x)                                                      \
 
28
  do {                                                                        \
 
29
    if (!(x)) {                                                               \
 
30
      printf("Assertion failure: %s\n"                                        \
 
31
             "  at %s line %d\n",                                             \
 
32
             #x, __FILE__, __LINE__);                                         \
 
33
      exit(1);                                                                \
 
34
    }                                                                         \
 
35
  } while (0)
 
36
 
 
37
// encodes a file with data and resource forks into a single
 
38
// AppleSingle encoded file..
 
39
 
 
40
static void append_file(FILE* output, const char* input_name)
 
41
{
 
42
  FILE* input = fopen(input_name, "rb");
 
43
  EXIT_IF_FALSE(input != NULL);
 
44
 
 
45
  while (1) {
 
46
    char buffer[4096];
 
47
    size_t amount = fread(buffer, 1, sizeof(buffer), input);
 
48
    if (amount == 0) {
 
49
      EXIT_IF_FALSE(feof(input) != 0);
 
50
      break;
 
51
    }
 
52
    fwrite(buffer, 1, amount, output);
 
53
  }
 
54
  fclose(input);
 
55
}
 
56
 
 
57
int main(int argc, char** argv)
 
58
{
 
59
  if (argc < 3) {
 
60
    printf("usage: %s input output\n", argv[0]);
 
61
    exit(1);
 
62
  }
 
63
 
 
64
  const char *input_name = argv[1];
 
65
 
 
66
  struct stat input_st;
 
67
  if (stat(input_name, &input_st) != 0) {
 
68
    printf("%s: can't open file `%s'\n", argv[0], input_name);
 
69
    exit(2);
 
70
  }
 
71
 
 
72
  if ((input_st.st_mode & S_IFMT) != S_IFREG) {
 
73
    printf("%s: file `%s' not a regular file\n", argv[0], input_name);
 
74
    exit(3);
 
75
  }
 
76
 
 
77
  char rez_name[512];
 
78
  strcpy(rez_name, input_name);
 
79
  strcat(rez_name, "/rsrc");
 
80
 
 
81
  struct stat rez_st;
 
82
  EXIT_IF_FALSE(stat(rez_name, &rez_st) == 0);
 
83
 
 
84
  if (rez_st.st_size == 0) {
 
85
    printf("%s: no resource fork found on file `%s'\n", argv[0], argv[1]);
 
86
    exit(4);
 
87
  }
 
88
 
 
89
  FILE* output = fopen(argv[2], "wb");
 
90
  if (output == NULL) {
 
91
    printf("%s: can't open file `%s'\n", argv[0], argv[2]);
 
92
    exit(5);
 
93
  }
 
94
 
 
95
  struct header {
 
96
    int magic_number;
 
97
    int version_number;
 
98
    char filler[16];
 
99
  } header;
 
100
 
 
101
  header.magic_number = 0x00051600;
 
102
  header.version_number = 0x00020000;
 
103
 
 
104
  EXIT_IF_FALSE(fwrite(&header, sizeof(header), 1, output) == 1);
 
105
 
 
106
  short entry_count = 5;
 
107
  EXIT_IF_FALSE(fwrite(&entry_count, sizeof(entry_count), 1, output) == 1);
 
108
 
 
109
  struct entry {
 
110
    unsigned int id;
 
111
    unsigned int offset;
 
112
    unsigned int length;
 
113
  };
 
114
 
 
115
  struct dates
 
116
  {
 
117
    int create; /* file creation date/time */
 
118
    int modify; /* last modification date/time */
 
119
    int backup; /* last backup date/time */
 
120
    int access; /* last access date/time */
 
121
  } dates;
 
122
 
 
123
  char *name_buf = strdup(input_name);
 
124
  char *orig_name = basename(name_buf);
 
125
  int orig_name_len = strlen(orig_name);
 
126
 
 
127
  entry entries[entry_count];
 
128
 
 
129
  int header_end = sizeof(header) + sizeof(entry_count) + sizeof(entries);
 
130
 
 
131
  entries[0].id = 1; // data fork
 
132
  entries[0].offset = header_end;
 
133
  entries[0].length = input_st.st_size;
 
134
 
 
135
  entries[1].id = 2; // data fork
 
136
  entries[1].offset = entries[0].offset + entries[0].length;
 
137
  entries[1].length = rez_st.st_size;
 
138
 
 
139
  entries[2].id = 3; // file name
 
140
  entries[2].offset = entries[1].offset + entries[1].length;
 
141
  entries[2].length = orig_name_len;
 
142
 
 
143
  entries[3].id = 8; // file dates
 
144
  entries[3].offset = entries[2].offset + entries[2].length;
 
145
  entries[3].length = sizeof(dates);
 
146
 
 
147
  entries[4].id = 9; // finder info
 
148
  entries[4].offset = entries[3].offset + entries[3].length;
 
149
  entries[4].length = sizeof(ASFinderInfo);
 
150
 
 
151
  EXIT_IF_FALSE(fwrite(entries, sizeof(entry), entry_count, output) ==
 
152
                entry_count);
 
153
 
 
154
  append_file(output, input_name);
 
155
  append_file(output, rez_name);
 
156
 
 
157
  EXIT_IF_FALSE(fwrite(orig_name, 1, orig_name_len, output) == orig_name_len);
 
158
 
 
159
  // Dates in an AppleSingle encoded file should be the number of
 
160
  // seconds since (or to) 00:00:00, January 1, 2000 UTC
 
161
#define Y2K_SECONDS (946710000U)
 
162
 
 
163
  dates.create = input_st.st_ctime - Y2K_SECONDS;
 
164
  dates.modify = input_st.st_mtime - Y2K_SECONDS;
 
165
  dates.backup = 0x80000000; // earliest possible time
 
166
  dates.access = input_st.st_atime - Y2K_SECONDS;
 
167
 
 
168
  EXIT_IF_FALSE(fwrite(&dates, 1, sizeof(dates), output) == sizeof(dates));
 
169
 
 
170
  char abs_input_name[PATH_MAX];
 
171
  EXIT_IF_FALSE(realpath(input_name, abs_input_name) == abs_input_name);
 
172
 
 
173
  FSRef fsref;
 
174
  EXIT_IF_FALSE(FSPathMakeRef((unsigned char *)abs_input_name, &fsref, 0) == 0);
 
175
 
 
176
  FSCatalogInfo cat_info;
 
177
  memset(&cat_info, 0, sizeof(cat_info));
 
178
  EXIT_IF_FALSE(FSGetCatalogInfo(&fsref,
 
179
                                 kFSCatInfoGettableInfo,
 
180
                                 &cat_info, NULL, NULL, NULL) == 0);
 
181
 
 
182
  ASFinderInfo finder_info;
 
183
  memcpy(&finder_info.ioFlFndrInfo, &cat_info.finderInfo,
 
184
         sizeof(finder_info.ioFlFndrInfo));
 
185
  memcpy(&finder_info.ioFlXFndrInfo, &cat_info.extFinderInfo,
 
186
         sizeof(finder_info.ioFlXFndrInfo));
 
187
 
 
188
  EXIT_IF_FALSE(fwrite(&finder_info, 1, sizeof(finder_info), output) ==
 
189
                sizeof(finder_info));
 
190
 
 
191
  fclose(output);
 
192
 
 
193
  return 0;
 
194
}