forked from XboxDev/extract-xiso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-xiso.c
2076 lines (1591 loc) · 72.7 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 modified
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.
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
01.11.14 twillecomme: Intégration of aiyyo's and somski's work.
Minor warn fixes.
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__ ) || defined( __OPENBSD__ )
#include <machine/limits.h>
#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 "/"
#define FORCE_ASCII 1
#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 FORCE_ASCII 1
#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 FORCE_ASCII 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( __OPENBSD__ )
#define exiso_target "openbsd"
#elif defined( _WIN32 )
#define exiso_target "win32"
#define PATH_CHAR '\\'
#define PATH_CHAR_STR "\\"
#define FORCE_ASCII 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 )
#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 )
#ifdef USE_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
#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.1 (01.11.14)"
#define VERSION_LENGTH 16
#define banner "extract-xiso v" exiso_version " for " exiso_target " - written by in <in@fishtank.com>\n"
#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 ] );
#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 DEBUG_DUMP_DIRECTORY "/Volumes/c/xbox/iso/exiso"
#define GETOPT_STRING "c:d:Dhlmp:qQrsvx"
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_list, k_rewrite } 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 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 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_warned = 0;
static bool s_remove_systemupdate = false;
static char *s_systemupdate = "$SystemUpdate";
static xoff_t s_xbox_disc_lseek = 0;
#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 extract = true, rewrite = false, free_user = false, free_pass = false, x_seen = false, delete = false, optimized;
char *cwd = nil, *path = nil, *buf = nil, *new_iso_path = nil, tag[ XISO_OPTIMIZED_TAG_LENGTH * sizeof(long) ];
if ( argc < 2 ) { usage(); exit( 1 ); }
while ( ! err && ( opt_char = getopt( argc, argv, GETOPT_STRING ) ) != -1 ) {
switch ( opt_char ) {
case 'c': {
if ( 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 'h': {
usage();
exit( 0 );
} break;
case 'l': {
if ( x_seen || rewrite || create ) {
usage();
exit( 1 );
}
extract = false;
} break;
case 'm': {
if ( x_seen || ! extract ) {
usage();
exit( 1 );
}
s_media_enable = false;
} break;
case 'q': {
s_quiet = true;
} break;
case 'Q': {
s_quiet = s_real_quiet = true;
} break;
case 'r': {
if ( x_seen || ! extract || create ) {
usage();
exit( 1 );
}
rewrite = true;
} break;
case 's': {
s_remove_systemupdate = true;
} break;
case 'v': {
printf( "%s", banner );
exit( 0 );
} break;
case 'x': {
if ( ! extract || rewrite || create ) {
usage();
exit( 1 );
}
x_seen = true;
} break;
default: {
usage();
exit( 1 );
} break;
}
}
if ( ! err ) {
if ( create ) { if ( optind < argc ) { usage(); exit( 1 ); } }
else if ( optind >= argc ) { usage(); exit( 1 ); }
exiso_log( "%s", banner );
if ( ( extract ) && ( s_copy_buffer = (char *) malloc( READWRITE_BUFFER_SIZE ) ) == nil ) mem_err();
}
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 ( ! 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 {
// 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, extract ? k_extract : k_list, nil, ! optimized );
}
}
}
if ( ! err ) exiso_log( "\n%u files in %s total %lld bytes\n", s_total_files, rewrite ? new_iso_path : argv[ i ], (long long int) 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 ) exiso_log( "\n%u files in %u xiso's total %lld bytes\n", s_total_files_all_isos, isos, (long long int) s_total_bytes_all_isos );
if ( s_warned ) exiso_log( "\nWARNING: Warning(s) were issued during execution--review stderr!\n" );
boyer_moore_done();
if ( s_copy_buffer ) free( s_copy_buffer );
if ( path ) free( path );
return err;
}
int log_err( const char *in_file, int in_line, const char *in_format, ... ) {
va_list ap;
char *format;
int ret;
#if DEBUG
asprintf( &format, "%s:%u %s", in_file, in_line, in_format );
#else
format = (char *) in_format;
#endif
if ( s_real_quiet ) ret = 0;
else {
va_start( ap, in_format );
ret = vfprintf( stderr, format, ap );
va_end( ap );
}
#if DEBUG
free( format );
#endif
return ret;
}
#if 0
#pragma mark -
#endif
int verify_xiso( int in_xiso, int32_t *out_root_dir_sector, int32_t *out_root_dir_size, char *in_iso_name ) {
int err = 0;
char buffer[ XISO_HEADER_DATA_LENGTH ];
if ( lseek( in_xiso, (xoff_t) XISO_HEADER_OFFSET, SEEK_SET ) == -1 ) seek_err();
if ( ! err && read( in_xiso, buffer, XISO_HEADER_DATA_LENGTH ) != XISO_HEADER_DATA_LENGTH ) read_err();
if ( ! err && memcmp( buffer, XISO_HEADER_DATA, XISO_HEADER_DATA_LENGTH ) )
{
if ( lseek( in_xiso, (xoff_t) XISO_HEADER_OFFSET + GLOBAL_LSEEK_OFFSET, SEEK_SET ) == -1 ) seek_err();
if ( ! err && read( in_xiso, buffer, XISO_HEADER_DATA_LENGTH ) != XISO_HEADER_DATA_LENGTH ) read_err();
if ( ! err && memcmp( buffer, XISO_HEADER_DATA, XISO_HEADER_DATA_LENGTH ) ) {
if ( lseek( in_xiso, (xoff_t) XISO_HEADER_OFFSET + XGD3_LSEEK_OFFSET, SEEK_SET ) == -1 ) seek_err();
if ( ! err && read( in_xiso, buffer, XISO_HEADER_DATA_LENGTH ) != XISO_HEADER_DATA_LENGTH ) read_err();
if ( ! err && memcmp( buffer, XISO_HEADER_DATA, XISO_HEADER_DATA_LENGTH ) ) misc_err( "%s does not appear to be a valid xbox iso image\n", in_iso_name, 0, 0 )
else s_xbox_disc_lseek = XGD3_LSEEK_OFFSET;
}
else s_xbox_disc_lseek = GLOBAL_LSEEK_OFFSET;
}
else s_xbox_disc_lseek = 0;
// read root directory information
if ( ! err && read( in_xiso, out_root_dir_sector, XISO_SECTOR_OFFSET_SIZE ) != XISO_SECTOR_OFFSET_SIZE ) read_err();
if ( ! err && read( in_xiso, out_root_dir_size, XISO_DIRTABLE_SIZE ) != XISO_DIRTABLE_SIZE ) read_err();
little32( *out_root_dir_sector );
little32( *out_root_dir_size );
// seek to header tail and verify media tag
if ( ! err && lseek( in_xiso, (xoff_t) XISO_FILETIME_SIZE + XISO_UNUSED_SIZE, SEEK_CUR ) == -1 ) seek_err();
if ( ! err && read( in_xiso, buffer, XISO_HEADER_DATA_LENGTH ) != XISO_HEADER_DATA_LENGTH ) read_err();
if ( ! err && memcmp( buffer, XISO_HEADER_DATA, XISO_HEADER_DATA_LENGTH ) ) misc_err( "%s appears to be corrupt\n", in_iso_name, 0, 0 );
// seek to root directory sector
if ( ! err ) {
if ( ! *out_root_dir_sector && ! *out_root_dir_size ) {
exiso_log( "xbox image %s contains no files.\n", in_iso_name );
err = err_iso_no_files;
} else {
if ( lseek( in_xiso, (xoff_t) *out_root_dir_sector * XISO_SECTOR_SIZE, SEEK_SET ) == -1 ) seek_err();
}
}
return err;
}
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 ) {
xoff_t pos;
dir_node_avl root;
FILE_TIME *ft = nil;
write_tree_context wt_context;
unsigned long start_sector;
int i, n, xiso = -1, err = 0;
char *cwd = nil, *buf = nil, *iso_name, *xiso_path, *iso_dir;
s_total_bytes = s_total_files = 0;
memset( &root, 0, sizeof(dir_node_avl) );
if ( ( cwd = getcwd( nil, 0 ) ) == nil ) mem_err();
if ( ! err ) {
if ( ! in_root ) {
if ( chdir( in_root_directory ) == -1 ) chdir_err( in_root_directory );
if ( ! err ) {
if ( in_root_directory[ i = (int) strlen( in_root_directory ) - 1 ] == '/' || in_root_directory[ i ] == '\\' ) in_root_directory[ i-- ] = 0;
for ( iso_dir = &in_root_directory[ i ]; iso_dir >= in_root_directory && *iso_dir != PATH_CHAR; --iso_dir ) ; ++iso_dir;
iso_name = in_name ? in_name : iso_dir;
}
} else {
iso_dir = iso_name = in_root_directory;
}
}
if ( ! err ) {
if ( ! *iso_dir ) iso_dir = PATH_CHAR_STR;
if ( ! in_output_directory ) in_output_directory = cwd;
if ( in_output_directory[ i = (int) strlen( in_output_directory ) - 1 ] == PATH_CHAR ) in_output_directory[ i-- ] = 0;
if ( ! iso_name || ! *iso_name ) iso_name = "root";
else if ( iso_name[ 1 ] == ':' ) { iso_name[ 1 ] = iso_name[ 0 ]; ++iso_name; }
#if defined( _WIN32 )
if ( ( asprintf( &xiso_path, "%s%c%s%s", *in_output_directory ? in_output_directory : cwd, PATH_CHAR, iso_name, in_name ? "" : ".iso" ) ) == -1 ) mem_err();
#else
if ( ( asprintf( &xiso_path, "%s%s%s%c%s%s", *in_output_directory == PATH_CHAR ? "" : cwd, *in_output_directory == PATH_CHAR ? "" : PATH_CHAR_STR, in_output_directory, PATH_CHAR, iso_name, in_name ? "" : ".iso" ) ) == -1 ) mem_err();
#endif
}
if ( ! err ) {
exiso_log( "%s %s%s:\n\n", in_root ? "rewriting" : "\ncreating", iso_name, in_name ? "" : ".iso" );
root.start_sector = XISO_ROOT_DIRECTORY_SECTOR;
s_total_bytes = s_total_files = 0;
if ( in_root ) {
root.subdirectory = in_root;
avl_traverse_depth_first( in_root, (traversal_callback) calculate_total_files_and_bytes, nil, k_prefix, 0 );
} else {
int i, n = 0;
exiso_log( "generating avl tree from %sfilesystem: ", "" ); flush();
err = generate_avl_tree_local( &root.subdirectory, &n );
for ( i = 0; i < n; ++i ) exiso_log( "\b" );
for ( i = 0; i < n; ++i ) exiso_log( " " );
for ( i = 0; i < n; ++i ) exiso_log( "\b" );
exiso_log( "%s\n\n", err ? "failed!" : "[OK]" );
}
}
if ( ! err && in_progress_callback ) (*in_progress_callback)( 0, s_total_bytes );
if ( ! err ) {
wt_context.final_bytes = s_total_bytes;
s_total_bytes = s_total_files = 0;
if ( root.subdirectory == EMPTY_SUBDIRECTORY ) root.start_sector = root.file_size = 0;
start_sector = root.start_sector;
avl_traverse_depth_first( &root, (traversal_callback) calculate_directory_requirements, nil, k_prefix, 0 );
avl_traverse_depth_first( &root, (traversal_callback) calculate_directory_offsets, &start_sector, k_prefix, 0 );
}
if ( ! err && ( buf = (char *) malloc( n = max( READWRITE_BUFFER_SIZE, XISO_HEADER_OFFSET ) ) ) == nil ) mem_err();
if ( ! err ) {
if ( ( xiso = open( xiso_path, WRITEFLAGS, 0644 ) ) == -1 ) open_err( xiso_path );