1 /* $OpenBSD: rip.c,v 1.21 2022/10/11 16:32:40 krw Exp $ */
2
3 /*
4 * Copyright (c) 2007 Alexey Vatchenko <av@bsdua.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include <sys/types.h>
19 #include <sys/signal.h>
20 #include <sys/device.h>
21
22 #include <sys/cdio.h>
23 #include <sys/ioctl.h>
24 #include <sys/scsiio.h>
25 #include <sys/stat.h>
26 #include <sys/time.h>
27
28 #include <scsi/scsi_all.h>
29 #include <scsi/scsi_disk.h>
30 #include <scsi/scsiconf.h>
31 #include <scsi/cd.h>
32
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <sndio.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <unistd.h>
43
44 #include "extern.h"
45
46 extern int fd;
47 extern int msf;
48 extern struct cd_toc_entry *toc_buffer;
49
50 extern u_int msf2lba(u_char m, u_char s, u_char f);
51 extern int read_toc_entrys(int size);
52
53 /*
54 * Arguments parser
55 */
56 TAILQ_HEAD(track_pair_head, track_pair);
57
58 static int _parse_val(char *start, char *nxt, int *val);
59 static int _parse_pair(char *start, char *nxt, int *val1, int *val2);
60 static int _add_pair(struct track_pair_head *head, int val1, int val2,
61 int issorted);
62
63 struct track_pair {
64 u_char start;
65 u_char end;
66 TAILQ_ENTRY(track_pair) list;
67 };
68
69 void parse_tracks_init(struct track_pair_head *head);
70 void parse_tracks_final(struct track_pair_head *head);
71 int parse_tracks(struct track_pair_head *head, u_char first, u_char last,
72 const char *arg, int issorted);
73 int parse_tracks_add(struct track_pair_head *head, u_char first,
74 u_char last, int issorted);
75
76 /*
77 * Tracks ripping
78 */
79 /* Header of the canonical WAVE file */
80 static u_char wavehdr[44] = {
81 'R', 'I', 'F', 'F', 0x0, 0x0, 0x0, 0x0, 'W', 'A', 'V', 'E',
82 'f', 'm', 't', ' ', 0x10, 0x0, 0x0, 0x0, 0x1, 0x0, 0x2, 0x0,
83 0x44, 0xac, 0x0, 0x0, 0x10, 0xb1, 0x2, 0x0, 0x4, 0x0, 0x10, 0x0,
84 'd', 'a', 't', 'a', 0x0, 0x0, 0x0, 0x0
85 };
86
87 static int write_sector(int, u_char *, u_int32_t);
88
89 int read_data_sector(u_int32_t, u_char *, u_int32_t);
90
91 struct track {
92 int fd; /* descriptor of output file */
93 struct sio_hdl *hdl; /* sndio handle */
94 struct sio_par par; /* sndio parameters */
95 u_int track; /* track number */
96 char name[12]; /* output file name, i.e. trackXX.wav/trackXX.dat */
97 u_char isaudio; /* true if audio track, otherwise it's data track */
98 u_int32_t start_lba; /* starting address of this track */
99 u_int32_t end_lba; /* starting address of the next track */
100 };
101
102 int read_track(struct track *);
103
104 int rip_next_track(struct track *);
105 int play_next_track(struct track *);
106
107 static int rip_tracks_loop(struct track_pair *tp, u_int n_tracks,
108 int (*next_track)(struct track *));
109
110 int rip_tracks(char *arg, int (*next_track)(struct track *),
111 int issorted);
112
113 /* Next-Track function exit codes */
114 #define NXTRACK_OK 0
115 #define NXTRACK_FAIL 1
116 #define NXTRACK_SKIP 2
117
118 static int
_parse_val(char * start,char * nxt,int * val)119 _parse_val(char *start, char *nxt, int *val)
120 {
121 char *p;
122 int i, base, n;
123
124 n = nxt - start;
125
126 if (n > 3 || n < 1)
127 return (-1);
128 for (p = start; p < nxt; p++) {
129 if (!isdigit((unsigned char)*p))
130 return (-1);
131 }
132
133 *val = 0;
134 base = 1;
135 for (i = 0; i < n; i++) {
136 *val += base * (start[n - i - 1] - '0');
137 base *= 10;
138 }
139 return (0);
140 }
141
142 static int
_parse_pair(char * start,char * nxt,int * val1,int * val2)143 _parse_pair(char *start, char *nxt, int *val1, int *val2)
144 {
145 char *p, *delim;
146 int error;
147
148 delim = NULL;
149 p = start;
150 while (p < nxt) {
151 if (*p == '-')
152 delim = p;
153 p++;
154 }
155
156 if (delim != NULL) {
157 error = 0;
158 if (delim - start < 1)
159 *val1 = -1;
160 else
161 error = _parse_val(start, delim, val1);
162
163 if (error == 0) {
164 if ((nxt - delim - 1) < 1)
165 *val2 = -1;
166 else
167 error = _parse_val(delim + 1, nxt, val2);
168 }
169 } else {
170 error = _parse_val(start, nxt, val1);
171 *val2 = *val1;
172 }
173
174 if (error == 0) {
175 if (*val1 > 99 || *val2 > 99)
176 error = -1;
177 }
178
179 return (error);
180 }
181
182 static int
_add_pair(struct track_pair_head * head,int val1,int val2,int issorted)183 _add_pair(struct track_pair_head *head, int val1, int val2, int issorted)
184 {
185 u_char v1, v2, v3;
186 struct track_pair *tp, *entry;
187 int fix;
188
189 v1 = (u_char)val1;
190 v2 = (u_char)val2;
191
192 if (issorted) {
193 /* 1. Fix order */
194 if (v1 > v2) {
195 v3 = v1;
196 v1 = v2;
197 v2 = v3;
198 }
199
200 /* 2. Find closest range and fix it */
201 fix = 0;
202 TAILQ_FOREACH(entry, head, list) {
203 if (v1 + 1 == entry->start || v1 == entry->start)
204 fix = 1;
205 else if (v1 > entry->start && v1 <= entry->end + 1)
206 fix = 1;
207 else if (v2 + 1 == entry->start || v2 == entry->start)
208 fix = 1;
209 else if (v2 > entry->start && v2 <= entry->end + 1)
210 fix = 1;
211 if (fix)
212 break;
213 }
214
215 if (fix) {
216 if (v1 < entry->start)
217 entry->start = v1;
218 if (v2 > entry->end)
219 entry->end = v2;
220
221 return (0);
222 }
223 }
224
225 tp = malloc(sizeof(*tp));
226 if (tp == NULL)
227 return (-1);
228
229 tp->start = v1;
230 tp->end = v2;
231 TAILQ_INSERT_TAIL(head, tp, list);
232
233 return (0);
234 }
235
236 void
parse_tracks_init(struct track_pair_head * head)237 parse_tracks_init(struct track_pair_head *head)
238 {
239
240 memset(head, 0, sizeof(*head));
241 TAILQ_INIT(head);
242 }
243
244 void
parse_tracks_final(struct track_pair_head * head)245 parse_tracks_final(struct track_pair_head *head)
246 {
247 struct track_pair *tp;
248
249 while ((tp = TAILQ_FIRST(head)) != NULL) {
250 TAILQ_REMOVE(head, tp, list);
251 free(tp);
252 }
253 }
254
255 int
parse_tracks(struct track_pair_head * head,u_char first,u_char last,const char * arg,int issorted)256 parse_tracks(struct track_pair_head *head, u_char first, u_char last,
257 const char *arg, int issorted)
258 {
259 char *p, *nxt;
260 int error, val1, val2;
261
262 p = (char *)arg;
263 for (;;) {
264 /* Skip trailing spaces */
265 while (*p != '\0' && isspace((unsigned char)*p))
266 ++p;
267 if (*p == '\0')
268 break;
269
270 /* Search for the next space symbol */
271 nxt = p;
272 while (*nxt != '\0' && !isspace((unsigned char)*nxt))
273 ++nxt;
274 /* ``nxt'' can't be equal to ``p'' here */
275 error = _parse_pair(p, nxt, &val1, &val2);
276 if (error != 0)
277 break; /* parse error */
278
279 if (val1 == -1)
280 val1 = first;
281 if (val2 == -1)
282 val2 = last;
283
284 error = _add_pair(head, val1, val2, issorted);
285 if (error != 0)
286 break; /* allocation error */
287
288 p = nxt;
289 }
290
291 return (0);
292 }
293
294 int
parse_tracks_add(struct track_pair_head * head,u_char first,u_char last,int issorted)295 parse_tracks_add(struct track_pair_head *head, u_char first, u_char last,
296 int issorted)
297 {
298
299 return _add_pair(head, first, last, issorted);
300 }
301
302 static int
write_sector(int fd,u_char * sec,u_int32_t secsize)303 write_sector(int fd, u_char *sec, u_int32_t secsize)
304 {
305 ssize_t res;
306
307 while (secsize > 0) {
308 res = write(fd, sec, secsize);
309 if (res == -1)
310 return (-1);
311
312 sec += res;
313 secsize -= res;
314 }
315
316 return (0);
317 }
318
319 /*
320 * ERRORS
321 * The function can return
322 * [EBUSY] Device is busy.
323 * [ETIMEDOUT] Operation timeout.
324 * [EIO] Any other errors.
325 * [EAGAIN] The operation must be made again. XXX - not implemented
326 */
327 int
read_data_sector(u_int32_t lba,u_char * sec,u_int32_t secsize)328 read_data_sector(u_int32_t lba, u_char *sec, u_int32_t secsize)
329 {
330 scsireq_t scr;
331 u_char *cmd;
332 int error;
333
334 memset(&scr, 0, sizeof(scr));
335
336 cmd = (u_char *)scr.cmd;
337 cmd[0] = 0xbe; /* READ CD */
338 _lto4b(lba, cmd + 2); /* Starting Logical Block Address */
339 _lto3b(1, cmd + 6); /* Transfer Length in Blocks */
340 cmd[9] = 0x10; /* User Data field */
341
342 scr.flags = SCCMD_ESCAPE | SCCMD_READ;
343 scr.databuf = sec;
344 scr.datalen = secsize;
345 scr.cmdlen = 12;
346 scr.timeout = 120000;
347 scr.senselen = SENSEBUFLEN;
348
349 /* XXX - what's wrong with DVD? */
350
351 error = ioctl(fd, SCIOCCOMMAND, &scr);
352 if (error == -1)
353 return (EIO);
354 else if (scr.retsts == SCCMD_BUSY)
355 return (EBUSY);
356 else if (scr.retsts == SCCMD_TIMEOUT)
357 return (ETIMEDOUT);
358 else if (scr.retsts != SCCMD_OK)
359 return (EIO);
360
361 return (0);
362 }
363
364 int
read_track(struct track * ti)365 read_track(struct track *ti)
366 {
367 struct timespec ts, ots, ats;
368 u_int32_t i, blksize, n_sec;
369 u_char *sec;
370 int error;
371
372 n_sec = ti->end_lba - ti->start_lba;
373 blksize = (ti->isaudio) ? 2352 : 2048;
374 sec = malloc(blksize);
375 if (sec == NULL)
376 return (-1);
377
378 timespecclear(&ots);
379 ats.tv_sec = 1;
380 ats.tv_nsec = 0;
381
382 for (i = 0; i < n_sec; ) {
383 clock_gettime(CLOCK_MONOTONIC, &ts);
384 if (timespeccmp(&ts, &ots, >)) {
385 fprintf(stderr, "\rtrack %u '%c' %08u/%08u %3u%%",
386 ti->track,
387 (ti->isaudio) ? 'a' : 'd', i, n_sec,
388 100 * i / n_sec);
389 timespecadd(&ts, &ats, &ots);
390 }
391
392 error = read_data_sector(i + ti->start_lba, sec, blksize);
393 if (error == 0) {
394 if (ti->fd >= 0 &&
395 (write_sector(ti->fd, sec, blksize) != 0)) {
396 free(sec);
397 fprintf(stderr, "\n");
398 warnx("error while writing to the %s file",
399 ti->name);
400 return (-1);
401 }
402 if (ti->hdl != NULL &&
403 (sio_write(ti->hdl, sec, blksize) == 0)) {
404 sio_close(ti->hdl);
405 ti->hdl = NULL;
406 free(sec);
407 fprintf(stderr, "\n");
408 warnx("error while writing to audio output");
409 return (-1);
410 }
411
412 i++;
413 } else if (error != EAGAIN) {
414 free(sec);
415 fprintf(stderr, "\n");
416 warnx("error while reading from device");
417 return (-1);
418 }
419 }
420
421 free(sec);
422 fprintf(stderr, "\rtrack %u '%c' %08u/%08u 100%%\n",
423 ti->track,
424 (ti->isaudio) ? 'a' : 'd', i, n_sec);
425 return (0);
426 }
427
428 int
rip_next_track(struct track * info)429 rip_next_track(struct track *info)
430 {
431 int error;
432 u_int32_t size;
433
434 info->fd = open(info->name, O_CREAT | O_TRUNC | O_RDWR,
435 S_IRUSR | S_IWUSR);
436 if (info->fd == -1) {
437 warnx("can't open %s file", info->name);
438 return (NXTRACK_FAIL);
439 }
440
441 if (info->isaudio) {
442 /*
443 * Prepend audio track with Wave header
444 */
445 size = 2352 * (info->end_lba - info->start_lba);
446 *(u_int32_t *)(wavehdr + 4) = htole32(size + 36);
447 *(u_int32_t *)(wavehdr + 40) = htole32(size);
448 error = write_sector(info->fd, wavehdr, sizeof(wavehdr));
449 if (error == -1) {
450 warnx("can't write WAVE header for %s file",
451 info->name);
452 return (NXTRACK_FAIL);
453 }
454 }
455
456 return (NXTRACK_OK);
457 }
458
459 int
play_next_track(struct track * info)460 play_next_track(struct track *info)
461 {
462 if (!info->isaudio)
463 return (NXTRACK_SKIP);
464
465 if (info->hdl != NULL)
466 return (NXTRACK_OK);
467
468 info->hdl = sio_open(NULL, SIO_PLAY, 0);
469 if (info->hdl == NULL) {
470 warnx("could not open audio backend");
471 goto bad;
472 }
473
474 sio_initpar(&info->par);
475
476 info->par.rate = 44100;
477 info->par.pchan = 2;
478 info->par.bits = 16;
479 info->par.sig = 1;
480 info->par.le = 1;
481 info->par.appbufsz = info->par.rate * 3 / 4;
482
483 if (sio_setpar(info->hdl, &info->par) == 0) {
484 warnx("could not set audio parameters");
485 goto bad;
486 }
487
488 if (sio_getpar(info->hdl, &info->par) == 0) {
489 warnx("could not get audio parameters");
490 goto bad;
491 }
492
493 if (info->par.le != 1 ||
494 info->par.sig != 1 ||
495 info->par.bits != 16 ||
496 info->par.pchan != 2 ||
497 (info->par.rate > 44100 * 1.05 || info->par.rate < 44100 * 0.95)) {
498 warnx("could not configure audio parameters as desired");
499 goto bad;
500 }
501
502 if (sio_start(info->hdl) == 0) {
503 warnx("could not start audio output");
504 goto bad;
505 }
506
507 return (NXTRACK_OK);
508
509 bad:
510 if (info->hdl != NULL) {
511 sio_close(info->hdl);
512 info->hdl = NULL;
513 }
514 return (NXTRACK_FAIL);
515 }
516
517 static int
rip_tracks_loop(struct track_pair * tp,u_int n_tracks,int (* next_track)(struct track *))518 rip_tracks_loop(struct track_pair *tp, u_int n_tracks,
519 int (*next_track)(struct track *))
520 {
521 struct track info;
522 u_char trk;
523 u_int i;
524 char order;
525 int error;
526
527 info.fd = -1;
528 info.hdl = NULL;
529
530 order = (tp->start > tp->end) ? -1 : 1;
531 trk = tp->start;
532 for (;;) {
533 error = 0;
534 for (i = 0; i < n_tracks; i++) {
535 if (trk == toc_buffer[i].track)
536 break;
537 }
538
539 if (i != n_tracks) {
540 /* Track is found */
541 info.track = toc_buffer[i].track;
542 info.isaudio = (toc_buffer[i].control & 4) == 0;
543 snprintf(info.name, sizeof(info.name), "track%02u.%s",
544 toc_buffer[i].track,
545 (info.isaudio) ? "wav" : "dat");
546
547 if (msf) {
548 info.start_lba = msf2lba(
549 toc_buffer[i].addr.msf.minute,
550 toc_buffer[i].addr.msf.second,
551 toc_buffer[i].addr.msf.frame);
552 info.end_lba = msf2lba(
553 toc_buffer[i + 1].addr.msf.minute,
554 toc_buffer[i + 1].addr.msf.second,
555 toc_buffer[i + 1].addr.msf.frame);
556 } else {
557 info.start_lba = toc_buffer[i].addr.lba;
558 info.end_lba = toc_buffer[i + 1].addr.lba;
559 }
560
561 error = next_track(&info);
562 if (error == NXTRACK_FAIL) {
563 error = -1;
564 break;
565 } else if (error != NXTRACK_SKIP) {
566 error = read_track(&info);
567 if (info.fd >= 0) {
568 close(info.fd);
569 info.fd = -1;
570 }
571 if (error != 0) {
572 warnx("can't rip track %u",
573 toc_buffer[i].track);
574 break;
575 }
576 }
577 }
578
579 if (trk == tp->end)
580 break;
581 trk += order;
582 }
583
584 if (info.hdl != NULL) {
585 sio_close(info.hdl);
586 info.hdl = NULL;
587 }
588
589 return (error);
590 }
591
592 int
rip_tracks(char * arg,int (* next_track)(struct track *),int issorted)593 rip_tracks(char *arg, int (*next_track)(struct track *), int issorted)
594 {
595 struct track_pair_head list;
596 struct track_pair *tp;
597 struct ioc_toc_header h;
598 u_int n;
599 int rc;
600
601 rc = ioctl(fd, CDIOREADTOCHEADER, &h);
602 if (rc == -1)
603 return (rc);
604
605 if (h.starting_track > h.ending_track) {
606 warnx("TOC starting_track > TOC ending_track");
607 return (0);
608 }
609
610 n = h.ending_track - h.starting_track + 1;
611 rc = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
612 if (rc < 0)
613 return (rc);
614
615 parse_tracks_init(&list);
616 /* We assume that all spaces are skipped in ``arg''. */
617 if (arg == NULL || *arg == '\0') {
618 rc = parse_tracks_add(&list, h.starting_track, h.ending_track,
619 0);
620 } else {
621 rc = parse_tracks(&list, h.starting_track, h.ending_track, arg,
622 issorted);
623 }
624 if (rc < 0) {
625 warnx("can't create track list");
626 parse_tracks_final(&list);
627 return (rc);
628 }
629
630 TAILQ_FOREACH(tp, &list, list) {
631 rc = rip_tracks_loop(tp, n, next_track);
632 if (rc < 0)
633 break;
634 }
635
636 parse_tracks_final(&list);
637 return (0);
638 }
639
640 int
cdrip(char * arg)641 cdrip(char *arg)
642 {
643 return rip_tracks(arg, rip_next_track, 1);
644 }
645
646 int
cdplay(char * arg)647 cdplay(char *arg)
648 {
649 return rip_tracks(arg, play_next_track, 0);
650 }
651