summaryrefslogtreecommitdiffstats
path: root/utils/jz4740_tools/HXFsplit.c
blob: dbeace8a202fa64ce6832e9431bf3c18f09854a1 (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by Maurus Cuelenaere
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdbool.h>

#define VERSION "0.2"

struct header{
    char main_header[20];
    unsigned int size;
    unsigned int checksum;
    unsigned int unknown;
    char other_header[32];
};

static char* basepath(char* path)
{
    static char tmp[255];
    char *ptr, *ptr2, *ptr3;
    ptr = path;
    ptr2 = (char*)tmp;
#ifdef _WIN32
    ptr3 = strrchr(path, 0x5C);
#else
    ptr3 = strrchr(path, 0x2F);
#endif
    while((int)ptr < (int)ptr3)
    {
        *ptr2 = *ptr;
        ptr++;
        ptr2++;
    }
#ifdef _WIN32
    *ptr2 = 0x5C;
#else
    *ptr2 = 0x2F;
#endif
    ptr2++;
    *ptr2 = 0;
    return (char*)tmp;
}

#ifndef _WIN32
static void replace(char* str)
{
    char *ptr = str;
    while(*ptr != 0)
    {
        if(*ptr == 0x5C) /* \ */
            *ptr = 0x2F; /* / */
        ptr++;
    }
}
#endif

static unsigned int le2int(unsigned char* buf)
{
   unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];

   return res;
}

#ifdef _WIN32
 #define PATH_SEPARATOR '\\'
#else
 #define PATH_SEPARATOR '/'
#endif

static unsigned int __mkdir(const char *path)
{
    char opath[256];
    char *p;
    size_t len;

    strncpy(opath, path, sizeof(opath));
    len = strlen(opath);
    if(opath[len - 1] == PATH_SEPARATOR)
        opath[len - 1] = '\0';
    for(p = opath; *p; p++)
        if(*p == PATH_SEPARATOR)
        {
            *p = '\0';
            if(access(opath, F_OK))
#ifdef _WIN32
                mkdir(opath);
#else
                mkdir(opath, S_IRWXU);
#endif
            *p = PATH_SEPARATOR;
        }
    if(access(opath, F_OK))    
#ifdef _WIN32
        return mkdir(opath);
#else
        return mkdir(opath, S_IRWXU);
#endif
    else
        return -1;
}

#if 0
static bool dir_exists(const char *dir)
{
  struct stat buf;
  memset(&buf, 0, sizeof(struct stat));
  printf("start: %s\n", dir);
  char *dir_cpy = (char*)malloc(strlen(dir));
  strcpy(dir_cpy, dir);
  printf("%s\n", dir_cpy);
  int tmp = (int)dir_cpy;
  while(*dir_cpy != 0)
  {
    dir_cpy++;
    if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0)
        *dir_cpy = 0;
  }
  printf("while_done\n");
  dir_cpy = (char*)tmp;
  printf("statting %s...\n", dir_cpy);
  tmp = stat(dir_cpy, &buf);
  printf("chk_dir(%s) = %d\n", dir_cpy, tmp);
  free(dir_cpy);
  return tmp == 0;
}
#endif

static bool file_exists(const char *file)
{
  struct stat buf;
  return stat(file, &buf) == 0;
}


static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath)
{
    FILE *outfile;
    char *filename;
    unsigned int filenamesize, filesize;
    while(size > 0)
    {
        filenamesize = le2int((unsigned char*)infile);
        infile += 4;
        size -= 4;
        if(size > 0)
        {
            filename = (char*)calloc(1, filenamesize+1+strlen(outpath));
            memcpy(filename, outpath, strlen(outpath));
            memcpy(&filename[strlen(outpath)], infile, filenamesize);
#ifndef _WIN32
            replace(filename);
#endif
            infile += filenamesize + 1; /* + padding */
            size -= filenamesize + 1;
            
            filesize = le2int((unsigned char*)infile);
            infile += 4;
            size -= 4;
#if 0
            if(!dir_exists(basepath(filename)))
#endif
            {
                printf("[INFO] %s\n", basepath(filename));
                if(__mkdir(basepath(filename)) != 0)
                {
#if 0
                    fprintf(stderr, "[ERR]  Error creating directory %s\n", basepath(filename));
                    return -3;
#endif
                }
            }
            
            if(!file_exists(filename))
            {
                printf("[INFO] %s: %d bytes\n", filename, filesize);
                if((outfile = fopen(filename, "wb")) == NULL)
                {
                    fprintf(stderr, "[ERR]  Error opening file %s\n", filename);
                    return -1;
                }
                if(filesize>0)
                {
                    if(fwrite(infile, filesize, 1, outfile) != 1)
                    {
                        fclose(outfile);
                        fprintf(stderr, "[ERR]  Error writing to file %s\n", filename);
                        return -2;
                    }
                }
                fclose(outfile);
            }
            
            infile += filesize;
            size -= filesize;
        }
    }
    return 0;
}

static void print_usage(void)
{
#ifdef _WIN32
    fprintf(stderr, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n");
    fprintf(stderr, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n");
#else
    fprintf(stderr, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n");
    fprintf(stderr, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n");
#endif
}

int main(int argc, char *argv[])
{
    FILE *infile;
    struct header hdr;
    unsigned char *inbuffer;
    
    fprintf(stderr, "HXFsplit v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
    fprintf(stderr, "This is free software; see the source for copying conditions.  There is NO\n");
    fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
    
    if(argc != 3)
    {
        print_usage();
        return 1;
    }

#ifdef _WIN32
    if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "\\") != 0)
    {
        fprintf(stderr, "[ERR]  Output path must end with a \\\n");
#else
    if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0)
    {
        fprintf(stderr, "[ERR]  Output path must end with a /\n");
#endif
        return 2;
    }

    if((infile = fopen(argv[1], "rb")) == NULL)
    {
        fprintf(stderr, "[ERR]  Cannot open %s\n", argv[1]);
        return 3;
    }
    
    if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL)
    {
        fclose(infile);
        fprintf(stderr, "[ERR]  Error allocating %d bytes buffer\n", sizeof(struct header));
        return 4;
    }

    if(fread(inbuffer, sizeof(struct header), 1, infile) != 1)
    {
        fclose(infile);
        fprintf(stderr, "Cannot read header of %s\n", argv[1]);
        return 5;
    }
    
    memcpy(hdr.main_header, inbuffer, 20);
    hdr.size = le2int(&inbuffer[20]);
    hdr.checksum = le2int(&inbuffer[24]);
    hdr.unknown = le2int(&inbuffer[28]);
    memcpy(hdr.other_header, &inbuffer[32], 32);
    free(inbuffer);
    
    if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0)
    {
        fclose(infile);
        fprintf(stderr, "[ERR]  Header doesn't match\n");
        return 6;
    }
    
    if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL)
    {
        fclose(infile);
        fprintf(stderr, "[ERR]  Error allocating %d bytes buffer\n", hdr.size);
        return 7;
    }
    
    fseek(infile, sizeof(struct header), SEEK_SET);
    
    if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1)
    {
        fclose(infile);
        free(inbuffer);
        fprintf(stderr, "[ERR]  Cannot read file in buffer\n");
        return 8;
    }
    
    fclose(infile);
    
    split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]);
    
    free(inbuffer);
    
    return 0;
}