summaryrefslogtreecommitdiff
path: root/lib/cdreader.cpp
blob: 77b14f9d2172437b91ef8f41c10f77b749ce5ba8 (plain)
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "cdreader.h"
#include "Exceptions.h"
#include "generic.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define _(x) x
#endif

bool cdreader::CanWrite() const {
    return 0;
}

bool cdreader::CanRead() const {
    return 1;
}

bool cdreader::CanSeek() const {
    return 1;
}

String cdreader::GetName() const {
    return n + " raw reading";
}

ssize_t cdreader::GetSize() const {
    return -1;
}

off_t cdreader::seek(off_t offset, int whence) throw (GeneralException) {
    switch (whence) {
    case SEEK_SET:
	itell = offset;
	break;
    case SEEK_CUR:
	itell += offset;
	break;
    case SEEK_END:
	throw GeneralException("Can not yet seek from the end of a CD.");
    }
    return itell;
}

ssize_t cdreader::read(void *buf, size_t count) throw (GeneralException) {
    char sector[2352];
    size_t r = count;
    
    size_t remain = itell % 2352;
    sectorseek(itell / 2352);
    
    while (count > 0) {
	size_t n = MIN(2352 - remain, count);
	getsector(sector);
	bcopy(sector + remain, buf, n);
	buf = ((char *) buf) + n;
	itell += n;
	count -= n;
	remain = 0;
    }
    
    return r;
}

void cdreader::sectorseek(int sec) {
    sector = sec;
}

#ifdef __linux__
#include <sys/ioctl.h>
#include <linux/cdrom.h>

cdreader::cdreader(const String & no) throw (GeneralException) :
    Handle(open(no.to_charp(), O_RDONLY)), n(no), sector(0) {

#ifdef DEBUG    
    fprintf(stderr, "Opening cdrom device %s\n", no.to_charp(), this);
#endif

    if (GetHandle() < 0) {
	throw IOGeneral(String(_("Error opening file ")) + no + _(" for reading: ") + strerror(errno));
    }
    
    struct stat s;
    fstat(GetHandle(), &s);

    if (!S_ISBLK(s.st_mode)) {
	throw GeneralException(no + " is not a block device.");
    }
}

cdreader::cdreader(const cdreader & i) : Handle(i), n(i.n) {
}

void cdreader::getsector(void *buf, int sec) throw (GeneralException) {
    struct cdrom_msf * msf = (struct cdrom_msf *) buf;
    if (sec >= 0)
	sector = sec;
    
    sector += 150;
    
    msf->cdmsf_min0  =   sector   /CD_SECS   /CD_FRAMES;
    msf->cdmsf_sec0  =  (sector   /CD_FRAMES)%CD_SECS;
    msf->cdmsf_frame0=   sector   %CD_FRAMES;
    msf->cdmsf_min1  =  (sector+1)/CD_SECS   /CD_FRAMES;
    msf->cdmsf_sec1  = ((sector+1)/CD_FRAMES)%CD_SECS;
    msf->cdmsf_frame1=  (sector+1)%CD_FRAMES;
    
    sector -= 150;

    if (ioctl(GetHandle(), CDROMREADRAW, buf) < 0) {
	throw GeneralException(String("unable to read cd sector ") + sector + ": " + strerror(errno));
    }
    
    sector++;
}

#endif