~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/config/asdecode.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

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
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
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/
 
9
 *
 
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.
 
14
 *
 
15
 * The Original Code is the AppleSingle decoder.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *  Patrick Beard <beard@netscape.com>  (Original author)
 
24
 *  Brian Ryner <bryner@brianryner.com>
 
25
 *
 
26
 * Alternatively, the contents of this file may be used under the terms of
 
27
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
28
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
29
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
30
 * of those above. If you wish to allow use of your version of this file only
 
31
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
32
 * use your version of this file under the terms of the MPL, indicate your
 
33
 * decision by deleting the provisions above and replace them with the notice
 
34
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
35
 * the provisions above, a recipient may use your version of this file under
 
36
 * the terms of any one of the MPL, the GPL or the LGPL.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
#include <cstdio>
 
41
#include <cstdlib>
 
42
#include <cstring>
 
43
#include <stdlib.h>
 
44
 
 
45
#define EXIT_IF_FALSE(x)                                                      \
 
46
  do {                                                                        \
 
47
    if (!(x)) {                                                               \
 
48
      printf("Assertion failure: %s\n"                                        \
 
49
             "  at %s line %d\n",                                             \
 
50
             #x, __FILE__, __LINE__);                                         \
 
51
      exit(1);                                                                \
 
52
    }                                                                         \
 
53
  } while (0)
 
54
 
 
55
// decodes a file into data and resource forks.
 
56
 
 
57
static int read_int(FILE* f)
 
58
{
 
59
  int result;
 
60
  EXIT_IF_FALSE(fread(&result, sizeof(result), 1, f) == 1);
 
61
  return result;
 
62
}
 
63
 
 
64
static void copy_range(FILE* input, size_t offset, size_t length,
 
65
                       const char* output_name)
 
66
{
 
67
  FILE* output = fopen(output_name, "wb");
 
68
  EXIT_IF_FALSE(output != NULL);
 
69
  fseek(input, offset, SEEK_SET);
 
70
  while (length != 0) {
 
71
    char buffer[4096];
 
72
    size_t amount = (length > sizeof(buffer) ? sizeof(buffer) : length);
 
73
    EXIT_IF_FALSE(fread(buffer, 1, amount, input) == amount);
 
74
    fwrite(buffer, 1, amount, output);
 
75
    length -= amount;
 
76
  }
 
77
  fclose(output);
 
78
}
 
79
 
 
80
int main(int argc, char** argv)
 
81
{
 
82
  if (argc < 3) {
 
83
    printf("usage:  %s input output\n", argv[0]);
 
84
    exit(1);
 
85
  }
 
86
 
 
87
  FILE* input = fopen(argv[1], "rb");
 
88
  if (input == NULL) {
 
89
    printf("%s: can't open file `%s'\n", argv[0], argv[1]);
 
90
    exit(2);
 
91
  }
 
92
 
 
93
  struct header {
 
94
    int magic_number;
 
95
    int version_number;
 
96
    char filler[16];
 
97
  } header;
 
98
 
 
99
  EXIT_IF_FALSE(fread(&header, sizeof(header), 1, input) == 1);
 
100
  EXIT_IF_FALSE(header.magic_number == 0x00051600);
 
101
  EXIT_IF_FALSE(header.version_number <= 0x00020000);
 
102
  // printf("sizeof(header) == %d\n", sizeof(header));
 
103
 
 
104
  short entry_count;
 
105
  EXIT_IF_FALSE(fread(&entry_count, sizeof(entry_count), 1, input) == 1);
 
106
 
 
107
  struct entry {
 
108
    unsigned int id;
 
109
    unsigned int offset;
 
110
    unsigned int length;
 
111
  };
 
112
 
 
113
  entry* entries = new entry[entry_count];
 
114
  EXIT_IF_FALSE(fread(entries, sizeof(entry), entry_count, input) == entry_count);
 
115
 
 
116
  entry* data_entry = NULL;
 
117
  entry* rez_entry = NULL;
 
118
 
 
119
  for (int i = 0; i < entry_count; i++) {
 
120
    entry& entry = entries[i];
 
121
    switch (entry.id) {
 
122
    case 1:
 
123
      // data fork.
 
124
      data_entry = &entry;
 
125
      break;
 
126
    case 2:
 
127
      rez_entry = &entry;
 
128
      break;
 
129
    }
 
130
  }
 
131
 
 
132
  const char* data_name = argv[2];
 
133
  if (data_entry && data_entry->length) {
 
134
    copy_range(input, data_entry->offset, data_entry->length, data_name);
 
135
  } else {
 
136
    // always create the data fork, even if the file doesn't have one.
 
137
    FILE* tmp = fopen(data_name, "wb");
 
138
    EXIT_IF_FALSE(tmp);
 
139
    fclose(tmp);
 
140
  }
 
141
 
 
142
  if (rez_entry && rez_entry->length) {
 
143
    char rez_name[512];
 
144
    strcpy(rez_name, data_name);
 
145
    strcat(rez_name, "/rsrc");
 
146
    copy_range(input, rez_entry->offset, rez_entry->length, rez_name);
 
147
  }
 
148
 
 
149
  delete[] entries;
 
150
 
 
151
  fclose(input);
 
152
 
 
153
  return 0;
 
154
}