File: tomtom.c

package info (click to toggle)
orange 0.2-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,044 kB
  • ctags: 299
  • sloc: sh: 7,824; ansic: 2,419; makefile: 83
file content (302 lines) | stat: -rw-r--r-- 7,149 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* $Id: tomtom.c,v 1.3 2003/09/12 14:39:37 twogood Exp $ */
#define _BSD_SOURCE 1
#include "liborange_internal.h"
#include <synce_log.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define VERBOSE 0

typedef struct
{
  unsigned offset;
  unsigned size;
  unsigned filename_size;
  unsigned unknown3;
  unsigned unknown4;
  char* filename;
} FileEntry;

/*
   .apk files used by TomTom products
 */

static uint8_t orange_read_byte(FILE* input_file)/*{{{*/
{
  uint8_t byte;
  if (sizeof(byte) != fread(&byte, 1, sizeof(byte), input_file))
    byte = 0;
#if VERBOSE
  fprintf(stderr, "%02x ", byte);
#endif
  return byte;
}/*}}}*/

static uint32_t orange_read32(FILE* input_file)
{
  return orange_read_byte(input_file) |
    (orange_read_byte(input_file) << 8) |
    (orange_read_byte(input_file) << 16) |
    (orange_read_byte(input_file) << 24);
}

static bool orange_write_byte(FILE* output_file, uint8_t byte)/*{{{*/
{
  return sizeof(byte) == fwrite(&byte, 1, sizeof(byte), output_file);
}/*}}}*/

static void ugly_copy(FILE* output_file, size_t offset, size_t size)/*{{{*/
{
  uint8_t* buffer = malloc(size);
  size_t bytes_copied;

#if VERBOSE
  fprintf(stderr, "Copy %08x bytes from offset %08x to offset %08lx\n",
      size, offset, ftell(output_file));
#endif

/*  fflush(output_file);*/
  fseek(output_file, offset, SEEK_SET);
  
  bytes_copied = fread(buffer, 1, size, output_file);
  
  fseek(output_file, 0, SEEK_END);

  if (size != bytes_copied)
  {
    fprintf(stderr, "Copy %08x bytes from offset %08x to offset %08lx failed\n",
        (int) size, (int) offset, ftell(output_file));
    abort(); 
  }
  
  
  bytes_copied = fwrite(buffer, 1, size, output_file);
  assert(size == bytes_copied);
}/*}}}*/

bool orange_extract_apk(/*{{{*/
    const char* input_filename,
    const char* output_directory)
{
  bool success = false;
  FILE* input_file = fopen(input_filename, "r");
  FILE* output_file = NULL;
  size_t uncompressed_size;
  uint8_t magic_byte;
  uint8_t current_byte;
  size_t bytes_written = 0;
  char output_filename[256];
  const char* basename;
  char* p;

  if (!input_file)
    goto exit;

  basename = strrchr(input_filename, '/');
  if (basename)
    basename++;
  else
    basename = input_filename;

  snprintf(output_filename, sizeof(output_filename), "%s/%s", output_directory, basename);

  p = strrchr(output_filename, '.');
  if (p && p > strrchr(output_filename, '/'))
    strcat(p, ".arh");

  output_file = fopen(output_filename, "w+");
  if (!output_file)
    goto exit;

  if (orange_read_byte(input_file) != 'A' ||
      orange_read_byte(input_file) != 'R' ||
      orange_read_byte(input_file) != 'P' ||
      orange_read_byte(input_file) != 'K')
    goto exit;

  uncompressed_size = 
    orange_read_byte(input_file) |
    (orange_read_byte(input_file) << 8) |
    (orange_read_byte(input_file) << 16) |
    (orange_read_byte(input_file) << 24);

  synce_trace("ARPK signature found");

  synce_trace("uncompressed size: %08x (%i)", uncompressed_size, uncompressed_size);

  magic_byte = orange_read_byte(input_file);

#if VERBOSE
  fprintf(stderr, "Block start\n");
#endif

  while (bytes_written < uncompressed_size)
  {
    unsigned count;

    current_byte = orange_read_byte(input_file);
  
    if (magic_byte == current_byte)
    {
      unsigned offset;

#if VERBOSE
      fprintf(stderr, "Block stop (offset %08lx)\n", ftell(output_file));
#endif

      current_byte = orange_read_byte(input_file);

      if (magic_byte == current_byte)
      {
        count = 1;
      }
      else if (current_byte <= 9)
      {
        size_t offset_bytes = current_byte % 5;
        size_t size_bytes   = current_byte / 5;

        offset = orange_read_byte(input_file);

        if (offset_bytes > 1)
          offset |= orange_read_byte(input_file) << 8;
        if (offset_bytes > 2)
          offset |= orange_read_byte(input_file) << 16;
        if (offset_bytes > 3)
          offset |= orange_read_byte(input_file) << 24;
         
        count = orange_read_byte(input_file);
        
        if (size_bytes > 0)
          count |= orange_read_byte(input_file) << 8;
        if (size_bytes > 1)
          abort();
        
        ugly_copy(output_file, offset, count);
        bytes_written += count;
        count = 0;
      }
      else
      {
        count = current_byte - 5;
        current_byte = orange_read_byte(input_file);
#if VERBOSE
        fprintf(stderr, "Byte %02x repeated %02x times at offset %08lx\n", current_byte, count, ftell(output_file));
#endif
      }
    }
    else
    {
      count = 1;
    }

    while (count--)
    {
      orange_write_byte(output_file, current_byte);
      bytes_written++;
    }
  } /* for() */

  success = (bytes_written == uncompressed_size);

  if (success)
    synce_trace("Wrote '%s'", output_filename);

exit:
  if (!success && output_file)  
    unlink(output_filename);
  FCLOSE(input_file);
  FCLOSE(output_file);
  return success;
}/*}}}*/

bool orange_extract_arh(/*{{{*/
    const char* input_filename,
    const char* output_directory)
{
  bool success = false;
  FILE* input_file = fopen(input_filename, "r");
  size_t uncompressed_size;
  unsigned count = 0;
  unsigned i;
  FileEntry* entries = NULL;
  char* buffer = NULL;
  size_t buffer_size = 0;
  
  if (!input_file)
    goto exit;

  if (orange_read_byte(input_file) != 'T' ||
      orange_read_byte(input_file) != 'O' ||
      orange_read_byte(input_file) != 'M' ||
      orange_read_byte(input_file) != 'A')
    goto exit;

  synce_trace("Found TOMA signature");

  uncompressed_size = orange_read32(input_file);
  count = orange_read32(input_file);

  entries = calloc(count, sizeof(FileEntry));
  
  for (i = 0; i < count; i++)
  {
    entries[i].offset         = orange_read32(input_file);
    entries[i].size           = orange_read32(input_file);
    entries[i].filename_size  = orange_read32(input_file);
    entries[i].unknown3       = orange_read32(input_file);
    entries[i].unknown4       = orange_read32(input_file);
  }

  synce_trace("File list offset: %08lx", ftell(input_file));

  for (i = 0; i < count; i++)
  {
    char* p;
    
    entries[i].filename = malloc(entries[i].filename_size);
    if (entries[i].filename_size != fread(entries[i].filename, 1, entries[i].filename_size, input_file))
      goto exit;

    for (p = entries[i].filename; *p; p++)
      if (*p == '\\')
        *p = '/';
  }

  for (i = 0; i < count; i++)
  {
    fseek(input_file, entries[i].offset, SEEK_SET);

    if (buffer_size < entries[i].size)
    {
      buffer_size = entries[i].size;
      buffer = realloc(buffer, buffer_size);
      if (!buffer)
        goto exit;
    }

    if (entries[i].size != fread(buffer, 1, entries[i].size, input_file))
    {
      goto exit;
    }

    synce_trace("Writing '%s'", entries[i].filename);
    orange_write(buffer, entries[i].size, output_directory, entries[i].filename);
  }

  success = true;

exit:
  FREE(buffer);
  if (entries)
  {
    for (i = 0; i < count; i++)
    {
      FREE(entries[i].filename);
    }
    free(entries);
  }
  FCLOSE(input_file);
  return success;
}