This repository was archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbin2iso.c
More file actions
1058 lines (921 loc) · 34.1 KB
/
bin2iso.c
File metadata and controls
1058 lines (921 loc) · 34.1 KB
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
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 1999 Bob Doiron, 2020 - 2025 Michael Ortmann */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __DOS__
#include <io.h>
#define ftruncate chsize
#else
#include <unistd.h>
#endif
#define DEBUG 0
#define CHECK 0 /* don't bother checking bin for validity... */
//----------------Wave Stuff---------------------/
typedef struct wavHdr {
char riff[4];
uint32_t bytestoend;
char wavetxt[4];
char fmttxt[4];
uint32_t formatsize; // 16 byte format specifier
uint16_t format; // Windows PCM
uint16_t channels; // 2 channels
uint32_t samplerate; // 44,100 Samples/sec
uint32_t avgbyterate; // 176,400 Bytes/sec
uint16_t samplebytes; // 4 bytes/sample
uint16_t channelbits; // 16 bits/channel
char datatxt[4];
uint32_t blocksize;
} tWavHead;
#define HEADBYTES 36
#define WINDOWS_PCM 0x0001
//-------------------------------------------------/
/* G L O B A L D E F I N E S */
#define SIZERAW 2352
#define SIZEISO_MODE1 2048
#define SIZEISO_MODE2_RAW 2352
#define SIZEISO_MODE2_FORM1 2048
#define SIZEISO_MODE2_FORM2 2336
#define AUDIO 0
#define MODE1 1
#define MODE2 2
#define MODE1_2352 10
#define MODE2_2352 20
#define MODE1_2048 30
#define MODE2_2336 40
#define RAWDATA FF // using this for leftover data when truncating for non-overburn
#define PROG_INTERVAL 1024
#define UNKNOWN -1
#define OFFSET 150
// got this from easycd pro by looking at a blank disk so it may be off...
#define CD74_MAX_SECTORS 334873 // 653.75 Mb
static unsigned long int Index(char m, char s, char f)
{
unsigned long int temp;
temp = (((m>>4)*10) + (m&0xf)) * 60;
temp = ( temp + (((s>>4)*10) + (s&0xf))) * 75;
temp = temp + (((f>>4)*10) + (f&0xf));
// printf("\n%d%d %d%d %d%d = %06d", m>>4, m&f, s>>4, s&f, f>>4, f&f, temp);
return temp;
}
static void unIndex(unsigned long int index, char *ptr)
{
char m, s, f;
f = (char) (index % 75);
s = (char) ((index/75) % 60);
m = (char) (index/(75*60));
sprintf(ptr, "%d%d:%d%d:%d%d", m/10, m%10, s/10, s%10, f/10, f%10);
}
// global variables
FILE* fdBinFile;
FILE* fdCueFile;
FILE* fdOutFile;
char sBinFilename[256];
char sOutFilename[256];
unsigned long int writepos = 0; // for inplace conversions...
#ifdef __DOS__
#define OUTBUF_SIZE 4*1024*4
#define INBUF_SIZE 4*1024*4
#else
#define OUTBUF_SIZE 4*1024*1024
#define INBUF_SIZE 4*1024*1024
#endif
unsigned char OUTBUF[OUTBUF_SIZE];
unsigned int OUTBUF_IDX = 0;
unsigned char INBUF[INBUF_SIZE];
unsigned int INBUF_RIDX = 0;
unsigned int INBUF_WIDX = 0;
unsigned char buf[SIZERAW + 100];
int mode2to1 = 0;
typedef struct track
{
unsigned short mode;
unsigned long idx0;
unsigned long idx1;
char num[3];
char name[80];
unsigned long offset0;
unsigned long offset1;
unsigned long size; /* track size in bytes */
} tTrack;
static int buffered_fread(unsigned char *array, unsigned int size) {
unsigned int i;
if(INBUF_WIDX == 0) {
INBUF_WIDX += fread( INBUF, 1, (INBUF_SIZE/size)*size, fdBinFile );
}
if(INBUF_WIDX == 0) return 0; // read failed.
for(i = 0; i< size; i++)
{
array[i] = INBUF[INBUF_RIDX++];
if((INBUF_RIDX == INBUF_WIDX) && (i < (size -1))) {
printf(" Warning: Premature EOF\n");
while(i++ < size)
array[i] = 0; /* zero fill the rest */
break;
}
}
if(INBUF_RIDX == INBUF_WIDX) {
INBUF_RIDX = 0;
INBUF_WIDX = 0;
}
return 1; // read passed
}
static void buffered_fwrite(unsigned char *array, unsigned int size) {
unsigned int idx;
unsigned long int readpos;
if(OUTBUF_IDX+size >= OUTBUF_SIZE) {
if(fdOutFile == fdBinFile) {
readpos = ftell(fdOutFile);
if(0 != fseek(fdOutFile, writepos, SEEK_SET)) {
perror("\nbin2iso(fseek)"); exit(1);
}
}
// printf("\nWriting \n");
if( 1 != fwrite( OUTBUF, OUTBUF_IDX, 1, fdOutFile )) {
perror("\nbin2iso(fwrite)");
fclose(fdOutFile);
// remove(sOutFilename);
exit(1);
}
if( 1 != fwrite( array, size, 1, fdOutFile )) {
perror("\nbin2iso(fwrite)");
fclose(fdOutFile);
// remove(sOutFilename);
exit(1);
}
// printf("\nWrote %d bytes \n", OUTBUF_IDX+size);
OUTBUF_IDX = 0;
if(fdOutFile == fdBinFile) {
writepos = ftell(fdOutFile);
if(0 != fseek(fdOutFile, readpos, SEEK_SET)) {
perror("\nbin2iso(fseek)"); exit(1);
}
}
} else {
for(idx = 0; idx < size; idx++) {
OUTBUF[OUTBUF_IDX + idx] = array[idx];
}
OUTBUF_IDX+=size;
}
}
static void flush_buffers(void)
{
unsigned long int readpos;
if(fdOutFile == fdBinFile) {
readpos = ftell(fdOutFile);
if(0 != fseek(fdOutFile, writepos, SEEK_SET)) {
perror("\nbin2iso(fseek)"); exit(1);
}
}
if( 1 != fwrite( OUTBUF, OUTBUF_IDX, 1, fdOutFile )) {
perror("\nbin2iso(fwrite)");
fclose(fdOutFile);
// remove(sOutFilename);
exit(1);
}
// printf("\nWrote %d bytes \n", OUTBUF_IDX);
OUTBUF_IDX = 0;
INBUF_RIDX = 0;
INBUF_WIDX = 0;
if(fdOutFile == fdBinFile) {
writepos = ftell(fdOutFile);
if(0 != fseek(fdOutFile, readpos, SEEK_SET)) {
perror("\nbin2iso(fseek)"); exit(1);
}
}
}
// presumes Line is preloaded with the "current" line of the file
static void getTrackinfo(char *Line, tTrack *track)
{
// char tnum[3];
char inum[3];
char min;
char sec;
char block;
track->idx0 = -1;
track->idx1 = -1;
// Get the 'mode'
if (strncmp(&Line[2], "TRACK ", 6)==0)
{
if (Line[8] < '0' || Line[8] > '9' || Line[9] < '0' || Line[9] > '9') {
printf("Error: Track # is not a 2 digit number\n");
exit(1);
}
strncpy(track->num, &Line[8], 2); track->num[2] = '\0';
track->mode = UNKNOWN;
if(strncmp(&Line[11], "AUDIO", 5)==0) track->mode = AUDIO;
if(strncmp(&Line[11], "MODE1/2352", 10)==0) track->mode = MODE1_2352;
if(strncmp(&Line[11], "MODE1/2048", 10)==0) track->mode = MODE1_2048;
if(strncmp(&Line[11], "MODE2/2352", 10)==0) track->mode = MODE2_2352;
if(strncmp(&Line[11], "MODE2/2336", 10)==0) track->mode = MODE2_2336;
}
else
{
printf("Error: 2nd line does not begin with ' TRACK '\n");
exit(1);
}
if( (track->mode != MODE1_2352) &&
(track->mode != MODE1_2048) &&
(track->mode != MODE2_2352) &&
(track->mode != MODE2_2336) &&
(track->mode != AUDIO) ) {
printf("Track %s Unsupported mode\n", track->num);
return;
}
// Get the track indexes
while(1) {
if(! fgets( Line, 256, fdCueFile ) ) { break; }
if (strncmp(&Line[2], "TRACK ", 6)==0)
{
break; // next track starting
}
if (strncmp(&Line[4], "INDEX ", 6)==0)
{
strncpy(inum, &Line[10], 2); inum[2] = '\0';
min = ((Line[13]-'0')<<4) | (Line[14]-'0');
sec = ((Line[16]-'0')<<4) | (Line[17]-'0');
block = ((Line[19]-'0')<<4) | (Line[20]-'0');
if(strcmp(inum, "00")==0) track->idx0 = Index(min, sec, block);
else if(strcmp(inum, "01")==0) track->idx1 = Index(min, sec, block);
else { printf("Unexpected Index number: %s\n", inum); exit(1); }
}
else if (strncmp(&Line[4], "PREGAP ", 7)==0) { ; /* ignore, handled below */ }
else if (strncmp(&Line[4], "FLAGS ", 6)==0) { ; /* ignore */ }
else { printf("Unexpected cuefile line: %s\n", Line); }
}
if(track->idx0 == -1) track->idx0 = track->idx1;
if(track->idx1 == -1) track->idx1 = track->idx0;
}
static void set_track_names(tTrack tracks[], int nTracks) {
int i;
for (i = 0; i < nTracks; i++) {
strcpy(tracks[i].name, sBinFilename);
tracks[i].name[strlen(sBinFilename)-4] = '\0';
if (nTracks > 1) {
strcat(tracks[i].name, "-");
strcat(tracks[i].name, tracks[i].num);
}
if( (tracks[i].mode == MODE1_2352) ||
(tracks[i].mode == MODE1_2048) ||
(tracks[i].mode == MODE2_2352) ||
(tracks[i].mode == MODE2_2336) )
{
strcat(tracks[i].name, ".iso");
} else if(tracks[i].mode == AUDIO) {
strcat(tracks[i].name, ".wav");
}
}
}
static void dotrack(short mode, long preidx, long startidx, long endidx, unsigned long offset)
{
unsigned long blockswritten = 0;
unsigned int uiLastIndex;
#if CHECK
unsigned int uiCurrentIndex;
#endif
unsigned int write = 1;
tWavHead wavhead = { "RIFF",
0,
"WAVE",
"fmt ",
16, // 16 byte format specifier
WINDOWS_PCM, // format
2, // 2 Channels
44100, // 44,100 Samples/sec
176400, // 176,400 Bytes/sec
4, // 4 bytes/sample
16, // 16 bits/channel
"data",
0 };
uiLastIndex = startidx-1;
// Input -- process -- Output
if(startidx != 0) printf("\nNote: PreGap = %ld frames\n", startidx-preidx);
else printf("\nNote: PreGap = %d frames\n", OFFSET); // cd standard: starting offset
// - of course this isn't true for bootable cd's...
if(sOutFilename[0] != '\0') {
printf("Creating %s (%06ld,%06ld) ", sOutFilename, startidx, endidx-1);
} else {
printf("Converting (%06ld,%06ld) ", startidx, endidx-1);
}
switch(mode)
{
case AUDIO:
printf("Audio");
break;
case MODE1_2352:
printf("Mode1/2048");
break;
case MODE2_2336:
printf("Mode2/2352");
break;
case MODE2_2352:
if(mode2to1 != 1)
printf("Mode2/2352");
else
printf("Mode1/2048");
break;
case MODE1_2048:
printf("Mode1/2048");
break;
default:
printf("Huh? What's going on?");
exit(1);
}
printf(" : ");
if(sOutFilename[0] != '\0') {
if(NULL == (fdOutFile = fopen (sOutFilename, "wb"))) {
perror("bin2iso(fopen)");
}
// printf("\nOpened File %s: %d\n", sOutFilename, fdOutFile);
} else {
fdOutFile = fdBinFile;
}
if (fdOutFile == NULL) { printf (" Unable to create %s\n", sOutFilename); exit (1); }
if(0 != fseek(fdBinFile, offset, SEEK_SET)) {
perror("\nbin2iso(fseek)"); exit(1);
}
#if (DEBUG == 0)
if(mode == AUDIO) {
if( 1 != fwrite( &wavhead, sizeof(wavhead), 1, fdOutFile ) ) { // write placeholder
perror("\nbin2iso(fwrite)");
fclose(fdOutFile);
// remove(sOutFilename);
exit(1);
}
}
#endif
memset( &buf[0], '\0', sizeof( buf ) );
if(mode == MODE2_2336) {
unsigned int M = 0, S = 2, F = 0;
while( buffered_fread( &buf[16], SIZEISO_MODE2_FORM2) ) {
//setup headed area (probably not necessary though...
//buf[0] = 0;
memset( &buf[1], 0xFF, sizeof(buf[0])*10 );
//buf[11] = 0;
buf[12] = M;
buf[13] = S;
buf[14] = F;
buf[15] = MODE2;
if((++F&0xF) == 0xA) F += 6;
if(F == 0x75) { S++; F = 0; }
if((S&0xF) == 0xA) S += 6;
if(S == 0x60) { M++; S = 0; }
if((M&0xF) == 0xA) M += 6;
// printf("\n%x:%x:%x", M, S, F);
buffered_fwrite( buf, SIZERAW );
uiLastIndex++;
memset( &buf[0], '\0', sizeof( buf ) );
if (startidx%PROG_INTERVAL == 0) { printf("\b\b\b\b\b\b%06ld", startidx); }
if (++startidx == endidx) { printf("\b\b\b\b\b\bComplete\n"); break; }
}
} else if (mode == MODE1_2048) {
while( buffered_fread( buf, SIZEISO_MODE1) ) {
buffered_fwrite( buf, SIZEISO_MODE1 );
uiLastIndex++;
if (startidx%PROG_INTERVAL == 0) { printf("\b\b\b\b\b\b%06ld", startidx); }
if (++startidx == endidx) { printf("\b\b\b\b\b\bComplete\n"); break; }
}
} else {
while( buffered_fread( buf, SIZERAW) ) {
switch(mode) {
case AUDIO:
#if (DEBUG == 0)
buffered_fwrite( buf, SIZERAW );
#endif
uiLastIndex++;
blockswritten++;
break;
case MODE1_2352:
// should put a crc check in here...
#if CHECK
if( buf[15] != MODE1)
{
printf("\nWarning: Mode Error in bin file!\n");
printf(" %02x:%02x:%02x : mode %02x\n", buf[12], buf[13], buf[14], buf[15] );
//exit(1);
}
uiCurrentIndex = Index(buf[12], buf[13], buf[14]) - OFFSET;
if(uiCurrentIndex != uiLastIndex+1)
{
printf("\nWarning: Frame Error in bin file!\n");
printf("Last %02d:%02d:%02d (%d)\n", ((uiLastIndex+OFFSET)/75)/60, ((uiLastIndex+OFFSET)/75)%60, (uiLastIndex+OFFSET)%75, uiLastIndex );
printf("Current %02x:%02x:%02x (%d)\n", buf[12], buf[13], buf[14], uiCurrentIndex );
printf("Expecting %02d:%02d:%02d (%d)\n", ((uiLastIndex+OFFSET+1)/75)/60, ((uiLastIndex+OFFSET+1)/75)%60, (uiLastIndex+OFFSET+1)%75, uiLastIndex+1 );
}
#endif
#if (DEBUG == 0)
buffered_fwrite( &buf[16], SIZEISO_MODE1 );
#endif
#if CHECK
uiLastIndex = uiCurrentIndex;
#endif
break;
case MODE2_2352:
#if CHECK
if( (buf[15]&0xf) != MODE2)
{
printf("\nWarning: Mode Error in bin file!\n");
printf(" %02x:%02x:%02x : mode %02x\n", buf[12], buf[13], buf[14], buf[15] );
//exit(1);
}
uiCurrentIndex = Index(buf[12], buf[13], buf[14]) - OFFSET;
if(uiCurrentIndex != uiLastIndex+1)
{
printf("\nWarning: Frame Error in bin file!\n");
printf("Last %02d:%02d:%02d (%d)\n", ((uiLastIndex+OFFSET)/75)/60, ((uiLastIndex+OFFSET)/75)%60, (uiLastIndex+OFFSET)%75, uiLastIndex );
printf("Current %02x:%02x:%02x (%d)\n", buf[12], buf[13], buf[14], uiCurrentIndex );
printf("Expecting %02d:%02d:%02d (%d)\n", ((uiLastIndex+OFFSET+1)/75)/60, ((uiLastIndex+OFFSET+1)/75)%60, (uiLastIndex+OFFSET+1)%75, uiLastIndex+1 );
}
#endif
#if (DEBUG == 0)
if(mode2to1) buffered_fwrite( &buf[16+8], SIZEISO_MODE1 );
else if(write) buffered_fwrite( &buf[0], SIZEISO_MODE2_RAW );
#endif
#if CHECK
uiLastIndex = uiCurrentIndex;
#endif
break;
default:
printf("Unknown Mode\n"); exit(1);
break;
}
memset( &buf[0], '\0', sizeof( buf ) );
if (startidx%PROG_INTERVAL == 0) { printf("\b\b\b\b\b\b%06ld", startidx); }
if (++startidx == endidx) { printf("\b\b\b\b\b\bComplete\n"); break; }
}
}
flush_buffers(); // flushes write buffer
// and clears read buffer.
if(mode == AUDIO) {
wavhead.blocksize = blockswritten*SIZERAW;
wavhead.bytestoend = wavhead.blocksize + HEADBYTES;
// rewind to the beginning
if(0 != fseek(fdOutFile, 0, SEEK_SET)) {
perror("\nbin2iso(fseek)"); exit(1);
}
#if (DEBUG == 0)
fwrite( &wavhead, sizeof(wavhead), 1, fdOutFile );
#endif
}
fclose(fdOutFile);
}
static void doCueFile(void) {
int track = 1;
unsigned long int binIndex = 0;
unsigned long int trackIndex = 0;
const int gapThreshold = 20; // look for 0.266 sec gap
const int valueThreshold = 800; // look for samples < 700
int count = 0;
int i, blank;
int gapon = 0;
short value;
char mode[13] = "AUDIO";
char index0[9] = "00:00:00";
char index1[9] = "00:00:00";
printf( "FILE %s BINARY\n", sBinFilename);
fprintf(fdCueFile, "FILE %s BINARY\n", sBinFilename);
memset( buf, '\0', sizeof( buf ) );
while( fread( buf, 1, SIZERAW, fdBinFile ) ) {
if(trackIndex == 0) {
if ( (buf[0] == 0x00) &&
(buf[1] == 0xFF) &&
(buf[2] == 0xFF) &&
(buf[3] == 0xFF) &&
(buf[4] == 0xFF) &&
(buf[5] == 0xFF) &&
(buf[6] == 0xFF) &&
(buf[7] == 0xFF) &&
(buf[8] == 0xFF) &&
(buf[9] == 0xFF) &&
(buf[10] == 0xFF) &&
(buf[11] == 0x00)
) {
sprintf(mode, "MODE%d/2352", buf[15]);
} else {
sprintf(mode, "AUDIO");
}
}
if(binIndex == 0) {
printf( " TRACK %02d %s\n", track, mode);
fprintf(fdCueFile, " TRACK %02d %s\n", track, mode);
printf( " INDEX 01 %s\n", index0);
fprintf(fdCueFile, " INDEX 01 %s\n", index0);
}
blank = 1;
for(i = 0; i < SIZERAW; i+=2) {
value = buf[i+1];
value = ((value << 8) | buf[i]);
// printf("%f %i\n",(1.0/75)*binIndex, value);
if(abs(value) > valueThreshold) {
blank = 0;
break;
}
}
// if(i == SIZERAW) printf("%f ~blank~\n", (1.0/75)*binIndex);
if(blank == 1) count++;
else if (gapon == 1) {
gapon = 0;
unIndex(binIndex-count, index0);
count = 0;
unIndex(binIndex, index1);
printf( " TRACK %02d %s\n", track, mode);
fprintf(fdCueFile, " TRACK %02d %s\n", track, mode);
printf( " INDEX 00 %s\n", index0);
fprintf(fdCueFile, " INDEX 00 %s\n", index0);
printf( " INDEX 01 %s\n", index1);
fprintf(fdCueFile, " INDEX 01 %s\n", index1);
}
if((count > gapThreshold) && (gapon == 0)) {
gapon = 1; track++;
trackIndex = -1;
}
memset( buf, '\0', sizeof( buf ) );
binIndex++;
trackIndex++;
}
}
// return 0 to when no data found, 1 when there is.
static int checkGaps(FILE *fdBinFile, tTrack tracks[], int nTracks) {
int i, k;
unsigned long int j;
int writegap = 0;
short value;
int count;
if(nTracks == 2) { return 0; }; // don't need to bother with single track images
printf("Checking gap data:\n");
for (i = 0; i < nTracks; i++) {
if((tracks[i].offset0 != tracks[i].offset1) && (tracks[i-1].mode == AUDIO)) {
if(0 != fseek(fdBinFile, tracks[i].offset0, SEEK_SET)) {
perror("\nbin2iso(fseek)"); exit(1);
}
count = 0;
for(j = tracks[i].idx0; j < tracks[i].idx1; j++) {
if(0 == fread( buf, SIZERAW, 1, fdBinFile ) ) {
perror("bin2iso(fread)");
exit(1);
}
for(k = 0; k < SIZERAW; k+=2) {
value = buf[k+1];
value = ((value << 8) | buf[k]);
if(value != 0) {
count++;
// printf("%10d: %2x\n", count ,value );
}
}
}
if(count != 0) {
printf(" Track%02d - %d values of Non-Zero gap data encountered\n", i-1, count);
if((count > SIZERAW/2/2) && (writegap == 0)) {
printf(" -->Threshold reached\n"); writegap = 1;
}
}
}
}
return writegap;
}
/* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
int main(int argc, char **argv) {
// int printon = 0;
char sLine[256];
int i,j,q;
// int writegap = -1; // auto detect pregap data action by default.
int writegap = 1; // keep pregap data by default.
int no_overburn = 0;
int createCue = 0;
char sTrack[3] = "00";
int doOneTrack = 0;
int doInPlace = 0;
tTrack trackA;
tTrack trackB;
tTrack tracks[100];
int nTracks = 0;
char sOutdir[192];
/* Tell them what I am. */
printf("\n%s, %s\n"
"bin2iso V1.9bm5 - Converts RAW format (.bin) files to ISO/WAV format\n"
"MIT License\n"
"(c) 1999 Bob Doiron\n"
"(c) 2020 - 2025 Michael Ortmann\n", __DATE__, __TIME__
);
if(argc < 2) {
printf("Usage: bin2iso <cuefile> [<output dir>] [-[a]wg] [-t XX] [-i] [-nob]\n"
"or : bin2iso <cuefile> -c <binfile>\n"
"\n"
"Where:\n"
" <cuefile> - The .cue file that belongs to the .bin file to be converted\n"
" <output dir> - The output directory (defaults to current dir)\n"
" -nwg - Indicates that audio data found in the track 'gaps'\n"
" shouldn't be appended to the audio tracks\n"
" -awg - Looks for non-zero data in the 'gaps', if found then gaps\n"
" are appended to audio tracks. Looks for more than 1/2 of a\n"
" sector of non-zero values (%d values).\n"
" -t XX - Extracts the XX'th track.\n"
" -i - Performs the conversion 'in place'. Meaning it truncates the\n"
" binfile after each track is created to minimize diskspace\n"
" requirements.\n"
" -nob - Doesn't use overburn data past %d sectors.\n"
" This of course presumes that the data is not useful.\n"
" -c - Attempts to create a <cuefile> from an existing <binfile>\n", SIZERAW/2/2, CD74_MAX_SECTORS
);
exit(1);
}
sOutFilename[0] = '\0';
strcpy(sOutdir, "./"); // default path
printf("\n");
for (i=2; i < argc; i++) {
if (argv[i][0] == '-') {
/* if (strncmp(&(argv[i][1]), "wg", 2)==0) {
writegap = 1;
} else */
if (strncmp(&(argv[i][1]), "awg", 3)==0) {
writegap = -1;
printf("Note: Auto-detecting pregap data\n");
} else if (strncmp(&(argv[i][1]), "nwg", 3)==0) {
writegap = 0;
} else if (strncmp(&(argv[i][1]), "m2to1", 5)==0) {
mode2to1 = 1;
printf("Note: Converting Mode2 ISO to Mode1\n");
} else if (strncmp(&(argv[i][1]), "t", 1)==0) {
strcpy(sTrack, argv[i+1]);
doOneTrack = 1;
i++;
} else if (strncmp(&(argv[i][1]), "i", 1)==0) {
if(doOneTrack == 1) { printf("Invalid combination of options...\n"); exit(1); }
printf("Bin file will be truncated after each track created\n");
doInPlace = 1;
} else if (strncmp(&(argv[i][1]), "c", 1)==0) {
createCue = 1;
strcpy(sBinFilename, argv[i+1]);
i++;
} else if (strncmp(&(argv[i][1]), "nob", 3)==0) {
no_overburn = 1;
}
} else {
strcpy(sOutdir, argv[2]);
}
}
if(createCue == 1) {
fdBinFile = fopen (sBinFilename, "rb");
if (fdBinFile == NULL) {
printf ("Unable to open %s\n", sBinFilename);
exit (1);
}
fdCueFile = fopen (argv[1], "w");
if (fdCueFile == NULL) {
printf ("Unable to create %s\n", argv[1]);
exit (1);
}
if((strcmp(&sBinFilename[strlen(sBinFilename)-4], ".wav")==0) ||
(strcmp(&sBinFilename[strlen(sBinFilename)-4], ".WAV")==0) ) {
printf(".wav binfile - Skipping wav header\n");
if (fread(sLine, 1, sizeof(tWavHead), fdBinFile ) < sizeof(tWavHead)) {
fprintf(stderr, "fread(%s)\n", sBinFilename);
exit(1);
}
}
doCueFile();
} else {
fdCueFile = fopen (argv[1], "r");
if (fdCueFile == NULL) {
printf ("Unable to open %s\n", argv[1]);
exit (1);
}
// get bin filename from cuefile... why? why not.
if(! fgets( sLine, 256, fdCueFile ) ) {
printf ("Error Reading Cuefile\n");
exit (1);
}
if (strncmp(sLine, "FILE ", 5)==0) {
i = 0;
j = 0;
q = 0; // track open and closed quotes
do {
sBinFilename[j] = sLine[5+i];
i++;
j++;
if ((sBinFilename[j-1] == '\\') || (sBinFilename[j-1] == '/')) { j = 0; } //strip out path info
if (sBinFilename[j-1] == '"') { j--; q++;} // strip out quotes
} while ((sLine[5+i-1] != ' ') || (q == 1));
sBinFilename[j] = '\0';
//bug?? Why did a trailing space show up??
while(sBinFilename[--j] == ' ') sBinFilename[j] = '\0';
// do not need to convert to lower case on unix system
// strlwr(sBinFilename);
} else {
printf ("Error: Filename not found on first line of cuefile %s.\n", argv[1]);
exit (1);
}
// Open the bin file
if(doInPlace == 1) {
fdBinFile = fopen (sBinFilename, "rb+");
} else {
fdBinFile = fopen (sBinFilename, "rb");
}
if (fdBinFile == NULL) {
printf ("Unable to open %s\n", sBinFilename);
perror("\nbin2iso(fopen)");
exit(1);
}
// Get next line
if(! fgets( sLine, 256, fdCueFile ) ) {
printf ("Error Reading Cuefile\n");
exit (1);
}
if(strlen(sOutdir) > 0) {
if((sOutdir[strlen(sOutdir)-1] != '/' ) && (sOutdir[strlen(sOutdir)-1] != ':' ) ) {
strcat(sOutdir, "/");
}
}
while(!feof(fdCueFile)) {
getTrackinfo(sLine, &tracks[nTracks++]);
}
set_track_names(tracks, nTracks);
tracks[nTracks].idx0 = tracks[nTracks].idx1 = -1;
switch (tracks[0].mode) {
case MODE1_2048:
tracks[0].offset0 = tracks[0].idx0*SIZEISO_MODE1;
break;
case MODE2_2336:
tracks[0].offset0 = tracks[0].idx0*SIZEISO_MODE2_FORM2;
break;
default: // AUDIO, MODE1_2352, MODE2_2352:
tracks[0].offset0 = tracks[0].idx0*SIZERAW;
break;
}
/* set offsets */
if(0 != fseek(fdBinFile, 0, SEEK_END)) {
perror("\nbin2iso(fseek)"); exit(1);
}
tracks[nTracks].offset0 = tracks[nTracks].offset1 = ftell(fdBinFile);
for(i = 0; i < nTracks; i++) {
switch (tracks[i].mode) {
case MODE1_2048:
tracks[i].offset1 = tracks[i].offset0 + (tracks[i].idx1-tracks[i].idx0)*SIZEISO_MODE1;
if(tracks[i+1].idx0 != -1)
tracks[i+1].offset0 = tracks[i].offset1 + (tracks[i+1].idx0 - tracks[i].idx1)*SIZEISO_MODE1;
else {
tracks[i+1].idx0 = tracks[i+1].idx1 = (tracks[i+1].offset0 - tracks[i].offset1)/SIZEISO_MODE1 + tracks[i].idx1;
if(((tracks[i+1].offset0 - tracks[i].offset1)%SIZEISO_MODE1) != 0) printf("Warning: Bin file has invalid byte count for cuefile.\n");
}
break;
case MODE2_2336:
tracks[i].offset1 = tracks[i].offset0 + (tracks[i].idx1-tracks[i].idx0)*SIZEISO_MODE2_FORM2;
if(tracks[i+1].idx0 != -1)
tracks[i+1].offset0 = tracks[i].offset1 + (tracks[i+1].idx0 - tracks[i].idx1)*SIZEISO_MODE2_FORM2;
else {
tracks[i+1].idx0 = tracks[i+1].idx1 = (tracks[i+1].offset0 - tracks[i].offset1)/SIZEISO_MODE2_FORM2 + tracks[i].idx1;
if(((tracks[i+1].offset0 - tracks[i].offset1)%SIZEISO_MODE2_FORM2) != 0) printf("Warning: Bin file has invalid byte count for cuefile.\n");
}
break;
default: // AUDIO, MODE1_2352, MODE2_2352:
tracks[i].offset1 = tracks[i].offset0 + (tracks[i].idx1-tracks[i].idx0)*SIZERAW;
if(tracks[i+1].idx0 != -1)
tracks[i+1].offset0 = tracks[i].offset1 + (tracks[i+1].idx0 - tracks[i].idx1)*SIZERAW;
else {
tracks[i+1].idx0 = tracks[i+1].idx1 = (tracks[i+1].offset0 - tracks[i].offset1)/SIZERAW + tracks[i].idx1;
if(((tracks[i+1].offset0 - tracks[i].offset1)%SIZERAW) != 0) printf("Warning: Bin file has invalid byte count for cuefile.\n");
}
break;
}
}
// if not allowing overburn, then create a new track to hold extra data...
if(no_overburn == 1) {
i = nTracks;
if(tracks[i].idx0 > CD74_MAX_SECTORS) {
tracks[i+1] = tracks[nTracks];
strcpy(tracks[i].name, "obdatatemp.bin");
tracks[i].idx0 = CD74_MAX_SECTORS;
tracks[i].idx1 = CD74_MAX_SECTORS;
switch (tracks[i-1].mode) {
case MODE1_2048:
tracks[i].offset0 = tracks[i-1].offset1 + (tracks[i].idx0 - tracks[i-1].idx1)*SIZEISO_MODE1;
break;
case MODE2_2336:
tracks[i].offset0 = tracks[i-1].offset1 + (tracks[i].idx0 - tracks[i-1].idx1)*SIZEISO_MODE2_FORM2;
break;
default: // AUDIO, MODE1_2352, MODE2_2352:
tracks[i].offset0 = tracks[i-1].offset1 + (tracks[i].idx0 - tracks[i-1].idx1)*SIZERAW;
break;
}
tracks[i].offset1 = tracks[i].offset0;
tracks[i].mode = tracks[i-1].mode;
nTracks++;
}
}
/* set sizes */
for(i = 0; i < nTracks; i++) {
switch (tracks[i].mode) {
case MODE1_2352:
tracks[i].size = ((tracks[i+1].offset1 - tracks[i].offset1) / SIZERAW ) * SIZEISO_MODE1;
break;
case MODE2_2336:
tracks[i].size = ((tracks[i+1].offset1 - tracks[i].offset1) / SIZEISO_MODE2_FORM2 ) * SIZERAW;
break;
default: // MODE1_2048, MODE2_2352, AUDIO
tracks[i].size = tracks[i+1].offset1 - tracks[i].offset1;
break;
}
}
if(writegap == -1) { writegap = checkGaps(fdBinFile, tracks, nTracks); }
if(writegap == 1)
printf("Note: Appending pregap data to end of audio tracks\n");
else
printf("Note: Discarding pregap data\n");
printf("\n");
for(i = 0; i <= nTracks-1; i++) {
#ifdef __DOS__
printf("%s (%3d Mb) - sectors %06ld:",
tracks[i].name,
tracks[i].size/(1024l*1024l),
tracks[i].idx1
);
printf("%06ld (offset %09ld:",
( ((writegap == 0) || (tracks[i].mode != AUDIO)) ? tracks[i+1].idx0 : tracks[i+1].idx1)-1,
tracks[i].offset1
);
printf("%09ld)\n",
( ((writegap == 0) || (tracks[i].mode != AUDIO)) ? tracks[i+1].offset0 : tracks[i+1].offset1)-1
);
#else
printf("%s (%3lu Mb) - sectors %06ld:%06ld (offset %09ld:%09ld)\n",
tracks[i].name,
tracks[i].size/(1024*1024),
tracks[i].idx1,
( ((writegap == 0) || (tracks[i].mode != AUDIO)) ? tracks[i+1].idx0 : tracks[i+1].idx1)-1,
tracks[i].offset1,
( ((writegap == 0) || (tracks[i].mode != AUDIO)) ? tracks[i+1].offset0 : tracks[i+1].offset1)-1
);
#endif
}
printf("\n");
if( (((mode2to1 != 1) && (tracks[0].mode == MODE2_2352)) || (tracks[0].mode == MODE1_2048)) && (nTracks == 1) ) {
if(tracks[0].mode == MODE2_2352) { printf("Mode2/2352"); }
if(tracks[0].mode == MODE1_2048) { printf("Mode1/2048"); }
printf(" single track bin file indicated by cue file\n");
fclose(fdBinFile);
if( 0 != rename(sBinFilename, tracks[0].name) ) {
perror("\nbin2iso(rename)");
exit(1);
}
printf("%s renamed to %s\n", sBinFilename, tracks[0].name);
fclose(fdCueFile);
return(0);
}
for(i=nTracks-1; i>=0; i--) {
trackA = tracks[i];
trackB = tracks[i+1];
// audio can't be done in the bin file due to header.