-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcdaccess.c
179 lines (146 loc) · 4.55 KB
/
cdaccess.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
/*
* cdaccess.c
* System-dependent code to access extended CD information
*
* Based on a contribution by Erik Andersen
*
* Copyright (c) 2003 Christoph Pfisterer
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "global.h"
#ifdef USE_IOCTL_LINUX
#include <sys/ioctl.h>
#include <linux/cdrom.h>
#define DO_CDACCESS 1
#endif /* USE_IOCTL_LINUX */
#ifdef DO_CDACCESS
/*
* CDDB stuff
*/
static int cddb_sum(int n);
#define LBA_TO_SECS(lba) (((lba) + 150) / 75)
/*
* CD structure:
* Tries to get the CD TOC using out-of-band means (i.e. ioctl()),
* prints the info, and analyzes just the data tracks. Quite
* system-dependent, but layed out for porability.
*/
int analyze_cdaccess(int fd, SOURCE *s, int level)
{
int i;
int first, last, ntracks;
int cksum, totaltime, seconds;
u4 diskid;
u1 ctrl[100];
u4 lba[100], length;
char human_readable_size[256];
#ifdef USE_IOCTL_LINUX
struct cdrom_tochdr tochdr;
struct cdrom_tocentry tocentry;
#endif
/* read TOC header */
#ifdef USE_IOCTL_LINUX
if (ioctl(fd, CDROMREADTOCHDR, &tochdr) < 0) {
return 0;
}
first = tochdr.cdth_trk0;
last = tochdr.cdth_trk1;
#endif
ntracks = last + 1 - first;
if (ntracks > 99) /* natural limit */
return 0;
/* read per-track data from TOC */
for (i = 0; i <= ntracks; i++) {
#ifdef USE_IOCTL_LINUX
if (i == ntracks)
tocentry.cdte_track = CDROM_LEADOUT;
else
tocentry.cdte_track = first + i;
tocentry.cdte_format = CDROM_LBA;
if (ioctl(fd, CDROMREADTOCENTRY, &tocentry) < 0) {
//printf("CDROMREADTOCENTRY: %s: ", strerror(errno));
return 0;
}
ctrl[i] = tocentry.cdte_ctrl;
lba[i] = tocentry.cdte_addr.lba;
#endif
}
/* System-dependent code ends here. From now on, we use the data
in first, last, ntracks, ctrl[], and lba[]. We also assume
all systems treat actual data access the same way... */
/* calculate CDDB disk id */
cksum = 0;
for (i = 0; i < ntracks; i++) {
cksum += cddb_sum(LBA_TO_SECS(lba[i]));
}
totaltime = LBA_TO_SECS(lba[ntracks]) - LBA_TO_SECS(lba[0]);
diskid = (u4)(cksum % 0xff) << 24 | (u4)totaltime << 8 | (u4)ntracks;
/* print disk info */
print_line(level, "CD-ROM, %d track%s, CDDB disk ID %08lX",
ntracks, (ntracks != 1) ? "s" : "", diskid);
/* Loop over each track */
for (i = 0; i < ntracks; i++) {
/* length of track in sectors */
length = lba[i+1] - lba[i];
if ((ctrl[i] & 0x4) == 0) {
/* Audio track, one sector holds 2352 actual data bytes */
seconds = length / 75;
format_size(human_readable_size, (u8)length * 2352);
print_line(level, "Track %d: Audio track, %s, %3d min %02d sec",
first + i, human_readable_size,
seconds / 60, seconds % 60);
} else {
/* Data track, one sector holds 2048 actual data bytes */
format_size(human_readable_size, length * 2048);
print_line(level, "Track %d: Data track, %s",
first + i, human_readable_size);
/* NOTE: we adjust the length to stay clear of padding or
post-gap stuff */
analyze_source_special(s, level + 1,
(u8)lba[i] * 2048, (u8)(length - 250) * 2048);
}
}
return 1;
}
/*
* helper function: calculate cddb disk id
*/
static int cddb_sum(int n)
{
/* a number like 2344 becomes 2+3+4+4 (13) */
int ret = 0;
while (n > 0) {
ret = ret + (n % 10);
n = n / 10;
}
return ret;
}
#else /* DO_CDACCESS */
/*
* the system is not supported, so use a dummy function
*/
int analyze_cdaccess(int fd, SOURCE *s, int level)
{
return 0;
}
#endif /* DO_CDACCESS */
/* EOF */