| 1 |
|
| /********************************************* |
| 2 |
|
| NAME: linux.h |
| 3 |
|
| FUNCTION: linux based cddb id generation code |
| 4 |
|
| CREATED BY: David Shultz |
| 5 |
|
| CREATED ON: 08/03/2001 |
| 6 |
|
| *********************************************/ |
| 7 |
|
| #ifndef LINUX_H |
| 8 |
|
| #define LINUX_H |
| 9 |
|
| |
| 10 |
|
| struct toc { |
| 11 |
|
| int min, sec, frame; |
| 12 |
|
| } cdtoc[100]; |
| 13 |
|
| |
| 14 |
|
| struct discdata { |
| 15 |
|
| unsigned long discid; |
| 16 |
|
| int num_of_trks; |
| 17 |
|
| int track_offsets[100]; |
| 18 |
|
| int seconds; |
| 19 |
|
| }; |
| 20 |
|
| |
| 21 |
0 |
| unsigned int cddb_sum(int n) { |
| 22 |
|
| unsigned int ret; |
| 23 |
|
| |
| 24 |
0 |
| ret = 0; |
| 25 |
0 |
▶ | while (n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n > 0) { |
0 |
0 |
0 |
|
| 26 |
0 |
| ret += (n % 10); |
| 27 |
0 |
| n /= 10; |
| 28 |
|
| } |
| 29 |
0 |
| return ret; |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
0 |
| unsigned long cddb_discid(int tot_trks) { |
| 33 |
0 |
| unsigned int i, t = 0, n = 0; |
| 34 |
|
| |
| 35 |
0 |
| i = 0; |
| 36 |
0 |
▶ | while (i < tot_trks) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < tot_trks) { |
0 |
0 |
0 |
|
| 37 |
0 |
| n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec); |
| 38 |
0 |
| i++; |
| 39 |
|
| } |
| 40 |
0 |
| t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) - |
| 41 |
0 |
| ((cdtoc[0].min * 60) + cdtoc[0].sec); |
| 42 |
0 |
| return ((n % 0xff) << 24 | t << 8 | tot_trks); |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
0 |
| struct discdata get_disc_id(char* dev) { |
| 46 |
|
| struct discdata data; |
| 47 |
|
| int i; |
| 48 |
|
| |
| 49 |
0 |
| data.num_of_trks = read_toc(dev); |
| 50 |
|
| |
| 51 |
0 |
▶ | if (data.num_of_trks == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (data.num_of_trks == -1) { |
0 |
0 |
0 |
|
| 52 |
0 |
| return data; |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
0 |
| data.discid = cddb_discid(data.num_of_trks); |
| 56 |
|
| |
| 57 |
0 |
▶ | for (i = 0; i < data.num_of_trks; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < data.num_of_trks; i++) { |
0 |
0 |
0 |
|
| 58 |
0 |
| data.track_offsets[i] = (cdtoc[i].frame); |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
0 |
| data.seconds = (cdtoc[data.num_of_trks].frame)/75; |
| 62 |
|
| |
| 63 |
0 |
| return data; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
0 |
| int read_toc(char* dev) { |
| 67 |
|
| int drive; |
| 68 |
|
| struct cdrom_tochdr tochdr; |
| 69 |
|
| struct cdrom_tocentry tocentry; |
| 70 |
|
| int i, status; |
| 71 |
|
| |
| 72 |
0 |
| drive = open(dev, O_RDONLY | O_NONBLOCK); |
| 73 |
0 |
▶ | if (drive == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (drive == -1) { |
0 |
0 |
0 |
|
| 74 |
|
| //printf("Device Error: %d\n", errno); |
| 75 |
0 |
| return(-1); |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
0 |
| status = ioctl(drive, CDROM_DRIVE_STATUS, CDSL_CURRENT); |
| 79 |
0 |
▶ | if (status<0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (status<0) { |
0 |
0 |
0 |
|
| 80 |
|
| //printf("Error: Error getting drive status\n"); |
| 81 |
0 |
| return(-1); |
| 82 |
|
| } else { |
| 83 |
0 |
| switch(status) { |
| 84 |
0 |
| case CDS_DISC_OK: |
| 85 |
|
| //printf("Disc ok, moving on\n"); |
| 86 |
0 |
| break; |
| 87 |
0 |
| case CDS_TRAY_OPEN: |
| 88 |
|
| //printf("Error: Drive reporting tray open...exiting\n"); |
| 89 |
0 |
| close(drive); |
| 90 |
0 |
| return(-1); |
| 91 |
0 |
| case CDS_DRIVE_NOT_READY: |
| 92 |
|
| //printf("Error: Drive Not Ready...exiting\n"); |
| 93 |
0 |
| close(drive); |
| 94 |
0 |
| return(-1); |
| 95 |
0 |
| default: |
| 96 |
|
| //printf("This shouldn't happen\n"); |
| 97 |
0 |
| close(drive); |
| 98 |
0 |
| return(-1); |
| 99 |
|
| } |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| |
| 103 |
|
| |
| 104 |
0 |
▶ | if (ioctl(drive, CDROMREADTOCHDR, &tochdr) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ioctl(drive, CDROMREADTOCHDR, &tochdr) == -1) { |
0 |
0 |
0 |
|
| 105 |
0 |
| switch(errno) { |
| 106 |
0 |
| case EBADF: |
| 107 |
|
| //printf("Error: Invalid device...exiting\n"); |
| 108 |
0 |
| break; |
| 109 |
0 |
| case EFAULT: |
| 110 |
|
| //printf("Error: Memory Write Error...exiting\n"); |
| 111 |
0 |
| break; |
| 112 |
0 |
| case ENOTTY: |
| 113 |
|
| //printf("Error: Invalid device or Request type...exiting\n"); |
| 114 |
0 |
| break; |
| 115 |
0 |
| case EINVAL: |
| 116 |
|
| //printf("Error: Invalid REQUEST...exiting\n"); |
| 117 |
0 |
| break; |
| 118 |
0 |
| case EAGAIN: |
| 119 |
|
| //printf("Error: Drive not ready...exiting\n"); |
| 120 |
0 |
| break; |
| 121 |
0 |
| default: |
| 122 |
|
| //printf("Error: %d\n", errno); |
| 123 |
0 |
| break; |
| 124 |
|
| } |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
0 |
▶ | for (i = tochdr.cdth_trk0; i <= tochdr.cdth_trk1; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = tochdr.cdth_trk0; i <= tochdr.cdth_trk1; i++) { |
0 |
0 |
0 |
|
| 128 |
0 |
| tocentry.cdte_track = i; |
| 129 |
0 |
| tocentry.cdte_format = CDROM_MSF; |
| 130 |
0 |
| ioctl(drive, CDROMREADTOCENTRY, &tocentry); |
| 131 |
0 |
| cdtoc[i-1].min = tocentry.cdte_addr.msf.minute; |
| 132 |
0 |
| cdtoc[i-1].sec = tocentry.cdte_addr.msf.second; |
| 133 |
0 |
| cdtoc[i-1].frame = tocentry.cdte_addr.msf.frame; |
| 134 |
0 |
| cdtoc[i-1].frame += cdtoc[i-1].min*60*75; |
| 135 |
0 |
| cdtoc[i-1].frame += cdtoc[i-1].sec*75; |
| 136 |
|
| } |
| 137 |
0 |
| tocentry.cdte_track = 0xAA; |
| 138 |
0 |
| tocentry.cdte_format = CDROM_MSF; |
| 139 |
0 |
| ioctl(drive, CDROMREADTOCENTRY, &tocentry); |
| 140 |
0 |
| cdtoc[tochdr.cdth_trk1].min = tocentry.cdte_addr.msf.minute; |
| 141 |
0 |
| cdtoc[tochdr.cdth_trk1].sec = tocentry.cdte_addr.msf.second; |
| 142 |
0 |
| cdtoc[tochdr.cdth_trk1].frame = tocentry.cdte_addr.msf.frame; |
| 143 |
0 |
| cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].min*60*75; |
| 144 |
0 |
| cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].sec*75; |
| 145 |
0 |
| close(drive); |
| 146 |
0 |
| return tochdr.cdth_trk1; |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
|
| #endif //LINUX_H |
| 150 |
|
| |
| 151 |
|
| |
| 152 |
|
| |
| 153 |
|
| |
| 154 |
|
| |
| 155 |
|
| |
| 156 |
|
| |