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
|
/* --------------------------------------------------------------------------
MusicBrainz -- The Internet music metadatabase
Copyright (C) 2000 Robert Kaye
Copyright (C) 1999 Marc E E van Woerkom
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <netinet/in.h>
#include "mb.h"
#include "diskid.h"
#include "config.h"
MUSICBRAINZ_DEVICE DEFAULT_DEVICE = "/dev/cd0";
int ReadTOCHeader(int fd,
int& first,
int& last)
{
struct ioc_toc_header th;
int ret = ioctl(fd,
CDIOREADTOCHEADER,
&th);
if (!ret)
{
first = th.starting_track;
last = th.ending_track;
}
return ret;
}
int ReadTOCEntry(int fd,
int track,
int& lba)
{
struct ioc_read_toc_entry te;
te.starting_track = (u_char) track;
te.address_format = CD_LBA_FORMAT; // experiment and cdio.h say
// lbas are given in network order!
struct cd_toc_entry cte;
te.data = &cte;
te.data_len = sizeof(cd_toc_entry);
int ret = ioctl(fd,
CDIOREADTOCENTRYS,
&te);
if (!ret) {
assert(te.address_format == CD_LBA_FORMAT);
lba = ntohl(te.data->addr.lba); // network to host order (long)
}
return ret;
}
bool DiskId::ReadTOC(MUSICBRAINZ_DEVICE device,
MUSICBRAINZ_CDINFO& cdinfo)
{
int fd;
int first;
int last;
int lba;
int i;
if (device == NULL)
{
device = DEFAULT_DEVICE;
}
fd = open(device, O_RDONLY);
if (fd < 0)
{
char err[256];
sprintf(err, "Cannot open '%s'", device);
ReportError(err);
return false;
}
// Initialize cdinfo to all zeroes.
memset(&cdinfo, 0, sizeof(MUSICBRAINZ_CDINFO));
// Find the number of the first track (usually 1) and the last track.
if (ReadTOCHeader(fd, first, last))
{
ReportError("Cannot read table of contents.");
close(fd);
return false;
}
// Do some basic error checking.
if (last==0)
{
ReportError("This disk has no tracks.");
close(fd);
return false;
}
// Now, for every track, find out the block address where it starts.
for (i = first; i <= last; i++)
{
ReadTOCEntry(fd, i, lba);
cdinfo.FrameOffset[i] = lba + 150;
}
// Get the logical block address (lba) for the end of the audio data.
// The "LEADOUT" track is the track beyond the final audio track
// so we're looking for the block address of the LEADOUT track.
int CDROM_LEADOUT = last + 1;
ReadTOCEntry(fd, CDROM_LEADOUT, lba);
cdinfo.FrameOffset[0] = lba + 150;
cdinfo.FirstTrack = first;
cdinfo.LastTrack = last;
close(fd);
return true;
}
|