-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract-xiso.c
2366 lines (1834 loc) · 83.5 KB
/
extract-xiso.c
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
extract-xiso.c
An xdvdfs .iso (xbox iso) file extraction and creation tool by in <in@fishtank.com>
written March 10, 2003
View this file with your tab stops set to 4 spaces or it it will look wacky.
Regarding licensing:
I think the GPL sucks! (it stands for Generosity Poor License)
My open-source code is really *FREE* so you can do whatever you want with it,
as long as 1) you don't claim that you wrote my code and 2) you retain a notice
that some parts of the code are copyright in@fishtank.com and 3) you understand
there are no warranties. I only guarantee that it will take up disk space!
If you want to help out with this project it would be welcome, just email me at
in@fishtank.com.
This code is copyright in@fishtank.com and is licensed under a slightly modifified
version of the Berkeley Software License, which follows:
/*
* Copyright (c) 2003 in <in@fishtank.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
*
* This product includes software developed by in <in@fishtank.com>.
*
* 4. Neither the name "in" nor the email address "in@fishtank.com"
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED `AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR OR ANY CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*\
Last modified:
03.29.03 in: Fixed a path display bug, changed the tree descent algorithm
and added ftp to xbox support (rev to v1.2)
04.04.03 in: Added a counter for total number of files in xiso (rev to
v1.2.1) THIS VERSION NOT FOR RELEASE!
04.18.03 in: Added xoff_t typecasts for __u32 * __u32 manipulations.
This fixed a bug with very large iso's where the directory
table was at the end of the iso--duh! (rev to v1.3)
04.19.03 in: A user pointed out that the program is increasing its
memory usage over large iso's. I've tracked this to the buffer
allocation in extract_file() during traverse_xiso()
recursions. As a fix I've moved the copy buffer to a static
variable. Not as encapsulated as I'd like but hey, this is
C after all.
Also added support for FreeBSD (on Intel x86) (rev to v1.4)
04.21.03 in: It looks like whomever is making xiso creation tools out there
has never heard of a binary tree and is sticking *every*
directory entry off the right subnode (at least on all the iso's
I've found so far). This causes extremely deep recursion for
iso's with lots of files (and also causes these iso's, when
burned to DVD, to behave as a linked list for file lookups, thus
providing *worst case* lookup performance at all times).
Not only do I find this annoying and extremely bad programming,
I've noticed that it is causing sporadic stack overflows with
my (formerly) otherwise good tree traversal code.
In order to combat such bad implementations, I've re-implemented
the traverse_xiso() routine to get rid of any non-directory
recursion. Additionally, I've made a few extra tweaks to
conserve even more memory. I can see now that I'm going to need
to write xiso creation as well and do it right. (rev to v1.5 beta)
NOT FOR RELEASE
04.22.03 in: Making some major changes...
Working on the optimization algorithm, still needs some tweaks
apparently. DO NOT RELEASE THIS SOURCE BUILD!
NOTE: I'm building this as 1.5 beta and sending the source to
Emil only, this source is not yet for release.
04.28.03 in: I've finally decided that optimizing in-place just *isn't* going
to happen. The xbox is *really* picky about how its b-trees
are laid out. I've noticed that it will read the directory if
I lay the entries out in prefix order. Seems kind of weird to
me that it would *have* to be that way but whatever. So, I guess
I'll write xiso creation and then piggyback a rewrite type op
on top of it. Not quite as nice since it means you need extra
disk space but such is life.
05.01.03 in: Well it looks like I got the creation code working tonight, what
a pain in the ass *that* was. I've been working on it in my free
time (which is almost non-existent) for a week now, bleh. Also
decided to implement rewriting xisos and I think I'll add build
xiso from ftp-server, just to be *really* lazy. I guess this
means I'll have to read the stat code in the ftp tree. Hmmm,
probably need to dig around in there anyway... A user reported
that newer builds of evox are barfing with ftp upload so I guess
I'll go debug that.
Also cleaned up the code quite a bit tonight just for posterity.
I'd just like to point out that I *know* I'm being really lazy with
all these big-ass functions and no header files and such. The fact
is I just can't seem to bring myself to care woohaahaa!
(rev to 2.0 beta) NOT FOR RELEASE until I get the other goodies
written ;)
05.03.03 in: Added support for create xiso from ftp server. Still need to debug
evox to see what the problem is-- looks like something to do for
tomorrow!
05.06.03 in: Finally got back to this little project ;0 -- the ftp bug was that
FtpWriteBlock() in the libftp kit was timing out on occasion and returning
less than a complete buffer. So I fixed that and some other little
bugs here and there, plus I changed the handling of the create mode
so that you can now specify an iso name. Hopefully that's a bit more
intuitive.
05.10.03 in: Fixed a lot of stuff by now, I think it's solid for 2.0 release.
(rev to 2.0, release)
05.13.03 in: Oops, fixed a bug in main() passing an (essentially) nil pointer to
create_xiso that was causing a core dump and cleaned up the avl_fetch()
and avl_insert() routines. (rev to 2.1, release)
05.14.03 in: Added media check fix, fixed a bug in the ftp library where FtpStat was
failing on filenames with spaces in them.
06.16.03 in: Based on code from zeek, I added support for win32, minus ftp
functionality. Neither he nor I have the time to port the ftp library
to windows right now, but at least the creation code will work. Big thanks
to zeek for taking the time to wade through the source and figure out
what needed to be tweaked for the windows build.
06.20.03 in: Well I just couldn't release the windows build without ftp support (can
you say OCD <g> ;-), anyway I sat down today and ported the ftp library
to win32. That was a major pain let me tell you as I don't have a decent
PC to run windows on (all my decent PC's run linux) and I've never really
programmed anything on Windows. Who'd have known that I couldn't just use
fdopen() to convert a socket descriptor to a FILE *! Anyway, whining aside
I think it all works the way it's supposed to. I'm sure once I throw it on
the PC community I'll have plenty of bug reports, but such is life. I also
fixed a few other minor glitches here and there that gcc never complained
about but that vc++ didn't like.
07.15.03 in: Fixed a bug first reported by Metal Maniac (thanks) where the path string was
being generated improperly during xiso creation on windows. Special thanks to
Hydra for submitting code that mostly fixed the problem, I needed to make a few
more tweaks but nothing much. Hopefully this will solve the problem. Also,
thanks to Huge for doing a Win32 GUI around extract-xiso 2.2! Rev to 2.3, release.
07.16.03 in: Changed some of the help text, looks like I introduced a copy-paste
bug somewhere along the line. Oops.
07.28.03 in: Added support for progress updating to create_xiso, now just pass in
a pointer to a progress_callback routine (see typedef below). Also added
support on darwin for burning the iso to cd/dvd. For some reason right now
everything works fine if I try to burn an image to a DVD, but if I try to
insert a cd it dies. I have no idea as of yet what's wrong. I am strongly
considering *not* opensourcing my cd-burning stuff once I get it working
as I can think of a few commercial uses for it. Have to mull that one
over a bit more. This version only for release to UI developers.
12.02.03 in: Fixed a few bugs in the ftp subsystem and increased the read-write buffer size
to 2Mb. That should help ftp performance quite a bit.
10.29.04 in: Well, it's been a looooong time since I've worked on this little program...
I've always been irritated by the fact that extract-xiso would never create an
iso that could be auto-detected by CD/DVD burning software. To burn iso's I've
always had to go in and select a manual sector size of 2048 bytes, etc. What
a pain! As a result, I've been trying to get my hands on the Yellow Book for
ages. I never did manage that as I didn't want to pay for it but I did some
research the other day and came across the ECMA-119 specification. It lays
out the exact volume format that I needed to use. Hooray! Now xiso's are
autodetected and burned properly by burning software...
If you try to follow what I've done and find the write_volume_descriptors()
method cryptic, just go download the ecma-119 specification from the ecma
website. Read about primary volume descriptors and it'll make sense.
Bleh! This code is ugly ;-)
10.25.05 in: Added in patch from Nordman. Thanks.
Added in security patch from Chris Bainbridge. Thanks.
Fixed a few minor bugs.
01.18.10 aiyyo: XBox 360 iso extraction supported by Aiyyo.
10.04.10 aiyyo: Added new command line switch (-s skip $SystemUpdate folder).
Display file progress in percent.
Try to create destination directory.
10.11.10 aiyyo: Fix -l bug (empty list).
05.02.11 aiyyo: Remove security patch.
09.30.11 somski: Added XGD3 support
enjoy!
in
*/
#if defined( __LINUX__ )
#define _LARGEFILE64_SOURCE
#endif
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#if defined( __FREEBSD__ )
#include <machine/limits.h>
#endif
#if ! defined( NO_FTP )
#define libftp_client
#include "libftp-5.0.1.modified.by.in/FtpLibrary.h"
#else
typedef struct FTP_STAT { char type; unsigned long size; char *name; void *next; } FTP_STAT;
#define FTP void
#define STATUS int
#define FtpBye( a ) do { } while ( 0 )
#define FtpClose( a ) do { } while ( 0 )
#define FtpBinary( a ) do { } while ( 0 )
#define FtpChdir( a, b ) do { } while ( 0 )
#define FtpMkdir( a, b ) do { } while ( 0 )
#define FtpStatFree( a ) do { } while ( 0 )
#define FtpStat( a, b, c ) do { } while ( 0 )
#define FtpOpenRead( a, b ) do { } while ( 0 )
#define FtpOpenWrite( a, b ) do { } while ( 0 )
#define FtpReadBlock( a, b, c ) do { } while ( 0 )
#define FtpWriteBlock( a, b, c ) do { } while ( 0 )
#define FtpLogin( a, b, c, d, e ) do { } while ( 0 )
#endif
#if defined( _WIN32 )
#include <direct.h>
#include "win32/dirent.c"
#else
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
#endif
#if defined( __DARWIN__ )
#define exiso_target "macos-x"
#define PATH_CHAR '/'
#define PATH_CHAR_STR "/"
// I'm not planning on distributing the cd-burning code on darwin, so BURN_ENABLED is 0
#define BURN_ENABLED 0
#define READFLAGS O_RDONLY
#define WRITEFLAGS O_WRONLY | O_CREAT | O_TRUNC
#define READWRITEFLAGS O_RDWR
typedef off_t xoff_t;
#elif defined( __FREEBSD__ )
#define exiso_target "freebsd"
#define PATH_CHAR '/'
#define PATH_CHAR_STR "/"
#define BURN_ENABLED 0
#define READFLAGS O_RDONLY
#define WRITEFLAGS O_WRONLY | O_CREAT | O_TRUNC
#define READWRITEFLAGS O_RDWR
typedef off_t xoff_t;
#elif defined( __LINUX__ )
#define exiso_target "linux"
#define PATH_CHAR '/'
#define PATH_CHAR_STR "/"
#define BURN_ENABLED 0
#define READFLAGS O_RDONLY | O_LARGEFILE
#define WRITEFLAGS O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE
#define READWRITEFLAGS O_RDWR | O_LARGEFILE
#define lseek lseek64
#define stat stat64
typedef __off64_t xoff_t;
#elif defined( _WIN32 )
#define exiso_target "win32"
#define PATH_CHAR '\\'
#define PATH_CHAR_STR "\\"
#define BURN_ENABLED 0
#define READFLAGS O_RDONLY | O_BINARY
#define WRITEFLAGS O_WRONLY | O_CREAT | O_TRUNC | O_BINARY
#define READWRITEFLAGS O_RDWR | O_BINARY
#define S_ISDIR( x ) ( ( x ) & _S_IFDIR )
#define S_ISREG( x ) ( ( x ) & _S_IFREG )
#define ULONG_MAX 0xfffffffful
#include "win32/getopt.c"
#include "win32/asprintf.c"
#define lseek _lseeki64
#define mkdir( a, b ) mkdir( a )
typedef __int32 int32_t;
typedef __int64 xoff_t;
#else
#error unknown target, cannot compile!
#endif
#define swap16( n ) ( ( n ) = ( n ) << 8 | ( n ) >> 8 )
#define swap32( n ) ( ( n ) = ( n ) << 24 | ( n ) << 8 & 0xff0000 | ( n ) >> 8 & 0xff00 | ( n ) >> 24 )
//#if BYTE_ORDER == BIG_ENDIAN
// #define big16( n )
// #define big32( n )
// #define little16( n ) swap16( n )
// #define little32( n ) swap32( n )
//#else
#define big16( n ) swap16( n )
#define big32( n ) swap32( n )
#define little16( n )
#define little32( n )
//#endif
#if BURN_ENABLED
#define BURN_OPTION_CHAR "b"
#define BURN_OPTION_TEXT " -b Burn xiso image to disc.\n"
#else
#define BURN_OPTION_CHAR ""
#define BURN_OPTION_TEXT ""
enum { err_burn_aborted = -5004 };
#endif
#define DEBUG_VERIFY_XISO 0
#define DEBUG_OPTIMIZE_XISO 0
#define DEBUG_TRAVERSE_XISO_DIR 0
#ifndef DEBUG
#define DEBUG 0
#endif
#if ! defined( __cplusplus ) && ! defined( bool )
typedef int bool;
enum { false, true };
#endif
#ifndef nil
#define nil 0
#endif
#define exiso_version "2.7.0 (09.30.11)"
#define VERSION_LENGTH 16
#define banner "extract-xiso v" exiso_version " for " exiso_target " - written by in <in@fishtank.com>\n modified by Aiyyo & somski\n"
#if ! defined( NO_FTP )
#define usage() fprintf( stderr, \
"%s\n\
Usage:\n\
\n\
%s [options] [-[" BURN_OPTION_CHAR "lrx]] <file1.xiso> [file2.xiso] ...\n\
%s [options] -c <dir> [name] [-c <dir> [name]] ...\n\
\n\
Mutually exclusive modes:\n\
\n\
" BURN_OPTION_TEXT "\
-c <dir> [name] Create xiso from file(s) starting in (local or remote)\n\
<dir>. If the [name] parameter is specified, the\n\
xiso will be created with the (path and) name given,\n\
otherwise the xiso will be created in the current\n\
directory with the name <dir>.iso. The -c option\n\
may be specified multiple times to create multiple\n\
xiso images.\n\
-l List files in xiso(s).\n\
-r Rewrite xiso(s) as optimized xiso(s).\n\
-x Extract xiso(s) (the default mode if none is given).\n\
If no directory is specified with -d, a directory\n\
with the name of the xiso (minus the .iso portion)\n\
will be created in the current directory and the\n\
xiso will be expanded there.\n\
\n\
Options:\n\
\n\
-d <directory> In extract mode, expand xiso in <directory>.\n\
In rewrite mode, rewrite xiso in <directory>.\n\
This option is required when extracting to an ftp\n\
server.\n\
-D In rewrite mode, delete old xiso after processing.\n\
-h Print this help text and exit.\n\
-f <ftp_server> In create or extract mode, use <ftp_server> instead of\n\
the local filesystem.\n\
-m In create or rewrite mode, disable automatic .xbe\n\
media enable patching (not recommended).\n\
-p <password> Ftp password (defaults to \"xbox\")\n\
-q Run quiet (suppress all non-error output).\n\
-Q Run silent (suppress all output).\n\
-s Skip $SystemUpdate folder.\n\
-u <user name> Ftp user name (defaults to \"xbox\")\n\
-v Print version information and exit.\n\
", banner, argv[ 0 ], argv[ 0 ] );
#else
#define usage() fprintf( stderr, \
"%s\n\
Usage:\n\
\n\
%s [options] [-[lrx]] <file1.xiso> [file2.xiso] ...\n\
%s [options] -c <dir> [name] [-c <dir> [name]] ...\n\
\n\
Mutually exclusive modes:\n\
\n\
-c <dir> [name] Create xiso from file(s) starting in <dir>. If the\n\
[name] parameter is specified, the xiso will be\n\
created with the (path and) name given, otherwise\n\
the xiso will be created in the current directory\n\
with the name <dir>.iso. The -c option may be\n\
specified multiple times to create multiple xiso\n\
images.\n\
-l List files in xiso(s).\n\
-r Rewrite xiso(s) as optimized xiso(s).\n\
-x Extract xiso(s) (the default mode if none is given).\n\
If no directory is specified with -d, a directory\n\
with the name of the xiso (minus the .iso portion)\n\
will be created in the current directory and the\n\
xiso will be expanded there.\n\
\n\
Options:\n\
\n\
-d <directory> In extract mode, expand xiso in <directory>.\n\
In rewrite mode, rewrite xiso in <directory>.\n\
-D In rewrite mode, delete old xiso after processing.\n\
-h Print this help text and exit.\n\
-m In create or rewrite mode, disable automatic .xbe\n\
media enable patching (not recommended).\n\
-q Run quiet (suppress all non-error output).\n\
-Q Run silent (suppress all output).\n\
-s Skip $SystemUpdate folder.\n\
-v Print version information and exit.\n\
", banner, argv[ 0 ], argv[ 0 ] );
#endif
#define exiso_log if ( ! s_quiet ) printf
#define flush() if ( ! s_quiet ) fflush( stdout )
#define mem_err() { log_err( __FILE__, __LINE__, "out of memory error\n" ); err = 1; }
#define read_err() { log_err( __FILE__, __LINE__, "read error: %s\n", strerror( errno ) ); err = 1; }
#define seek_err() { log_err( __FILE__, __LINE__, "seek error: %s\n", strerror( errno ) ); err = 1; }
#define write_err() { log_err( __FILE__, __LINE__, "write error: %s\n", strerror( errno ) ); err = 1; }
#define rread_err() { log_err( __FILE__, __LINE__, "unable to read remote file\n" ); err = 1; }
#define rwrite_err() { log_err( __FILE__, __LINE__, "unable to write to remote file\n" ); err = 1; }
#define unknown_err() { log_err( __FILE__, __LINE__, "an unrecoverable error has occurred\n" ); err = 1; }
#define open_err( in_file ) { log_err( __FILE__, __LINE__, "open error: %s %s\n", ( in_file ), strerror( errno ) ); err = 1; }
#define chdir_err( in_dir ) { log_err( __FILE__, __LINE__, "unable to change to directory %s: %s\n", ( in_dir ), strerror( errno ) ); err = 1; }
#define mkdir_err( in_dir ) { log_err( __FILE__, __LINE__, "unable to create directory %s: %s\n", ( in_dir ), strerror( errno ) ); err = 1; }
#define ropen_err( in_file ) { log_err( __FILE__, __LINE__, "unable to open remote file %s\n", ( in_file ) ); err = 1; }
#define rchdir_err( in_dir ) { log_err( __FILE__, __LINE__, "unable to change to remote directory %s\n", ( in_dir ) ); err = 1; }
#define rmkdir_err( in_dir ) { log_err( __FILE__, __LINE__, "unable to create remote directory %s\n", ( in_dir ) ); err = 1; }
#define misc_err( in_format, a, b, c ) { log_err( __FILE__, __LINE__, ( in_format ), ( a ), ( b ), ( c ) ); err = 1; }
#ifndef min
#define min( a , b ) ( ( a ) < ( b ) ? ( a ) : ( b ) )
#define max( a , b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
#endif
#define GLOBAL_LSEEK_OFFSET 0xFD90000ul
#define XGD3_LSEEK_OFFSET 0x2080000ul
#define n_sectors( size ) ( ( size ) / XISO_SECTOR_SIZE + ( ( size ) % XISO_SECTOR_SIZE ? 1 : 0 ) )
#define XISO_HEADER_DATA "MICROSOFT*XBOX*MEDIA"
#define XISO_HEADER_DATA_LENGTH 20
#define XISO_HEADER_OFFSET 0x10000
#define XISO_FILE_MODULUS 0x10000
#define XISO_ROOT_DIRECTORY_SECTOR 0x108
#define XISO_OPTIMIZED_TAG_OFFSET 31337
#define XISO_OPTIMIZED_TAG "in!xiso!" exiso_version
#define XISO_OPTIMIZED_TAG_LENGTH ( 8 + VERSION_LENGTH )
#define XISO_OPTIMIZED_TAG_LENGTH_MIN 7
#define XISO_ATTRIBUTES_SIZE 1
#define XISO_FILENAME_LENGTH_SIZE 1
#define XISO_TABLE_OFFSET_SIZE 2
#define XISO_SECTOR_OFFSET_SIZE 4
#define XISO_DIRTABLE_SIZE 4
#define XISO_FILESIZE_SIZE 4
#define XISO_DWORD_SIZE 4
#define XISO_FILETIME_SIZE 8
#define XISO_SECTOR_SIZE 2048
#define XISO_UNUSED_SIZE 0x7c8
#define XISO_FILENAME_OFFSET 14
#define XISO_FILENAME_LENGTH_OFFSET ( XISO_FILENAME_OFFSET - 1 )
#define XISO_FILENAME_MAX_CHARS 255
#define XISO_ATTRIBUTE_RO 0x01
#define XISO_ATTRIBUTE_HID 0x02
#define XISO_ATTRIBUTE_SYS 0x04
#define XISO_ATTRIBUTE_DIR 0x10
#define XISO_ATTRIBUTE_ARC 0x20
#define XISO_ATTRIBUTE_NOR 0x80
#define XISO_PAD_BYTE 0xff
#define XISO_PAD_SHORT 0xffff
#define XISO_MEDIA_ENABLE "\xe8\xca\xfd\xff\xff\x85\xc0\x7d"
#define XISO_MEDIA_ENABLE_BYTE '\xeb'
#define XISO_MEDIA_ENABLE_LENGTH 8
#define XISO_MEDIA_ENABLE_BYTE_POS 7
#define EMPTY_SUBDIRECTORY ( (dir_node_avl *) 1 )
#define READWRITE_BUFFER_SIZE 0x00200000
#define FTP_DEFAULT_USERPASS "xbox"
#define DEBUG_DUMP_DIRECTORY "/Volumes/c/xbox/iso/exiso"
#if ! defined( NO_FTP )
#define GETOPT_STRING BURN_OPTION_CHAR "c:d:Df:hlmp:qQrsu:vx"
#else
#define GETOPT_STRING BURN_OPTION_CHAR "c:d:Dhlmp:qQrsvx"
#endif
typedef enum avl_skew { k_no_skew , k_left_skew , k_right_skew } avl_skew;
typedef enum avl_result { no_err, k_avl_error, k_avl_balanced } avl_result;
typedef enum avl_traversal_method { k_prefix, k_infix, k_postfix } avl_traversal_method;
typedef enum bm_constants { k_default_alphabet_size = 256 } bm_constants;
typedef enum modes { k_generate_avl, k_extract, k_upload, k_list, k_rewrite, k_burn } modes;
typedef enum errors { err_end_of_sector = -5001, err_iso_rewritten = -5002, err_iso_no_files = -5003 } errors;
typedef void (*progress_callback)( xoff_t in_current_value, xoff_t in_final_value );
typedef int (*traversal_callback)( void *in_node, void *in_context, long in_depth );
typedef struct dir_node dir_node;
typedef struct create_list create_list;
typedef struct dir_node_avl dir_node_avl;
struct dir_node {
dir_node *left;
dir_node *parent;
dir_node_avl *avl_node;
char *filename;
unsigned short r_offset;
unsigned char attributes;
unsigned char filename_length;
unsigned long file_size;
unsigned long start_sector;
};
struct dir_node_avl {
unsigned long offset;
xoff_t dir_start;
char *filename;
unsigned long file_size;
unsigned long start_sector;
dir_node_avl *subdirectory;
unsigned long old_start_sector;
avl_skew skew;
dir_node_avl *left;
dir_node_avl *right;
};
struct create_list {
char *path;
char *name;
create_list *next;
};
typedef struct FILE_TIME {
unsigned long l;
unsigned long h;
} FILE_TIME;
typedef struct wdsafp_context {
xoff_t dir_start;
unsigned long *current_sector;
} wdsafp_context;
typedef struct write_tree_context {
int xiso;
char *path;
int from;
progress_callback progress;
xoff_t final_bytes;
} write_tree_context;
int log_err( const char *in_file, int in_line, const char *in_format, ... );
void avl_rotate_left( dir_node_avl **in_root );
void avl_rotate_right( dir_node_avl **in_root );
int avl_compare_key( char *in_lhs, char *in_rhs );
avl_result avl_left_grown( dir_node_avl **in_root );
avl_result avl_right_grown( dir_node_avl **in_root );
dir_node_avl *avl_fetch( dir_node_avl *in_root, char *in_filename );
avl_result avl_insert( dir_node_avl **in_root, dir_node_avl *in_node );
int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback in_callback, void *in_context, avl_traversal_method in_method, long in_depth );
void boyer_moore_done();
char *boyer_moore_search( char *in_text, long in_text_len );
int boyer_moore_init( char *in_pattern, long in_pat_len, long in_alphabet_size );
int free_dir_node_avl( void *in_dir_node_avl, void *, long );
int extract_file( int in_xiso, dir_node *in_file, modes in_mode, char *path );
int open_ftp_connection( char *in_host, char *in_user, char *in_password, FTP **out_ftp );
int decode_xiso( char *in_xiso, char *in_path, modes in_mode, char **out_iso_path, bool in_ll_compat );
int verify_xiso( int in_xiso, int32_t *out_root_dir_sector, int32_t *out_root_dir_size, char *in_iso_name );
int traverse_xiso( int in_xiso, dir_node *in_dir_node, xoff_t in_dir_start, char *in_path, modes in_mode, dir_node_avl **in_root, bool in_ll_compat );
int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_avl *in_root, int in_xiso, char **out_iso_path, char *in_name, progress_callback in_progress_callback );
FILE_TIME *alloc_filetime_now( void );
int generate_avl_tree_local( dir_node_avl **out_root, int *io_n );
int generate_avl_tree_remote( dir_node_avl **out_root, int *io_n );
int write_directory( dir_node_avl *in_avl, int in_xiso, int in_depth );
int write_file( dir_node_avl *in_avl, write_tree_context *in_context, int in_depth );
int write_tree( dir_node_avl *in_avl, write_tree_context *in_context, int in_depth );
int calculate_total_files_and_bytes( dir_node_avl *in_avl, void *in_context, int in_depth );
int calculate_directory_size( dir_node_avl *in_avl, unsigned long *out_size, long in_depth );
int calculate_directory_requirements( dir_node_avl *in_avl, void *in_context, int in_depth );
int calculate_directory_offsets( dir_node_avl *in_avl, unsigned long *io_context, int in_depth );
int write_dir_start_and_file_positions( dir_node_avl *in_avl, wdsafp_context *io_context, int in_depth );
int write_volume_descriptors( int in_xiso, unsigned long in_total_sectors );
#if DEBUG
void write_sector( int in_xiso, xoff_t in_start, char *in_name, char *in_extension );
#endif
static long s_pat_len;
static FTP *s_ftp = nil;
static bool s_quiet = false;
static char *s_pattern = nil;
static long *s_gs_table = nil;
static long *s_bc_table = nil;
static xoff_t s_total_bytes = 0;
static int s_total_files = 0;
static char *s_copy_buffer = nil;
static bool s_real_quiet = false;
static bool s_media_enable = true;
static xoff_t s_total_bytes_all_isos = 0;
static int s_total_files_all_isos = 0;
static bool s_remove_systemupdate = false;
static char *s_systemupdate = "$SystemUpdate";
static xoff_t s_xbox_disc_lseek = 0;
#if BURN_ENABLED
#if defined( __DARWIN__ )
#include "darwin/burn.c"
#endif
#endif
#if 0 // #pragma mark - inserts a spacer in the function popup of certain text editors (i.e. mine ;-)
#pragma mark -
#endif
int main( int argc, char **argv ) {
struct stat sb;
create_list *create = nil, *p, *q, **r;
int i, fd, opt_char, err = 0, isos = 0;
bool burn = false, extract = true, rewrite = false, free_user = false, free_pass = false, x_seen = false, delete = false, optimized;
char *cwd = nil, *server = nil, *pass, *path = nil, *user, *buf = nil, *new_iso_path = nil, tag[ XISO_OPTIMIZED_TAG_LENGTH * sizeof(long) ];
if ( argc < 2 ) { usage(); exit( 1 ); }
user = pass = FTP_DEFAULT_USERPASS;
while ( ! err && ( opt_char = getopt( argc, argv, GETOPT_STRING ) ) != -1 ) {
switch ( opt_char ) {
case 'b': {
if ( x_seen || rewrite || ! extract || create ) {
usage();
exit( 1 );
}
burn = true;
} break;
case 'c': {
if ( burn || x_seen || rewrite || ! extract ) {
usage();
exit( 1 );
}
for ( r = &create; *r != nil; r = &(*r)->next ) ;
if ( ( *r = (create_list *) malloc( sizeof(create_list) ) ) == nil ) mem_err();
if ( ! err ) {
(*r)->name = nil;
(*r)->next = nil;
if ( ( (*r)->path = strdup( optarg ) ) == nil ) mem_err();
}
if ( ! err && argv[ optind ] && *argv[ optind ] != '-' && *argv[ optind ] && ( (*r)->name = strdup( argv[ optind++ ] ) ) == nil ) mem_err();
} break;
case 'd': {
if ( path ) free( path );
if ( ( path = strdup( optarg ) ) == nil ) mem_err();
} break;
case 'D': {
delete = true;
} break;
case 'f': {
if ( server ) free( server );
if ( ( server = strdup( optarg ) ) == nil ) mem_err();
} break;
case 'h': {
usage();
exit( 0 );
} break;
case 'l': {
if ( burn || x_seen || rewrite || create ) {
usage();
exit( 1 );
}
extract = false;
} break;
case 'm': {
if ( burn || x_seen || ! extract ) {
usage();
exit( 1 );
}
s_media_enable = false;
} break;
case 'p': {
if ( pass && free_pass ) free( pass );
if ( ( pass = strdup( optarg ) ) == nil ) mem_err();
free_pass = true;
} break;
case 'q': {
s_quiet = true;
} break;
case 'Q': {
s_quiet = s_real_quiet = true;
} break;
case 'r': {
if ( burn || x_seen || ! extract || create ) {
usage();
exit( 1 );
}
rewrite = true;
} break;
case 's': {
s_remove_systemupdate = true;
} break;
case 'u': {
if ( user && free_user ) free( user );
if ( ( user = strdup( optarg ) ) == nil ) mem_err();
free_user = true;
} break;
case 'v': {
printf( "%s", banner );
exit( 0 );
} break;
case 'x': {
if ( burn || ! extract || rewrite || create ) {
usage();
exit( 1 );
}
x_seen = true;
} break;
default: {
usage();
exit( 1 );
} break;
}
}
if ( ! err ) {
if ( ( ! extract || rewrite ) && server ) { free( server ); server = nil; }
if ( create ) { if ( optind < argc ) { usage(); exit( 1 ); } }
else if ( optind >= argc || server && ! path ) { usage(); exit( 1 ); }
exiso_log( "%s", banner );
if ( ( server || extract ) && ( s_copy_buffer = (char *) malloc( READWRITE_BUFFER_SIZE ) ) == nil ) mem_err();
}
if ( ! err && server ) {
#if defined( _WIN32 )
WSADATA ws_data;
WORD ws_version_requested = MAKEWORD( 2, 0 );
if ( ( err = WSAStartup( ws_version_requested, &ws_data ) ) || LOBYTE( ws_data.wVersion ) != 2 || HIBYTE( ws_data.wVersion ) != 0 ) misc_err( "unable to initialize winsock v2 dll, aborting ftp operation\n", 0, 0, 0 );
#endif
if ( ! err ) err = open_ftp_connection( server, user, pass, &s_ftp );
}
if ( ! err && ( create || rewrite ) ) err = boyer_moore_init( XISO_MEDIA_ENABLE, XISO_MEDIA_ENABLE_LENGTH, k_default_alphabet_size );
if ( ! err && create ) {
for ( p = create; ! err && p != nil; ) {
char *tmp = nil;
if ( p->name ) {
for ( i = (int) strlen( p->name ); i >= 0 && p->name[ i ] != PATH_CHAR; --i ) ; ++i;
if ( i ) {
if ( ( tmp = (char *) malloc( i + 1 ) ) == nil ) mem_err();
if ( ! err ) {
strncpy( tmp, p->name, i );
tmp[ i ] = 0;
}
}
}
if ( ! err ) err = create_xiso( p->path, tmp, nil, -1, nil, p->name ? p->name + i : nil, nil );
if ( tmp ) free( tmp );
q = p->next;
if ( p->name ) free( p->name );
free( p->path );
free( p );
p = q;
}
} else for ( i = optind; ! err && i < argc; ++i ) {
++isos;
exiso_log( "\n" );
s_total_bytes = s_total_files = 0;
if ( server && path ) {
char *tmp;
if ( ( tmp = strdup( path ) ) == NULL ) { mem_err(); }
else if ( tmp[ 1 ] == ':' ) {
int i, n;
tmp[ 1 ] = tmp[ 0 ];
tmp[ 0 ] = '/';
for ( i = 2, n = (int) strlen( tmp ); i < n; ++i ) if ( tmp[ i ] == '\\' ) tmp[ i ] = '/';
}
if ( ! err && FtpChdir( s_ftp, tmp ) < 0 ) rchdir_err( tmp );
if ( tmp ) free( tmp );
}
if ( ! err ) {
optimized = false;
if ( ( fd = open( argv[ i ], READFLAGS, 0 ) ) == -1 ) open_err( argv[ i ] );
if ( ! err && lseek( fd, (xoff_t) XISO_OPTIMIZED_TAG_OFFSET, SEEK_SET ) == -1 ) seek_err();
if ( ! err && read( fd, tag, XISO_OPTIMIZED_TAG_LENGTH ) != XISO_OPTIMIZED_TAG_LENGTH ) read_err();
if ( fd != -1 ) close( fd );
if ( ! err ) {
tag[ XISO_OPTIMIZED_TAG_LENGTH ] = 0;
if ( ! strncmp( tag, XISO_OPTIMIZED_TAG, XISO_OPTIMIZED_TAG_LENGTH_MIN ) ) optimized = true;
if ( rewrite ) {
if ( optimized ) {
exiso_log( "%s is already optimized, skipping...\n", argv[ i ] );
continue;
}
if ( ! err && ( buf = (char *) malloc( strlen( argv[ i ] ) + 5 ) ) == nil ) mem_err(); // + 5 magic number is for ".old\0"
if ( ! err ) {
sprintf( buf, "%s.old", argv[ i ] );
if ( stat( buf, &sb ) != -1 ) misc_err( "%s already exists, cannot rewrite %s\n", buf, argv[ i ], 0 );
if ( ! err && rename( argv[ i ], buf ) == -1 ) misc_err( "cannot rename %s to %s\n", argv[ i ], buf, 0 );
if ( err ) { err = 0; free( buf ); continue; }
}
if ( ! err ) err = decode_xiso( buf, path, k_rewrite, &new_iso_path, true );
if ( ! err && delete && unlink( buf ) == -1 ) log_err( __FILE__, __LINE__, "unable to delete %s\n", buf );
if ( buf ) free( buf );
} else {
if ( burn && ! optimized ) {
if ( s_quiet ) misc_err( "refusing to burn a non-optimized xiso in quiet mode\n", 0, 0, 0 );
if ( ! err ) {
exiso_log( "%s is not optimized!!\n", argv[ i ] );
exiso_log( "... you should rewrite it with the -r option before you burn it.\n" );
exiso_log( "\ncontinue burn [y/N]? " );
*tag = 0;
if ( *fgets( tag, sizeof(tag), stdin ) != 'y' && *tag != 'Y' ) err = err_burn_aborted;
}
}
// the order of the mutually exclusive options here is important, the extract ? k_extract : k_list test *must* be the final comparison
if ( ! err ) err = decode_xiso( argv[ i ], path, server ? k_upload : burn ? k_burn : extract ? k_extract : k_list, nil, ! optimized );
if ( burn && err == err_burn_aborted ) exiso_log( "burn aborted.\n" );
}
}
}
if ( ! err && ! burn ) exiso_log( "\n%u files in %s total %llu bytes\n", s_total_files, rewrite ? new_iso_path : argv[ i ], s_total_bytes );
if ( new_iso_path ) {
if ( ! err ) exiso_log( "\n%s successfully rewritten%s%s\n", argv[ i ], path ? " as " : ".", path ? new_iso_path : "" );
free( new_iso_path );
new_iso_path = nil;
}
if ( err == err_iso_no_files ) err = 0;
}
if ( ! err && isos > 1 && ! burn ) exiso_log( "\n%u files in %u xiso's total %llu bytes\n", s_total_files_all_isos, isos, s_total_bytes_all_isos );
if ( s_ftp ) FtpBye( s_ftp );
boyer_moore_done();