summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
Diffstat (limited to 'contrib')
-rw-r--r--contrib/Makefile.am4
-rw-r--r--contrib/libcdio/FreeBSD/freebsd.c638
-rw-r--r--contrib/libcdio/FreeBSD/freebsd.h232
-rw-r--r--contrib/libcdio/FreeBSD/freebsd_cam.c346
-rw-r--r--contrib/libcdio/FreeBSD/freebsd_ioctl.c267
-rw-r--r--contrib/libcdio/MSWindows/Makefile.am3
-rw-r--r--contrib/libcdio/MSWindows/aspi32.c805
-rw-r--r--contrib/libcdio/MSWindows/aspi32.h249
-rw-r--r--contrib/libcdio/MSWindows/win32.c805
-rw-r--r--contrib/libcdio/MSWindows/win32.h170
-rw-r--r--contrib/libcdio/MSWindows/win32_ioctl.c739
-rw-r--r--contrib/libcdio/Makefile.am62
-rw-r--r--contrib/libcdio/_cdio_bsdi.c846
-rw-r--r--contrib/libcdio/_cdio_generic.c426
-rw-r--r--contrib/libcdio/_cdio_linux.c1197
-rw-r--r--contrib/libcdio/_cdio_osx.c1470
-rw-r--r--contrib/libcdio/_cdio_stdio.c222
-rw-r--r--contrib/libcdio/_cdio_stdio.h52
-rw-r--r--contrib/libcdio/_cdio_stream.c200
-rw-r--r--contrib/libcdio/_cdio_stream.h127
-rw-r--r--contrib/libcdio/_cdio_sunos.c917
-rw-r--r--contrib/libcdio/cd_types.c358
-rw-r--r--contrib/libcdio/cdio.c1125
-rw-r--r--contrib/libcdio/cdio/Makefile.am19
-rw-r--r--contrib/libcdio/cdio/bytesex.h196
-rw-r--r--contrib/libcdio/cdio/bytesex_asm.h123
-rw-r--r--contrib/libcdio/cdio/cd_types.h155
-rw-r--r--contrib/libcdio/cdio/cdio.h980
-rw-r--r--contrib/libcdio/cdio/cdtext.h108
-rw-r--r--contrib/libcdio/cdio/ds.h73
-rw-r--r--contrib/libcdio/cdio/dvd.h113
-rw-r--r--contrib/libcdio/cdio/iso9660.h786
-rw-r--r--contrib/libcdio/cdio/logging.h137
-rw-r--r--contrib/libcdio/cdio/scsi_mmc.h415
-rw-r--r--contrib/libcdio/cdio/sector.h326
-rw-r--r--contrib/libcdio/cdio/types.h379
-rw-r--r--contrib/libcdio/cdio/util.h136
-rw-r--r--contrib/libcdio/cdio/version.h10
-rw-r--r--contrib/libcdio/cdio/xa.h151
-rw-r--r--contrib/libcdio/cdio_assert.h59
-rw-r--r--contrib/libcdio/cdio_private.h315
-rw-r--r--contrib/libcdio/cdtext.c228
-rw-r--r--contrib/libcdio/cdtext_private.h119
-rw-r--r--contrib/libcdio/ds.c249
-rw-r--r--contrib/libcdio/generic.h179
-rw-r--r--contrib/libcdio/image.h75
-rw-r--r--contrib/libcdio/image/Makefile.am3
-rw-r--r--contrib/libcdio/image/bincue.c1214
-rw-r--r--contrib/libcdio/image/cdrdao.c1198
-rw-r--r--contrib/libcdio/image/nrg.c1274
-rw-r--r--contrib/libcdio/image/nrg.h115
-rw-r--r--contrib/libcdio/image_common.h225
-rw-r--r--contrib/libcdio/iso9660.c1007
-rw-r--r--contrib/libcdio/iso9660_fs.c1260
-rw-r--r--contrib/libcdio/iso9660_private.h85
-rw-r--r--contrib/libcdio/logging.c142
-rw-r--r--contrib/libcdio/portable.h74
-rw-r--r--contrib/libcdio/scsi_mmc.c589
-rw-r--r--contrib/libcdio/scsi_mmc.h63
-rw-r--r--contrib/libcdio/scsi_mmc_private.h105
-rw-r--r--contrib/libcdio/sector.c289
-rw-r--r--contrib/libcdio/util.c197
-rw-r--r--contrib/libcdio/xa.c136
-rw-r--r--contrib/libvcd/Makefile.am57
-rw-r--r--contrib/libvcd/bitvec.h131
-rw-r--r--contrib/libvcd/bytesex.h145
-rw-r--r--contrib/libvcd/bytesex_asm.h123
-rw-r--r--contrib/libvcd/data_structures.c342
-rw-r--r--contrib/libvcd/data_structures.h92
-rw-r--r--contrib/libvcd/dict.h165
-rw-r--r--contrib/libvcd/directory.c507
-rw-r--r--contrib/libvcd/directory.h64
-rw-r--r--contrib/libvcd/files.c1020
-rw-r--r--contrib/libvcd/image.c108
-rw-r--r--contrib/libvcd/image_bincue.c259
-rw-r--r--contrib/libvcd/image_cdrdao.c313
-rw-r--r--contrib/libvcd/image_nrg.c352
-rw-r--r--contrib/libvcd/image_sink.h80
-rw-r--r--contrib/libvcd/inf.c531
-rw-r--r--contrib/libvcd/info.c2103
-rw-r--r--contrib/libvcd/info_private.c328
-rw-r--r--contrib/libvcd/info_private.h118
-rw-r--r--contrib/libvcd/libvcd/Makefile.am3
-rw-r--r--contrib/libvcd/libvcd/files.h101
-rw-r--r--contrib/libvcd/libvcd/files_private.h562
-rw-r--r--contrib/libvcd/libvcd/inf.h260
-rw-r--r--contrib/libvcd/libvcd/info.h860
-rw-r--r--contrib/libvcd/libvcd/logging.h137
-rw-r--r--contrib/libvcd/libvcd/sector.h118
-rw-r--r--contrib/libvcd/libvcd/types.h97
-rw-r--r--contrib/libvcd/libvcd/version.h11
-rw-r--r--contrib/libvcd/logging.c145
-rw-r--r--contrib/libvcd/mpeg.c1177
-rw-r--r--contrib/libvcd/mpeg.h194
-rw-r--r--contrib/libvcd/mpeg_stream.c487
-rw-r--r--contrib/libvcd/mpeg_stream.h116
-rw-r--r--contrib/libvcd/obj.h202
-rw-r--r--contrib/libvcd/pbc.c880
-rw-r--r--contrib/libvcd/pbc.h157
-rw-r--r--contrib/libvcd/salloc.c212
-rw-r--r--contrib/libvcd/salloc.h56
-rw-r--r--contrib/libvcd/sector.c268
-rw-r--r--contrib/libvcd/sector_private.h1348
-rw-r--r--contrib/libvcd/stream.c276
-rw-r--r--contrib/libvcd/stream.h123
-rw-r--r--contrib/libvcd/stream_stdio.c236
-rw-r--r--contrib/libvcd/stream_stdio.h43
-rw-r--r--contrib/libvcd/util.c179
-rw-r--r--contrib/libvcd/util.h108
-rw-r--r--contrib/libvcd/vcd.c2412
-rw-r--r--contrib/libvcd/vcd.h176
-rw-r--r--contrib/libvcd/vcd_assert.h55
-rw-r--r--contrib/libvcd/vcd_read.c108
-rw-r--r--contrib/libvcd/vcd_read.h36
114 files changed, 42548 insertions, 0 deletions
diff --git a/contrib/Makefile.am b/contrib/Makefile.am
index 03cb3390f..2258d2fd2 100644
--- a/contrib/Makefile.am
+++ b/contrib/Makefile.am
@@ -14,6 +14,10 @@ endif
SUBDIRS += vidix
endif
+if !WITH_EXTERNAL_VCDLIBS
+SUBDIRS += libcdio libvcd
+endif
+
srcdir = $(shell cd @srcdir@; pwd)
ffmpeg_builder = $(srcdir)/ffmpeg-universal.sh
diff --git a/contrib/libcdio/FreeBSD/freebsd.c b/contrib/libcdio/FreeBSD/freebsd.c
new file mode 100644
index 000000000..daea9b3f5
--- /dev/null
+++ b/contrib/libcdio/FreeBSD/freebsd.c
@@ -0,0 +1,638 @@
+/*
+ $Id: freebsd.c,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains FreeBSD-specific code and implements low-level
+ control of the CD drive. Culled initially I think from xine's or
+ mplayer's FreeBSD code with lots of modifications.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: freebsd.c,v 1.1 2005/01/01 02:43:57 rockyb Exp $";
+
+#include "freebsd.h"
+
+#ifdef HAVE_FREEBSD_CDROM
+
+#include <cdio/sector.h>
+
+static access_mode_t
+str_to_access_mode_freebsd(const char *psz_access_mode)
+{
+ const access_mode_t default_access_mode = DEFAULT_FREEBSD_AM;
+
+ if (NULL==psz_access_mode) return default_access_mode;
+
+ if (!strcmp(psz_access_mode, "ioctl"))
+ return _AM_IOCTL;
+ else if (!strcmp(psz_access_mode, "CAM"))
+ return _AM_CAM;
+ else {
+ cdio_warn ("unknown access type: %s. Default ioctl used.",
+ psz_access_mode);
+ return default_access_mode;
+ }
+}
+
+static void
+_free_freebsd (void *obj)
+{
+ _img_private_t *env = obj;
+
+ if (NULL == env) return;
+
+ if (NULL != env->device) free(env->device);
+
+ if (_AM_CAM == env->access_mode)
+ return free_freebsd_cam(env);
+ else
+ return cdio_generic_free(obj);
+}
+
+/* Check a drive to see if it is a CD-ROM
+ Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive
+ and -1 if no device exists .
+*/
+static bool
+cdio_is_cdrom(char *drive, char *mnttype)
+{
+ return cdio_is_cdrom_freebsd_ioctl(drive, mnttype);
+}
+
+/*!
+ Reads nblocks of audio sectors from cd device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_audio_sectors_freebsd (void *user_data, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ return read_audio_sectors_freebsd_ioctl(user_data, data, lsn, nblocks);
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode2_sector_freebsd (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ _img_private_t *env = user_data;
+
+ if ( env->access_mode == _AM_CAM )
+ return read_mode2_sector_freebsd_cam(env, data, lsn, b_form2);
+ else
+ return read_mode2_sector_freebsd_ioctl(env, data, lsn, b_form2);
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode2_sectors_freebsd (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *env = user_data;
+
+ if ( env->access_mode == _AM_CAM && b_form2) {
+ /* We have a routine that covers this case without looping. */
+ return read_mode2_sectors_freebsd_cam(env, data, lsn, nblocks);
+ } else {
+ unsigned int i;
+ unsigned int i_blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ /* For each frame, pick out the data part we need */
+ for (i = 0; i < nblocks; i++) {
+ int retval = _read_mode2_sector_freebsd (env,
+ ((char *)data) +
+ (i_blocksize * i),
+ lsn + i, b_form2);
+ if (retval) return retval;
+ }
+ }
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_stat_size_freebsd (void *obj)
+{
+ _img_private_t *env = obj;
+
+ if (NULL == env) return CDIO_INVALID_LBA;
+
+ if (_AM_CAM == env->access_mode)
+ return stat_size_freebsd_cam(env);
+ else
+ return stat_size_freebsd_ioctl(env);
+}
+
+/*!
+ Set the key "arg" to "value" in source device.
+*/
+static int
+_set_arg_freebsd (void *user_data, const char key[], const char value[])
+{
+ _img_private_t *env = user_data;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (env->gen.source_name);
+
+ env->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ env->access_mode = str_to_access_mode_freebsd(value);
+ if (env->access_mode == _AM_CAM && !env->b_cam_init)
+ return init_freebsd_cam(env) ? 1 : -3;
+ return 0;
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return false if unsuccessful;
+*/
+static bool
+read_toc_freebsd (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+ track_t i, j;
+
+ /* read TOC header */
+ if ( ioctl(p_env->gen.fd, CDIOREADTOCHEADER, &p_env->tochdr) == -1 ) {
+ cdio_warn("error in ioctl(CDIOREADTOCHEADER): %s\n", strerror(errno));
+ return false;
+ }
+
+ p_env->gen.i_first_track = p_env->tochdr.starting_track;
+ p_env->gen.i_tracks = p_env->tochdr.ending_track -
+ p_env->gen.i_first_track + 1;
+
+ j=0;
+ for (i=p_env->gen.i_first_track; i<=p_env->gen.i_tracks; i++, j++) {
+ p_env->tocent[j].track = i;
+ p_env->tocent[j].address_format = CD_LBA_FORMAT;
+
+ if ( ioctl(p_env->gen.fd, CDIOREADTOCENTRY, &(p_env->tocent[j]) ) ) {
+ cdio_warn("%s %d: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for track",
+ i, strerror(errno));
+ return false;
+ }
+ }
+
+ p_env->tocent[j].track = CDIO_CDROM_LEADOUT_TRACK;
+ p_env->tocent[j].address_format = CD_LBA_FORMAT;
+ if ( ioctl(p_env->gen.fd, CDIOREADTOCENTRY, &(p_env->tocent[j]) ) ){
+ cdio_warn("%s: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for leadout track",
+ strerror(errno));
+ return false;
+ }
+
+ p_env->gen.toc_init = true;
+ return true;
+}
+
+/*!
+ Eject media. Return 1 if successful, 0 otherwise.
+ */
+static int
+_eject_media_freebsd (void *user_data)
+{
+ _img_private_t *p_env = user_data;
+
+ return (p_env->access_mode == _AM_IOCTL)
+ ? eject_media_freebsd_ioctl(p_env)
+ : eject_media_freebsd_cam(p_env);
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_get_arg_freebsd (void *user_data, const char key[])
+{
+ _img_private_t *env = user_data;
+
+ if (!strcmp (key, "source")) {
+ return env->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (env->access_mode) {
+ case _AM_IOCTL:
+ return "ioctl";
+ case _AM_CAM:
+ return "CAM";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ FIXME: This is just a guess.
+
+ */
+static char *
+_get_mcn_freebsd (const void *p_user_data) {
+
+ const _img_private_t *p_env = p_user_data;
+
+ return (p_env->access_mode == _AM_IOCTL)
+ ? get_mcn_freebsd_ioctl(p_env)
+ : scsi_mmc_get_mcn(p_env->gen.cdio);
+
+}
+
+static void
+get_drive_cap_freebsd (const void *p_user_data,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap)
+{
+ const _img_private_t *p_env = p_user_data;
+
+ if (p_env->access_mode == _AM_CAM)
+ scsi_mmc_get_drive_cap_generic (p_user_data, p_read_cap, p_write_cap,
+ p_misc_cap);
+
+}
+
+/*!
+ Run a SCSI MMC command.
+
+ p_user_data internal CD structure.
+ i_timeout_ms time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ i_cdb Size of p_cdb
+ p_cdb CDB bytes.
+ e_direction direction the transfer is to go.
+ i_buf Size of buffer
+ p_buf Buffer for data, both sending and receiving
+
+ Return 0 if no error.
+ */
+static int
+run_scsi_cmd_freebsd( const void *p_user_data, unsigned int i_timeout_ms,
+ unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf )
+{
+ const _img_private_t *p_env = p_user_data;
+
+ if (p_env->access_mode == _AM_CAM)
+ return run_scsi_cmd_freebsd_cam( p_user_data, i_timeout_ms, i_cdb, p_cdb,
+ e_direction, i_buf, p_buf );
+ else
+ return 2;
+}
+
+/*!
+ Get format of track.
+
+ FIXME: We're just guessing this from the GNU/Linux code.
+
+*/
+static track_format_t
+_get_track_format_freebsd(void *p_user_data, track_t i_track)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if (!p_env->gen.toc_init) read_toc_freebsd (p_user_data) ;
+
+ if (i_track > TOTAL_TRACKS || i_track == 0)
+ return TRACK_FORMAT_ERROR;
+
+ i_track -= FIRST_TRACK_NUM;
+
+ /* This is pretty much copied from the "badly broken" cdrom_count_tracks
+ in linux/cdrom.c.
+ */
+ if (p_env->tocent[i_track].entry.control & CDIO_CDROM_DATA_TRACK) {
+ if (p_env->tocent[i_track].address_format == CDIO_CDROM_CDI_TRACK)
+ return TRACK_FORMAT_CDI;
+ else if (p_env->tocent[i_track].address_format == CDIO_CDROM_XA_TRACK)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_get_track_green_freebsd(void *user_data, track_t i_track)
+{
+ _img_private_t *p_env = user_data;
+
+ if (i_track == CDIO_CDROM_LEADOUT_TRACK) i_track = TOTAL_TRACKS+1;
+
+ if (i_track > TOTAL_TRACKS+1 || i_track == 0)
+ return false;
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cdinfo for a while.
+ */
+ return ((p_env->tocent[i_track-FIRST_TRACK_NUM].entry.control & 2) != 0);
+}
+
+/*!
+ Return the starting LSN track number
+ i_track in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using i_track LEADOUT_TRACK or the total tracks+1.
+ CDIO_INVALID_LBA is returned if there is no track entry.
+*/
+static lba_t
+_get_track_lba_freebsd(void *user_data, track_t i_track)
+{
+ _img_private_t *p_env = user_data;
+
+ if (!p_env->gen.toc_init) read_toc_freebsd (p_env) ;
+
+ if (i_track == CDIO_CDROM_LEADOUT_TRACK) i_track = TOTAL_TRACKS+1;
+
+ if (i_track > TOTAL_TRACKS+1 || i_track == 0 || !p_env->gen.toc_init) {
+ return CDIO_INVALID_LBA;
+ } else {
+ return cdio_lsn_to_lba(ntohl(p_env->tocent[i_track-FIRST_TRACK_NUM].entry.addr.lba));
+ }
+}
+
+#endif /* HAVE_FREEBSD_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_freebsd (void)
+{
+#ifndef HAVE_FREEBSD_CDROM
+ return NULL;
+#else
+ char drive[40];
+ char **drives = NULL;
+ unsigned int num_drives=0;
+ bool exists=true;
+ char c;
+
+ /* Scan the system for CD-ROM drives.
+ */
+
+#ifdef USE_ETC_FSTAB
+
+ struct fstab *fs;
+ setfsent();
+
+ /* Check what's in /etc/fstab... */
+ while ( (fs = getfsent()) )
+ {
+ if (strncmp(fs->fs_spec, "/dev/sr", 7))
+ cdio_add_device_list(&drives, fs->fs_spec, &num_drives);
+ }
+
+#endif
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+
+ /* Scan SCSI and CAM devices */
+ for ( c='0'; exists && c <='9'; c++ ) {
+ sprintf(drive, "/dev/cd%c%s", c, DEVICE_POSTFIX);
+ exists = cdio_is_cdrom(drive, NULL);
+ if ( exists ) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+ }
+
+ /* Scan are ATAPI devices */
+ for ( c='0'; exists && c <='9'; c++ ) {
+ sprintf(drive, "/dev/acd%c%s", c, DEVICE_POSTFIX);
+ exists = cdio_is_cdrom(drive, NULL);
+ if ( exists ) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+ }
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /*HAVE_FREEBSD_CDROM*/
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char *
+cdio_get_default_device_freebsd()
+{
+#ifndef HAVE_FREEBSD_CDROM
+ return NULL;
+#else
+ char drive[40];
+ bool exists=true;
+ char c;
+
+ /* Scan the system for CD-ROM drives.
+ */
+
+#ifdef USE_ETC_FSTAB
+
+ struct fstab *fs;
+ setfsent();
+
+ /* Check what's in /etc/fstab... */
+ while ( (fs = getfsent()) )
+ {
+ if (strncmp(fs->fs_spec, "/dev/sr", 7))
+ return strdup(fs->fs_spec);
+ }
+
+#endif
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+
+ /* Scan SCSI and CAM devices */
+ for ( c='0'; exists && c <='9'; c++ ) {
+ sprintf(drive, "/dev/cd%c%s", c, DEVICE_POSTFIX);
+ exists = cdio_is_cdrom(drive, NULL);
+ if ( exists ) {
+ return strdup(drive);
+ }
+ }
+
+ /* Scan are ATAPI devices */
+ for ( c='0'; exists && c <='9'; c++ ) {
+ sprintf(drive, "/dev/acd%c%s", c, DEVICE_POSTFIX);
+ exists = cdio_is_cdrom(drive, NULL);
+ if ( exists ) {
+ return strdup(drive);
+ }
+ }
+ return NULL;
+#endif /*HAVE_FREEBSD_CDROM*/
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_freebsd (const char *psz_source_name)
+{
+ return cdio_open_am_freebsd(psz_source_name, NULL);
+}
+
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_am_freebsd (const char *psz_orig_source_name,
+ const char *psz_access_mode)
+{
+
+#ifdef HAVE_FREEBSD_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+ char *psz_source_name;
+
+ cdio_funcs _funcs = {
+ .eject_media = _eject_media_freebsd,
+ .free = _free_freebsd,
+ .get_arg = _get_arg_freebsd,
+ .get_cdtext = get_cdtext_generic,
+ .get_default_device = cdio_get_default_device_freebsd,
+ .get_devices = cdio_get_devices_freebsd,
+ .get_discmode = get_discmode_generic,
+ .get_drive_cap = get_drive_cap_freebsd,
+ .get_first_track_num= get_first_track_num_generic,
+ .get_mcn = _get_mcn_freebsd,
+ .get_num_tracks = get_num_tracks_generic,
+ .get_track_format = _get_track_format_freebsd,
+ .get_track_green = _get_track_green_freebsd,
+ .get_track_lba = _get_track_lba_freebsd,
+ .get_track_msf = NULL,
+ .lseek = cdio_generic_lseek,
+ .read = cdio_generic_read,
+ .read_audio_sectors = _read_audio_sectors_freebsd,
+ .read_mode2_sector = _read_mode2_sector_freebsd,
+ .read_mode2_sectors = _read_mode2_sectors_freebsd,
+ .read_toc = read_toc_freebsd,
+ .run_scsi_mmc_cmd = run_scsi_cmd_freebsd,
+ .set_arg = _set_arg_freebsd,
+ .stat_size = _stat_size_freebsd
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->access_mode = str_to_access_mode_freebsd(psz_access_mode);
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+ _data->gen.toc_init = false;
+ _data->gen.b_cdtext_init = false;
+ _data->gen.b_cdtext_error = false;
+
+ if (NULL == psz_orig_source_name) {
+ psz_source_name=cdio_get_default_device_freebsd();
+ if (NULL == psz_source_name) return NULL;
+ _data->device = psz_source_name;
+ _set_arg_freebsd(_data, "source", psz_source_name);
+ } else {
+ if (cdio_is_device_generic(psz_orig_source_name)) {
+ _set_arg_freebsd(_data, "source", psz_orig_source_name);
+ _data->device = strdup(psz_orig_source_name);
+ } else {
+ /* The below would be okay if all device drivers worked this way. */
+#if 0
+ cdio_info ("source %s is a not a device", psz_orig_source_name);
+#endif
+ return NULL;
+ }
+ }
+
+ ret = cdio_new ((void *)_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (cdio_generic_init(_data))
+ if ( _data->access_mode == _AM_IOCTL ) {
+ return ret;
+ } else {
+ if (init_freebsd_cam(_data))
+ return ret;
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+ }
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_FREEBSD_CDROM */
+
+}
+
+bool
+cdio_have_freebsd (void)
+{
+#ifdef HAVE_FREEBSD_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_FREEBSD_CDROM */
+}
diff --git a/contrib/libcdio/FreeBSD/freebsd.h b/contrib/libcdio/FreeBSD/freebsd.h
new file mode 100644
index 000000000..50894c912
--- /dev/null
+++ b/contrib/libcdio/FreeBSD/freebsd.h
@@ -0,0 +1,232 @@
+/*
+ $Id: freebsd.h,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains FreeBSD-specific code and implements low-level
+ control of the CD drive. Culled initially I think from xine's or
+ mplayer's FreeBSD code with lots of modifications.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/sector.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+
+/*!
+ For ioctl access /dev/acd0c is preferred over /dev/cd0c.
+ For cam access /dev/cd0c is preferred. DEFAULT_CDIO_DEVICE and
+ DEFAULT_FREEBSD_AM should be consistent.
+ */
+
+#ifndef DEFAULT_CDIO_DEVICE
+#define DEFAULT_CDIO_DEVICE "/dev/cd0c"
+#endif
+
+#ifndef DEFUALT_FREEBSD_AM
+#define DEFAULT_FREEBSD_AM _AM_CAM
+#endif
+
+#include <string.h>
+
+#ifdef HAVE_FREEBSD_CDROM
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_CDIO_H
+# include <sys/cdio.h>
+#endif
+
+#ifndef CDIOCREADAUDIO
+struct ioc_read_audio
+{
+ u_char address_format;
+ union msf_lba address;
+ int nframes;
+ u_char* buffer;
+};
+
+#define CDIOCREADAUDIO _IOWR('c',31,struct ioc_read_audio)
+#endif
+
+#include <sys/cdrio.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/param.h> /* for __FreeBSD_version */
+
+#if __FreeBSD_version < 500000
+#define DEVICE_POSTFIX "c"
+#else
+#define DEVICE_POSTFIX ""
+#endif
+
+#define HAVE_FREEBSD_CAM
+#ifdef HAVE_FREEBSD_CAM
+#include <camlib.h>
+
+#include <cam/scsi/scsi_message.h>
+#include <cam/scsi/scsi_pass.h>
+#include <errno.h>
+#define ERRCODE(s) ((((s)[2]&0x0F)<<16)|((s)[12]<<8)|((s)[13]))
+#define EMEDIUMTYPE EINVAL
+#define ENOMEDIUM ENODEV
+#define CREAM_ON_ERRNO(s) do { \
+ switch ((s)[12]) \
+ { case 0x04: errno=EAGAIN; break; \
+ case 0x20: errno=ENODEV; break; \
+ case 0x21: if ((s)[13]==0) errno=ENOSPC; \
+ else errno=EINVAL; \
+ break; \
+ case 0x30: errno=EMEDIUMTYPE; break; \
+ case 0x3A: errno=ENOMEDIUM; break; \
+ } \
+} while(0)
+#endif /*HAVE_FREEBSD_CAM*/
+
+#include <cdio/util.h>
+
+#define TOTAL_TRACKS ( p_env->tochdr.ending_track \
+ - p_env->tochdr.starting_track + 1)
+#define FIRST_TRACK_NUM (p_env->tochdr.starting_track)
+
+typedef enum {
+ _AM_NONE,
+ _AM_IOCTL,
+ _AM_CAM
+} access_mode_t;
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+#ifdef HAVE_FREEBSD_CAM
+ char *device;
+ struct cam_device *cam;
+ union ccb ccb;
+#endif
+
+ access_mode_t access_mode;
+
+ bool b_ioctl_init;
+ bool b_cam_init;
+
+ /* Track information */
+ struct ioc_toc_header tochdr;
+
+ /* Entry info for each track. Add 1 for leadout. */
+ struct ioc_read_toc_single_entry tocent[CDIO_CD_MAX_TRACKS+1];
+
+} _img_private_t;
+
+bool cdio_is_cdrom_freebsd_ioctl(char *drive, char *mnttype);
+
+track_format_t get_track_format_freebsd_ioctl(const _img_private_t *env,
+ track_t i_track);
+bool get_track_green_freebsd_ioctl(const _img_private_t *env,
+ track_t i_track);
+
+int eject_media_freebsd_ioctl (_img_private_t *env);
+int eject_media_freebsd_cam (_img_private_t *env);
+
+void get_drive_cap_freebsd_cam (const _img_private_t *p_env,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap);
+
+char *get_mcn_freebsd_ioctl (const _img_private_t *p_env);
+
+void free_freebsd_cam (void *obj);
+
+/*!
+ Using the ioctl method, r nblocks of audio sectors from cd device
+ into data starting from lsn. Returns 0 if no error.
+ */
+int read_audio_sectors_freebsd_ioctl (_img_private_t *env, void *data,
+ lsn_t lsn, unsigned int nblocks);
+/*!
+ Using the CAM method, reads nblocks of mode2 sectors from
+ cd device using into data starting from lsn. Returns 0 if no
+ error.
+*/
+int read_mode2_sector_freebsd_cam (_img_private_t *env, void *data,
+ lsn_t lsn, bool b_form2);
+
+/*!
+ Using the ioctl method, reads nblocks of mode2 sectors from
+ cd device using into data starting from lsn. Returns 0 if no
+ error.
+*/
+int read_mode2_sector_freebsd_ioctl (_img_private_t *env, void *data,
+ lsn_t lsn, bool b_form2);
+
+/*!
+ Using the CAM method, reads nblocks of mode2 form2 sectors from
+ cd device using into data starting from lsn. Returns 0 if no
+ error.
+
+ Note: if you want form1 sectors, the caller has to pick out the
+ appropriate piece.
+*/
+int read_mode2_sectors_freebsd_cam (_img_private_t *env, void *buf,
+ lsn_t lsn, unsigned int nblocks);
+
+bool read_toc_freebsd_ioctl (_img_private_t *env);
+
+/*!
+ Run a SCSI MMC command.
+
+ p_user_data internal CD structure.
+ i_timeout time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ i_cdb Size of p_cdb
+ p_cdb CDB bytes.
+ e_direction direction the transfer is to go.
+ i_buf Size of buffer
+ p_buf Buffer for data, both sending and receiving
+
+ Return 0 if no error.
+ */
+int run_scsi_cmd_freebsd_cam( const void *p_user_data,
+ unsigned int i_timeout_ms,
+ unsigned int i_cdb,
+ const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf,
+ /*in/out*/ void *p_buf );
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+uint32_t stat_size_freebsd_cam (_img_private_t *env);
+uint32_t stat_size_freebsd_ioctl (_img_private_t *env);
+
+bool init_freebsd_cam (_img_private_t *env);
+void free_freebsd_cam (void *user_data);
+
+#endif /*HAVE_FREEBSD_CDROM*/
diff --git a/contrib/libcdio/FreeBSD/freebsd_cam.c b/contrib/libcdio/FreeBSD/freebsd_cam.c
new file mode 100644
index 000000000..68e38ccae
--- /dev/null
+++ b/contrib/libcdio/FreeBSD/freebsd_cam.c
@@ -0,0 +1,346 @@
+/*
+ $Id: freebsd_cam.c,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains FreeBSD-specific code and implements low-level
+ control of the CD drive via SCSI emulation.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: freebsd_cam.c,v 1.1 2005/01/01 02:43:57 rockyb Exp $";
+
+#ifdef HAVE_FREEBSD_CDROM
+
+#include "freebsd.h"
+#include <cdio/scsi_mmc.h>
+
+/* Default value in seconds we will wait for a command to
+ complete. */
+#define DEFAULT_TIMEOUT_MSECS 10000
+
+/*!
+ Run a SCSI MMC command.
+
+ p_user_data internal CD structure.
+ i_timeout_ms time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ i_cdb Size of p_cdb
+ p_cdb CDB bytes.
+ e_direction direction the transfer is to go.
+ i_buf Size of buffer
+ p_buf Buffer for data, both sending and receiving
+
+ Return 0 if no error.
+ */
+int
+run_scsi_cmd_freebsd_cam( const void *p_user_data, unsigned int i_timeout_ms,
+ unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf )
+{
+ const _img_private_t *p_env = p_user_data;
+ int i_status;
+ int direction = CAM_DEV_QFRZDIS;
+ union ccb ccb;
+
+ if (!p_env || !p_env->cam) return -2;
+
+ memset(&ccb, 0, sizeof(ccb));
+
+ ccb.ccb_h.path_id = p_env->cam->path_id;
+ ccb.ccb_h.target_id = p_env->cam->target_id;
+ ccb.ccb_h.target_lun = p_env->cam->target_lun;
+ ccb.ccb_h.timeout = i_timeout_ms;
+
+ if (!i_cdb)
+ direction |= CAM_DIR_NONE;
+ else
+ direction |= (e_direction == SCSI_MMC_DATA_READ)?CAM_DIR_IN : CAM_DIR_OUT;
+ cam_fill_csio (&(ccb.csio), 1, NULL,
+ direction | CAM_DEV_QFRZDIS, MSG_SIMPLE_Q_TAG, p_buf, i_buf,
+ sizeof(ccb.csio.sense_data), 0, 30*1000);
+
+ memcpy(ccb.csio.cdb_io.cdb_bytes, p_cdb, i_cdb);
+
+ ccb.csio.cdb_len =
+ scsi_mmc_get_cmd_len(ccb.csio.cdb_io.cdb_bytes[0]);
+
+ if ((i_status = cam_send_ccb(p_env->cam, &ccb)) < 0)
+ {
+ cdio_warn ("transport failed: %d", i_status);
+ return -1;
+ }
+ if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
+ {
+ return 0;
+ }
+ errno = EIO;
+ i_status = ERRCODE(((unsigned char *)&ccb.csio.sense_data));
+ if (i_status == 0)
+ i_status = -1;
+ else
+ CREAM_ON_ERRNO(((unsigned char *)&ccb.csio.sense_data));
+ cdio_warn ("transport failed: %d", i_status);
+ return i_status;
+}
+
+bool
+init_freebsd_cam (_img_private_t *p_env)
+{
+ char pass[100];
+
+ p_env->cam=NULL;
+ memset (&p_env->ccb, 0, sizeof(p_env->ccb));
+ p_env->ccb.ccb_h.func_code = XPT_GDEVLIST;
+
+ if (-1 == p_env->gen.fd)
+ p_env->gen.fd = open (p_env->device, O_RDONLY, 0);
+
+ if (p_env->gen.fd < 0)
+ {
+ cdio_warn ("open (%s): %s", p_env->device, strerror (errno));
+ return false;
+ }
+
+ if (ioctl (p_env->gen.fd, CAMGETPASSTHRU, &p_env->ccb) < 0)
+ {
+ cdio_warn ("open: %s", strerror (errno));
+ return false;
+ }
+ sprintf (pass,"/dev/%.15s%u",
+ p_env->ccb.cgdl.periph_name,
+ p_env->ccb.cgdl.unit_number);
+ p_env->cam = cam_open_pass (pass,O_RDWR,NULL);
+ if (!p_env->cam) return false;
+
+ p_env->gen.init = true;
+ p_env->b_cam_init = true;
+ return true;
+}
+
+void
+free_freebsd_cam (void *user_data)
+{
+ _img_private_t *p_env = user_data;
+
+ if (NULL == p_env) return;
+
+ if (p_env->gen.fd > 0)
+ close (p_env->gen.fd);
+ p_env->gen.fd = -1;
+
+ if(p_env->cam)
+ cam_close_device(p_env->cam);
+
+ free (p_env);
+}
+
+static int
+_set_bsize (_img_private_t *p_env, unsigned int bsize)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+
+ struct
+ {
+ uint8_t reserved1;
+ uint8_t medium;
+ uint8_t reserved2;
+ uint8_t block_desc_length;
+ uint8_t density;
+ uint8_t number_of_blocks_hi;
+ uint8_t number_of_blocks_med;
+ uint8_t number_of_blocks_lo;
+ uint8_t reserved3;
+ uint8_t block_length_hi;
+ uint8_t block_length_med;
+ uint8_t block_length_lo;
+ } mh;
+
+ memset (&mh, 0, sizeof (mh));
+ mh.block_desc_length = 0x08;
+ mh.block_length_hi = (bsize >> 16) & 0xff;
+ mh.block_length_med = (bsize >> 8) & 0xff;
+ mh.block_length_lo = (bsize >> 0) & 0xff;
+
+ memset (&cdb, 0, sizeof (cdb));
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_MODE_SELECT_6);
+
+ cdb.field[1] = 1 << 4;
+ cdb.field[4] = 12;
+
+ return run_scsi_cmd_freebsd_cam (p_env, DEFAULT_TIMEOUT_MSECS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_WRITE,
+ sizeof(mh), &mh);
+}
+
+int
+read_mode2_sector_freebsd_cam (_img_private_t *p_env, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ if ( b_form2 )
+ return read_mode2_sectors_freebsd_cam(p_env, data, lsn, 1);
+ else {
+ /* Need to pick out the data portion from a mode2 form2 frame */
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ int retval = read_mode2_sectors_freebsd_cam(p_env, buf, lsn, 1);
+ if ( retval ) return retval;
+ memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ return 0;
+ }
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+int
+read_mode2_sectors_freebsd_cam (_img_private_t *p_env, void *p_buf,
+ lsn_t lsn, unsigned int nblocks)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+
+ bool b_read_10 = false;
+
+ CDIO_MMC_SET_READ_LBA(cdb.field, lsn);
+
+ if (b_read_10) {
+ int retval;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_10);
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, nblocks);
+ if ((retval = _set_bsize (p_env, M2RAW_SECTOR_SIZE)))
+ return retval;
+
+ if ((retval = run_scsi_cmd_freebsd_cam (p_env, 0,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb,
+ SCSI_MMC_DATA_READ,
+ M2RAW_SECTOR_SIZE * nblocks,
+ p_buf)))
+ {
+ _set_bsize (p_env, CDIO_CD_FRAMESIZE);
+ return retval;
+ }
+
+ if ((retval = _set_bsize (p_env, CDIO_CD_FRAMESIZE)))
+ return retval;
+ } else
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_CD);
+ CDIO_MMC_SET_READ_LENGTH24(cdb.field, nblocks);
+ cdb.field[1] = 0; /* sector size mode2 */
+ cdb.field[9] = 0x58; /* 2336 mode2 */
+ return run_scsi_cmd_freebsd_cam (p_env, 0,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb,
+ SCSI_MMC_DATA_READ,
+ M2RAW_SECTOR_SIZE * nblocks, p_buf);
+
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+uint32_t
+stat_size_freebsd_cam (_img_private_t *p_env)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+ uint8_t buf[12] = { 0, };
+
+ uint32_t retval;
+ int i_status;
+
+ /* Operation code */
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_TOC);
+
+ cdb.field[1] = 0; /* lba; msf: 0x2 */
+
+ /* Format */
+ cdb.field[2] = CDIO_MMC_READTOC_FMT_TOC;
+
+ CDIO_MMC_SET_START_TRACK(cdb.field, CDIO_CDROM_LEADOUT_TRACK);
+
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, sizeof(buf));
+
+ p_env->ccb.csio.data_ptr = buf;
+ p_env->ccb.csio.dxfer_len = sizeof (buf);
+
+ i_status = run_scsi_cmd_freebsd_cam(p_env, DEFAULT_TIMEOUT_MSECS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ sizeof(buf), buf);
+ if (0 != i_status)
+ return 0;
+
+ {
+ int i;
+
+ retval = 0;
+ for (i = 8; i < 12; i++)
+ {
+ retval <<= 8;
+ retval += buf[i];
+ }
+ }
+
+ return retval;
+}
+
+/*!
+ * Eject using SCSI MMC commands. Return 0 if successful.
+ */
+int
+eject_media_freebsd_cam (_img_private_t *p_env)
+{
+ int i_status;
+ scsi_mmc_cdb_t cdb = {{0, }};
+ uint8_t buf[1];
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_ALLOW_MEDIUM_REMOVAL);
+
+ i_status = run_scsi_cmd_freebsd_cam (p_env, DEFAULT_TIMEOUT_MSECS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_WRITE, 0, &buf);
+ if (0 != i_status)
+ return i_status;
+
+ cdb.field[4] = 1;
+ i_status = run_scsi_cmd_freebsd_cam (p_env, DEFAULT_TIMEOUT_MSECS,
+ scsi_mmc_get_cmd_len(cdb.field[0]), &cdb,
+ SCSI_MMC_DATA_WRITE, 0, &buf);
+ if (0 != i_status)
+ return i_status;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_START_STOP);
+ cdb.field[4] = 2; /* eject */
+
+ return run_scsi_cmd_freebsd_cam (p_env, DEFAULT_TIMEOUT_MSECS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb,
+ SCSI_MMC_DATA_WRITE, 0, &buf);
+}
+
+#endif /* HAVE_FREEBSD_CDROM */
diff --git a/contrib/libcdio/FreeBSD/freebsd_ioctl.c b/contrib/libcdio/FreeBSD/freebsd_ioctl.c
new file mode 100644
index 000000000..be9835c95
--- /dev/null
+++ b/contrib/libcdio/FreeBSD/freebsd_ioctl.c
@@ -0,0 +1,267 @@
+/*
+ $Id: freebsd_ioctl.c,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains FreeBSD-specific code and implements low-level
+ control of the CD drive. Culled initially I think from xine's or
+ mplayer's FreeBSD code with lots of modifications.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: freebsd_ioctl.c,v 1.1 2005/01/01 02:43:57 rockyb Exp $";
+
+#ifdef HAVE_FREEBSD_CDROM
+
+#include "freebsd.h"
+
+/* Check a drive to see if it is a CD-ROM
+ Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive
+ and -1 if no device exists .
+*/
+bool
+cdio_is_cdrom_freebsd_ioctl(char *drive, char *mnttype)
+{
+ bool is_cd=false;
+ int cdfd;
+ struct ioc_toc_header tochdr;
+
+ /* If it doesn't exist, return -1 */
+ if ( !cdio_is_device_quiet_generic(drive) ) {
+ return(false);
+ }
+
+ /* If it does exist, verify that it's an available CD-ROM */
+ cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
+
+ /* Should we want to test the condition in more detail:
+ ENOENT is the error for /dev/xxxxx does not exist;
+ ENODEV means there's no drive present. */
+
+ if ( cdfd >= 0 ) {
+ if ( ioctl(cdfd, CDIOREADTOCHEADER, &tochdr) != -1 ) {
+ is_cd = true;
+ }
+ close(cdfd);
+ }
+ /* Even if we can't read it, it might be mounted */
+ else if ( mnttype && (strcmp(mnttype, "iso9660") == 0) ) {
+ is_cd = true;
+ }
+ return(is_cd);
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting from lsn.
+ Returns 0 if no error.
+ */
+int
+read_audio_sectors_freebsd_ioctl (_img_private_t *_obj, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ unsigned char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ struct ioc_read_audio cdda;
+
+ cdda.address.lba = lsn;
+ cdda.buffer = buf;
+ cdda.nframes = nblocks;
+ cdda.address_format = CDIO_CDROM_LBA;
+
+ /* read a frame */
+ if(ioctl(_obj->gen.fd, CDIOCREADAUDIO, &cdda) < 0) {
+ perror("CDIOCREADAUDIO");
+ return 1;
+ }
+ memcpy (data, buf, CDIO_CD_FRAMESIZE_RAW);
+
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+int
+read_mode2_sector_freebsd_ioctl (_img_private_t *env, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ int retval;
+
+ if ( !b_form2 )
+ return cdio_generic_read_form1_sector (env, buf, lsn);
+
+ if ( (retval = read_audio_sectors_freebsd_ioctl (env, buf, lsn, 1)) )
+ return retval;
+
+ memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, M2RAW_SECTOR_SIZE);
+
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+uint32_t
+stat_size_freebsd_ioctl (_img_private_t *_obj)
+{
+ struct ioc_read_toc_single_entry tocent;
+ uint32_t size;
+
+ tocent.track = CDIO_CDROM_LEADOUT_TRACK;
+ tocent.address_format = CDIO_CDROM_LBA;
+ if (ioctl (_obj->gen.fd, CDIOREADTOCENTRY, &tocent) == -1)
+ {
+ perror ("ioctl(CDROMREADTOCENTRY)");
+ exit (EXIT_FAILURE);
+ }
+
+ size = tocent.entry.addr.lba;
+
+ return size;
+}
+
+/*!
+ Eject media. Return 1 if successful, 0 otherwise.
+ */
+int
+eject_media_freebsd_ioctl (_img_private_t *env)
+{
+ _img_private_t *_obj = env;
+ int ret=2;
+ int fd;
+
+ if ((fd = open(_obj->gen.source_name, O_RDONLY|O_NONBLOCK)) > -1) {
+ ret = 1;
+ if (ioctl(fd, CDIOCALLOW) == -1) {
+ cdio_warn("ioctl(fd, CDIOCALLOW) failed: %s\n", strerror(errno));
+ } else if (ioctl(fd, CDIOCEJECT) == -1) {
+ cdio_warn("ioctl(CDIOCEJECT) failed: %s\n", strerror(errno));
+ } else {
+ ret = 0;
+ }
+ close(fd);
+ }
+
+ return ret;
+}
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ FIXME: This is just a guess.
+
+ */
+char *
+get_mcn_freebsd_ioctl (const _img_private_t *env) {
+
+ struct ioc_read_subchannel subchannel;
+ struct cd_sub_channel_info subchannel_info;
+
+ subchannel.address_format = CDIO_CDROM_MSF;
+ subchannel.data_format = CDIO_SUBCHANNEL_MEDIA_CATALOG;
+ subchannel.track = 0;
+ subchannel.data_len = sizeof(subchannel_info);
+ subchannel.data = &subchannel_info;
+
+ if(ioctl(env->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
+ perror("CDIOCREADSUBCHANNEL");
+ return NULL;
+ }
+
+ /* Probably need a loop over tracks rather than give up if we
+ can't find in track 0.
+ */
+ if (subchannel_info.what.media_catalog.mc_valid)
+ return strdup(subchannel_info.what.media_catalog.mc_number);
+ else
+ return NULL;
+}
+
+/*!
+ Get format of track.
+
+ FIXME: We're just guessing this from the GNU/Linux code.
+
+*/
+track_format_t
+get_track_format_freebsd_ioctl(const _img_private_t *env, track_t i_track)
+{
+ struct ioc_read_subchannel subchannel;
+ struct cd_sub_channel_info subchannel_info;
+
+ subchannel.address_format = CDIO_CDROM_LBA;
+ subchannel.data_format = CDIO_SUBCHANNEL_CURRENT_POSITION;
+ subchannel.track = i_track;
+ subchannel.data_len = 1;
+ subchannel.data = &subchannel_info;
+
+ if(ioctl(env->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
+ perror("CDIOCREADSUBCHANNEL");
+ return 1;
+ }
+
+ if (subchannel_info.what.position.control == 0x04) {
+ if (subchannel_info.what.position.data_format == 0x10)
+ return TRACK_FORMAT_CDI;
+ else if (subchannel_info.what.position.data_format == 0x20)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+bool
+get_track_green_freebsd_ioctl(const _img_private_t *env, track_t i_track)
+{
+ struct ioc_read_subchannel subchannel;
+ struct cd_sub_channel_info subchannel_info;
+
+ subchannel.address_format = CDIO_CDROM_LBA;
+ subchannel.data_format = CDIO_SUBCHANNEL_CURRENT_POSITION;
+ subchannel.track = i_track;
+ subchannel.data_len = 1;
+ subchannel.data = &subchannel_info;
+
+ if(ioctl(env->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
+ perror("CDIOCREADSUBCHANNEL");
+ return 1;
+ }
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cdinfo for a while.
+ */
+ return (subchannel_info.what.position.control & 2) != 0;
+}
+
+#endif /* HAVE_FREEBSD_CDROM */
diff --git a/contrib/libcdio/MSWindows/Makefile.am b/contrib/libcdio/MSWindows/Makefile.am
new file mode 100644
index 000000000..61089cf69
--- /dev/null
+++ b/contrib/libcdio/MSWindows/Makefile.am
@@ -0,0 +1,3 @@
+include $(top_srcdir)/misc/Makefile.common
+
+EXTRA_DIST = aspi32.c aspi32.h win32.c win32.h win32_ioctl.c
diff --git a/contrib/libcdio/MSWindows/aspi32.c b/contrib/libcdio/MSWindows/aspi32.c
new file mode 100644
index 000000000..238a4b4e9
--- /dev/null
+++ b/contrib/libcdio/MSWindows/aspi32.c
@@ -0,0 +1,805 @@
+/*
+ $Id: aspi32.c,v 1.2 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains Win32-specific code and implements low-level
+ control of the CD drive via the ASPI API.
+ Inspired by vlc's cdrom.h code
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: aspi32.c,v 1.2 2005/01/01 02:43:58 rockyb Exp $";
+
+#include <cdio/cdio.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include <cdio/scsi_mmc.h>
+#include "cdio_assert.h"
+
+#include <string.h>
+
+#ifdef HAVE_WIN32_CDROM
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <windows.h>
+#include "win32.h"
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include "aspi32.h"
+#include "cdtext_private.h"
+
+/* Amount of time we are willing to wait for an operation to complete.
+ 10 seconds?
+*/
+#define OP_TIMEOUT_MS 10000
+
+static const
+char *aspierror(int nErrorCode)
+{
+ switch (nErrorCode)
+ {
+ case SS_PENDING:
+ return "SRB being processed";
+ break;
+ case SS_COMP:
+ return "SRB completed without error";
+ break;
+ case SS_ABORTED:
+ return "SRB aborted";
+ break;
+ case SS_ABORT_FAIL:
+ return "Unable to abort SRB";
+ break;
+ case SS_ERR:
+ return "SRB completed with error";
+ break;
+ case SS_INVALID_CMD:
+ return "Invalid ASPI command";
+ break;
+ case SS_INVALID_HA:
+ return "Invalid host adapter number";
+ break;
+ case SS_NO_DEVICE:
+ return "SCSI device not installed";
+ break;
+ case SS_INVALID_SRB:
+ return "Invalid parameter set in SRB";
+ break;
+ case SS_OLD_MANAGER:
+ return "ASPI manager doesn't support";
+ break;
+ case SS_ILLEGAL_MODE:
+ return "Unsupported MS Windows mode";
+ break;
+ case SS_NO_ASPI:
+ return "No ASPI managers";
+ break;
+ case SS_FAILED_INIT:
+ return "ASPI for windows failed init";
+ break;
+ case SS_ASPI_IS_BUSY:
+ return "No resources available to execute command.";
+ break;
+ case SS_BUFFER_TOO_BIG:
+ return "Buffer size is too big to handle.";
+ break;
+ case SS_MISMATCHED_COMPONENTS:
+ return "The DLLs/EXEs of ASPI don't version check";
+ break;
+ case SS_NO_ADAPTERS:
+ return "No host adapters found";
+ break;
+ case SS_INSUFFICIENT_RESOURCES:
+ return "Couldn't allocate resources needed to init";
+ break;
+ case SS_ASPI_IS_SHUTDOWN:
+ return "Call came to ASPI after PROCESS_DETACH";
+ break;
+ case SS_BAD_INSTALL:
+ return "The DLL or other components are installed wrong.";
+ break;
+ default:
+ return "Unknown ASPI error.";
+ }
+}
+
+/* General ioctl() CD-ROM command function */
+static bool
+mciSendCommand_aspi(int id, UINT msg, DWORD flags, void *arg)
+{
+ MCIERROR mci_error;
+
+ mci_error = mciSendCommand(id, msg, flags, (DWORD)arg);
+ if ( mci_error ) {
+ char error[256];
+
+ mciGetErrorString(mci_error, error, 256);
+ cdio_warn("mciSendCommand() error: %s", error);
+ }
+ return(mci_error == 0);
+}
+
+/*
+ See if the ASPI DLL is loadable. If so pointers are returned
+ and we return true. Return false if there was a problem.
+ */
+static bool
+have_aspi( HMODULE *hASPI,
+ long (**lpGetSupport)( void ),
+ long (**lpSendCommand)( void* ) )
+{
+ /* check if aspi is available */
+ *hASPI = LoadLibrary( "wnaspi32.dll" );
+
+ if( *hASPI == NULL ) {
+ cdio_debug("Unable to load ASPI DLL");
+ return false;
+ }
+
+ (FARPROC) *lpGetSupport = GetProcAddress( *hASPI,
+ "GetASPI32SupportInfo" );
+ (FARPROC) *lpSendCommand = GetProcAddress( *hASPI,
+ "SendASPI32Command" );
+
+ /* make sure that we've got both function addresses */
+ if( *lpGetSupport == NULL || *lpSendCommand == NULL ) {
+ cdio_debug("Unable to get ASPI function pointers");
+ FreeLibrary( *hASPI );
+ return false;
+ }
+
+ return true;
+}
+
+/*!
+ Get disc type associated with cd object.
+*/
+discmode_t
+get_discmode_aspi (_img_private_t *p_env)
+{
+ track_t i_track;
+ discmode_t discmode=CDIO_DISC_MODE_NO_INFO;
+
+ /* See if this is a DVD. */
+ cdio_dvd_struct_t dvd; /* DVD READ STRUCT for layer 0. */
+
+ dvd.physical.type = CDIO_DVD_STRUCT_PHYSICAL;
+ dvd.physical.layer_num = 0;
+ if (0 == scsi_mmc_get_dvd_struct_physical_private (p_env,
+ &run_scsi_cmd_aspi,
+ &dvd)) {
+ switch(dvd.physical.layer[0].book_type) {
+ case CDIO_DVD_BOOK_DVD_ROM: return CDIO_DISC_MODE_DVD_ROM;
+ case CDIO_DVD_BOOK_DVD_RAM: return CDIO_DISC_MODE_DVD_RAM;
+ case CDIO_DVD_BOOK_DVD_R: return CDIO_DISC_MODE_DVD_R;
+ case CDIO_DVD_BOOK_DVD_RW: return CDIO_DISC_MODE_DVD_RW;
+ case CDIO_DVD_BOOK_DVD_PR: return CDIO_DISC_MODE_DVD_PR;
+ case CDIO_DVD_BOOK_DVD_PRW: return CDIO_DISC_MODE_DVD_PRW;
+ default: return CDIO_DISC_MODE_DVD_OTHER;
+ }
+ }
+
+ if (!p_env->gen.toc_init)
+ read_toc_aspi (p_env);
+
+ if (!p_env->gen.toc_init)
+ return CDIO_DISC_MODE_NO_INFO;
+
+ for (i_track = p_env->gen.i_first_track;
+ i_track < p_env->gen.i_first_track + p_env->gen.i_tracks ;
+ i_track ++) {
+ track_format_t track_fmt=get_track_format_aspi(p_env, i_track);
+
+ switch(track_fmt) {
+ case TRACK_FORMAT_AUDIO:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_DA;
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_XA:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_DATA:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_ERROR:
+ default:
+ discmode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ return discmode;
+}
+
+const char *
+is_cdrom_aspi(const char drive_letter)
+{
+ static char psz_win32_drive[7];
+ HMODULE hASPI = NULL;
+ long (*lpGetSupport)( void ) = NULL;
+ long (*lpSendCommand)( void* ) = NULL;
+ DWORD dwSupportInfo;
+ int i_adapter, i_hostadapters;
+ char c_drive;
+ int i_rc;
+
+ if ( !have_aspi(&hASPI, &lpGetSupport, &lpSendCommand) )
+ return NULL;
+
+ /* ASPI support seems to be there. */
+
+ dwSupportInfo = lpGetSupport();
+
+ i_rc = HIBYTE( LOWORD ( dwSupportInfo ) );
+
+ if( SS_COMP != i_rc ) {
+ cdio_debug("ASPI: %s", aspierror(i_rc));
+ FreeLibrary( hASPI );
+ return NULL;
+ }
+
+ i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
+ if( i_hostadapters == 0 ) {
+ FreeLibrary( hASPI );
+ return NULL;
+ }
+
+ c_drive = toupper(drive_letter) - 'A';
+
+ for( i_adapter = 0; i_adapter < i_hostadapters; i_adapter++ ) {
+ struct SRB_GetDiskInfo srbDiskInfo;
+ int i_target;
+ SRB_HAInquiry srbInquiry;
+
+ srbInquiry.SRB_Cmd = SC_HA_INQUIRY;
+ srbInquiry.SRB_HaId = i_adapter;
+
+ lpSendCommand( (void*) &srbInquiry );
+
+ if( srbInquiry.SRB_Status != SS_COMP ) continue;
+ if( !srbInquiry.HA_Unique[3]) srbInquiry.HA_Unique[3]=8;
+
+ for(i_target=0; i_target < srbInquiry.HA_Unique[3]; i_target++)
+ {
+ int i_lun;
+ for( i_lun=0; i_lun<8; i_lun++)
+ {
+ srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
+ srbDiskInfo.SRB_Flags = 0;
+ srbDiskInfo.SRB_Hdr_Rsvd = 0;
+ srbDiskInfo.SRB_HaId = i_adapter;
+ srbDiskInfo.SRB_Target = i_target;
+ srbDiskInfo.SRB_Lun = i_lun;
+
+ lpSendCommand( (void*) &srbDiskInfo );
+
+ if( (srbDiskInfo.SRB_Status == SS_COMP) &&
+ (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) ) {
+ /* Make sure this is a CD-ROM device. */
+ struct SRB_GDEVBlock srbGDEVBlock;
+
+ memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
+ srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE;
+ srbDiskInfo.SRB_HaId = i_adapter;
+ srbGDEVBlock.SRB_Target = i_target;
+ srbGDEVBlock.SRB_Lun = i_lun;
+
+ lpSendCommand( (void*) &srbGDEVBlock );
+
+ if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
+ ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) ) {
+ sprintf( psz_win32_drive, "%c:", drive_letter );
+ FreeLibrary( hASPI );
+ return(psz_win32_drive);
+ }
+ }
+ }
+ }
+ }
+ FreeLibrary( hASPI );
+ return NULL;
+}
+
+/*!
+ Initialize CD device.
+ */
+bool
+init_aspi (_img_private_t *env)
+{
+ HMODULE hASPI = NULL;
+ long (*lpGetSupport)( void ) = NULL;
+ long (*lpSendCommand)( void* ) = NULL;
+ DWORD dwSupportInfo;
+ int i_adapter, i_hostadapters;
+ char c_drive;
+ int i_rc;
+
+ if (2 == strlen(env->gen.source_name) && isalpha(env->gen.source_name[0]) )
+ {
+ c_drive = env->gen.source_name[0];
+ } else if ( 6 == strlen(env->gen.source_name)
+ && isalpha(env->gen.source_name[4] )) {
+ c_drive = env->gen.source_name[4];
+ } else {
+ c_drive = 'C';
+ }
+
+ if ( !have_aspi(&hASPI, &lpGetSupport, &lpSendCommand) )
+ return false;
+
+ /* ASPI support seems to be there. */
+
+ dwSupportInfo = lpGetSupport();
+
+ i_rc = HIBYTE( LOWORD ( dwSupportInfo ) );
+
+ if( SS_COMP != i_rc ) {
+ cdio_info("ASPI: %s", aspierror(i_rc));
+ FreeLibrary( hASPI );
+ return false;
+ }
+
+ i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
+ if( i_hostadapters == 0 ) {
+ FreeLibrary( hASPI );
+ return false;
+ }
+
+ c_drive = toupper(c_drive) - 'A';
+
+ for( i_adapter = 0; i_adapter < i_hostadapters; i_adapter++ ) {
+ struct SRB_GetDiskInfo srbDiskInfo;
+ int i_target;
+ SRB_HAInquiry srbInquiry;
+
+ srbInquiry.SRB_Cmd = SC_HA_INQUIRY;
+ srbInquiry.SRB_HaId = i_adapter;
+
+ lpSendCommand( (void*) &srbInquiry );
+
+ if( srbInquiry.SRB_Status != SS_COMP ) continue;
+ if( !srbInquiry.HA_Unique[3]) srbInquiry.HA_Unique[3]=8;
+
+ for(i_target=0; i_target < srbInquiry.HA_Unique[3]; i_target++)
+ {
+ int i_lun;
+ for (i_lun = 0; i_lun < 8; i_lun++ ) {
+ srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
+ srbDiskInfo.SRB_Flags = 0;
+ srbDiskInfo.SRB_Hdr_Rsvd = 0;
+ srbDiskInfo.SRB_HaId = i_adapter;
+ srbDiskInfo.SRB_Target = i_target;
+ srbDiskInfo.SRB_Lun = i_lun;
+
+ lpSendCommand( (void*) &srbDiskInfo );
+
+ if( (srbDiskInfo.SRB_Status == SS_COMP) ) {
+
+ if (srbDiskInfo.SRB_Int13HDriveInfo != c_drive)
+ {
+ continue;
+ } else {
+ /* Make sure this is a CD-ROM device. */
+ struct SRB_GDEVBlock srbGDEVBlock;
+
+ memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
+ srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE;
+ srbGDEVBlock.SRB_HaId = i_adapter;
+ srbGDEVBlock.SRB_Target = i_target;
+
+ lpSendCommand( (void*) &srbGDEVBlock );
+
+ if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
+ ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) ) {
+ env->i_sid = MAKEWORD( i_adapter, i_target );
+ env->hASPI = (long)hASPI;
+ env->lpSendCommand = lpSendCommand;
+ env->b_aspi_init = true;
+ env->i_lun = i_lun;
+ cdio_debug("Using ASPI layer");
+
+ return true;
+ } else {
+ FreeLibrary( hASPI );
+ cdio_debug( "%c: is not a CD-ROM drive",
+ env->gen.source_name[0] );
+ return false;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ FreeLibrary( hASPI );
+ cdio_debug( "Unable to get HaId and target (ASPI)" );
+ return false;
+}
+
+/*!
+ Run a SCSI MMC command.
+
+ env private CD structure
+ i_timeout_ms time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ p_buf Buffer for data, both sending and receiving
+ i_buf Size of buffer
+ e_direction direction the transfer is to go.
+ cdb CDB bytes. All values that are needed should be set on
+ input. We'll figure out what the right CDB length should be.
+
+ We return 0 if command completed successfully.
+ */
+int
+run_scsi_cmd_aspi( const void *p_user_data, unsigned int i_timeout_ms,
+ unsigned int i_cdb, const scsi_mmc_cdb_t * p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf )
+{
+ const _img_private_t *p_env = p_user_data;
+ HANDLE hEvent;
+ struct SRB_ExecSCSICmd ssc;
+
+ /* Create the transfer completion event */
+ hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
+ if( hEvent == NULL ) {
+ cdio_info("CreateEvent failed");
+ return 1;
+ }
+
+ memset( &ssc, 0, sizeof( ssc ) );
+
+ ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
+
+ ssc.SRB_Flags = SCSI_MMC_DATA_READ == e_direction ?
+ SRB_DIR_IN | SRB_EVENT_NOTIFY : SRB_DIR_OUT | SRB_EVENT_NOTIFY;
+
+ ssc.SRB_HaId = LOBYTE( p_env->i_sid );
+ ssc.SRB_Target = HIBYTE( p_env->i_sid );
+ ssc.SRB_Lun = p_env->i_lun;
+ ssc.SRB_SenseLen = SENSE_LEN;
+
+ ssc.SRB_PostProc = (LPVOID) hEvent;
+ ssc.SRB_CDBLen = i_cdb;
+
+ /* Result buffer */
+ ssc.SRB_BufPointer = p_buf;
+ ssc.SRB_BufLen = i_buf;
+
+ memcpy( ssc.CDBByte, p_cdb, i_cdb );
+
+ ResetEvent( hEvent );
+ p_env->lpSendCommand( (void*) &ssc );
+
+ /* If the command has still not been processed, wait until it's
+ * finished */
+ if( ssc.SRB_Status == SS_PENDING ) {
+ WaitForSingleObject( hEvent, msecs2secs(i_timeout_ms) );
+ }
+ CloseHandle( hEvent );
+
+ /* check that the transfer went as planned */
+ if( ssc.SRB_Status != SS_COMP ) {
+ cdio_info("ASPI: %s", aspierror(ssc.SRB_Status));
+ return 2;
+ }
+
+ return 0;
+}
+
+
+/*!
+ Reads nblocks sectors from cd device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+read_sectors_aspi (const _img_private_t *env, void *data, lsn_t lsn,
+ int sector_type, unsigned int nblocks)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+ unsigned int i_buf;
+
+ int sync = 0;
+ int header_code = 2;
+ int i_user_data = 1;
+ int edc_ecc = 0;
+ int error_field = 0;
+
+#if 0
+ sector_type = 0; /*all types */
+#endif
+
+ /* Set up passthrough command */
+ CDIO_MMC_SET_COMMAND (cdb.field, CDIO_MMC_GPCMD_READ_CD);
+ CDIO_MMC_SET_READ_TYPE (cdb.field, sector_type);
+ CDIO_MMC_SET_READ_LBA (cdb.field, lsn);
+ CDIO_MMC_SET_READ_LENGTH24(cdb.field, nblocks);
+
+#if 1
+ cdb.field[ 9 ] = (sync << 7) |
+ (header_code << 5) |
+ (i_user_data << 4) |
+ (edc_ecc << 3) |
+ (error_field << 1);
+ /* ssc.CDBByte[ 9 ] = READ_CD_USERDATA_MODE2; */
+#else
+ CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(cmd,
+ CDIO_MMC_MCSB_ALL_HEADERS);
+#endif
+
+ switch (sector_type) {
+ case CDIO_MMC_READ_TYPE_ANY:
+ case CDIO_MMC_READ_TYPE_CDDA:
+ i_buf = CDIO_CD_FRAMESIZE_RAW;
+ break;
+ case CDIO_MMC_READ_TYPE_M2F1:
+ i_buf = CDIO_CD_FRAMESIZE;
+ break;
+ case CDIO_MMC_READ_TYPE_M2F2:
+ i_buf = 2324;
+ break;
+ case CDIO_MMC_READ_TYPE_MODE1:
+ i_buf = CDIO_CD_FRAMESIZE;
+ break;
+ default:
+ i_buf = CDIO_CD_FRAMESIZE_RAW;
+ }
+
+ return run_scsi_cmd_aspi(env, OP_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ, i_buf*nblocks, data);
+}
+
+/*!
+ Reads an audio device into data starting from lsn.
+ Returns 0 if no error.
+ */
+int
+read_audio_sectors_aspi (_img_private_t *env, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ if (read_sectors_aspi(env, data, lsn, CDIO_MMC_READ_TYPE_CDDA, 1)) {
+ return read_sectors_aspi(env, data, lsn, CDIO_MMC_READ_TYPE_ANY, 1);
+ }
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+int
+read_mode2_sector_aspi (const _img_private_t *env, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ return read_sectors_aspi(env, data, lsn, b_form2
+ ? CDIO_MMC_READ_TYPE_M2F2
+ : CDIO_MMC_READ_TYPE_M2F1,
+ 1);
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+int
+read_mode1_sector_aspi (const _img_private_t *env, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ return read_sectors_aspi(env, data, lsn, CDIO_MMC_READ_TYPE_MODE1, 1);
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return true if successful or false if an error.
+*/
+bool
+read_toc_aspi (_img_private_t *p_env)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+ unsigned char tocheader[ 4 ];
+ int i_status;
+
+ /* Operation code */
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_TOC);
+
+ /* Format */
+ cdb.field[ 2 ] = CDIO_MMC_READTOC_FMT_TOC;
+
+ /* Starting track */
+ CDIO_MMC_SET_START_TRACK(cdb.field, 0);
+
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, sizeof(tocheader));
+
+ i_status = run_scsi_cmd_aspi (p_env, OP_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ sizeof(tocheader), &tocheader);
+
+ if (0 != i_status) return false;
+
+ p_env->gen.i_first_track = tocheader[2];
+ p_env->gen.i_tracks = tocheader[3] - tocheader[2] + 1;
+
+ {
+ int i, i_toclength;
+ unsigned char *p_fulltoc;
+
+ i_toclength = 4 /* header */ + tocheader[0] +
+ ((unsigned int) tocheader[1] << 8);
+
+ p_fulltoc = malloc( i_toclength );
+
+ if( p_fulltoc == NULL ) {
+ cdio_error( "out of memory" );
+ return false;
+ }
+
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, i_toclength);
+
+ i_status = run_scsi_cmd_aspi (p_env, OP_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ i_toclength, p_fulltoc);
+ if( 0 != i_status ) {
+ p_env->gen.i_tracks = 0;
+ }
+
+ for( i = 0 ; i <= p_env->gen.i_tracks ; i++ ) {
+ int i_index = 8 + 8 * i;
+ p_env->tocent[ i ].start_lsn = ((int)p_fulltoc[ i_index ] << 24) +
+ ((int)p_fulltoc[ i_index+1 ] << 16) +
+ ((int)p_fulltoc[ i_index+2 ] << 8) +
+ (int)p_fulltoc[ i_index+3 ];
+ p_env->tocent[ i ].Control = (UCHAR)p_fulltoc[ 1 + 8 * i ];
+
+ cdio_debug( "p_sectors: %i %lu",
+ i, (unsigned long int) p_env->tocent[i].start_lsn );
+ }
+
+ free( p_fulltoc );
+ }
+
+ p_env->gen.toc_init = true;
+ return true;
+}
+
+/* Eject media will eventually get removed from _cdio_win32.c */
+#if 0
+/*!
+ Eject media. Return 1 if successful, 0 otherwise.
+ */
+int
+wnaspi32_eject_media (void *user_data) {
+
+ _img_private_t *env = user_data;
+
+
+ MCI_OPEN_PARMS op;
+ MCI_STATUS_PARMS st;
+ DWORD i_flags;
+ char psz_drive[4];
+ int ret;
+
+ memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
+ op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
+ strcpy( psz_drive, "X:" );
+ psz_drive[0] = env->gen.source_name[0];
+ op.lpstrElementName = psz_drive;
+
+ /* Set the flags for the device type */
+ i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
+ MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
+
+ if( mciSendCommand_aspi( 0, MCI_OPEN, i_flags, &op ) ) {
+ st.dwItem = MCI_STATUS_READY;
+ /* Eject disc */
+ ret = mciSendCommand_aspi( op.wDeviceID, MCI_SET,
+ MCI_SET_DOOR_OPEN, 0 ) != 0;
+ /* Release access to the device */
+ mciSendCommand_aspi( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
+ } else
+ ret = 0;
+
+ return ret;
+}
+#endif
+
+/*!
+ Get format of track.
+*/
+track_format_t
+get_track_format_aspi(const _img_private_t *p_env, track_t track_num)
+{
+ MCI_OPEN_PARMS op;
+ MCI_STATUS_PARMS st;
+ DWORD i_flags;
+ int ret;
+
+ memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
+ op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
+ op.lpstrElementName = p_env->gen.source_name;
+
+ /* Set the flags for the device type */
+ i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
+ MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
+
+ if( mciSendCommand_aspi( 0, MCI_OPEN, i_flags, &op ) ) {
+ st.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
+ st.dwTrack = track_num;
+ i_flags = MCI_TRACK | MCI_STATUS_ITEM ;
+ ret = mciSendCommand_aspi( op.wDeviceID, MCI_STATUS, i_flags, &st );
+
+ /* Release access to the device */
+ mciSendCommand_aspi( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
+
+ switch(st.dwReturn) {
+ case MCI_CDA_TRACK_AUDIO:
+ return TRACK_FORMAT_AUDIO;
+ case MCI_CDA_TRACK_OTHER:
+ return TRACK_FORMAT_DATA;
+ default:
+ return TRACK_FORMAT_XA;
+ }
+ }
+ return TRACK_FORMAT_ERROR;
+}
+
+#endif /* HAVE_WIN32_CDROM */
diff --git a/contrib/libcdio/MSWindows/aspi32.h b/contrib/libcdio/MSWindows/aspi32.h
new file mode 100644
index 000000000..8742c0794
--- /dev/null
+++ b/contrib/libcdio/MSWindows/aspi32.h
@@ -0,0 +1,249 @@
+/* Win32 aspi specific */
+/*
+ $Id: aspi32.h,v 1.2 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#define ASPI_HAID 0
+#define ASPI_TARGET 0
+#define DTYPE_CDROM 0x05
+
+#define SENSE_LEN 0x0E
+#define SC_HA_INQUIRY 0x00
+#define SC_GET_DEV_TYPE 0x01
+#define SC_EXEC_SCSI_CMD 0x02
+#define SC_GET_DISK_INFO 0x06
+
+//*****************************************************************************
+// %%% SRB Status %%%
+//*****************************************************************************
+
+#define SS_PENDING 0x00 // SRB being processed
+#define SS_COMP 0x01 // SRB completed without error
+#define SS_ABORTED 0x02 // SRB aborted
+#define SS_ABORT_FAIL 0x03 // Unable to abort SRB
+#define SS_ERR 0x04 // SRB completed with error
+
+#define SS_INVALID_CMD 0x80 // Invalid ASPI command
+#define SS_INVALID_HA 0x81 // Invalid host adapter number
+#define SS_NO_DEVICE 0x82 // SCSI device not installed
+
+#define SS_INVALID_SRB 0xE0 // Invalid parameter set in SRB
+#define SS_OLD_MANAGER 0xE1 // ASPI manager doesn't support Windows
+#define SS_BUFFER_ALIGN 0xE1 // Buffer not aligned (replaces
+ // OLD_MANAGER in Win32)
+#define SS_ILLEGAL_MODE 0xE2 // Unsupported Windows mode
+#define SS_NO_ASPI 0xE3 // No ASPI managers resident
+#define SS_FAILED_INIT 0xE4 // ASPI for windows failed init
+#define SS_ASPI_IS_BUSY 0xE5 // No resources available to execute
+ // cmd
+#define SS_BUFFER_TOO_BIG 0xE6 // Buffer size to big to handle!
+#define SS_MISMATCHED_COMPONENTS 0xE7 // The DLLs/EXEs of ASPI don't version
+ // check
+#define SS_NO_ADAPTERS 0xE8 // No host adapters to manage
+#define SS_INSUFFICIENT_RESOURCES 0xE9 // Couldn't allocate resources needed
+ // to init
+#define SS_ASPI_IS_SHUTDOWN 0xEA // Call came to ASPI after
+ // PROCESS_DETACH
+#define SS_BAD_INSTALL 0xEB // The DLL or other components are installed wrong
+
+//*****************************************************************************
+// %%% Host Adapter Status %%%
+//*****************************************************************************
+
+#define HASTAT_OK 0x00 // Host adapter did not detect an
+ // error
+#define HASTAT_SEL_TO 0x11 // Selection Timeout
+#define HASTAT_DO_DU 0x12 // Data overrun data underrun
+#define HASTAT_BUS_FREE 0x13 // Unexpected bus free
+#define HASTAT_PHASE_ERR 0x14 // Target bus phase sequence
+ // failure
+#define HASTAT_TIMEOUT 0x09 // Timed out while SRB was
+ // waiting to beprocessed.
+#define HASTAT_COMMAND_TIMEOUT 0x0B // Adapter timed out processing SRB.
+#define HASTAT_MESSAGE_REJECT 0x0D // While processing SRB, the
+ // adapter received a MESSAGE
+#define HASTAT_BUS_RESET 0x0E // A bus reset was detected.
+#define HASTAT_PARITY_ERROR 0x0F // A parity error was detected.
+#define HASTAT_REQUEST_SENSE_FAILED 0x10 // The adapter failed in issuing
+#define SS_NO_ADAPTERS 0xE8
+#define SRB_DIR_IN 0x08
+#define SRB_DIR_OUT 0x10
+#define SRB_EVENT_NOTIFY 0x40
+
+#define SECTOR_TYPE_MODE2 0x14
+#define READ_CD_USERDATA_MODE2 0x10
+
+#define READ_TOC 0x43
+#define READ_TOC_FORMAT_TOC 0x0
+
+#pragma pack(1)
+
+struct SRB_GetDiskInfo
+{
+ unsigned char SRB_Cmd;
+ unsigned char SRB_Status;
+ unsigned char SRB_HaId;
+ unsigned char SRB_Flags;
+ unsigned long SRB_Hdr_Rsvd;
+ unsigned char SRB_Target;
+ unsigned char SRB_Lun;
+ unsigned char SRB_DriveFlags;
+ unsigned char SRB_Int13HDriveInfo;
+ unsigned char SRB_Heads;
+ unsigned char SRB_Sectors;
+ unsigned char SRB_Rsvd1[22];
+};
+
+struct SRB_GDEVBlock
+{
+ unsigned char SRB_Cmd;
+ unsigned char SRB_Status;
+ unsigned char SRB_HaId;
+ unsigned char SRB_Flags;
+ unsigned long SRB_Hdr_Rsvd;
+ unsigned char SRB_Target;
+ unsigned char SRB_Lun;
+ unsigned char SRB_DeviceType;
+ unsigned char SRB_Rsvd1;
+};
+
+struct SRB_ExecSCSICmd
+{
+ unsigned char SRB_Cmd;
+ unsigned char SRB_Status;
+ unsigned char SRB_HaId;
+ unsigned char SRB_Flags;
+ unsigned long SRB_Hdr_Rsvd;
+ unsigned char SRB_Target;
+ unsigned char SRB_Lun;
+ unsigned short SRB_Rsvd1;
+ unsigned long SRB_BufLen;
+ unsigned char *SRB_BufPointer;
+ unsigned char SRB_SenseLen;
+ unsigned char SRB_CDBLen;
+ unsigned char SRB_HaStat;
+ unsigned char SRB_TargStat;
+ unsigned long *SRB_PostProc;
+ unsigned char SRB_Rsvd2[20];
+ unsigned char CDBByte[16];
+ unsigned char SenseArea[SENSE_LEN+2];
+};
+
+/*****************************************************************************
+ %%% SRB - HOST ADAPTER INQUIRY - SC_HA_INQUIRY (0) %%%
+*****************************************************************************/
+
+typedef struct // Offset
+{ // HX/DEC
+ BYTE SRB_Cmd; // 00/000 ASPI command code = SC_HA_INQUIRY
+ BYTE SRB_Status; // 01/001 ASPI command status byte
+ BYTE SRB_HaId; // 02/002 ASPI host adapter number
+ BYTE SRB_Flags; // 03/003 ASPI request flags
+ DWORD SRB_Hdr_Rsvd; // 04/004 Reserved, MUST = 0
+ BYTE HA_Count; // 08/008 Number of host adapters present
+ BYTE HA_SCSI_ID; // 09/009 SCSI ID of host adapter
+ BYTE HA_ManagerId[16]; // 0A/010 String describing the manager
+ BYTE HA_Identifier[16]; // 1A/026 String describing the host adapter
+ BYTE HA_Unique[16]; // 2A/042 Host Adapter Unique parameters
+ WORD HA_Rsvd1; // 3A/058 Reserved, MUST = 0
+}
+SRB_HAInquiry;
+
+/*!
+ Get disc type associated with cd object.
+*/
+discmode_t get_discmode_aspi (_img_private_t *p_env);
+
+/*!
+ Return the the kind of drive capabilities of device.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+char * get_mcn_aspi (const _img_private_t *env);
+
+/*!
+ Get the format (XA, DATA, AUDIO) of a track.
+*/
+track_format_t get_track_format_aspi(const _img_private_t *env,
+ track_t i_track);
+
+/*!
+ Initialize internal structures for CD device.
+ */
+bool init_aspi (_img_private_t *env);
+
+/*
+ Read cdtext information for a CdIo object .
+
+ return true on success, false on error or CD-TEXT information does
+ not exist.
+*/
+bool init_cdtext_aspi (_img_private_t *env);
+
+const char *is_cdrom_aspi(const char drive_letter);
+
+/*!
+ Reads an audio device using the DeviceIoControl method into data
+ starting from lsn. Returns 0 if no error.
+ */
+int read_audio_sectors_aspi (_img_private_t *obj, void *data, lsn_t lsn,
+ unsigned int nblocks);
+/*!
+ Reads a single mode1 sector using the DeviceIoControl method into
+ data starting from lsn. Returns 0 if no error.
+ */
+int read_mode1_sector_aspi (const _img_private_t *env, void *data,
+ lsn_t lsn, bool b_form2);
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+int read_mode2_sector_aspi (const _img_private_t *env, void *data, lsn_t lsn,
+ bool b_form2);
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return true if successful or false if an error.
+*/
+bool read_toc_aspi (_img_private_t *env);
+
+/*!
+ Run a SCSI MMC command.
+
+ env private CD structure
+ i_timeout time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ p_buf Buffer for data, both sending and receiving
+ i_buf Size of buffer
+ e_direction direction the transfer is to go.
+ cdb CDB bytes. All values that are needed should be set on
+ input. We'll figure out what the right CDB length should be.
+
+ Return 0 if command completed successfully.
+ */
+int run_scsi_cmd_aspi( const void *p_user_data,
+ unsigned int i_timeout,
+ unsigned int i_cdb,
+ const scsi_mmc_cdb_t * p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf );
+
diff --git a/contrib/libcdio/MSWindows/win32.c b/contrib/libcdio/MSWindows/win32.c
new file mode 100644
index 000000000..1c9d9b93d
--- /dev/null
+++ b/contrib/libcdio/MSWindows/win32.c
@@ -0,0 +1,805 @@
+/*
+ $Id: win32.c,v 1.3 2006/09/26 22:10:24 dgp85 Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains Win32-specific code and implements low-level
+ control of the CD drive. Inspired by vlc's cdrom.h code
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: win32.c,v 1.3 2006/09/26 22:10:24 dgp85 Exp $";
+
+#include <cdio/cdio.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include <cdio/scsi_mmc.h>
+#include "cdio_assert.h"
+#include "cdio_private.h" /* protoype for cdio_is_device_win32 */
+
+#include <string.h>
+
+#ifdef HAVE_WIN32_CDROM
+
+#include <ctype.h>
+#include <stdio.h>
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
+#include <windows.h>
+#include <winioctl.h>
+#include "win32.h"
+
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#if defined (MSVC) || defined (_XBOX)
+#undef IN
+#else
+#include "aspi32.h"
+#endif
+
+#ifdef _XBOX
+#include "stdint.h"
+#include <xtl.h>
+#define WIN_NT 1
+#else
+#define WIN_NT ( GetVersion() < 0x80000000 )
+#endif
+
+/* General ioctl() CD-ROM command function */
+static bool
+_cdio_mciSendCommand(int id, UINT msg, DWORD flags, void *arg)
+{
+#ifdef _XBOX
+ return false;
+#else
+ MCIERROR mci_error;
+
+ mci_error = mciSendCommand(id, msg, flags, (DWORD)arg);
+ if ( mci_error ) {
+ char error[256];
+
+ mciGetErrorString(mci_error, error, 256);
+ cdio_warn("mciSendCommand() error: %s", error);
+ }
+ return(mci_error == 0);
+#endif
+}
+
+static access_mode_t
+str_to_access_mode_win32(const char *psz_access_mode)
+{
+ const access_mode_t default_access_mode =
+ WIN_NT ? _AM_IOCTL : _AM_ASPI;
+
+ if (NULL==psz_access_mode) return default_access_mode;
+
+ if (!strcmp(psz_access_mode, "ioctl"))
+ return _AM_IOCTL;
+ else if (!strcmp(psz_access_mode, "ASPI")) {
+#ifdef _XBOX
+ return _AM_ASPI;
+#else
+ cdio_warn ("XBOX doesn't support access type: %s. Default used instead.",
+ psz_access_mode);
+ return default_access_mode;
+#endif
+ } else {
+ cdio_warn ("unknown access type: %s. Default used instead.",
+ psz_access_mode);
+ return default_access_mode;
+ }
+}
+
+static discmode_t
+get_discmode_win32(void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if (p_env->hASPI) {
+ return get_discmode_aspi (p_env);
+ } else {
+ return get_discmode_win32ioctl (p_env);
+ }
+}
+
+static const char *
+is_cdrom_win32(const char drive_letter) {
+ if ( WIN_NT ) {
+ return is_cdrom_win32ioctl (drive_letter);
+ } else {
+ return is_cdrom_aspi(drive_letter);
+ }
+}
+
+/*!
+ Run a SCSI MMC command.
+
+ env private CD structure
+ i_timeout_ms time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ p_buf Buffer for data, both sending and receiving
+ i_buf Size of buffer
+ e_direction direction the transfer is to go.
+ cdb CDB bytes. All values that are needed should be set on
+ input. We'll figure out what the right CDB length should be.
+
+ Return 0 if command completed successfully.
+ */
+static int
+run_scsi_cmd_win32( const void *p_user_data, unsigned int i_timeout_ms,
+ unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf )
+{
+ const _img_private_t *p_env = p_user_data;
+
+ if (p_env->hASPI) {
+ return run_scsi_cmd_aspi( p_env, i_timeout_ms, i_cdb, p_cdb,
+ e_direction, i_buf, p_buf );
+ } else {
+ return run_scsi_cmd_win32ioctl( p_env, i_timeout_ms, i_cdb, p_cdb,
+ e_direction, i_buf, p_buf );
+ }
+}
+
+/*!
+ Initialize CD device.
+ */
+static bool
+_cdio_init_win32 (void *user_data)
+{
+ _img_private_t *p_env = user_data;
+ if (p_env->gen.init) {
+ cdio_error ("init called more than once");
+ return false;
+ }
+
+ p_env->gen.init = true;
+ p_env->gen.toc_init = false;
+ p_env->gen.b_cdtext_init = false;
+ p_env->gen.b_cdtext_error = false;
+
+ /* Initializations */
+ p_env->h_device_handle = NULL;
+ p_env->i_sid = 0;
+ p_env->hASPI = 0;
+ p_env->lpSendCommand = 0;
+ p_env->b_aspi_init = false;
+ p_env->b_ioctl_init = false;
+
+ if ( _AM_IOCTL == p_env->access_mode ) {
+ return init_win32ioctl(p_env);
+ } else {
+ return init_aspi(p_env);
+ }
+}
+
+/*!
+ Release and free resources associated with cd.
+ */
+static void
+_free_win32 (void *user_data)
+{
+ _img_private_t *p_env = user_data;
+
+ if (NULL == p_env) return;
+ free (p_env->gen.source_name);
+
+ if( p_env->h_device_handle )
+ CloseHandle( p_env->h_device_handle );
+ if( p_env->hASPI )
+ FreeLibrary( (HMODULE)p_env->hASPI );
+
+ free (p_env);
+}
+
+/*!
+ Reads an audio device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_audio_sectors (void *user_data, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *p_env = user_data;
+ if ( p_env->hASPI ) {
+ return read_audio_sectors_aspi( p_env, data, lsn, nblocks );
+ } else {
+ return read_audio_sectors_win32ioctl( p_env, data, lsn, nblocks );
+ }
+}
+
+/*!
+ Reads a single mode1 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_cdio_read_mode1_sector (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ _img_private_t *p_env = user_data;
+
+ if (p_env->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged < 75
+ || (p_env->gen.ioctls_debugged < (30 * 75)
+ && p_env->gen.ioctls_debugged % 75 == 0)
+ || p_env->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %lu", (unsigned long int) lsn);
+
+ p_env->gen.ioctls_debugged++;
+
+ if ( p_env->hASPI ) {
+ return read_mode1_sector_aspi( p_env, data, lsn, b_form2 );
+ } else {
+ return read_mode1_sector_win32ioctl( p_env, data, lsn, b_form2 );
+ }
+}
+
+/*!
+ Reads nblocks of mode1 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode1_sectors (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *p_env = user_data;
+ int i;
+ int retval;
+
+ for (i = 0; i < nblocks; i++) {
+ if (b_form2) {
+ if ( (retval = _cdio_read_mode1_sector (p_env,
+ ((char *)data) + (M2RAW_SECTOR_SIZE * i),
+ lsn + i, true)) )
+ return retval;
+ } else {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ if ( (retval = _cdio_read_mode1_sector (p_env, buf, lsn + i, false)) )
+ return retval;
+
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ buf, CDIO_CD_FRAMESIZE);
+ }
+ }
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sector (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ _img_private_t *p_env = user_data;
+
+ if (p_env->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged < 75
+ || (p_env->gen.ioctls_debugged < (30 * 75)
+ && p_env->gen.ioctls_debugged % 75 == 0)
+ || p_env->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %lu", (unsigned long int) lsn);
+
+ p_env->gen.ioctls_debugged++;
+
+ if ( p_env->hASPI ) {
+ int ret;
+ ret = read_mode2_sector_aspi(user_data, buf, lsn, 1);
+ if( ret != 0 ) return ret;
+ if (b_form2)
+ memcpy (data, buf, M2RAW_SECTOR_SIZE);
+ else
+ memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ return 0;
+ } else {
+ return read_mode2_sector_win32ioctl( p_env, data, lsn, b_form2 );
+ }
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sectors (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _cdio_read_mode2_sector (user_data,
+ ((char *)data) + (blocksize * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+stat_size_win32 (void *user_data)
+{
+ _img_private_t *p_env = user_data;
+
+ return p_env->tocent[p_env->gen.i_tracks].start_lsn;
+}
+
+/*!
+ Set the key "arg" to "value" in source device.
+*/
+static int
+set_arg_win32 (void *user_data, const char key[], const char value[])
+{
+ _img_private_t *p_env = user_data;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (p_env->gen.source_name);
+
+ p_env->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ p_env->access_mode = str_to_access_mode_win32(value);
+ if (p_env->access_mode == _AM_ASPI && !p_env->b_aspi_init)
+ return init_aspi(p_env) ? 1 : -3;
+ else if (p_env->access_mode == _AM_IOCTL && !p_env->b_ioctl_init)
+ return init_win32ioctl(p_env) ? 1 : -3;
+ else
+ return -4;
+ return 0;
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return true if successful or false if an error.
+*/
+static bool
+read_toc_win32 (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+ bool ret;
+ if( p_env->hASPI ) {
+ ret = read_toc_aspi( p_env );
+ } else {
+ ret = read_toc_win32ioctl( p_env );
+ }
+ if (ret) p_env->gen.toc_init = true ;
+ return true;
+}
+
+/*!
+ Eject media. Return 1 if successful, 0 otherwise.
+ */
+static int
+_cdio_eject_media (void *user_data) {
+#ifdef _XBOX
+ return -1;
+#else
+ _img_private_t *env = user_data;
+
+
+ MCI_OPEN_PARMS op;
+ MCI_STATUS_PARMS st;
+ DWORD i_flags;
+ char psz_drive[4];
+ int ret;
+
+ memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
+ op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
+ strcpy( psz_drive, "X:" );
+ psz_drive[0] = env->gen.source_name[0];
+ op.lpstrElementName = psz_drive;
+
+ /* Set the flags for the device type */
+ i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
+ MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
+
+ if( _cdio_mciSendCommand( 0, MCI_OPEN, i_flags, &op ) ) {
+ st.dwItem = MCI_STATUS_READY;
+ /* Eject disc */
+ ret = _cdio_mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 ) != 0;
+ /* Release access to the device */
+ _cdio_mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
+ } else
+ ret = 0;
+
+ return ret;
+#endif
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_get_arg_win32 (void *user_data, const char key[])
+{
+ _img_private_t *env = user_data;
+
+ if (!strcmp (key, "source")) {
+ return env->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ if (env->hASPI)
+ return "ASPI";
+ else
+ return "ioctl";
+ }
+ return NULL;
+}
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+static char *
+_cdio_get_mcn (const void *p_user_data) {
+ const _img_private_t *p_env = p_user_data;
+
+ if( p_env->hASPI ) {
+ return scsi_mmc_get_mcn( p_env->gen.cdio );
+ } else {
+ return get_mcn_win32ioctl(p_env);
+ }
+}
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+_cdio_get_track_format(void *p_obj, track_t i_track)
+{
+ _img_private_t *p_env = p_obj;
+
+ if ( !p_env ) return TRACK_FORMAT_ERROR;
+
+ if (!p_env->gen.toc_init) read_toc_win32 (p_env) ;
+
+ if ( i_track < p_env->gen.i_first_track
+ || i_track >= p_env->gen.i_tracks + p_env->gen.i_first_track )
+ return TRACK_FORMAT_ERROR;
+
+ if( p_env->hASPI ) {
+ return get_track_format_aspi(p_env, i_track);
+ } else {
+ return get_track_format_win32ioctl(p_env, i_track);
+ }
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *obj, track_t i_track)
+{
+ _img_private_t *p_env = obj;
+
+ switch (_cdio_get_track_format(p_env, i_track)) {
+ case TRACK_FORMAT_XA:
+ return true;
+ case TRACK_FORMAT_ERROR:
+ case TRACK_FORMAT_CDI:
+ case TRACK_FORMAT_AUDIO:
+ return false;
+ case TRACK_FORMAT_DATA:
+ if (_AM_ASPI == p_env->access_mode )
+ return ((p_env->tocent[i_track-p_env->gen.i_first_track].Control & 8) != 0);
+ default:
+ break;
+ }
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cd-info for a while.
+ */
+ return ((p_env->tocent[i_track-p_env->gen.i_first_track].Control & 2) != 0);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ i_tracks in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using i_tracks LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static bool
+_cdio_get_track_msf(void *p_user_data, track_t i_tracks, msf_t *msf)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if (NULL == msf) return false;
+
+ if (!p_env->gen.toc_init) read_toc_win32 (p_env) ;
+
+ if (i_tracks == CDIO_CDROM_LEADOUT_TRACK) i_tracks = p_env->gen.i_tracks+1;
+
+ if (i_tracks > p_env->gen.i_tracks+1 || i_tracks == 0) {
+ return false;
+ } else {
+ cdio_lsn_to_msf(p_env->tocent[i_tracks-1].start_lsn, msf);
+ return true;
+ }
+}
+
+#endif /* HAVE_WIN32_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_win32 (void)
+{
+#ifndef HAVE_WIN32_CDROM
+ return NULL;
+#else
+ char **drives = NULL;
+ unsigned int num_drives=0;
+ char drive_letter;
+
+ /* Scan the system for CD-ROM drives.
+ */
+
+#if FINISHED
+ /* Now check the currently mounted CD drives */
+ if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab"))) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+
+ /* Finally check possible mountable drives in /etc/fstab */
+ if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab"))) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+#endif
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+ for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) {
+ const char *drive_str=is_cdrom_win32(drive_letter);
+ if (drive_str != NULL) {
+ cdio_add_device_list(&drives, drive_str, &num_drives);
+ }
+ }
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /*HAVE_WIN32_CDROM*/
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then find a suitable one and return the default device for that.
+
+ NULL is returned if we couldn't get a default device.
+*/
+char *
+cdio_get_default_device_win32(void)
+{
+
+#ifdef HAVE_WIN32_CDROM
+ char drive_letter;
+
+ for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) {
+ const char *drive_str=is_cdrom_win32(drive_letter);
+ if (drive_str != NULL) {
+ return strdup(drive_str);
+ }
+ }
+#endif
+ return NULL;
+}
+
+/*!
+ Return true if source_name could be a device containing a CD-ROM.
+*/
+bool
+cdio_is_device_win32(const char *source_name)
+{
+ unsigned int len;
+
+ if (NULL == source_name) return false;
+ len = strlen(source_name);
+
+#ifdef HAVE_WIN32_CDROM
+ if ((len == 2) && isalpha(source_name[0])
+ && (source_name[len-1] == ':'))
+ return true;
+
+ if ( ! WIN_NT ) return false;
+
+ /* Test to see if of form: \\.\x: */
+ return ( (len == 6)
+ && source_name[0] == '\\' && source_name[1] == '\\'
+ && source_name[2] == '.' && source_name[3] == '\\'
+ && isalpha(source_name[len-2])
+ && (source_name[len-1] == ':') );
+#else
+ return false;
+#endif
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_win32 (const char *psz_source_name)
+{
+#ifdef HAVE_WIN32_CDROM
+ if ( WIN_NT ) {
+ return cdio_open_am_win32(psz_source_name, "ioctl");
+ } else {
+ return cdio_open_am_win32(psz_source_name, "ASPI");
+ }
+#else
+ return NULL;
+#endif /* HAVE_WIN32_CDROM */
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_am_win32 (const char *psz_orig_source, const char *psz_access_mode)
+{
+
+#ifdef HAVE_WIN32_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+ char *psz_source;
+
+ cdio_funcs _funcs;
+
+ memset( &_funcs, 0, sizeof(_funcs) );
+
+ _funcs.eject_media = _cdio_eject_media;
+ _funcs.free = _free_win32;
+ _funcs.get_arg = _get_arg_win32;
+ _funcs.get_cdtext = get_cdtext_generic;
+ _funcs.get_default_device = cdio_get_default_device_win32;
+ _funcs.get_devices = cdio_get_devices_win32;
+ _funcs.get_discmode = get_discmode_win32;
+ _funcs.get_drive_cap = scsi_mmc_get_drive_cap_generic;
+ _funcs.get_first_track_num= get_first_track_num_generic;
+ _funcs.get_hwinfo = NULL;
+ _funcs.get_mcn = _cdio_get_mcn;
+ _funcs.get_num_tracks = get_num_tracks_generic;
+ _funcs.get_track_format = _cdio_get_track_format;
+ _funcs.get_track_green = _cdio_get_track_green;
+ _funcs.get_track_lba = NULL; /* This could be implemented if need be. */
+ _funcs.get_track_msf = _cdio_get_track_msf;
+ _funcs.lseek = NULL;
+ _funcs.read = NULL;
+ _funcs.read_audio_sectors = _cdio_read_audio_sectors;
+ _funcs.read_mode1_sector = _cdio_read_mode1_sector;
+ _funcs.read_mode1_sectors = _cdio_read_mode1_sectors;
+ _funcs.read_mode2_sector = _cdio_read_mode2_sector;
+ _funcs.read_mode2_sectors = _cdio_read_mode2_sectors;
+ _funcs.read_toc = &read_toc_win32;
+ _funcs.run_scsi_mmc_cmd = &run_scsi_cmd_win32;
+ _funcs.set_arg = set_arg_win32;
+ _funcs.stat_size = stat_size_win32;
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->access_mode = str_to_access_mode_win32(psz_access_mode);
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+
+ if (NULL == psz_orig_source) {
+ psz_source=cdio_get_default_device_win32();
+ if (NULL == psz_source) return NULL;
+ set_arg_win32(_data, "source", psz_source);
+ free(psz_source);
+ } else {
+ if (cdio_is_device_win32(psz_orig_source))
+ set_arg_win32(_data, "source", psz_orig_source);
+ else {
+ /* The below would be okay if all device drivers worked this way. */
+#if 0
+ cdio_info ("source %s is a not a device", psz_orig_source);
+#endif
+ return NULL;
+ }
+ }
+
+ ret = cdio_new ((void *)_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (_cdio_init_win32(_data))
+ return ret;
+ else {
+ _free_win32 (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_WIN32_CDROM */
+
+}
+
+bool
+cdio_have_win32 (void)
+{
+#ifdef HAVE_WIN32_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_WIN32_CDROM */
+}
diff --git a/contrib/libcdio/MSWindows/win32.h b/contrib/libcdio/MSWindows/win32.h
new file mode 100644
index 000000000..84f3b67b3
--- /dev/null
+++ b/contrib/libcdio/MSWindows/win32.h
@@ -0,0 +1,170 @@
+/*
+ $Id: win32.h,v 1.2 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#include "cdio_private.h"
+
+#pragma pack()
+
+typedef struct {
+ lsn_t start_lsn;
+ UCHAR Control : 4;
+ UCHAR Format;
+ cdtext_t cdtext; /* CD-TEXT */
+} track_info_t;
+
+typedef enum {
+ _AM_NONE,
+ _AM_IOCTL,
+ _AM_ASPI,
+} access_mode_t;
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ access_mode_t access_mode;
+
+ /* Some of the more OS specific things. */
+ /* Entry info for each track, add 1 for leadout. */
+ track_info_t tocent[CDIO_CD_MAX_TRACKS+1];
+
+ HANDLE h_device_handle; /* device descriptor */
+ long hASPI;
+ short i_sid;
+ short i_lun;
+ long (*lpSendCommand)( void* );
+
+ bool b_ioctl_init;
+ bool b_aspi_init;
+
+} _img_private_t;
+
+/*!
+ Get disc type associated with cd object.
+*/
+discmode_t get_discmode_win32ioctl (_img_private_t *p_env);
+
+/*!
+ Reads an audio device using the DeviceIoControl method into data
+ starting from lsn. Returns 0 if no error.
+*/
+int read_audio_sectors_win32ioctl (_img_private_t *obj, void *data, lsn_t lsn,
+ unsigned int nblocks);
+/*!
+ Reads a single mode2 sector using the DeviceIoControl method into
+ data starting from lsn. Returns 0 if no error.
+ */
+int read_mode2_sector_win32ioctl (const _img_private_t *env, void *data,
+ lsn_t lsn, bool b_form2);
+
+/*!
+ Reads a single mode1 sector using the DeviceIoControl method into
+ data starting from lsn. Returns 0 if no error.
+ */
+int read_mode1_sector_win32ioctl (const _img_private_t *env, void *data,
+ lsn_t lsn, bool b_form2);
+
+const char *is_cdrom_win32ioctl (const char drive_letter);
+
+/*!
+ Run a SCSI MMC command.
+
+ env private CD structure
+ i_timeout_ms time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ p_buf Buffer for data, both sending and receiving
+ i_buf Size of buffer
+ e_direction direction the transfer is to go.
+ cdb CDB bytes. All values that are needed should be set on
+ input. We'll figure out what the right CDB length should be.
+
+ Return 0 if command completed successfully.
+ */
+int run_scsi_cmd_win32ioctl( const void *p_user_data,
+ unsigned int i_timeout,
+ unsigned int i_cdb,
+ const scsi_mmc_cdb_t * p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf );
+
+/*!
+ Initialize internal structures for CD device.
+ */
+bool init_win32ioctl (_img_private_t *env);
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return true if successful or false if an error.
+*/
+bool read_toc_win32ioctl (_img_private_t *env);
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+char *get_mcn_win32ioctl (const _img_private_t *env);
+
+/*
+ Read cdtext information for a CdIo object .
+
+ return true on success, false on error or CD-TEXT information does
+ not exist.
+*/
+bool init_cdtext_win32ioctl (_img_private_t *env);
+
+/*!
+ Return the the kind of drive capabilities of device.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+void get_drive_cap_aspi (const _img_private_t *env,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap);
+
+/*!
+ Return the the kind of drive capabilities of device.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+void get_drive_cap_win32ioctl (const _img_private_t *env,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap);
+
+/*!
+ Get the format (XA, DATA, AUDIO) of a track.
+*/
+track_format_t get_track_format_win32ioctl(const _img_private_t *env,
+ track_t i_track);
+
+void set_cdtext_field_win32(void *user_data, track_t i_track,
+ track_t i_first_track,
+ cdtext_field_t e_field, const char *psz_value);
+
diff --git a/contrib/libcdio/MSWindows/win32_ioctl.c b/contrib/libcdio/MSWindows/win32_ioctl.c
new file mode 100644
index 000000000..dec24e785
--- /dev/null
+++ b/contrib/libcdio/MSWindows/win32_ioctl.c
@@ -0,0 +1,739 @@
+/*
+ $Id: win32_ioctl.c,v 1.1 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains Win32-specific code using the DeviceIoControl
+ access method.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: win32_ioctl.c,v 1.1 2005/01/01 02:43:58 rockyb Exp $";
+
+#ifdef HAVE_WIN32_CDROM
+
+#if defined (_XBOX)
+#include "inttypes.h"
+#include "NtScsi.h"
+#include "undocumented.h"
+#define FORMAT_ERROR(i_err, psz_msg) \
+ psz_msg=(char *)LocalAlloc(LMEM_ZEROINIT, 255); \
+ sprintf(psz_msg, "error file %s: line %d (%s) %d\n",
+ _FILE__, __LINE__, __PRETTY_FUNCTION__, i_err)
+#else
+#include <ddk/ntddstor.h>
+#include <ddk/ntddscsi.h>
+#include <ddk/scsi.h>
+#define FORMAT_ERROR(i_err, psz_msg) \
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, \
+ NULL, i_err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
+ (LPSTR) psz_msg, 0, NULL)
+#endif
+
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+#include <stdio.h>
+#include <stddef.h> /* offsetof() macro */
+#include <sys/stat.h>
+#include <errno.h>
+#include <sys/types.h>
+
+#include <cdio/cdio.h>
+#include <cdio/sector.h>
+#include "cdio_assert.h"
+#include <cdio/scsi_mmc.h>
+#include "cdtext_private.h"
+#include "cdio/logging.h"
+
+/* Win32 DeviceIoControl specifics */
+/***** FIXME: #include ntddcdrm.h from Wine, but probably need to
+ modify it a little.
+*/
+
+#ifndef IOCTL_CDROM_BASE
+# define IOCTL_CDROM_BASE FILE_DEVICE_CD_ROM
+#endif
+#ifndef IOCTL_CDROM_READ_TOC
+#define IOCTL_CDROM_READ_TOC \
+ CTL_CODE(IOCTL_CDROM_BASE, 0x0000, METHOD_BUFFERED, FILE_READ_ACCESS)
+#endif
+#ifndef IOCTL_CDROM_RAW_READ
+#define IOCTL_CDROM_RAW_READ CTL_CODE(IOCTL_CDROM_BASE, 0x000F, \
+ METHOD_OUT_DIRECT, FILE_READ_ACCESS)
+#endif
+
+#ifndef IOCTL_CDROM_READ_Q_CHANNEL
+#define IOCTL_CDROM_READ_Q_CHANNEL \
+ CTL_CODE(IOCTL_CDROM_BASE, 0x000B, METHOD_BUFFERED, FILE_READ_ACCESS)
+#endif
+
+typedef struct {
+ SCSI_PASS_THROUGH Spt;
+ ULONG Filler;
+ UCHAR SenseBuf[32];
+ UCHAR DataBuf[512];
+} SCSI_PASS_THROUGH_WITH_BUFFERS;
+
+typedef struct _TRACK_DATA {
+ UCHAR Format;
+ UCHAR Control : 4;
+ UCHAR Adr : 4;
+ UCHAR TrackNumber;
+ UCHAR Reserved1;
+ UCHAR Address[4];
+} TRACK_DATA, *PTRACK_DATA;
+
+typedef struct _CDROM_TOC {
+ UCHAR Length[2];
+ UCHAR FirstTrack;
+ UCHAR LastTrack;
+ TRACK_DATA TrackData[CDIO_CD_MAX_TRACKS+1];
+} CDROM_TOC, *PCDROM_TOC;
+
+typedef struct _TRACK_DATA_FULL {
+ UCHAR SessionNumber;
+ UCHAR Control : 4;
+ UCHAR Adr : 4;
+ UCHAR TNO;
+ UCHAR POINT; /* Tracknumber (of session?) or lead-out/in (0xA0, 0xA1, 0xA2) */
+ UCHAR Min; /* Only valid if disctype is CDDA ? */
+ UCHAR Sec; /* Only valid if disctype is CDDA ? */
+ UCHAR Frame; /* Only valid if disctype is CDDA ? */
+ UCHAR Zero; /* Always zero */
+ UCHAR PMIN; /* start min, if POINT is a track; if lead-out/in 0xA0: First Track */
+ UCHAR PSEC;
+ UCHAR PFRAME;
+} TRACK_DATA_FULL, *PTRACK_DATA_FULL;
+
+typedef struct _CDROM_TOC_FULL {
+ UCHAR Length[2];
+ UCHAR FirstSession;
+ UCHAR LastSession;
+ TRACK_DATA_FULL TrackData[CDIO_CD_MAX_TRACKS+3];
+} CDROM_TOC_FULL, *PCDROM_TOC_FULL;
+
+typedef enum _TRACK_MODE_TYPE {
+ YellowMode2,
+ XAForm2,
+ CDDA
+} TRACK_MODE_TYPE, *PTRACK_MODE_TYPE;
+
+typedef struct __RAW_READ_INFO {
+ LARGE_INTEGER DiskOffset;
+ ULONG SectorCount;
+ TRACK_MODE_TYPE TrackMode;
+} RAW_READ_INFO, *PRAW_READ_INFO;
+
+typedef struct _CDROM_SUB_Q_DATA_FORMAT {
+ UCHAR Format;
+ UCHAR Track;
+} CDROM_SUB_Q_DATA_FORMAT, *PCDROM_SUB_Q_DATA_FORMAT;
+
+typedef struct _SUB_Q_HEADER {
+ UCHAR Reserved;
+ UCHAR AudioStatus;
+ UCHAR DataLength[2];
+} SUB_Q_HEADER, *PSUB_Q_HEADER;
+
+typedef struct _SUB_Q_MEDIA_CATALOG_NUMBER {
+ SUB_Q_HEADER Header;
+ UCHAR FormatCode;
+ UCHAR Reserved[3];
+ UCHAR Reserved1 : 7;
+ UCHAR Mcval :1;
+ UCHAR MediaCatalog[15];
+} SUB_Q_MEDIA_CATALOG_NUMBER, *PSUB_Q_MEDIA_CATALOG_NUMBER;
+
+#include "win32.h"
+
+#define OP_TIMEOUT_MS 60
+
+/*!
+ Run a SCSI MMC command.
+
+ env private CD structure
+ i_timeout time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ p_buf Buffer for data, both sending and receiving
+ i_buf Size of buffer
+ e_direction direction the transfer is to go.
+ cdb CDB bytes. All values that are needed should be set on
+ input. We'll figure out what the right CDB length should be.
+
+ Return 0 if command completed successfully.
+ */
+int
+run_scsi_cmd_win32ioctl( const void *p_user_data,
+ unsigned int i_timeout_ms,
+ unsigned int i_cdb, const scsi_mmc_cdb_t * p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf )
+{
+ const _img_private_t *p_env = p_user_data;
+ SCSI_PASS_THROUGH_DIRECT sptd;
+ bool success;
+ DWORD dwBytesReturned;
+
+ sptd.Length = sizeof(sptd);
+ sptd.PathId = 0; /* SCSI card ID will be filled in automatically */
+ sptd.TargetId= 0; /* SCSI target ID will also be filled in */
+ sptd.Lun=0; /* SCSI lun ID will also be filled in */
+ sptd.CdbLength = i_cdb;
+ sptd.SenseInfoLength = 0; /* Don't return any sense data */
+ sptd.DataIn = SCSI_MMC_DATA_READ == e_direction ?
+ SCSI_IOCTL_DATA_IN : SCSI_IOCTL_DATA_OUT;
+ sptd.DataTransferLength= i_buf;
+ sptd.TimeOutValue = msecs2secs(i_timeout_ms);
+ sptd.DataBuffer = (void *) p_buf;
+ sptd.SenseInfoOffset = 0;
+
+ memcpy(sptd.Cdb, p_cdb, i_cdb);
+
+ /* Send the command to drive */
+ success=DeviceIoControl(p_env->h_device_handle,
+ IOCTL_SCSI_PASS_THROUGH_DIRECT,
+ (void *)&sptd,
+ (DWORD)sizeof(SCSI_PASS_THROUGH_DIRECT),
+ NULL, 0,
+ &dwBytesReturned,
+ NULL);
+
+ if(! success) {
+ char *psz_msg = NULL;
+ long int i_err = GetLastError();
+ FORMAT_ERROR(i_err, psz_msg);
+ cdio_info("Error: %s", psz_msg);
+ LocalFree(psz_msg);
+ return 1;
+ }
+ return 0;
+}
+
+/*!
+ Get disc type associated with cd object.
+*/
+discmode_t
+get_discmode_win32ioctl (_img_private_t *p_env)
+{
+ track_t i_track;
+ discmode_t discmode=CDIO_DISC_MODE_NO_INFO;
+
+ /* See if this is a DVD. */
+ cdio_dvd_struct_t dvd; /* DVD READ STRUCT for layer 0. */
+
+ dvd.physical.type = CDIO_DVD_STRUCT_PHYSICAL;
+ dvd.physical.layer_num = 0;
+ if (0 == scsi_mmc_get_dvd_struct_physical_private (p_env,
+ &run_scsi_cmd_win32ioctl,
+ &dvd)) {
+ switch(dvd.physical.layer[0].book_type) {
+ case CDIO_DVD_BOOK_DVD_ROM: return CDIO_DISC_MODE_DVD_ROM;
+ case CDIO_DVD_BOOK_DVD_RAM: return CDIO_DISC_MODE_DVD_RAM;
+ case CDIO_DVD_BOOK_DVD_R: return CDIO_DISC_MODE_DVD_R;
+ case CDIO_DVD_BOOK_DVD_RW: return CDIO_DISC_MODE_DVD_RW;
+ case CDIO_DVD_BOOK_DVD_PR: return CDIO_DISC_MODE_DVD_PR;
+ case CDIO_DVD_BOOK_DVD_PRW: return CDIO_DISC_MODE_DVD_PRW;
+ default: return CDIO_DISC_MODE_DVD_OTHER;
+ }
+ }
+
+ if (!p_env->gen.toc_init)
+ read_toc_win32ioctl (p_env);
+
+ if (!p_env->gen.toc_init)
+ return CDIO_DISC_MODE_NO_INFO;
+
+ for (i_track = p_env->gen.i_first_track;
+ i_track < p_env->gen.i_first_track + p_env->gen.i_tracks ;
+ i_track ++) {
+ track_format_t track_fmt=get_track_format_win32ioctl(p_env, i_track);
+
+ switch(track_fmt) {
+ case TRACK_FORMAT_AUDIO:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_DA;
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_XA:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_DATA:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_ERROR:
+ default:
+ discmode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ return discmode;
+}
+
+/*
+ Returns a string that can be used in a CreateFile call if
+ c_drive letter is a character. If not NULL is returned.
+ */
+
+const char *
+is_cdrom_win32ioctl(const char c_drive_letter)
+{
+#ifdef _XBOX
+ char sz_win32_drive_full[] = "\\\\.\\X:";
+ sz_win32_drive_full[4] = c_drive_letter;
+ return strdup(sz_win32_drive_full);
+#else
+ UINT uDriveType;
+ char sz_win32_drive[4];
+
+ sz_win32_drive[0]= c_drive_letter;
+ sz_win32_drive[1]=':';
+ sz_win32_drive[2]='\\';
+ sz_win32_drive[3]='\0';
+
+ uDriveType = GetDriveType(sz_win32_drive);
+
+ switch(uDriveType) {
+ case DRIVE_CDROM: {
+ char sz_win32_drive_full[] = "\\\\.\\X:";
+ sz_win32_drive_full[4] = c_drive_letter;
+ return strdup(sz_win32_drive_full);
+ }
+ default:
+ cdio_debug("Drive %c is not a CD-ROM", c_drive_letter);
+ return NULL;
+ }
+#endif
+}
+
+/*!
+ Reads an audio device using the DeviceIoControl method into data
+ starting from lsn. Returns 0 if no error.
+ */
+int
+read_audio_sectors_win32ioctl (_img_private_t *env, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ DWORD dwBytesReturned;
+ RAW_READ_INFO cdrom_raw;
+
+ /* Initialize CDROM_RAW_READ structure */
+ cdrom_raw.DiskOffset.QuadPart = CDIO_CD_FRAMESIZE_RAW * lsn;
+ cdrom_raw.SectorCount = nblocks;
+ cdrom_raw.TrackMode = CDDA;
+
+ if( DeviceIoControl( env->h_device_handle,
+ IOCTL_CDROM_RAW_READ, &cdrom_raw,
+ sizeof(RAW_READ_INFO), data,
+ CDIO_CD_FRAMESIZE_RAW * nblocks,
+ &dwBytesReturned, NULL ) == 0 ) {
+ char *psz_msg = NULL;
+ long int i_err = GetLastError();
+ FORMAT_ERROR(i_err, psz_msg);
+ cdio_info("Error reading audio-mode %lu\n%s)",
+ (long unsigned int) lsn, psz_msg);
+ LocalFree(psz_msg);
+ return 1;
+ }
+ return 0;
+}
+
+/*!
+ Reads a single raw sector using the DeviceIoControl method into
+ data starting from lsn. Returns 0 if no error.
+ */
+static int
+read_raw_sector (const _img_private_t *p_env, void *p_buf, lsn_t lsn)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+
+ /* ReadCD CDB12 command. The values were taken from MMC1 draft paper. */
+ CDIO_MMC_SET_COMMAND (cdb.field, CDIO_MMC_GPCMD_READ_CD);
+ CDIO_MMC_SET_READ_LBA (cdb.field, lsn);
+ CDIO_MMC_SET_READ_LENGTH24(cdb.field, 1);
+
+ cdb.field[9]=0xF8; /* Raw read, 2352 bytes per sector */
+
+ return run_scsi_cmd_win32ioctl(p_env, OP_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ CDIO_CD_FRAMESIZE_RAW, p_buf);
+}
+
+/*!
+ Reads a single mode2 sector using the DeviceIoControl method into
+ data starting from lsn. Returns 0 if no error.
+ */
+int
+read_mode2_sector_win32ioctl (const _img_private_t *p_env, void *p_data,
+ lsn_t lsn, bool b_form2)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ int ret = read_raw_sector (p_env, buf, lsn);
+
+ if ( 0 != ret) return ret;
+
+ memcpy (p_data,
+ buf + CDIO_CD_SYNC_SIZE + CDIO_CD_XA_HEADER,
+ b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE);
+
+ return 0;
+
+}
+
+/*!
+ Reads a single mode2 sector using the DeviceIoControl method into
+ data starting from lsn. Returns 0 if no error.
+ */
+int
+read_mode1_sector_win32ioctl (const _img_private_t *env, void *data,
+ lsn_t lsn, bool b_form2)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ int ret = read_raw_sector (env, buf, lsn);
+
+ if ( 0 != ret) return ret;
+
+ memcpy (data,
+ buf + CDIO_CD_SYNC_SIZE+CDIO_CD_HEADER_SIZE,
+ b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE);
+
+ return 0;
+
+}
+
+/*!
+ Initialize internal structures for CD device.
+ */
+bool
+init_win32ioctl (_img_private_t *env)
+{
+#ifdef WIN32
+ OSVERSIONINFO ov;
+#endif
+
+#ifdef _XBOX
+ ANSI_STRING filename;
+ OBJECT_ATTRIBUTES attributes;
+ IO_STATUS_BLOCK status;
+ HANDLE hDevice;
+ NTSTATUS error;
+#else
+ unsigned int len=strlen(env->gen.source_name);
+ char psz_win32_drive[7];
+ DWORD dw_access_flags;
+#endif
+
+ cdio_debug("using winNT/2K/XP ioctl layer");
+
+#ifdef WIN32
+ memset(&ov,0,sizeof(OSVERSIONINFO));
+ ov.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
+ GetVersionEx(&ov);
+
+ if((ov.dwPlatformId==VER_PLATFORM_WIN32_NT) &&
+ (ov.dwMajorVersion>4))
+ dw_access_flags = GENERIC_READ|GENERIC_WRITE; /* add gen write on W2k/XP */
+ else dw_access_flags = GENERIC_READ;
+#endif
+
+ if (cdio_is_device_win32(env->gen.source_name))
+ {
+#ifdef _XBOX
+ // Use XBOX cdrom, no matter what drive letter is given.
+ RtlInitAnsiString(&filename,"\\Device\\Cdrom0");
+ InitializeObjectAttributes(&attributes, &filename, OBJ_CASE_INSENSITIVE,
+ NULL);
+ error = NtCreateFile( &hDevice,
+ GENERIC_READ |SYNCHRONIZE | FILE_READ_ATTRIBUTES,
+ &attributes,
+ &status,
+ NULL,
+ 0,
+ FILE_SHARE_READ,
+ FILE_OPEN,
+ FILE_NON_DIRECTORY_FILE
+ | FILE_SYNCHRONOUS_IO_NONALERT );
+
+ if (!NT_SUCCESS(error))
+ {
+ return false;
+ }
+ env->h_device_handle = hDevice;
+#else
+ sprintf( psz_win32_drive, "\\\\.\\%c:", env->gen.source_name[len-2] );
+
+ env->h_device_handle = CreateFile( psz_win32_drive,
+ dw_access_flags,
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ NULL,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL );
+
+ if( env->h_device_handle == INVALID_HANDLE_VALUE )
+ {
+ /* No good. try toggle write. */
+ dw_access_flags ^= GENERIC_WRITE;
+ env->h_device_handle = CreateFile( psz_win32_drive,
+ dw_access_flags,
+ FILE_SHARE_READ,
+ NULL,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL );
+ if (env->h_device_handle == NULL)
+ return false;
+ }
+#endif
+ env->b_ioctl_init = true;
+ return true;
+ }
+ return false;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ via a SCSI MMC READ_TOC (FULTOC). Return true if successful or
+ false if an error.
+*/
+static bool
+read_fulltoc_win32mmc (_img_private_t *p_env)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+ CDROM_TOC_FULL cdrom_toc_full;
+ int i_status, i, i_track_format, i_seen_flag;
+
+ /* Operation code */
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_TOC);
+
+ cdb.field[1] = 0x00;
+
+ /* Format */
+ cdb.field[2] = CDIO_MMC_READTOC_FMT_FULTOC;
+
+ memset(&cdrom_toc_full, 0, sizeof(cdrom_toc_full));
+
+ /* Setup to read header, to get length of data */
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, sizeof(cdrom_toc_full));
+
+ i_status = run_scsi_cmd_win32ioctl (p_env, 1000*60*3,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ sizeof(cdrom_toc_full), &cdrom_toc_full);
+
+ if ( 0 != i_status ) {
+ cdio_debug ("SCSI MMC READ_TOC failed\n");
+ return false;
+ }
+
+ i_seen_flag=0;
+ for( i = 0 ; i <= CDIO_CD_MAX_TRACKS+3; i++ ) {
+
+ if ( 0xA0 == cdrom_toc_full.TrackData[i].POINT ) {
+ /* First track number */
+ p_env->gen.i_first_track = cdrom_toc_full.TrackData[i].PMIN;
+ i_track_format = cdrom_toc_full.TrackData[i].PSEC;
+ i_seen_flag|=0x01;
+ }
+
+ if ( 0xA1 == cdrom_toc_full.TrackData[i].POINT ) {
+ /* Last track number */
+ p_env->gen.i_tracks =
+ cdrom_toc_full.TrackData[i].PMIN - p_env->gen.i_first_track + 1;
+ i_seen_flag|=0x02;
+ }
+
+ if ( 0xA2 == cdrom_toc_full.TrackData[i].POINT ) {
+ /* Start position of the lead out */
+ p_env->tocent[ p_env->gen.i_tracks ].start_lsn =
+ cdio_msf3_to_lba(
+ cdrom_toc_full.TrackData[i].PMIN,
+ cdrom_toc_full.TrackData[i].PSEC,
+ cdrom_toc_full.TrackData[i].PFRAME );
+ p_env->tocent[ p_env->gen.i_tracks ].Control
+ = cdrom_toc_full.TrackData[i].Control;
+ p_env->tocent[ p_env->gen.i_tracks ].Format = i_track_format;
+ i_seen_flag|=0x04;
+ }
+
+ if (cdrom_toc_full.TrackData[i].POINT > 0
+ && cdrom_toc_full.TrackData[i].POINT <= p_env->gen.i_tracks) {
+ p_env->tocent[ cdrom_toc_full.TrackData[i].POINT - 1 ].start_lsn =
+ cdio_msf3_to_lba(
+ cdrom_toc_full.TrackData[i].PMIN,
+ cdrom_toc_full.TrackData[i].PSEC,
+ cdrom_toc_full.TrackData[i].PFRAME );
+ p_env->tocent[ cdrom_toc_full.TrackData[i].POINT - 1 ].Control =
+ cdrom_toc_full.TrackData[i].Control;
+ p_env->tocent[ cdrom_toc_full.TrackData[i].POINT - 1 ].Format =
+ i_track_format;
+
+ cdio_debug("p_sectors: %i, %lu", i,
+ (unsigned long int) (p_env->tocent[i].start_lsn));
+
+ if (cdrom_toc_full.TrackData[i].POINT == p_env->gen.i_tracks)
+ i_seen_flag|=0x08;
+ }
+
+ if ( 0x0F == i_seen_flag ) break;
+ }
+ if ( 0x0F == i_seen_flag ) {
+ p_env->gen.toc_init = true;
+ return true;
+ }
+ return false;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return true if successful or false if an error.
+*/
+bool
+read_toc_win32ioctl (_img_private_t *p_env)
+{
+ CDROM_TOC cdrom_toc;
+ DWORD dwBytesReturned;
+ unsigned int i;
+
+ if ( ! p_env ) return false;
+
+ if ( read_fulltoc_win32mmc(p_env) ) return true;
+
+ /* SCSI-MMC READ_TOC (FULTOC) read failed. Try reading TOC via
+ DeviceIoControl instead */
+ if( DeviceIoControl( p_env->h_device_handle,
+ IOCTL_CDROM_READ_TOC,
+ NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
+ &dwBytesReturned, NULL ) == 0 ) {
+ char *psz_msg = NULL;
+ long int i_err = GetLastError();
+ FORMAT_ERROR(i_err, psz_msg);
+ if (psz_msg) {
+ cdio_warn("could not read TOC (%ld): %s", i_err, psz_msg);
+ LocalFree(psz_msg);
+ } else
+ cdio_warn("could not read TOC (%ld)", i_err);
+ return false;
+ }
+
+ p_env->gen.i_first_track = cdrom_toc.FirstTrack;
+ p_env->gen.i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
+
+ for( i = 0 ; i <= p_env->gen.i_tracks ; i++ ) {
+ p_env->tocent[ i ].start_lsn =
+ cdio_msf3_to_lba( cdrom_toc.TrackData[i].Address[1],
+ cdrom_toc.TrackData[i].Address[2],
+ cdrom_toc.TrackData[i].Address[3] );
+ p_env->tocent[ i ].Control = cdrom_toc.TrackData[i].Control;
+ p_env->tocent[ i ].Format = cdrom_toc.TrackData[i].Format;
+ cdio_debug("p_sectors: %i, %lu", i,
+ (unsigned long int) (p_env->tocent[i].start_lsn));
+ }
+ p_env->gen.toc_init = true;
+ return true;
+}
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+char *
+get_mcn_win32ioctl (const _img_private_t *env) {
+
+ DWORD dwBytesReturned;
+ SUB_Q_MEDIA_CATALOG_NUMBER mcn;
+ CDROM_SUB_Q_DATA_FORMAT q_data_format;
+
+ memset( &mcn, 0, sizeof(mcn) );
+
+ q_data_format.Format = CDIO_SUBCHANNEL_MEDIA_CATALOG;
+
+ q_data_format.Track=1;
+
+ if( DeviceIoControl( env->h_device_handle,
+ IOCTL_CDROM_READ_Q_CHANNEL,
+ &q_data_format, sizeof(q_data_format),
+ &mcn, sizeof(mcn),
+ &dwBytesReturned, NULL ) == 0 ) {
+ cdio_warn( "could not read Q Channel at track %d", 1);
+ } else if (mcn.Mcval)
+ return strdup(mcn.MediaCatalog);
+ return NULL;
+}
+
+/*!
+ Get the format (XA, DATA, AUDIO) of a track.
+*/
+track_format_t
+get_track_format_win32ioctl(const _img_private_t *env, track_t i_track)
+{
+ /* This is pretty much copied from the "badly broken" cdrom_count_tracks
+ in linux/cdrom.c.
+ */
+
+ if (env->tocent[i_track - env->gen.i_first_track].Control & 0x04) {
+ if (env->tocent[i_track - env->gen.i_first_track].Format == 0x10)
+ return TRACK_FORMAT_CDI;
+ else if (env->tocent[i_track - env->gen.i_first_track].Format == 0x20)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+}
+
+#endif /*HAVE_WIN32_CDROM*/
diff --git a/contrib/libcdio/Makefile.am b/contrib/libcdio/Makefile.am
new file mode 100644
index 000000000..a79525c8f
--- /dev/null
+++ b/contrib/libcdio/Makefile.am
@@ -0,0 +1,62 @@
+include $(top_srcdir)/misc/Makefile.common
+
+AM_CFLAGS = $(DEFAULT_OCFLAGS) $(VISIBILITY_FLAG)
+AM_LDFLAGS = $(xineplug_ldflags)
+
+SUBDIRS = cdio MSWindows image
+
+INCLUDES = $(LIBCDIO_CFLAGS) -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/lib -I$(top_builddir)/lib
+
+noinst_HEADERS = \
+ cdio_assert.h \
+ _cdio_stdio.h \
+ scsi_mmc.h \
+ cdio_private.h \
+ _cdio_stream.h \
+ iso9660_private.h \
+ portable.h
+
+noinst_LTLIBRARIES = libcdio.la libiso9660.la
+
+libcdio_la_SOURCES = \
+ _cdio_bsdi.c \
+ _cdio_generic.c \
+ _cdio_linux.c \
+ _cdio_osx.c \
+ _cdio_stdio.c \
+ _cdio_stdio.h \
+ _cdio_stream.c \
+ _cdio_stream.h \
+ _cdio_sunos.c \
+ cd_types.c \
+ cdio.c \
+ cdtext.c \
+ cdtext_private.h \
+ ds.c \
+ FreeBSD/freebsd.c \
+ FreeBSD/freebsd.h \
+ FreeBSD/freebsd_cam.c \
+ FreeBSD/freebsd_ioctl.c \
+ generic.h \
+ image.h \
+ image/bincue.c \
+ image/cdrdao.c \
+ image_common.h \
+ image/nrg.c \
+ image/nrg.h \
+ MSWindows/aspi32.c \
+ MSWindows/aspi32.h \
+ MSWindows/win32_ioctl.c \
+ MSWindows/win32.c \
+ MSWindows/win32.h \
+ logging.c \
+ scsi_mmc.c \
+ scsi_mmc_private.h \
+ sector.c \
+ util.c
+
+libiso9660_la_SOURCES = \
+ iso9660.c \
+ iso9660_private.h \
+ iso9660_fs.c \
+ xa.c
diff --git a/contrib/libcdio/_cdio_bsdi.c b/contrib/libcdio/_cdio_bsdi.c
new file mode 100644
index 000000000..aef535752
--- /dev/null
+++ b/contrib/libcdio/_cdio_bsdi.c
@@ -0,0 +1,846 @@
+/*
+ $Id: _cdio_bsdi.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains BSDI-specific code and implements low-level
+ control of the CD drive.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_bsdi.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $";
+
+#include <cdio/logging.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+
+#define DEFAULT_CDIO_DEVICE "/dev/rsr0c"
+#include <string.h>
+
+#ifdef HAVE_BSDI_CDROM
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+/*#define USE_ETC_FSTAB*/
+#ifdef USE_ETC_FSTAB
+#include <fstab.h>
+#endif
+
+#include <dvd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include </sys/dev/scsi/scsi.h>
+#include </sys/dev/scsi/scsi_ioctl.h>
+#include "cdtext_private.h"
+
+typedef enum {
+ _AM_NONE,
+ _AM_IOCTL,
+} access_mode_t;
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ access_mode_t access_mode;
+
+ /* Some of the more OS specific things. */
+ /* Track information */
+ struct cdrom_tochdr tochdr;
+ struct cdrom_tocentry tocent[CDIO_CD_MAX_TRACKS+1];
+
+} _img_private_t;
+
+/* Define the Cdrom Generic Command structure */
+typedef struct cgc
+{
+ scsi_mmc_cdb_t cdb;
+ u_char *buf;
+ int buflen;
+ int rw;
+ unsigned int timeout;
+ scsi_user_sense_t *sus;
+} cgc_t;
+
+
+/*
+ This code adapted from Steven M. Schultz's libdvd
+*/
+static int
+run_scsi_cmd_bsdi(const void *p_user_data, unsigned int i_timeout_ms,
+ unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf )
+{
+ const _img_private_t *p_env = p_user_data;
+ int i_status, i_asc;
+ struct scsi_user_cdb suc;
+ struct scsi_sense *sp;
+
+ again:
+ suc.suc_flags = SCSI_MMC_DATA_READ == e_direction ?
+ SUC_READ : SUC_WRITE;
+ suc.suc_cdblen = i_cdb;
+ memcpy(suc.suc_cdb, p_cdb, i_cdb);
+ suc.suc_data = p_buf;
+ suc.suc_datalen = i_buf;
+ suc.suc_timeout = msecs2secs(i_timeout_ms);
+ if (ioctl(p_env->gen.fd, SCSIRAWCDB, &suc) == -1)
+ return(errno);
+ i_status = suc.suc_sus.sus_status;
+
+#if 0
+ /*
+ * If the device returns a scsi sense error and debugging is enabled print
+ * some hopefully useful information on stderr.
+ */
+ if (i_status && debug)
+ {
+ unsigned char *cp;
+ int i;
+ cp = suc.suc_sus.sus_sense;
+ fprintf(stderr,"i_status = %x cdb =",
+ i_status);
+ for (i = 0; i < cdblen; i++)
+ fprintf(stderr, " %x", cgc->cdb[i]);
+ fprintf(stderr, "\nsense =");
+ for (i = 0; i < 16; i++)
+ fprintf(stderr, " %x", cp[i]);
+ fprintf(stderr, "\n");
+ }
+#endif
+
+ /*
+ * HACK! Some drives return a silly "medium changed" on the first
+ * command AND a non-zero i_status which gets turned into a fatal
+ * (EIO) error even though the operation was a success. Retrying
+ * the operation clears the media changed status and gets the
+ * answer. */
+
+ sp = (struct scsi_sense *)&suc.suc_sus.sus_sense;
+ i_asc = XSENSE_ASC(sp);
+ if (i_status == STS_CHECKCOND && i_asc == 0x28)
+ goto again;
+#if 0
+ if (cgc->sus)
+ memcpy(cgc->sus, &suc.suc_sus, sizeof (struct scsi_user_sense));
+#endif
+
+ return(i_status);
+}
+
+
+
+/* Check a drive to see if it is a CD-ROM
+ Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive
+ and -1 if no device exists .
+*/
+static bool
+cdio_is_cdrom(char *drive, char *mnttype)
+{
+ bool is_cd=false;
+ int cdfd;
+ struct cdrom_tochdr tochdr;
+
+ /* If it doesn't exist, return -1 */
+ if ( !cdio_is_device_quiet_generic(drive) ) {
+ return(false);
+ }
+
+ /* If it does exist, verify that it's an available CD-ROM */
+ cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
+
+ /* Should we want to test the condition in more detail:
+ ENOENT is the error for /dev/xxxxx does not exist;
+ ENODEV means there's no drive present. */
+
+ if ( cdfd >= 0 ) {
+ if ( ioctl(cdfd, CDROMREADTOCHDR, &tochdr) != -1 ) {
+ is_cd = true;
+ }
+ close(cdfd);
+ }
+ /* Even if we can't read it, it might be mounted */
+ else if ( mnttype && (strcmp(mnttype, "cd9660") == 0) ) {
+ is_cd = true;
+ }
+ return(is_cd);
+}
+
+/*!
+ Initialize CD device.
+ */
+static bool
+_cdio_init (_img_private_t *p_env)
+{
+ if (p_env->gen.init) {
+ cdio_warn ("init called more than once");
+ return false;
+ }
+
+ p_env->gen.fd = open (p_env->gen.source_name, O_RDONLY, 0);
+
+ if (p_env->gen.fd < 0)
+ {
+ cdio_warn ("open (%s): %s", p_env->gen.source_name, strerror (errno));
+ return false;
+ }
+
+ p_env->gen.init = true;
+ p_env->gen.toc_init = false;
+ return true;
+}
+
+/* Read audio sectors
+*/
+static int
+_read_audio_sectors_bsdi (void *user_data, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ struct cdrom_msf *msf = (struct cdrom_msf *) &buf;
+ msf_t _msf;
+
+ _img_private_t *p_env = user_data;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ msf->cdmsf_min0 = cdio_from_bcd8(_msf.m);
+ msf->cdmsf_sec0 = cdio_from_bcd8(_msf.s);
+ msf->cdmsf_frame0 = cdio_from_bcd8(_msf.f);
+
+ if (p_env->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged < 75
+ || (p_env->gen.ioctls_debugged < (30 * 75)
+ && p_env->gen.ioctls_debugged % 75 == 0)
+ || p_env->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %2.2d:%2.2d:%2.2d",
+ msf->cdmsf_min0, msf->cdmsf_sec0, msf->cdmsf_frame0);
+
+ p_env->gen.ioctls_debugged++;
+
+ switch (p_env->access_mode) {
+ case _AM_NONE:
+ cdio_warn ("no way to read audio");
+ return 1;
+ break;
+
+ case _AM_IOCTL: {
+ unsigned int i;
+ for (i=0; i < nblocks; i++) {
+ if (ioctl (p_env->gen.fd, CDROMREADRAW, &buf) == -1) {
+ perror ("ioctl()");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE_RAW * i), buf,
+ CDIO_CD_FRAMESIZE_RAW);
+ }
+ break;
+ }
+ }
+
+ return 0;
+}
+
+/*!
+ Reads a single mode1 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode1_sector_bsdi (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+
+#if FIXED
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ do something here.
+#else
+ return cdio_generic_read_form1_sector(user_data, data, lsn);
+#endif
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode1_sectors_bsdi (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *p_env = user_data;
+ unsigned int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode1_sector_bsdi (p_env,
+ ((char *)data) + (blocksize * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode2_sector_bsdi (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ struct cdrom_msf *msf = (struct cdrom_msf *) &buf;
+ msf_t _msf;
+
+ _img_private_t *p_env = user_data;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ msf->cdmsf_min0 = cdio_from_bcd8(_msf.m);
+ msf->cdmsf_sec0 = cdio_from_bcd8(_msf.s);
+ msf->cdmsf_frame0 = cdio_from_bcd8(_msf.f);
+
+ if (p_env->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged < 75
+ || (p_env->gen.ioctls_debugged < (30 * 75)
+ && p_env->gen.ioctls_debugged % 75 == 0)
+ || p_env->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %2.2d:%2.2d:%2.2d",
+ msf->cdmsf_min0, msf->cdmsf_sec0, msf->cdmsf_frame0);
+
+ p_env->gen.ioctls_debugged++;
+
+ switch (p_env->access_mode)
+ {
+ case _AM_NONE:
+ cdio_warn ("no way to read mode2");
+ return 1;
+ break;
+
+ case _AM_IOCTL:
+ if (ioctl (p_env->gen.fd, CDROMREADMODE2, &buf) == -1)
+ {
+ perror ("ioctl()");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+ break;
+ }
+
+ if (b_form2)
+ memcpy (data, buf, M2RAW_SECTOR_SIZE);
+ else
+ memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode2_sectors_bsdi (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *p_env = user_data;
+ unsigned int i;
+ unsigned int i_blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ /* For each frame, pick out the data part we need */
+ for (i = 0; i < nblocks; i++) {
+ int retval = _read_mode2_sector_bsdi(p_env,
+ ((char *)data) +
+ (i_blocksize * i),
+ lsn + i, b_form2);
+ if (retval) return retval;
+ }
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_stat_size_bsdi (void *user_data)
+{
+ _img_private_t *p_env = user_data;
+
+ struct cdrom_tocentry tocent;
+ uint32_t size;
+
+ tocent.cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ tocent.cdte_format = CDROM_LBA;
+ if (ioctl (p_env->gen.fd, CDROMREADTOCENTRY, &tocent) == -1)
+ {
+ perror ("ioctl(CDROMREADTOCENTRY)");
+ exit (EXIT_FAILURE);
+ }
+
+ size = tocent.cdte_addr.lba;
+
+ return size;
+}
+
+/*!
+ Set the key "arg" to "value" in source device.
+*/
+static int
+_set_arg_bsdi (void *user_data, const char key[], const char value[])
+{
+ _img_private_t *p_env = user_data;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (p_env->gen.source_name);
+
+ p_env->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ if (!strcmp(value, "IOCTL"))
+ p_env->access_mode = _AM_IOCTL;
+ else
+ cdio_warn ("unknown access type: %s. ignored.", value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return false if successful or true if an error.
+*/
+static bool
+read_toc_bsdi (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+ int i;
+
+ /* read TOC header */
+ if ( ioctl(p_env->gen.fd, CDROMREADTOCHDR, &p_env->tochdr) == -1 ) {
+ cdio_warn("%s: %s\n",
+ "error in ioctl CDROMREADTOCHDR", strerror(errno));
+ return false;
+ }
+
+ p_env->gen.i_first_track = p_env->tochdr.cdth_trk0;
+ p_env->gen.i_tracks = p_env->tochdr.cdth_trk1;
+
+ /* read individual tracks */
+ for (i= p_env->gen.i_first_track; i<=p_env->gen.i_tracks; i++) {
+ p_env->tocent[i-1].cdte_track = i;
+ p_env->tocent[i-1].cdte_format = CDROM_MSF;
+ if (ioctl(p_env->gen.fd, CDROMREADTOCENTRY, &p_env->tocent[i-1]) == -1) {
+ cdio_warn("%s %d: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for track",
+ i, strerror(errno));
+ return false;
+ }
+ /****
+ struct cdrom_msf0 *msf= &p_env->tocent[i-1].cdte_addr.msf;
+
+ fprintf (stdout, "--- track# %d (msf %2.2x:%2.2x:%2.2x)\n",
+ i, msf->minute, msf->second, msf->frame);
+ ****/
+
+ }
+
+ /* read the lead-out track */
+ p_env->tocent[p_env->gen.i_tracks].cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ p_env->tocent[p_env->gen.i_tracks].cdte_format = CDROM_MSF;
+
+ if (ioctl(p_env->gen.fd, CDROMREADTOCENTRY,
+ &p_env->tocent[p_env->gen.i_tracks]) == -1 ) {
+ cdio_warn("%s: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for lead-out",
+ strerror(errno));
+ return false;
+ }
+
+ /*
+ struct cdrom_msf0 *msf= &p_env->tocent[p_env->gen.i_tracks].cdte_addr.msf;
+
+ fprintf (stdout, "--- track# %d (msf %2.2x:%2.2x:%2.2x)\n",
+ i, msf->minute, msf->second, msf->frame);
+ */
+
+ p_env->gen.toc_init = true;
+ return true;
+}
+
+/*!
+ Eject media in CD drive. If successful, as a side effect we
+ also free obj.
+ */
+static int
+_eject_media_bsdi (void *user_data) {
+
+ _img_private_t *p_env = user_data;
+ int ret=2;
+ int status;
+ int fd;
+
+ close(p_env->gen.fd);
+ p_env->gen.fd = -1;
+ if ((fd = open (p_env->gen.source_name, O_RDONLY|O_NONBLOCK)) > -1) {
+ if((status = ioctl(fd, CDROM_DRIVE_STATUS, (void *) CDSL_CURRENT)) > 0) {
+ switch(status) {
+ case CDS_TRAY_OPEN:
+ if((ret = ioctl(fd, CDROMCLOSETRAY, 0)) != 0) {
+ cdio_warn ("ioctl CDROMCLOSETRAY failed: %s\n", strerror(errno));
+ }
+ break;
+ case CDS_DISC_OK:
+ if((ret = ioctl(fd, CDROMEJECT, 0)) != 0) {
+ cdio_warn("ioctl CDROMEJECT failed: %s\n", strerror(errno));
+ }
+ break;
+ }
+ ret=0;
+ } else {
+ cdio_warn ("CDROM_DRIVE_STATUS failed: %s\n", strerror(errno));
+ ret=1;
+ }
+ close(fd);
+ }
+ return 2;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_get_arg_bsdi (void *user_data, const char key[])
+{
+ _img_private_t *p_env = user_data;
+
+ if (!strcmp (key, "source")) {
+ return p_env->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (p_env->access_mode) {
+ case _AM_IOCTL:
+ return "ioctl";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+/*!
+ Return the media catalog number MCN.
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+ */
+static char *
+_get_mcn_bsdi (const void *user_data) {
+
+ struct cdrom_mcn mcn;
+ const _img_private_t *p_env = user_data;
+ if (ioctl(p_env->gen.fd, CDROM_GET_MCN, &mcn) != 0)
+ return NULL;
+ return strdup(mcn.medium_catalog_number);
+}
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+get_track_format_bsdi(void *user_data, track_t i_track)
+{
+ _img_private_t *p_env = user_data;
+
+ if (!p_env->gen.toc_init) read_toc_bsdi (p_env) ;
+
+ if (i_track > p_env->gen.i_tracks || i_track == 0)
+ return TRACK_FORMAT_ERROR;
+
+ i_track -= p_env->gen.i_first_track;
+
+ /* This is pretty much copied from the "badly broken" cdrom_count_tracks
+ in linux/cdrom.c.
+ */
+ if (p_env->tocent[i_track].cdte_ctrl & CDROM_DATA_TRACK) {
+ if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_CDI_TRACK)
+ return TRACK_FORMAT_CDI;
+ else if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_XA_TRACK)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_get_track_green_bsdi(void *user_data, track_t i_track)
+{
+ _img_private_t *p_env = user_data;
+
+ if (!p_env->gen.toc_init) read_toc_bsdi (p_env) ;
+
+ if (i_track == CDIO_CDROM_LEADOUT_TRACK) i_track = p_env->gen.i_tracks+1;
+
+ if (i_track > p_env->gen.i_tracks+1 || i_track == 0)
+ return false;
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cdinfo for a while.
+ */
+ return ((p_env->tocent[i_track-1].cdte_ctrl & 2) != 0);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ i_track in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using i_track LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static bool
+_get_track_msf_bsdi(void *user_data, track_t i_track, msf_t *msf)
+{
+ _img_private_t *p_env = user_data;
+
+ if (NULL == msf) return false;
+
+ if (!p_env->gen.toc_init) read_toc_bsdi (p_env) ;
+
+ if (i_track == CDIO_CDROM_LEADOUT_TRACK) i_track = p_env->gen.i_tracks+1;
+
+ if (i_track > p_env->gen.i_tracks+1 || i_track == 0) {
+ return false;
+ }
+
+ i_track -= p_env->gen.i_first_track;
+
+ {
+ struct cdrom_msf0 *msf0= &p_env->tocent[i_track].cdte_addr.msf;
+ msf->m = cdio_to_bcd8(msf0->minute);
+ msf->s = cdio_to_bcd8(msf0->second);
+ msf->f = cdio_to_bcd8(msf0->frame);
+ return true;
+ }
+}
+
+#endif /* HAVE_BSDI_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_bsdi (void)
+{
+#ifndef HAVE_BSDI_CDROM
+ return NULL;
+#else
+ char drive[40];
+ char **drives = NULL;
+ unsigned int num_drives=0;
+ bool exists=true;
+ char c;
+
+ /* Scan the system for CD-ROM drives.
+ */
+
+#ifdef USE_ETC_FSTAB
+
+ struct fstab *fs;
+ setfsent();
+
+ /* Check what's in /etc/fstab... */
+ while ( (fs = getfsent()) )
+ {
+ if (strncmp(fs->fs_spec, "/dev/sr", 7))
+ cdio_add_device_list(&drives, fs->fs_spec, &num_drives);
+ }
+
+#endif
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+ for ( c='0'; exists && c <='9'; c++ ) {
+ sprintf(drive, "/dev/rsr%cc", c);
+ exists = cdio_is_cdrom(drive, NULL);
+ if ( exists ) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+ }
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /*HAVE_BSDI_CDROM*/
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char *
+cdio_get_default_device_bsdi(void)
+{
+ return strdup(DEFAULT_CDIO_DEVICE);
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_am_bsdi (const char *psz_source_name, const char *psz_access_mode)
+{
+ if (psz_access_mode != NULL)
+ cdio_warn ("there is only one access mode for bsdi. Arg %s ignored",
+ psz_access_mode);
+ return cdio_open_bsdi(psz_source_name);
+}
+
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_bsdi (const char *psz_orig_source)
+{
+
+#ifdef HAVE_BSDI_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+ char *psz_source;
+
+ cdio_funcs _funcs = {
+ .eject_media = _eject_media_bsdi,
+ .free = cdio_generic_free,
+ .get_arg = _get_arg_bsdi,
+ .get_cdtext = get_cdtext_generic,
+ .get_default_device = cdio_get_default_device_bsdi,
+ .get_devices = cdio_get_devices_bsdi,
+ .get_drive_cap = scsi_mmc_get_drive_cap_generic,
+ .get_discmode = get_discmode_generic,
+ .get_first_track_num= get_first_track_num_generic,
+ .get_hwinfo = NULL,
+ .get_mcn = _get_mcn_bsdi,
+ .get_num_tracks = get_num_tracks_generic,
+ .get_track_format = get_track_format_bsdi,
+ .get_track_green = _get_track_green_bsdi,
+ .get_track_lba = NULL, /* This could be implemented if need be. */
+ .get_track_msf = _get_track_msf_bsdi,
+ .lseek = cdio_generic_lseek,
+ .read = cdio_generic_read,
+ .read_audio_sectors = _read_audio_sectors_bsdi,
+ .read_mode1_sector = _read_mode1_sector_bsdi,
+ .read_mode1_sectors = _read_mode1_sectors_bsdi,
+ .read_mode2_sector = _read_mode2_sector_bsdi,
+ .read_mode2_sectors = _read_mode2_sectors_bsdi,
+ .read_toc = &read_toc_bsdi,
+ .run_scsi_mmc_cmd = &run_scsi_cmd_bsdi,
+ .set_arg = _set_arg_bsdi,
+ .stat_size = _stat_size_bsdi
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->access_mode = _AM_IOCTL;
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+ _data->gen.toc_init = false;
+ _data->gen.b_cdtext_init = false;
+ _data->gen.b_cdtext_error = false;
+
+ if (NULL == psz_orig_source) {
+ psz_source=cdio_get_default_device_linux();
+ if (NULL == psz_source) return NULL;
+ _set_arg_bsdi(_data, "source", psz_source);
+ free(psz_source);
+ } else {
+ if (cdio_is_device_generic(psz_orig_source))
+ _set_arg_bsdi(_data, "source", psz_orig_source);
+ else {
+ /* The below would be okay if all device drivers worked this way. */
+#if 0
+ cdio_info ("source %s is not a device", psz_orig_source);
+#endif
+ return NULL;
+ }
+ }
+
+ ret = cdio_new ( (void *) _data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (_cdio_init(_data))
+ return ret;
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_BSDI_CDROM */
+
+}
+
+bool
+cdio_have_bsdi (void)
+{
+#ifdef HAVE_BSDI_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_BSDI_CDROM */
+}
diff --git a/contrib/libcdio/_cdio_generic.c b/contrib/libcdio/_cdio_generic.c
new file mode 100644
index 000000000..fb3ed5a9c
--- /dev/null
+++ b/contrib/libcdio/_cdio_generic.c
@@ -0,0 +1,426 @@
+/*
+ $Id: _cdio_generic.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains generic implementations of device-dirver routines.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_generic.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $";
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif /*HAVE_UNISTD_H*/
+
+#include <fcntl.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include <cdio/logging.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+#include "_cdio_stdio.h"
+#include "portable.h"
+
+/*!
+ Eject media -- there's nothing to do here. We always return 2.
+ Should we also free resources?
+ */
+int
+cdio_generic_bogus_eject_media (void *user_data) {
+ /* Sort of a stub here. Perhaps log a message? */
+ return 2;
+}
+
+
+/*!
+ Release and free resources associated with cd.
+ */
+void
+cdio_generic_free (void *p_user_data)
+{
+ generic_img_private_t *p_env = p_user_data;
+ track_t i_track;
+
+ if (NULL == p_env) return;
+ free (p_env->source_name);
+
+ for (i_track=0; i_track < p_env->i_tracks; i_track++) {
+ cdtext_destroy(&(p_env->cdtext_track[i_track]));
+ }
+
+ if (p_env->fd >= 0)
+ close (p_env->fd);
+
+ free (p_env);
+}
+
+/*!
+ Initialize CD device.
+ */
+bool
+cdio_generic_init (void *user_data)
+{
+ generic_img_private_t *p_env = user_data;
+ if (p_env->init) {
+ cdio_warn ("init called more than once");
+ return false;
+ }
+
+ p_env->fd = open (p_env->source_name, O_RDONLY, 0);
+
+ if (p_env->fd < 0)
+ {
+ cdio_warn ("open (%s): %s", p_env->source_name, strerror (errno));
+ return false;
+ }
+
+ p_env->init = true;
+ p_env->toc_init = false;
+ p_env->b_cdtext_init = false;
+ p_env->b_cdtext_error = false;
+ p_env->i_joliet_level = 0; /* Assume no Joliet extensions initally */
+ return true;
+}
+
+/*!
+ Reads a single form1 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+int
+cdio_generic_read_form1_sector (void * user_data, void *data, lsn_t lsn)
+{
+ if (0 > cdio_generic_lseek(user_data, CDIO_CD_FRAMESIZE*lsn, SEEK_SET))
+ return -1;
+ if (0 > cdio_generic_read(user_data, data, CDIO_CD_FRAMESIZE))
+ return -1;
+ return 0;
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Is in fact libc's lseek().
+*/
+off_t
+cdio_generic_lseek (void *user_data, off_t offset, int whence)
+{
+ generic_img_private_t *p_env = user_data;
+ return lseek(p_env->fd, offset, whence);
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Is in fact libc's read().
+*/
+ssize_t
+cdio_generic_read (void *user_data, void *buf, size_t size)
+{
+ generic_img_private_t *p_env = user_data;
+ return read(p_env->fd, buf, size);
+}
+
+/*!
+ Release and free resources associated with stream or disk image.
+*/
+void
+cdio_generic_stdio_free (void *user_data)
+{
+ generic_img_private_t *p_env = user_data;
+
+ if (NULL == p_env) return;
+ if (NULL != p_env->source_name)
+ free (p_env->source_name);
+
+ if (p_env->data_source)
+ cdio_stdio_destroy (p_env->data_source);
+}
+
+
+/*!
+ Return true if source_name could be a device containing a CD-ROM.
+*/
+bool
+cdio_is_device_generic(const char *source_name)
+{
+ struct stat buf;
+ if (0 != stat(source_name, &buf)) {
+ cdio_warn ("Can't get file status for %s:\n%s", source_name,
+ strerror(errno));
+ return false;
+ }
+ return (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode));
+}
+
+/*!
+ Like above, but don't give a warning device doesn't exist.
+*/
+bool
+cdio_is_device_quiet_generic(const char *source_name)
+{
+ struct stat buf;
+ if (0 != stat(source_name, &buf)) {
+ return false;
+ }
+ return (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode));
+}
+
+/*!
+ Add/allocate a drive to the end of drives.
+ Use cdio_free_device_list() to free this device_list.
+*/
+void
+cdio_add_device_list(char **device_list[], const char *drive,
+ unsigned int *num_drives)
+{
+ if (NULL != drive) {
+ unsigned int j;
+
+ /* Check if drive is already in list. */
+ for (j=0; j<*num_drives; j++) {
+ if (strcmp((*device_list)[j], drive) == 0) break;
+ }
+
+ if (j==*num_drives) {
+ /* Drive not in list. Add it. */
+ (*num_drives)++;
+ if (*device_list) {
+ *device_list = realloc(*device_list, (*num_drives) * sizeof(char *));
+ } else {
+ /* num_drives should be 0. Add assert? */
+ *device_list = malloc((*num_drives) * sizeof(char *));
+ }
+
+ (*device_list)[*num_drives-1] = strdup(drive);
+ }
+ } else {
+ (*num_drives)++;
+ if (*device_list) {
+ *device_list = realloc(*device_list, (*num_drives) * sizeof(char *));
+ } else {
+ *device_list = malloc((*num_drives) * sizeof(char *));
+ }
+ (*device_list)[*num_drives-1] = NULL;
+ }
+}
+
+
+/*!
+ Get cdtext information for a CdIo object .
+
+ @param obj the CD object that may contain CD-TEXT information.
+ @return the CD-TEXT object or NULL if obj is NULL
+ or CD-TEXT information does not exist.
+*/
+const cdtext_t *
+get_cdtext_generic (void *p_user_data, track_t i_track)
+{
+ generic_img_private_t *p_env = p_user_data;
+
+ if ( NULL == p_env ||
+ (0 != i_track
+ && i_track >= p_env->i_tracks+p_env->i_first_track ) )
+ return NULL;
+
+ if (!p_env->b_cdtext_init)
+ init_cdtext_generic(p_env);
+ if (!p_env->b_cdtext_init) return NULL;
+
+ if (0 == i_track)
+ return &(p_env->cdtext);
+ else
+ return &(p_env->cdtext_track[i_track-p_env->i_first_track]);
+
+}
+
+/*!
+ Get disc type associated with cd object.
+*/
+discmode_t
+get_discmode_generic (void *p_user_data )
+{
+ generic_img_private_t *p_env = p_user_data;
+
+ /* See if this is a DVD. */
+ cdio_dvd_struct_t dvd; /* DVD READ STRUCT for layer 0. */
+
+ dvd.physical.type = CDIO_DVD_STRUCT_PHYSICAL;
+ dvd.physical.layer_num = 0;
+ if (0 == scsi_mmc_get_dvd_struct_physical (p_env->cdio, &dvd)) {
+ switch(dvd.physical.layer[0].book_type) {
+ case CDIO_DVD_BOOK_DVD_ROM: return CDIO_DISC_MODE_DVD_ROM;
+ case CDIO_DVD_BOOK_DVD_RAM: return CDIO_DISC_MODE_DVD_RAM;
+ case CDIO_DVD_BOOK_DVD_R: return CDIO_DISC_MODE_DVD_R;
+ case CDIO_DVD_BOOK_DVD_RW: return CDIO_DISC_MODE_DVD_RW;
+ case CDIO_DVD_BOOK_DVD_PR: return CDIO_DISC_MODE_DVD_PR;
+ case CDIO_DVD_BOOK_DVD_PRW: return CDIO_DISC_MODE_DVD_PRW;
+ default: return CDIO_DISC_MODE_DVD_OTHER;
+ }
+ }
+
+ return get_discmode_cd_generic(p_user_data);
+}
+
+/*!
+ Get disc type associated with cd object.
+*/
+discmode_t
+get_discmode_cd_generic (void *p_user_data )
+{
+ generic_img_private_t *p_env = p_user_data;
+ track_t i_track;
+ discmode_t discmode=CDIO_DISC_MODE_NO_INFO;
+
+ if (!p_env->toc_init)
+ p_env->cdio->op.read_toc (p_user_data);
+
+ if (!p_env->toc_init)
+ return CDIO_DISC_MODE_NO_INFO;
+
+ for (i_track = p_env->i_first_track;
+ i_track < p_env->i_first_track + p_env->i_tracks ;
+ i_track ++) {
+ track_format_t track_fmt =
+ p_env->cdio->op.get_track_format(p_env, i_track);
+
+ switch(track_fmt) {
+ case TRACK_FORMAT_AUDIO:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_DA;
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_XA:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_DATA:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_ERROR:
+ default:
+ discmode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ return discmode;
+}
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+track_t
+get_first_track_num_generic(void *p_user_data)
+{
+ generic_img_private_t *p_env = p_user_data;
+
+ if (!p_env->toc_init)
+ p_env->cdio->op.read_toc (p_user_data);
+
+ return p_env->toc_init ? p_env->i_first_track : CDIO_INVALID_TRACK;
+}
+
+
+/*!
+ Return the number of tracks in the current medium.
+*/
+ track_t
+get_num_tracks_generic(void *p_user_data)
+{
+ generic_img_private_t *p_env = p_user_data;
+
+ if (!p_env->toc_init)
+ p_env->cdio->op.read_toc (p_user_data);
+
+ return p_env->toc_init ? p_env->i_tracks : CDIO_INVALID_TRACK;
+}
+
+void
+set_cdtext_field_generic(void *user_data, track_t i_track,
+ track_t i_first_track,
+ cdtext_field_t e_field, const char *psz_value)
+{
+ char **pp_field;
+ generic_img_private_t *env = user_data;
+
+ if( i_track == 0 )
+ pp_field = &(env->cdtext.field[e_field]);
+
+ else
+ pp_field = &(env->cdtext_track[i_track-i_first_track].field[e_field]);
+
+ *pp_field = strdup(psz_value);
+}
+
+/*!
+ Read CD-Text information for a CdIo object .
+
+ return true on success, false on error or CD-TEXT information does
+ not exist.
+*/
+bool
+init_cdtext_generic (generic_img_private_t *p_env)
+{
+ return scsi_mmc_init_cdtext_private( p_env,
+ p_env->cdio->op.run_scsi_mmc_cmd,
+ set_cdtext_field_generic
+ );
+}
+
diff --git a/contrib/libcdio/_cdio_linux.c b/contrib/libcdio/_cdio_linux.c
new file mode 100644
index 000000000..ea70d262c
--- /dev/null
+++ b/contrib/libcdio/_cdio_linux.c
@@ -0,0 +1,1197 @@
+/*
+ $Id: _cdio_linux.c,v 1.4 2006/09/26 22:18:44 dgp85 Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains Linux-specific code and implements low-level
+ control of the CD drive.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_linux.c,v 1.4 2006/09/26 22:18:44 dgp85 Exp $";
+
+#include <string.h>
+
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include <cdio/types.h>
+#include <cdio/scsi_mmc.h>
+#include <cdio/cdtext.h>
+#include "cdtext_private.h"
+#include "cdio_assert.h"
+#include "cdio_private.h"
+
+#ifdef HAVE_LINUX_CDROM
+
+#if defined(HAVE_LINUX_VERSION_H)
+# include <linux/version.h>
+# if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,16)
+# define __CDIO_LINUXCD_BUILD
+# else
+# error "You need a kernel greater than 2.2.16 to have CDROM support"
+# endif
+#else
+# error "You need <linux/version.h> to have CDROM support"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <mntent.h>
+
+#include <linux/cdrom.h>
+#include <scsi/scsi.h>
+#include <scsi/sg.h>
+#include <scsi/scsi_ioctl.h>
+#include <sys/mount.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+typedef enum {
+ _AM_NONE,
+ _AM_IOCTL,
+ _AM_READ_CD,
+ _AM_READ_10
+} access_mode_t;
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ access_mode_t access_mode;
+
+ /* Some of the more OS specific things. */
+ /* Entry info for each track, add 1 for leadout. */
+ struct cdrom_tocentry tocent[CDIO_CD_MAX_TRACKS+1];
+
+ struct cdrom_tochdr tochdr;
+
+} _img_private_t;
+
+/* Some ioctl() errno values which occur when the tray is empty */
+#define ERRNO_TRAYEMPTY(errno) \
+ ((errno == EIO) || (errno == ENOENT) || (errno == EINVAL))
+
+/**** prototypes for static functions ****/
+static bool is_cdrom_linux(const char *drive, char *mnttype);
+static bool read_toc_linux (void *p_user_data);
+static int run_scsi_cmd_linux( const void *p_user_data,
+ unsigned int i_timeout,
+ unsigned int i_cdb,
+ const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf,
+ /*in/out*/ void *p_buf );
+static access_mode_t
+
+str_to_access_mode_linux(const char *psz_access_mode)
+{
+ const access_mode_t default_access_mode = _AM_IOCTL;
+
+ if (NULL==psz_access_mode) return default_access_mode;
+
+ if (!strcmp(psz_access_mode, "IOCTL"))
+ return _AM_IOCTL;
+ else if (!strcmp(psz_access_mode, "READ_CD"))
+ return _AM_READ_CD;
+ else if (!strcmp(psz_access_mode, "READ_10"))
+ return _AM_READ_10;
+ else {
+ cdio_warn ("unknown access type: %s. Default IOCTL used.",
+ psz_access_mode);
+ return default_access_mode;
+ }
+}
+
+static char *
+check_mounts_linux(const char *mtab)
+{
+ FILE *mntfp;
+ struct mntent *mntent;
+
+ mntfp = setmntent(mtab, "r");
+ if ( mntfp != NULL ) {
+ char *tmp;
+ char *mnt_type;
+ char *mnt_dev;
+
+ while ( (mntent=getmntent(mntfp)) != NULL ) {
+ mnt_type = malloc(strlen(mntent->mnt_type) + 1);
+ if (mnt_type == NULL)
+ continue; /* maybe you'll get lucky next time. */
+
+ mnt_dev = malloc(strlen(mntent->mnt_fsname) + 1);
+ if (mnt_dev == NULL) {
+ free(mnt_type);
+ continue;
+ }
+
+ strcpy(mnt_type, mntent->mnt_type);
+ strcpy(mnt_dev, mntent->mnt_fsname);
+
+ /* Handle "supermount" filesystem mounts */
+ if ( strcmp(mnt_type, "supermount") == 0 ) {
+ tmp = strstr(mntent->mnt_opts, "fs=");
+ if ( tmp ) {
+ free(mnt_type);
+ mnt_type = strdup(tmp + strlen("fs="));
+ if ( mnt_type ) {
+ tmp = strchr(mnt_type, ',');
+ if ( tmp ) {
+ *tmp = '\0';
+ }
+ }
+ }
+ tmp = strstr(mntent->mnt_opts, "dev=");
+ if ( tmp ) {
+ free(mnt_dev);
+ mnt_dev = strdup(tmp + strlen("dev="));
+ if ( mnt_dev ) {
+ tmp = strchr(mnt_dev, ',');
+ if ( tmp ) {
+ *tmp = '\0';
+ }
+ }
+ }
+ }
+ if ( strcmp(mnt_type, "iso9660") == 0 ) {
+ if (is_cdrom_linux(mnt_dev, mnt_type) > 0) {
+ free(mnt_type);
+ endmntent(mntfp);
+ return mnt_dev;
+ }
+ }
+ free(mnt_dev);
+ free(mnt_type);
+ }
+ endmntent(mntfp);
+ }
+ return NULL;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+get_arg_linux (void *env, const char key[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source")) {
+ return _obj->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (_obj->access_mode) {
+ case _AM_IOCTL:
+ return "ioctl";
+ case _AM_READ_CD:
+ return "READ_CD";
+ case _AM_READ_10:
+ return "READ_10";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+#undef USE_LINUX_CAP
+#ifdef USE_LINUX_CAP
+/*!
+ Return the the kind of drive capabilities of device.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+static void
+get_drive_cap_linux (const void *p_user_data,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap)
+{
+ const _img_private_t *p_env = p_user_data;
+ int32_t i_drivetype;
+
+ i_drivetype = ioctl (p_env->gen.fd, CDROM_GET_CAPABILITY, CDSL_CURRENT);
+
+ if (i_drivetype < 0) {
+ *p_read_cap = CDIO_DRIVE_CAP_ERROR;
+ *p_write_cap = CDIO_DRIVE_CAP_ERROR;
+ *p_misc_cap = CDIO_DRIVE_CAP_ERROR;
+ return;
+ }
+
+ *p_read_cap = 0;
+ *p_write_cap = 0;
+ *p_misc_cap = 0;
+
+ /* Reader */
+ if (i_drivetype & CDC_PLAY_AUDIO)
+ *p_read_cap |= CDIO_DRIVE_CAP_READ_AUDIO;
+ if (i_drivetype & CDC_CD_R)
+ *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_R;
+ if (i_drivetype & CDC_CD_RW)
+ *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_RW;
+ if (i_drivetype & CDC_DVD)
+ *p_read_cap |= CDIO_DRIVE_CAP_READ_DVD_ROM;
+
+ /* Writer */
+ if (i_drivetype & CDC_CD_RW)
+ *p_read_cap |= CDIO_DRIVE_CAP_WRITE_CD_RW;
+ if (i_drivetype & CDC_DVD_R)
+ *p_read_cap |= CDIO_DRIVE_CAP_WRITE_DVD_R;
+ if (i_drivetype & CDC_DVD_RAM)
+ *p_read_cap |= CDIO_DRIVE_CAP_WRITE_DVD_RAM;
+
+ /* Misc */
+ if (i_drivetype & CDC_CLOSE_TRAY)
+ *p_misc_cap |= CDIO_DRIVE_CAP_MISC_CLOSE_TRAY;
+ if (i_drivetype & CDC_OPEN_TRAY)
+ *p_misc_cap |= CDIO_DRIVE_CAP_MISC_EJECT;
+ if (i_drivetype & CDC_LOCK)
+ *p_misc_cap |= CDIO_DRIVE_CAP_MISC_LOCK;
+ if (i_drivetype & CDC_SELECT_SPEED)
+ *p_misc_cap |= CDIO_DRIVE_CAP_MISC_SELECT_SPEED;
+ if (i_drivetype & CDC_SELECT_DISC)
+ *p_misc_cap |= CDIO_DRIVE_CAP_MISC_SELECT_DISC;
+ if (i_drivetype & CDC_MULTI_SESSION)
+ *p_misc_cap |= CDIO_DRIVE_CAP_MISC_MULTI_SESSION;
+ if (i_drivetype & CDC_MEDIA_CHANGED)
+ *p_misc_cap |= CDIO_DRIVE_CAP_MISC_MEDIA_CHANGED;
+ if (i_drivetype & CDC_RESET)
+ *p_misc_cap |= CDIO_DRIVE_CAP_MISC_RESET;
+}
+#endif
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+static char *
+get_mcn_linux (const void *p_user_data) {
+
+ struct cdrom_mcn mcn;
+ const _img_private_t *p_env = p_user_data;
+ memset(&mcn, 0, sizeof(mcn));
+ if (ioctl(p_env->gen.fd, CDROM_GET_MCN, &mcn) != 0)
+ return NULL;
+ return strdup(mcn.medium_catalog_number);
+}
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+get_track_format_linux(void *p_user_data, track_t i_track)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if ( !p_env ) return TRACK_FORMAT_ERROR;
+
+ if (!p_env->gen.toc_init) read_toc_linux (p_user_data) ;
+
+ if (i_track > (p_env->gen.i_tracks+p_env->gen.i_first_track)
+ || i_track < p_env->gen.i_first_track)
+ return TRACK_FORMAT_ERROR;
+
+ i_track -= p_env->gen.i_first_track;
+
+ /* This is pretty much copied from the "badly broken" cdrom_count_tracks
+ in linux/cdrom.c.
+ */
+ if (p_env->tocent[i_track].cdte_ctrl & CDIO_CDROM_DATA_TRACK) {
+ if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_CDI_TRACK)
+ return TRACK_FORMAT_CDI;
+ else if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_XA_TRACK)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+get_track_green_linux(void *p_user_data, track_t i_track)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if (!p_env->gen.toc_init) read_toc_linux (p_user_data) ;
+
+ if (i_track >= (p_env->gen.i_tracks+p_env->gen.i_first_track)
+ || i_track < p_env->gen.i_first_track)
+ return false;
+
+ i_track -= p_env->gen.i_first_track;
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cd-info for a while.
+ */
+ return ((p_env->tocent[i_track].cdte_ctrl & 2) != 0);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj. Track numbers usually start at something
+ greater than 0, usually 1.
+
+ The "leadout" track is specified either by
+ using i_track LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static bool
+get_track_msf_linux(void *p_user_data, track_t i_track, msf_t *msf)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if (NULL == msf) return false;
+
+ if (!p_env->gen.toc_init) read_toc_linux (p_user_data) ;
+
+ if (i_track == CDIO_CDROM_LEADOUT_TRACK)
+ i_track = p_env->gen.i_tracks + p_env->gen.i_first_track;
+
+ if (i_track > (p_env->gen.i_tracks+p_env->gen.i_first_track)
+ || i_track < p_env->gen.i_first_track) {
+ return false;
+ } else {
+ struct cdrom_msf0 *msf0=
+ &p_env->tocent[i_track-p_env->gen.i_first_track].cdte_addr.msf;
+ msf->m = cdio_to_bcd8(msf0->minute);
+ msf->s = cdio_to_bcd8(msf0->second);
+ msf->f = cdio_to_bcd8(msf0->frame);
+ return true;
+ }
+}
+
+/*!
+ Eject media in CD drive.
+ Return 0 if success and 1 for failure, and 2 if no routine.
+ */
+static int
+eject_media_linux (void *p_user_data) {
+
+ _img_private_t *p_env = p_user_data;
+ int ret=2;
+ int status;
+ int fd;
+
+ if ((fd = open (p_env->gen.source_name, O_RDONLY|O_NONBLOCK)) > -1) {
+ if((status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT)) > 0) {
+ switch(status) {
+ case CDS_TRAY_OPEN:
+ if((ret = ioctl(fd, CDROMCLOSETRAY)) != 0) {
+ cdio_warn ("ioctl CDROMCLOSETRAY failed: %s\n", strerror(errno));
+ ret = 1;
+ }
+ break;
+ case CDS_DISC_OK:
+ if((ret = ioctl(fd, CDROMEJECT)) != 0) {
+ int eject_error = errno;
+ /* Try ejecting the MMC way... */
+ ret = scsi_mmc_eject_media(p_env->gen.cdio);
+ if (0 != ret) {
+ cdio_warn("ioctl CDROMEJECT failed: %s\n",
+ strerror(eject_error));
+ ret = 1;
+ }
+ }
+ /* force kernel to reread partition table when new disc inserted */
+ ret = ioctl(p_env->gen.fd, BLKRRPART);
+ break;
+ default:
+ cdio_warn ("Unknown CD-ROM (%d)\n", status);
+ ret = 1;
+ }
+ } else {
+ cdio_warn ("CDROM_DRIVE_STATUS failed: %s\n", strerror(errno));
+ ret=1;
+ }
+ close(fd);
+ } else
+ ret = 2;
+ close(p_env->gen.fd);
+ p_env->gen.fd = -1;
+ return ret;
+}
+
+/*!
+ Get disc type associated with the cd object.
+*/
+static discmode_t
+get_discmode_linux (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+
+ int32_t i_discmode;
+
+ /* See if this is a DVD. */
+ cdio_dvd_struct_t dvd; /* DVD READ STRUCT for layer 0. */
+
+ dvd.physical.type = CDIO_DVD_STRUCT_PHYSICAL;
+ dvd.physical.layer_num = 0;
+ if (0 == ioctl (p_env->gen.fd, DVD_READ_STRUCT, &dvd)) {
+ switch(dvd.physical.layer[0].book_type) {
+ case CDIO_DVD_BOOK_DVD_ROM: return CDIO_DISC_MODE_DVD_ROM;
+ case CDIO_DVD_BOOK_DVD_RAM: return CDIO_DISC_MODE_DVD_RAM;
+ case CDIO_DVD_BOOK_DVD_R: return CDIO_DISC_MODE_DVD_R;
+ case CDIO_DVD_BOOK_DVD_RW: return CDIO_DISC_MODE_DVD_RW;
+ case CDIO_DVD_BOOK_DVD_PR: return CDIO_DISC_MODE_DVD_PR;
+ case CDIO_DVD_BOOK_DVD_PRW: return CDIO_DISC_MODE_DVD_PRW;
+ default: return CDIO_DISC_MODE_DVD_OTHER;
+ }
+ }
+
+ i_discmode = ioctl (p_env->gen.fd, CDROM_DISC_STATUS);
+
+ if (i_discmode < 0) return CDIO_DISC_MODE_ERROR;
+
+ /* FIXME Need to add getting DVD types. */
+ switch(i_discmode) {
+ case CDS_AUDIO:
+ return CDIO_DISC_MODE_CD_DA;
+ case CDS_DATA_1:
+ case CDS_DATA_2:
+ return CDIO_DISC_MODE_CD_DATA;
+ case CDS_MIXED:
+ return CDIO_DISC_MODE_CD_MIXED;
+ case CDS_XA_2_1:
+ case CDS_XA_2_2:
+ return CDIO_DISC_MODE_CD_XA;
+ case CDS_NO_INFO:
+ return CDIO_DISC_MODE_NO_INFO;
+ default:
+ return CDIO_DISC_MODE_ERROR;
+ }
+}
+
+/* Check a drive to see if it is a CD-ROM
+ Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive
+ and -1 if no device exists .
+*/
+static bool
+is_cdrom_linux(const char *drive, char *mnttype)
+{
+ bool is_cd=false;
+ int cdfd;
+ struct cdrom_tochdr tochdr;
+
+ /* If it doesn't exist, return -1 */
+ if ( !cdio_is_device_quiet_generic(drive) ) {
+ return(false);
+ }
+
+ /* If it does exist, verify that it's an available CD-ROM */
+ cdfd = open(drive, (O_RDONLY|O_NONBLOCK), 0);
+ if ( cdfd >= 0 ) {
+ if ( ioctl(cdfd, CDROMREADTOCHDR, &tochdr) != -1 ) {
+ is_cd = true;
+ }
+ close(cdfd);
+ }
+ /* Even if we can't read it, it might be mounted */
+ else if ( mnttype && (strcmp(mnttype, "iso9660") == 0) ) {
+ is_cd = true;
+ }
+ return(is_cd);
+}
+
+/* MMC driver to read audio sectors.
+ Can read only up to 25 blocks.
+*/
+static int
+_read_audio_sectors_linux (void *p_user_data, void *buf, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *p_env = p_user_data;
+ return scsi_mmc_read_sectors( p_env->gen.cdio, buf, lsn,
+ CDIO_MMC_READ_TYPE_CDDA, nblocks);
+}
+
+/* Packet driver to read mode2 sectors.
+ Can read only up to 25 blocks.
+*/
+static int
+_read_mode2_sectors_mmc (_img_private_t *p_env, void *p_buf, lba_t lba,
+ unsigned int nblocks, bool b_read_10)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+
+ CDIO_MMC_SET_READ_LBA(cdb.field, lba);
+
+ if (b_read_10) {
+ int retval;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_10);
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, nblocks);
+
+ if ((retval = scsi_mmc_set_blocksize (p_env->gen.cdio, M2RAW_SECTOR_SIZE)))
+ return retval;
+
+ if ((retval = run_scsi_cmd_linux (p_env, 0,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb,
+ SCSI_MMC_DATA_READ,
+ M2RAW_SECTOR_SIZE * nblocks,
+ p_buf)))
+ {
+ scsi_mmc_set_blocksize (p_env->gen.cdio, CDIO_CD_FRAMESIZE);
+ return retval;
+ }
+
+ if ((retval = scsi_mmc_set_blocksize (p_env->gen.cdio, CDIO_CD_FRAMESIZE)))
+ return retval;
+ } else
+
+ cdb.field[1] = 0; /* sector size mode2 */
+ cdb.field[9] = 0x58; /* 2336 mode2 */
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_CD);
+ CDIO_MMC_SET_READ_LENGTH24(cdb.field, nblocks);
+
+ return run_scsi_cmd_linux (p_env, 0,
+ scsi_mmc_get_cmd_len(cdb.field[0]), &cdb,
+ SCSI_MMC_DATA_READ,
+ M2RAW_SECTOR_SIZE * nblocks, p_buf);
+
+ return 0;
+}
+
+static int
+_read_mode2_sectors (_img_private_t *p_env, void *p_buf, lba_t lba,
+ unsigned int nblocks, bool b_read_10)
+{
+ unsigned int l = 0;
+ int retval = 0;
+
+ while (nblocks > 0)
+ {
+ const unsigned nblocks2 = (nblocks > 25) ? 25 : nblocks;
+ void *p_buf2 = ((char *)p_buf ) + (l * M2RAW_SECTOR_SIZE);
+
+ retval |= _read_mode2_sectors_mmc (p_env, p_buf2, lba + l,
+ nblocks2, b_read_10);
+
+ if (retval)
+ break;
+
+ nblocks -= nblocks2;
+ l += nblocks2;
+ }
+
+ return retval;
+}
+
+/*!
+ Reads a single mode1 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode1_sector_linux (void *p_user_data, void *p_data, lsn_t lsn,
+ bool b_form2)
+{
+
+#if FIXED
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ struct cdrom_msf *p_msf = (struct cdrom_msf *) &buf;
+ msf_t _msf;
+
+ _img_private_t *p_env = p_user_data;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ msf->cdmsf_min0 = cdio_from_bcd8(_msf.m);
+ msf->cdmsf_sec0 = cdio_from_bcd8(_msf.s);
+ msf->cdmsf_frame0 = cdio_from_bcd8(_msf.f);
+
+ retry:
+ switch (p_env->access_mode)
+ {
+ case _AM_NONE:
+ cdio_warn ("no way to read mode1");
+ return 1;
+ break;
+
+ case _AM_IOCTL:
+ if (ioctl (p_env->gen.fd, CDROMREADMODE1, &buf) == -1)
+ {
+ perror ("ioctl()");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+ break;
+
+ case _AM_READ_CD:
+ case _AM_READ_10:
+ if (_read_mode2_sectors (p_env->gen.fd, buf, lsn, 1,
+ (p_env->access_mode == _AM_READ_10)))
+ {
+ perror ("ioctl()");
+ if (p_env->access_mode == _AM_READ_CD)
+ {
+ cdio_info ("READ_CD failed; switching to READ_10 mode...");
+ p_env->access_mode = _AM_READ_10;
+ goto retry;
+ }
+ else
+ {
+ cdio_info ("READ_10 failed; switching to ioctl(CDROMREADMODE2) mode...");
+ p_env->access_mode = _AM_IOCTL;
+ goto retry;
+ }
+ return 1;
+ }
+ break;
+ }
+
+ memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE,
+ b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE);
+
+#else
+ return cdio_generic_read_form1_sector(p_user_data, p_data, lsn);
+#endif
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode1_sectors_linux (void *p_user_data, void *p_data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *p_env = p_user_data;
+ unsigned int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode1_sector_linux (p_env,
+ ((char *)p_data) + (blocksize*i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode2_sector_linux (void *p_user_data, void *p_data, lsn_t lsn,
+ bool b_form2)
+{
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ struct cdrom_msf *msf = (struct cdrom_msf *) &buf;
+ msf_t _msf;
+
+ _img_private_t *p_env = p_user_data;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ msf->cdmsf_min0 = cdio_from_bcd8(_msf.m);
+ msf->cdmsf_sec0 = cdio_from_bcd8(_msf.s);
+ msf->cdmsf_frame0 = cdio_from_bcd8(_msf.f);
+
+ retry:
+ switch (p_env->access_mode)
+ {
+ case _AM_NONE:
+ cdio_warn ("no way to read mode2");
+ return 1;
+ break;
+
+ case _AM_IOCTL:
+ if (ioctl (p_env->gen.fd, CDROMREADMODE2, &buf) == -1)
+ {
+ perror ("ioctl()");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+ break;
+
+ case _AM_READ_CD:
+ case _AM_READ_10:
+ if (_read_mode2_sectors (p_env, buf, lsn, 1,
+ (p_env->access_mode == _AM_READ_10)))
+ {
+ perror ("ioctl()");
+ if (p_env->access_mode == _AM_READ_CD)
+ {
+ cdio_info ("READ_CD failed; switching to READ_10 mode...");
+ p_env->access_mode = _AM_READ_10;
+ goto retry;
+ }
+ else
+ {
+ cdio_info ("READ_10 failed; switching to ioctl(CDROMREADMODE2) mode...");
+ p_env->access_mode = _AM_IOCTL;
+ goto retry;
+ }
+ return 1;
+ }
+ break;
+ }
+
+ if (b_form2)
+ memcpy (p_data, buf, M2RAW_SECTOR_SIZE);
+ else
+ memcpy (((char *)p_data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode2_sectors_linux (void *p_user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *p_env = p_user_data;
+ unsigned int i;
+ unsigned int i_blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ /* For each frame, pick out the data part we need */
+ for (i = 0; i < nblocks; i++) {
+ int retval;
+ if ( (retval = _read_mode2_sector_linux (p_env,
+ ((char *)data) + (i_blocksize*i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return false if successful or true if an error.
+*/
+static bool
+read_toc_linux (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+ int i;
+
+ /* read TOC header */
+ if ( ioctl(p_env->gen.fd, CDROMREADTOCHDR, &p_env->tochdr) == -1 ) {
+ cdio_warn("%s: %s\n",
+ "error in ioctl CDROMREADTOCHDR", strerror(errno));
+ return false;
+ }
+
+ p_env->gen.i_first_track = p_env->tochdr.cdth_trk0;
+ p_env->gen.i_tracks = p_env->tochdr.cdth_trk1;
+
+ /* read individual tracks */
+ for (i= p_env->gen.i_first_track; i<=p_env->gen.i_tracks; i++) {
+ p_env->tocent[i-p_env->gen.i_first_track].cdte_track = i;
+ p_env->tocent[i-p_env->gen.i_first_track].cdte_format = CDROM_MSF;
+ if ( ioctl(p_env->gen.fd, CDROMREADTOCENTRY,
+ &p_env->tocent[i-p_env->gen.i_first_track]) == -1 ) {
+ cdio_warn("%s %d: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for track",
+ i, strerror(errno));
+ return false;
+ }
+ /****
+ struct cdrom_msf0 *msf= &env->tocent[i-1].cdte_addr.msf;
+
+ fprintf (stdout, "--- track# %d (msf %2.2x:%2.2x:%2.2x)\n",
+ i, msf->minute, msf->second, msf->frame);
+ ****/
+
+ }
+
+ /* read the lead-out track */
+ p_env->tocent[p_env->gen.i_tracks].cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ p_env->tocent[p_env->gen.i_tracks].cdte_format = CDROM_MSF;
+
+ if (ioctl(p_env->gen.fd, CDROMREADTOCENTRY,
+ &p_env->tocent[p_env->gen.i_tracks]) == -1 ) {
+ cdio_warn("%s: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for lead-out",
+ strerror(errno));
+ return false;
+ }
+
+ /*
+ struct cdrom_msf0 *msf= &env->tocent[p_env->gen.i_tracks].cdte_addr.msf;
+
+ fprintf (stdout, "--- track# %d (msf %2.2x:%2.2x:%2.2x)\n",
+ i, msf->minute, msf->second, msf->frame);
+ */
+
+ p_env->gen.toc_init = true;
+ return true;
+}
+
+/*!
+ Run a SCSI MMC command.
+
+ cdio CD structure set by cdio_open().
+ i_timeout time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ p_buf Buffer for data, both sending and receiving
+ i_buf Size of buffer
+ e_direction direction the transfer is to go.
+ cdb CDB bytes. All values that are needed should be set on
+ input. We'll figure out what the right CDB length should be.
+
+ We return true if command completed successfully and false if not.
+ */
+static int
+run_scsi_cmd_linux( const void *p_user_data,
+ unsigned int i_timeout_ms,
+ unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf )
+{
+ const _img_private_t *p_env = p_user_data;
+ struct cdrom_generic_command cgc;
+ memset (&cgc, 0, sizeof (struct cdrom_generic_command));
+ memcpy(&cgc.cmd, p_cdb, i_cdb);
+ cgc.buflen = i_buf;
+ cgc.buffer = p_buf;
+ cgc.data_direction = (SCSI_MMC_DATA_READ == cgc.data_direction)
+ ? CGC_DATA_READ : CGC_DATA_WRITE;
+
+#ifdef HAVE_LINUX_CDROM_TIMEOUT
+ cgc.timeout = i_timeout_ms;
+#endif
+
+ return ioctl (p_env->gen.fd, CDROM_SEND_PACKET, &cgc);
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+stat_size_linux (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+
+ struct cdrom_tocentry tocent;
+ uint32_t size;
+
+ tocent.cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ tocent.cdte_format = CDROM_LBA;
+ if (ioctl (p_env->gen.fd, CDROMREADTOCENTRY, &tocent) == -1)
+ {
+ perror ("ioctl(CDROMREADTOCENTRY)");
+ exit (EXIT_FAILURE);
+ }
+
+ size = tocent.cdte_addr.lba;
+
+ return size;
+}
+
+/*!
+ Set the arg "key" with "value" in the source device.
+ Currently "source" and "access-mode" are valid keys.
+ "source" sets the source device in I/O operations
+ "access-mode" sets the the method of CD access
+
+ 0 is returned if no error was found, and nonzero if there as an error.
+*/
+static int
+set_arg_linux (void *p_user_data, const char key[], const char value[])
+{
+ _img_private_t *p_env = p_user_data;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (p_env->gen.source_name);
+
+ p_env->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ return str_to_access_mode_linux(value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/* checklist: /dev/cdrom, /dev/dvd /dev/hd?, /dev/scd? /dev/sr? */
+static char checklist1[][40] = {
+ {"cdrom"}, {"dvd"}, {""}
+};
+static char checklist2[][40] = {
+ {"?a hd?"}, {"?0 scd?"}, {"?0 sr?"}, {""}
+};
+
+#endif /* HAVE_LINUX_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_linux (void)
+{
+#ifndef HAVE_LINUX_CDROM
+ return NULL;
+#else
+ unsigned int i;
+ char drive[40];
+ char *ret_drive;
+ bool exists;
+ char **drives = NULL;
+ unsigned int num_drives=0;
+
+ /* Scan the system for CD-ROM drives.
+ */
+ for ( i=0; strlen(checklist1[i]) > 0; ++i ) {
+ sprintf(drive, "/dev/%s", checklist1[i]);
+ if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+ }
+
+ /* Now check the currently mounted CD drives */
+ if (NULL != (ret_drive = check_mounts_linux("/etc/mtab"))) {
+ cdio_add_device_list(&drives, ret_drive, &num_drives);
+ free(ret_drive);
+ }
+
+ /* Finally check possible mountable drives in /etc/fstab */
+ if (NULL != (ret_drive = check_mounts_linux("/etc/fstab"))) {
+ cdio_add_device_list(&drives, ret_drive, &num_drives);
+ free(ret_drive);
+ }
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+ for ( i=0; strlen(checklist2[i]) > 0; ++i ) {
+ unsigned int j;
+ char *insert;
+ exists = true;
+ for ( j=checklist2[i][1]; exists; ++j ) {
+ sprintf(drive, "/dev/%s", &checklist2[i][3]);
+ insert = strchr(drive, '?');
+ if ( insert != NULL ) {
+ *insert = j;
+ }
+ if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+ }
+ }
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /*HAVE_LINUX_CDROM*/
+}
+
+/*!
+ Return a string containing the default CD device.
+ */
+char *
+cdio_get_default_device_linux(void)
+{
+#ifndef HAVE_LINUX_CDROM
+ return NULL;
+
+#else
+ unsigned int i;
+ char drive[40];
+ bool exists;
+ char *ret_drive;
+
+ /* Scan the system for CD-ROM drives.
+ */
+ for ( i=0; strlen(checklist1[i]) > 0; ++i ) {
+ sprintf(drive, "/dev/%s", checklist1[i]);
+ if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) {
+ return strdup(drive);
+ }
+ }
+
+ /* Now check the currently mounted CD drives */
+ if (NULL != (ret_drive = check_mounts_linux("/etc/mtab")))
+ return ret_drive;
+
+ /* Finally check possible mountable drives in /etc/fstab */
+ if (NULL != (ret_drive = check_mounts_linux("/etc/fstab")))
+ return ret_drive;
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+ for ( i=0; strlen(checklist2[i]) > 0; ++i ) {
+ unsigned int j;
+ char *insert;
+ exists = true;
+ for ( j=checklist2[i][1]; exists; ++j ) {
+ sprintf(drive, "/dev/%s", &checklist2[i][3]);
+ insert = strchr(drive, '?');
+ if ( insert != NULL ) {
+ *insert = j;
+ }
+ if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) {
+ return(strdup(drive));
+ }
+ }
+ }
+ return NULL;
+#endif /*HAVE_LINUX_CDROM*/
+}
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_linux (const char *psz_source_name)
+{
+ return cdio_open_am_linux(psz_source_name, NULL);
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_am_linux (const char *psz_orig_source, const char *access_mode)
+{
+
+#ifdef HAVE_LINUX_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+ char *psz_source;
+
+ cdio_funcs _funcs = {
+ .eject_media = eject_media_linux,
+ .free = cdio_generic_free,
+ .get_arg = get_arg_linux,
+ .get_cdtext = get_cdtext_generic,
+ .get_default_device = cdio_get_default_device_linux,
+ .get_devices = cdio_get_devices_linux,
+ .get_discmode = get_discmode_linux,
+#if USE_LINUX_CAP
+ .get_drive_cap = get_drive_cap_linux,
+#else
+ .get_drive_cap = scsi_mmc_get_drive_cap_generic,
+#endif
+ .get_first_track_num= get_first_track_num_generic,
+ .get_hwinfo = NULL,
+ .get_mcn = get_mcn_linux,
+ .get_num_tracks = get_num_tracks_generic,
+ .get_track_format = get_track_format_linux,
+ .get_track_green = get_track_green_linux,
+ .get_track_lba = NULL, /* This could be implemented if need be. */
+ .get_track_msf = get_track_msf_linux,
+ .lseek = cdio_generic_lseek,
+ .read = cdio_generic_read,
+ .read_audio_sectors = _read_audio_sectors_linux,
+ .read_mode1_sector = _read_mode1_sector_linux,
+ .read_mode1_sectors = _read_mode1_sectors_linux,
+ .read_mode2_sector = _read_mode2_sector_linux,
+ .read_mode2_sectors = _read_mode2_sectors_linux,
+ .read_toc = read_toc_linux,
+ .run_scsi_mmc_cmd = run_scsi_cmd_linux,
+ .set_arg = set_arg_linux,
+ .stat_size = stat_size_linux
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+
+ _data->access_mode = str_to_access_mode_linux(access_mode);
+ _data->gen.init = false;
+ _data->gen.toc_init = false;
+ _data->gen.fd = -1;
+ _data->gen.b_cdtext_init = false;
+ _data->gen.b_cdtext_error = false;
+
+ if (NULL == psz_orig_source) {
+ psz_source=cdio_get_default_device_linux();
+ if (NULL == psz_source) {
+ free(_data);
+ return NULL;
+ }
+ set_arg_linux(_data, "source", psz_source);
+ free(psz_source);
+ } else {
+ if (cdio_is_device_generic(psz_orig_source))
+ set_arg_linux(_data, "source", psz_orig_source);
+ else {
+ /* The below would be okay if all device drivers worked this way. */
+#if 0
+ cdio_info ("source %s is not a device", psz_orig_source);
+#endif
+ free(_data);
+ return NULL;
+ }
+ }
+
+ ret = cdio_new ((void *)_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (cdio_generic_init(_data)) {
+ return ret;
+ } else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_LINUX_CDROM */
+
+}
+
+bool
+cdio_have_linux (void)
+{
+#ifdef HAVE_LINUX_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_LINUX_CDROM */
+}
diff --git a/contrib/libcdio/_cdio_osx.c b/contrib/libcdio/_cdio_osx.c
new file mode 100644
index 000000000..f754933c6
--- /dev/null
+++ b/contrib/libcdio/_cdio_osx.c
@@ -0,0 +1,1470 @@
+/*
+ $Id: _cdio_osx.c,v 1.4 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+ from vcdimager code:
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ and VideoLAN code Copyright (C) 1998-2001 VideoLAN
+ Authors: Johan Bilien <jobi@via.ecp.fr>
+ Gildas Bazin <gbazin@netcourrier.com>
+ Jon Lech Johansen <jon-vl@nanocrew.net>
+ Derk-Jan Hartman <hartman at videolan.org>
+ Justin F. Hallett <thesin@southofheaven.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains OSX-specific code and implements low-level
+ control of the CD drive.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_osx.c,v 1.4 2005/01/01 02:43:57 rockyb Exp $";
+
+#include <cdio/logging.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+
+#include <string.h>
+
+#ifdef HAVE_DARWIN_CDROM
+#undef VERSION
+
+#include <CoreFoundation/CoreFoundation.h>
+#include <IOKit/IOKitLib.h>
+#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
+
+#include <mach/mach.h>
+#include <Carbon/Carbon.h>
+#include <IOKit/scsi-commands/SCSITaskLib.h>
+#include <IOKit/IOCFPlugIn.h>
+#include <mach/mach_error.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+
+#include <paths.h>
+#include <CoreFoundation/CoreFoundation.h>
+#include <IOKit/IOKitLib.h>
+#include <IOKit/IOBSD.h>
+#include <IOKit/scsi-commands/IOSCSIMultimediaCommandsDevice.h>
+#include <IOKit/storage/IOCDTypes.h>
+#include <IOKit/storage/IODVDTypes.h>
+#include <IOKit/storage/IOMedia.h>
+#include <IOKit/storage/IOCDMedia.h>
+#include <IOKit/storage/IODVDMedia.h>
+#include <IOKit/storage/IOCDMediaBSDClient.h>
+#include <IOKit/storage/IODVDMediaBSDClient.h>
+#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
+
+#define kIOCDBlockStorageDeviceClassString "IOCDBlockStorageDevice"
+
+/* Note leadout is normally defined 0xAA, But on OSX 0xA0 is "lead in" while
+ 0xA2 is "lead out". Don't ask me why. */
+#define OSX_CDROM_LEADOUT_TRACK 0xA2
+
+#define TOTAL_TRACKS (p_env->i_last_track - p_env->gen.i_first_track + 1)
+
+#define CDROM_CDI_TRACK 0x1
+#define CDROM_XA_TRACK 0x2
+
+typedef enum {
+ _AM_NONE,
+ _AM_OSX,
+} access_mode_t;
+
+#define MAX_SERVICE_NAME 1000
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ access_mode_t access_mode;
+
+ /* Track information */
+ CDTOC *pTOC;
+ int i_descriptors;
+ track_t i_last_track; /* highest track number */
+ track_t i_last_session; /* highest session number */
+ track_t i_first_session; /* first session number */
+ lsn_t *pp_lba;
+ io_service_t MediaClass_service;
+ char psz_MediaClass_service[MAX_SERVICE_NAME];
+ SCSITaskDeviceInterface **pp_scsiTaskDeviceInterface;
+
+} _img_private_t;
+
+static bool read_toc_osx (void *p_user_data);
+
+/****
+ * GetRegistryEntryProperties - Gets the registry entry properties for
+ * an io_service_t.
+ *****/
+
+static CFMutableDictionaryRef
+GetRegistryEntryProperties ( io_service_t service )
+{
+ IOReturn err = kIOReturnSuccess;
+ CFMutableDictionaryRef dict = 0;
+
+ err = IORegistryEntryCreateCFProperties (service, &dict, kCFAllocatorDefault, 0);
+ if ( err != kIOReturnSuccess )
+ cdio_warn( "IORegistryEntryCreateCFProperties: 0x%08x", err );
+
+ return dict;
+}
+
+
+static bool
+init_osx(_img_private_t *p_env) {
+ mach_port_t port;
+ char *psz_devname;
+ kern_return_t ret;
+ io_iterator_t iterator;
+
+ p_env->gen.fd = open( p_env->gen.source_name, O_RDONLY | O_NONBLOCK );
+ if (-1 == p_env->gen.fd) {
+ cdio_warn("Failed to open %s: %s", p_env->gen.source_name,
+ strerror(errno));
+ return false;
+ }
+
+ /* get the device name */
+ psz_devname = strrchr( p_env->gen.source_name, '/');
+ if( NULL != psz_devname )
+ ++psz_devname;
+ else
+ psz_devname = p_env->gen.source_name;
+
+ /* unraw the device name */
+ if( *psz_devname == 'r' )
+ ++psz_devname;
+
+ /* get port for IOKit communication */
+ ret = IOMasterPort( MACH_PORT_NULL, &port );
+
+ if( ret != KERN_SUCCESS )
+ {
+ cdio_warn( "IOMasterPort: 0x%08x", ret );
+ return false;
+ }
+
+ ret = IOServiceGetMatchingServices( port,
+ IOBSDNameMatching(port, 0, psz_devname),
+ &iterator );
+
+ /* get service iterator for the device */
+ if( ret != KERN_SUCCESS )
+ {
+ cdio_warn( "IOServiceGetMatchingServices: 0x%08x", ret );
+ return false;
+ }
+
+ /* first service */
+ p_env->MediaClass_service = IOIteratorNext( iterator );
+ IOObjectRelease( iterator );
+
+ /* search for kIOCDMediaClass or kIOCDVDMediaClass */
+ while( p_env->MediaClass_service &&
+ (!IOObjectConformsTo(p_env->MediaClass_service, kIOCDMediaClass)) &&
+ (!IOObjectConformsTo(p_env->MediaClass_service, kIODVDMediaClass)) )
+ {
+
+ ret = IORegistryEntryGetParentIterator( p_env->MediaClass_service,
+ kIOServicePlane,
+ &iterator );
+ if( ret != KERN_SUCCESS )
+ {
+ cdio_warn( "IORegistryEntryGetParentIterator: 0x%08x", ret );
+ IOObjectRelease( p_env->MediaClass_service );
+ return false;
+ }
+
+ IOObjectRelease( p_env->MediaClass_service );
+ p_env->MediaClass_service = IOIteratorNext( iterator );
+ IOObjectRelease( iterator );
+ }
+
+ if ( 0 == p_env->MediaClass_service ) {
+ cdio_warn( "search for kIOCDMediaClass/kIODVDMediaClass came up empty" );
+ return false;
+ }
+
+ /* Save the name so we can compare against this in case we have to do
+ another scan. FIXME: this is hoaky and there's got to be a better
+ variable to test or way to do.
+ */
+ IORegistryEntryGetPath(p_env->MediaClass_service, kIOServicePlane,
+ p_env->psz_MediaClass_service);
+ return true;
+}
+
+/*!
+ Run a SCSI MMC command.
+
+ cdio CD structure set by cdio_open().
+ i_timeout time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ p_buf Buffer for data, both sending and receiving
+ i_buf Size of buffer
+ e_direction direction the transfer is to go.
+ cdb CDB bytes. All values that are needed should be set on
+ input. We'll figure out what the right CDB length should be.
+
+ We return true if command completed successfully and false if not.
+ */
+static int
+run_scsi_cmd_osx( const void *p_user_data,
+ unsigned int i_timeout_ms,
+ unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf )
+{
+
+#ifndef SCSI_MMC_FIXED
+ return 2;
+#else
+ const _img_private_t *p_env = p_user_data;
+ SCSITaskDeviceInterface **sc;
+ SCSITaskInterface **cmd = NULL;
+ IOVirtualRange iov;
+ SCSI_Sense_Data senseData;
+ SCSITaskStatus status;
+ UInt64 bytesTransferred;
+ IOReturn ioReturnValue;
+ int ret = 0;
+
+ if (NULL == p_user_data) return 2;
+
+ /* Make sure pp_scsiTaskDeviceInterface is initialized. FIXME: The code
+ should probably be reorganized better for this. */
+ if (!p_env->gen.toc_init) read_toc_osx (p_user_data) ;
+
+ sc = p_env->pp_scsiTaskDeviceInterface;
+
+ if (NULL == sc) return 3;
+
+ cmd = (*sc)->CreateSCSITask(sc);
+ if (cmd == NULL) {
+ cdio_warn("Failed to create SCSI task");
+ return -1;
+ }
+
+ iov.address = (IOVirtualAddress) p_buf;
+ iov.length = i_buf;
+
+ ioReturnValue = (*cmd)->SetCommandDescriptorBlock(cmd, (UInt8 *) p_cdb,
+ i_cdb);
+ if (ioReturnValue != kIOReturnSuccess) {
+ cdio_warn("SetCommandDescriptorBlock failed with status %x",
+ ioReturnValue);
+ return -1;
+ }
+
+ ioReturnValue = (*cmd)->SetScatterGatherEntries(cmd, &iov, 1, i_buf,
+ (SCSI_MMC_DATA_READ == e_direction ) ?
+ kSCSIDataTransfer_FromTargetToInitiator :
+ kSCSIDataTransfer_FromInitiatorToTarget);
+ if (ioReturnValue != kIOReturnSuccess) {
+ cdio_warn("SetScatterGatherEntries failed with status %x", ioReturnValue);
+ return -1;
+ }
+
+ ioReturnValue = (*cmd)->SetTimeoutDuration(cmd, i_timeout_ms );
+ if (ioReturnValue != kIOReturnSuccess) {
+ cdio_warn("SetTimeoutDuration failed with status %x", ioReturnValue);
+ return -1;
+ }
+
+ memset(&senseData, 0, sizeof(senseData));
+
+ ioReturnValue = (*cmd)->ExecuteTaskSync(cmd,&senseData, &status, &
+ bytesTransferred);
+
+ if (ioReturnValue != kIOReturnSuccess) {
+ cdio_warn("Command execution failed with status %x", ioReturnValue);
+ return -1;
+ }
+
+ if (cmd != NULL) {
+ (*cmd)->Release(cmd);
+ }
+
+ return (ret);
+#endif
+}
+
+/***************************************************************************
+ * GetDeviceIterator - Gets an io_iterator_t for our class type
+ ***************************************************************************/
+
+static io_iterator_t
+GetDeviceIterator ( const char * deviceClass )
+{
+
+ IOReturn err = kIOReturnSuccess;
+ io_iterator_t iterator = MACH_PORT_NULL;
+
+ err = IOServiceGetMatchingServices ( kIOMasterPortDefault,
+ IOServiceMatching ( deviceClass ),
+ &iterator );
+ check ( err == kIOReturnSuccess );
+
+ return iterator;
+
+}
+
+/***************************************************************************
+ * GetFeaturesFlagsForDrive -Gets the bitfield which represents the
+ * features flags.
+ ***************************************************************************/
+
+static bool
+GetFeaturesFlagsForDrive ( CFDictionaryRef dict,
+ uint32_t *i_cdFlags,
+ uint32_t *i_dvdFlags )
+{
+ CFDictionaryRef propertiesDict = 0;
+ CFNumberRef flagsNumberRef = 0;
+
+ *i_cdFlags = 0;
+ *i_dvdFlags= 0;
+
+ propertiesDict = ( CFDictionaryRef )
+ CFDictionaryGetValue ( dict,
+ CFSTR ( kIOPropertyDeviceCharacteristicsKey ) );
+
+ if ( propertiesDict == 0 ) return false;
+
+ /* Get the CD features */
+ flagsNumberRef = ( CFNumberRef )
+ CFDictionaryGetValue ( propertiesDict,
+ CFSTR ( kIOPropertySupportedCDFeatures ) );
+ if ( flagsNumberRef != 0 ) {
+ CFNumberGetValue ( flagsNumberRef, kCFNumberLongType, i_cdFlags );
+ }
+
+ /* Get the DVD features */
+ flagsNumberRef = ( CFNumberRef )
+ CFDictionaryGetValue ( propertiesDict,
+ CFSTR ( kIOPropertySupportedDVDFeatures ) );
+ if ( flagsNumberRef != 0 ) {
+ CFNumberGetValue ( flagsNumberRef, kCFNumberLongType, i_dvdFlags );
+ }
+
+ return true;
+}
+
+/*!
+ Get disc type associated with the cd object.
+*/
+static discmode_t
+get_discmode_osx (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+ char str[10];
+ int32_t i_discmode = CDIO_DISC_MODE_ERROR;
+ CFDictionaryRef propertiesDict = 0;
+ CFStringRef data;
+
+ propertiesDict = GetRegistryEntryProperties ( p_env->MediaClass_service );
+
+ if ( propertiesDict == 0 ) return i_discmode;
+
+ data = ( CFStringRef )
+ CFDictionaryGetValue ( propertiesDict, CFSTR ( kIODVDMediaTypeKey ) );
+
+ if( CFStringGetCString( data, str, sizeof(str),
+ kCFStringEncodingASCII ) ) {
+ if (0 == strncmp(str, "DVD+R", strlen(str)) )
+ i_discmode = CDIO_DISC_MODE_DVD_PR;
+ else if (0 == strncmp(str, "DVD+RW", strlen(str)) )
+ i_discmode = CDIO_DISC_MODE_DVD_PRW;
+ else if (0 == strncmp(str, "DVD-R", strlen(str)) )
+ i_discmode = CDIO_DISC_MODE_DVD_R;
+ else if (0 == strncmp(str, "DVD-RW", strlen(str)) )
+ i_discmode = CDIO_DISC_MODE_DVD_RW;
+ else if (0 == strncmp(str, "DVD-ROM", strlen(str)) )
+ i_discmode = CDIO_DISC_MODE_DVD_ROM;
+ else if (0 == strncmp(str, "DVD-RAM", strlen(str)) )
+ i_discmode = CDIO_DISC_MODE_DVD_RAM;
+ else if (0 == strncmp(str, "CD-ROM", strlen(str)) )
+ i_discmode = CDIO_DISC_MODE_CD_DATA;
+ else if (0 == strncmp(str, "CDR", strlen(str)) )
+ i_discmode = CDIO_DISC_MODE_CD_DATA;
+ else if (0 == strncmp(str, "CDRW", strlen(str)) )
+ i_discmode = CDIO_DISC_MODE_CD_DATA;
+ //?? Handled by below? CFRelease( data );
+ }
+ CFRelease( propertiesDict );
+ if (CDIO_DISC_MODE_CD_DATA == i_discmode) {
+ /* Need to do more classification */
+ return get_discmode_cd_generic(p_user_data);
+ }
+ return i_discmode;
+
+}
+
+static io_service_t
+get_drive_service_osx(const _img_private_t *p_env)
+{
+ io_service_t service;
+ io_iterator_t service_iterator;
+
+ service_iterator = GetDeviceIterator ( kIOCDBlockStorageDeviceClassString );
+
+ if( service_iterator == MACH_PORT_NULL ) return 0;
+
+ service = IOIteratorNext( service_iterator );
+ if( service == 0 ) return 0;
+
+ do
+ {
+ char psz_service[MAX_SERVICE_NAME];
+ IORegistryEntryGetPath(service, kIOServicePlane, psz_service);
+ psz_service[MAX_SERVICE_NAME-1] = '\0';
+
+ /* FIXME: This is all hoaky. Here we need info from a parent class,
+ psz_service of what we opened above. We are relying on the
+ fact that the name will be a substring of the name we
+ openned with.
+ */
+ if (0 == strncmp(psz_service, p_env->psz_MediaClass_service,
+ strlen(psz_service))) {
+ /* Found our device */
+ IOObjectRelease( service_iterator );
+ return service;
+ }
+
+ IOObjectRelease( service );
+
+ } while( ( service = IOIteratorNext( service_iterator ) ) != 0 );
+
+ IOObjectRelease( service_iterator );
+ return service;
+}
+
+static void
+get_drive_cap_osx(const void *p_user_data,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap)
+{
+ const _img_private_t *p_env = p_user_data;
+ uint32_t i_cdFlags;
+ uint32_t i_dvdFlags;
+
+ io_service_t service = get_drive_service_osx(p_env);
+
+ if( service == 0 ) goto err_exit;
+
+ /* Found our device */
+ {
+ CFDictionaryRef properties = GetRegistryEntryProperties ( service );
+
+ if (! GetFeaturesFlagsForDrive ( properties, &i_cdFlags,
+ &i_dvdFlags ) ) {
+ IOObjectRelease( service );
+ goto err_exit;
+ }
+
+ /* Reader */
+
+ if ( 0 != (i_cdFlags & kCDFeaturesAnalogAudioMask) )
+ *p_read_cap |= CDIO_DRIVE_CAP_READ_AUDIO;
+
+ if ( 0 != (i_cdFlags & kCDFeaturesWriteOnceMask) )
+ *p_write_cap |= CDIO_DRIVE_CAP_WRITE_CD_R;
+
+ if ( 0 != (i_cdFlags & kCDFeaturesCDDAStreamAccurateMask) )
+ *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_DA;
+
+ if ( 0 != (i_dvdFlags & kDVDFeaturesReadStructuresMask) )
+ *p_read_cap |= CDIO_DRIVE_CAP_READ_DVD_ROM;
+
+ if ( 0 != (i_cdFlags & kCDFeaturesReWriteableMask) )
+ *p_write_cap |= CDIO_DRIVE_CAP_WRITE_CD_RW;
+
+ if ( 0 != (i_dvdFlags & kDVDFeaturesWriteOnceMask) )
+ *p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_R;
+
+ if ( 0 != (i_dvdFlags & kDVDFeaturesRandomWriteableMask) )
+ *p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_RAM;
+
+ if ( 0 != (i_dvdFlags & kDVDFeaturesReWriteableMask) )
+ *p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_RW;
+
+ /***
+ if ( 0 != (i_dvdFlags & kDVDFeaturesPlusRMask) )
+ *p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_PR;
+
+ if ( 0 != (i_dvdFlags & kDVDFeaturesPlusRWMask )
+ *p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_PRW;
+ ***/
+
+ /* FIXME: fill out. For now assume CD-ROM is relatively modern. */
+ *p_misc_cap = (
+ CDIO_DRIVE_CAP_MISC_CLOSE_TRAY
+ | CDIO_DRIVE_CAP_MISC_EJECT
+ | CDIO_DRIVE_CAP_MISC_LOCK
+ | CDIO_DRIVE_CAP_MISC_SELECT_SPEED
+ | CDIO_DRIVE_CAP_MISC_MULTI_SESSION
+ | CDIO_DRIVE_CAP_MISC_MEDIA_CHANGED
+ | CDIO_DRIVE_CAP_MISC_RESET
+ | CDIO_DRIVE_CAP_MCN
+ | CDIO_DRIVE_CAP_ISRC
+ );
+
+ IOObjectRelease( service );
+ }
+
+ return;
+
+ err_exit:
+ *p_misc_cap = *p_write_cap = *p_read_cap = CDIO_DRIVE_CAP_UNKNOWN;
+ return;
+}
+
+#if 1
+/****************************************************************************
+ * GetDriveDescription - Gets drive description.
+ ****************************************************************************/
+
+static bool
+get_hwinfo_osx ( const CdIo *p_cdio, /*out*/ cdio_hwinfo_t *hw_info)
+{
+ _img_private_t *p_env = (_img_private_t *) p_cdio->env;
+ io_service_t service = get_drive_service_osx(p_env);
+
+ if ( service == 0 ) return false;
+
+ /* Found our device */
+ {
+ CFStringRef vendor = NULL;
+ CFStringRef product = NULL;
+ CFStringRef revision = NULL;
+
+ CFDictionaryRef properties = GetRegistryEntryProperties ( service );
+ CFDictionaryRef deviceDict = ( CFDictionaryRef )
+ CFDictionaryGetValue ( properties,
+ CFSTR ( kIOPropertyDeviceCharacteristicsKey ) );
+
+ if ( deviceDict == 0 ) return false;
+
+ vendor = ( CFStringRef )
+ CFDictionaryGetValue ( deviceDict, CFSTR ( kIOPropertyVendorNameKey ) );
+
+ if ( CFStringGetCString( vendor,
+ (char *) &(hw_info->psz_vendor),
+ sizeof(hw_info->psz_vendor),
+ kCFStringEncodingASCII ) )
+ CFRelease( vendor );
+
+ product = ( CFStringRef )
+ CFDictionaryGetValue ( deviceDict, CFSTR ( kIOPropertyProductNameKey ) );
+
+ if ( CFStringGetCString( product,
+ (char *) &(hw_info->psz_model),
+ sizeof(hw_info->psz_model),
+ kCFStringEncodingASCII ) )
+ CFRelease( product );
+
+ revision = ( CFStringRef )
+ CFDictionaryGetValue ( deviceDict,
+ CFSTR ( kIOPropertyProductRevisionLevelKey ) );
+
+ if ( CFStringGetCString( product,
+ (char *) &(hw_info->psz_revision),
+ sizeof(hw_info->psz_revision),
+ kCFStringEncodingASCII ) )
+ CFRelease( revision );
+ }
+ return true;
+
+}
+#endif
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+static const cdtext_t *
+get_cdtext_osx (void *p_user_data, track_t i_track)
+{
+ return NULL;
+}
+
+static void
+_free_osx (void *p_user_data) {
+ _img_private_t *p_env = p_user_data;
+ if (NULL == p_env) return;
+ cdio_generic_free(p_env);
+ if (NULL != p_env->pp_lba) free((void *) p_env->pp_lba);
+ if (NULL != p_env->pTOC) free((void *) p_env->pTOC);
+ IOObjectRelease( p_env->MediaClass_service );
+
+ if (NULL != p_env->pp_scsiTaskDeviceInterface)
+ ( *(p_env->pp_scsiTaskDeviceInterface) )->
+ Release ( (p_env->pp_scsiTaskDeviceInterface) );
+
+}
+
+/*!
+ Reads nblocks of mode2 form2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_get_read_mode1_sectors_osx (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *env = user_data;
+ dk_cd_read_t cd_read;
+
+ memset( &cd_read, 0, sizeof(cd_read) );
+
+ cd_read.sectorArea = kCDSectorAreaUser;
+ cd_read.buffer = data;
+ cd_read.sectorType = kCDSectorTypeMode1;
+
+ if (b_form2) {
+ cd_read.offset = lsn * kCDSectorSizeMode2;
+ cd_read.bufferLength = kCDSectorSizeMode2 * nblocks;
+ } else {
+ cd_read.offset = lsn * kCDSectorSizeMode1;
+ cd_read.bufferLength = kCDSectorSizeMode1 * nblocks;
+ }
+
+ if( ioctl( env->gen.fd, DKIOCCDREAD, &cd_read ) == -1 )
+ {
+ cdio_info( "could not read block %d, %s", lsn, strerror(errno) );
+ return -1;
+ }
+ return 0;
+}
+
+
+/*!
+ Reads nblocks of mode2 form2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_get_read_mode2_sectors_osx (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *env = user_data;
+ dk_cd_read_t cd_read;
+
+ memset( &cd_read, 0, sizeof(cd_read) );
+
+ cd_read.sectorArea = kCDSectorAreaUser;
+ cd_read.buffer = data;
+
+ if (b_form2) {
+ cd_read.offset = lsn * kCDSectorSizeMode2Form2;
+ cd_read.sectorType = kCDSectorTypeMode2Form2;
+ cd_read.bufferLength = kCDSectorSizeMode2Form2 * nblocks;
+ } else {
+ cd_read.offset = lsn * kCDSectorSizeMode2Form1;
+ cd_read.sectorType = kCDSectorTypeMode2Form1;
+ cd_read.bufferLength = kCDSectorSizeMode2Form1 * nblocks;
+ }
+
+ if( ioctl( env->gen.fd, DKIOCCDREAD, &cd_read ) == -1 )
+ {
+ cdio_info( "could not read block %d, %s", lsn, strerror(errno) );
+ return -1;
+ }
+ return 0;
+}
+
+
+/*!
+ Reads a single audio sector from CD device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+_get_read_audio_sectors_osx (void *user_data, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *env = user_data;
+ dk_cd_read_t cd_read;
+
+ memset( &cd_read, 0, sizeof(cd_read) );
+
+ cd_read.offset = lsn * kCDSectorSizeCDDA;
+ cd_read.sectorArea = kCDSectorAreaUser;
+ cd_read.sectorType = kCDSectorTypeCDDA;
+
+ cd_read.buffer = data;
+ cd_read.bufferLength = kCDSectorSizeCDDA * nblocks;
+
+ if( ioctl( env->gen.fd, DKIOCCDREAD, &cd_read ) == -1 )
+ {
+ cdio_info( "could not read block %d", lsn );
+ return -1;
+ }
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_get_read_mode1_sector_osx (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ return _get_read_mode1_sectors_osx(user_data, data, lsn, b_form2, 1);
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_get_read_mode2_sector_osx (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ return _get_read_mode2_sectors_osx(user_data, data, lsn, b_form2, 1);
+}
+
+/*!
+ Set the key "arg" to "value" in source device.
+*/
+static int
+_set_arg_osx (void *user_data, const char key[], const char value[])
+{
+ _img_private_t *env = user_data;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (env->gen.source_name);
+
+ env->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ if (!strcmp(value, "OSX"))
+ env->access_mode = _AM_OSX;
+ else
+ cdio_warn ("unknown access type: %s. ignored.", value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+#if 0
+static void TestDevice(_img_private_t *p_env, io_service_t service)
+{
+ SInt32 score;
+ HRESULT herr;
+ kern_return_t err;
+ IOCFPlugInInterface **plugInInterface = NULL;
+ MMCDeviceInterface **mmcInterface = NULL;
+
+ /* Create the IOCFPlugIn interface so we can query it. */
+
+ err = IOCreatePlugInInterfaceForService ( service,
+ kIOMMCDeviceUserClientTypeID,
+ kIOCFPlugInInterfaceID,
+ &plugInInterface,
+ &score );
+ if ( err != noErr ) {
+ printf("IOCreatePlugInInterfaceForService returned %d\n", err);
+ return;
+ }
+
+ /* Query the interface for the MMCDeviceInterface. */
+
+ herr = ( *plugInInterface )->QueryInterface ( plugInInterface,
+ CFUUIDGetUUIDBytes ( kIOMMCDeviceInterfaceID ),
+ ( LPVOID ) &mmcInterface );
+
+ if ( herr != S_OK ) {
+ printf("QueryInterface returned %ld\n", herr);
+ return;
+ }
+
+ p_env->pp_scsiTaskDeviceInterface =
+ ( *mmcInterface )->GetSCSITaskDeviceInterface ( mmcInterface );
+
+ if ( NULL == p_env->pp_scsiTaskDeviceInterface ) {
+ printf("GetSCSITaskDeviceInterface returned NULL\n");
+ return;
+ }
+
+ ( *mmcInterface )->Release ( mmcInterface );
+ IODestroyPlugInInterface ( plugInInterface );
+}
+#endif
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return false if successful or true if an error.
+*/
+static bool
+read_toc_osx (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+ CFDictionaryRef propertiesDict = 0;
+ CFDataRef data;
+
+ /* create a CF dictionary containing the TOC */
+ propertiesDict = GetRegistryEntryProperties( p_env->MediaClass_service );
+
+ if ( 0 == propertiesDict ) {
+ return false;
+ }
+
+ /* get the TOC from the dictionary */
+ data = (CFDataRef) CFDictionaryGetValue( propertiesDict,
+ CFSTR(kIOCDMediaTOCKey) );
+ if ( data != NULL ) {
+ CFRange range;
+ CFIndex buf_len;
+
+ buf_len = CFDataGetLength( data ) + 1;
+ range = CFRangeMake( 0, buf_len );
+
+ if( ( p_env->pTOC = (CDTOC *)malloc( buf_len ) ) != NULL ) {
+ CFDataGetBytes( data, range, (u_char *) p_env->pTOC );
+ } else {
+ cdio_warn( "Trouble allocating CDROM TOC" );
+ return false;
+ }
+ } else {
+ cdio_warn( "Trouble reading TOC" );
+ return false;
+ }
+
+ /* TestDevice(p_env, service); */
+ CFRelease( propertiesDict );
+
+ p_env->i_descriptors = CDTOCGetDescriptorCount ( p_env->pTOC );
+
+ /* Read in starting sectors. There may be non-tracks mixed in with
+ the real tracks. So find the first and last track number by
+ scanning. Also find the lead-out track position.
+ */
+ {
+ int i, i_leadout = -1;
+
+ CDTOCDescriptor *pTrackDescriptors;
+
+ p_env->pp_lba = malloc( p_env->i_descriptors * sizeof(int) );
+ if( p_env->pp_lba == NULL )
+ {
+ cdio_warn("Out of memory in allocating track starting LSNs" );
+ free( p_env->pTOC );
+ return false;
+ }
+
+ pTrackDescriptors = p_env->pTOC->descriptors;
+
+ p_env->gen.i_first_track = CDIO_CD_MAX_TRACKS+1;
+ p_env->i_last_track = CDIO_CD_MIN_TRACK_NO;
+ p_env->i_first_session = CDIO_CD_MAX_TRACKS+1;
+ p_env->i_last_session = CDIO_CD_MIN_TRACK_NO;
+
+ for( i = 0; i <= p_env->i_descriptors; i++ )
+ {
+ track_t i_track = pTrackDescriptors[i].point;
+ session_t i_session = pTrackDescriptors[i].session;
+
+ cdio_debug( "point: %d, tno: %d, session: %d, adr: %d, control:%d, "
+ "address: %d:%d:%d, p: %d:%d:%d",
+ i_track,
+ pTrackDescriptors[i].tno, i_session,
+ pTrackDescriptors[i].adr, pTrackDescriptors[i].control,
+ pTrackDescriptors[i].address.minute,
+ pTrackDescriptors[i].address.second,
+ pTrackDescriptors[i].address.frame,
+ pTrackDescriptors[i].p.minute,
+ pTrackDescriptors[i].p.second,
+ pTrackDescriptors[i].p.frame );
+
+ /* track information has adr = 1 */
+ if ( 0x01 != pTrackDescriptors[i].adr )
+ continue;
+
+ if( i_track == OSX_CDROM_LEADOUT_TRACK )
+ i_leadout = i;
+
+ if( i_track > CDIO_CD_MAX_TRACKS || i_track < CDIO_CD_MIN_TRACK_NO )
+ continue;
+
+ if (p_env->gen.i_first_track > i_track)
+ p_env->gen.i_first_track = i_track;
+
+ if (p_env->i_last_track < i_track)
+ p_env->i_last_track = i_track;
+
+ if (p_env->i_first_session > i_session)
+ p_env->i_first_session = i_session;
+
+ if (p_env->i_last_session < i_session)
+ p_env->i_last_session = i_session;
+ }
+
+ /* Now that we know what the first track number is, we can make sure
+ index positions are ordered starting at 0.
+ */
+ for( i = 0; i <= p_env->i_descriptors; i++ )
+ {
+ track_t i_track = pTrackDescriptors[i].point;
+
+ if( i_track > CDIO_CD_MAX_TRACKS || i_track < CDIO_CD_MIN_TRACK_NO )
+ continue;
+
+ /* Note what OSX calls a LBA we call an LSN. So below re we
+ really have have MSF -> LSN -> LBA.
+ */
+ p_env->pp_lba[i_track - p_env->gen.i_first_track] =
+ cdio_lsn_to_lba(CDConvertMSFToLBA( pTrackDescriptors[i].p ));
+ }
+
+ if( i_leadout == -1 )
+ {
+ cdio_warn( "CD leadout not found" );
+ free( p_env->pp_lba );
+ free( (void *) p_env->pTOC );
+ return false;
+ }
+
+ /* Set leadout sector.
+ Note what OSX calls a LBA we call an LSN. So below re we
+ really have have MSF -> LSN -> LBA.
+ */
+ p_env->pp_lba[TOTAL_TRACKS] =
+ cdio_lsn_to_lba(CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p ));
+ p_env->gen.i_tracks = TOTAL_TRACKS;
+ }
+
+ p_env->gen.toc_init = true;
+
+ return( true );
+
+}
+
+/*!
+ Return the starting LSN track number
+ i_track in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using i_track LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static lsn_t
+get_track_lba_osx(void *p_user_data, track_t i_track)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if (!p_env->gen.toc_init) read_toc_osx (p_env) ;
+ if (!p_env->gen.toc_init) return CDIO_INVALID_LSN;
+
+ if (i_track == CDIO_CDROM_LEADOUT_TRACK) i_track = p_env->i_last_track+1;
+
+ if (i_track > p_env->i_last_track + 1 || i_track < p_env->gen.i_first_track) {
+ return CDIO_INVALID_LSN;
+ } else {
+ return p_env->pp_lba[i_track - p_env->gen.i_first_track];
+ }
+}
+
+/*!
+ Eject media . Return 1 if successful, 0 otherwise.
+
+ The only way to cleanly unmount the disc under MacOS X is to use the
+ 'disktool' command line utility. It uses the non-public Disk
+ Arbitration API, which can not be used by Cocoa or Carbon
+ applications.
+
+ */
+
+static int
+_eject_media_osx (void *user_data) {
+
+ _img_private_t *p_env = user_data;
+
+ FILE *p_eject;
+ char *psz_disk;
+ char sz_cmd[32];
+
+ if( ( psz_disk = (char *)strstr( p_env->gen.source_name, "disk" ) ) != NULL &&
+ strlen( psz_disk ) > 4 )
+ {
+#define EJECT_CMD "/usr/sbin/hdiutil eject %s"
+ snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
+#undef EJECT_CMD
+
+ if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
+ {
+ char psz_result[0x200];
+ int i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
+
+ if( i_ret == 0 && ferror( p_eject ) != 0 )
+ {
+ pclose( p_eject );
+ return 0;
+ }
+
+ pclose( p_eject );
+
+ psz_result[ i_ret ] = 0;
+
+ if( strstr( psz_result, "Disk Ejected" ) != NULL )
+ {
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_stat_size_osx (void *user_data)
+{
+ return get_track_lba_osx(user_data, CDIO_CDROM_LEADOUT_TRACK);
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_get_arg_osx (void *user_data, const char key[])
+{
+ _img_private_t *p_env = user_data;
+
+ if (!strcmp (key, "source")) {
+ return p_env->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (p_env->access_mode) {
+ case _AM_OSX:
+ return "OS X";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+/*!
+ Return the media catalog number MCN.
+ */
+static char *
+get_mcn_osx (const void *user_data) {
+ const _img_private_t *p_env = user_data;
+ dk_cd_read_mcn_t cd_read;
+
+ memset( &cd_read, 0, sizeof(cd_read) );
+
+ if( ioctl( p_env->gen.fd, DKIOCCDREADMCN, &cd_read ) < 0 )
+ {
+ cdio_debug( "could not read MCN, %s", strerror(errno) );
+ return NULL;
+ }
+ return strdup((char*)cd_read.mcn);
+}
+
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+get_track_format_osx(void *user_data, track_t i_track)
+{
+ _img_private_t *p_env = user_data;
+ dk_cd_read_track_info_t cd_read;
+ CDTrackInfo a_track;
+
+ if (!p_env->gen.toc_init) read_toc_osx (p_env) ;
+
+ if (i_track > p_env->i_last_track || i_track < p_env->gen.i_first_track)
+ return TRACK_FORMAT_ERROR;
+
+ memset( &cd_read, 0, sizeof(cd_read) );
+
+ cd_read.address = i_track;
+ cd_read.addressType = kCDTrackInfoAddressTypeTrackNumber;
+
+ cd_read.buffer = &a_track;
+ cd_read.bufferLength = sizeof(CDTrackInfo);
+
+ if( ioctl( p_env->gen.fd, DKIOCCDREADTRACKINFO, &cd_read ) == -1 )
+ {
+ cdio_warn( "could not read trackinfo for track %d", i_track );
+ return TRACK_FORMAT_ERROR;
+ }
+
+ cdio_debug( "%d: trackinfo trackMode: %x dataMode: %x", i_track, a_track.trackMode, a_track.dataMode );
+
+ if (a_track.trackMode == CDIO_CDROM_DATA_TRACK) {
+ if (a_track.dataMode == CDROM_CDI_TRACK) {
+ return TRACK_FORMAT_CDI;
+ } else if (a_track.dataMode == CDROM_XA_TRACK) {
+ return TRACK_FORMAT_XA;
+ } else {
+ return TRACK_FORMAT_DATA;
+ }
+ } else {
+ return TRACK_FORMAT_AUDIO;
+ }
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+get_track_green_osx(void *user_data, track_t i_track)
+{
+ _img_private_t *p_env = user_data;
+ CDTrackInfo a_track;
+
+ if (!p_env->gen.toc_init) read_toc_osx (p_env) ;
+
+ if ( i_track > p_env->i_last_track || i_track < p_env->gen.i_first_track )
+ return false;
+
+ else {
+
+ dk_cd_read_track_info_t cd_read;
+
+ memset( &cd_read, 0, sizeof(cd_read) );
+
+ cd_read.address = i_track;
+ cd_read.addressType = kCDTrackInfoAddressTypeTrackNumber;
+
+ cd_read.buffer = &a_track;
+ cd_read.bufferLength = sizeof(CDTrackInfo);
+
+ if( ioctl( p_env->gen.fd, DKIOCCDREADTRACKINFO, &cd_read ) == -1 ) {
+ cdio_warn( "could not read trackinfo for track %d", i_track );
+ return false;
+ }
+ return ((a_track.trackMode & CDIO_CDROM_DATA_TRACK) != 0);
+ }
+}
+
+#endif /* HAVE_DARWIN_CDROM */
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char **
+cdio_get_devices_osx(void)
+{
+#ifndef HAVE_DARWIN_CDROM
+ return NULL;
+#else
+ io_object_t next_media;
+ mach_port_t master_port;
+ kern_return_t kern_result;
+ io_iterator_t media_iterator;
+ CFMutableDictionaryRef classes_to_match;
+ char **drives = NULL;
+ unsigned int num_drives=0;
+
+ kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
+ if( kern_result != KERN_SUCCESS )
+ {
+ return( NULL );
+ }
+
+ classes_to_match = IOServiceMatching( kIOCDMediaClass );
+ if( classes_to_match == NULL )
+ {
+ return( NULL );
+ }
+
+ CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectableKey),
+ kCFBooleanTrue );
+
+ kern_result = IOServiceGetMatchingServices( master_port,
+ classes_to_match,
+ &media_iterator );
+ if( kern_result != KERN_SUCCESS )
+ {
+ return( NULL );
+ }
+
+ next_media = IOIteratorNext( media_iterator );
+ if( next_media != 0 )
+ {
+ char psz_buf[0x32];
+ size_t dev_path_length;
+ CFTypeRef str_bsd_path;
+
+ do
+ {
+ str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
+ CFSTR( kIOBSDNameKey ),
+ kCFAllocatorDefault,
+ 0 );
+ if( str_bsd_path == NULL )
+ {
+ IOObjectRelease( next_media );
+ continue;
+ }
+
+ snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
+ dev_path_length = strlen( psz_buf );
+
+ if( CFStringGetCString( str_bsd_path,
+ (char*)&psz_buf + dev_path_length,
+ sizeof(psz_buf) - dev_path_length,
+ kCFStringEncodingASCII ) )
+ {
+ CFRelease( str_bsd_path );
+ IOObjectRelease( next_media );
+ IOObjectRelease( media_iterator );
+ cdio_add_device_list(&drives, strdup(psz_buf), &num_drives);
+ }
+
+ CFRelease( str_bsd_path );
+ IOObjectRelease( next_media );
+
+ } while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 );
+ }
+ IOObjectRelease( media_iterator );
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /* HAVE_DARWIN_CDROM */
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char *
+cdio_get_default_device_osx(void)
+{
+#ifndef HAVE_DARWIN_CDROM
+ return NULL;
+#else
+ io_object_t next_media;
+ mach_port_t master_port;
+ kern_return_t kern_result;
+ io_iterator_t media_iterator;
+ CFMutableDictionaryRef classes_to_match;
+
+ kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
+ if( kern_result != KERN_SUCCESS )
+ {
+ return( NULL );
+ }
+
+ classes_to_match = IOServiceMatching( kIOCDMediaClass );
+ if( classes_to_match == NULL )
+ {
+ return( NULL );
+ }
+
+ CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectableKey),
+ kCFBooleanTrue );
+
+ kern_result = IOServiceGetMatchingServices( master_port,
+ classes_to_match,
+ &media_iterator );
+ if( kern_result != KERN_SUCCESS )
+ {
+ return( NULL );
+ }
+
+ next_media = IOIteratorNext( media_iterator );
+ if( next_media != 0 )
+ {
+ char psz_buf[0x32];
+ size_t dev_path_length;
+ CFTypeRef str_bsd_path;
+
+ do
+ {
+ str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
+ CFSTR( kIOBSDNameKey ),
+ kCFAllocatorDefault,
+ 0 );
+ if( str_bsd_path == NULL )
+ {
+ IOObjectRelease( next_media );
+ continue;
+ }
+
+ snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
+ dev_path_length = strlen( psz_buf );
+
+ if( CFStringGetCString( str_bsd_path,
+ (char*)&psz_buf + dev_path_length,
+ sizeof(psz_buf) - dev_path_length,
+ kCFStringEncodingASCII ) )
+ {
+ CFRelease( str_bsd_path );
+ IOObjectRelease( next_media );
+ IOObjectRelease( media_iterator );
+ return strdup( psz_buf );
+ }
+
+ CFRelease( str_bsd_path );
+ IOObjectRelease( next_media );
+
+ } while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 );
+ }
+ IOObjectRelease( media_iterator );
+ return NULL;
+#endif /* HAVE_DARWIN_CDROM */
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_am_osx (const char *psz_source_name, const char *psz_access_mode)
+{
+
+ if (psz_access_mode != NULL)
+ cdio_warn ("there is only one access mode for OS X. Arg %s ignored",
+ psz_access_mode);
+ return cdio_open_osx(psz_source_name);
+}
+
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_osx (const char *psz_orig_source)
+{
+
+#ifdef HAVE_DARWIN_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+ char *psz_source;
+
+ cdio_funcs _funcs = {
+ .eject_media = _eject_media_osx,
+ .free = _free_osx,
+ .get_arg = _get_arg_osx,
+ .get_cdtext = get_cdtext_osx,
+ .get_default_device = cdio_get_default_device_osx,
+ .get_devices = cdio_get_devices_osx,
+ .get_discmode = get_discmode_osx,
+ .get_drive_cap = get_drive_cap_osx,
+ .get_first_track_num= get_first_track_num_generic,
+ .get_hwinfo = get_hwinfo_osx,
+ .get_mcn = get_mcn_osx,
+ .get_num_tracks = get_num_tracks_generic,
+ .get_track_format = get_track_format_osx,
+ .get_track_green = get_track_green_osx,
+ .get_track_lba = get_track_lba_osx,
+ .get_track_msf = NULL,
+ .lseek = cdio_generic_lseek,
+ .read = cdio_generic_read,
+ .read_audio_sectors = _get_read_audio_sectors_osx,
+ .read_mode1_sector = _get_read_mode1_sector_osx,
+ .read_mode1_sectors = _get_read_mode1_sectors_osx,
+ .read_mode2_sector = _get_read_mode2_sector_osx,
+ .read_mode2_sectors = _get_read_mode2_sectors_osx,
+ .read_toc = read_toc_osx,
+ .run_scsi_mmc_cmd = run_scsi_cmd_osx,
+ .set_arg = _set_arg_osx,
+ .stat_size = _stat_size_osx
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->access_mode = _AM_OSX;
+ _data->MediaClass_service = 0;
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+ _data->gen.toc_init = false;
+ _data->gen.b_cdtext_init = false;
+ _data->gen.b_cdtext_error = false;
+
+ if (NULL == psz_orig_source) {
+ psz_source=cdio_get_default_device_osx();
+ if (NULL == psz_source) return NULL;
+ _set_arg_osx(_data, "source", psz_source);
+ free(psz_source);
+ } else {
+ if (cdio_is_device_generic(psz_orig_source))
+ _set_arg_osx(_data, "source", psz_orig_source);
+ else {
+ /* The below would be okay if all device drivers worked this way. */
+#if 0
+ cdio_info ("source %s is a not a device", psz_orig_source);
+#endif
+ return NULL;
+ }
+ }
+
+ ret = cdio_new ((void *)_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (cdio_generic_init(_data) && init_osx(_data))
+ return ret;
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_DARWIN_CDROM */
+
+}
+
+bool
+cdio_have_osx (void)
+{
+#ifdef HAVE_DARWIN_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_DARWIN_CDROM */
+}
diff --git a/contrib/libcdio/_cdio_stdio.c b/contrib/libcdio/_cdio_stdio.c
new file mode 100644
index 000000000..0083b2194
--- /dev/null
+++ b/contrib/libcdio/_cdio_stdio.c
@@ -0,0 +1,222 @@
+/*
+ $Id: _cdio_stdio.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include <cdio/logging.h>
+#include <cdio/util.h>
+#include "_cdio_stream.h"
+#include "_cdio_stdio.h"
+
+static const char _rcsid[] = "$Id: _cdio_stdio.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $";
+
+#define CDIO_STDIO_BUFSIZE (128*1024)
+
+typedef struct {
+ char *pathname;
+ FILE *fd;
+ char *fd_buf;
+ off_t st_size; /* used only for source */
+} _UserData;
+
+static int
+_stdio_open (void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if ((ud->fd = fopen (ud->pathname, "rb")))
+ {
+ ud->fd_buf = _cdio_malloc (CDIO_STDIO_BUFSIZE);
+ setvbuf (ud->fd, ud->fd_buf, _IOFBF, CDIO_STDIO_BUFSIZE);
+ }
+
+ return (ud->fd == NULL);
+}
+
+static int
+_stdio_close(void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if (fclose (ud->fd))
+ cdio_error ("fclose (): %s", strerror (errno));
+
+ ud->fd = NULL;
+
+ free (ud->fd_buf);
+ ud->fd_buf = NULL;
+
+ return 0;
+}
+
+static void
+_stdio_free(void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if (ud->pathname)
+ free(ud->pathname);
+
+ if (ud->fd) /* should be NULL anyway... */
+ _stdio_close(user_data);
+
+ free(ud);
+}
+
+/*!
+ Like fseek(3) and in fact may be the same.
+
+ This function sets the file position indicator for the stream
+ pointed to by stream. The new position, measured in bytes, is obtained
+ by adding offset bytes to the position specified by whence. If whence
+ is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to
+ the start of the file, the current position indicator, or end-of-file,
+ respectively. A successful call to the fseek function clears the end-
+ of-file indicator for the stream and undoes any effects of the
+ ungetc(3) function on the same stream.
+
+ RETURN VALUE
+ Upon successful completion, return 0,
+ Otherwise, -1 is returned and the global variable errno is set to indi-
+ cate the error.
+*/
+static long
+_stdio_seek(void *user_data, long offset, int whence)
+{
+ _UserData *const ud = user_data;
+
+ if ( (offset=fseek (ud->fd, offset, whence)) ) {
+ cdio_error ("fseek (): %s", strerror (errno));
+ }
+
+ return offset;
+}
+
+static long int
+_stdio_stat(void *user_data)
+{
+ const _UserData *const ud = user_data;
+
+ return ud->st_size;
+}
+
+/*!
+ Like fread(3) and in fact is about the same.
+
+ DESCRIPTION:
+ The function fread reads nmemb elements of data, each size bytes long,
+ from the stream pointed to by stream, storing them at the location
+ given by ptr.
+
+ RETURN VALUE:
+ return the number of items successfully read or written (i.e.,
+ not the number of characters). If an error occurs, or the
+ end-of-file is reached, the return value is a short item count
+ (or zero).
+
+ We do not distinguish between end-of-file and error, and callers
+ must use feof(3) and ferror(3) to determine which occurred.
+ */
+static long
+_stdio_read(void *user_data, void *buf, long int count)
+{
+ _UserData *const ud = user_data;
+ long read;
+
+ read = fread(buf, 1, count, ud->fd);
+
+ if (read != count)
+ { /* fixme -- ferror/feof */
+ if (feof (ud->fd))
+ {
+ cdio_debug ("fread (): EOF encountered");
+ clearerr (ud->fd);
+ }
+ else if (ferror (ud->fd))
+ {
+ cdio_error ("fread (): %s", strerror (errno));
+ clearerr (ud->fd);
+ }
+ else
+ cdio_debug ("fread (): short read and no EOF?!?");
+ }
+
+ return read;
+}
+
+/*!
+ Deallocate resources assocaited with obj. After this obj is unusable.
+*/
+void
+cdio_stdio_destroy(CdioDataSource *obj)
+{
+ cdio_stream_destroy(obj);
+}
+
+CdioDataSource*
+cdio_stdio_new(const char pathname[])
+{
+ CdioDataSource *new_obj = NULL;
+ cdio_stream_io_functions funcs = { 0, };
+ _UserData *ud = NULL;
+ struct stat statbuf;
+
+ if (stat (pathname, &statbuf) == -1)
+ {
+ cdio_warn ("could not retrieve file info for `%s': %s",
+ pathname, strerror (errno));
+ return NULL;
+ }
+
+ ud = _cdio_malloc (sizeof (_UserData));
+
+ ud->pathname = strdup(pathname);
+ ud->st_size = statbuf.st_size; /* let's hope it doesn't change... */
+
+ funcs.open = _stdio_open;
+ funcs.seek = _stdio_seek;
+ funcs.stat = _stdio_stat;
+ funcs.read = _stdio_read;
+ funcs.close = _stdio_close;
+ funcs.free = _stdio_free;
+
+ new_obj = cdio_stream_new(ud, &funcs);
+
+ return new_obj;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/_cdio_stdio.h b/contrib/libcdio/_cdio_stdio.h
new file mode 100644
index 000000000..f5e79c41c
--- /dev/null
+++ b/contrib/libcdio/_cdio_stdio.h
@@ -0,0 +1,52 @@
+/*
+ $Id: _cdio_stdio.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifndef __CDIO_STDIO_H__
+#define __CDIO_STDIO_H__
+
+#include "_cdio_stream.h"
+
+/*!
+ Initialize a new stdio stream reading from pathname.
+ A pointer to the stream is returned or NULL if there was an error.
+
+ cdio_stream_free should be called on the returned value when you
+ don't need the stream any more. No other finalization is needed.
+ */
+CdioDataSource* cdio_stdio_new(const char pathname[]);
+
+/*!
+ Deallocate resources assocaited with obj. After this obj is unusable.
+*/
+void cdio_stdio_destroy(CdioDataSource *obj);
+
+
+#endif /* __CDIO_STREAM_STDIO_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/_cdio_stream.c b/contrib/libcdio/_cdio_stream.c
new file mode 100644
index 000000000..fc1f7fce7
--- /dev/null
+++ b/contrib/libcdio/_cdio_stream.c
@@ -0,0 +1,200 @@
+/*
+ $Id: _cdio_stream.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $
+
+ Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include "cdio_assert.h"
+
+/* #define STREAM_DEBUG */
+
+#include <cdio/logging.h>
+#include <cdio/util.h>
+#include "_cdio_stream.h"
+
+static const char _rcsid[] = "$Id: _cdio_stream.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $";
+
+/*
+ * DataSource implementations
+ */
+
+struct _CdioDataSource {
+ void* user_data;
+ cdio_stream_io_functions op;
+ int is_open;
+ long position;
+};
+
+/*
+ Open if not already open.
+ Return false if we hit an error. Errno should be set for that error.
+*/
+static bool
+_cdio_stream_open_if_necessary(CdioDataSource *obj)
+{
+ cdio_assert (obj != NULL);
+
+ if (!obj->is_open) {
+ if (obj->op.open(obj->user_data)) {
+ cdio_warn ("could not open input stream...");
+ return false;
+ } else {
+ cdio_debug ("opened source...");
+ obj->is_open = 1;
+ obj->position = 0;
+ }
+ }
+ return true;
+}
+
+/*!
+ Like 3 fseek and in fact may be the same.
+
+ This function sets the file position indicator for the stream
+ pointed to by stream. The new position, measured in bytes, is obtained
+ by adding offset bytes to the position specified by whence. If whence
+ is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to
+ the start of the file, the current position indicator, or end-of-file,
+ respectively. A successful call to the fseek function clears the end-
+ of-file indicator for the stream and undoes any effects of the
+ ungetc(3) function on the same stream.
+
+ RETURN VALUE
+ Upon successful completion, return 0,
+ Otherwise, -1 is returned and the global variable errno is set to indi-
+ cate the error.
+*/
+long
+cdio_stream_seek(CdioDataSource* obj, long offset, int whence)
+{
+ cdio_assert (obj != NULL);
+
+ if (!_cdio_stream_open_if_necessary(obj))
+ /* errno is set by _cdio_stream_open_if necessary. */
+ return -1;
+
+ if (obj->position != offset) {
+#ifdef STREAM_DEBUG
+ cdio_warn("had to reposition DataSource from %ld to %ld!", obj->position, offset);
+#endif
+ obj->position = offset;
+ return obj->op.seek(obj->user_data, offset, whence);
+ }
+
+ return 0;
+}
+
+CdioDataSource*
+cdio_stream_new(void *user_data, const cdio_stream_io_functions *funcs)
+{
+ CdioDataSource *new_obj;
+
+ new_obj = _cdio_malloc (sizeof (CdioDataSource));
+
+ new_obj->user_data = user_data;
+ memcpy(&(new_obj->op), funcs, sizeof(cdio_stream_io_functions));
+
+ return new_obj;
+}
+
+/*!
+ Like fread(3) and in fact may be the same.
+
+ DESCRIPTION:
+ The function fread reads nmemb elements of data, each size bytes long,
+ from the stream pointed to by stream, storing them at the location
+ given by ptr.
+
+ RETURN VALUE:
+ return the number of items successfully read or written (i.e.,
+ not the number of characters). If an error occurs, or the
+ end-of-file is reached, the return value is a short item count
+ (or zero).
+
+ We do not distinguish between end-of-file and error, and callers
+ must use feof(3) and ferror(3) to determine which occurred.
+*/
+long
+cdio_stream_read(CdioDataSource* obj, void *ptr, long size, long nmemb)
+{
+ long read_bytes;
+
+ cdio_assert (obj != NULL);
+
+ if (!_cdio_stream_open_if_necessary(obj)) return 0;
+
+ read_bytes = obj->op.read(obj->user_data, ptr, size*nmemb);
+ obj->position += read_bytes;
+
+ return read_bytes;
+}
+
+/*!
+ Return whatever size of stream reports, I guess unit size is bytes.
+ On error return -1;
+ */
+long int
+cdio_stream_stat(CdioDataSource* obj)
+{
+ cdio_assert (obj != NULL);
+
+ if (!_cdio_stream_open_if_necessary(obj)) return -1;
+
+ return obj->op.stat(obj->user_data);
+}
+
+void
+cdio_stream_close(CdioDataSource* obj)
+{
+ cdio_assert (obj != NULL);
+
+ if (obj->is_open) {
+ cdio_debug ("closed source...");
+ obj->op.close(obj->user_data);
+ obj->is_open = 0;
+ obj->position = 0;
+ }
+}
+
+void
+cdio_stream_destroy(CdioDataSource* obj)
+{
+ cdio_assert (obj != NULL);
+
+ cdio_stream_close(obj);
+
+ obj->op.free(obj->user_data);
+
+ free(obj);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/_cdio_stream.h b/contrib/libcdio/_cdio_stream.h
new file mode 100644
index 000000000..ffbb4098e
--- /dev/null
+++ b/contrib/libcdio/_cdio_stream.h
@@ -0,0 +1,127 @@
+/*
+ $Id: _cdio_stream.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifndef __CDIO_STREAM_H__
+#define __CDIO_STREAM_H__
+
+#include <cdio/types.h>
+#include "cdio_private.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /* typedef'ed IO functions prototypes */
+
+ typedef int(*cdio_data_open_t)(void *user_data);
+
+ typedef long(*cdio_data_read_t)(void *user_data, void *buf, long count);
+
+ typedef long(*cdio_data_seek_t)(void *user_data, long offset, int whence);
+
+ typedef long(*cdio_data_stat_t)(void *user_data);
+
+ typedef int(*cdio_data_close_t)(void *user_data);
+
+ typedef void(*cdio_data_free_t)(void *user_data);
+
+
+ /* abstract data source */
+
+ typedef struct {
+ cdio_data_open_t open;
+ cdio_data_seek_t seek;
+ cdio_data_stat_t stat;
+ cdio_data_read_t read;
+ cdio_data_close_t close;
+ cdio_data_free_t free;
+ } cdio_stream_io_functions;
+
+ CdioDataSource*
+ cdio_stream_new(void *user_data, const cdio_stream_io_functions *funcs);
+
+ /*!
+ Like fread(3) and in fact may be the same.
+
+ DESCRIPTION:
+ The function fread reads nmemb elements of data, each size bytes long,
+ from the stream pointed to by stream, storing them at the location
+ given by ptr.
+
+ RETURN VALUE:
+ return the number of items successfully read or written (i.e.,
+ not the number of characters). If an error occurs, or the
+ end-of-file is reached, the return value is a short item count
+ (or zero).
+
+ We do not distinguish between end-of-file and error, and callers
+ must use feof(3) and ferror(3) to determine which occurred.
+ */
+ long
+ cdio_stream_read(CdioDataSource* obj, void *ptr, long size, long nmemb);
+
+ /*!
+ Like fseek(3) and in fact may be the same.
+
+ This function sets the file position indicator for the stream
+ pointed to by stream. The new position, measured in bytes, is obtained
+ by adding offset bytes to the position specified by whence. If whence
+ is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to
+ the start of the file, the current position indicator, or end-of-file,
+ respectively. A successful call to the fseek function clears the end-
+ of-file indicator for the stream and undoes any effects of the
+ ungetc(3) function on the same stream.
+
+ RETURN VALUE
+ Upon successful completion, return 0,
+ Otherwise, -1 is returned and the global variable errno is set to indi-
+ cate the error.
+ */
+ long int cdio_stream_seek(CdioDataSource* obj, long offset, int whence);
+
+ /*!
+ Return whatever size of stream reports, I guess unit size is bytes.
+ On error return -1;
+ */
+ long int cdio_stream_stat(CdioDataSource* obj);
+
+ /*!
+ Deallocate resources assocaited with obj. After this obj is unusable.
+ */
+ void cdio_stream_destroy(CdioDataSource* obj);
+
+ void cdio_stream_close(CdioDataSource* obj);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_STREAM_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/_cdio_sunos.c b/contrib/libcdio/_cdio_sunos.c
new file mode 100644
index 000000000..245d1e319
--- /dev/null
+++ b/contrib/libcdio/_cdio_sunos.c
@@ -0,0 +1,917 @@
+/*
+ $Id: _cdio_sunos.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include <cdio/logging.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include <cdio/scsi_mmc.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+
+#define DEFAULT_CDIO_DEVICE "/vol/dev/aliases/cdrom0"
+
+#ifdef HAVE_SOLARIS_CDROM
+
+static const char _rcsid[] = "$Id: _cdio_sunos.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $";
+
+#ifdef HAVE_GLOB_H
+#include <glob.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_CDIO_H
+# include <sys/cdio.h> /* CDIOCALLOW etc... */
+#else
+#error "You need <sys/cdio.h> to have CDROM support"
+#endif
+
+#include <sys/dkio.h>
+#include <sys/scsi/generic/commands.h>
+#include <sys/scsi/impl/uscsi.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include "cdtext_private.h"
+
+/* not defined in dkio.h yet */
+#define DK_DVDRW 0x13
+
+/* reader */
+
+typedef enum {
+ _AM_NONE,
+ _AM_SUN_CTRL_ATAPI,
+ _AM_SUN_CTRL_SCSI
+#if FINISHED
+ _AM_READ_CD,
+ _AM_READ_10
+#endif
+} access_mode_t;
+
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ access_mode_t access_mode;
+
+ /* Some of the more OS specific things. */
+ /* Entry info for each track, add 1 for leadout. */
+ struct cdrom_tocentry tocent[CDIO_CD_MAX_TRACKS+1];
+
+ /* Track information */
+ struct cdrom_tochdr tochdr;
+} _img_private_t;
+
+static track_format_t get_track_format_solaris(void *p_user_data,
+ track_t i_track);
+
+static access_mode_t
+str_to_access_mode_sunos(const char *psz_access_mode)
+{
+ const access_mode_t default_access_mode = _AM_SUN_CTRL_SCSI;
+
+ if (NULL==psz_access_mode) return default_access_mode;
+
+ if (!strcmp(psz_access_mode, "ATAPI"))
+ return _AM_SUN_CTRL_SCSI; /* force ATAPI to be SCSI */
+ else if (!strcmp(psz_access_mode, "SCSI"))
+ return _AM_SUN_CTRL_SCSI;
+ else {
+ cdio_warn ("unknown access type: %s. Default SCSI used.",
+ psz_access_mode);
+ return default_access_mode;
+ }
+}
+
+
+/*!
+ Initialize CD device.
+ */
+static bool
+init_solaris (_img_private_t *p_env)
+{
+
+ if (!cdio_generic_init(p_env)) return false;
+
+ p_env->access_mode = _AM_SUN_CTRL_SCSI;
+
+ return true;
+}
+
+/*!
+ Run a SCSI MMC command.
+
+ p_user_data internal CD structure.
+ i_timeout_ms time in milliseconds we will wait for the command
+ to complete.
+ i_cdb Size of p_cdb
+ p_cdb CDB bytes.
+ e_direction direction the transfer is to go.
+ i_buf Size of buffer
+ p_buf Buffer for data, both sending and receiving
+
+ Return 0 if no error.
+ */
+static int
+run_scsi_cmd_solaris( const void *p_user_data, unsigned int i_timeout_ms,
+ unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf )
+{
+ const _img_private_t *p_env = p_user_data;
+ struct uscsi_cmd cgc;
+
+ memset (&cgc, 0, sizeof (struct uscsi_cmd));
+ cgc.uscsi_cdb = (caddr_t) p_cdb;
+
+ cgc.uscsi_flags = SCSI_MMC_DATA_READ == e_direction ?
+ USCSI_READ : USCSI_WRITE;
+
+ cgc.uscsi_timeout = msecs2secs(i_timeout_ms);
+ cgc.uscsi_bufaddr = p_buf;
+ cgc.uscsi_buflen = i_buf;
+ cgc.uscsi_cdblen = i_cdb;
+
+ return ioctl(p_env->gen.fd, USCSICMD, &cgc);
+}
+
+/*!
+ Reads audio sectors from CD device into data starting from lsn.
+ Returns 0 if no error.
+
+ May have to check size of nblocks. There may be a limit that
+ can be read in one go, e.g. 25 blocks.
+*/
+
+static int
+_read_audio_sectors_solaris (void *p_user_data, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ struct cdrom_msf solaris_msf;
+ msf_t _msf;
+ struct cdrom_cdda cdda;
+
+ _img_private_t *p_env = p_user_data;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ solaris_msf.cdmsf_min0 = cdio_from_bcd8(_msf.m);
+ solaris_msf.cdmsf_sec0 = cdio_from_bcd8(_msf.s);
+ solaris_msf.cdmsf_frame0 = cdio_from_bcd8(_msf.f);
+
+ if (p_env->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged < 75
+ || (p_env->gen.ioctls_debugged < (30 * 75)
+ && p_env->gen.ioctls_debugged % 75 == 0)
+ || p_env->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %d", lsn);
+
+ p_env->gen.ioctls_debugged++;
+
+ cdda.cdda_addr = lsn;
+ cdda.cdda_length = nblocks;
+ cdda.cdda_data = (caddr_t) data;
+ cdda.cdda_subcode = CDROM_DA_NO_SUBCODE;
+
+ if (ioctl (p_env->gen.fd, CDROMCDDA, &cdda) == -1) {
+ perror ("ioctl(..,CDROMCDDA,..)");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+
+ return 0;
+}
+
+/*!
+ Reads a single mode1 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode1_sector_solaris (void *env, void *data, lsn_t lsn,
+ bool b_form2)
+{
+
+#if FIXED
+ do something here.
+#else
+ return cdio_generic_read_form1_sector(env, data, lsn);
+#endif
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode1_sectors_solaris (void *p_user_data, void *p_data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *p_env = p_user_data;
+ unsigned int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode1_sector_solaris (p_env,
+ ((char *)p_data) + (blocksize * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode2_sector_solaris (void *p_user_data, void *p_data, lsn_t lsn,
+ bool b_form2)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ struct cdrom_msf solaris_msf;
+ msf_t _msf;
+ int offset = 0;
+ struct cdrom_cdxa cd_read;
+
+ _img_private_t *p_env = p_user_data;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ solaris_msf.cdmsf_min0 = cdio_from_bcd8(_msf.m);
+ solaris_msf.cdmsf_sec0 = cdio_from_bcd8(_msf.s);
+ solaris_msf.cdmsf_frame0 = cdio_from_bcd8(_msf.f);
+
+ if (p_env->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (p_env->gen.ioctls_debugged < 75
+ || (p_env->gen.ioctls_debugged < (30 * 75)
+ && p_env->gen.ioctls_debugged % 75 == 0)
+ || p_env->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %2.2d:%2.2d:%2.2d",
+ solaris_msf.cdmsf_min0, solaris_msf.cdmsf_sec0,
+ solaris_msf.cdmsf_frame0);
+
+ p_env->gen.ioctls_debugged++;
+
+ /* Using CDROMXA ioctl will actually use the same uscsi command
+ * as ATAPI, except we don't need to be root
+ */
+ offset = CDIO_CD_XA_SYNC_HEADER;
+ cd_read.cdxa_addr = lsn;
+ cd_read.cdxa_data = buf;
+ cd_read.cdxa_length = 1;
+ cd_read.cdxa_format = CDROM_XA_SECTOR_DATA;
+ if (ioctl (p_env->gen.fd, CDROMCDXA, &cd_read) == -1) {
+ perror ("ioctl(..,CDROMCDXA,..)");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+
+ if (b_form2)
+ memcpy (p_data, buf + (offset-CDIO_CD_SUBHEADER_SIZE), M2RAW_SECTOR_SIZE);
+ else
+ memcpy (((char *)p_data), buf + offset, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode2_sectors_solaris (void *p_user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *env = p_user_data;
+ unsigned int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode2_sector_solaris (env,
+ ((char *)data) + (blocksize * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_cdio_stat_size (void *p_user_data)
+{
+ _img_private_t *env = p_user_data;
+
+ struct cdrom_tocentry tocent;
+ uint32_t size;
+
+ tocent.cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ tocent.cdte_format = CDIO_CDROM_LBA;
+ if (ioctl (env->gen.fd, CDROMREADTOCENTRY, &tocent) == -1)
+ {
+ perror ("ioctl(CDROMREADTOCENTRY)");
+ exit (EXIT_FAILURE);
+ }
+
+ size = tocent.cdte_addr.lba;
+
+ return size;
+}
+
+/*!
+ Set the arg "key" with "value" in the source device.
+ Currently "source" and "access-mode" are valid keys.
+ "source" sets the source device in I/O operations
+ "access-mode" sets the the method of CD access
+
+ 0 is returned if no error was found, and nonzero if there as an error.
+*/
+static int
+_set_arg_solaris (void *p_user_data, const char key[], const char value[])
+{
+ _img_private_t *env = p_user_data;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (env->gen.source_name);
+
+ env->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ env->access_mode = str_to_access_mode_sunos(key);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return true if successful or false if an error.
+*/
+static bool
+read_toc_solaris (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+ int i;
+
+ /* read TOC header */
+ if ( ioctl(p_env->gen.fd, CDROMREADTOCHDR, &p_env->tochdr) == -1 ) {
+ cdio_warn("%s: %s\n",
+ "error in ioctl CDROMREADTOCHDR", strerror(errno));
+ return false;
+ }
+
+ p_env->gen.i_first_track = p_env->tochdr.cdth_trk0;
+ p_env->gen.i_tracks = p_env->tochdr.cdth_trk1;
+
+ /* read individual tracks */
+ for (i=p_env->gen.i_first_track; i<=p_env->gen.i_tracks; i++) {
+ p_env->tocent[i-1].cdte_track = i;
+ p_env->tocent[i-1].cdte_format = CDIO_CDROM_MSF;
+ if ( ioctl(p_env->gen.fd, CDROMREADTOCENTRY, &p_env->tocent[i-1]) == -1 ) {
+ cdio_warn("%s %d: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for track",
+ i, strerror(errno));
+ return false;
+ }
+ }
+
+ /* read the lead-out track */
+ p_env->tocent[p_env->tochdr.cdth_trk1].cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ p_env->tocent[p_env->tochdr.cdth_trk1].cdte_format = CDIO_CDROM_MSF;
+
+ if (ioctl(p_env->gen.fd, CDROMREADTOCENTRY,
+ &p_env->tocent[p_env->tochdr.cdth_trk1]) == -1 ) {
+ cdio_warn("%s: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for lead-out",
+ strerror(errno));
+ return false;
+ }
+
+ p_env->gen.toc_init = true;
+ return true;
+}
+
+/*!
+ Eject media in CD drive. If successful, as a side effect we
+ also free obj.
+ */
+static int
+eject_media_solaris (void *p_user_data) {
+
+ _img_private_t *env = p_user_data;
+ int ret;
+
+ close(env->gen.fd);
+ env->gen.fd = -1;
+ if (env->gen.fd > -1) {
+ if ((ret = ioctl(env->gen.fd, CDROMEJECT)) != 0) {
+ cdio_generic_free((void *) env);
+ cdio_warn ("CDROMEJECT failed: %s\n", strerror(errno));
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+ return 2;
+}
+
+
+static void *
+_cdio_malloc_and_zero(size_t size) {
+ void *ptr;
+
+ if( !size ) size++;
+
+ if((ptr = malloc(size)) == NULL) {
+ cdio_warn("malloc() failed: %s", strerror(errno));
+ return NULL;
+ }
+
+ memset(ptr, 0, size);
+ return ptr;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+get_arg_solaris (void *p_user_data, const char key[])
+{
+ _img_private_t *env = p_user_data;
+
+ if (!strcmp (key, "source")) {
+ return env->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (env->access_mode) {
+ case _AM_SUN_CTRL_ATAPI:
+ return "ATAPI";
+ case _AM_SUN_CTRL_SCSI:
+ return "SCSI";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char *
+cdio_get_default_device_solaris(void)
+{
+ char *volume_device;
+ char *volume_name;
+ char *volume_action;
+ char *device;
+ struct stat stb;
+
+ if ((volume_device = getenv("VOLUME_DEVICE")) != NULL &&
+ (volume_name = getenv("VOLUME_NAME")) != NULL &&
+ (volume_action = getenv("VOLUME_ACTION")) != NULL &&
+ strcmp(volume_action, "insert") == 0) {
+
+ device = _cdio_malloc_and_zero(strlen(volume_device)
+ + strlen(volume_name) + 2);
+ if (device == NULL)
+ return strdup(DEFAULT_CDIO_DEVICE);
+ sprintf(device, "%s/%s", volume_device, volume_name);
+ if (stat(device, &stb) != 0 || !S_ISCHR(stb.st_mode)) {
+ free(device);
+ return strdup(DEFAULT_CDIO_DEVICE);
+ }
+ return device;
+ }
+ /* Check if it could be a Solaris media*/
+ if((stat(DEFAULT_CDIO_DEVICE, &stb) == 0) && S_ISDIR(stb.st_mode)) {
+ device = _cdio_malloc_and_zero(strlen(DEFAULT_CDIO_DEVICE) + 4);
+ sprintf(device, "%s/s0", DEFAULT_CDIO_DEVICE);
+ return device;
+ }
+ return strdup(DEFAULT_CDIO_DEVICE);
+}
+
+/*!
+ Get disc type associated with cd object.
+*/
+
+static discmode_t
+get_discmode_solaris (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+ track_t i_track;
+ discmode_t discmode=CDIO_DISC_MODE_NO_INFO;
+ struct dk_minfo media;
+ int ret;
+
+ /* Get the media info */
+ if((ret = ioctl(p_env->gen.fd, DKIOCGMEDIAINFO, &media)) != 0) {
+ cdio_warn ("DKIOCGMEDIAINFO failed: %s\n", strerror(errno));
+ return CDIO_DISC_MODE_NO_INFO;
+ }
+ switch(media.dki_media_type) {
+ case DK_CDROM:
+ case DK_CDR:
+ case DK_CDRW:
+ /* Do cdrom detection */
+ break;
+ case DK_DVDROM: return CDIO_DISC_MODE_DVD_ROM;
+ case DK_DVDR: discmode = CDIO_DISC_MODE_DVD_R;
+ break;
+ case DK_DVDRAM: discmode = CDIO_DISC_MODE_DVD_RAM;
+ break;
+ case DK_DVDRW:
+ case DK_DVDRW+1: discmode = CDIO_DISC_MODE_DVD_RW;
+ break;
+ default: /* no valid match */
+ return CDIO_DISC_MODE_NO_INFO;
+ }
+
+ if((discmode == CDIO_DISC_MODE_DVD_RAM ||
+ discmode == CDIO_DISC_MODE_DVD_RW ||
+ discmode == CDIO_DISC_MODE_DVD_R)) {
+ /* Fallback to uscsi if we can */
+ if(geteuid() == 0)
+ return get_discmode_solaris(p_user_data);
+ return discmode;
+ }
+
+ if (!p_env->gen.toc_init)
+ read_toc_solaris (p_env);
+
+ if (!p_env->gen.toc_init)
+ return CDIO_DISC_MODE_NO_INFO;
+
+ for (i_track = p_env->gen.i_first_track;
+ i_track < p_env->gen.i_first_track + p_env->tochdr.cdth_trk1 ;
+ i_track ++) {
+ track_format_t track_fmt=get_track_format_solaris(p_env, i_track);
+
+ switch(track_fmt) {
+ case TRACK_FORMAT_AUDIO:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_DA;
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_XA:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_DATA:
+ switch(discmode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ discmode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* No change*/
+ break;
+ default:
+ discmode = CDIO_DISC_MODE_CD_MIXED;
+ }
+ break;
+ case TRACK_FORMAT_ERROR:
+ default:
+ discmode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ return discmode;
+}
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+get_track_format_solaris(void *p_user_data, track_t i_track)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if ( !p_env ) return TRACK_FORMAT_ERROR;
+ if (!p_env->gen.init) init_solaris(p_env);
+ if (!p_env->gen.toc_init) read_toc_solaris (p_user_data) ;
+
+ if ( (i_track > p_env->gen.i_tracks+p_env->gen.i_first_track)
+ || i_track < p_env->gen.i_first_track)
+ return TRACK_FORMAT_ERROR;
+
+ i_track -= p_env->gen.i_first_track;
+
+ /* This is pretty much copied from the "badly broken" cdrom_count_tracks
+ in linux/cdrom.c.
+ */
+ if (p_env->tocent[i_track].cdte_ctrl & CDROM_DATA_TRACK) {
+ if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_CDI_TRACK)
+ return TRACK_FORMAT_CDI;
+ else if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_XA_TRACK)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *p_user_data, track_t i_track)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if ( !p_env ) return false;
+ if (!p_env->gen.init) init_solaris(p_env);
+ if (!p_env->gen.toc_init) read_toc_solaris (p_env) ;
+
+ if (i_track >= p_env->gen.i_tracks+p_env->gen.i_first_track
+ || i_track < p_env->gen.i_first_track)
+ return false;
+
+ i_track -= p_env->gen.i_first_track;
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cd-info for a while.
+ */
+ return ((p_env->tocent[i_track].cdte_ctrl & 2) != 0);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj. Track numbers usually start at something
+ greater than 0, usually 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no entry.
+*/
+static bool
+_cdio_get_track_msf(void *p_user_data, track_t i_track, msf_t *msf)
+{
+ _img_private_t *p_env = p_user_data;
+
+ if (NULL == msf) return false;
+
+ if (!p_env->gen.init) init_solaris(p_env);
+ if (!p_env->gen.toc_init) read_toc_solaris (p_env) ;
+
+ if (i_track == CDIO_CDROM_LEADOUT_TRACK)
+ i_track = p_env->gen.i_tracks + p_env->gen.i_first_track;
+
+ if (i_track > (p_env->gen.i_tracks+p_env->gen.i_first_track)
+ || i_track < p_env->gen.i_first_track) {
+ return false;
+ } else {
+ struct cdrom_tocentry *msf0 = &p_env->tocent[i_track-1];
+ msf->m = cdio_to_bcd8(msf0->cdte_addr.msf.minute);
+ msf->s = cdio_to_bcd8(msf0->cdte_addr.msf.second);
+ msf->f = cdio_to_bcd8(msf0->cdte_addr.msf.frame);
+ return true;
+ }
+}
+
+#else
+/*!
+ Return a string containing the default VCD device if none is specified.
+ */
+char *
+cdio_get_default_device_solaris(void)
+{
+ return strdup(DEFAULT_CDIO_DEVICE);
+}
+
+#endif /* HAVE_SOLARIS_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_solaris (void)
+{
+#ifndef HAVE_SOLARIS_CDROM
+ return NULL;
+#else
+ char volpath[256];
+ struct stat st;
+ char **drives = NULL;
+ unsigned int i_files=0;
+#ifdef HAVE_GLOB_H
+ unsigned int i;
+ glob_t globbuf;
+
+ globbuf.gl_offs = 0;
+ glob("/vol/dev/aliases/cdrom*", GLOB_DOOFFS, NULL, &globbuf);
+ for (i=0; i<globbuf.gl_pathc; i++) {
+ if(stat(globbuf.gl_pathv[i], &st) < 0)
+ continue;
+
+ /* Check if this is a directory, if so it's probably Solaris media */
+ if(S_ISDIR(st.st_mode)) {
+ sprintf(volpath, "%s/s0", globbuf.gl_pathv[i]);
+ if(stat(volpath, &st) == 0)
+ cdio_add_device_list(&drives, volpath, &i_files);
+ }else
+ cdio_add_device_list(&drives, globbuf.gl_pathv[i], &i_files);
+ }
+ globfree(&globbuf);
+#else
+ if(stat(DEFAULT_CDIO_DEVICE, &st) == 0) {
+ /* Check if this is a directory, if so it's probably Solaris media */
+ if(S_ISDIR(st.st_mode)) {
+ sprintf(volpath, "%s/s0", DEFAULT_CDIO_DEVICE);
+ if(stat(volpath, &st) == 0)
+ cdio_add_device_list(&drives, volpath, &i_files);
+ }else
+ cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &i_files);
+ }
+#endif /*HAVE_GLOB_H*/
+ cdio_add_device_list(&drives, NULL, &i_files);
+ return drives;
+#endif /*HAVE_SOLARIS_CDROM*/
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_solaris (const char *psz_source_name)
+{
+ return cdio_open_am_solaris(psz_source_name, NULL);
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_am_solaris (const char *psz_orig_source, const char *access_mode)
+{
+
+#ifdef HAVE_SOLARIS_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+ char *psz_source;
+
+ cdio_funcs _funcs;
+
+ _funcs.eject_media = eject_media_solaris;
+ _funcs.free = cdio_generic_free;
+ _funcs.get_arg = get_arg_solaris;
+ _funcs.get_cdtext = get_cdtext_generic;
+ _funcs.get_default_device = cdio_get_default_device_solaris;
+ _funcs.get_devices = cdio_get_devices_solaris;
+ _funcs.get_discmode = get_discmode_solaris;
+ _funcs.get_drive_cap = scsi_mmc_get_drive_cap_generic;
+ _funcs.get_first_track_num= get_first_track_num_generic;
+ _funcs.get_hwinfo = NULL;
+ _funcs.get_mcn = scsi_mmc_get_mcn_generic,
+ _funcs.get_num_tracks = get_num_tracks_generic;
+ _funcs.get_track_format = get_track_format_solaris;
+ _funcs.get_track_green = _cdio_get_track_green;
+ _funcs.get_track_lba = NULL; /* This could be implemented if need be. */
+ _funcs.get_track_msf = _cdio_get_track_msf;
+ _funcs.lseek = cdio_generic_lseek;
+ _funcs.read = cdio_generic_read;
+ _funcs.read_audio_sectors = _read_audio_sectors_solaris;
+ _funcs.read_mode1_sector = _read_mode1_sector_solaris;
+ _funcs.read_mode1_sectors = _read_mode1_sectors_solaris;
+ _funcs.read_mode2_sector = _read_mode2_sector_solaris;
+ _funcs.read_mode2_sectors = _read_mode2_sectors_solaris;
+ _funcs.read_toc = read_toc_solaris;
+ _funcs.run_scsi_mmc_cmd = run_scsi_cmd_solaris;
+ _funcs.stat_size = _cdio_stat_size;
+ _funcs.set_arg = _set_arg_solaris;
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+
+ _data->access_mode = _AM_SUN_CTRL_SCSI;
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+ _data->gen.toc_init = false;
+ _data->gen.b_cdtext_init = false;
+ _data->gen.b_cdtext_error = false;
+
+ if (NULL == psz_orig_source) {
+ psz_source = cdio_get_default_device_solaris();
+ if (NULL == psz_source) return NULL;
+ _set_arg_solaris(_data, "source", psz_source);
+ free(psz_source);
+ } else {
+ if (cdio_is_device_generic(psz_orig_source))
+ _set_arg_solaris(_data, "source", psz_orig_source);
+ else {
+ /* The below would be okay if all device drivers worked this way. */
+#if 0
+ cdio_info ("source %s is not a device", psz_orig_source);
+#endif
+ return NULL;
+ }
+ }
+
+ ret = cdio_new ( (void *) _data, &_funcs );
+ if (ret == NULL) return NULL;
+
+ if (init_solaris(_data))
+ return ret;
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_SOLARIS_CDROM */
+
+}
+
+bool
+cdio_have_solaris (void)
+{
+#ifdef HAVE_SOLARIS_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_SOLARIS_CDROM */
+}
diff --git a/contrib/libcdio/cd_types.c b/contrib/libcdio/cd_types.c
new file mode 100644
index 000000000..b983f9899
--- /dev/null
+++ b/contrib/libcdio/cd_types.c
@@ -0,0 +1,358 @@
+/*
+ $Id: cd_types.c,v 1.4 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*
+ This tries to determine what kind of CD-image or filesystems on a
+ track we've got.
+*/
+#include "config.h"
+
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+#include <cdio/logging.h>
+#include <cdio/util.h>
+#include <cdio/cd_types.h>
+
+/*
+Subject: -65- How can I read an IRIX (EFS) CD-ROM on a machine which
+ doesn't use EFS?
+Date: 18 Jun 1995 00:00:01 EST
+
+ You want 'efslook', at
+ ftp://viz.tamu.edu/pub/sgi/software/efslook.tar.gz.
+
+and
+! Robert E. Seastrom <rs@access4.digex.net>'s software (with source
+! code) for using an SGI CD-ROM on a Macintosh is at
+! ftp://bifrost.seastrom.com/pub/mac/CDROM-Jumpstart.sit151.hqx.
+
+*/
+
+static char buffer[6][CDIO_CD_FRAMESIZE_RAW]; /* for CD-Data */
+
+/* Some interesting sector numbers stored in the above buffer. */
+#define ISO_SUPERBLOCK_SECTOR 16 /* buffer[0] */
+#define UFS_SUPERBLOCK_SECTOR 4 /* buffer[2] */
+#define BOOT_SECTOR 17 /* buffer[3] */
+#define VCD_INFO_SECTOR 150 /* buffer[4] */
+#define XISO_SECTOR 32 /* buffer[4] */
+#define UDFX_SECTOR 32 /* buffer[4] */
+#define UDF_ANCHOR_SECTOR 256 /* buffer[5] */
+
+
+typedef struct signature
+{
+ unsigned int buf_num;
+ unsigned int offset;
+ const char *sig_str;
+ const char *description;
+} signature_t;
+
+static signature_t sigs[] =
+ {
+/*buffer[x] off look for description */
+ {0, 0, "MICROSOFT*XBOX*MEDIA", "XBOX CD"},
+ {0, 1, "BEA01", "UDF"},
+ {0, 1, ISO_STANDARD_ID, "ISO 9660"},
+ {0, 1, "CD-I", "CD-I"},
+ {0, 8, "CDTV", "CDTV"},
+ {0, 8, "CD-RTOS", "CD-RTOS"},
+ {0, 9, "CDROM", "HIGH SIERRA"},
+ {0, 16, "CD-BRIDGE", "BRIDGE"},
+ {0, ISO_XA_MARKER_OFFSET, ISO_XA_MARKER_STRING, "XA"},
+ {1, 64, "PPPPHHHHOOOOTTTTOOOO____CCCCDDDD", "PHOTO CD"},
+ {1, 0x438, "\x53\xef", "EXT2 FS"},
+ {2, 1372, "\x54\x19\x01\x0", "UFS"},
+ {3, 7, "EL TORITO", "BOOTABLE"},
+ {4, 0, "VIDEO_CD", "VIDEO CD"},
+ {4, 0, "SUPERVCD", "SVCD or Chaoji VCD"},
+ { 0 }
+ };
+
+
+/* The below index into the above sigs array. Make sure things match. */
+#define INDEX_XISO 0 /* Microsoft X-BOX filesystem */
+#define INDEX_UDF 1
+#define INDEX_ISOFS 2
+#define INDEX_CD_I 3
+#define INDEX_CDTV 4
+#define INDEX_CD_RTOS 5
+#define INDEX_HS 6
+#define INDEX_BRIDGE 7
+#define INDEX_XA 8
+#define INDEX_PHOTO_CD 9
+#define INDEX_EXT2 10
+#define INDEX_UFS 11
+#define INDEX_BOOTABLE 12
+#define INDEX_VIDEO_CD 13 /* Video CD */
+#define INDEX_SVCD 14 /* CVD *or* SVCD */
+
+
+/*
+ Read a particular block into the global array to be used for further
+ analysis later.
+*/
+static int
+_cdio_read_block(const CdIo *cdio, int superblock, uint32_t offset,
+ uint8_t bufnum, track_t i_track)
+{
+ unsigned int track_sec_count = cdio_get_track_sec_count(cdio, i_track);
+ memset(buffer[bufnum], 0, CDIO_CD_FRAMESIZE);
+
+ if ( track_sec_count < superblock) {
+ cdio_debug("reading block %u skipped track %d has only %u sectors\n",
+ superblock, i_track, track_sec_count);
+ return -1;
+ }
+
+ cdio_debug("about to read sector %lu\n",
+ (long unsigned int) offset+superblock);
+
+ if (cdio_get_track_green(cdio, i_track)) {
+ if (0 > cdio_read_mode2_sector(cdio, buffer[bufnum],
+ offset+superblock, false))
+ return -1;
+ } else {
+ if (0 > cdio_read_mode1_sector(cdio, buffer[bufnum],
+ offset+superblock, false))
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ Return true if the previously read-in buffer contains a "signature" that
+ matches index "num".
+ */
+static bool
+_cdio_is_it(int num)
+{
+ signature_t *sigp=&sigs[num];
+ int len=strlen(sigp->sig_str);
+
+ /* TODO: check that num < largest sig. */
+ return 0 == memcmp(&buffer[sigp->buf_num][sigp->offset], sigp->sig_str, len);
+}
+
+static int
+_cdio_is_hfs(void)
+{
+ return (0 == memcmp(&buffer[1][512],"PM",2)) ||
+ (0 == memcmp(&buffer[1][512],"TS",2)) ||
+ (0 == memcmp(&buffer[1][1024], "BD",2));
+}
+
+static int
+_cdio_is_3do(void)
+{
+ return (0 == memcmp(&buffer[1][0],"\x01\x5a\x5a\x5a\x5a\x5a\x01", 7)) &&
+ (0 == memcmp(&buffer[1][40], "CD-ROM", 6));
+}
+
+static int
+_cdio_is_joliet(void)
+{
+ return 2 == buffer[3][0] && buffer[3][88] == 0x25 && buffer[3][89] == 0x2f;
+}
+
+static int
+_cdio_is_UDF(void)
+{
+ return 2 == ((uint16_t)buffer[5][0] | ((uint16_t)buffer[5][1] << 8));
+}
+
+/* ISO 9660 volume space in M2F1_SECTOR_SIZE byte units */
+static int
+_cdio_get_iso9660_fs_sec_count(void)
+{
+ return ((buffer[0][80] & 0xff) |
+ ((buffer[0][81] & 0xff) << 8) |
+ ((buffer[0][82] & 0xff) << 16) |
+ ((buffer[0][83] & 0xff) << 24));
+}
+
+static int
+_cdio_get_joliet_level( void )
+{
+ switch (buffer[3][90]) {
+ case 0x40: return 1;
+ case 0x43: return 2;
+ case 0x45: return 3;
+ }
+ return 0;
+}
+
+/*
+ Try to determine what kind of CD-image and/or filesystem we
+ have at track i_track. Return information about the CD image
+ is returned in cdio_analysis and the return value.
+*/
+cdio_fs_anal_t
+cdio_guess_cd_type(const CdIo *cdio, int start_session, track_t i_track,
+ /*out*/ cdio_iso_analysis_t *iso_analysis)
+{
+ int ret = CDIO_FS_UNKNOWN;
+ bool sector0_read_ok;
+
+ if (TRACK_FORMAT_AUDIO == cdio_get_track_format(cdio, i_track))
+ return CDIO_FS_AUDIO;
+
+ if ( _cdio_read_block(cdio, ISO_PVD_SECTOR, start_session,
+ 0, i_track) < 0 )
+ return CDIO_FS_UNKNOWN;
+
+ if ( _cdio_is_it(INDEX_XISO) )
+ return CDIO_FS_ANAL_XISO;
+
+ if (_cdio_read_block(cdio, ISO_SUPERBLOCK_SECTOR, start_session, 0,
+ i_track) < 0)
+ return ret;
+
+ if ( _cdio_is_it(INDEX_UDF) ) {
+ /* Detect UDF version
+ Test if we have a valid version of UDF the xbox can read natively */
+ if (_cdio_read_block(cdio, 35, start_session, 5, i_track) < 0)
+ return CDIO_FS_UNKNOWN;
+
+ iso_analysis->UDFVerMinor=(unsigned int)buffer[5][240];
+ iso_analysis->UDFVerMajor=(unsigned int)buffer[5][241];
+ /* Read disc label */
+ if (_cdio_read_block(cdio, 32, start_session, 5, i_track) < 0)
+ return CDIO_FS_UDF;
+
+ strncpy(iso_analysis->iso_label, buffer[5]+25, 33);
+ iso_analysis->iso_label[32] = '\0';
+ return CDIO_FS_UDF;
+ }
+
+ /* We have something that smells of a filesystem. */
+ if (_cdio_is_it(INDEX_CD_I) && _cdio_is_it(INDEX_CD_RTOS)
+ && !_cdio_is_it(INDEX_BRIDGE) && !_cdio_is_it(INDEX_XA)) {
+ return CDIO_FS_INTERACTIVE;
+ } else {
+ /* read sector 0 ONLY, when NO greenbook CD-I !!!! */
+
+ sector0_read_ok =
+ _cdio_read_block(cdio, 0, start_session, 1, i_track) == 0;
+
+ if (_cdio_is_it(INDEX_HS))
+ ret |= CDIO_FS_HIGH_SIERRA;
+ else if (_cdio_is_it(INDEX_ISOFS)) {
+ if (_cdio_is_it(INDEX_CD_RTOS) && _cdio_is_it(INDEX_BRIDGE))
+ ret = CDIO_FS_ISO_9660_INTERACTIVE;
+ else if (_cdio_is_hfs())
+ ret = CDIO_FS_ISO_HFS;
+ else
+ ret = CDIO_FS_ISO_9660;
+ iso_analysis->isofs_size = _cdio_get_iso9660_fs_sec_count();
+ strncpy(iso_analysis->iso_label, buffer[0]+40,33);
+ iso_analysis->iso_label[32] = '\0';
+
+ if ( _cdio_read_block(cdio, UDF_ANCHOR_SECTOR, start_session, 5,
+ i_track) < 0)
+ return ret;
+
+ /* Maybe there is an UDF anchor in IOS session
+ so its ISO/UDF session and we prefere UDF */
+ if ( _cdio_is_UDF() ) {
+ /* Detect UDF version.
+ Test if we have a valid version of UDF the xbox can read natively */
+ if ( _cdio_read_block(cdio, 35, start_session, 5, i_track) < 0)
+ return ret;
+
+ iso_analysis->UDFVerMinor=(unsigned int)buffer[5][240];
+ iso_analysis->UDFVerMajor=(unsigned int)buffer[5][241];
+#if 0
+ /* We are using ISO/UDF cd's as iso,
+ no need to get UDF disc label */
+ if (_cdio_read_block(cdio, 32, start_session, 5, i_track) < 0)
+ return ret;
+ stnrcpy(iso_analysis->iso_label, buffer[5]+25, 33);
+ iso_analysis->iso_label[32] = '\0';
+#endif
+ ret=CDIO_FS_ISO_UDF;
+ }
+
+#if 0
+ if (_cdio_is_rockridge())
+ ret |= CDIO_FS_ANAL_ROCKRIDGE;
+#endif
+
+ if (_cdio_read_block(cdio, BOOT_SECTOR, start_session, 3, i_track) < 0)
+ return ret;
+
+ if (_cdio_is_joliet()) {
+ iso_analysis->joliet_level = _cdio_get_joliet_level();
+ ret |= CDIO_FS_ANAL_JOLIET;
+ }
+ if (_cdio_is_it(INDEX_BOOTABLE))
+ ret |= CDIO_FS_ANAL_BOOTABLE;
+
+ if ( _cdio_is_it(INDEX_XA) && _cdio_is_it(INDEX_ISOFS)
+ && !(sector0_read_ok && _cdio_is_it(INDEX_PHOTO_CD)) ) {
+
+ if ( _cdio_read_block(cdio, VCD_INFO_SECTOR, start_session, 4,
+ i_track) < 0 )
+ return ret;
+
+ if (_cdio_is_it(INDEX_BRIDGE) && _cdio_is_it(INDEX_CD_RTOS)) {
+ if (_cdio_is_it(INDEX_VIDEO_CD)) ret |= CDIO_FS_ANAL_VIDEOCD;
+ else if (_cdio_is_it(INDEX_SVCD)) ret |= CDIO_FS_ANAL_SVCD;
+ } else if (_cdio_is_it(INDEX_SVCD)) ret |= CDIO_FS_ANAL_CVD;
+
+ }
+ }
+ else if (_cdio_is_hfs()) ret |= CDIO_FS_HFS;
+ else if (sector0_read_ok && _cdio_is_it(INDEX_EXT2)) ret |= CDIO_FS_EXT2;
+ else if (_cdio_is_3do()) ret |= CDIO_FS_3DO;
+ else {
+ if ( _cdio_read_block(cdio, UFS_SUPERBLOCK_SECTOR, start_session, 2,
+ i_track) < 0 )
+ return ret;
+
+ if (sector0_read_ok && _cdio_is_it(INDEX_UFS))
+ ret |= CDIO_FS_UFS;
+ else
+ ret |= CDIO_FS_UNKNOWN;
+ }
+ }
+
+ /* other checks */
+ if (_cdio_is_it(INDEX_XA)) ret |= CDIO_FS_ANAL_XA;
+ if (_cdio_is_it(INDEX_PHOTO_CD)) ret |= CDIO_FS_ANAL_PHOTO_CD;
+ if (_cdio_is_it(INDEX_CDTV)) ret |= CDIO_FS_ANAL_CDTV;
+ return ret;
+}
diff --git a/contrib/libcdio/cdio.c b/contrib/libcdio/cdio.c
new file mode 100644
index 000000000..fc18add91
--- /dev/null
+++ b/contrib/libcdio/cdio.c
@@ -0,0 +1,1125 @@
+/*
+ $Id: cdio.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <errno.h>
+#include <string.h>
+
+#include "cdio_assert.h"
+#include <cdio/cdio.h>
+#include <cdio/cd_types.h>
+#include <cdio/util.h>
+#include <cdio/logging.h>
+#include "cdio_private.h"
+
+static const char _rcsid[] = "$Id: cdio.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $";
+
+
+const char *track_format2str[6] =
+ {
+ "audio", "CD-i", "XA", "data", "PSX", "error"
+ };
+
+/* Must match discmode enumeration */
+const char *discmode2str[] = {
+ "CD-DA",
+ "CD-DATA Form 1",
+ "CD DATA Form 2",
+ "CD-ROM Mixed",
+ "DVD-ROM",
+ "DVD-RAM",
+ "DVD-R",
+ "DVD-RW",
+ "DVD+R",
+ "DVD+RW",
+ "Unknown/unclassified DVD",
+ "No information",
+ "Error in getting information"
+};
+
+
+/* The below array gives of the drivers that are currently available for
+ on a particular host. */
+
+CdIo_driver_t CdIo_driver[CDIO_MAX_DRIVER] = { {0} };
+
+/* The last valid entry of Cdio_driver.
+ -1 or (CDIO_DRIVER_UNINIT) means uninitialzed.
+ -2 means some sort of error.
+*/
+
+#define CDIO_DRIVER_UNINIT -1
+int CdIo_last_driver = CDIO_DRIVER_UNINIT;
+
+#ifdef HAVE_BSDI_CDROM
+const driver_id_t cdio_os_driver = DRIVER_BSDI;
+#elif HAVE_FREEBSD_CDROM
+const driver_id_t cdio_os_driver = DRIVER_FREEBSD;
+#elif HAVE_LINUX_CDROM
+const driver_id_t cdio_os_driver = DRIVER_LINUX;
+#elif HAVE_DARWIN_CDROM
+const driver_id_t cdio_os_driver = DRIVER_OSX;
+#elif HAVE_DARWIN_SOLARIS
+const driver_id_t cdio_os_driver = DRIVER_SOLARIS;
+#elif HAVE_DARWIN_WIN32
+const driver_id_t cdio_os_driver = DRIVER_WIN32;
+#else
+const driver_id_t cdio_os_driver = DRIVER_UNKNOWN;
+#endif
+
+static bool
+cdio_have_false(void)
+{
+ return false;
+}
+
+/* The below array gives all drivers that can possibly appear.
+ on a particular host. */
+
+CdIo_driver_t CdIo_all_drivers[CDIO_MAX_DRIVER+1] = {
+ {DRIVER_UNKNOWN,
+ 0,
+ "Unknown",
+ "No driver",
+ &cdio_have_false,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+ },
+
+ {DRIVER_BSDI,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "BSDI",
+ "BSDI ATAPI and SCSI driver",
+ &cdio_have_bsdi,
+ &cdio_open_bsdi,
+ &cdio_open_am_bsdi,
+ &cdio_get_default_device_bsdi,
+ &cdio_is_device_generic,
+ &cdio_get_devices_bsdi
+ },
+
+ {DRIVER_FREEBSD,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "FreeBSD",
+ "FreeBSD driver",
+ &cdio_have_freebsd,
+ &cdio_open_freebsd,
+ &cdio_open_am_freebsd,
+ &cdio_get_default_device_freebsd,
+ &cdio_is_device_generic,
+ NULL
+ },
+
+ {DRIVER_LINUX,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK,
+ "GNU/Linux",
+ "GNU/Linux ioctl and MMC driver",
+ &cdio_have_linux,
+ &cdio_open_linux,
+ &cdio_open_am_linux,
+ &cdio_get_default_device_linux,
+ &cdio_is_device_generic,
+ &cdio_get_devices_linux
+ },
+
+ {DRIVER_SOLARIS,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "Solaris",
+ "Solaris ATAPI and SCSI driver",
+ &cdio_have_solaris,
+ &cdio_open_solaris,
+ &cdio_open_am_solaris,
+ &cdio_get_default_device_solaris,
+ &cdio_is_device_generic,
+ &cdio_get_devices_solaris
+ },
+
+ {DRIVER_OSX,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "OS X",
+ "Apple Darwin OS X driver",
+ &cdio_have_osx,
+ &cdio_open_osx,
+ &cdio_open_am_osx,
+ &cdio_get_default_device_osx,
+ &cdio_is_device_generic,
+ &cdio_get_devices_osx
+ },
+
+ {DRIVER_WIN32,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "WIN32",
+ "MS Windows ASPI and ioctl driver",
+ &cdio_have_win32,
+ &cdio_open_win32,
+ &cdio_open_am_win32,
+ &cdio_get_default_device_win32,
+ &cdio_is_device_win32,
+ &cdio_get_devices_win32
+ },
+
+ {DRIVER_CDRDAO,
+ CDIO_SRC_IS_DISK_IMAGE_MASK,
+ "CDRDAO",
+ "cdrdao (TOC) disk image driver",
+ &cdio_have_cdrdao,
+ &cdio_open_cdrdao,
+ &cdio_open_am_cdrdao,
+ &cdio_get_default_device_cdrdao,
+ NULL,
+ &cdio_get_devices_cdrdao
+ },
+
+ {DRIVER_BINCUE,
+ CDIO_SRC_IS_DISK_IMAGE_MASK,
+ "BIN/CUE",
+ "bin/cuesheet disk image driver",
+ &cdio_have_bincue,
+ &cdio_open_bincue,
+ &cdio_open_am_bincue,
+ &cdio_get_default_device_bincue,
+ NULL,
+ &cdio_get_devices_bincue
+ },
+
+ {DRIVER_NRG,
+ CDIO_SRC_IS_DISK_IMAGE_MASK,
+ "NRG",
+ "Nero NRG disk image driver",
+ &cdio_have_nrg,
+ &cdio_open_nrg,
+ &cdio_open_am_nrg,
+ &cdio_get_default_device_nrg,
+ NULL,
+ &cdio_get_devices_nrg
+ }
+
+};
+
+static CdIo *
+scan_for_driver(driver_id_t start, driver_id_t end,
+ const char *psz_source, const char *access_mode)
+{
+ driver_id_t driver_id;
+
+ for (driver_id=start; driver_id<=end; driver_id++) {
+ if ((*CdIo_all_drivers[driver_id].have_driver)()) {
+ CdIo *ret=
+ (*CdIo_all_drivers[driver_id].driver_open_am)(psz_source, access_mode);
+ if (ret != NULL) {
+ ret->driver_id = driver_id;
+ return ret;
+ }
+ }
+ }
+ return NULL;
+}
+
+const char *
+cdio_driver_describe(driver_id_t driver_id)
+{
+ return CdIo_all_drivers[driver_id].describe;
+}
+
+/*!
+ Eject media in CD drive if there is a routine to do so.
+ Return 0 if success and 1 for failure, and 2 if no routine.
+ If the CD is ejected *obj is freed and obj set to NULL.
+ */
+int
+cdio_eject_media (CdIo **obj)
+{
+
+ if ((obj == NULL) || (*obj == NULL)) return 1;
+
+ if ((*obj)->op.eject_media) {
+ int ret = (*obj)->op.eject_media ((*obj)->env);
+ if (0 == ret) {
+ cdio_destroy(*obj);
+ *obj = NULL;
+ }
+ return ret;
+ } else {
+ cdio_destroy(*obj);
+ *obj = NULL;
+ return 2;
+ }
+}
+
+/*!
+ Free device list returned by cdio_get_devices or
+ cdio_get_devices_with_cap.
+*/
+void cdio_free_device_list (char * device_list[])
+{
+ if (NULL == device_list) return;
+ for ( ; *device_list != NULL ; device_list++ )
+ free(*device_list);
+}
+
+
+/*!
+ Return the value associatied with key. NULL is returned if obj is NULL
+ or "key" does not exist.
+ */
+const char *
+cdio_get_arg (const CdIo *obj, const char key[])
+{
+ if (obj == NULL) return NULL;
+
+ if (obj->op.get_arg) {
+ return obj->op.get_arg (obj->env, key);
+ } else {
+ return NULL;
+ }
+}
+
+/*!
+ Get cdtext information for a CdIo object .
+
+ @param obj the CD object that may contain CD-TEXT information.
+ @return the CD-TEXT object or NULL if obj is NULL
+ or CD-TEXT information does not exist.
+*/
+const cdtext_t *
+cdio_get_cdtext (CdIo *obj, track_t i_track)
+{
+ if (obj == NULL) return NULL;
+
+ if (obj->op.get_cdtext) {
+ return obj->op.get_cdtext (obj->env, i_track);
+ } else {
+ return NULL;
+ }
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then find a suitable one and return the default device for that.
+
+ NULL is returned if we couldn't get a default device.
+ */
+char *
+cdio_get_default_device (const CdIo *obj)
+{
+ if (obj == NULL) {
+ driver_id_t driver_id;
+ /* Scan for driver */
+ for (driver_id=DRIVER_UNKNOWN; driver_id<=CDIO_MAX_DRIVER; driver_id++) {
+ if ( (*CdIo_all_drivers[driver_id].have_driver)() &&
+ *CdIo_all_drivers[driver_id].get_default_device ) {
+ return (*CdIo_all_drivers[driver_id].get_default_device)();
+ }
+ }
+ return NULL;
+ }
+
+ if (obj->op.get_default_device) {
+ return obj->op.get_default_device ();
+ } else {
+ return NULL;
+ }
+}
+
+/*!Return an array of device names. If you want a specific
+ devices, dor a driver give that device, if you want hardware
+ devices, give DRIVER_DEVICE and if you want all possible devices,
+ image drivers and hardware drivers give DRIVER_UNKNOWN.
+
+ NULL is returned if we couldn't return a list of devices.
+*/
+char **
+cdio_get_devices (driver_id_t driver_id)
+{
+ /* Probably could get away with &driver_id below. */
+ driver_id_t driver_id_temp = driver_id;
+ return cdio_get_devices_ret (&driver_id_temp);
+}
+
+char **
+cdio_get_devices_ret (/*in/out*/ driver_id_t *p_driver_id)
+{
+ CdIo *p_cdio;
+
+ switch (*p_driver_id) {
+ /* FIXME: spit out unknown to give image drivers as well. */
+ case DRIVER_UNKNOWN:
+ case DRIVER_DEVICE:
+ p_cdio = scan_for_driver(DRIVER_UNKNOWN, CDIO_MAX_DRIVER, NULL, NULL);
+ *p_driver_id = cdio_get_driver_id(p_cdio);
+ break;
+ default:
+ return (*CdIo_all_drivers[*p_driver_id].get_devices)();
+ }
+
+ if (p_cdio == NULL) return NULL;
+ if (p_cdio->op.get_devices) {
+ char **devices = p_cdio->op.get_devices ();
+ cdio_destroy(p_cdio);
+ return devices;
+ } else {
+ return NULL;
+ }
+}
+
+/*!
+ Return an array of device names in search_devices that have at
+ least the capabilities listed by cap. If search_devices is NULL,
+ then we'll search all possible CD drives.
+
+ If "any" is set false then every capability listed in the extended
+ portion of capabilities (i.e. not the basic filesystem) must be
+ satisified. If "any" is set true, then if any of the capabilities
+ matches, we call that a success.
+
+ To find a CD-drive of any type, use the mask CDIO_FS_MATCH_ALL.
+
+ NULL is returned if we couldn't get a default device.
+ It is also possible to return a non NULL but after dereferencing the
+ the value is NULL. This also means nothing was found.
+*/
+char **
+cdio_get_devices_with_cap (/*out*/ char* search_devices[],
+ cdio_fs_anal_t capabilities, bool any)
+{
+ driver_id_t p_driver_id;
+ return cdio_get_devices_with_cap_ret (search_devices, capabilities, any,
+ &p_driver_id);
+}
+
+char **
+cdio_get_devices_with_cap_ret (/*out*/ char* search_devices[],
+ cdio_fs_anal_t capabilities, bool any,
+ /*out*/ driver_id_t *p_driver_id)
+{
+ char **drives=search_devices;
+ char **drives_ret=NULL;
+ unsigned int i_drives=0;
+
+ *p_driver_id = DRIVER_DEVICE;
+
+ if (NULL == drives) drives=cdio_get_devices_ret(p_driver_id);
+ if (NULL == drives) return NULL;
+
+ if (capabilities == CDIO_FS_MATCH_ALL) {
+ /* Duplicate drives into drives_ret. */
+ char **d = drives;
+
+ for( ; *d != NULL; d++ ) {
+ cdio_add_device_list(&drives_ret, *d, &i_drives);
+ }
+ } else {
+ cdio_fs_anal_t got_fs=0;
+ cdio_fs_anal_t need_fs = CDIO_FSTYPE(capabilities);
+ cdio_fs_anal_t need_fs_ext;
+ char **d = drives;
+ need_fs_ext = capabilities & ~CDIO_FS_MASK;
+
+ for( ; *d != NULL; d++ ) {
+ CdIo *cdio = cdio_open(*d, *p_driver_id);
+
+ if (NULL != cdio) {
+ track_t first_track = cdio_get_first_track_num(cdio);
+ cdio_iso_analysis_t cdio_iso_analysis;
+ got_fs = cdio_guess_cd_type(cdio, 0, first_track,
+ &cdio_iso_analysis);
+ /* Match on fs and add */
+ if ( (CDIO_FS_UNKNOWN == need_fs || CDIO_FSTYPE(got_fs) == need_fs) )
+ {
+ bool doit = any
+ ? (got_fs & need_fs_ext) != 0
+ : (got_fs | ~need_fs_ext) == -1;
+ if (doit)
+ cdio_add_device_list(&drives_ret, *d, &i_drives);
+ }
+
+ cdio_destroy(cdio);
+ }
+ }
+ }
+ cdio_add_device_list(&drives_ret, NULL, &i_drives);
+ cdio_free_device_list(drives);
+ free(drives);
+ return drives_ret;
+}
+
+/*!
+ Get medium associated with cd_obj.
+*/
+discmode_t
+cdio_get_discmode (CdIo *cd_obj)
+{
+ if (cd_obj == NULL) return CDIO_DISC_MODE_ERROR;
+
+ if (cd_obj->op.get_discmode) {
+ return cd_obj->op.get_discmode (cd_obj->env);
+ } else {
+ return CDIO_DISC_MODE_NO_INFO;
+ }
+}
+
+/*!
+ Return the the kind of drive capabilities of device.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+void
+cdio_get_drive_cap (const CdIo *p_cdio,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap)
+{
+ /* This seems like a safe bet. */
+ *p_read_cap = CDIO_DRIVE_CAP_UNKNOWN;
+ *p_write_cap = CDIO_DRIVE_CAP_UNKNOWN;
+ *p_misc_cap = CDIO_DRIVE_CAP_UNKNOWN;
+
+ if (p_cdio && p_cdio->op.get_drive_cap) {
+ p_cdio->op.get_drive_cap(p_cdio->env, p_read_cap, p_write_cap, p_misc_cap);
+ }
+}
+
+/*!
+ Return the the kind of drive capabilities of device.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+void
+cdio_get_drive_cap_dev (const char *device,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap)
+{
+ /* This seems like a safe bet. */
+ CdIo *cdio=scan_for_driver(CDIO_MIN_DRIVER, CDIO_MAX_DRIVER,
+ device, NULL);
+ if (cdio) {
+ cdio_get_drive_cap(cdio, p_read_cap, p_write_cap, p_misc_cap);
+ cdio_destroy(cdio);
+ } else {
+ *p_read_cap = CDIO_DRIVE_CAP_UNKNOWN;
+ *p_write_cap = CDIO_DRIVE_CAP_UNKNOWN;
+ *p_misc_cap = CDIO_DRIVE_CAP_UNKNOWN;
+ }
+}
+
+
+/*!
+ Return a string containing the name of the driver in use.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then return NULL.
+*/
+const char *
+cdio_get_driver_name (const CdIo *cdio)
+{
+ if (NULL==cdio) return NULL;
+ return CdIo_all_drivers[cdio->driver_id].name;
+}
+
+ /*!
+ Return the driver id.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then return DRIVER_UNKNOWN.
+ */
+driver_id_t
+cdio_get_driver_id (const CdIo *cdio)
+{
+ if (NULL==cdio) return DRIVER_UNKNOWN;
+ return cdio->driver_id;
+}
+
+
+/*!
+ Return the number of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+track_t
+cdio_get_first_track_num (const CdIo *p_cdio)
+{
+ if (NULL == p_cdio) return CDIO_INVALID_TRACK;
+
+ if (p_cdio->op.get_first_track_num) {
+ return p_cdio->op.get_first_track_num (p_cdio->env);
+ } else {
+ return CDIO_INVALID_TRACK;
+ }
+}
+
+/*!
+ Return a string containing the name of the driver in use.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then return NULL.
+*/
+bool
+cdio_get_hwinfo (const CdIo *p_cdio, cdio_hwinfo_t *hw_info)
+{
+ if (!p_cdio) return false;
+ if (p_cdio->op.get_hwinfo) {
+ return p_cdio->op.get_hwinfo (p_cdio, hw_info);
+ } else {
+ /* Perhaps driver forgot to initialize. We are no worse off Using
+ scsi_mmc than returning false here. */
+ return scsi_mmc_get_hwinfo(p_cdio, hw_info);
+ }
+}
+
+/*!
+ Return a string containing the name of the driver in use.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then return NULL.
+*/
+char *
+cdio_get_mcn (const CdIo *p_cdio)
+{
+ if (p_cdio->op.get_mcn) {
+ return p_cdio->op.get_mcn (p_cdio->env);
+ } else {
+ return NULL;
+ }
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+track_t
+cdio_get_num_tracks (const CdIo *p_cdio)
+{
+ if (p_cdio == NULL) return CDIO_INVALID_TRACK;
+
+ if (p_cdio->op.get_num_tracks) {
+ return p_cdio->op.get_num_tracks (p_cdio->env);
+ } else {
+ return CDIO_INVALID_TRACK;
+ }
+}
+
+/*!
+ Get format of track.
+*/
+track_format_t
+cdio_get_track_format(const CdIo *p_cdio, track_t i_track)
+{
+ cdio_assert (p_cdio != NULL);
+
+ if (p_cdio->op.get_track_format) {
+ return p_cdio->op.get_track_format (p_cdio->env, i_track);
+ } else {
+ return TRACK_FORMAT_ERROR;
+ }
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+bool
+cdio_get_track_green(const CdIo *cdio, track_t track_num)
+{
+ cdio_assert (cdio != NULL);
+
+ if (cdio->op.get_track_green) {
+ return cdio->op.get_track_green (cdio->env, track_num);
+ } else {
+ return false;
+ }
+}
+
+/*!
+ Return the starting LBA for track number
+ track_num in cdio. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ CDIO_INVALID_LBA is returned on error.
+*/
+lba_t
+cdio_get_track_lba(const CdIo *cdio, track_t track_num)
+{
+ if (cdio == NULL) return CDIO_INVALID_LBA;
+
+ if (cdio->op.get_track_lba) {
+ return cdio->op.get_track_lba (cdio->env, track_num);
+ } else {
+ msf_t msf;
+ if (cdio->op.get_track_msf)
+ if (cdio_get_track_msf(cdio, track_num, &msf))
+ return cdio_msf_to_lba(&msf);
+ return CDIO_INVALID_LBA;
+ }
+}
+
+/*!
+ Return the starting LSN for track number
+ track_num in cdio. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ CDIO_INVALID_LBA is returned on error.
+*/
+lsn_t
+cdio_get_track_lsn(const CdIo *cdio, track_t track_num)
+{
+ if (cdio == NULL) return CDIO_INVALID_LBA;
+
+ if (cdio->op.get_track_lba) {
+ return cdio_lba_to_lsn(cdio->op.get_track_lba (cdio->env, track_num));
+ } else {
+ msf_t msf;
+ if (cdio_get_track_msf(cdio, track_num, &msf))
+ return cdio_msf_to_lsn(&msf);
+ return CDIO_INVALID_LSN;
+ }
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in cdio. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+bool
+cdio_get_track_msf(const CdIo *cdio, track_t track_num, /*out*/ msf_t *msf)
+{
+ cdio_assert (cdio != NULL);
+
+ if (cdio->op.get_track_msf) {
+ return cdio->op.get_track_msf (cdio->env, track_num, msf);
+ } else if (cdio->op.get_track_lba) {
+ lba_t lba = cdio->op.get_track_lba (cdio->env, track_num);
+ if (lba == CDIO_INVALID_LBA) return false;
+ cdio_lba_to_msf(lba, msf);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+/*!
+ Return the number of sectors between this track an the next. This
+ includes any pregap sectors before the start of the next track.
+ Tracks start at 1.
+ 0 is returned if there is an error.
+*/
+unsigned int
+cdio_get_track_sec_count(const CdIo *cdio, track_t track_num)
+{
+ track_t num_tracks = cdio_get_num_tracks(cdio);
+
+ if (track_num >=1 && track_num <= num_tracks)
+ return ( cdio_get_track_lba(cdio, track_num+1)
+ - cdio_get_track_lba(cdio, track_num) );
+ return 0;
+}
+
+bool
+cdio_have_driver(driver_id_t driver_id)
+{
+ return (*CdIo_all_drivers[driver_id].have_driver)();
+}
+
+/*!
+ Return the Joliet level recognized for p_cdio.
+*/
+uint8_t
+cdio_get_joliet_level(const CdIo *p_cdio)
+{
+ if (!p_cdio) return 0;
+ {
+ const generic_img_private_t *p_env
+ = (generic_img_private_t *) (p_cdio->env);
+ return p_env->i_joliet_level;
+ }
+}
+
+bool
+cdio_is_device(const char *psz_source, driver_id_t driver_id)
+{
+ if (CdIo_all_drivers[driver_id].is_device == NULL) return false;
+ return (*CdIo_all_drivers[driver_id].is_device)(psz_source);
+}
+
+
+/*!
+ Initialize CD Reading and control routines. Should be called first.
+ May be implicitly called by other routines if not called first.
+*/
+bool
+cdio_init(void)
+{
+
+ CdIo_driver_t *all_dp;
+ CdIo_driver_t *dp = CdIo_driver;
+ driver_id_t driver_id;
+
+ if (CdIo_last_driver != CDIO_DRIVER_UNINIT) {
+ cdio_warn ("Init routine called more than once.");
+ return false;
+ }
+
+ for (driver_id=DRIVER_UNKNOWN; driver_id<=CDIO_MAX_DRIVER; driver_id++) {
+ all_dp = &CdIo_all_drivers[driver_id];
+ if ((*CdIo_all_drivers[driver_id].have_driver)()) {
+ *dp++ = *all_dp;
+ CdIo_last_driver++;
+ }
+ }
+
+ return true;
+}
+
+CdIo *
+cdio_new (generic_img_private_t *p_env, cdio_funcs *p_funcs)
+{
+ CdIo *p_new_cdio = _cdio_malloc (sizeof (CdIo));
+
+ if (NULL == p_new_cdio) return NULL;
+
+ p_new_cdio->env = p_env; /* This is the private "environment" that
+ driver-dependent routines use. */
+ p_new_cdio->op = *p_funcs;
+ p_env->cdio = p_new_cdio; /* A way for the driver-dependent routines
+ to access the higher-level general cdio
+ object. */
+ return p_new_cdio;
+}
+
+/*!
+ Free any resources associated with cdio.
+*/
+void
+cdio_destroy (CdIo *cdio)
+{
+ CdIo_last_driver = CDIO_DRIVER_UNINIT;
+ if (cdio == NULL) return;
+
+ if (cdio->op.free != NULL)
+ cdio->op.free (cdio->env);
+ free (cdio);
+}
+
+/*!
+ lseek - reposition read/write file offset
+ Returns (off_t) -1 on error.
+ Similar to (if not the same as) libc's lseek()
+*/
+off_t
+cdio_lseek (const CdIo *cdio, off_t offset, int whence)
+{
+ if (cdio == NULL) return -1;
+
+ if (cdio->op.lseek)
+ return cdio->op.lseek (cdio->env, offset, whence);
+ return -1;
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Similar to (if not the same as) libc's read()
+*/
+ssize_t
+cdio_read (const CdIo *p_cdio, void *buf, size_t size)
+{
+ if (p_cdio == NULL) return -1;
+
+ if (p_cdio->op.read)
+ return p_cdio->op.read (p_cdio->env, buf, size);
+ return -1;
+}
+
+/*!
+ Reads an audio sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+*/
+int
+cdio_read_audio_sector (const CdIo *p_cdio, void *buf, lsn_t lsn)
+{
+
+ if (NULL == p_cdio || NULL == buf || CDIO_INVALID_LSN == lsn )
+ return 0;
+
+ if (p_cdio->op.read_audio_sectors != NULL)
+ return p_cdio->op.read_audio_sectors (p_cdio->env, buf, lsn, 1);
+ return -1;
+}
+
+/*!
+ Reads audio sectors from cd device into data starting
+ from lsn. Returns 0 if no error.
+*/
+int
+cdio_read_audio_sectors (const CdIo *p_cdio, void *buf, lsn_t lsn,
+ unsigned int nblocks)
+{
+ if ( NULL == p_cdio || NULL == buf || CDIO_INVALID_LSN == lsn )
+ return 0;
+
+ if (p_cdio->op.read_audio_sectors != NULL)
+ return p_cdio->op.read_audio_sectors (p_cdio->env, buf, lsn, nblocks);
+ return -1;
+}
+
+#ifndef SEEK_SET
+#define SEEK_SET 0
+#endif
+
+/*!
+ Reads a single mode1 form1 or form2 sector from cd device
+ into data starting from lsn. Returns 0 if no error.
+ */
+int
+cdio_read_mode1_sector (const CdIo *p_cdio, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ uint32_t size = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE ;
+
+ if (NULL == p_cdio || NULL == data || CDIO_INVALID_LSN == lsn )
+ return 0;
+
+ if (p_cdio->op.read_mode1_sector) {
+ return p_cdio->op.read_mode1_sector(p_cdio->env, data, lsn, b_form2);
+ } else if (p_cdio->op.lseek && p_cdio->op.read) {
+ char buf[CDIO_CD_FRAMESIZE] = { 0, };
+ if (0 > cdio_lseek(p_cdio, CDIO_CD_FRAMESIZE*lsn, SEEK_SET))
+ return -1;
+ if (0 > cdio_read(p_cdio, buf, CDIO_CD_FRAMESIZE))
+ return -1;
+ memcpy (data, buf, size);
+ return 0;
+ }
+
+ return 1;
+
+}
+
+int
+cdio_read_mode1_sectors (const CdIo *cdio, void *buf, lsn_t lsn,
+ bool b_form2, unsigned int num_sectors)
+{
+
+ if (NULL == cdio || NULL == buf || CDIO_INVALID_LSN == lsn )
+ return 0;
+
+ cdio_assert (cdio->op.read_mode1_sectors != NULL);
+
+ return cdio->op.read_mode1_sectors (cdio->env, buf, lsn, b_form2,
+ num_sectors);
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+int
+cdio_read_mode2_sector (const CdIo *cdio, void *buf, lsn_t lsn,
+ bool b_form2)
+{
+ if (NULL == cdio || NULL == buf || CDIO_INVALID_LSN == lsn )
+ return 0;
+
+ cdio_assert (cdio->op.read_mode2_sector != NULL
+ || cdio->op.read_mode2_sectors != NULL);
+
+ if (cdio->op.read_mode2_sector)
+ return cdio->op.read_mode2_sector (cdio->env, buf, lsn, b_form2);
+
+ /* fallback */
+ if (cdio->op.read_mode2_sectors != NULL)
+ return cdio_read_mode2_sectors (cdio, buf, lsn, b_form2, 1);
+ return 1;
+}
+
+int
+cdio_read_mode2_sectors (const CdIo *cdio, void *buf, lsn_t lsn,
+ bool b_form2, unsigned int num_sectors)
+{
+
+ if (NULL == cdio || NULL == buf || CDIO_INVALID_LSN == lsn )
+ return 0;
+
+ cdio_assert (cdio->op.read_mode2_sectors != NULL);
+
+ return cdio->op.read_mode2_sectors (cdio->env, buf, lsn,
+ b_form2, num_sectors);
+}
+
+uint32_t
+cdio_stat_size (const CdIo *cdio)
+{
+ cdio_assert (cdio != NULL);
+
+ return cdio->op.stat_size (cdio->env);
+}
+
+/*!
+ Set the arg "key" with "value" in the source device.
+*/
+int
+cdio_set_arg (CdIo *cdio, const char key[], const char value[])
+{
+ cdio_assert (cdio != NULL);
+ cdio_assert (cdio->op.set_arg != NULL);
+ cdio_assert (key != NULL);
+
+ return cdio->op.set_arg (cdio->env, key, value);
+}
+
+/*! Sets up to read from place specified by source_name and
+ driver_id. This should be called before using any other routine,
+ except cdio_init. This will call cdio_init, if that hasn't been
+ done previously.
+
+ NULL is returned on error.
+*/
+CdIo *
+cdio_open (const char *orig_source_name, driver_id_t driver_id)
+{
+ return cdio_open_am(orig_source_name, driver_id, NULL);
+}
+
+/*! Sets up to read from place specified by source_name and
+ driver_id. This should be called before using any other routine,
+ except cdio_init. This will call cdio_init, if that hasn't been
+ done previously.
+
+ NULL is returned on error.
+*/
+CdIo *
+cdio_open_am (const char *psz_orig_source, driver_id_t driver_id,
+ const char *psz_access_mode)
+{
+ char *psz_source;
+
+ if (CdIo_last_driver == -1) cdio_init();
+
+ if (NULL == psz_orig_source || strlen(psz_orig_source)==0)
+ psz_source = cdio_get_default_device(NULL);
+ else
+ psz_source = strdup(psz_orig_source);
+
+ switch (driver_id) {
+ case DRIVER_UNKNOWN:
+ {
+ CdIo *cdio=scan_for_driver(CDIO_MIN_DRIVER, CDIO_MAX_DRIVER,
+ psz_source, psz_access_mode);
+ free(psz_source);
+ return cdio;
+ }
+ case DRIVER_DEVICE:
+ {
+ /* Scan for a driver. */
+ CdIo *ret = cdio_open_am_cd(psz_source, psz_access_mode);
+ free(psz_source);
+ return ret;
+ }
+ break;
+ case DRIVER_BSDI:
+ case DRIVER_FREEBSD:
+ case DRIVER_LINUX:
+ case DRIVER_SOLARIS:
+ case DRIVER_WIN32:
+ case DRIVER_OSX:
+ case DRIVER_NRG:
+ case DRIVER_BINCUE:
+ case DRIVER_CDRDAO:
+ if ((*CdIo_all_drivers[driver_id].have_driver)()) {
+ CdIo *ret =
+ (*CdIo_all_drivers[driver_id].driver_open_am)(psz_source,
+ psz_access_mode);
+ if (ret) ret->driver_id = driver_id;
+ free(psz_source);
+ return ret;
+ }
+ }
+
+ free(psz_source);
+ return NULL;
+}
+
+
+/*!
+ Set up CD-ROM for reading. The device_name is
+ the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no driver for a some sort of hardware CD-ROM.
+*/
+CdIo *
+cdio_open_cd (const char *psz_source)
+{
+ return cdio_open_am_cd(psz_source, NULL);
+}
+
+/*!
+ Set up CD-ROM for reading. The device_name is
+ the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no driver for a some sort of hardware CD-ROM.
+*/
+/* In the future we'll have more complicated code to allow selection
+ of an I/O routine as well as code to find an appropriate default
+ routine among the "registered" routines. Possibly classes too
+ disk-based, SCSI-based, native-based, vendor (e.g. Sony, or
+ Plextor) based
+
+ For now though, we'll start more simply...
+*/
+CdIo *
+cdio_open_am_cd (const char *psz_source, const char *psz_access_mode)
+{
+ if (CdIo_last_driver == -1) cdio_init();
+
+ /* Scan for a driver. */
+ return scan_for_driver(CDIO_MIN_DEVICE_DRIVER, CDIO_MAX_DEVICE_DRIVER,
+ psz_source, psz_access_mode);
+}
+
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/Makefile.am b/contrib/libcdio/cdio/Makefile.am
new file mode 100644
index 000000000..0910d60e5
--- /dev/null
+++ b/contrib/libcdio/cdio/Makefile.am
@@ -0,0 +1,19 @@
+include $(top_srcdir)/misc/Makefile.common
+
+noinst_HEADERS = \
+ bytesex.h \
+ bytesex_asm.h \
+ cdio.h \
+ cdtext.h \
+ cdtext.h \
+ cd_types.h \
+ ds.h \
+ dvd.h \
+ iso9660.h \
+ logging.h \
+ sector.h \
+ scsi_mmc.h \
+ types.h \
+ util.h \
+ version.h \
+ xa.h
diff --git a/contrib/libcdio/cdio/bytesex.h b/contrib/libcdio/cdio/bytesex.h
new file mode 100644
index 000000000..c40e44729
--- /dev/null
+++ b/contrib/libcdio/cdio/bytesex.h
@@ -0,0 +1,196 @@
+/*
+ $Id: bytesex.h,v 1.1 2005/01/02 00:51:38 rockyb Exp $
+
+ Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_BYTESEX_H__
+#define __CDIO_BYTESEX_H__
+
+#include <cdio/types.h>
+#include <cdio/bytesex_asm.h>
+#include <cdio/logging.h>
+
+/* generic byteswap routines */
+
+#define UINT16_SWAP_LE_BE_C(val) ((uint16_t) ( \
+ (((uint16_t) (val) & (uint16_t) 0x00ffU) << 8) | \
+ (((uint16_t) (val) & (uint16_t) 0xff00U) >> 8)))
+
+#define UINT32_SWAP_LE_BE_C(val) ((uint32_t) ( \
+ (((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
+ (((uint32_t) (val) & (uint32_t) 0x0000ff00U) << 8) | \
+ (((uint32_t) (val) & (uint32_t) 0x00ff0000U) >> 8) | \
+ (((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24)))
+
+#define UINT64_SWAP_LE_BE_C(val) ((uint64_t) ( \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000ff000000)) << 8) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000ff00000000)) >> 8) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56)))
+
+#ifndef UINT16_SWAP_LE_BE
+# define UINT16_SWAP_LE_BE UINT16_SWAP_LE_BE_C
+#endif
+
+#ifndef UINT32_SWAP_LE_BE
+# define UINT32_SWAP_LE_BE UINT32_SWAP_LE_BE_C
+#endif
+
+#ifndef UINT64_SWAP_LE_BE
+# define UINT64_SWAP_LE_BE UINT64_SWAP_LE_BE_C
+#endif
+
+inline static
+uint16_t uint16_swap_le_be (const uint16_t val)
+{
+ return UINT16_SWAP_LE_BE (val);
+}
+
+inline static
+uint32_t uint32_swap_le_be (const uint32_t val)
+{
+ return UINT32_SWAP_LE_BE (val);
+}
+
+inline static
+uint64_t uint64_swap_le_be (const uint64_t val)
+{
+ return UINT64_SWAP_LE_BE (val);
+}
+
+# define UINT8_TO_BE(val) ((uint8_t) (val))
+# define UINT8_TO_LE(val) ((uint8_t) (val))
+#ifdef WORDS_BIGENDIAN
+# define UINT16_TO_BE(val) ((uint16_t) (val))
+# define UINT16_TO_LE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
+
+# define UINT32_TO_BE(val) ((uint32_t) (val))
+# define UINT32_TO_LE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
+
+# define UINT64_TO_BE(val) ((uint64_t) (val))
+# define UINT64_TO_LE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
+#else
+# define UINT16_TO_BE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
+# define UINT16_TO_LE(val) ((uint16_t) (val))
+
+# define UINT32_TO_BE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
+# define UINT32_TO_LE(val) ((uint32_t) (val))
+
+# define UINT64_TO_BE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
+# define UINT64_TO_LE(val) ((uint64_t) (val))
+#endif
+
+/* symmetric conversions */
+#define UINT8_FROM_BE(val) (UINT8_TO_BE (val))
+#define UINT8_FROM_LE(val) (UINT8_TO_LE (val))
+#define UINT16_FROM_BE(val) (UINT16_TO_BE (val))
+#define UINT16_FROM_LE(val) (UINT16_TO_LE (val))
+#define UINT32_FROM_BE(val) (UINT32_TO_BE (val))
+#define UINT32_FROM_LE(val) (UINT32_TO_LE (val))
+#define UINT64_FROM_BE(val) (UINT64_TO_BE (val))
+#define UINT64_FROM_LE(val) (UINT64_TO_LE (val))
+
+/* converter function template */
+#define CVT_TO_FUNC(bits) \
+ static inline uint ## bits ## _t \
+ uint ## bits ## _to_be (uint ## bits ## _t val) \
+ { return UINT ## bits ## _TO_BE (val); } \
+ static inline uint ## bits ## _t \
+ uint ## bits ## _to_le (uint ## bits ## _t val) \
+ { return UINT ## bits ## _TO_LE (val); } \
+
+CVT_TO_FUNC(8)
+CVT_TO_FUNC(16)
+CVT_TO_FUNC(32)
+CVT_TO_FUNC(64)
+
+#undef CVT_TO_FUNC
+
+#define uint8_from_be(val) (uint8_to_be (val))
+#define uint8_from_le(val) (uint8_to_le (val))
+#define uint16_from_be(val) (uint16_to_be (val))
+#define uint16_from_le(val) (uint16_to_le (val))
+#define uint32_from_be(val) (uint32_to_be (val))
+#define uint32_from_le(val) (uint32_to_le (val))
+#define uint64_from_be(val) (uint64_to_be (val))
+#define uint64_from_le(val) (uint64_to_le (val))
+
+/* ISO9660 related stuff */
+
+#define to_711(i) uint8_to_le(i)
+#define from_711(i) uint8_from_le(i)
+
+#define to_721(i) uint16_to_le(i)
+#define from_721(i) uint16_from_le(i)
+
+#define to_721(i) uint16_to_le(i)
+#define from_721(i) uint16_from_le(i)
+
+#define to_722(i) uint16_to_be(i)
+#define from_722(i) uint16_from_be(i)
+
+static inline uint32_t
+to_723(uint16_t i)
+{
+ return uint32_swap_le_be(i) | i;
+}
+
+static inline uint16_t
+from_723 (uint32_t p)
+{
+ if (uint32_swap_le_be (p) != p)
+ cdio_warn ("from_723: broken byte order");
+
+ return (0xFFFF & p);
+}
+
+#define to_731(i) uint32_to_le(i)
+#define from_731(i) uint32_from_le(i)
+
+#define to_732(i) uint32_to_be(i)
+#define from_732(i) uint32_from_be(i)
+
+static inline uint64_t
+to_733(uint32_t i)
+{
+ return uint64_swap_le_be(i) | i;
+}
+
+static inline uint32_t
+from_733 (uint64_t p)
+{
+ if (uint64_swap_le_be (p) != p)
+ cdio_warn ("from_733: broken byte order");
+
+ return (UINT32_C(0xFFFFFFFF) & p);
+}
+
+#endif /* __CDIO_BYTESEX_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/bytesex_asm.h b/contrib/libcdio/cdio/bytesex_asm.h
new file mode 100644
index 000000000..4291563ec
--- /dev/null
+++ b/contrib/libcdio/cdio/bytesex_asm.h
@@ -0,0 +1,123 @@
+/*
+ $Id: bytesex_asm.h,v 1.1 2005/01/02 00:51:38 rockyb Exp $
+
+ Copyright (C) 2001 Sven Ottemann <ac-logic@freenet.de>
+ 2001, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_BYTESEX_ASM_H__
+#define __CDIO_BYTESEX_ASM_H__
+#if !defined(DISABLE_ASM_OPTIMIZE)
+
+#include <cdio/types.h>
+
+#if defined(__powerpc__) && defined(__GNUC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(const uint32_t a)
+{
+ uint32_t b;
+
+ __asm__ ("lwbrx %0,0,%1"
+ :"=r"(b)
+ :"r"(&a), "m"(a));
+
+ return b;
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(const uint16_t a)
+{
+ uint32_t b;
+
+ __asm__ ("lhbrx %0,0,%1"
+ :"=r"(b)
+ :"r"(&a), "m"(a));
+
+ return b;
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#elif defined(__mc68000__) && defined(__STORMGCC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(uint32_t a __asm__("d0"))
+{
+ /* __asm__("rolw #8,%0; swap %0; rolw #8,%0" : "=d" (val) : "0" (val)); */
+
+ __asm__("move.l %1,d0;rol.w #8,d0;swap d0;rol.w #8,d0;move.l d0,%0"
+ :"=r"(a)
+ :"r"(a));
+
+ return(a);
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(uint16_t a __asm__("d0"))
+{
+ __asm__("move.l %1,d0;rol.w #8,d0;move.l d0,%0"
+ :"=r"(a)
+ :"r"(a));
+
+ return(a);
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#elif 0 && defined(__i386__) && defined(__GNUC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(uint32_t a)
+{
+ __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
+ "rorl $16,%0\n\t" /* swap words */
+ "xchgb %b0,%h0" /* swap higher bytes */
+ :"=q" (a)
+ : "0" (a));
+
+ return(a);
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(uint16_t a)
+{
+ __asm__("xchgb %b0,%h0" /* swap bytes */
+ : "=q" (a)
+ : "0" (a));
+
+ return(a);
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#endif
+
+#endif /* !defined(DISABLE_ASM_OPTIMIZE) */
+#endif /* __CDIO_BYTESEX_ASM_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/cd_types.h b/contrib/libcdio/cdio/cd_types.h
new file mode 100644
index 000000000..9f4a73f67
--- /dev/null
+++ b/contrib/libcdio/cdio/cd_types.h
@@ -0,0 +1,155 @@
+/*
+ $Id: cd_types.h,v 1.3 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+ Copyright (C) 1996,1997,1998 Gerd Knorr <kraxel@bytesex.org>
+ and Heiko Eißfeldt <heiko@hexco.de>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/** \file cd_types.h
+ * \brief Header for routines which automatically determine the Compact Disc
+ * format and possibly filesystem on the CD.
+ *
+ */
+
+#ifndef __CDIO_CD_TYPES_H__
+#define __CDIO_CD_TYPES_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/**
+ * Filesystem types we understand. The highest-numbered fs type should
+ * be less than CDIO_FS_MASK defined below.
+ */
+#define CDIO_FS_AUDIO 1 /**< audio only - not really a
+ filesystem */
+#define CDIO_FS_HIGH_SIERRA 2
+#define CDIO_FS_ISO_9660 3 /**< ISO 9660 filesystem */
+#define CDIO_FS_INTERACTIVE 4
+#define CDIO_FS_HFS 5 /**< file system used on the Macintosh
+ system in MacOS 6 through MacOS 9
+ and depricated in OSX. */
+#define CDIO_FS_UFS 6 /**< Generic Unix file system derived
+ from the Berkeley fast file
+ system. */
+
+/**
+ * EXT2 was the GNU/Linux native filesystem for early kernels. Newer
+ * GNU/Linux OS's may use EXT3 which EXT2 with a journal.
+ */
+#define CDIO_FS_EXT2 7
+
+#define CDIO_FS_ISO_HFS 8 /**< both HFS & ISO-9660 filesystem */
+#define CDIO_FS_ISO_9660_INTERACTIVE 9 /**< both CD-RTOS and ISO filesystem */
+
+
+/**
+ * The 3DO is, technically, a set of specifications created by the 3DO
+ * company. These specs are for making a 3DO Interactive Multiplayer
+ * which uses a CD-player. Panasonic in the early 90's was the first
+ * company to manufacture and market a 3DO player.
+ */
+#define CDIO_FS_3DO 10
+
+/**
+ Microsoft X-BOX CD.
+ */
+#define CDIO_FS_XISO 11
+#define CDIO_FS_UDFX 12
+#define CDIO_FS_UDF 13
+#define CDIO_FS_ISO_UDF 14
+
+
+#define CDIO_FS_MASK 15 /**< Note: this should be 2**n-1 and
+ and greater than the highest
+ CDIO_FS number above */
+#define CDIO_FS_UNKNOWN CDIO_FS_MASK
+
+/**
+ * Macro to extract just the FS type portion defined above
+*/
+#define CDIO_FSTYPE(fs) (fs & CDIO_FS_MASK)
+
+/**
+ * Bit masks for the classes of CD-images. These are generally
+ * higher-level than the fs-type information above and may be determined
+ * based of the fs type information.
+ */
+#define CDIO_FS_ANAL_XA 0x0010 /**< eXtended Architecture format */
+#define CDIO_FS_ANAL_MULTISESSION 0x0020 /**< CD has multisesion */
+#define CDIO_FS_ANAL_PHOTO_CD 0x0040 /**< Is a Kodak Photo CD */
+#define CDIO_FS_ANAL_HIDDEN_TRACK 0x0080 /**< Hidden track at the
+ beginning of the CD */
+#define CDIO_FS_ANAL_CDTV 0x0100
+#define CDIO_FS_ANAL_BOOTABLE 0x0200 /**< CD is bootable */
+#define CDIO_FS_ANAL_VIDEOCD 0x0400 /**< VCD 1.1 */
+#define CDIO_FS_ANAL_ROCKRIDGE 0x0800 /**< Has Rock Ridge Extensions to
+ ISO 9660 */
+#define CDIO_FS_ANAL_JOLIET 0x1000 /**< Microsoft Joliet extensions
+ to ISO 9660 */
+#define CDIO_FS_ANAL_SVCD 0x2000 /**< Super VCD or Choiji Video CD */
+#define CDIO_FS_ANAL_CVD 0x4000 /**< Choiji Video CD */
+#define CDIO_FS_ANAL_XISO 0x8000 /**< XBOX CD */
+
+/**
+ * Pattern which can be used by cdio_get_devices to specify matching
+ * any sort of CD.
+ */
+#define CDIO_FS_MATCH_ALL (cdio_fs_anal_t) (~CDIO_FS_MASK)
+
+
+/*!
+ \brief The type used to return analysis information from
+ cdio_guess_cd_type.
+
+ These fields make sense only for when an ISO-9660 filesystem is used.
+ */
+typedef struct
+{
+ unsigned int joliet_level; /**< If has Joliet extensions, this is the
+ associated level number (i.e. 1, 2, or 3). */
+ char iso_label[33]; /**< This is 32 + 1 for null byte at the end in
+ formatting the string */
+ unsigned int isofs_size;
+ uint8_t UDFVerMinor; /**< For UDF filesystems only */
+ uint8_t UDFVerMajor; /**< For UDF filesystems only */
+} cdio_iso_analysis_t;
+
+/**
+ * Try to determine what kind of CD-image and/or filesystem we
+ * have at track track_num. Return information about the CD image
+ * is returned in iso_analysis and the return value.
+ */
+cdio_fs_anal_t cdio_guess_cd_type(const CdIo *cdio, int start_session,
+ track_t track_num,
+ /*out*/ cdio_iso_analysis_t *iso_analysis);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_CD_TYPES_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/cdio.h b/contrib/libcdio/cdio/cdio.h
new file mode 100644
index 000000000..f9e2a38e3
--- /dev/null
+++ b/contrib/libcdio/cdio/cdio.h
@@ -0,0 +1,980 @@
+/* -*- c -*-
+ $Id: cdio.h,v 1.4 2005/04/27 23:28:42 rockyb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/** \file cdio.h
+ *
+ * \brief The top-level header for libcdio: the CD Input and Control
+ * library. Applications include this for anything regarding libcdio.
+ */
+
+
+#ifndef __CDIO_H__
+#define __CDIO_H__
+
+/** Application Interface or Protocol version number. If the public
+ * interface changes, we increase this number.
+ */
+#define CDIO_API_VERSION 2
+
+#include <cdio/version.h>
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <cdio/types.h>
+#include <cdio/sector.h>
+
+/**! Flags specifying the category of device to open or is opened. */
+
+#define CDIO_SRC_IS_DISK_IMAGE_MASK 0x0001 /**< Read source is a CD image. */
+#define CDIO_SRC_IS_DEVICE_MASK 0x0002 /**< Read source is a CD device. */
+#define CDIO_SRC_IS_SCSI_MASK 0x0004 /**< Read source SCSI device. */
+#define CDIO_SRC_IS_NATIVE_MASK 0x0008
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/*! Size of fields returned by an INQUIRY command */
+#define CDIO_MMC_HW_VENDOR_LEN 8 /**< length of vendor field */
+#define CDIO_MMC_HW_MODEL_LEN 16 /**< length of model field */
+#define CDIO_MMC_HW_REVISION_LEN 4 /**< length of revision field */
+
+ /*! \brief Structure to return CD vendor, model, and revision-level
+ strings obtained via the INQUIRY command */
+ typedef struct cdio_hwinfo
+ {
+ char psz_vendor [CDIO_MMC_HW_VENDOR_LEN+1];
+ char psz_model [CDIO_MMC_HW_MODEL_LEN+1];
+ char psz_revision[CDIO_MMC_HW_REVISION_LEN+1];
+ } cdio_hwinfo_t;
+
+/* For compatability. */
+#define CdIo CdIo_t
+
+ /** This is an opaque structure for the CD object. */
+ typedef struct _CdIo CdIo_t;
+
+ /** This is an opaque structure for the CD-Text object. */
+ typedef struct cdtext cdtext_t;
+
+ /** The driver_id_t enumerations may be used to tag a specific driver
+ * that is opened or is desired to be opened. Note that this is
+ * different than what is available on a given host.
+ *
+ * Order is a little significant since the order is used in scans.
+ * We have to start with DRIVER_UNKNOWN and devices should come before
+ * disk-image readers. By putting something towards the top (a lower
+ * enumeration number), in an iterative scan we prefer that to
+ * something with a higher enumeration number.
+ *
+ * NOTE: IF YOU MODIFY ENUM MAKE SURE INITIALIZATION IN CDIO.C AGREES.
+ *
+ */
+ typedef enum {
+ DRIVER_UNKNOWN, /**< Used as input when we don't care what kind
+ of driver to use. */
+ DRIVER_BSDI, /**< BSDI driver */
+ DRIVER_FREEBSD, /**< FreeBSD driver - includes CAM and ioctl access */
+ DRIVER_LINUX, /**< GNU/Linux Driver */
+ DRIVER_SOLARIS, /**< Sun Solaris Driver */
+ DRIVER_OSX, /**< Apple OSX Driver */
+ DRIVER_WIN32, /**< Microsoft Windows Driver. Includes ASPI and
+ ioctl acces. */
+ DRIVER_CDRDAO, /**< cdrdao format CD image. This is listed
+ before BIN/CUE, to make the code prefer cdrdao
+ over BIN/CUE when both exist. */
+ DRIVER_BINCUE, /**< CDRWIN BIN/CUE format CD image. This is
+ listed before NRG, to make the code prefer
+ BIN/CUE over NRG when both exist. */
+ DRIVER_NRG, /**< Nero NRG format CD image. */
+ DRIVER_DEVICE /**< Is really a set of the above; should come last */
+ } driver_id_t;
+
+ /** There will generally be only one hardware for a given
+ build/platform from the list above. You can use the variable
+ below to determine which you've got. If the build doesn't make an
+ hardware driver, then the value will be DRIVER_UNKNOWN.
+ */
+ extern const driver_id_t cdio_os_driver;
+
+
+/** Make sure what's listed for CDIO_MIN_DRIVER is the last
+ enumeration in driver_id_t. Since we have a bogus (but useful) 0th
+ entry above we don't have to add one.
+*/
+#define CDIO_MIN_DRIVER DRIVER_BSDI
+#define CDIO_MIN_DEVICE_DRIVER CDIO_MIN_DRIVER
+#define CDIO_MAX_DRIVER DRIVER_NRG
+#define CDIO_MAX_DEVICE_DRIVER DRIVER_WIN32
+
+ typedef enum {
+ TRACK_FORMAT_AUDIO, /**< Audio track, e.g. CD-DA */
+ TRACK_FORMAT_CDI, /**< CD-i. How this is different from DATA below? */
+ TRACK_FORMAT_XA, /**< Mode2 of some sort */
+ TRACK_FORMAT_DATA, /**< Mode1 of some sort */
+ TRACK_FORMAT_PSX, /**< Playstation CD. Like audio but only 2336 bytes
+ * of user data.
+ */
+ TRACK_FORMAT_ERROR /**< Dunno what is, or some other error. */
+ } track_format_t;
+
+ extern const char *discmode2str[];
+
+ /*! Printable tags for track_format_t enumeration. */
+ extern const char *track_format2str[6];
+
+ /*!
+ Eject media in CD drive if there is a routine to do so.
+
+ @param p_cdio the CD object to be acted upon.
+ @return 0 if success and 1 for failure, and 2 if no routine.
+ If the CD is ejected *p_cdio is freed and p_cdio set to NULL.
+ */
+ int cdio_eject_media (CdIo_t **p_cdio);
+
+ /*!
+ Free any resources associated with p_cdio. Call this when done using p_cdio
+ and using CD reading/control operations.
+
+ @param p_cdio the CD object to eliminated.
+ */
+ void cdio_destroy (CdIo_t *p_cdio);
+
+ /*!
+ Free device list returned by cdio_get_devices or
+ cdio_get_devices_with_cap.
+
+ @param device_list list returned by cdio_get_devices or
+ cdio_get_devices_with_cap
+
+ @see cdio_get_devices, cdio_get_devices_with_cap
+
+ */
+ void cdio_free_device_list (char * device_list[]);
+
+ /*!
+ Get the value associatied with key.
+
+ @param p_cdio the CD object queried
+ @param key the key to retrieve
+ @return the value associatd with "key" or NULL if p_cdio is NULL
+ or "key" does not exist.
+ */
+ const char * cdio_get_arg (const CdIo_t *p_cdio, const char key[]);
+
+ /*!
+ Get CD-Text information for a CdIo object.
+
+ @param p_cdio the CD object that may contain CD-Text information.
+ @param i_track track for which we are requesting CD-Text information.
+ @return the CD-Text object or NULL if obj is NULL
+ or CD-Text information does not exist.
+
+ If i_track is 0 or CDIO_CDROM_LEADOUT_TRACK the track returned
+ is the information assocated with the CD.
+ */
+ const cdtext_t *cdio_get_cdtext (CdIo_t *p_cdio, track_t i_track);
+
+ /*!
+ Get the default CD device.
+ if p_cdio is NULL (we haven't initialized a specific device driver),
+ then find a suitable one and return the default device for that.
+
+ @param p_cdio the CD object queried
+ @return a string containing the default CD device or NULL is
+ if we couldn't get a default device.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+ */
+ char * cdio_get_default_device (const CdIo_t *p_cdio);
+
+ /*! Return an array of device names. If you want a specific
+ devices for a driver, give that device. If you want hardware
+ devices, give DRIVER_DEVICE and if you want all possible devices,
+ image drivers and hardware drivers give DRIVER_UNKNOWN.
+
+ NULL is returned if we couldn't return a list of devices.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+ */
+ char ** cdio_get_devices (driver_id_t driver_id);
+
+ /*!
+ Get an array of device names in search_devices that have at least
+ the capabilities listed by the capabities parameter. If
+ search_devices is NULL, then we'll search all possible CD drives.
+
+ If "b_any" is set false then every capability listed in the
+ extended portion of capabilities (i.e. not the basic filesystem)
+ must be satisified. If "any" is set true, then if any of the
+ capabilities matches, we call that a success.
+
+ To find a CD-drive of any type, use the mask CDIO_FS_MATCH_ALL.
+
+ @return the array of device names or NULL if we couldn't get a
+ default device. It is also possible to return a non NULL but
+ after dereferencing the the value is NULL. This also means nothing
+ was found.
+ */
+ char ** cdio_get_devices_with_cap (char* ppsz_search_devices[],
+ cdio_fs_anal_t capabilities, bool b_any);
+
+ /*!
+ Like cdio_get_devices_with_cap but we return the driver we found
+ as well. This is because often one wants to search for kind of drive
+ and then *open* it afterwards. Giving the driver back facilitates this,
+ and speeds things up for libcdio as well.
+ */
+ char ** cdio_get_devices_with_cap_ret (/*out*/ char* ppsz_search_devices[],
+ cdio_fs_anal_t capabilities,
+ bool b_any,
+ /*out*/ driver_id_t *p_driver_id);
+
+ /*! Like cdio_get_devices, but we may change the p_driver_id if we
+ were given DRIVER_DEVICE or DRIVER_UNKNOWN. This is because
+ often one wants to get a drive name and then *open* it
+ afterwards. Giving the driver back facilitates this, and speeds
+ things up for libcdio as well.
+ */
+
+ char ** cdio_get_devices_ret (/*in/out*/ driver_id_t *p_driver_id);
+
+ /*!
+ Get disc mode - the kind of CD (CD-DA, CD-ROM mode 1, CD-MIXED, etc.
+ that we've got. The notion of "CD" is extended a little to include
+ DVD's.
+ */
+ discmode_t cdio_get_discmode (CdIo_t *p_cdio);
+
+ /*!
+ Get the what kind of device we've got.
+
+ @param p_cdio the CD object queried
+ @param p_read_cap pointer to return read capabilities
+ @param p_write_cap pointer to return write capabilities
+ @param p_misc_cap pointer to return miscellaneous other capabilities
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+ */
+ void cdio_get_drive_cap (const CdIo_t *p_cdio,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap);
+
+ /*!
+ Get the drive capabilities for a specified device.
+
+ @return a list of device capabilities.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+ */
+ void cdio_get_drive_cap_dev (const char *device,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap);
+
+ /*!
+ Get a string containing the name of the driver in use.
+
+ @return a string with driver name or NULL if CdIo is NULL (we
+ haven't initialized a specific device.
+ */
+ const char * cdio_get_driver_name (const CdIo_t *p_cdio);
+
+ /*!
+ Get the driver id.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then return DRIVER_UNKNOWN.
+
+ @return the driver id..
+ */
+ driver_id_t cdio_get_driver_id (const CdIo_t *p_cdio);
+
+ /*!
+ Get the number of the first track.
+
+ @return the track number or CDIO_INVALID_TRACK
+ on error.
+ */
+ track_t cdio_get_first_track_num(const CdIo_t *p_cdio);
+
+ /*!
+ Get the CD-ROM hardware info via a SCSI MMC INQUIRY command.
+ False is returned if we had an error getting the information.
+ */
+ bool cdio_get_hwinfo ( const CdIo_t *p_cdio,
+ /* out*/ cdio_hwinfo_t *p_hw_info );
+
+
+ /*!
+ Return the Joliet level recognized for p_cdio.
+ */
+ uint8_t cdio_get_joliet_level(const CdIo_t *p_cdio);
+
+ /*!
+ Get the media catalog number (MCN) from the CD.
+
+ @return the media catalog number r NULL if there is none or we
+ don't have the ability to get it.
+
+ Note: string is malloc'd so caller has to free() the returned
+ string when done with it.
+
+ */
+ char * cdio_get_mcn (const CdIo_t *p_cdio);
+
+ /*!
+ Get the number of tracks on the CD.
+
+ @return the number of tracks, or CDIO_INVALID_TRACK if there is
+ an error.
+ */
+ track_t cdio_get_num_tracks (const CdIo_t *p_cdio);
+
+ /*!
+ Get the format (audio, mode2, mode1) of track.
+ */
+ track_format_t cdio_get_track_format(const CdIo_t *p_cdio, track_t i_track);
+
+ /*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+ */
+ bool cdio_get_track_green(const CdIo_t *p_cdio, track_t i_track);
+
+ /*!
+ Get the starting LBA for track number
+ i_track in p_cdio. Track numbers usually start at something
+ greater than 0, usually 1.
+
+ The "leadout" track is specified either by
+ using i_track CDIO_CDROM_LEADOUT_TRACK or the total tracks+1.
+
+ @param p_cdio object to get information from
+ @param i_track the track number we want the LSN for
+ @return the starting LBA or CDIO_INVALID_LBA on error.
+ */
+ lba_t cdio_get_track_lba(const CdIo_t *p_cdio, track_t i_track);
+
+ /*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ i_track in p_cdio. Track numbers usually start at something
+ greater than 0, usually 1.
+
+ The "leadout" track is specified either by
+ using i_track CDIO_CDROM_LEADOUT_TRACK or the total tracks+1.
+
+ @param p_cdio object to get information from
+ @param i_track the track number we want the LSN for
+ @return the starting LSN or CDIO_INVALID_LSN on error.
+ */
+ lsn_t cdio_get_track_lsn(const CdIo_t *p_cdio, track_t i_track);
+
+ /*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ i_track in p_cdio. Track numbers usually start at something
+ greater than 0, usually 1.
+
+ The "leadout" track is specified either by
+ using i_track CDIO_CDROM_LEADOUT_TRACK or the total tracks+1.
+
+ @return true if things worked or false if there is no track entry.
+ */
+ bool cdio_get_track_msf(const CdIo_t *p_cdio, track_t i_track,
+ /*out*/ msf_t *msf);
+
+ /*!
+ Get the number of sectors between this track an the next. This
+ includes any pregap sectors before the start of the next track.
+ Track numbers usually start at something
+ greater than 0, usually 1.
+
+ @return the number of sectors or 0 if there is an error.
+ */
+ unsigned int cdio_get_track_sec_count(const CdIo_t *p_cdio, track_t i_track);
+
+ /*!
+ Reposition read offset
+ Similar to (if not the same as) libc's lseek()
+
+ @param p_cdio object to get information from
+ @param offset amount to seek
+ @param whence like corresponding parameter in libc's lseek, e.g.
+ SEEK_SET or SEEK_END.
+ @return (off_t) -1 on error.
+ */
+ off_t cdio_lseek(const CdIo_t *p_cdio, off_t offset, int whence);
+
+ /*!
+ Reads into buf the next size bytes.
+ Similar to (if not the same as) libc's read()
+
+ @return (ssize_t) -1 on error.
+ */
+ ssize_t cdio_read(const CdIo_t *p_cdio, void *buf, size_t size);
+
+ /*!
+ Read an audio sector
+
+ @param p_cdio object to read from
+ @param buf place to read data into
+ @param lsn sector to read
+
+ @return 0 if no error, nonzero otherwise.
+ */
+ int cdio_read_audio_sector (const CdIo_t *p_cdio, void *buf, lsn_t lsn);
+
+ /*!
+ Reads audio sectors
+
+ @param p_cdio object to read from
+ @param buf place to read data into
+ @param lsn sector to read
+ @param i_sectors number of sectors to read
+
+ @return 0 if no error, nonzero otherwise.
+ */
+ int cdio_read_audio_sectors (const CdIo_t *p_cdio, void *buf, lsn_t lsn,
+ unsigned int i_sectors);
+
+ /*!
+ Reads a mode1 sector
+
+ @param p_cdio object to read from
+ @param buf place to read data into
+ @param lsn sector to read
+ @param b_form2 true for reading mode1 form2 sectors or false for
+ mode1 form1 sectors.
+
+ @return 0 if no error, nonzero otherwise.
+ */
+ int cdio_read_mode1_sector (const CdIo_t *p_cdio, void *buf, lsn_t lsn,
+ bool b_form2);
+
+ /*!
+ Reads mode1 sectors
+
+ @param p_cdio object to read from
+ @param buf place to read data into
+ @param lsn sector to read
+ @param b_form2 true for reading mode1 form2 sectors or false for
+ mode1 form1 sectors.
+ @param i_sectors number of sectors to read
+
+ @return 0 if no error, nonzero otherwise.
+ */
+ int cdio_read_mode1_sectors (const CdIo_t *p_cdio, void *buf, lsn_t lsn,
+ bool b_form2, unsigned int i_sectors);
+
+ /*!
+ Reads a mode1 sector
+
+ @param p_cdio object to read from
+ @param buf place to read data into
+ @param lsn sector to read
+ @param b_form2 true for reading mode1 form2 sectors or false for
+ mode1 form1 sectors.
+
+ @return 0 if no error, nonzero otherwise.
+ */
+ int cdio_read_mode2_sector (const CdIo_t *p_cdio, void *buf, lsn_t lsn,
+ bool b_form2);
+
+ /*!
+ Reads mode2 sectors
+
+ @param p_cdio object to read from
+ @param buf place to read data into
+ @param lsn sector to read
+ @param b_form2 true for reading mode1 form2 sectors or false for
+ mode1 form1 sectors.
+ @param i_sectors number of sectors to read
+
+ @return 0 if no error, nonzero otherwise.
+ */
+ int cdio_read_mode2_sectors (const CdIo_t *p_cdio, void *buf, lsn_t lsn,
+ bool b_form2, unsigned int i_sectors);
+
+ /*!
+ Set the arg "key" with "value" in "obj".
+
+ @param p_cdio the CD object to set
+ @param key the key to set
+ @param value the value to assocaiate with key
+ @return 0 if no error was found, and nonzero otherwise.
+ */
+ int cdio_set_arg (CdIo_t *p_cdio, const char key[], const char value[]);
+
+ /*!
+ Get the size of the CD in logical block address (LBA) units.
+
+ @param p_cdio the CD object queried
+ @return the size
+ */
+ uint32_t cdio_stat_size (const CdIo_t *p_cdio);
+
+ /*!
+ Initialize CD Reading and control routines. Should be called first.
+ */
+ bool cdio_init(void);
+
+ /* True if xxx driver is available. where xxx=linux, solaris, nrg, ...
+ */
+
+ /*! True if BSDI driver is available. */
+ bool cdio_have_bsdi (void);
+
+ /*! True if FreeBSD driver is available. */
+ bool cdio_have_freebsd (void);
+
+ /*! True if GNU/Linux driver is available. */
+ bool cdio_have_linux (void);
+
+ /*! True if Sun Solaris driver is available. */
+ bool cdio_have_solaris (void);
+
+ /*! True if Apple OSX driver is available. */
+ bool cdio_have_osx (void);
+
+ /*! True if Microsoft Windows driver is available. */
+ bool cdio_have_win32 (void);
+
+ /*! True if Nero driver is available. */
+ bool cdio_have_nrg (void);
+
+ /*! True if BIN/CUE driver is available. */
+ bool cdio_have_bincue (void);
+
+ /*! True if cdrdao CDRDAO driver is available. */
+ bool cdio_have_cdrdao (void);
+
+ /*! Like cdio_have_xxx but uses an enumeration instead. */
+ bool cdio_have_driver (driver_id_t driver_id);
+
+ /*!
+ Get a string decribing driver_id.
+
+ @param driver_id the driver you want the description for
+ @return a sring of driver description
+ */
+ const char *cdio_driver_describe (driver_id_t driver_id);
+
+ /*! Sets up to read from place specified by source_name and
+ driver_id. This or cdio_open_* should be called before using any
+ other routine, except cdio_init. This will call cdio_init, if
+ that hasn't been done previously. to call one of the specific
+ cdio_open_xxx routines.
+
+ @return the cdio object or NULL on error or no device.
+ */
+ CdIo_t * cdio_open (const char *source_name, driver_id_t driver_id);
+
+ /*! Sets up to read from place specified by source_name, driver_id
+ and access mode. This or cdio_open should be called before using
+ any other routine, except cdio_init. This will call cdio_init, if
+ that hasn't been done previously. to call one of the specific
+ cdio_open_xxx routines.
+
+ @return the cdio object or NULL on error or no device.
+ */
+ CdIo_t * cdio_open_am (const char *psz_source_name,
+ driver_id_t driver_id, const char *psz_access_mode);
+
+ /*! Set up BIN/CUE CD disk-image for reading. Source is the .bin or
+ .cue file
+
+ @return the cdio object or NULL on error or no device.
+ */
+ CdIo_t * cdio_open_bincue (const char *psz_cue_name);
+
+ /*! Set up BIN/CUE CD disk-image for reading. Source is the .bin or
+ .cue file
+
+ @return the cdio object or NULL on error or no device..
+ */
+ CdIo_t * cdio_open_am_bincue (const char *psz_cue_name,
+ const char *psz_access_mode);
+
+ /*! Set up cdrdao CD disk-image for reading. Source is the .toc file
+
+ @return the cdio object or NULL on error or no device.
+ */
+ CdIo_t * cdio_open_cdrdao (const char *psz_toc_name);
+
+ /*! Set up cdrdao CD disk-image for reading. Source is the .toc file
+
+ @return the cdio object or NULL on error or no device..
+ */
+ CdIo_t * cdio_open_am_cdrdao (const char *psz_toc_name,
+ const char *psz_access_mode);
+
+ /*! Return a string containing the default CUE file that would
+ be used when none is specified.
+
+ @return the cdio object or NULL on error or no device.
+ */
+ char * cdio_get_default_device_bincue(void);
+
+ char **cdio_get_devices_bincue(void);
+
+ /*! Return a string containing the default CUE file that would
+ be used when none is specified.
+
+ NULL is returned on error or there is no device.
+ */
+ char * cdio_get_default_device_cdrdao(void);
+
+ char **cdio_get_devices_cdrdao(void);
+
+ /*! Set up CD-ROM for reading. The device_name is
+ the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no driver for a some sort of hardware CD-ROM.
+ */
+ CdIo_t * cdio_open_cd (const char *device_name);
+
+ /*! Set up CD-ROM for reading. The device_name is
+ the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no driver for a some sort of hardware CD-ROM.
+ */
+ CdIo_t * cdio_open_am_cd (const char *psz_device,
+ const char *psz_access_mode);
+
+ /*! CDRWIN BIN/CUE CD disc-image routines. Source is the .cue file
+
+ @return the cdio object for subsequent operations.
+ NULL on error.
+ */
+ CdIo_t * cdio_open_cue (const char *cue_name);
+
+ /*! Set up CD-ROM for reading using the BSDI driver. The device_name is
+ the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no BSDI driver.
+
+ @see cdio_open
+ */
+ CdIo_t * cdio_open_bsdi (const char *psz_source_name);
+
+ /*! Set up CD-ROM for reading using the BSDI driver. The device_name is
+ the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no BSDI driver.
+
+ @see cdio_open
+ */
+ CdIo_t * cdio_open_am_bsdi (const char *psz_source_name,
+ const char *psz_access_mode);
+
+ /*! Return a string containing the default device name that the
+ BSDI driver would use when none is specified.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no BSDI driver.
+
+ @see cdio_open_cd, cdio_open
+ */
+ char * cdio_get_default_device_bsdi(void);
+
+ /*! Return a list of all of the CD-ROM devices that the BSDI driver
+ can find.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+ */
+ char **cdio_get_devices_bsdi(void);
+
+ /*! Set up CD-ROM for reading using the FreeBSD driver. The device_name is
+ the some sort of device name.
+
+ NULL is returned on error or there is no FreeBSD driver.
+
+ @see cdio_open_cd, cdio_open
+ */
+ CdIo_t * cdio_open_freebsd (const char *paz_source_name);
+
+ /*! Set up CD-ROM for reading using the FreeBSD driver. The device_name is
+ the some sort of device name.
+
+ NULL is returned on error or there is no FreeBSD driver.
+
+ @see cdio_open_cd, cdio_open
+ */
+ CdIo_t * cdio_open_am_freebsd (const char *psz_source_name,
+ const char *psz_access_mode);
+
+ /*! Return a string containing the default device name that the
+ FreeBSD driver would use when none is specified.
+
+ NULL is returned on error or there is no CD-ROM device.
+ */
+ char * cdio_get_default_device_freebsd(void);
+
+ /*! Return a list of all of the CD-ROM devices that the FreeBSD driver
+ can find.
+ */
+ char **cdio_get_devices_freebsd(void);
+
+ /*! Set up CD-ROM for reading using the GNU/Linux driver. The device_name is
+ the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no GNU/Linux driver.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+ */
+ CdIo_t * cdio_open_linux (const char *source_name);
+
+ /*! Set up CD-ROM for reading using the GNU/Linux driver. The
+ device_name is the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no GNU/Linux driver.
+ */
+ CdIo_t * cdio_open_am_linux (const char *source_name,
+ const char *access_mode);
+
+ /*! Return a string containing the default device name that the
+ GNU/Linux driver would use when none is specified. A scan is made
+ for CD-ROM drives with CDs in them.
+
+ NULL is returned on error or there is no CD-ROM device.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+
+ @see cdio_open_cd, cdio_open
+ */
+ char * cdio_get_default_device_linux(void);
+
+ /*! Return a list of all of the CD-ROM devices that the GNU/Linux driver
+ can find.
+ */
+ char **cdio_get_devices_linux(void);
+
+ /*! Set up CD-ROM for reading using the Sun Solaris driver. The
+ device_name is the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no Solaris driver.
+ */
+ CdIo_t * cdio_open_solaris (const char *source_name);
+
+ /*! Set up CD-ROM for reading using the Sun Solaris driver. The
+ device_name is the some sort of device name.
+
+ @return the cdio object for subsequent operations.
+ NULL on error or there is no Solaris driver.
+ */
+ CdIo_t * cdio_open_am_solaris (const char *psz_source_name,
+ const char *psz_access_mode);
+
+ /*! Return a string containing the default device name that the
+ Solaris driver would use when none is specified. A scan is made
+ for CD-ROM drives with CDs in them.
+
+ NULL is returned on error or there is no CD-ROM device.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+
+ @see cdio_open_cd, cdio_open
+ */
+ char * cdio_get_default_device_solaris(void);
+
+ /*! Return a list of all of the CD-ROM devices that the Solaris driver
+ can find.
+ */
+ char **cdio_get_devices_solaris(void);
+
+ /*! Set up CD-ROM for reading using the Apple OSX driver. The
+ device_name is the some sort of device name.
+
+ NULL is returned on error or there is no OSX driver.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+
+ @see cdio_open_cd, cdio_open
+ */
+ CdIo_t * cdio_open_osx (const char *psz_source_name);
+
+ /*! Set up CD-ROM for reading using the Apple OSX driver. The
+ device_name is the some sort of device name.
+
+ NULL is returned on error or there is no OSX driver.
+
+ @see cdio_open_cd, cdio_open
+ */
+ CdIo_t * cdio_open_am_osx (const char *psz_source_name,
+ const char *psz_access_mode);
+
+ /*! Return a string containing the default device name that the
+ OSX driver would use when none is specified. A scan is made
+ for CD-ROM drives with CDs in them.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+ */
+ char * cdio_get_default_device_osx(void);
+
+ /*! Return a list of all of the CD-ROM devices that the OSX driver
+ can find.
+ */
+ char **cdio_get_devices_osx(void);
+
+ /*! Set up CD-ROM for reading using the Microsoft Windows driver. The
+ device_name is the some sort of device name.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+ */
+ CdIo_t * cdio_open_win32 (const char *source_name);
+
+ /*! Set up CD-ROM for reading using the Microsoft Windows driver. The
+ device_name is the some sort of device name.
+
+ NULL is returned on error or there is no Microsof Windows driver.
+ */
+ CdIo_t * cdio_open_am_win32 (const char *psz_source_name,
+ const char *psz_access_mode);
+
+ /*! Return a string containing the default device name that the
+ Win32 driver would use when none is specified. A scan is made
+ for CD-ROM drives with CDs in them.
+
+ In some situations of drivers or OS's we can't find a CD device if
+ there is no media in it and it is possible for this routine to return
+ NULL even though there may be a hardware CD-ROM.
+
+ @see cdio_open_cd, cdio_open
+ */
+ char * cdio_get_default_device_win32(void);
+
+ char **cdio_get_devices_win32(void);
+
+ /*! Set up CD-ROM for reading using the Nero driver. The
+ device_name is the some sort of device name.
+
+ @return true on success; NULL on error or there is no Nero driver.
+ */
+ CdIo_t * cdio_open_nrg (const char *source_name);
+
+ /*! Set up CD-ROM for reading using the Nero driver. The
+ device_name is the some sort of device name.
+
+ @return true on success; NULL on error or there is no Nero driver.
+ */
+ CdIo_t * cdio_open_am_nrg (const char *psz_source_name,
+ const char *psz_access_mode);
+
+ /*! Return a string containing the default device name that the
+ NRG driver would use when none is specified. A scan is made
+ for NRG disk images in the current directory..
+
+ NULL is returned on error or there is no CD-ROM device.
+ */
+ char * cdio_get_default_device_nrg(void);
+
+ char **cdio_get_devices_nrg(void);
+
+ /*!
+
+ Determine if bin_name is the bin file part of a CDRWIN CD disk image.
+
+ @param bin_name location of presumed CDRWIN bin image file.
+ @return the corresponding CUE file if bin_name is a BIN file or
+ NULL if not a BIN file.
+ */
+ char *cdio_is_binfile(const char *bin_name);
+
+ /*!
+ Determine if cue_name is the cue sheet for a CDRWIN CD disk image.
+
+ @return corresponding BIN file if cue_name is a CDRWIN cue file or
+ NULL if not a CUE file.
+ */
+ char *cdio_is_cuefile(const char *cue_name);
+
+ /*!
+ Determine if psg_nrg is a Nero CD disk image.
+
+ @param psz_nrg location of presumed NRG image file.
+ @return true if psz_nrg is a Nero NRG image or false
+ if not a NRG image.
+ */
+ bool cdio_is_nrg(const char *psz_nrg);
+
+ /*!
+ Determine if psg_toc is a TOC file for a cdrdao CD disk image.
+
+ @param psz_toc location of presumed TOC image file.
+ @return true if toc_name is a cdrdao TOC file or false
+ if not a TOC file.
+ */
+ bool cdio_is_tocfile(const char *psz_toc);
+
+ /*!
+ Determine if source_name refers to a real hardware CD-ROM.
+
+ @param source_name location name of object
+ @param driver_id driver for reading object. Use DRIVER_UNKNOWN if you
+ don't know what driver to use.
+ @return true if source_name is a device; If false is returned we
+ could have a CD disk image.
+ */
+ bool cdio_is_device(const char *source_name, driver_id_t driver_id);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_H__ */
diff --git a/contrib/libcdio/cdio/cdtext.h b/contrib/libcdio/cdio/cdtext.h
new file mode 100644
index 000000000..4b397a3ff
--- /dev/null
+++ b/contrib/libcdio/cdio/cdtext.h
@@ -0,0 +1,108 @@
+/*
+ $Id: cdtext.h,v 1.1 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+ adapted from cuetools
+ Copyright (C) 2003 Svend Sanjay Sorensen <ssorensen@fastmail.fm>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*!
+ * \file cdtext.h
+ * \brief Header CD-Text information
+*/
+
+
+#ifndef __CDIO_CDTEXT_H__
+#define __CDIO_CDTEXT_H__
+
+#include <cdio/cdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#define MAX_CDTEXT_FIELDS 13
+
+ /*! \brief structure for holding CD-Text information
+
+ @see cdtext_init, cdtext_destroy, cdtext_get, and cdtext_set.
+ */
+ struct cdtext {
+ char *field[MAX_CDTEXT_FIELDS];
+ };
+
+ /*! \brief A list of all of the CD-Text fields */
+ typedef enum {
+ CDTEXT_ARRANGER = 0, /**< name(s) of the arranger(s) */
+ CDTEXT_COMPOSER = 1, /**< name(s) of the composer(s) */
+ CDTEXT_DISCID = 2, /**< disc identification information */
+ CDTEXT_GENRE = 3, /**< genre identification and genre information */
+ CDTEXT_MESSAGE = 4, /**< ISRC code of each track */
+ CDTEXT_ISRC = 5, /**< message(s) from the content provider or artist */
+ CDTEXT_PERFORMER = 6, /**< name(s) of the performer(s) */
+ CDTEXT_SIZE_INFO = 7, /**< size information of the block */
+ CDTEXT_SONGWRITER = 8, /**< name(s) of the songwriter(s) */
+ CDTEXT_TITLE = 9, /**< title of album name or track titles */
+ CDTEXT_TOC_INFO = 10, /**< table of contents information */
+ CDTEXT_TOC_INFO2 = 11, /**< second table of contents information */
+ CDTEXT_UPC_EAN = 12,
+ CDTEXT_INVALID = MAX_CDTEXT_FIELDS
+ } cdtext_field_t;
+
+ /*! Return string representation of the enum values above */
+ const char *cdtext_field2str (cdtext_field_t i);
+
+ /*! Initialize a new cdtext structure.
+ When the structure is no longer needed, release the
+ resources using cdtext_delete.
+ */
+ void cdtext_init (cdtext_t *cdtext);
+
+ /*! Free memory assocated with cdtext*/
+ void cdtext_destroy (cdtext_t *cdtext);
+
+ /*! returns the string associated with the given field. NULL is
+ returned if key is CDTEXT_INVALID or the field is not set.
+
+ @see cdio_get_cdtext to retrieve the cdtext structure used as
+ input here.
+ */
+ const char *cdtext_get (cdtext_field_t key, const cdtext_t *cdtext);
+
+ /*!
+ returns enum of keyword if key is a CD-Text keyword,
+ returns MAX_CDTEXT_FIELDS non-zero otherwise.
+ */
+ cdtext_field_t cdtext_is_keyword (const char *key);
+
+ /*!
+ sets cdtext's keyword entry to field
+ */
+ void cdtext_set (cdtext_field_t key, const char *value, cdtext_t *cdtext);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_CDTEXT_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/ds.h b/contrib/libcdio/cdio/ds.h
new file mode 100644
index 000000000..c811cadd4
--- /dev/null
+++ b/contrib/libcdio/cdio/ds.h
@@ -0,0 +1,73 @@
+/*
+ $Id: ds.h,v 1.1 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_DS_H__
+#define __CDIO_DS_H__
+
+#include <cdio/types.h>
+
+/* opaque... */
+typedef struct _CdioList CdioList;
+typedef struct _CdioListNode CdioListNode;
+
+typedef int (*_cdio_list_cmp_func) (void *data1, void *data2);
+
+typedef int (*_cdio_list_iterfunc) (void *data, void *user_data);
+
+/* methods */
+CdioList *_cdio_list_new (void);
+
+void _cdio_list_free (CdioList *list, int free_data);
+
+unsigned _cdio_list_length (const CdioList *list);
+
+void _cdio_list_prepend (CdioList *list, void *data);
+
+void _cdio_list_append (CdioList *list, void *data);
+
+void _cdio_list_foreach (CdioList *list, _cdio_list_iterfunc func, void *user_data);
+
+CdioListNode *_cdio_list_find (CdioList *list, _cdio_list_iterfunc cmp_func, void *user_data);
+
+#define _CDIO_LIST_FOREACH(node, list) \
+ for (node = _cdio_list_begin (list); node; node = _cdio_list_node_next (node))
+
+/* node ops */
+
+CdioListNode *_cdio_list_begin (const CdioList *list);
+
+CdioListNode *_cdio_list_end (CdioList *list);
+
+CdioListNode *_cdio_list_node_next (CdioListNode *node);
+
+void _cdio_list_node_free (CdioListNode *node, int free_data);
+
+void *_cdio_list_node_data (CdioListNode *node);
+
+#endif /* __CDIO_DS_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
+
diff --git a/contrib/libcdio/cdio/dvd.h b/contrib/libcdio/cdio/dvd.h
new file mode 100644
index 000000000..df58c4322
--- /dev/null
+++ b/contrib/libcdio/cdio/dvd.h
@@ -0,0 +1,113 @@
+/*
+ $Id: dvd.h,v 1.1 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+ Modeled after GNU/Linux definitions in linux/cdrom.h
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*!
+ \file dvd.h
+ \brief Definitions for DVD access.
+*/
+
+#ifndef __CDIO_DVD_H__
+#define __CDIO_DVD_H__
+
+#include <cdio/types.h>
+
+/*! Values used in a READ DVD STRUCTURE */
+
+#define CDIO_DVD_STRUCT_PHYSICAL 0x00
+#define CDIO_DVD_STRUCT_COPYRIGHT 0x01
+#define CDIO_DVD_STRUCT_DISCKEY 0x02
+#define CDIO_DVD_STRUCT_BCA 0x03
+#define CDIO_DVD_STRUCT_MANUFACT 0x04
+
+/*! Media definitions for "Book Type" */
+#define CDIO_DVD_BOOK_DVD_ROM 0
+#define CDIO_DVD_BOOK_DVD_RAM 1
+#define CDIO_DVD_BOOK_DVD_R 2 /**< DVD-R */
+#define CDIO_DVD_BOOK_DVD_RW 3 /**< DVD-RW */
+#define CDIO_DVD_BOOK_DVD_PR 8 /**< DVD+R */
+#define CDIO_DVD_BOOK_DVD_PRW 9 /**< DVD+RW */
+
+typedef struct cdio_dvd_layer {
+ uint8_t book_version : 4;
+ uint8_t book_type : 4;
+ uint8_t min_rate : 4;
+ uint8_t disc_size : 4;
+ uint8_t layer_type : 4;
+ uint8_t track_path : 1;
+ uint8_t nlayers : 2;
+ uint8_t track_density : 4;
+ uint8_t linear_density: 4;
+ uint8_t bca : 1;
+ uint32_t start_sector;
+ uint32_t end_sector;
+ uint32_t end_sector_l0;
+} cdio_dvd_layer_t;
+
+/*! Maximum number of layers in a DVD. */
+#define CDIO_DVD_MAX_LAYERS 4
+
+typedef struct cdio_dvd_physical {
+ uint8_t type;
+ uint8_t layer_num;
+ cdio_dvd_layer_t layer[CDIO_DVD_MAX_LAYERS];
+} cdio_dvd_physical_t;
+
+typedef struct cdio_dvd_copyright {
+ uint8_t type;
+
+ uint8_t layer_num;
+ uint8_t cpst;
+ uint8_t rmi;
+} cdio_dvd_copyright_t;
+
+typedef struct cdio_dvd_disckey {
+ uint8_t type;
+
+ unsigned agid : 2;
+ uint8_t value[2048];
+} cdio_dvd_disckey_t;
+
+typedef struct cdio_dvd_bca {
+ uint8_t type;
+
+ int len;
+ uint8_t value[188];
+} cdio_dvd_bca_t;
+
+typedef struct cdio_dvd_manufact {
+ uint8_t type;
+
+ uint8_t layer_num;
+ int len;
+ uint8_t value[2048];
+} cdio_dvd_manufact_t;
+
+typedef union {
+ uint8_t type;
+
+ cdio_dvd_physical_t physical;
+ cdio_dvd_copyright_t copyright;
+ cdio_dvd_disckey_t disckey;
+ cdio_dvd_bca_t bca;
+ cdio_dvd_manufact_t manufact;
+} cdio_dvd_struct_t;
+
+#endif /* __SCSI_MMC_H__ */
diff --git a/contrib/libcdio/cdio/iso9660.h b/contrib/libcdio/cdio/iso9660.h
new file mode 100644
index 000000000..104b6ba62
--- /dev/null
+++ b/contrib/libcdio/cdio/iso9660.h
@@ -0,0 +1,786 @@
+/*
+ $Id: iso9660.h,v 1.3 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ See also iso9660.h by Eric Youngdale (1993).
+
+ Copyright 1993 Yggdrasil Computing, Incorporated
+ Copyright (c) 1999,2000 J. Schilling
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*!
+ * \file iso9660.h
+ * \brief Header for libiso9660: the ISO-9660 filesystem library.
+*/
+
+
+#ifndef __CDIO_ISO9660_H__
+#define __CDIO_ISO9660_H__
+
+#include <cdio/cdio.h>
+#include <cdio/ds.h>
+#include <cdio/xa.h>
+
+#include <time.h>
+
+#define _delta(from, to) ((to) - (from) + 1)
+
+#define MIN_TRACK_SIZE 4*75
+#define MIN_ISO_SIZE MIN_TRACK_SIZE
+
+/*!
+ An ISO filename is: "abcde.eee;1" -> <filename> '.' <ext> ';' <version #>
+
+ For ISO-9660 Level 1, the maximum needed string length is:
+
+\verbatim
+ 30 chars (filename + ext)
+ + 2 chars ('.' + ';')
+ + 5 chars (strlen("32767"))
+ + 1 null byte
+ ================================
+ = 38 chars
+\endverbatim
+*/
+
+/*! size in bytes of the filename portion + null byte */
+#define LEN_ISONAME 31
+
+/*! Max # characters in the entire ISO 9660 filename. */
+#define MAX_ISONAME 37
+
+/*! Max # characters in the entire ISO 9660 filename. */
+#define MAX_ISOPATHNAME 255
+
+/*! Max # characters in an perparer id. */
+#define ISO_MAX_PREPARER_ID 128
+
+/*! Max # characters in an publisher id. */
+#define ISO_MAX_PUBLISHER_ID 128
+
+/*! Max # characters in an application id. */
+#define ISO_MAX_APPLICATION_ID 128
+
+/*! Max # characters in an system id. */
+#define ISO_MAX_SYSTEM_ID 32
+
+/*! Max # characters in an volume id. */
+#define ISO_MAX_VOLUME_ID 32
+
+/*! Max # characters in an volume-set id. */
+#define ISO_MAX_VOLUMESET_ID 128
+
+/**! ISO 9660 directory flags. */
+#define ISO_FILE 0 /**< Not really a flag... */
+#define ISO_EXISTENCE 1 /**< Do not make existence known (hidden) */
+#define ISO_DIRECTORY 2 /**< This file is a directory */
+#define ISO_ASSOCIATED 4 /**< This file is an associated file */
+#define ISO_RECORD 8 /**< Record format in extended attr. != 0 */
+#define ISO_PROTECTION 16 /**< No read/execute perm. in ext. attr. */
+#define ISO_DRESERVED1 32 /**< Reserved bit 5 */
+#define ISO_DRESERVED2 64 /**< Reserved bit 6 */
+#define ISO_MULTIEXTENT 128 /**< Not final entry of a mult. ext. file */
+
+/**! Volume descriptor types */
+#define ISO_VD_PRIMARY 1
+#define ISO_VD_SUPPLEMENTARY 2 /**< Used by Joliet */
+#define ISO_VD_END 255
+
+/*! Sector of Primary Volume Descriptor */
+#define ISO_PVD_SECTOR 16
+
+/*! Sector of End Volume Descriptor */
+#define ISO_EVD_SECTOR 17
+
+/*! String inside track identifying an ISO 9660 filesystem. */
+#define ISO_STANDARD_ID "CD001"
+
+
+/*! Number of bytes in an ISO 9660 block */
+#define ISO_BLOCKSIZE 2048
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+enum strncpy_pad_check {
+ ISO9660_NOCHECK = 0,
+ ISO9660_7BIT,
+ ISO9660_ACHARS,
+ ISO9660_DCHARS
+};
+
+#ifndef EMPTY_ARRAY_SIZE
+#define EMPTY_ARRAY_SIZE 0
+#endif
+
+PRAGMA_BEGIN_PACKED
+
+/*!
+ \brief ISO-9660 shorter-format time structure.
+
+ @see iso9660_dtime
+ */
+struct iso9660_dtime {
+ uint8_t dt_year;
+ uint8_t dt_month; /**< Has value in range 1..12. Note starts
+ at 1, not 0 like a tm struct. */
+ uint8_t dt_day;
+ uint8_t dt_hour;
+ uint8_t dt_minute;
+ uint8_t dt_second;
+ int8_t dt_gmtoff; /**< GMT values -48 .. + 52 in 15 minute
+ intervals */
+} GNUC_PACKED;
+
+typedef struct iso9660_dtime iso9660_dtime_t;
+
+/*!
+ \brief ISO-9660 longer-format time structure.
+
+ @see iso9660_ltime
+ */
+struct iso9660_ltime {
+ char lt_year [_delta( 1, 4)]; /**< Add 1900 to value
+ for the Julian
+ year */
+ char lt_month [_delta( 5, 6)]; /**< Has value in range
+ 1..12. Note starts
+ at 1, not 0 like a
+ tm struct. */
+ char lt_day [_delta( 7, 8)];
+ char lt_hour [_delta( 9, 10)];
+ char lt_minute [_delta( 11, 12)];
+ char lt_second [_delta( 13, 14)];
+ char lt_hsecond [_delta( 15, 16)]; /**<! The value is in
+ units of 1/100's of
+ a second */
+ int8_t lt_gmtoff [_delta( 17, 17)];
+} GNUC_PACKED;
+
+typedef struct iso9660_ltime iso9660_ltime_t;
+
+/*! \brief Format of an ISO-9660 directory record
+
+ This structure may have an odd length depending on how many
+ characters there are in the filename! Some compilers (e.g. on
+ Sun3/mc68020) pad the structures to an even length. For this reason,
+ we cannot use sizeof (struct iso_path_table) or sizeof (struct
+ iso_directory_record) to compute on disk sizes. Instead, we use
+ offsetof(..., name) and add the name size. See mkisofs.h of the
+ cdrtools package.
+
+ @see iso9660_stat
+*/
+struct iso9660_dir {
+ uint8_t length; /*! 711 encoded */
+ uint8_t xa_length; /*! 711 encoded */
+ uint64_t extent; /*! 733 encoded */
+ uint64_t size; /*! 733 encoded */
+ iso9660_dtime_t recording_time; /*! 7 711-encoded units */
+ uint8_t file_flags;
+ uint8_t file_unit_size; /*! 711 encoded */
+ uint8_t interleave_gap; /*! 711 encoded */
+ uint32_t volume_sequence_number; /*! 723 encoded */
+ uint8_t filename_len; /*! 711 encoded */
+ char filename[EMPTY_ARRAY_SIZE];
+} GNUC_PACKED;
+
+typedef struct iso9660_dir iso9660_dir_t;
+
+/*!
+ \brief ISO-9660 Primary Volume Descriptor.
+ */
+struct iso9660_pvd {
+ uint8_t type; /**< 711 encoded */
+ char id[5];
+ uint8_t version; /**< 711 encoded */
+ char unused1[1];
+ char system_id[ISO_MAX_SYSTEM_ID]; /**< each char is an achar */
+ char volume_id[ISO_MAX_VOLUME_ID]; /**< each char is a dchar */
+ char unused2[8];
+ uint64_t volume_space_size; /**< 733 encoded */
+ char unused3[32];
+ uint32_t volume_set_size; /**< 723 encoded */
+ uint32_t volume_sequence_number; /**< 723 encoded */
+ uint32_t logical_block_size; /**< 723 encoded */
+ uint64_t path_table_size; /**< 733 encoded */
+ uint32_t type_l_path_table; /**< 731 encoded */
+ uint32_t opt_type_l_path_table; /**< 731 encoded */
+ uint32_t type_m_path_table; /**< 732 encoded */
+ uint32_t opt_type_m_path_table; /**< 732 encoded */
+ iso9660_dir_t root_directory_record; /**< See section 9.1 of
+ ISO 9660 spec. */
+ char root_directory_filename; /**< Is \0 */
+ char volume_set_id[ISO_MAX_VOLUMESET_ID]; /**< dchars */
+ char publisher_id[ISO_MAX_PUBLISHER_ID]; /**< achars */
+ char preparer_id[ISO_MAX_PREPARER_ID]; /**< achars */
+ char application_id[ISO_MAX_APPLICATION_ID]; /**< achars */
+ char copyright_file_id[37]; /**< See section 7.5 of
+ ISO 9660 spec. Each char is
+ a dchar */
+ char abstract_file_id[37]; /**< See section 7.5 of
+ ISO 9660 spec. Each char is
+ a dchar */
+ char bibliographic_file_id[37]; /**< See section 7.5 of
+ ISO 9660 spec. Each char is
+ a dchar. */
+ iso9660_ltime_t creation_date; /**< See section 8.4.26.1 of
+ ISO 9660 spec. */
+ iso9660_ltime_t modification_date; /**< See section 8.4.26.1 of
+ ISO 9660 spec. */
+ iso9660_ltime_t expiration_date; /**< See section 8.4.26.1 of
+ ISO 9660 spec. */
+ iso9660_ltime_t effective_date; /**< See section 8.4.26.1 of
+ ISO 9660 spec. */
+ uint8_t file_structure_version; /**< 711 encoded */
+ char unused4[1];
+ char application_data[512];
+ char unused5[653];
+} GNUC_PACKED;
+
+typedef struct iso9660_pvd iso9660_pvd_t;
+
+/*!
+ \brief ISO-9660 Supplementary Volume Descriptor.
+
+ This is used for Joliet Extentions and is almost the same as the
+ the primary descriptor but two unused fields, "unused1" and "unused3
+ become "flags and "escape_sequences" respectively.
+*/
+struct iso9660_svd {
+ uint8_t type; /**< 711 encoded */
+ char id[5];
+ uint8_t version; /**< 711 encoded */
+ char flags; /**< 853 */
+ char system_id[ISO_MAX_SYSTEM_ID]; /**< each char is an achar */
+ char volume_id[ISO_MAX_VOLUME_ID]; /**< each char is a dchar */
+ char unused2[8];
+ uint64_t volume_space_size; /**< 733 encoded */
+ char escape_sequences[32]; /**< 856 */
+ uint32_t volume_set_size; /**< 723 encoded */
+ uint32_t volume_sequence_number; /**< 723 encoded */
+ uint32_t logical_block_size; /**< 723 encoded */
+ uint64_t path_table_size; /**< 733 encoded */
+ uint32_t type_l_path_table; /**< 731 encoded */
+ uint32_t opt_type_l_path_table; /**< 731 encoded */
+ uint32_t type_m_path_table; /**< 732 encoded */
+ uint32_t opt_type_m_path_table; /**< 732 encoded */
+ iso9660_dir_t root_directory_record; /**< See section 9.1 of
+ ISO 9660 spec. */
+ char volume_set_id[ISO_MAX_VOLUMESET_ID]; /**< dchars */
+ char publisher_id[ISO_MAX_PUBLISHER_ID]; /**< achars */
+ char preparer_id[ISO_MAX_PREPARER_ID]; /**< achars */
+ char application_id[ISO_MAX_APPLICATION_ID]; /**< achars */
+ char copyright_file_id[37]; /**< See section 7.5 of
+ ISO 9660 spec. Each char is
+ a dchar */
+ char abstract_file_id[37]; /**< See section 7.5 of
+ ISO 9660 spec. Each char is
+ a dchar */
+ char bibliographic_file_id[37]; /**< See section 7.5 of
+ ISO 9660 spec. Each char is
+ a dchar. */
+ iso9660_ltime_t creation_date; /**< See section 8.4.26.1 of
+ ISO 9660 spec. */
+ iso9660_ltime_t modification_date; /**< See section 8.4.26.1 of
+ ISO 9660 spec. */
+ iso9660_ltime_t expiration_date; /**< See section 8.4.26.1 of
+ ISO 9660 spec. */
+ iso9660_ltime_t effective_date; /**< See section 8.4.26.1 of
+ ISO 9660 spec. */
+ uint8_t file_structure_version; /**< 711 encoded */
+ char unused4[1];
+ char application_data[512];
+ char unused5[653];
+} GNUC_PACKED;
+
+typedef struct iso9660_svd iso9660_svd_t;
+
+PRAGMA_END_PACKED
+
+/*! \brief Unix stat-like version of iso9660_dir
+
+ The iso9660_stat structure is not part of the ISO-9660
+ specification. We use it for our to communicate information
+ in a C-library friendly way, e.g struct tm time structures and
+ a C-style filename string.
+
+ @see iso9660_dir
+*/
+struct iso9660_stat { /* big endian!! */
+ struct tm tm; /**< time on entry */
+ lsn_t lsn; /**< start logical sector number */
+ uint32_t size; /**< total size in bytes */
+ uint32_t secsize; /**< number of sectors allocated */
+ iso9660_xa_t xa; /**< XA attributes */
+ enum { _STAT_FILE = 1, _STAT_DIR = 2 } type;
+ char filename[EMPTY_ARRAY_SIZE]; /**< filename */
+};
+
+typedef struct iso9660_stat iso9660_stat_t;
+
+
+/** A mask used in iso9660_ifs_read_vd which allows what kinds
+ of extensions we allow, eg. Joliet, Rock Ridge, etc. */
+typedef uint8_t iso_extension_mask_t;
+
+#define ISO_EXTENSION_JOLIET_LEVEL1 0x01
+#define ISO_EXTENSION_JOLIET_LEVEL2 0x02
+#define ISO_EXTENSION_JOLIET_LEVEL3 0x04
+#define ISO_EXTENSION_ROCK_RIDGE 0x08
+#define ISO_EXTENSION_HIGH_SIERRA 0x10
+
+#define ISO_EXTENSION_ALL 0xFF
+#define ISO_EXTENSION_NONE 0x00
+#define ISO_EXTENSION_JOLIET \
+ (ISO_EXTENSION_JOLIET_LEVEL1 | \
+ ISO_EXTENSION_JOLIET_LEVEL2 | \
+ ISO_EXTENSION_JOLIET_LEVEL3 )
+
+
+/** This is an opaque structure. */
+typedef struct _iso9660 iso9660_t;
+
+/*!
+ Open an ISO 9660 image for reading. Maybe in the future we will have
+ a mode. NULL is returned on error.
+*/
+ iso9660_t *iso9660_open (const char *psz_pathname /*flags, mode */);
+
+/*!
+ Open an ISO 9660 image for reading allowing various ISO 9660
+ extensions. Maybe in the future we will have a mode. NULL is
+ returned on error.
+*/
+ iso9660_t *iso9660_open_ext (const char *psz_pathname,
+ iso_extension_mask_t iso_extension_mask);
+
+/*!
+ Close previously opened ISO 9660 image.
+ True is unconditionally returned. If there was an error false would
+ be returned.
+*/
+ bool iso9660_close (iso9660_t * p_iso);
+
+
+/*!
+ Seek to a position and then read n bytes. Size read is returned.
+*/
+ long int iso9660_iso_seek_read (const iso9660_t *p_iso, void *ptr,
+ lsn_t start, long int i_size);
+
+/*!
+ Read the Primary Volume Descriptor for a CD.
+ True is returned if read, and false if there was an error.
+*/
+ bool iso9660_fs_read_pvd ( const CdIo *p_cdio,
+ /*out*/ iso9660_pvd_t *p_pvd );
+
+/*!
+ Read the Primary Volume Descriptor for an ISO 9660 image.
+ True is returned if read, and false if there was an error.
+*/
+ bool iso9660_ifs_read_pvd (const iso9660_t *p_iso,
+ /*out*/ iso9660_pvd_t *p_pvd);
+
+/*!
+ Read the Super block of an ISO 9660 image. This is the
+ Primary Volume Descriptor (PVD) and perhaps a Supplemental Volume
+ Descriptor if (Joliet) extensions are acceptable.
+*/
+ bool iso9660_fs_read_superblock (CdIo *p_cdio,
+ iso_extension_mask_t iso_extension_mask);
+
+/*!
+ Read the Supper block of an ISO 9660 image. This is the
+ Primary Volume Descriptor (PVD) and perhaps a Supplemental Volume
+ Descriptor if (Joliet) extensions are acceptable.
+*/
+ bool iso9660_ifs_read_superblock (iso9660_t *p_iso,
+ iso_extension_mask_t iso_extension_mask);
+
+
+/*====================================================
+ Time conversion
+ ====================================================*/
+/*!
+ Set time in format used in ISO 9660 directory index record
+ from a Unix time structure. */
+ void iso9660_set_dtime (const struct tm *tm,
+ /*out*/ iso9660_dtime_t *idr_date);
+
+
+/*!
+ Set "long" time in format used in ISO 9660 primary volume descriptor
+ from a Unix time structure. */
+ void iso9660_set_ltime (const struct tm *_tm,
+ /*out*/ iso9660_ltime_t *p_pvd_date);
+
+/*!
+ Get Unix time structure from format use in an ISO 9660 directory index
+ record. Even though tm_wday and tm_yday fields are not explicitly in
+ idr_date, they are calculated from the other fields.
+
+ If tm is to reflect the localtime, set "use_localtime" true, otherwise
+ tm will reported in GMT.
+*/
+ void iso9660_get_dtime (const iso9660_dtime_t *idr_date, bool use_localtime,
+ /*out*/ struct tm *tm);
+
+
+/*====================================================
+ Characters used in file and directory and manipulation
+ ====================================================*/
+/*!
+ Return true if c is a DCHAR - a character that can appear in an an
+ ISO-9600 level 1 directory name. These are the ASCII capital
+ letters A-Z, the digits 0-9 and an underscore.
+*/
+bool iso9660_isdchar (int c);
+
+/*!
+ Return true if c is an ACHAR -
+ These are the DCHAR's plus some ASCII symbols including the space
+ symbol.
+*/
+bool iso9660_isachar (int c);
+
+/*!
+ Convert ISO-9660 file name that stored in a directory entry into
+ what's usually listed as the file name in a listing.
+ Lowercase name, and remove trailing ;1's or .;1's and
+ turn the other ;'s into version numbers.
+
+ The length of the translated string is returned.
+*/
+int iso9660_name_translate(const char *psz_oldname, char *psz_newname);
+
+/*!
+ Convert ISO-9660 file name that stored in a directory entry into
+ what's usually listed as the file name in a listing. Lowercase
+ name if not using Joliet extension. Remove trailing ;1's or .;1's and
+ turn the other ;'s into version numbers.
+
+ The length of the translated string is returned.
+*/
+int iso9660_name_translate_ext(const char *old, char *new,
+ uint8_t i_joliet_level);
+
+/*!
+ Pad string src with spaces to size len and copy this to dst. If
+ len is less than the length of src, dst will be truncated to the
+ first len characters of src.
+
+ src can also be scanned to see if it contains only ACHARs, DCHARs,
+ 7-bit ASCII chars depending on the enumeration _check.
+
+ In addition to getting changed, dst is the return value.
+ Note: this string might not be NULL terminated.
+ */
+char *iso9660_strncpy_pad(char dst[], const char src[], size_t len,
+ enum strncpy_pad_check _check);
+
+/*=====================================================================
+ file/dirname's
+======================================================================*/
+
+/*!
+ Check that pathname is a valid ISO-9660 directory name.
+
+ A valid directory name should not start out with a slash (/),
+ dot (.) or null byte, should be less than 37 characters long,
+ have no more than 8 characters in a directory component
+ which is separated by a /, and consist of only DCHARs.
+
+ True is returned if pathname is valid.
+ */
+bool iso9660_dirname_valid_p (const char pathname[]);
+
+/*!
+ Take pathname and a version number and turn that into a ISO-9660
+ pathname. (That's just the pathname followd by ";" and the version
+ number. For example, mydir/file.ext -> MYDIR/FILE.EXT;1 for version
+ 1. The resulting ISO-9660 pathname is returned.
+*/
+char *iso9660_pathname_isofy (const char pathname[], uint16_t i_version);
+
+/*!
+ Check that pathname is a valid ISO-9660 pathname.
+
+ A valid pathname contains a valid directory name, if one appears and
+ the filename portion should be no more than 8 characters for the
+ file prefix and 3 characters in the extension (or portion after a
+ dot). There should be exactly one dot somewhere in the filename
+ portion and the filename should be composed of only DCHARs.
+
+ True is returned if pathname is valid.
+ */
+bool iso9660_pathname_valid_p (const char pathname[]);
+
+/*=====================================================================
+ directory tree
+======================================================================*/
+
+void
+iso9660_dir_init_new (void *dir, uint32_t self, uint32_t ssize,
+ uint32_t parent, uint32_t psize,
+ const time_t *dir_time);
+
+void
+iso9660_dir_init_new_su (void *dir, uint32_t self, uint32_t ssize,
+ const void *ssu_data, unsigned int ssu_size,
+ uint32_t parent, uint32_t psize,
+ const void *psu_data, unsigned int psu_size,
+ const time_t *dir_time);
+
+void
+iso9660_dir_add_entry_su (void *dir, const char filename[], uint32_t extent,
+ uint32_t size, uint8_t file_flags,
+ const void *su_data,
+ unsigned int su_size, const time_t *entry_time);
+
+unsigned int
+iso9660_dir_calc_record_size (unsigned int namelen, unsigned int su_len);
+
+/*!
+ Given a directory pointer, find the filesystem entry that contains
+ lsn and return information about it.
+
+ Returns stat_t of entry if we found lsn, or NULL otherwise.
+ */
+iso9660_stat_t *iso9660_find_fs_lsn(CdIo *p_cdio, lsn_t i_lsn);
+
+
+/*!
+ Given a directory pointer, find the filesystem entry that contains
+ lsn and return information about it.
+
+ Returns stat_t of entry if we found lsn, or NULL otherwise.
+ */
+iso9660_stat_t *iso9660_find_ifs_lsn(const iso9660_t *p_iso, lsn_t i_lsn);
+
+
+/*!
+ Get file status for pathname into stat. NULL is returned on error.
+ */
+iso9660_stat_t *iso9660_fs_stat (CdIo *p_cdio, const char pathname[]);
+
+
+/*!
+ Get file status for pathname into stat. NULL is returned on error.
+ pathname version numbers in the ISO 9660
+ name are dropped, i.e. ;1 is removed and if level 1 ISO-9660 names
+ are lowercased.
+ */
+iso9660_stat_t *iso9660_fs_stat_translate (CdIo *p_cdio,
+ const char pathname[],
+ bool b_mode2);
+
+/*!
+ Get file status for pathname into stat. NULL is returned on error.
+ */
+iso9660_stat_t *iso9660_ifs_stat (iso9660_t *p_iso, const char pathname[]);
+
+
+/*!
+ Get file status for pathname into stat. NULL is returned on error.
+ pathname version numbers in the ISO 9660
+ name are dropped, i.e. ;1 is removed and if level 1 ISO-9660 names
+ are lowercased.
+ */
+iso9660_stat_t *iso9660_ifs_stat_translate (iso9660_t *p_iso,
+ const char pathname[]);
+
+/*!
+ Read pathname (a directory) and return a list of iso9660_stat_t
+ of the files inside that. The caller must free the returned result.
+*/
+CdioList * iso9660_fs_readdir (CdIo *p_cdio, const char pathname[],
+ bool b_mode2);
+
+/*!
+ Read pathname (a directory) and return a list of iso9660_stat_t
+ of the files inside that. The caller must free the returned result.
+*/
+CdioList * iso9660_ifs_readdir (iso9660_t *p_iso, const char pathname[]);
+
+/*!
+ Return the PVD's application ID.
+ NULL is returned if there is some problem in getting this.
+*/
+char * iso9660_get_application_id(iso9660_pvd_t *p_pvd);
+
+/*!
+ Get the application ID. psz_app_id is set to NULL if there
+ is some problem in getting this and false is returned.
+*/
+bool iso9660_ifs_get_application_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_app_id);
+
+/*!
+ Return the Joliet level recognized for p_iso.
+*/
+uint8_t iso9660_ifs_get_joliet_level(iso9660_t *p_iso);
+
+uint8_t iso9660_get_dir_len(const iso9660_dir_t *p_idr);
+
+#if FIXME
+uint8_t iso9660_get_dir_size(const iso9660_dir_t *p_idr);
+
+lsn_t iso9660_get_dir_extent(const iso9660_dir_t *p_idr);
+#endif
+
+/*!
+ Return the directory name stored in the iso9660_dir_t
+
+ A string is allocated: the caller must deallocate.
+*/
+char * iso9660_dir_to_name (const iso9660_dir_t *p_iso9660_dir);
+
+/*!
+ Return a string containing the preparer id with trailing
+ blanks removed.
+*/
+char *iso9660_get_preparer_id(const iso9660_pvd_t *p_pvd);
+
+/*!
+ Get the preparer ID. psz_preparer_id is set to NULL if there
+ is some problem in getting this and false is returned.
+*/
+bool iso9660_ifs_get_preparer_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_preparer_id);
+
+/*!
+ Return a string containing the PVD's publisher id with trailing
+ blanks removed.
+*/
+char *iso9660_get_publisher_id(const iso9660_pvd_t *p_pvd);
+
+/*!
+ Get the publisher ID. psz_publisher_id is set to NULL if there
+ is some problem in getting this and false is returned.
+*/
+bool iso9660_ifs_get_publisher_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_publisher_id);
+
+uint8_t iso9660_get_pvd_type(const iso9660_pvd_t *p_pvd);
+
+const char * iso9660_get_pvd_id(const iso9660_pvd_t *p_pvd);
+
+int iso9660_get_pvd_space_size(const iso9660_pvd_t *p_pvd);
+
+int iso9660_get_pvd_block_size(const iso9660_pvd_t *p_pvd) ;
+
+/*! Return the primary volume id version number (of pvd).
+ If there is an error 0 is returned.
+ */
+int iso9660_get_pvd_version(const iso9660_pvd_t *pvd) ;
+
+/*!
+ Return a string containing the PVD's system id with trailing
+ blanks removed.
+*/
+char *iso9660_get_system_id(const iso9660_pvd_t *p_pvd);
+
+/*!
+ Get the system ID. psz_system_id is set to NULL if there
+ is some problem in getting this and false is returned.
+*/
+bool iso9660_ifs_get_system_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_system_id);
+
+
+/*! Return the LSN of the root directory for pvd.
+ If there is an error CDIO_INVALID_LSN is returned.
+ */
+lsn_t iso9660_get_root_lsn(const iso9660_pvd_t *p_pvd);
+
+/*!
+ Return the PVD's volume ID.
+*/
+char *iso9660_get_volume_id(const iso9660_pvd_t *p_pvd);
+
+/*!
+ Get the system ID. psz_system_id is set to NULL if there
+ is some problem in getting this and false is returned.
+*/
+bool iso9660_ifs_get_volume_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_volume_id);
+
+/*!
+ Return the PVD's volumeset ID.
+ NULL is returned if there is some problem in getting this.
+*/
+char *iso9660_get_volumeset_id(const iso9660_pvd_t *p_pvd);
+
+/*!
+ Get the systemset ID. psz_systemset_id is set to NULL if there
+ is some problem in getting this and false is returned.
+*/
+bool iso9660_ifs_get_volumeset_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_volumeset_id);
+
+/* pathtable */
+
+/*! Zero's out pathable. Do this first. */
+void iso9660_pathtable_init (void *pt);
+
+unsigned int iso9660_pathtable_get_size (const void *pt);
+
+uint16_t
+iso9660_pathtable_l_add_entry (void *pt, const char name[], uint32_t extent,
+ uint16_t parent);
+
+uint16_t
+iso9660_pathtable_m_add_entry (void *pt, const char name[], uint32_t extent,
+ uint16_t parent);
+
+/*=====================================================================
+ Volume Descriptors
+======================================================================*/
+
+void
+iso9660_set_pvd (void *pd, const char volume_id[], const char application_id[],
+ const char publisher_id[], const char preparer_id[],
+ uint32_t iso_size, const void *root_dir,
+ uint32_t path_table_l_extent, uint32_t path_table_m_extent,
+ uint32_t path_table_size, const time_t *pvd_time);
+
+void
+iso9660_set_evd (void *pd);
+
+/*!
+ Return true if ISO 9660 image has extended attrributes (XA).
+*/
+bool iso9660_ifs_is_xa (const iso9660_t * p_iso);
+
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_ISO9660_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/logging.h b/contrib/libcdio/cdio/logging.h
new file mode 100644
index 000000000..8c78259ea
--- /dev/null
+++ b/contrib/libcdio/cdio/logging.h
@@ -0,0 +1,137 @@
+/*
+ $Id: logging.h,v 1.3 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2000, Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/** \file logging.h
+ * \brief Header to control logging and level of detail of output.
+ *
+ */
+
+#ifndef __LOGGING_H__
+#define __LOGGING_H__
+
+#include <cdio/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * The different log levels supported.
+ */
+typedef enum {
+ CDIO_LOG_DEBUG = 1, /**< Debug-level messages - helps debug what's up. */
+ CDIO_LOG_INFO, /**< Informational - indicates perhaps something of
+ interest. */
+ CDIO_LOG_WARN, /**< Warning conditions - something that looks funny. */
+ CDIO_LOG_ERROR, /**< Error conditions - may terminate program. */
+ CDIO_LOG_ASSERT /**< Critical conditions - may abort program. */
+} cdio_log_level_t;
+
+/**
+ * The place to save the preference concerning how much verbosity
+ * is desired. This is used by the internal default log handler, but
+ * it could be use by applications which provide their own log handler.
+ */
+extern cdio_log_level_t cdio_loglevel_default;
+
+/**
+ * This type defines the signature of a log handler. For every
+ * message being logged, the handler will receive the log level and
+ * the message string.
+ *
+ * @see cdio_log_set_handler
+ * @see cdio_log_level_t
+ *
+ * @param level The log level.
+ * @param message The log message.
+ */
+typedef void (*cdio_log_handler_t) (cdio_log_level_t level,
+ const char message[]);
+
+/**
+ * Set a custom log handler for libcdio. The return value is the log
+ * handler being replaced. If the provided parameter is NULL, then
+ * the handler will be reset to the default handler.
+ *
+ * @see cdio_log_handler_t
+ *
+ * @param new_handler The new log handler.
+ * @return The previous log handler.
+ */
+cdio_log_handler_t cdio_log_set_handler (cdio_log_handler_t new_handler);
+
+/**
+ * Handle an message with the given log level.
+ *
+ * @see cdio_debug
+ * @see cdio_info
+ * @see cdio_warn
+ * @see cdio_error
+
+ * @param level The log level.
+ * @param format printf-style format string
+ * @param ... remaining arguments needed by format string
+ */
+void cdio_log (cdio_log_level_t level,
+ const char format[], ...) GNUC_PRINTF(2, 3);
+
+/**
+ * Handle a debugging message.
+ *
+ * @see cdio_log for a more generic routine
+ */
+void cdio_debug (const char format[], ...) GNUC_PRINTF(1,2);
+
+/**
+ * Handle an informative message.
+ *
+ * @see cdio_log for a more generic routine
+ */
+void cdio_info (const char format[], ...) GNUC_PRINTF(1,2);
+
+/**
+ * Handle a warning message.
+ *
+ * @see cdio_log for a more generic routine
+ */
+void cdio_warn (const char format[], ...) GNUC_PRINTF(1,2);
+
+/**
+ * Handle an error message. Execution is terminated.
+ *
+ * @see cdio_log for a more generic routine.
+ */
+void cdio_error (const char format[], ...) GNUC_PRINTF(1,2);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __LOGGING_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/scsi_mmc.h b/contrib/libcdio/cdio/scsi_mmc.h
new file mode 100644
index 000000000..12860247e
--- /dev/null
+++ b/contrib/libcdio/cdio/scsi_mmc.h
@@ -0,0 +1,415 @@
+/*
+ $Id: scsi_mmc.h,v 1.1 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*!
+ \file scsi_mmc.h
+ \brief Common definitions for SCSI MMC (Multi-Media Commands).
+*/
+
+#ifndef __SCSI_MMC_H__
+#define __SCSI_MMC_H__
+
+#include <cdio/cdio.h>
+#include <cdio/types.h>
+#include <cdio/dvd.h>
+
+/*! The generic packet command opcodes for CD/DVD Logical Units. */
+
+#define CDIO_MMC_GPCMD_INQUIRY 0x12
+#define CDIO_MMC_GPCMD_MODE_SELECT_6 0x15
+#define CDIO_MMC_GPCMD_MODE_SENSE 0x1a
+#define CDIO_MMC_GPCMD_START_STOP 0x1b
+#define CDIO_MMC_GPCMD_ALLOW_MEDIUM_REMOVAL 0x1e
+#define CDIO_MMC_GPCMD_READ_10 0x28
+
+/*!
+ Group 2 Commands
+ */
+#define CDIO_MMC_GPCMD_READ_SUBCHANNEL 0x42
+#define CDIO_MMC_GPCMD_READ_TOC 0x43
+#define CDIO_MMC_GPCMD_READ_HEADER 0x44
+#define CDIO_MMC_GPCMD_PLAY_AUDIO_10 0x45
+#define CDIO_MMC_GPCMD_GET_CONFIGURATION 0x46
+#define CDIO_MMC_GPCMD_PLAY_AUDIO_MSF 0x47
+#define CDIO_MMC_GPCMD_PLAY_AUDIO_TI 0x48
+#define CDIO_MMC_GPCMD_PLAY_TRACK_REL_10 0x49
+#define CDIO_MMC_GPCMD_PAUSE_RESUME 0x4b
+
+#define CDIO_MMC_GPCMD_READ_DISC_INFO 0x51
+#define CDIO_MMC_GPCMD_MODE_SELECT 0x55
+#define CDIO_MMC_GPCMD_MODE_SENSE_10 0x5a
+
+/*!
+ Group 5 Commands
+ */
+#define CDIO_MMC_GPCMD_PLAY_AUDIO_12 0xa5
+#define CDIO_MMC_GPCMD_READ_12 0xa8
+#define CDIO_MMC_GPCMD_PLAY_TRACK_REL_12 0xa9
+#define CDIO_MMC_GPCMD_READ_DVD_STRUCTURE 0xad
+#define CDIO_MMC_GPCMD_READ_CD 0xbe
+#define CDIO_MMC_GPCMD_READ_MSF 0xb9
+
+/*!
+ Group 6 Commands
+ */
+
+#define CDIO_MMC_GPCMD_CD_PLAYBACK_STATUS 0xc4 /**< SONY unique command */
+#define CDIO_MMC_GPCMD_PLAYBACK_CONTROL 0xc9 /**< SONY unique command */
+#define CDIO_MMC_GPCMD_READ_CDDA 0xd8 /**< Vendor unique command */
+#define CDIO_MMC_GPCMD_READ_CDXA 0xdb /**< Vendor unique command */
+#define CDIO_MMC_GPCMD_READ_ALL_SUBCODES 0xdf /**< Vendor unique command */
+
+
+
+/*! Level values that can go into READ_CD */
+#define CDIO_MMC_READ_TYPE_ANY 0 /**< All types */
+#define CDIO_MMC_READ_TYPE_CDDA 1 /**< Only CD-DA sectors */
+#define CDIO_MMC_READ_TYPE_MODE1 2 /**< mode1 sectors (user data = 2048) */
+#define CDIO_MMC_READ_TYPE_MODE2 3 /**< mode2 sectors form1 or form2 */
+#define CDIO_MMC_READ_TYPE_M2F1 4 /**< mode2 sectors form1 */
+#define CDIO_MMC_READ_TYPE_M2F2 5 /**< mode2 sectors form2 */
+
+/*! Format values for READ_TOC */
+#define CDIO_MMC_READTOC_FMT_TOC 0
+#define CDIO_MMC_READTOC_FMT_SESSION 1
+#define CDIO_MMC_READTOC_FMT_FULTOC 2
+#define CDIO_MMC_READTOC_FMT_PMA 3 /**< Q subcode data */
+#define CDIO_MMC_READTOC_FMT_ATIP 4 /**< includes media type */
+#define CDIO_MMC_READTOC_FMT_CDTEXT 5 /**< CD-TEXT info */
+
+/*! Page codes for MODE SENSE and MODE SET. */
+#define CDIO_MMC_R_W_ERROR_PAGE 0x01
+#define CDIO_MMC_WRITE_PARMS_PAGE 0x05
+#define CDIO_MMC_AUDIO_CTL_PAGE 0x0e
+#define CDIO_MMC_CDR_PARMS_PAGE 0x0d
+#define CDIO_MMC_POWER_PAGE 0x1a
+#define CDIO_MMC_FAULT_FAIL_PAGE 0x1c
+#define CDIO_MMC_TO_PROTECT_PAGE 0x1d
+#define CDIO_MMC_CAPABILITIES_PAGE 0x2a
+#define CDIO_MMC_ALL_PAGES 0x3f
+
+/*! Return type codes for GET_CONFIGURATION. */
+#define CDIO_MMC_GET_CONF_ALL_FEATURES 0 /**< all features without regard
+ to currency. */
+#define CDIO_MMC_GET_CONF_CURRENT_FEATURES 1 /**< features which are currently
+ in effect (e.g. based on
+ medium inserted). */
+#define CDIO_MMC_GET_CONF_NAMED_FEATURE 2 /**< just the feature named in
+ the GET_CONFIGURATION
+ cdb. */
+
+/*! FEATURE codes used in GET CONFIGURATION. */
+
+#define CDIO_MMC_FEATURE_PROFILE_LIST 0x000 /**< Profile List Feature */
+#define CDIO_MMC_FEATURE_CORE 0x001
+#define CDIO_MMC_FEATURE_REMOVABLE_MEDIUM 0x002 /**< Removable Medium
+ Feature */
+#define CDIO_MMC_FEATURE_WRITE_PROTECT 0x003 /**< Write Protect
+ Feature */
+#define CDIO_MMC_FEATURE_RANDOM_READABLE 0x010 /**< Random Readable
+ Feature */
+#define CDIO_MMC_FEATURE_MULTI_READ 0x01D /**< Multi-Read
+ Feature */
+#define CDIO_MMC_FEATURE_CD_READ 0x01E /**< CD Read
+ Feature */
+#define CDIO_MMC_FEATURE_DVD_READ 0x01F /**< DVD Read
+ Feature */
+#define CDIO_MMC_FEATURE_RANDOM_WRITABLE 0x020 /**< Random Writable
+ Feature */
+#define CDIO_MMC_FEATURE_INCR_WRITE 0x021 /**< Incremental
+ Streaming Writable
+ Feature */
+#define CDIO_MMC_FEATURE_SECTOR_ERASE 0x022 /**< Sector Erasable
+ Feature */
+#define CDIO_MMC_FEATURE_FORMATABLE 0x023 /**< Formattable
+ Feature */
+#define CDIO_MMC_FEATURE_DEFECT_MGMT 0x024 /**< Management
+ Ability of the
+ Logical Unit/media
+ system to provide
+ an apparently
+ defect-free
+ space.*/
+#define CDIO_MMC_FEATURE_WRITE_ONCE 0x025 /**< Write Once
+ Feature */
+#define CDIO_MMC_FEATURE_RESTRICT_OVERW 0x026 /**< Restricted
+ Overwrite
+ Feature */
+#define CDIO_MMC_FEATURE_CD_RW_CAV 0x027 /**< CD-RW CAV Write
+ Feature */
+#define CDIO_MMC_FEATURE_MRW 0x028 /**< MRW Feature */
+#define CDIO_MMC_FEATURE_DVD_PRW 0x02A /**< DVD+RW Feature */
+#define CDIO_MMC_FEATURE_DVD_PR 0x02B /**< DVD+R Feature */
+#define CDIO_MMC_FEATURE_CD_TAO 0x02D
+#define CDIO_MMC_FEATURE_CD_SAO 0x02E
+#define CDIO_MMC_FEATURE_POWER_MGMT 0x100 /**< Initiator and
+ device directed
+ power management */
+#define CDIO_MMC_FEATURE_CDDA_EXT_PLAY 0x103 /**< Ability to play
+ audio CDs via the
+ Logical Unit s own
+ analog output */
+#define CDIO_MMC_FEATURE_MCODE_UPGRADE 0x104 /* Ability for the
+ device to accept
+ new microcode via
+ the interface */
+#define CDIO_MMC_FEATURE_TIME_OUT 0x105 /**< Ability to
+ respond to all
+ commands within a
+ specific time */
+#define CDIO_MMC_FEATURE_DVD_CSS 0x106 /**< Ability to
+ perform DVD
+ CSS/CPPM
+ authentication and
+ RPC */
+#define CDIO_MMC_FEATURE_RT_STREAMING 0x107 /**< Ability to read
+ and write using
+ Initiator requested
+ performance
+ parameters
+ */
+#define CDIO_MMC_FEATURE_LU_SN 0x108 /**< The Logical Unit
+ has a unique
+ identifier. */
+#define CDIO_MMC_FEATURE_FIRMWARE_DATE 0x1FF /**< Firmware creation
+ date report */
+
+/*! Profile codes used in GET_CONFIGURATION - PROFILE LIST. */
+#define CDIO_MMC_FEATURE_PROF_NON_REMOVABLE 0x0001 /**< Re-writable
+ disk, capable of
+ changing
+ behavior */
+#define CDIO_MMC_FEATURE_PROF_REMOVABLE 0x0002 /**< disk
+ Re-writable;
+ with removable
+ media */
+#define CDIO_MMC_FEATURE_PROF_MO_ERASABLE 0x0003 /**< Erasable
+ Magneto-Optical
+ disk with sector
+ erase
+ capability */
+#define CDIO_MMC_FEATURE_PROF_MO_WRITE_ONCE 0x0004 /**< Write Once
+ Magneto-Optical
+ write once */
+#define CDIO_MMC_FEATURE_PROF_AS_MO 0x0005 /**< Advance
+ Storage
+ Magneto-Optical */
+#define CDIO_MMC_FEATURE_PROF_CD_ROM 0x0008 /**< Read only
+ Compact Disc
+ capable */
+#define CDIO_MMC_FEATURE_PROF_CD_R 0x0009 /**< Write once
+ Compact Disc
+ capable */
+#define CDIO_MMC_FEATURE_PROF_CD_RW 0x000A /**< CD-RW
+ Re-writable
+ Compact Disc
+ capable */
+#define CDIO_MMC_FEATURE_PROF_DVD_ROM 0x0010 /**< Read only
+ DVD */
+#define CDIO_MMC_FEATURE_PROF_DVD_R_SEQ 0x0011 /**< Re-recordable
+ DVD using
+ Sequential
+ recording */
+#define CDIO_MMC_FEATURE_PROF_DVD_RAM 0x0012 /**< Re-writable
+ DVD */
+#define CDIO_MMC_FEATURE_PROF_DVD_RW_RO 0x0013 /**< Re-recordable
+ DVD using
+ Restricted
+ Overwrite */
+#define CDIO_MMC_FEATURE_PROF_DVD_RW_SEQ 0x0014 /**< Re-recordable
+ DVD using
+ Sequential
+ recording */
+#define CDIO_MMC_FEATURE_PROF_DVD_PRW 0x001A /**< DVD+RW - DVD
+ ReWritable */
+#define CDIO_MMC_FEATURE_PROF_DVD_PR 0x001B /**< DVD+R - DVD
+ Recordable */
+#define CDIO_MMC_FEATURE_PROF_DDCD_ROM 0x0020 /**< Read only
+ DDCD */
+#define CDIO_MMC_FEATURE_PROF_DDCD_R 0x0021 /**< DDCD-R Write
+ only DDCD */
+#define CDIO_MMC_FEATURE_PROF_DDCD_RW 0x0022 /**< Re-Write only
+ DDCD */
+#define CDIO_MMC_FEATURE_PROF_NON_CONFORM 0xFFFF /**< The Logical
+ Unit does not
+ conform to any
+ Profile. */
+
+/*! This is listed as optional in ATAPI 2.6, but is (curiously)
+ missing from Mt. Fuji, Table 57. It _is_ mentioned in Mt. Fuji
+ Table 377 as an MMC command for SCSi devices though... Most ATAPI
+ drives support it. */
+#define CDIO_MMC_GPCMD_SET_SPEED 0xbb
+
+
+/*! The largest Command Descriptor Buffer (CDB) size.
+ The possible sizes are 6, 10, and 12 bytes.
+ */
+#define MAX_CDB_LEN 12
+
+/*! \brief A Command Descriptor Buffer (CDB) used in sending SCSI MMC
+ commands.
+ */
+typedef struct scsi_mmc_cdb {
+ uint8_t field[MAX_CDB_LEN];
+} scsi_mmc_cdb_t;
+
+/*! \brief Format of header block in data returned from a SCSI-MMC
+ GET_CONFIGURATION command.
+ */
+typedef struct scsi_mmc_feature_list_header {
+ unsigned char length_msb;
+ unsigned char length_1sb;
+ unsigned char length_2sb;
+ unsigned char length_lsb;
+ unsigned char reserved1;
+ unsigned char reserved2;
+ unsigned char profile_msb;
+ unsigned char profile_lsb;
+} scs_mmc_feature_list_header_t;
+
+/*! An enumeration indicating whether a SCSI MMC command is sending
+ data or getting data.
+ */
+typedef enum scsi_mmc_direction {
+ SCSI_MMC_DATA_READ,
+ SCSI_MMC_DATA_WRITE
+} scsi_mmc_direction_t;
+
+#define CDIO_MMC_SET_COMMAND(cdb, command) \
+ cdb[0] = command
+
+#define CDIO_MMC_SET_READ_TYPE(cdb, sector_type) \
+ cdb[1] = (sector_type << 2)
+
+#define CDIO_MMC_GET_LEN16(p) \
+ (p[0]<<8) + p[1]
+
+#define CDIO_MMC_GET_LEN32(p) \
+ (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
+
+#define CDIO_MMC_SET_LEN16(cdb, pos, len) \
+ cdb[pos ] = (len >> 8) & 0xff; \
+ cdb[pos+1] = (len ) & 0xff
+
+#define CDIO_MMC_SET_READ_LBA(cdb, lba) \
+ cdb[2] = (lba >> 24) & 0xff; \
+ cdb[3] = (lba >> 16) & 0xff; \
+ cdb[4] = (lba >> 8) & 0xff; \
+ cdb[5] = (lba ) & 0xff
+
+#define CDIO_MMC_SET_START_TRACK(cdb, command) \
+ cdb[6] = command
+
+#define CDIO_MMC_SET_READ_LENGTH24(cdb, len) \
+ cdb[6] = (len >> 16) & 0xff; \
+ cdb[7] = (len >> 8) & 0xff; \
+ cdb[8] = (len ) & 0xff
+
+#define CDIO_MMC_SET_READ_LENGTH16(cdb, len) \
+ CDIO_MMC_SET_LEN16(cdb, 7, len)
+
+#define CDIO_MMC_SET_READ_LENGTH8(cdb, len) \
+ cdb[8] = (len ) & 0xff
+
+#define CDIO_MMC_MCSB_ALL_HEADERS 0x78
+
+#define CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(cdb, val) \
+ cdb[9] = val;
+
+/*!
+ Return the number of length in bytes of the Command Descriptor
+ buffer (CDB) for a given SCSI MMC command. The length will be
+ either 6, 10, or 12.
+*/
+uint8_t scsi_mmc_get_cmd_len(uint8_t scsi_cmd);
+
+
+/*!
+ Run a SCSI MMC command.
+
+ cdio CD structure set by cdio_open().
+ i_timeout_ms time in milliseconds we will wait for the command
+ to complete.
+ p_cdb CDB bytes. All values that are needed should be set on
+ input. We'll figure out what the right CDB length should be.
+ e_direction direction the transfer is to go.
+ i_buf Size of buffer
+ p_buf Buffer for data, both sending and receiving.
+
+ Returns 0 if command completed successfully.
+ */
+int scsi_mmc_run_cmd( const CdIo *p_cdio, unsigned int i_timeout_ms,
+ const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction, unsigned int i_buf,
+ /*in/out*/ void *p_buf );
+
+/*!
+ * Eject using SCSI MMC commands. Return 0 if successful.
+ */
+int scsi_mmc_eject_media( const CdIo *p_cdio);
+
+/*! Packet driver to read mode2 sectors.
+ Can read only up to 25 blocks.
+*/
+int scsi_mmc_read_sectors ( const CdIo *p_cdio, void *p_buf, lba_t lba,
+ int sector_type, unsigned int nblocks);
+
+/*!
+ Set the block size for subsequest read requests, via a SCSI MMC
+ MODE_SELECT 6 command.
+ */
+int scsi_mmc_set_blocksize ( const CdIo *p_cdio, unsigned int bsize);
+
+/*!
+ Return the the kind of drive capabilities of device.
+ */
+void scsi_mmc_get_drive_cap (const CdIo *p_cdio,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap);
+
+/*!
+ Get the DVD type associated with cd object.
+*/
+discmode_t scsi_mmc_get_dvd_struct_physical ( const CdIo *p_cdio,
+ cdio_dvd_struct_t *s);
+
+/*!
+ Get the CD-ROM hardware info via a SCSI MMC INQUIRY command.
+ False is returned if we had an error getting the information.
+*/
+bool scsi_mmc_get_hwinfo ( const CdIo *p_cdio,
+ /* out*/ cdio_hwinfo_t *p_hw_info );
+
+
+/*!
+ Get the media catalog number (MCN) from the CD via MMC.
+
+ @return the media catalog number r NULL if there is none or we
+ don't have the ability to get it.
+
+ Note: string is malloc'd so caller has to free() the returned
+ string when done with it.
+
+*/
+char *scsi_mmc_get_mcn ( const CdIo *p_cdio );
+
+#endif /* __SCSI_MMC_H__ */
diff --git a/contrib/libcdio/cdio/sector.h b/contrib/libcdio/cdio/sector.h
new file mode 100644
index 000000000..826883aea
--- /dev/null
+++ b/contrib/libcdio/cdio/sector.h
@@ -0,0 +1,326 @@
+/*
+ $Id: sector.h,v 1.3 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*!
+ \file sector.h
+ \brief Things related to CD-ROM layout: tracks, sector sizes, MSFs, LBAs.
+
+ A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336,
+ 2340, or 2352 bytes long.
+
+ Sector types of the standard CD-ROM data formats:
+
+\verbatim
+ format sector type user data size (bytes)
+ -----------------------------------------------------------------------------
+ 1 (Red Book) CD-DA 2352 (CDIO_CD_FRAMESIZE_RAW)
+ 2 (Yellow Book) Mode1 Form1 2048 (CDIO_CD_FRAMESIZE)
+ 3 (Yellow Book) Mode1 Form2 2336 (M2RAW_SECTOR_SIZE)
+ 4 (Green Book) Mode2 Form1 2048 (CDIO_CD_FRAMESIZE)
+ 5 (Green Book) Mode2 Form2 2328 (2324+4 spare bytes)
+
+
+ The layout of the standard CD-ROM data formats:
+ -----------------------------------------------------------------------------
+ - audio (red): | audio_sample_bytes |
+ | 2352 |
+
+ - data (yellow, mode1): | sync - head - data - EDC - zero - ECC |
+ | 12 - 4 - 2048 - 4 - 8 - 276 |
+
+ - data (yellow, mode2): | sync - head - data |
+ | 12 - 4 - 2336 |
+
+ - XA data (green, mode2 form1): | sync - head - sub - data - EDC - ECC |
+ | 12 - 4 - 8 - 2048 - 4 - 276 |
+
+ - XA data (green, mode2 form2): | sync - head - sub - data - Spare |
+ | 12 - 4 - 8 - 2324 - 4 |
+\endverbatim
+
+
+*/
+
+#ifndef _CDIO_SECTOR_H_
+#define _CDIO_SECTOR_H_
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+#include <cdio/types.h>
+
+/*! track modes (Table 350)
+ reference: MMC-3 draft revsion - 10g
+*/
+typedef enum {
+ AUDIO, /**< 2352 byte block length */
+ MODE1, /**< 2048 byte block length */
+ MODE1_RAW, /**< 2352 byte block length */
+ MODE2, /**< 2336 byte block length */
+ MODE2_FORM1, /**< 2048 byte block length */
+ MODE2_FORM2, /**< 2324 byte block length */
+ MODE2_FORM_MIX, /**< 2336 byte block length */
+ MODE2_RAW /**< 2352 byte block length */
+} trackmode_t;
+
+/*! disc modes. The first combined from MMC-3 5.29.2.8 (Send CUESHEET)
+ and GNU/Linux /usr/include/linux/cdrom.h and we've added DVD.
+ */
+typedef enum {
+ CDIO_DISC_MODE_CD_DA, /**< CD-DA */
+ CDIO_DISC_MODE_CD_DATA, /**< CD-ROM form 1 */
+ CDIO_DISC_MODE_CD_XA, /**< CD-ROM XA form2 */
+ CDIO_DISC_MODE_CD_MIXED, /**< Some combo of above. */
+ CDIO_DISC_MODE_DVD_ROM, /**< DVD ROM (e.g. movies) */
+ CDIO_DISC_MODE_DVD_RAM, /**< DVD-RAM */
+ CDIO_DISC_MODE_DVD_R, /**< DVD-R */
+ CDIO_DISC_MODE_DVD_RW, /**< DVD-RW */
+ CDIO_DISC_MODE_DVD_PR, /**< DVD+R */
+ CDIO_DISC_MODE_DVD_PRW, /**< DVD+RW */
+ CDIO_DISC_MODE_DVD_OTHER, /**< Unknown/unclassified DVD type */
+ CDIO_DISC_MODE_NO_INFO,
+ CDIO_DISC_MODE_ERROR
+} discmode_t;
+
+/*! Information that can be obtained through a Read Subchannel
+ command.
+ */
+#define CDIO_SUBCHANNEL_SUBQ_DATA 0
+#define CDIO_SUBCHANNEL_CURRENT_POSITION 1
+#define CDIO_SUBCHANNEL_MEDIA_CATALOG 2
+#define CDIO_SUBCHANNEL_TRACK_ISRC 3
+
+/*! track flags
+ * Q Sub-channel Control Field (4.2.3.3)
+ */
+typedef enum {
+ NONE = 0x00, /* no flags set */
+ PRE_EMPHASIS = 0x01, /* audio track recorded with pre-emphasis */
+ COPY_PERMITTED = 0x02, /* digital copy permitted */
+ DATA = 0x04, /* data track */
+ FOUR_CHANNEL_AUDIO = 0x08, /* 4 audio channels */
+ SCMS = 0x10 /* SCMS (5.29.2.7) */
+} flag_t;
+
+#define CDIO_PREGAP_SECTORS 150
+#define CDIO_POSTGAP_SECTORS 150
+
+/*
+ Some generally useful CD-ROM information -- mostly based on the above.
+ This is from linux.h - not to slight other OS's. This was the first
+ place I came across such useful stuff.
+*/
+#define CDIO_CD_MINS 74 /**< max. minutes per CD, not really
+ a limit */
+#define CDIO_CD_SECS_PER_MIN 60 /**< seconds per minute */
+#define CDIO_CD_FRAMES_PER_SEC 75 /**< frames per second */
+#define CDIO_CD_SYNC_SIZE 12 /**< 12 sync bytes per raw data frame */
+#define CDIO_CD_CHUNK_SIZE 24 /**< lowest-level "data bytes piece" */
+#define CDIO_CD_NUM_OF_CHUNKS 98 /**< chunks per frame */
+#define CDIO_CD_FRAMESIZE_SUB 96 /**< subchannel data "frame" size */
+#define CDIO_CD_HEADER_SIZE 4 /**< header (address) bytes per raw
+ data frame */
+#define CDIO_CD_SUBHEADER_SIZE 8 /**< subheader bytes per raw XA data
+ frame */
+#define CDIO_CD_EDC_SIZE 4 /**< bytes EDC per most raw data
+ frame types */
+#define CDIO_CD_M1F1_ZERO_SIZE 8 /**< bytes zero per yellow book mode
+ 1 frame */
+#define CDIO_CD_ECC_SIZE 276 /**< bytes ECC per most raw data frame
+ types */
+#define CDIO_CD_FRAMESIZE 2048 /**< bytes per frame, "cooked" mode */
+#define CDIO_CD_FRAMESIZE_RAW 2352 /**< bytes per frame, "raw" mode */
+#define CDIO_CD_FRAMESIZE_RAWER 2646 /**< The maximum possible returned
+ bytes */
+#define CDIO_CD_FRAMESIZE_RAW1 (CDIO_CD_CD_FRAMESIZE_RAW-CDIO_CD_SYNC_SIZE) /*2340*/
+#define CDIO_CD_FRAMESIZE_RAW0 (CDIO_CD_FRAMESIZE_RAW-CDIO_CD_SYNC_SIZE-CDIO_CD__HEAD_SIZE) /*2336*/
+
+/*! "before data" part of raw XA (green, mode2) frame */
+#define CDIO_CD_XA_HEADER (CDIO_CD_HEADER_SIZE+CDIO_CD_SUBHEADER_SIZE)
+
+/*! "after data" part of raw XA (green, mode2 form1) frame */
+#define CDIO_CD_XA_TAIL (CDIO_CD_EDC_SIZE+CDIO_CD_ECC_SIZE)
+
+/*! "before data" sync bytes + header of XA (green, mode2) frame */
+#define CDIO_CD_XA_SYNC_HEADER (CDIO_CD_SYNC_SIZE+CDIO_CD_XA_HEADER)
+
+/*! CD-ROM address types (GNU/Linux e.g. cdrom_tocentry.cdte_format) */
+#define CDIO_CDROM_LBA 0x01 /**< "logical block": first frame is #0 */
+#define CDIO_CDROM_MSF 0x02 /**< "minute-second-frame": binary, not
+ BCD here! */
+
+/*! CD-ROM track format types (GNU/Linux cdte_ctrl) */
+#define CDIO_CDROM_DATA_TRACK 0x04
+#define CDIO_CDROM_CDI_TRACK 0x10
+#define CDIO_CDROM_XA_TRACK 0x20
+
+/*! The leadout track is always 0xAA, regardless of # of tracks on
+ disc, or what value may be used internally. For example although
+ OS X uses a different value for the lead-out track internally than
+ given below, programmers should use CDIO_CDROM_LEADOUT_TRACK and
+ not worry about this.
+ */
+#define CDIO_CDROM_LEADOUT_TRACK 0xAA
+
+#define M2F2_SECTOR_SIZE 2324
+#define M2SUB_SECTOR_SIZE 2332
+#define M2RAW_SECTOR_SIZE 2336
+
+/*! Largest CD track number */
+#define CDIO_CD_MAX_TRACKS 99
+/*! Smallest CD track number */
+#define CDIO_CD_MIN_TRACK_NO 1
+
+/*! Largest CD session number */
+#define CDIO_CD_MAX_SESSIONS 99
+/*! Smallest CD session number */
+#define CDIO_CD_MIN_SESSION_NO 1
+
+/*! Largest LSN in a CD */
+#define CDIO_CD_MAX_LSN 450150
+/*! Smallest LSN in a CD */
+#define CDIO_CD_MIN_LSN -450150
+
+
+#define CDIO_CD_FRAMES_PER_MIN \
+ (CDIO_CD_FRAMES_PER_SEC*CDIO_CD_SECS_PER_MIN)
+
+#define CDIO_CD_74MIN_SECTORS (UINT32_C(74)*CDIO_CD_FRAMES_PER_MIN)
+#define CDIO_CD_80MIN_SECTORS (UINT32_C(80)*CDIO_CD_FRAMES_PER_MIN)
+#define CDIO_CD_90MIN_SECTORS (UINT32_C(90)*CDIO_CD_FRAMES_PER_MIN)
+
+#define CDIO_CD_MAX_SECTORS \
+ (UINT32_C(100)*CDIO_CD_FRAMES_PER_MIN-CDIO_PREGAP_SECTORS)
+
+#define msf_t_SIZEOF 3
+
+/*!
+ Convert an LBA into a string representation of the MSF.
+ \warning cdio_lba_to_msf_str returns new allocated string */
+char *cdio_lba_to_msf_str (lba_t lba);
+
+/*!
+ Convert an MSF into a string representation of the MSF.
+ \warning cdio_msf_to_msf_str returns new allocated string */
+char *cdio_msf_to_str (const msf_t *msf);
+
+/*!
+ Convert an LBA into the corresponding LSN.
+*/
+lba_t cdio_lba_to_lsn (lba_t lba);
+
+/*!
+ Convert an LBA into the corresponding MSF.
+*/
+void cdio_lba_to_msf(lba_t lba, msf_t *msf);
+
+/*!
+ Convert an LSN into the corresponding LBA.
+ CDIO_INVALID_LBA is returned if there is an error.
+*/
+lba_t cdio_lsn_to_lba (lsn_t lsn);
+
+/*!
+ Convert an LSN into the corresponding MSF.
+*/
+void cdio_lsn_to_msf (lsn_t lsn, msf_t *msf);
+
+/*!
+ Convert a MSF into the corresponding LBA.
+ CDIO_INVALID_LBA is returned if there is an error.
+*/
+lba_t cdio_msf_to_lba (const msf_t *msf);
+
+/*!
+ Convert a MSF into the corresponding LSN.
+ CDIO_INVALID_LSN is returned if there is an error.
+*/
+lsn_t cdio_msf_to_lsn (const msf_t *msf);
+
+/*!
+ Convert a MSF - broken out as 3 integer components into the
+ corresponding LBA.
+ CDIO_INVALID_LBA is returned if there is an error.
+*/
+lba_t cdio_msf3_to_lba (unsigned int minutes, unsigned int seconds,
+ unsigned int frames);
+
+/*!
+ Convert a string of the form MM:SS:FF into the corresponding LBA.
+ CDIO_INVALID_LBA is returned if there is an error.
+*/
+lba_t cdio_mmssff_to_lba (const char *psz_mmssff);
+
+/*!
+ Return true if discmode is some sort of CD.
+*/
+bool cdio_is_discmode_cdrom (discmode_t discmode);
+
+/*!
+ Return true if discmode is some sort of DVD.
+*/
+bool cdio_is_discmode_dvd (discmode_t discmode);
+
+
+#ifdef __cplusplus
+ }
+#endif
+
+static inline bool discmode_is_cd(discmode_t discmode)
+{
+ switch (discmode) {
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static inline bool discmode_is_dvd(discmode_t discmode)
+{
+ switch (discmode) {
+ case CDIO_DISC_MODE_DVD_ROM:
+ case CDIO_DISC_MODE_DVD_RAM:
+ case CDIO_DISC_MODE_DVD_R:
+ case CDIO_DISC_MODE_DVD_RW:
+ case CDIO_DISC_MODE_DVD_PR:
+ case CDIO_DISC_MODE_DVD_PRW:
+ case CDIO_DISC_MODE_DVD_OTHER:
+ return true;
+ default:
+ return false;
+ }
+}
+
+
+#endif /* _CDIO_SECTOR_H_ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/types.h b/contrib/libcdio/cdio/types.h
new file mode 100644
index 000000000..ec84a142b
--- /dev/null
+++ b/contrib/libcdio/cdio/types.h
@@ -0,0 +1,379 @@
+/*
+ $Id: types.h,v 1.3 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/** \file types.h
+ * \brief Common type definitions used pervasively in libcdio.
+ */
+
+
+#ifndef __CDIO_TYPES_H__
+#define __CDIO_TYPES_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /* provide some C99 definitions */
+
+#if defined(HAVE_SYS_TYPES_H)
+#include <sys/types.h>
+#endif
+
+#if defined(HAVE_STDINT_H)
+# include <stdint.h>
+#elif defined(HAVE_INTTYPES_H)
+# include <inttypes.h>
+#elif defined(AMIGA) || defined(__linux__)
+ typedef u_int8_t uint8_t;
+ typedef u_int16_t uint16_t;
+ typedef u_int32_t uint32_t;
+ typedef u_int64_t uint64_t;
+#else
+ /* warning ISO/IEC 9899:1999 <stdint.h> was missing and even <inttypes.h> */
+ /* fixme */
+#endif /* HAVE_STDINT_H */
+
+ /* default HP/UX macros are broken */
+#if defined(__hpux__)
+# undef UINT16_C
+# undef UINT32_C
+# undef UINT64_C
+# undef INT64_C
+#endif
+
+ /* if it's still not defined, take a good guess... should work for
+ most 32bit and 64bit archs */
+
+#ifndef UINT16_C
+# define UINT16_C(c) c ## U
+#endif
+
+#ifndef UINT32_C
+# if defined (SIZEOF_INT) && SIZEOF_INT == 4
+# define UINT32_C(c) c ## U
+# elif defined (SIZEOF_LONG) && SIZEOF_LONG == 4
+# define UINT32_C(c) c ## UL
+# else
+# define UINT32_C(c) c ## U
+# endif
+#endif
+
+#ifndef UINT64_C
+# if defined (SIZEOF_LONG) && SIZEOF_LONG == 8
+# define UINT64_C(c) c ## UL
+# elif defined (SIZEOF_INT) && SIZEOF_INT == 8
+# define UINT64_C(c) c ## U
+# else
+# define UINT64_C(c) c ## ULL
+# endif
+#endif
+
+#ifndef INT64_C
+# if defined (SIZEOF_LONG) && SIZEOF_LONG == 8
+# define INT64_C(c) c ## L
+# elif defined (SIZEOF_INT) && SIZEOF_INT == 8
+# define INT64_C(c) c
+# else
+# define INT64_C(c) c ## LL
+# endif
+#endif
+
+#if defined(HAVE_STDBOOL_H)
+#include <stdbool.h>
+#else
+ /* ISO/IEC 9899:1999 <stdbool.h> missing -- enabling workaround */
+
+# ifndef __cplusplus
+ typedef enum
+ {
+ false = 0,
+ true = 1
+ } _cdio_Bool;
+
+# define false false
+# define true true
+# define bool _cdio_Bool
+# endif
+#endif
+
+ /* some GCC optimizations -- gcc 2.5+ */
+
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
+#define GNUC_PRINTF( format_idx, arg_idx ) \
+ __attribute__((format (printf, format_idx, arg_idx)))
+#define GNUC_SCANF( format_idx, arg_idx ) \
+ __attribute__((format (scanf, format_idx, arg_idx)))
+#define GNUC_FORMAT( arg_idx ) \
+ __attribute__((format_arg (arg_idx)))
+#define GNUC_NORETURN \
+ __attribute__((noreturn))
+#define GNUC_CONST \
+ __attribute__((const))
+#define GNUC_UNUSED \
+ __attribute__((unused))
+#define GNUC_PACKED \
+ __attribute__((packed))
+#else /* !__GNUC__ */
+#define GNUC_PRINTF( format_idx, arg_idx )
+#define GNUC_SCANF( format_idx, arg_idx )
+#define GNUC_FORMAT( arg_idx )
+#define GNUC_NORETURN
+#define GNUC_CONST
+#define GNUC_UNUSED
+#define GNUC_PACKED
+#endif /* !__GNUC__ */
+
+#if defined(__GNUC__)
+ /* for GCC we try to use GNUC_PACKED */
+# define PRAGMA_BEGIN_PACKED
+# define PRAGMA_END_PACKED
+#elif defined(HAVE_ISOC99_PRAGMA)
+ /* should work with most EDG-frontend based compilers */
+# define PRAGMA_BEGIN_PACKED _Pragma("pack(1)")
+# define PRAGMA_END_PACKED _Pragma("pack()")
+#else /* neither gcc nor _Pragma() available... */
+ /* ...so let's be naive and hope the regression testsuite is run... */
+# define PRAGMA_BEGIN_PACKED
+# define PRAGMA_END_PACKED
+#endif
+
+ /*
+ * user directed static branch prediction gcc 2.96+
+ */
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 95)
+# define GNUC_LIKELY(x) __builtin_expect((x),true)
+# define GNUC_UNLIKELY(x) __builtin_expect((x),false)
+#else
+# define GNUC_LIKELY(x) (x)
+# define GNUC_UNLIKELY(x) (x)
+#endif
+
+#ifndef NULL
+# define NULL ((void*) 0)
+#endif
+
+ /* our own offsetof()-like macro */
+#define __cd_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+
+ /*!
+ \brief MSF (minute/second/frame) structure
+
+ One CD-ROMs addressing scheme especially used in audio formats
+ (Red Book) is an address by minute, sector and frame which
+ BCD-encoded in three bytes. An alternative format is an lba_t.
+
+ @see lba_t
+ */
+ PRAGMA_BEGIN_PACKED
+ struct msf_rec {
+ uint8_t m, s, f;
+ } GNUC_PACKED;
+ PRAGMA_END_PACKED
+
+ typedef struct msf_rec msf_t;
+
+#define msf_t_SIZEOF 3
+
+ /* type used for bit-fields in structs (1 <= bits <= 8) */
+#if defined(__GNUC__)
+ /* this is strict ISO C99 which allows only 'unsigned int', 'signed
+ int' and '_Bool' explicitly as bit-field type */
+ typedef unsigned int bitfield_t;
+#else
+ /* other compilers might increase alignment requirements to match the
+ 'unsigned int' type -- fixme: find out how unalignment accesses can
+ be pragma'ed on non-gcc compilers */
+ typedef uint8_t bitfield_t;
+#endif
+
+ /*! The type of a Logical Block Address. We allow for an lba to be
+ negative to be consistent with an lba, although I'm not sure this
+ this is possible.
+
+ */
+ typedef int32_t lba_t;
+
+ /*! The type of a Logical Sector Number. Note that an lba lsn be negative
+ and the MMC3 specs allow for a conversion of a negative lba
+
+ @see msf_t
+ */
+ typedef int32_t lsn_t;
+
+ /*! The type of a track number 0..99. */
+ typedef uint8_t track_t;
+
+ /*!
+ Constant for invalid track number
+ */
+#define CDIO_INVALID_TRACK 0xFF
+
+ /*! The type of a session number 0..99. */
+ typedef uint8_t session_t;
+
+ /*!
+ Constant for invalid session number
+ */
+#define CDIO_INVALID_SESSION 0xFF
+
+ /*!
+ Constant for invalid LBA. It is 151 less than the most negative
+ LBA -45150. This provide slack for the 150-frame offset in
+ LBA to LSN 150 conversions
+ */
+#define CDIO_INVALID_LBA -45301
+
+ /*!
+ Constant for invalid LSN
+ */
+#define CDIO_INVALID_LSN CDIO_INVALID_LBA
+
+ /*!
+ Number of ASCII bytes in a media catalog number (MCN).
+ */
+#define CDIO_MCN_SIZE 13
+
+ /*!
+ Type to hold ASCII bytes in a media catalog number (MCN).
+ We include an extra 0 byte so these can be used as C strings.
+ */
+ typedef char cdio_mcn_t[CDIO_MCN_SIZE+1];
+
+
+ /*!
+ Number of ASCII bytes in International Standard Recording Codes (ISRC)
+ */
+#define CDIO_ISRC_SIZE 12
+
+ /*!
+ Type to hold ASCII bytes in a media catalog number (MCN).
+ We include an extra 0 byte so these can be used as C strings.
+ */
+ typedef char cdio_isrc_t[CDIO_ISRC_SIZE+1];
+
+ typedef int cdio_fs_anal_t;
+
+ /*! The type of an drive capability bit mask. See below for values*/
+ typedef uint32_t cdio_drive_read_cap_t;
+ typedef uint32_t cdio_drive_write_cap_t;
+ typedef uint32_t cdio_drive_misc_cap_t;
+
+ /*!
+ \brief Drive types returned by cdio_get_drive_cap()
+
+ NOTE: Setting a bit here means the presence of a capability.
+ */
+
+#define CDIO_DRIVE_CAP_ERROR 0x40000 /**< Error */
+#define CDIO_DRIVE_CAP_UNKNOWN 0x80000 /**< Dunno. It can be on if we
+ have only partial information
+ or are not completely certain
+ */
+
+#define CDIO_DRIVE_CAP_MISC_CLOSE_TRAY 0x00001 /**< caddy systems can't
+ close... */
+#define CDIO_DRIVE_CAP_MISC_EJECT 0x00002 /**< but can eject. */
+#define CDIO_DRIVE_CAP_MISC_LOCK 0x00004 /**< disable manual eject */
+#define CDIO_DRIVE_CAP_MISC_SELECT_SPEED 0x00008 /**< programmable speed */
+#define CDIO_DRIVE_CAP_MISC_SELECT_DISC 0x00010 /**< select disc from
+ juke-box */
+#define CDIO_DRIVE_CAP_MISC_MULTI_SESSION 0x00020 /**< read sessions>1 */
+#define CDIO_DRIVE_CAP_MISC_MEDIA_CHANGED 0x00080 /**< media changed */
+#define CDIO_DRIVE_CAP_MISC_RESET 0x00100 /**< hard reset device */
+#define CDIO_DRIVE_CAP_MCN 0x00200 /**< can read MCN */
+#define CDIO_DRIVE_CAP_ISRC 0x00200 /**< can read ISRC */
+#define CDIO_DRIVE_CAP_MISC_FILE 0x20000 /**< drive is really a file,
+ i.e a CD file image */
+
+ /*! Reading masks.. */
+#define CDIO_DRIVE_CAP_READ_AUDIO 0x00001 /**< drive can play CD audio */
+#define CDIO_DRIVE_CAP_READ_CD_DA 0x00002 /**< drive can read CD-DA */
+#define CDIO_DRIVE_CAP_READ_CD_G 0x00004 /**< drive can read CD+G */
+#define CDIO_DRIVE_CAP_READ_CD_R 0x00008 /**< drive can read CD-R */
+#define CDIO_DRIVE_CAP_READ_CD_RW 0x00010 /**< drive can read CD-RW */
+#define CDIO_DRIVE_CAP_READ_DVD_R 0x00020 /**< drive can read DVD-R */
+#define CDIO_DRIVE_CAP_READ_DVD_PR 0x00040 /**< drive can read DVD+R */
+#define CDIO_DRIVE_CAP_READ_DVD_RAM 0x00080 /**< drive can read DVD-RAM */
+#define CDIO_DRIVE_CAP_READ_DVD_ROM 0x00100 /**< drive can read DVD-ROM */
+#define CDIO_DRIVE_CAP_READ_DVD_RW 0x00200 /**< drive can read DVD-RW */
+#define CDIO_DRIVE_CAP_READ_DVD_RPW 0x00400 /**< drive can read DVD+RW */
+#define CDIO_DRIVE_CAP_READ_C2_ERRS 0x00800 /**< has C2 error correction */
+
+ /*! Writing masks.. */
+#define CDIO_DRIVE_CAP_WRITE_CD_R 0x00001 /**< drive can write CD-R */
+#define CDIO_DRIVE_CAP_WRITE_CD_RW 0x00002 /**< drive can write CD-R */
+#define CDIO_DRIVE_CAP_WRITE_DVD_R 0x00004 /**< drive can write DVD-R */
+#define CDIO_DRIVE_CAP_WRITE_DVD_PR 0x00008 /**< drive can write DVD+R */
+#define CDIO_DRIVE_CAP_WRITE_DVD_RAM 0x00010 /**< drive can write DVD-RAM */
+#define CDIO_DRIVE_CAP_WRITE_DVD_RW 0x00020 /**< drive can write DVD-RW */
+#define CDIO_DRIVE_CAP_WRITE_DVD_RPW 0x00040 /**< drive can write DVD+RW */
+#define CDIO_DRIVE_CAP_WRITE_MT_RAINIER 0x00080 /**< Mount Rainier */
+#define CDIO_DRIVE_CAP_WRITE_BURN_PROOF 0x00100 /**< burn proof */
+
+/**< Masks derived from above... */
+#define CDIO_DRIVE_CAP_WRITE_CD ( \
+ CDIO_DRIVE_CAP_WRITE_CD_R \
+ | CDIO_DRIVE_CAP_WRITE_CD_RW \
+ )
+/**< Has some sort of CD writer ability */
+
+/**< Masks derived from above... */
+#define CDIO_DRIVE_CAP_WRITE_DVD ( \
+ | CDIO_DRIVE_CAP_WRITE_DVD_R \
+ | CDIO_DRIVE_CAP_WRITE_DVD_PR \
+ | CDIO_DRIVE_CAP_WRITE_DVD_RAM \
+ | CDIO_DRIVE_CAP_WRITE_DVD_RW \
+ | CDIO_DRIVE_CAP_WRITE_DVD_RPW \
+ )
+/**< Has some sort of DVD writer ability */
+
+#define CDIO_DRIVE_CAP_WRITE \
+ (CDIO_DRIVE_CAP_WRITE_CD | CDIO_DRIVE_CAP_WRITE_DVD)
+/**< Has some sort of DVD or CD writing ability */
+
+ /*!
+ track flags
+ Q Sub-channel Control Field (4.2.3.3)
+ */
+ typedef enum {
+ CDIO_TRACK_FLAG_NONE = 0x00, /**< no flags set */
+ CDIO_TRACK_FLAG_PRE_EMPHASIS = 0x01, /**< audio track recorded with
+ pre-emphasis */
+ CDIO_TRACK_FLAG_COPY_PERMITTED = 0x02, /**< digital copy permitted */
+ CDIO_TRACK_FLAG_DATA = 0x04, /**< data track */
+ CDIO_TRACK_FLAG_FOUR_CHANNEL_AUDIO = 0x08, /**< 4 audio channels */
+ CDIO_TRACK_FLAG_SCMS = 0x10 /**< SCMS (5.29.2.7) */
+} cdio_track_flag;
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_TYPES_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/util.h b/contrib/libcdio/cdio/util.h
new file mode 100644
index 000000000..3cea313b4
--- /dev/null
+++ b/contrib/libcdio/cdio/util.h
@@ -0,0 +1,136 @@
+/*
+ $Id: util.h,v 1.3 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_UTIL_H__
+#define __CDIO_UTIL_H__
+
+/*!
+ \file util.h
+ \brief Miscellaneous utility functions.
+
+ Warning: this will probably get removed/replaced by using glib.h
+*/
+#include <stdlib.h>
+
+#undef MAX
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
+#undef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+
+#undef IN
+#define IN(x, low, high) ((x) >= (low) && (x) <= (high))
+
+#undef CLAMP
+#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
+
+static inline unsigned
+_cdio_len2blocks (unsigned len, int blocksize)
+{
+ unsigned blocks;
+
+ blocks = len / blocksize;
+ if (len % blocksize)
+ blocks++;
+
+ return blocks;
+}
+
+/* round up to next block boundary */
+static inline unsigned
+_cdio_ceil2block (unsigned offset, int blocksize)
+{
+ return _cdio_len2blocks (offset, blocksize) * blocksize;
+}
+
+static inline unsigned
+_cdio_ofs_add (unsigned offset, unsigned length, int blocksize)
+{
+ if (blocksize - (offset % blocksize) < length)
+ offset = _cdio_ceil2block (offset, blocksize);
+
+ offset += length;
+
+ return offset;
+}
+
+static inline const char *
+_cdio_bool_str (bool b)
+{
+ return b ? "yes" : "no";
+}
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void *
+_cdio_malloc (size_t size);
+
+void *
+_cdio_memdup (const void *mem, size_t count);
+
+char *
+_cdio_strdup_upper (const char str[]);
+
+void
+_cdio_strfreev(char **strv);
+
+char *
+_cdio_strjoin (char *strv[], unsigned count, const char delim[]);
+
+size_t
+_cdio_strlenv(char **str_array);
+
+char **
+_cdio_strsplit(const char str[], char delim);
+
+uint8_t cdio_to_bcd8(uint8_t n);
+uint8_t cdio_from_bcd8(uint8_t p);
+
+#if defined(__GNUC__) && __GNUC__ >= 3
+static inline __attribute__((deprecated))
+uint8_t to_bcd8(uint8_t n) {
+ return cdio_to_bcd8(n);
+}
+static inline __attribute__((deprecated))
+uint8_t from_bcd8(uint8_t p) {
+ return cdio_from_bcd8(p);
+}
+#else
+#define to_bcd8 cdio_to_bcd8
+#define from_bcd8 cdio_from_bcd8
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CDIO_UTIL_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio/version.h b/contrib/libcdio/cdio/version.h
new file mode 100644
index 000000000..345924cab
--- /dev/null
+++ b/contrib/libcdio/cdio/version.h
@@ -0,0 +1,10 @@
+/* $Id: version.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ */
+/** \file version.h
+ * \brief A file simply containing the library version number.
+ */
+
+/*! CDIO_VERSION can as a string in programs to show what version is used. */
+#define CDIO_VERSION "0.68"
+
+/*! LIBCDIO_VERSION_NUM can be used for testing in the C preprocessor */
+#define LIBCDIO_VERSION_NUM 68
diff --git a/contrib/libcdio/cdio/xa.h b/contrib/libcdio/cdio/xa.h
new file mode 100644
index 000000000..3af27eab5
--- /dev/null
+++ b/contrib/libcdio/cdio/xa.h
@@ -0,0 +1,151 @@
+/*
+ $Id: xa.h,v 1.3 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ See also iso9660.h by Eric Youngdale (1993) and in cdrtools. These
+ are
+
+ Copyright 1993 Yggdrasil Computing, Incorporated
+ Copyright (c) 1999,2000 J. Schilling
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*!
+ \file xa.h
+ \brief Things related to the ISO-9660 XA (Extended Attributes) format
+*/
+
+
+#ifndef __CDIO_XA_H__
+#define __CDIO_XA_H__
+
+#include <cdio/types.h>
+
+#define ISO_XA_MARKER_STRING "CD-XA001"
+#define ISO_XA_MARKER_OFFSET 1024
+
+/* XA attribute definitions */
+#define XA_PERM_RSYS 0x0001 /**< System Group Read */
+#define XA_PERM_XSYS 0x0004 /**< System Group Execute */
+
+#define XA_PERM_RUSR 0x0010 /**< User (owner) Read */
+#define XA_PERM_XUSR 0x0040 /**< User (owner) Execute */
+
+#define XA_PERM_RGRP 0x0100 /**< Group Read */
+#define XA_PERM_XGRP 0x0400 /**< Group Execute */
+
+#define XA_PERM_ROTH 0x1000 /**< Other (world) Read */
+#define XA_PERM_XOTH 0x4000 /**< Other (world) Execute */
+
+#define XA_ATTR_MODE2FORM1 (1 << 11)
+#define XA_ATTR_MODE2FORM2 (1 << 12)
+#define XA_ATTR_INTERLEAVED (1 << 13)
+#define XA_ATTR_CDDA (1 << 14)
+#define XA_ATTR_DIRECTORY (1 << 15)
+
+/* some aggregations */
+#define XA_PERM_ALL_READ (XA_PERM_RUSR | XA_PERM_RSYS | XA_PERM_RGRP)
+#define XA_PERM_ALL_EXEC (XA_PERM_XUSR | XA_PERM_XSYS | XA_PERM_XGRP)
+#define XA_PERM_ALL_ALL (XA_PERM_ALL_READ | XA_PERM_ALL_EXEC)
+
+#define XA_FORM1_DIR (XA_ATTR_DIRECTORY | XA_ATTR_MODE2FORM1 | XA_PERM_ALL_ALL)
+#define XA_FORM1_FILE (XA_ATTR_MODE2FORM1 | XA_PERM_ALL_ALL)
+#define XA_FORM2_FILE (XA_ATTR_MODE2FORM2 | XA_PERM_ALL_ALL)
+
+/*! \brief "Extended Architecture according to the Philips Yellow Book.
+
+CD-ROM EXtended Architecture is a modification to the CD-ROM
+specification that defines two new types of sectors. CD-ROM XA was
+developed jointly by Sony, Philips, and Microsoft, and announced in
+August 1988. Its specifications were published in an extension to the
+Yellow Book. CD-i, Photo CD, Video CD and CD-EXTRA have all
+subsequently been based on CD-ROM XA.
+
+CD-XA defines another way of formatting sectors on a CD-ROM, including
+headers in the sectors that describe the type (audio, video, data) and
+some additional info (markers, resolution in case of a video or audio
+sector, file numbers, etc).
+
+The data written on a CD-XA is consistent with and can be in ISO-9660
+file system format and therefore be readable by ISO-9660 file system
+translators. But also a CD-I player can also read CD-XA discs even if
+its own `Green Book' file system only resembles ISO 9660 and isn't
+fully compatible.
+
+ Note structure is big-endian.
+*/
+typedef struct iso9660_xa
+{
+ uint16_t group_id; /**< 0 */
+ uint16_t user_id; /**< 0 */
+ uint16_t attributes; /**< XA_ATTR_ */
+ uint8_t signature[2]; /**< { 'X', 'A' } */
+ uint8_t filenum; /**< file number, see also XA subheader */
+ uint8_t reserved[5]; /**< zero */
+} GNUC_PACKED iso9660_xa_t;
+
+
+/*!
+ Returns a string which interpreting the extended attribute xa_attr.
+ For example:
+ \verbatim
+ d---1xrxrxr
+ ---2--r-r-r
+ -a--1xrxrxr
+ \endverbatim
+
+ A description of the characters in the string follows
+ The 1st character is either "d" if the entry is a directory, or "-" if not
+ The 2nd character is either "a" if the entry is CDDA (audio), or "-" if not
+ The 3rd character is either "i" if the entry is interleaved, or "-" if not
+ The 4th character is either "2" if the entry is mode2 form2 or "-" if not
+ The 5th character is either "1" if the entry is mode2 form1 or "-" if not
+ Note that an entry will either be in mode2 form1 or mode form2. That
+ is you will either see "2-" or "-1" in the 4th & 5th positions.
+
+ The 6th and 7th characters refer to permissions for a user while the
+ the 8th and 9th characters refer to permissions for a group while, and
+ the 10th and 11th characters refer to permissions for everyone.
+
+ In each of these pairs the first character (6, 8, 10) is "x" if the
+ entry is executable. For a directory this means the directory is
+ allowed to be listed or "searched".
+ The second character of a pair (7, 9, 11) is "r" if the entry is allowed
+ to be read.
+*/
+const char *
+iso9660_get_xa_attr_str (uint16_t xa_attr);
+
+/*!
+ Allocates and initalizes a new iso9600_xa_t variable and returns
+ it. The caller should free the returned result.
+
+ @see iso9660_xa
+*/
+iso9660_xa_t *
+iso9660_xa_init (iso9660_xa_t *_xa, uint16_t uid, uint16_t gid, uint16_t attr,
+ uint8_t filenum);
+
+#endif /* __CDIO_XA_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/cdio_assert.h b/contrib/libcdio/cdio_assert.h
new file mode 100644
index 000000000..2433bf0c7
--- /dev/null
+++ b/contrib/libcdio/cdio_assert.h
@@ -0,0 +1,59 @@
+/*
+ $Id: cdio_assert.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_ASSERT_H__
+#define __CDIO_ASSERT_H__
+
+#if defined(__GNUC__)
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/types.h>
+#include <cdio/logging.h>
+
+#define cdio_assert(expr) \
+ { \
+ if (GNUC_UNLIKELY (!(expr))) cdio_log (CDIO_LOG_ASSERT, \
+ "file %s: line %d (%s): assertion failed: (%s)", \
+ __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
+ }
+
+#define cdio_assert_not_reached() \
+ { \
+ cdio_log (CDIO_LOG_ASSERT, \
+ "file %s: line %d (%s): should not be reached", \
+ __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ }
+
+#else /* non GNU C */
+
+#include <assert.h>
+
+#define cdio_assert(expr) \
+ assert(expr)
+
+#define cdio_assert_not_reached() \
+ assert(0)
+
+#endif
+
+#endif /* __CDIO_ASSERT_H__ */
diff --git a/contrib/libcdio/cdio_private.h b/contrib/libcdio/cdio_private.h
new file mode 100644
index 000000000..b1e2777ed
--- /dev/null
+++ b/contrib/libcdio/cdio_private.h
@@ -0,0 +1,315 @@
+/*
+ $Id: cdio_private.h,v 1.3 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* Internal routines for CD I/O drivers. */
+
+
+#ifndef __CDIO_PRIVATE_H__
+#define __CDIO_PRIVATE_H__
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/cdtext.h>
+#include "scsi_mmc_private.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /* Opaque type */
+ typedef struct _CdioDataSource CdioDataSource;
+
+#ifdef __cplusplus
+}
+
+#endif /* __cplusplus */
+
+#include "generic.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+
+ typedef struct {
+
+ /*!
+ Eject media in CD drive. If successful, as a side effect we
+ also free obj. Return 0 if success and 1 for failure.
+ */
+ int (*eject_media) (void *env);
+
+ /*!
+ Release and free resources associated with cd.
+ */
+ void (*free) (void *env);
+
+ /*!
+ Return the value associated with the key "arg".
+ */
+ const char * (*get_arg) (void *env, const char key[]);
+
+ /*!
+ Get cdtext information for a CdIo object.
+
+ @param obj the CD object that may contain CD-TEXT information.
+ @return the CD-TEXT object or NULL if obj is NULL
+ or CD-TEXT information does not exist.
+
+ If i_track is 0 or CDIO_CDROM_LEADOUT_TRACK the track returned
+ is the information assocated with the CD.
+ */
+ const cdtext_t * (*get_cdtext) (void *env, track_t i_track);
+
+ /*!
+ Return an array of device names. if CdIo is NULL (we haven't
+ initialized a specific device driver), then find a suitable device
+ driver.
+
+ NULL is returned if we couldn't return a list of devices.
+ */
+ char ** (*get_devices) (void);
+
+ /*!
+ Return a string containing the default CD device if none is specified.
+ */
+ char * (*get_default_device)(void);
+
+ /*!
+ Get disc mode associated with cd_obj.
+ */
+ discmode_t (*get_discmode) (void *p_env);
+
+ /*!
+ Return the what kind of device we've got.
+
+ See cd_types.h for a list of bitmasks for the drive type;
+ */
+ void (*get_drive_cap) (const void *env,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap);
+ /*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+ */
+ track_t (*get_first_track_num) (void *p_env);
+
+ /*!
+ Get the CD-ROM hardware info via a SCSI MMC INQUIRY command.
+ False is returned if we had an error getting the information.
+ */
+ bool (*get_hwinfo) ( const CdIo *p_cdio,
+ /* out*/ cdio_hwinfo_t *p_hw_info );
+
+ /*!
+ Return the media catalog number MCN from the CD or NULL if
+ there is none or we don't have the ability to get it.
+ */
+ char * (*get_mcn) (const void *env);
+
+ /*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+ */
+ track_t (*get_num_tracks) (void *env);
+
+ /*!
+ Return the starting LBA for track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ CDIO_INVALID_LBA is returned on error.
+ */
+ lba_t (*get_track_lba) (void *env, track_t track_num);
+
+ /*!
+ Get format of track.
+ */
+ track_format_t (*get_track_format) (void *env, track_t track_num);
+
+ /*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+ */
+ bool (*get_track_green) (void *env, track_t track_num);
+
+ /*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned on error.
+ */
+ bool (*get_track_msf) (void *env, track_t track_num, msf_t *msf);
+
+ /*!
+ lseek - reposition read/write file offset
+ Returns (off_t) -1 on error.
+ Similar to libc's lseek()
+ */
+ off_t (*lseek) (void *env, off_t offset, int whence);
+
+ /*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Similar to libc's read()
+ */
+ ssize_t (*read) (void *env, void *buf, size_t size);
+
+ /*!
+ Reads a single mode2 sector from cd device into buf starting
+ from lsn. Returns 0 if no error.
+ */
+ int (*read_audio_sectors) (void *env, void *buf, lsn_t lsn,
+ unsigned int nblocks);
+
+ /*!
+ Reads a single mode2 sector from cd device into buf starting
+ from lsn. Returns 0 if no error.
+ */
+ int (*read_mode2_sector) (void *env, void *buf, lsn_t lsn,
+ bool mode2_form2);
+
+ /*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+ int (*read_mode2_sectors) (void *p_env, void *p_buf, lsn_t lsn,
+ bool mode2_form2, unsigned int nblocks);
+
+ /*!
+ Reads a single mode1 sector from cd device into buf starting
+ from lsn. Returns 0 if no error.
+ */
+ int (*read_mode1_sector) (void *p_env, void *p_buf, lsn_t lsn,
+ bool mode1_form2);
+
+ /*!
+ Reads nblocks of mode1 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+ int (*read_mode1_sectors) (void *p_env, void *p_buf, lsn_t lsn,
+ bool mode1_form2, unsigned int nblocks);
+
+ bool (*read_toc) ( void *p_env ) ;
+
+ /*!
+ Run a SCSI MMC command.
+
+ cdio CD structure set by cdio_open().
+ i_timeout_ms time in milliseconds we will wait for the command
+ to complete.
+ cdb_len number of bytes in cdb (6, 10, or 12).
+ cdb CDB bytes. All values that are needed should be set on
+ input.
+ b_return_data TRUE if the command expects data to be returned in
+ the buffer
+ len Size of buffer
+ buf Buffer for data, both sending and receiving
+
+ Returns 0 if command completed successfully.
+ */
+ scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd;
+
+ /*!
+ Set the arg "key" with "value" in the source device.
+ */
+ int (*set_arg) (void *env, const char key[], const char value[]);
+
+ /*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+ uint32_t (*stat_size) (void *env);
+
+ } cdio_funcs;
+
+
+ /*! Implementation of CdIo type */
+ struct _CdIo {
+ driver_id_t driver_id; /**< Particular driver opened. */
+ cdio_funcs op; /**< driver-specific routines handling
+ implementation*/
+ void *env; /**< environment. Passed to routine above. */
+ };
+
+ /* This is used in drivers that must keep their own internal
+ position pointer for doing seeks. Stream-based drivers (like bincue,
+ nrg, toc, network) would use this.
+ */
+ typedef struct
+ {
+ off_t buff_offset; /* buffer offset in disk-image seeks. */
+ track_t index; /* Current track index in tocent. */
+ lba_t lba; /* Current LBA */
+ } internal_position_t;
+
+ CdIo * cdio_new (generic_img_private_t *p_env, cdio_funcs *funcs);
+
+ /* The below structure describes a specific CD Input driver */
+ typedef struct
+ {
+ driver_id_t id;
+ unsigned int flags;
+ const char *name;
+ const char *describe;
+ bool (*have_driver) (void);
+ CdIo *(*driver_open) (const char *psz_source_name);
+ CdIo *(*driver_open_am) (const char *psz_source_name,
+ const char *psz_access_mode);
+ char *(*get_default_device) (void);
+ bool (*is_device) (const char *psz_source_name);
+ char **(*get_devices) (void);
+ } CdIo_driver_t;
+
+ /* The below array gives of the drivers that are currently available for
+ on a particular host. */
+ extern CdIo_driver_t CdIo_driver[CDIO_MAX_DRIVER];
+
+ /* The last valid entry of Cdio_driver. -1 means uninitialzed. -2
+ means some sort of error.
+ */
+ extern int CdIo_last_driver;
+
+ /* The below array gives all drivers that can possibly appear.
+ on a particular host. */
+ extern CdIo_driver_t CdIo_all_drivers[CDIO_MAX_DRIVER+1];
+
+ /*!
+ Add/allocate a drive to the end of drives.
+ Use cdio_free_device_list() to free this device_list.
+ */
+ void cdio_add_device_list(char **device_list[], const char *drive,
+ unsigned int *i_drives);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_PRIVATE_H__ */
diff --git a/contrib/libcdio/cdtext.c b/contrib/libcdio/cdtext.c
new file mode 100644
index 000000000..5842641f7
--- /dev/null
+++ b/contrib/libcdio/cdtext.c
@@ -0,0 +1,228 @@
+/*
+ $Id: cdtext.c,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+ toc reading routine adapted from cuetools
+ Copyright (C) 2003 Svend Sanjay Sorensen <ssorensen@fastmail.fm>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdtext.h>
+#include <cdio/logging.h>
+#include "cdtext_private.h"
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+/*! Note: the order and number items (except CDTEXT_INVALID) should
+ match the cdtext_field_t enumeration. */
+const char *cdtext_keywords[] =
+ {
+ "ARRANGER",
+ "COMPOSER",
+ "DISC_ID",
+ "GENRE",
+ "ISRC",
+ "MESSAGE",
+ "PERFORMER",
+ "SIZE_INFO",
+ "SONGWRITER",
+ "TITLE",
+ "TOC_INFO",
+ "TOC_INFO2",
+ "UPC_EAN",
+ };
+
+
+/*! Return string representation of the enum values above */
+const char *
+cdtext_field2str (cdtext_field_t i)
+{
+ if (i >= MAX_CDTEXT_FIELDS)
+ return "Invalid CDTEXT field index";
+ else
+ return cdtext_keywords[i];
+}
+
+
+/*! Free memory assocated with cdtext*/
+void
+cdtext_destroy (cdtext_t *cdtext)
+{
+ cdtext_field_t i;
+
+ for (i=0; i < MAX_CDTEXT_FIELDS; i++) {
+ if (cdtext->field[i]) free(cdtext->field[i]);
+ }
+}
+
+/*!
+ returns the CDTEXT value associated with key. NULL is returned
+ if key is CDTEXT_INVALID or the field is not set.
+ */
+const char *
+cdtext_get (cdtext_field_t key, const cdtext_t *cdtext)
+{
+ if (key == CDTEXT_INVALID) return NULL;
+ return cdtext->field[key];
+}
+
+/*! Initialize a new cdtext structure.
+ When the structure is no longer needed, release the
+ resources using cdtext_delete.
+*/
+void
+cdtext_init (cdtext_t *cdtext)
+{
+ cdtext_field_t i;
+
+ for (i=0; i < MAX_CDTEXT_FIELDS; i++) {
+ cdtext->field[i] = NULL;
+ }
+}
+
+/*!
+ returns 0 if field is a CD-TEXT keyword, returns non-zero otherwise
+*/
+cdtext_field_t
+cdtext_is_keyword (const char *key)
+{
+#if 0
+ char *item;
+
+ item = bsearch(key,
+ cdtext_keywords, 12,
+ sizeof (char *),
+ (int (*)(const void *, const void *))
+ strcmp);
+ return (NULL != item) ? 0 : 1;
+#else
+ unsigned int i;
+
+ for (i = 0; i < 13 ; i++)
+ if (0 == strcmp (cdtext_keywords[i], key)) {
+ return i;
+ }
+ return CDTEXT_INVALID;
+#endif
+}
+
+/*! sets cdtext's keyword entry to field.
+ */
+void
+cdtext_set (cdtext_field_t key, const char *value, cdtext_t *cdtext)
+{
+ if (NULL == value || key == CDTEXT_INVALID) return;
+
+ if (cdtext->field[key]) free (cdtext->field[key]);
+ cdtext->field[key] = strdup (value);
+
+}
+
+#define SET_CDTEXT_FIELD(FIELD) \
+ (*set_cdtext_field_fn)(user_data, i_track, i_first_track, FIELD, buffer);
+
+/*
+ parse all CD-TEXT data retrieved.
+*/
+bool
+cdtext_data_init(void *user_data, track_t i_first_track,
+ unsigned char *wdata,
+ set_cdtext_field_fn_t set_cdtext_field_fn)
+{
+ CDText_data_t *pdata;
+ int i;
+ int j;
+ char buffer[256];
+ int idx;
+ int i_track;
+ bool b_ret = false;
+
+ memset( buffer, 0x00, sizeof(buffer) );
+ idx = 0;
+
+ pdata = (CDText_data_t *) (&wdata[4]);
+ for( i=0; i < CDIO_CDTEXT_MAX_PACK_DATA; i++ ) {
+
+#if TESTED
+ if ( pdata->bDBC ) {
+ cdio_warn("Double-byte characters not supported");
+ return false;
+ }
+#endif
+
+ if( pdata->seq != i )
+ break;
+
+ if( (pdata->type >= 0x80)
+ && (pdata->type <= 0x85) && (pdata->block == 0) ) {
+ i_track = pdata->i_track;
+
+ for( j=0; j < CDIO_CDTEXT_MAX_TEXT_DATA; j++ ) {
+ if( pdata->text[j] == 0x00 ) {
+ bool b_field_set=true;
+ switch( pdata->type) {
+ case CDIO_CDTEXT_TITLE:
+ SET_CDTEXT_FIELD(CDTEXT_TITLE);
+ break;
+ case CDIO_CDTEXT_PERFORMER:
+ SET_CDTEXT_FIELD(CDTEXT_PERFORMER);
+ break;
+ case CDIO_CDTEXT_SONGWRITER:
+ SET_CDTEXT_FIELD(CDTEXT_SONGWRITER);
+ break;
+ case CDIO_CDTEXT_COMPOSER:
+ SET_CDTEXT_FIELD(CDTEXT_COMPOSER);
+ break;
+ case CDIO_CDTEXT_ARRANGER:
+ SET_CDTEXT_FIELD(CDTEXT_ARRANGER);
+ break;
+ case CDIO_CDTEXT_MESSAGE:
+ SET_CDTEXT_FIELD(CDTEXT_MESSAGE);
+ break;
+ case CDIO_CDTEXT_DISCID:
+ SET_CDTEXT_FIELD(CDTEXT_DISCID);
+ break;
+ case CDIO_CDTEXT_GENRE:
+ SET_CDTEXT_FIELD(CDTEXT_GENRE);
+ break;
+ default : b_field_set = false;
+ }
+ if (b_field_set) {
+ b_ret = true;
+ i_track++;
+ idx = 0;
+ }
+ } else {
+ buffer[idx++] = pdata->text[j];
+ }
+ buffer[idx] = 0x00;
+ }
+ }
+ pdata++;
+ }
+ return b_ret;
+}
+
diff --git a/contrib/libcdio/cdtext_private.h b/contrib/libcdio/cdtext_private.h
new file mode 100644
index 000000000..03a9c4945
--- /dev/null
+++ b/contrib/libcdio/cdtext_private.h
@@ -0,0 +1,119 @@
+/*
+ $Id: cdtext_private.h,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_CDTEXT_PRIVATE_H__
+#define __CDIO_CDTEXT_PRIVATE_H__
+
+#include <cdio/cdio.h>
+#include <cdio/cdtext.h>
+
+#define CDIO_CDTEXT_MAX_PACK_DATA 255
+#define CDIO_CDTEXT_MAX_TEXT_DATA 12
+
+/* From table J.2 - Pack Type Indicator Definitions from
+ Working Draft NCITS XXX T10/1364-D Revision 10G. November 12, 2001.
+*/
+/* Title of Alubm name (ID=0) or Track Titles (ID != 0) */
+#define CDIO_CDTEXT_TITLE 0x80
+
+/* Name(s) of the performer(s) in ASCII */
+#define CDIO_CDTEXT_PERFORMER 0x81
+
+/* Name(s) of the songwriter(s) in ASCII */
+#define CDIO_CDTEXT_SONGWRITER 0x82
+
+/* Name(s) of the Composers in ASCII */
+#define CDIO_CDTEXT_COMPOSER 0x83
+
+/* Name(s) of the Arrangers in ASCII */
+#define CDIO_CDTEXT_ARRANGER 0x84
+
+/* Message(s) from content provider and/or artist in ASCII */
+#define CDIO_CDTEXT_MESSAGE 0x85
+
+/* Disc Identificatin information */
+#define CDIO_CDTEXT_DISCID 0x86
+
+/* Genre Identification and Genre Information */
+#define CDIO_CDTEXT_GENRE 0x87
+
+/* Table of Content Information */
+#define CDIO_CDTEXT_TOC 0x88
+
+/* Second Table of Content Information */
+#define CDIO_CDTEXT_TOC2 0x89
+
+/* 0x8A, 0x8B, 0x8C are reserved
+ 0x8D Reserved for content provider only.
+ */
+
+/* UPC/EAN code of the album and ISRC code of each track */
+#define CDIO_CDTEXT_UPC 0x8E
+
+/* Size information of the Block */
+#define CDIO_CDTEXT_BLOCKSIZE 0x8F
+
+
+PRAGMA_BEGIN_PACKED
+
+struct CDText_data
+{
+ uint8_t type;
+ track_t i_track;
+ uint8_t seq;
+#ifdef WORDS_BIGENDIAN
+ uint8_t bDBC: 1; /* double byte character */
+ uint8_t block: 3; /* block number 0..7 */
+ uint8_t characterPosition:4; /* character position */
+#else
+ uint8_t characterPosition:4; /* character position */
+ uint8_t block :3; /* block number 0..7 */
+ uint8_t bDBC :1; /* double byte character */
+#endif
+ char text[CDIO_CDTEXT_MAX_TEXT_DATA];
+ uint8_t crc[2];
+} GNUC_PACKED;
+
+PRAGMA_END_PACKED
+
+typedef struct CDText_data CDText_data_t;
+
+typedef void (*set_cdtext_field_fn_t) (void *user_data, track_t i_track,
+ track_t i_first_track,
+ cdtext_field_t field,
+ const char *buffer);
+
+/*
+ Internal routine to parse all CD-TEXT data retrieved.
+*/
+bool cdtext_data_init(void *user_data, track_t i_first_track,
+ unsigned char *wdata,
+ set_cdtext_field_fn_t set_cdtext_field_fn);
+
+
+#endif /* __CDIO_CDTEXT_PRIVATE_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/ds.c b/contrib/libcdio/ds.c
new file mode 100644
index 000000000..381c0f00c
--- /dev/null
+++ b/contrib/libcdio/ds.c
@@ -0,0 +1,249 @@
+/*
+ $Id: ds.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <cdio/ds.h>
+#include <cdio/util.h>
+#include <cdio/types.h>
+#include "cdio_assert.h"
+
+static const char _rcsid[] = "$Id: ds.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $";
+
+struct _CdioList
+{
+ unsigned length;
+
+ CdioListNode *begin;
+ CdioListNode *end;
+};
+
+struct _CdioListNode
+{
+ CdioList *list;
+
+ CdioListNode *next;
+
+ void *data;
+};
+
+/* impl */
+
+CdioList *
+_cdio_list_new (void)
+{
+ CdioList *new_obj = _cdio_malloc (sizeof (CdioList));
+
+ return new_obj;
+}
+
+void
+_cdio_list_free (CdioList *list, int free_data)
+{
+ while (_cdio_list_length (list))
+ _cdio_list_node_free (_cdio_list_begin (list), free_data);
+
+ free (list);
+}
+
+unsigned
+_cdio_list_length (const CdioList *list)
+{
+ cdio_assert (list != NULL);
+
+ return list->length;
+}
+
+void
+_cdio_list_prepend (CdioList *list, void *data)
+{
+ CdioListNode *new_node;
+
+ cdio_assert (list != NULL);
+
+ new_node = _cdio_malloc (sizeof (CdioListNode));
+
+ new_node->list = list;
+ new_node->next = list->begin;
+ new_node->data = data;
+
+ list->begin = new_node;
+ if (list->length == 0)
+ list->end = new_node;
+
+ list->length++;
+}
+
+void
+_cdio_list_append (CdioList *list, void *data)
+{
+ cdio_assert (list != NULL);
+
+ if (list->length == 0)
+ {
+ _cdio_list_prepend (list, data);
+ }
+ else
+ {
+ CdioListNode *new_node = _cdio_malloc (sizeof (CdioListNode));
+
+ new_node->list = list;
+ new_node->next = NULL;
+ new_node->data = data;
+
+ list->end->next = new_node;
+ list->end = new_node;
+
+ list->length++;
+ }
+}
+
+void
+_cdio_list_foreach (CdioList *list, _cdio_list_iterfunc func, void *user_data)
+{
+ CdioListNode *node;
+
+ cdio_assert (list != NULL);
+ cdio_assert (func != 0);
+
+ for (node = _cdio_list_begin (list);
+ node != NULL;
+ node = _cdio_list_node_next (node))
+ func (_cdio_list_node_data (node), user_data);
+}
+
+CdioListNode *
+_cdio_list_find (CdioList *list, _cdio_list_iterfunc cmp_func, void *user_data)
+{
+ CdioListNode *node;
+
+ cdio_assert (list != NULL);
+ cdio_assert (cmp_func != 0);
+
+ for (node = _cdio_list_begin (list);
+ node != NULL;
+ node = _cdio_list_node_next (node))
+ if (cmp_func (_cdio_list_node_data (node), user_data))
+ break;
+
+ return node;
+}
+
+CdioListNode *
+_cdio_list_begin (const CdioList *list)
+{
+ cdio_assert (list != NULL);
+
+ return list->begin;
+}
+
+CdioListNode *
+_cdio_list_end (CdioList *list)
+{
+ cdio_assert (list != NULL);
+
+ return list->end;
+}
+
+CdioListNode *
+_cdio_list_node_next (CdioListNode *node)
+{
+ if (node)
+ return node->next;
+
+ return NULL;
+}
+
+void
+_cdio_list_node_free (CdioListNode *node, int free_data)
+{
+ CdioList *list;
+ CdioListNode *prev_node;
+
+ cdio_assert (node != NULL);
+
+ list = node->list;
+
+ cdio_assert (_cdio_list_length (list) > 0);
+
+ if (free_data)
+ free (_cdio_list_node_data (node));
+
+ if (_cdio_list_length (list) == 1)
+ {
+ cdio_assert (list->begin == list->end);
+
+ list->end = list->begin = NULL;
+ list->length = 0;
+ free (node);
+ return;
+ }
+
+ cdio_assert (list->begin != list->end);
+
+ if (list->begin == node)
+ {
+ list->begin = node->next;
+ free (node);
+ list->length--;
+ return;
+ }
+
+ for (prev_node = list->begin; prev_node->next; prev_node = prev_node->next)
+ if (prev_node->next == node)
+ break;
+
+ cdio_assert (prev_node->next != NULL);
+
+ if (list->end == node)
+ list->end = prev_node;
+
+ prev_node->next = node->next;
+
+ list->length--;
+
+ free (node);
+}
+
+void *
+_cdio_list_node_data (CdioListNode *node)
+{
+ if (node)
+ return node->data;
+
+ return NULL;
+}
+
+/* eof */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
+
diff --git a/contrib/libcdio/generic.h b/contrib/libcdio/generic.h
new file mode 100644
index 000000000..9deb77254
--- /dev/null
+++ b/contrib/libcdio/generic.h
@@ -0,0 +1,179 @@
+/*
+ $Id: generic.h,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* Internal routines for CD I/O drivers. */
+
+
+#ifndef __CDIO_GENERIC_H__
+#define __CDIO_GENERIC_H__
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/cdtext.h>
+#include <cdio/iso9660.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /*!
+ Things common to private device structures. Even though not all
+ devices may have some of these fields, by listing common ones
+ we facilitate writing generic routines and even cut-and-paste
+ code.
+ */
+ typedef struct {
+ char *source_name; /**< Name used in open. */
+ bool init; /**< True if structure has been initialized */
+ bool toc_init; /**< True if TOC read in */
+ bool b_cdtext_init; /**< True if CD-Text read in */
+ bool b_cdtext_error; /**< True if trouble reading CD-Text */
+
+ int ioctls_debugged; /**< for debugging */
+
+ /* Only one of data_source or fd is used; fd is for CD-ROM
+ devices and the data_source for stream reading (bincue, nrg, toc,
+ network).
+ */
+ CdioDataSource *data_source;
+ int fd; /**< File descriptor of device */
+ track_t i_first_track; /**< The starting track number. */
+ track_t i_tracks; /**< The number of tracks. */
+
+ uint8_t i_joliet_level; /**< 0 = no Joliet extensions.
+ 1-3: Joliet level. */
+ iso9660_pvd_t pvd;
+ iso9660_svd_t svd;
+ CdIo *cdio; /**< a way to call general cdio routines. */
+ cdtext_t cdtext; /**< CD-Text for disc. */
+ cdtext_t cdtext_track[CDIO_CD_MAX_TRACKS+1]; /*CD-TEXT for each track*/
+
+ } generic_img_private_t;
+
+ /*!
+ Bogus eject media when there is no ejectable media, e.g. a disk image
+ We always return 2. Should we also free resources?
+ */
+ int cdio_generic_bogus_eject_media (void *env);
+
+ /*!
+ Release and free resources associated with cd.
+ */
+ void cdio_generic_free (void *env);
+
+ /*!
+ Initialize CD device.
+ */
+ bool cdio_generic_init (void *env);
+
+ /*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Is in fact libc's read().
+ */
+ off_t cdio_generic_lseek (void *env, off_t offset, int whence);
+
+ /*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Is in fact libc's read().
+ */
+ ssize_t cdio_generic_read (void *env, void *buf, size_t size);
+
+ /*!
+ Reads a single form1 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+ int cdio_generic_read_form1_sector (void * user_data, void *data,
+ lsn_t lsn);
+
+ /*!
+ Release and free resources associated with stream or disk image.
+ */
+ void cdio_generic_stdio_free (void *env);
+
+ /*!
+ Return true if source_name could be a device containing a CD-ROM on
+ Win32
+ */
+ bool cdio_is_device_win32(const char *source_name);
+
+
+ /*!
+ Return true if source_name could be a device containing a CD-ROM on
+ most Unix servers with block and character devices.
+ */
+ bool cdio_is_device_generic(const char *source_name);
+
+
+ /*!
+ Like above, but don't give a warning device doesn't exist.
+ */
+ bool cdio_is_device_quiet_generic(const char *source_name);
+
+ /*!
+ Get cdtext information for a CdIo object .
+
+ @param obj the CD object that may contain CD-TEXT information.
+ @return the CD-TEXT object or NULL if obj is NULL
+ or CD-TEXT information does not exist.
+ */
+ const cdtext_t *get_cdtext_generic (void *p_user_data, track_t i_track);
+
+ /*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+ */
+ track_t get_first_track_num_generic(void *p_user_data);
+
+ /*!
+ Return the number of tracks in the current medium.
+ */
+ track_t get_num_tracks_generic(void *p_user_data);
+
+ /*!
+ Get disc type associated with cd object.
+ */
+ discmode_t get_discmode_generic (void *p_user_data );
+
+ /*!
+ Same as above but only handles CD cases
+ */
+ discmode_t get_discmode_cd_generic (void *p_user_data );
+
+ void set_cdtext_field_generic(void *user_data, track_t i_track,
+ track_t i_first_track,
+ cdtext_field_t e_field, const char *psz_value);
+ /*!
+ Read cdtext information for a CdIo object .
+
+ return true on success, false on error or CD-Text information does
+ not exist.
+ */
+ bool init_cdtext_generic (generic_img_private_t *p_env);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_GENERIC_H__ */
diff --git a/contrib/libcdio/image.h b/contrib/libcdio/image.h
new file mode 100644
index 000000000..79c836d32
--- /dev/null
+++ b/contrib/libcdio/image.h
@@ -0,0 +1,75 @@
+/*
+ $Id: image.h,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*!
+ Header for image drivers. In contrast to image_common.h which contains
+ routines, this header like most C headers does not depend on anything
+ defined before it is included.
+*/
+
+#ifndef __CDIO_IMAGE_H__
+#define __CDIO_IMAGE_H__
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/types.h>
+#include <cdio/cdtext.h>
+#include "cdio_private.h"
+#include <cdio/sector.h>
+
+/*!
+ The universal format for information about a track for CD image readers
+ It may be that some fields can be derived from other fields.
+ Over time this structure may get cleaned up. Possibly this can be
+ expanded/reused for real CD formats.
+*/
+
+typedef struct {
+ track_t track_num; /**< Probably is index+1 */
+ msf_t start_msf;
+ lba_t start_lba;
+ int start_index;
+ lba_t length;
+ lba_t pregap; /**< pre-gap with zero audio data */
+ int sec_count; /**< Number of sectors in this track. Does not
+ include pregap */
+ int num_indices;
+ flag_t flags; /**< "[NO] COPY", "4CH", "[NO] PREMPAHSIS" */
+ char *isrc; /**< IRSC Code (5.22.4) exactly 12 bytes */
+ char *filename;
+ CdioDataSource *data_source;
+ track_format_t track_format;
+ bool track_green;
+ cdtext_t cdtext; /**< CD-TEXT */
+
+ trackmode_t mode;
+ uint16_t datasize; /**< How much is in the portion we return
+ back? */
+ uint16_t datastart; /**< Offset from begining that data starts */
+ uint16_t endsize; /**< How much stuff at the end to skip over.
+ This stuff may have error correction
+ (EDC, or ECC).*/
+ uint16_t blocksize; /**< total block size = start + size + end */
+} track_info_t;
+
+
+#endif /* __CDIO_IMAGE_H__ */
diff --git a/contrib/libcdio/image/Makefile.am b/contrib/libcdio/image/Makefile.am
new file mode 100644
index 000000000..e7a09e980
--- /dev/null
+++ b/contrib/libcdio/image/Makefile.am
@@ -0,0 +1,3 @@
+include $(top_srcdir)/misc/Makefile.common
+
+EXTRA_DIST = bincue.c nrg.c
diff --git a/contrib/libcdio/image/bincue.c b/contrib/libcdio/image/bincue.c
new file mode 100644
index 000000000..56f0e151c
--- /dev/null
+++ b/contrib/libcdio/image/bincue.c
@@ -0,0 +1,1214 @@
+/*
+ $Id: bincue.c,v 1.2 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com>
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ cue parsing routine adapted from cuetools
+ Copyright (C) 2003 Svend Sanjay Sorensen <ssorensen@fastmail.fm>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This code implements low-level access functions for a CD images
+ residing inside a disk file (*.bin) and its associated cue sheet.
+ (*.cue).
+*/
+
+static const char _rcsid[] = "$Id: bincue.c,v 1.2 2005/01/01 02:43:58 rockyb Exp $";
+
+#include "image.h"
+#include "cdio_assert.h"
+#include "cdio_private.h"
+#include "_cdio_stdio.h"
+
+#include <cdio/logging.h>
+#include <cdio/util.h>
+#include <cdio/version.h>
+
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+#ifdef HAVE_GLOB_H
+#include <glob.h>
+#endif
+#include <ctype.h>
+
+#include "portable.h"
+/* reader */
+
+#define DEFAULT_CDIO_DEVICE "videocd.bin"
+#define DEFAULT_CDIO_CUE "videocd.cue"
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+ internal_position_t pos;
+
+ char *psz_cue_name;
+ char *psz_mcn; /* Media Catalog Number (5.22.3)
+ exactly 13 bytes */
+ track_info_t tocent[CDIO_CD_MAX_TRACKS+1]; /* entry info for each track
+ add 1 for leadout. */
+ discmode_t disc_mode;
+} _img_private_t;
+
+static uint32_t _stat_size_bincue (void *user_data);
+static bool parse_cuefile (_img_private_t *cd, const char *toc_name);
+
+#define NEED_MEDIA_EJECT_IMAGE
+#include "image_common.h"
+
+/*!
+ Initialize image structures.
+ */
+static bool
+_init_bincue (_img_private_t *env)
+{
+ lsn_t lead_lsn;
+
+ if (env->gen.init)
+ return false;
+
+ if (!(env->gen.data_source = cdio_stdio_new (env->gen.source_name))) {
+ cdio_warn ("init failed");
+ return false;
+ }
+
+ /* Have to set init before calling _stat_size_bincue() or we will
+ get into infinite recursion calling passing right here.
+ */
+ env->gen.init = true;
+ env->gen.i_first_track = 1;
+ env->psz_mcn = NULL;
+ env->disc_mode = CDIO_DISC_MODE_NO_INFO;
+
+ cdtext_init (&(env->gen.cdtext));
+
+ lead_lsn = _stat_size_bincue( (_img_private_t *) env);
+
+ if (-1 == lead_lsn) return false;
+
+ if ((env->psz_cue_name == NULL)) return false;
+
+ /* Read in CUE sheet. */
+ if ( !parse_cuefile(env, env->psz_cue_name) ) return false;
+
+ /* Fake out leadout track and sector count for last track*/
+ cdio_lsn_to_msf (lead_lsn, &env->tocent[env->gen.i_tracks].start_msf);
+ env->tocent[env->gen.i_tracks].start_lba = cdio_lsn_to_lba(lead_lsn);
+ env->tocent[env->gen.i_tracks - env->gen.i_first_track].sec_count =
+ cdio_lsn_to_lba(lead_lsn -
+ env->tocent[env->gen.i_tracks - env->gen.i_first_track].start_lba);
+
+ return true;
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Would be libc's seek() but we have to adjust for the extra track header
+ information in each sector.
+*/
+static off_t
+_lseek_bincue (void *user_data, off_t offset, int whence)
+{
+ _img_private_t *env = user_data;
+
+ /* real_offset is the real byte offset inside the disk image
+ The number below was determined empirically. I'm guessing
+ the 1st 24 bytes of a bin file are used for something.
+ */
+ off_t real_offset=0;
+
+ unsigned int i;
+
+ env->pos.lba = 0;
+ for (i=0; i<env->gen.i_tracks; i++) {
+ track_info_t *this_track=&(env->tocent[i]);
+ env->pos.index = i;
+ if ( (this_track->sec_count*this_track->datasize) >= offset) {
+ int blocks = offset / this_track->datasize;
+ int rem = offset % this_track->datasize;
+ int block_offset = blocks * this_track->blocksize;
+ real_offset += block_offset + rem;
+ env->pos.buff_offset = rem;
+ env->pos.lba += blocks;
+ break;
+ }
+ real_offset += this_track->sec_count*this_track->blocksize;
+ offset -= this_track->sec_count*this_track->datasize;
+ env->pos.lba += this_track->sec_count;
+ }
+
+ if (i==env->gen.i_tracks) {
+ cdio_warn ("seeking outside range of disk image");
+ return -1;
+ } else {
+ real_offset += env->tocent[i].datastart;
+ return cdio_stream_seek(env->gen.data_source, real_offset, whence);
+ }
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ FIXME:
+ At present we assume a read doesn't cross sector or track
+ boundaries.
+*/
+static ssize_t
+_read_bincue (void *user_data, void *data, size_t size)
+{
+ _img_private_t *env = user_data;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ char *p = data;
+ ssize_t final_size=0;
+ ssize_t this_size;
+ track_info_t *this_track=&(env->tocent[env->pos.index]);
+ ssize_t skip_size = this_track->datastart + this_track->endsize;
+
+ while (size > 0) {
+ long int rem = this_track->datasize - env->pos.buff_offset;
+ if ((long int) size <= rem) {
+ this_size = cdio_stream_read(env->gen.data_source, buf, size, 1);
+ final_size += this_size;
+ memcpy (p, buf, this_size);
+ break;
+ }
+
+ /* Finish off reading this sector. */
+ cdio_warn ("Reading across block boundaries not finished");
+
+ size -= rem;
+ this_size = cdio_stream_read(env->gen.data_source, buf, rem, 1);
+ final_size += this_size;
+ memcpy (p, buf, this_size);
+ p += this_size;
+ this_size = cdio_stream_read(env->gen.data_source, buf, rem, 1);
+
+ /* Skip over stuff at end of this sector and the beginning of the next.
+ */
+ cdio_stream_read(env->gen.data_source, buf, skip_size, 1);
+
+ /* Get ready to read another sector. */
+ env->pos.buff_offset=0;
+ env->pos.lba++;
+
+ /* Have gone into next track. */
+ if (env->pos.lba >= env->tocent[env->pos.index+1].start_lba) {
+ env->pos.index++;
+ this_track=&(env->tocent[env->pos.index]);
+ skip_size = this_track->datastart + this_track->endsize;
+ }
+ }
+ return final_size;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_stat_size_bincue (void *user_data)
+{
+ _img_private_t *env = user_data;
+ long size;
+
+ size = cdio_stream_stat (env->gen.data_source);
+
+ if (size % CDIO_CD_FRAMESIZE_RAW)
+ {
+ cdio_warn ("image %s size (%ld) not multiple of blocksize (%d)",
+ env->gen.source_name, size, CDIO_CD_FRAMESIZE_RAW);
+ if (size % M2RAW_SECTOR_SIZE == 0)
+ cdio_warn ("this may be a 2336-type disc image");
+ else if (size % CDIO_CD_FRAMESIZE_RAW == 0)
+ cdio_warn ("this may be a 2352-type disc image");
+ /* exit (EXIT_FAILURE); */
+ }
+
+ size /= CDIO_CD_FRAMESIZE_RAW;
+
+ return size;
+}
+
+#define MAXLINE 4096 /* maximum line length + 1 */
+
+static bool
+parse_cuefile (_img_private_t *cd, const char *psz_cue_name)
+{
+ /* The below declarations may be common in other image-parse routines. */
+ FILE *fp;
+ char psz_line[MAXLINE]; /* text of current line read in file fp. */
+ unsigned int i_line=0; /* line number in file of psz_line. */
+ int i = -1; /* Position in tocent. Same as
+ cd->gen.i_tracks - 1 */
+ char *psz_keyword, *psz_field;
+ cdio_log_level_t log_level = (NULL == cd) ? CDIO_LOG_INFO : CDIO_LOG_WARN;
+ cdtext_field_t cdtext_key;
+
+ /* The below declarations may be unique to this image-parse routine. */
+ int start_index;
+ bool b_first_index_for_track=false;
+
+ if (NULL == psz_cue_name)
+ return false;
+
+ fp = fopen (psz_cue_name, "r");
+ if (fp == NULL) {
+ cdio_log(log_level, "error opening %s for reading: %s",
+ psz_cue_name, strerror(errno));
+ return false;
+ }
+
+ if (cd) {
+ cd->gen.i_tracks=0;
+ cd->gen.i_first_track=1;
+ cd->gen.b_cdtext_init = true;
+ cd->gen.b_cdtext_error = false;
+ cd->psz_mcn=NULL;
+ }
+
+ while ((fgets(psz_line, MAXLINE, fp)) != NULL) {
+
+ i_line++;
+
+ if (NULL != (psz_keyword = strtok (psz_line, " \t\n\r"))) {
+ /* REM remarks ... */
+ if (0 == strcmp ("REM", psz_keyword)) {
+ ;
+
+ /* global section */
+ /* CATALOG ddddddddddddd */
+ } else if (0 == strcmp ("CATALOG", psz_keyword)) {
+ if (-1 == i) {
+ if (NULL == (psz_field = strtok (NULL, " \t\n\r"))) {
+ cdio_log(log_level,
+ "%s line %d after word CATALOG: ",
+ psz_cue_name, i_line);
+ cdio_log(log_level,
+ "expecting 13-digit media catalog number, got nothing.");
+ goto err_exit;
+ }
+ if (strlen(psz_field) != 13) {
+ cdio_log(log_level,
+ "%s line %d after word CATALOG: ",
+ psz_cue_name, i_line);
+ cdio_log(log_level,
+ "Token %s has length %ld. Should be 13 digits.",
+ psz_field, (long int) strlen(psz_field));
+ goto err_exit;
+ } else {
+ /* Check that we have all digits*/
+ unsigned int i;
+ for (i=0; i<13; i++) {
+ if (!isdigit(psz_field[i])) {
+ cdio_log(log_level,
+ "%s line %d after word CATALOG:",
+ psz_cue_name, i_line);
+ cdio_log(log_level,
+ "Character \"%c\" at postition %i of token \"%s\" "
+ "is not all digits.",
+ psz_field[i], i+1, psz_field);
+ goto err_exit;
+ }
+ }
+ }
+
+ if (cd) cd->psz_mcn = strdup (psz_field);
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ } else {
+ goto not_in_global_section;
+ }
+
+ /* FILE "<filename>" <BINARY|WAVE|other?> */
+ } else if (0 == strcmp ("FILE", psz_keyword)) {
+ if (NULL != (psz_field = strtok (NULL, "\"\t\n\r"))) {
+ if (cd) cd->tocent[i + 1].filename = strdup (psz_field);
+ } else {
+ goto format_error;
+ }
+
+ /* TRACK N <mode> */
+ } else if (0 == strcmp ("TRACK", psz_keyword)) {
+ int i_track;
+
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ if (1!=sscanf(psz_field, "%d", &i_track)) {
+ cdio_log(log_level,
+ "%s line %d after word TRACK:",
+ psz_cue_name, i_line);
+ cdio_log(log_level,
+ "Expecting a track number, got %s", psz_field);
+ goto err_exit;
+ }
+ }
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ track_info_t *this_track=NULL;
+
+ if (cd) {
+ this_track = &(cd->tocent[cd->gen.i_tracks]);
+ this_track->track_num = cd->gen.i_tracks;
+ this_track->num_indices = 0;
+ b_first_index_for_track = false;
+ cdtext_init (&(cd->gen.cdtext_track[cd->gen.i_tracks]));
+ cd->gen.i_tracks++;
+ }
+ i++;
+
+ if (0 == strcmp ("AUDIO", psz_field)) {
+ if (cd) {
+ this_track->mode = AUDIO;
+ this_track->blocksize = CDIO_CD_FRAMESIZE_RAW;
+ this_track->datasize = CDIO_CD_FRAMESIZE_RAW;
+ this_track->datastart = 0;
+ this_track->endsize = 0;
+ this_track->track_format = TRACK_FORMAT_AUDIO;
+ this_track->track_green = false;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_DA;
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_XA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE1/2048", psz_field)) {
+ if (cd) {
+ this_track->mode = MODE1;
+ this_track->blocksize = 2048;
+ this_track->track_format= TRACK_FORMAT_DATA;
+ this_track->track_green = false;
+ /* Is the below correct? */
+ this_track->datastart = 0;
+ this_track->datasize = CDIO_CD_FRAMESIZE;
+ this_track->endsize = 0;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_XA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE1/2352", psz_field)) {
+ if (cd) {
+ this_track->blocksize = 2352;
+ this_track->track_format= TRACK_FORMAT_DATA;
+ this_track->track_green = false;
+ this_track->datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE;
+ this_track->datasize = CDIO_CD_FRAMESIZE;
+ this_track->endsize = CDIO_CD_EDC_SIZE
+ + CDIO_CD_M1F1_ZERO_SIZE + CDIO_CD_ECC_SIZE;
+ this_track->mode = MODE1_RAW;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_XA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2/2336", psz_field)) {
+ if (cd) {
+ this_track->blocksize = 2336;
+ this_track->track_format= TRACK_FORMAT_XA;
+ this_track->track_green = true;
+ this_track->mode = MODE2;
+ this_track->datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE;
+ this_track->datasize = M2RAW_SECTOR_SIZE;
+ this_track->endsize = 0;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_XA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2/2048", psz_field)) {
+ if (cd) {
+ this_track->blocksize = 2048;
+ this_track->track_format= TRACK_FORMAT_XA;
+ this_track->track_green = true;
+ this_track->mode = MODE2_FORM1;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2/2324", psz_field)) {
+ if (cd) {
+ this_track->blocksize = 2324;
+ this_track->track_format= TRACK_FORMAT_XA;
+ this_track->track_green = true;
+ this_track->mode = MODE2_FORM2;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2/2336", psz_field)) {
+ if (cd) {
+ this_track->blocksize = 2336;
+ this_track->track_format= TRACK_FORMAT_XA;
+ this_track->track_green = true;
+ this_track->mode = MODE2_FORM_MIX;
+ this_track->datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE;
+ this_track->datasize = M2RAW_SECTOR_SIZE;
+ this_track->endsize = 0;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2/2352", psz_field)) {
+ if (cd) {
+ this_track->blocksize = 2352;
+ this_track->track_format= TRACK_FORMAT_XA;
+ this_track->track_green = true;
+ this_track->mode = MODE2_RAW;
+ this_track->datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE;
+ this_track->datasize = CDIO_CD_FRAMESIZE;
+ this_track->endsize = CDIO_CD_SYNC_SIZE + CDIO_CD_ECC_SIZE;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else {
+ cdio_log(log_level,
+ "%s line %d after word TRACK:",
+ psz_cue_name, i_line);
+ cdio_log(log_level,
+ "Unknown track mode %s", psz_field);
+ goto err_exit;
+ }
+ } else {
+ goto format_error;
+ }
+
+ /* FLAGS flag1 flag2 ... */
+ } else if (0 == strcmp ("FLAGS", psz_keyword)) {
+ if (0 <= i) {
+ while (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ if (0 == strcmp ("PRE", psz_field)) {
+ if (cd) cd->tocent[i].flags |= PRE_EMPHASIS;
+ } else if (0 == strcmp ("DCP", psz_field)) {
+ if (cd) cd->tocent[i].flags |= COPY_PERMITTED;
+ } else if (0 == strcmp ("4CH", psz_field)) {
+ if (cd) cd->tocent[i].flags |= FOUR_CHANNEL_AUDIO;
+ } else if (0 == strcmp ("SCMS", psz_field)) {
+ if (cd) cd->tocent[i].flags |= SCMS;
+ } else {
+ goto format_error;
+ }
+ }
+ } else {
+ goto format_error;
+ }
+
+ /* ISRC CCOOOYYSSSSS */
+ } else if (0 == strcmp ("ISRC", psz_keyword)) {
+ if (0 <= i) {
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ if (cd) cd->tocent[i].isrc = strdup (psz_field);
+ } else {
+ goto format_error;
+ }
+ } else {
+ goto in_global_section;
+ }
+
+ /* PREGAP MM:SS:FF */
+ } else if (0 == strcmp ("PREGAP", psz_keyword)) {
+ if (0 <= i) {
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ lba_t lba = cdio_lsn_to_lba(cdio_mmssff_to_lba (psz_field));
+ if (CDIO_INVALID_LBA == lba) {
+ cdio_log(log_level, "%s line %d: after word PREGAP:",
+ psz_cue_name, i_line);
+ cdio_log(log_level, "Invalid MSF string %s",
+ psz_field);
+ goto err_exit;
+ }
+ if (cd) {
+ cd->tocent[i].pregap = lba;
+ }
+ } else {
+ goto format_error;
+ } if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ } else {
+ goto in_global_section;
+ }
+
+ /* INDEX [##] MM:SS:FF */
+ } else if (0 == strcmp ("INDEX", psz_keyword)) {
+ if (0 <= i) {
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r")))
+ if (1!=sscanf(psz_field, "%d", &start_index)) {
+ cdio_log(log_level,
+ "%s line %d after word INDEX:",
+ psz_cue_name, i_line);
+ cdio_log(log_level,
+ "expecting an index number, got %s",
+ psz_field);
+ goto err_exit;
+ }
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ lba_t lba = cdio_mmssff_to_lba (psz_field);
+ if (CDIO_INVALID_LBA == lba) {
+ cdio_log(log_level, "%s line %d: after word INDEX:",
+ psz_cue_name, i_line);
+ cdio_log(log_level, "Invalid MSF string %s",
+ psz_field);
+ goto err_exit;
+ }
+ if (cd) {
+#if FIXED_ME
+ cd->tocent[i].indexes[cd->tocent[i].nindex++] = lba;
+#else
+ track_info_t *this_track=
+ &(cd->tocent[cd->gen.i_tracks - cd->gen.i_first_track]);
+
+ if (start_index != 0) {
+ if (!b_first_index_for_track) {
+ lba += CDIO_PREGAP_SECTORS;
+ cdio_lba_to_msf(lba, &(this_track->start_msf));
+ b_first_index_for_track = true;
+ this_track->start_lba = lba;
+ }
+
+ if (cd->gen.i_tracks > 1) {
+ /* Figure out number of sectors for previous track */
+ track_info_t *prev_track=&(cd->tocent[cd->gen.i_tracks-2]);
+ if ( this_track->start_lba < prev_track->start_lba ) {
+ cdio_log (log_level,
+ "track %d at LBA %lu starts before track %d at LBA %lu",
+ cd->gen.i_tracks,
+ (unsigned long int) this_track->start_lba,
+ cd->gen.i_tracks,
+ (unsigned long int) prev_track->start_lba);
+ prev_track->sec_count = 0;
+ } else if ( this_track->start_lba >= prev_track->start_lba
+ + CDIO_PREGAP_SECTORS ) {
+ prev_track->sec_count = this_track->start_lba -
+ prev_track->start_lba - CDIO_PREGAP_SECTORS ;
+ } else {
+ cdio_log (log_level,
+ "%lu fewer than pregap (%d) sectors in track %d",
+ (long unsigned int)
+ this_track->start_lba - prev_track->start_lba,
+ CDIO_PREGAP_SECTORS,
+ cd->gen.i_tracks);
+ /* Include pregap portion in sec_count. Maybe the pregap
+ was omitted. */
+ prev_track->sec_count = this_track->start_lba -
+ prev_track->start_lba;
+ }
+ }
+ this_track->num_indices++;
+ }
+ }
+#endif
+ } else {
+ goto format_error;
+ }
+ } else {
+ goto in_global_section;
+ }
+
+ /* CD-TEXT */
+ } else if ( CDTEXT_INVALID !=
+ (cdtext_key = cdtext_is_keyword (psz_keyword)) ) {
+ if (-1 == i) {
+ if (cd) {
+ cdtext_set (cdtext_key,
+ strtok (NULL, "\"\t\n\r"),
+ &(cd->gen.cdtext));
+ }
+ } else {
+ if (cd) {
+ cdtext_set (cdtext_key, strtok (NULL, "\"\t\n\r"),
+ &(cd->gen.cdtext_track[i]));
+ }
+ }
+
+ /* unrecognized line */
+ } else {
+ cdio_log(log_level, "%s line %d: warning: unrecognized keyword: %s",
+ psz_cue_name, i_line, psz_keyword);
+ goto err_exit;
+ }
+ }
+ }
+
+ if (NULL != cd) {
+ cd->gen.toc_init = true;
+ }
+
+ fclose (fp);
+ return true;
+
+ format_error:
+ cdio_log(log_level, "%s line %d after word %s",
+ psz_cue_name, i_line, psz_keyword);
+ goto err_exit;
+
+ in_global_section:
+ cdio_log(log_level, "%s line %d: word %s not allowed in global section",
+ psz_cue_name, i_line, psz_keyword);
+ goto err_exit;
+
+ not_in_global_section:
+ cdio_log(log_level, "%s line %d: word %s only allowed in global section",
+ psz_cue_name, i_line, psz_keyword);
+
+ err_exit:
+ fclose (fp);
+ return false;
+
+}
+
+/*!
+ Reads a single audio sector from CD device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_audio_sectors_bincue (void *user_data, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *env = user_data;
+ int ret;
+
+ /* Why the adjustment of 272, I don't know. It seems to work though */
+ if (lsn != 0) {
+ ret = cdio_stream_seek (env->gen.data_source,
+ (lsn * CDIO_CD_FRAMESIZE_RAW) - 272, SEEK_SET);
+ if (ret!=0) return ret;
+
+ ret = cdio_stream_read (env->gen.data_source, data,
+ CDIO_CD_FRAMESIZE_RAW, nblocks);
+ } else {
+ /* We need to pad out the first 272 bytes with 0's */
+ BZERO(data, 272);
+
+ ret = cdio_stream_seek (env->gen.data_source, 0, SEEK_SET);
+
+ if (ret!=0) return ret;
+
+ ret = cdio_stream_read (env->gen.data_source, (uint8_t *) data+272,
+ CDIO_CD_FRAMESIZE_RAW - 272, nblocks);
+ }
+
+ /* ret is number of bytes if okay, but we need to return 0 okay. */
+ return ret == 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode1_sector_bincue (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ _img_private_t *p_env = user_data;
+ int ret;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ int blocksize = CDIO_CD_FRAMESIZE_RAW;
+
+ ret = cdio_stream_seek (p_env->gen.data_source, lsn * blocksize, SEEK_SET);
+ if (ret!=0) return ret;
+
+ /* FIXME: Not completely sure the below is correct. */
+ ret = cdio_stream_read (p_env->gen.data_source, buf, CDIO_CD_FRAMESIZE_RAW, 1);
+ if (ret==0) return ret;
+
+ memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE,
+ b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode1 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode1_sectors_bincue (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *p_env = user_data;
+ int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode1_sector_bincue (p_env,
+ ((char *)data) + (blocksize * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Reads a single mode1 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode2_sector_bincue (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ _img_private_t *p_env = user_data;
+ int ret;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ /* NOTE: The logic below seems a bit wrong and convoluted
+ to me, but passes the regression tests. (Perhaps it is why we get
+ valgrind errors in vcdxrip). Leave it the way it was for now.
+ Review this sector 2336 stuff later.
+ */
+
+ int blocksize = CDIO_CD_FRAMESIZE_RAW;
+
+ ret = cdio_stream_seek (p_env->gen.data_source, lsn * blocksize, SEEK_SET);
+ if (ret!=0) return ret;
+
+ ret = cdio_stream_read (p_env->gen.data_source, buf, CDIO_CD_FRAMESIZE_RAW, 1);
+ if (ret==0) return ret;
+
+
+ /* See NOTE above. */
+ if (b_form2)
+ memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE,
+ M2RAW_SECTOR_SIZE);
+ else
+ memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode2_sectors_bincue (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *p_env = user_data;
+ int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode2_sector_bincue (p_env,
+ ((char *)data) + (blocksize * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Return an array of strings giving possible BIN/CUE disk images.
+ */
+char **
+cdio_get_devices_bincue (void)
+{
+ char **drives = NULL;
+ unsigned int num_files=0;
+#ifdef HAVE_GLOB_H
+ unsigned int i;
+ glob_t globbuf;
+ globbuf.gl_offs = 0;
+ glob("*.cue", GLOB_DOOFFS, NULL, &globbuf);
+ for (i=0; i<globbuf.gl_pathc; i++) {
+ cdio_add_device_list(&drives, globbuf.gl_pathv[i], &num_files);
+ }
+ globfree(&globbuf);
+#else
+ cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &num_files);
+#endif /*HAVE_GLOB_H*/
+ cdio_add_device_list(&drives, NULL, &num_files);
+ return drives;
+}
+
+/*!
+ Return a string containing the default CD device.
+ */
+char *
+cdio_get_default_device_bincue(void)
+{
+ char **drives = cdio_get_devices_nrg();
+ char *drive = (drives[0] == NULL) ? NULL : strdup(drives[0]);
+ cdio_free_device_list(drives);
+ return drive;
+}
+
+static bool
+get_hwinfo_bincue ( const CdIo *p_cdio, /*out*/ cdio_hwinfo_t *hw_info)
+{
+ strcpy(hw_info->psz_vendor, "libcdio");
+ strcpy(hw_info->psz_model, "CDRWIN");
+ strcpy(hw_info->psz_revision, CDIO_VERSION);
+ return true;
+
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_format_t
+_get_track_format_bincue(void *user_data, track_t i_track)
+{
+ _img_private_t *p_env = user_data;
+
+ if (i_track > p_env->gen.i_tracks || i_track == 0)
+ return TRACK_FORMAT_ERROR;
+
+ return p_env->tocent[i_track-p_env->gen.i_first_track].track_format;
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_get_track_green_bincue(void *user_data, track_t i_track)
+{
+ _img_private_t *p_env = user_data;
+
+ if ( NULL == p_env ||
+ ( i_track < p_env->gen.i_first_track
+ || i_track >= p_env->gen.i_tracks + p_env->gen.i_first_track ) )
+ return false;
+
+ return p_env->tocent[i_track-p_env->gen.i_first_track].track_green;
+}
+
+/*!
+ Return the starting LSN track number
+ i_track in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using i_track LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static lba_t
+_get_lba_track_bincue(void *user_data, track_t i_track)
+{
+ _img_private_t *p_env = user_data;
+
+ if (i_track == CDIO_CDROM_LEADOUT_TRACK) i_track = p_env->gen.i_tracks+1;
+
+ if (i_track <= p_env->gen.i_tracks + p_env->gen.i_first_track && i_track != 0) {
+ return p_env->tocent[i_track-p_env->gen.i_first_track].start_lba;
+ } else
+ return CDIO_INVALID_LBA;
+}
+
+/*!
+ Return corresponding BIN file if psz_cue_name is a cue file or NULL
+ if not a CUE file.
+*/
+char *
+cdio_is_cuefile(const char *psz_cue_name)
+{
+ int i;
+ char *psz_bin_name;
+
+ if (psz_cue_name == NULL) return NULL;
+
+ /* FIXME? Now that we have cue parsing, should we really force
+ the filename extension requirement or is it enough just to
+ parse the cuefile?
+ */
+
+ psz_bin_name=strdup(psz_cue_name);
+ i=strlen(psz_bin_name)-strlen("cue");
+
+ if (i>0) {
+ if (psz_cue_name[i]=='c' && psz_cue_name[i+1]=='u' && psz_cue_name[i+2]=='e') {
+ psz_bin_name[i++]='b'; psz_bin_name[i++]='i'; psz_bin_name[i++]='n';
+ if (parse_cuefile(NULL, psz_cue_name))
+ return psz_bin_name;
+ else
+ goto error;
+ }
+ else if (psz_cue_name[i]=='C' && psz_cue_name[i+1]=='U' && psz_cue_name[i+2]=='E') {
+ psz_bin_name[i++]='B'; psz_bin_name[i++]='I'; psz_bin_name[i++]='N';
+ if (parse_cuefile(NULL, psz_cue_name))
+ return psz_bin_name;
+ else
+ goto error;
+ }
+ }
+ error:
+ free(psz_bin_name);
+ return NULL;
+}
+
+/*!
+ Return corresponding CUE file if psz_bin_name is a bin file or NULL
+ if not a BIN file.
+*/
+char *
+cdio_is_binfile(const char *psz_bin_name)
+{
+ int i;
+ char *psz_cue_name;
+
+ if (psz_bin_name == NULL) return NULL;
+
+ psz_cue_name=strdup(psz_bin_name);
+ i=strlen(psz_bin_name)-strlen("bin");
+
+ if (i>0) {
+ if (psz_bin_name[i]=='b' && psz_bin_name[i+1]=='i' && psz_bin_name[i+2]=='n') {
+ psz_cue_name[i++]='c'; psz_cue_name[i++]='u'; psz_cue_name[i++]='e';
+ return psz_cue_name;
+ }
+ else if (psz_bin_name[i]=='B' && psz_bin_name[i+1]=='I' && psz_bin_name[i+2]=='N') {
+ psz_cue_name[i++]='C'; psz_cue_name[i++]='U'; psz_cue_name[i++]='E';
+ return psz_cue_name;
+ }
+ }
+ free(psz_cue_name);
+ return NULL;
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_am_bincue (const char *psz_source_name, const char *psz_access_mode)
+{
+ if (psz_access_mode != NULL)
+ cdio_warn ("there is only one access mode for bincue. Arg %s ignored",
+ psz_access_mode);
+ return cdio_open_bincue(psz_source_name);
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_bincue (const char *source_name)
+{
+ char *psz_bin_name = cdio_is_cuefile(source_name);
+
+ if (NULL != psz_bin_name) {
+ free(psz_bin_name);
+ return cdio_open_cue(source_name);
+ } else {
+ char *psz_cue_name = cdio_is_binfile(source_name);
+ CdIo *cdio = cdio_open_cue(psz_cue_name);
+ free(psz_cue_name);
+ return cdio;
+ }
+}
+
+CdIo *
+cdio_open_cue (const char *psz_cue_name)
+{
+ CdIo *ret;
+ _img_private_t *_data;
+ char *psz_bin_name;
+
+ cdio_funcs _funcs;
+
+ memset( &_funcs, 0, sizeof(_funcs) );
+
+ _funcs.eject_media = _eject_media_image;
+ _funcs.free = _free_image;
+ _funcs.get_arg = _get_arg_image;
+ _funcs.get_cdtext = get_cdtext_generic;
+ _funcs.get_devices = cdio_get_devices_bincue;
+ _funcs.get_default_device = cdio_get_default_device_bincue;
+ _funcs.get_discmode = _get_discmode_image;
+ _funcs.get_drive_cap = _get_drive_cap_image;
+ _funcs.get_first_track_num= _get_first_track_num_image;
+ _funcs.get_hwinfo = get_hwinfo_bincue;
+ _funcs.get_mcn = _get_mcn_image;
+ _funcs.get_num_tracks = _get_num_tracks_image;
+ _funcs.get_track_format = _get_track_format_bincue;
+ _funcs.get_track_green = _get_track_green_bincue;
+ _funcs.get_track_lba = _get_lba_track_bincue;
+ _funcs.get_track_msf = _get_track_msf_image;
+ _funcs.lseek = _lseek_bincue;
+ _funcs.read = _read_bincue;
+ _funcs.read_audio_sectors = _read_audio_sectors_bincue;
+ _funcs.read_mode1_sector = _read_mode1_sector_bincue;
+ _funcs.read_mode1_sectors = _read_mode1_sectors_bincue;
+ _funcs.read_mode2_sector = _read_mode2_sector_bincue;
+ _funcs.read_mode2_sectors = _read_mode2_sectors_bincue;
+ _funcs.set_arg = _set_arg_image;
+ _funcs.stat_size = _stat_size_bincue;
+
+ if (NULL == psz_cue_name) return NULL;
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->gen.init = false;
+ _data->psz_cue_name = NULL;
+
+ ret = cdio_new ((void *)_data, &_funcs);
+
+ if (ret == NULL) {
+ free(_data);
+ return NULL;
+ }
+
+ psz_bin_name = cdio_is_cuefile(psz_cue_name);
+
+ if (NULL == psz_bin_name) {
+ cdio_error ("source name %s is not recognized as a CUE file",
+ psz_cue_name);
+ }
+
+ _set_arg_image (_data, "cue", psz_cue_name);
+ _set_arg_image (_data, "source", psz_bin_name);
+ free(psz_bin_name);
+
+ if (_init_bincue(_data)) {
+ return ret;
+ } else {
+ _free_image(_data);
+ free(ret);
+ return NULL;
+ }
+}
+
+bool
+cdio_have_bincue (void)
+{
+ return true;
+}
diff --git a/contrib/libcdio/image/cdrdao.c b/contrib/libcdio/image/cdrdao.c
new file mode 100644
index 000000000..828172721
--- /dev/null
+++ b/contrib/libcdio/image/cdrdao.c
@@ -0,0 +1,1198 @@
+/*
+ $Id: cdrdao.c,v 1.1 2005/01/01 02:43:58 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+ toc reading routine adapted from cuetools
+ Copyright (C) 2003 Svend Sanjay Sorensen <ssorensen@fastmail.fm>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This code implements low-level access functions for a CD images
+ residing inside a disk file (*.bin) and its associated cue sheet.
+ (*.cue).
+*/
+
+static const char _rcsid[] = "$Id: cdrdao.c,v 1.1 2005/01/01 02:43:58 rockyb Exp $";
+
+#include "image.h"
+#include "cdio_assert.h"
+#include "_cdio_stdio.h"
+
+#include <cdio/logging.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include <cdio/version.h>
+
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+#ifdef HAVE_GLOB_H
+#include <glob.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
+#include <ctype.h>
+
+#include "portable.h"
+
+/* reader */
+
+#define DEFAULT_CDIO_DEVICE "videocd.bin"
+#define DEFAULT_CDIO_CDRDAO "videocd.toc"
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+ internal_position_t pos;
+
+ char *psz_cue_name;
+ char *psz_mcn; /* Media Catalog Number (5.22.3)
+ exactly 13 bytes */
+ track_info_t tocent[CDIO_CD_MAX_TRACKS+1]; /* entry info for each track
+ add 1 for leadout. */
+ discmode_t disc_mode;
+} _img_private_t;
+
+static uint32_t _stat_size_cdrdao (void *user_data);
+static bool parse_tocfile (_img_private_t *cd, const char *toc_name);
+
+#define NEED_MEDIA_EJECT_IMAGE
+#include "image_common.h"
+
+/*!
+ Initialize image structures.
+ */
+static bool
+_init_cdrdao (_img_private_t *env)
+{
+ lsn_t lead_lsn;
+
+ if (env->gen.init)
+ return false;
+
+ /* Have to set init before calling _stat_size_cdrdao() or we will
+ get into infinite recursion calling passing right here.
+ */
+ env->gen.init = true;
+ env->gen.i_first_track = 1;
+ env->psz_mcn = NULL;
+ env->disc_mode = CDIO_DISC_MODE_NO_INFO;
+
+ cdtext_init (&(env->gen.cdtext));
+
+ /* Read in TOC sheet. */
+ if ( !parse_tocfile(env, env->psz_cue_name) ) return false;
+
+ lead_lsn = _stat_size_cdrdao( (_img_private_t *) env);
+
+ if (-1 == lead_lsn)
+ return false;
+
+ /* Fake out leadout track and sector count for last track*/
+ cdio_lsn_to_msf (lead_lsn, &env->tocent[env->gen.i_tracks].start_msf);
+ env->tocent[env->gen.i_tracks].start_lba = cdio_lsn_to_lba(lead_lsn);
+ env->tocent[env->gen.i_tracks-env->gen.i_first_track].sec_count =
+ cdio_lsn_to_lba(lead_lsn - env->tocent[env->gen.i_tracks-1].start_lba);
+
+ return true;
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Would be libc's seek() but we have to adjust for the extra track header
+ information in each sector.
+*/
+static off_t
+_lseek_cdrdao (void *user_data, off_t offset, int whence)
+{
+ _img_private_t *env = user_data;
+
+ /* real_offset is the real byte offset inside the disk image
+ The number below was determined empirically. I'm guessing
+ the 1st 24 bytes of a bin file are used for something.
+ */
+ off_t real_offset=0;
+
+ unsigned int i;
+
+ env->pos.lba = 0;
+ for (i=0; i<env->gen.i_tracks; i++) {
+ track_info_t *this_track=&(env->tocent[i]);
+ env->pos.index = i;
+ if ( (this_track->sec_count*this_track->datasize) >= offset) {
+ int blocks = offset / this_track->datasize;
+ int rem = offset % this_track->datasize;
+ int block_offset = blocks * this_track->blocksize;
+ real_offset += block_offset + rem;
+ env->pos.buff_offset = rem;
+ env->pos.lba += blocks;
+ break;
+ }
+ real_offset += this_track->sec_count*this_track->blocksize;
+ offset -= this_track->sec_count*this_track->datasize;
+ env->pos.lba += this_track->sec_count;
+ }
+
+ if (i==env->gen.i_tracks) {
+ cdio_warn ("seeking outside range of disk image");
+ return -1;
+ } else {
+ real_offset += env->tocent[i].datastart;
+ return cdio_stream_seek(env->tocent[i].data_source, real_offset, whence);
+ }
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ FIXME:
+ At present we assume a read doesn't cross sector or track
+ boundaries.
+*/
+static ssize_t
+_read_cdrdao (void *user_data, void *data, size_t size)
+{
+ _img_private_t *env = user_data;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ char *p = data;
+ ssize_t final_size=0;
+ ssize_t this_size;
+ track_info_t *this_track=&(env->tocent[env->pos.index]);
+ ssize_t skip_size = this_track->datastart + this_track->endsize;
+
+ while (size > 0) {
+ int rem = this_track->datasize - env->pos.buff_offset;
+ if (size <= rem) {
+ this_size = cdio_stream_read(this_track->data_source, buf, size, 1);
+ final_size += this_size;
+ memcpy (p, buf, this_size);
+ break;
+ }
+
+ /* Finish off reading this sector. */
+ cdio_warn ("Reading across block boundaries not finished");
+
+ size -= rem;
+ this_size = cdio_stream_read(this_track->data_source, buf, rem, 1);
+ final_size += this_size;
+ memcpy (p, buf, this_size);
+ p += this_size;
+ this_size = cdio_stream_read(this_track->data_source, buf, rem, 1);
+
+ /* Skip over stuff at end of this sector and the beginning of the next.
+ */
+ cdio_stream_read(this_track->data_source, buf, skip_size, 1);
+
+ /* Get ready to read another sector. */
+ env->pos.buff_offset=0;
+ env->pos.lba++;
+
+ /* Have gone into next track. */
+ if (env->pos.lba >= env->tocent[env->pos.index+1].start_lba) {
+ env->pos.index++;
+ this_track=&(env->tocent[env->pos.index]);
+ skip_size = this_track->datastart + this_track->endsize;
+ }
+ }
+ return final_size;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_stat_size_cdrdao (void *user_data)
+{
+ _img_private_t *env = user_data;
+ long size;
+
+ size = cdio_stream_stat (env->tocent[0].data_source);
+
+ if (size % CDIO_CD_FRAMESIZE_RAW)
+ {
+ cdio_warn ("image %s size (%ld) not multiple of blocksize (%d)",
+ env->tocent[0].filename, size, CDIO_CD_FRAMESIZE_RAW);
+ if (size % M2RAW_SECTOR_SIZE == 0)
+ cdio_warn ("this may be a 2336-type disc image");
+ else if (size % CDIO_CD_FRAMESIZE_RAW == 0)
+ cdio_warn ("this may be a 2352-type disc image");
+ /* exit (EXIT_FAILURE); */
+ }
+
+ size /= CDIO_CD_FRAMESIZE_RAW;
+
+ return size;
+}
+
+#define MAXLINE 512
+#define UNIMPLIMENTED_MSG \
+ cdio_log(log_level, "%s line %d: unimplimented keyword: %s", \
+ psz_cue_name, i_line, psz_keyword)
+
+
+static bool
+parse_tocfile (_img_private_t *cd, const char *psz_cue_name)
+{
+ /* The below declarations may be common in other image-parse routines. */
+ FILE *fp;
+ char psz_line[MAXLINE]; /* text of current line read in file fp. */
+ unsigned int i_line=0; /* line number in file of psz_line. */
+ int i = -1; /* Position in tocent. Same as
+ cd->gen.i_tracks - 1 */
+ char *psz_keyword, *psz_field;
+ cdio_log_level_t log_level = (NULL == cd) ? CDIO_LOG_INFO : CDIO_LOG_WARN;
+ cdtext_field_t cdtext_key;
+
+ /* The below declaration(s) may be unique to this image-parse routine. */
+ unsigned int i_cdtext_nest = 0;
+
+ if (NULL == psz_cue_name)
+ return false;
+
+ fp = fopen (psz_cue_name, "r");
+ if (fp == NULL) {
+ cdio_log(log_level, "error opening %s for reading: %s",
+ psz_cue_name, strerror(errno));
+ return false;
+ }
+
+ if (cd) {
+ cd->gen.b_cdtext_init = true;
+ cd->gen.b_cdtext_error = false;
+ }
+
+ while ((fgets(psz_line, MAXLINE, fp)) != NULL) {
+
+ i_line++;
+
+ /* strip comment from line */
+ /* todo: // in quoted strings? */
+ /* //comment */
+ if (NULL != (psz_field = strstr (psz_line, "//")))
+ *psz_field = '\0';
+
+ if (NULL != (psz_keyword = strtok (psz_line, " \t\n\r"))) {
+ /* CATALOG "ddddddddddddd" */
+ if (0 == strcmp ("CATALOG", psz_keyword)) {
+ if (-1 == i) {
+ if (NULL != (psz_field = strtok (NULL, "\"\t\n\r"))) {
+ if (13 != strlen(psz_field)) {
+ cdio_log(log_level,
+ "%s line %d after word CATALOG:",
+ psz_cue_name, i_line);
+ cdio_log(log_level,
+ "Token %s has length %ld. Should be 13 digits.",
+ psz_field, (long int) strlen(psz_field));
+
+ goto err_exit;
+ } else {
+ /* Check that we have all digits*/
+ unsigned int i;
+ for (i=0; i<13; i++) {
+ if (!isdigit(psz_field[i])) {
+ cdio_log(log_level,
+ "%s line %d after word CATALOG:",
+ psz_cue_name, i_line);
+ cdio_log(log_level,
+ "Character \"%c\" at postition %i of token \"%s\""
+ " is not all digits.",
+ psz_field[i], i+1, psz_field);
+ goto err_exit;
+ }
+ }
+ if (NULL != cd) cd->psz_mcn = strdup (psz_field);
+ }
+ } else {
+ cdio_log(log_level,
+ "%s line %d after word CATALOG:",
+ psz_cue_name, i_line);
+ cdio_log(log_level, "Expecting 13 digits; nothing seen.");
+ goto err_exit;
+ }
+ } else {
+ goto err_exit;
+ }
+
+ /* CD_DA | CD_ROM | CD_ROM_XA */
+ } else if (0 == strcmp ("CD_DA", psz_keyword)) {
+ if (-1 == i) {
+ if (NULL != cd)
+ cd->disc_mode = CDIO_DISC_MODE_CD_DA;
+ } else {
+ goto not_in_global_section;
+ }
+ } else if (0 == strcmp ("CD_ROM", psz_keyword)) {
+ if (-1 == i) {
+ if (NULL != cd)
+ cd->disc_mode = CDIO_DISC_MODE_CD_DATA;
+ } else {
+ goto not_in_global_section;
+ }
+
+ } else if (0 == strcmp ("CD_ROM_XA", psz_keyword)) {
+ if (-1 == i) {
+ if (NULL != cd)
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ } else {
+ goto not_in_global_section;
+ }
+
+ /* TRACK <track-mode> [<sub-channel-mode>] */
+ } else if (0 == strcmp ("TRACK", psz_keyword)) {
+ i++;
+ if (NULL != cd) cdtext_init (&(cd->gen.cdtext_track[i]));
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ if (0 == strcmp ("AUDIO", psz_field)) {
+ if (NULL != cd) {
+ cd->tocent[i].track_format = TRACK_FORMAT_AUDIO;
+ cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW;
+ cd->tocent[i].datasize = CDIO_CD_FRAMESIZE_RAW;
+ cd->tocent[i].datastart = 0;
+ cd->tocent[i].endsize = 0;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_DA;
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_XA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+
+ }
+ } else if (0 == strcmp ("MODE1", psz_field)) {
+ if (NULL != cd) {
+ cd->tocent[i].track_format = TRACK_FORMAT_DATA;
+ cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW;
+ cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE;
+ cd->tocent[i].datasize = CDIO_CD_FRAMESIZE;
+ cd->tocent[i].endsize = CDIO_CD_EDC_SIZE
+ + CDIO_CD_M1F1_ZERO_SIZE + CDIO_CD_ECC_SIZE;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_XA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE1_RAW", psz_field)) {
+ if (NULL != cd) {
+ cd->tocent[i].track_format = TRACK_FORMAT_DATA;
+ cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW;
+ cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE;
+ cd->tocent[i].datasize = CDIO_CD_FRAMESIZE;
+ cd->tocent[i].endsize = CDIO_CD_EDC_SIZE
+ + CDIO_CD_M1F1_ZERO_SIZE + CDIO_CD_ECC_SIZE;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_XA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2", psz_field)) {
+ if (NULL != cd) {
+ cd->tocent[i].track_format = TRACK_FORMAT_XA;
+ cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE;
+ cd->tocent[i].datasize = M2RAW_SECTOR_SIZE;
+ cd->tocent[i].endsize = 0;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2_FORM1", psz_field)) {
+ if (NULL != cd) {
+ cd->tocent[i].track_format = TRACK_FORMAT_XA;
+ cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE;
+ cd->tocent[i].datasize = CDIO_CD_FRAMESIZE_RAW;
+ cd->tocent[i].endsize = 0;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2_FORM2", psz_field)) {
+ if (NULL != cd) {
+ cd->tocent[i].track_format = TRACK_FORMAT_XA;
+ cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE;
+ cd->tocent[i].datasize = CDIO_CD_FRAMESIZE;
+ cd->tocent[i].endsize = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_ECC_SIZE;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2_FORM_MIX", psz_field)) {
+ if (NULL != cd) {
+ cd->tocent[i].track_format = TRACK_FORMAT_XA;
+ cd->tocent[i].datasize = M2RAW_SECTOR_SIZE;
+ cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW;
+ cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE +
+ CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE;
+ cd->tocent[i].track_green = true;
+ cd->tocent[i].endsize = 0;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else if (0 == strcmp ("MODE2_RAW", psz_field)) {
+ if (NULL != cd) {
+ cd->tocent[i].track_format = TRACK_FORMAT_XA;
+ cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW;
+ cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE +
+ CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE;
+ cd->tocent[i].datasize = CDIO_CD_FRAMESIZE;
+ cd->tocent[i].track_green = true;
+ cd->tocent[i].endsize = 0;
+ switch(cd->disc_mode) {
+ case CDIO_DISC_MODE_NO_INFO:
+ cd->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_ERROR:
+ /* Disc type stays the same. */
+ break;
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ cd->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ default:
+ cd->disc_mode = CDIO_DISC_MODE_ERROR;
+ }
+ }
+ } else {
+ cdio_log(log_level, "%s line %d after TRACK:",
+ psz_cue_name, i_line);
+ cdio_log(log_level, "'%s' not a valid mode.", psz_field);
+ goto err_exit;
+ }
+ }
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ /* todo: set sub-channel-mode */
+ if (0 == strcmp ("RW", psz_field))
+ ;
+ else if (0 == strcmp ("RW_RAW", psz_field))
+ ;
+ }
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+
+ /* track flags */
+ /* [NO] COPY | [NO] PRE_EMPHASIS */
+ } else if (0 == strcmp ("NO", psz_keyword)) {
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ if (0 == strcmp ("COPY", psz_field)) {
+ if (NULL != cd)
+ cd->tocent[i].flags &= ~CDIO_TRACK_FLAG_COPY_PERMITTED;
+
+ } else if (0 == strcmp ("PRE_EMPHASIS", psz_field))
+ if (NULL != cd) {
+ cd->tocent[i].flags &= ~CDIO_TRACK_FLAG_PRE_EMPHASIS;
+ goto err_exit;
+ }
+ } else {
+ goto format_error;
+ }
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ } else if (0 == strcmp ("COPY", psz_keyword)) {
+ if (NULL != cd)
+ cd->tocent[i].flags |= CDIO_TRACK_FLAG_COPY_PERMITTED;
+ } else if (0 == strcmp ("PRE_EMPHASIS", psz_keyword)) {
+ if (NULL != cd)
+ cd->tocent[i].flags |= CDIO_TRACK_FLAG_PRE_EMPHASIS;
+ /* TWO_CHANNEL_AUDIO */
+ } else if (0 == strcmp ("TWO_CHANNEL_AUDIO", psz_keyword)) {
+ if (NULL != cd)
+ cd->tocent[i].flags &= ~CDIO_TRACK_FLAG_FOUR_CHANNEL_AUDIO;
+ /* FOUR_CHANNEL_AUDIO */
+ } else if (0 == strcmp ("FOUR_CHANNEL_AUDIO", psz_keyword)) {
+ if (NULL != cd)
+ cd->tocent[i].flags |= CDIO_TRACK_FLAG_FOUR_CHANNEL_AUDIO;
+
+ /* ISRC "CCOOOYYSSSSS" */
+ } else if (0 == strcmp ("ISRC", psz_keyword)) {
+ if (NULL != (psz_field = strtok (NULL, "\"\t\n\r"))) {
+ if (NULL != cd)
+ cd->tocent[i].isrc = strdup(psz_field);
+ } else {
+ goto format_error;
+ }
+
+ /* SILENCE <length> */
+ } else if (0 == strcmp ("SILENCE", psz_keyword)) {
+ UNIMPLIMENTED_MSG;
+
+ /* ZERO <length> */
+ } else if (0 == strcmp ("ZERO", psz_keyword)) {
+ UNIMPLIMENTED_MSG;
+
+ /* [FILE|AUDIOFILE] "<filename>" <start> [<length>] */
+ } else if (0 == strcmp ("FILE", psz_keyword)
+ || 0 == strcmp ("AUDIOFILE", psz_keyword)) {
+ if (0 <= i) {
+ if (NULL != (psz_field = strtok (NULL, "\"\t\n\r"))) {
+ if (NULL != cd) {
+ cd->tocent[i].filename = strdup (psz_field);
+ /* Todo: do something about reusing existing files. */
+ if (!(cd->tocent[i].data_source = cdio_stdio_new (psz_field))) {
+ cdio_log (log_level,
+ "%s line %d: can't open file `%s' for reading",
+ psz_cue_name, i_line, psz_field);
+ goto err_exit;
+ }
+ }
+ }
+
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ lba_t lba = cdio_lsn_to_lba(cdio_mmssff_to_lba (psz_field));
+ if (CDIO_INVALID_LBA == lba) {
+ cdio_log(log_level, "%s line %d: invalid MSF string %s",
+ psz_cue_name, i_line, psz_field);
+ goto err_exit;
+ }
+
+ if (NULL != cd) {
+ cd->tocent[i].start_lba = lba;
+ cdio_lba_to_msf(lba, &(cd->tocent[i].start_msf));
+ }
+ }
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r")))
+ if (NULL != cd)
+ cd->tocent[i].length = cdio_mmssff_to_lba (psz_field);
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ } else {
+ goto not_in_global_section;
+ }
+
+ /* DATAFILE "<filename>" <start> [<length>] */
+ } else if (0 == strcmp ("DATAFILE", psz_keyword)) {
+ goto unimplimented_error;
+
+ /* FIFO "<fifo path>" [<length>] */
+ } else if (0 == strcmp ("FIFO", psz_keyword)) {
+ goto unimplimented_error;
+
+ /* START MM:SS:FF */
+ } else if (0 == strcmp ("START", psz_keyword)) {
+ if (0 <= i) {
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ /* todo: line is too long! */
+ if (NULL != cd) {
+ cd->tocent[i].start_lba += cdio_mmssff_to_lba (psz_field);
+ cdio_lba_to_msf(cd->tocent[i].start_lba,
+ &(cd->tocent[i].start_msf));
+ }
+ }
+
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ } else {
+ goto not_in_global_section;
+ }
+
+ /* PREGAP MM:SS:FF */
+ } else if (0 == strcmp ("PREGAP", psz_keyword)) {
+ if (0 <= i) {
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ if (NULL != cd)
+ cd->tocent[i].pregap = cdio_mmssff_to_lba (psz_field);
+ } else {
+ goto format_error;
+ }
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ } else {
+ goto not_in_global_section;
+ }
+
+ /* INDEX MM:SS:FF */
+ } else if (0 == strcmp ("INDEX", psz_keyword)) {
+ if (0 <= i) {
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ if (NULL != cd) {
+#if 0
+ if (1 == cd->tocent[i].nindex) {
+ cd->tocent[i].indexes[1] = cd->tocent[i].indexes[0];
+ cd->tocent[i].nindex++;
+ }
+ cd->tocent[i].indexes[cd->tocent[i].nindex++] =
+ cdio_mmssff_to_lba (psz_field) + cd->tocent[i].indexes[0];
+#else
+ ;
+
+#endif
+ }
+ } else {
+ goto format_error;
+ }
+ if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ } else {
+ goto not_in_global_section;
+ }
+
+ /* CD_TEXT { ... } */
+ /* todo: opening { must be on same line as CD_TEXT */
+ } else if (0 == strcmp ("CD_TEXT", psz_keyword)) {
+ if (NULL == (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ if ( 0 == strcmp( "{", psz_field ) ) {
+ i_cdtext_nest++;
+ } else {
+ cdio_log (log_level,
+ "%s line %d: expecting '{'", psz_cue_name, i_line);
+ goto err_exit;
+ }
+
+ } else if (0 == strcmp ("LANGUAGE_MAP", psz_keyword)) {
+ /* LANGUAGE d { ... } */
+ } else if (0 == strcmp ("LANGUAGE", psz_keyword)) {
+ if (NULL == (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ /* Language number */
+ if (NULL == (psz_field = strtok (NULL, " \t\n\r"))) {
+ goto format_error;
+ }
+ if ( 0 == strcmp( "{", psz_field ) ) {
+ i_cdtext_nest++;
+ }
+ } else if (0 == strcmp ("{", psz_keyword)) {
+ i_cdtext_nest++;
+ } else if (0 == strcmp ("}", psz_keyword)) {
+ if (i_cdtext_nest > 0) i_cdtext_nest--;
+ } else if ( CDTEXT_INVALID !=
+ (cdtext_key = cdtext_is_keyword (psz_keyword)) ) {
+ if (-1 == i) {
+ if (NULL != cd) {
+ cdtext_set (cdtext_key,
+ strtok (NULL, "\"\t\n\r"),
+ &(cd->gen.cdtext));
+ }
+ } else {
+ if (NULL != cd) {
+ cdtext_set (cdtext_key,
+ strtok (NULL, "\"\t\n\r"),
+ &(cd->gen.cdtext_track[i]));
+ }
+ }
+
+ /* unrecognized line */
+ } else {
+ cdio_log(log_level, "%s line %d: warning: unrecognized word: %s",
+ psz_cue_name, i_line, psz_keyword);
+ goto err_exit;
+ }
+ }
+ }
+
+ if (NULL != cd) {
+ cd->gen.i_tracks = i+1;
+ cd->gen.toc_init = true;
+ }
+
+ fclose (fp);
+ return true;
+
+ unimplimented_error:
+ UNIMPLIMENTED_MSG;
+ goto err_exit;
+
+ format_error:
+ cdio_log(log_level, "%s line %d after word %s",
+ psz_cue_name, i_line, psz_keyword);
+ goto err_exit;
+
+ not_in_global_section:
+ cdio_log(log_level, "%s line %d: word %s only allowed in global section",
+ psz_cue_name, i_line, psz_keyword);
+
+ err_exit:
+ fclose (fp);
+ return false;
+}
+
+/*!
+ Reads a single audio sector from CD device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_audio_sectors_cdrdao (void *user_data, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *env = user_data;
+ int ret;
+
+ /* Why the adjustment of 272, I don't know. It seems to work though */
+ if (lsn != 0) {
+ ret = cdio_stream_seek (env->tocent[0].data_source,
+ (lsn * CDIO_CD_FRAMESIZE_RAW) - 272, SEEK_SET);
+ if (ret!=0) return ret;
+
+ ret = cdio_stream_read (env->tocent[0].data_source, data,
+ CDIO_CD_FRAMESIZE_RAW, nblocks);
+ } else {
+ /* We need to pad out the first 272 bytes with 0's */
+ BZERO(data, 272);
+
+ ret = cdio_stream_seek (env->tocent[0].data_source, 0, SEEK_SET);
+
+ if (ret!=0) return ret;
+
+ ret = cdio_stream_read (env->tocent[0].data_source, (uint8_t *) data+272,
+ CDIO_CD_FRAMESIZE_RAW - 272, nblocks);
+ }
+
+ /* ret is number of bytes if okay, but we need to return 0 okay. */
+ return ret == 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode1_sector_cdrdao (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ _img_private_t *env = user_data;
+ int ret;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ ret = cdio_stream_seek (env->tocent[0].data_source,
+ lsn * CDIO_CD_FRAMESIZE_RAW, SEEK_SET);
+ if (ret!=0) return ret;
+
+ /* FIXME: Not completely sure the below is correct. */
+ ret = cdio_stream_read (env->tocent[0].data_source, buf,
+ CDIO_CD_FRAMESIZE_RAW, 1);
+ if (ret==0) return ret;
+
+ memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE,
+ b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode1 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode1_sectors_cdrdao (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *env = user_data;
+ int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode1_sector_cdrdao (env,
+ ((char *)data) + (blocksize * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Reads a single mode1 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_read_mode2_sector_cdrdao (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ _img_private_t *env = user_data;
+ int ret;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ /* NOTE: The logic below seems a bit wrong and convoluted
+ to me, but passes the regression tests. (Perhaps it is why we get
+ valgrind errors in vcdxrip). Leave it the way it was for now.
+ Review this sector 2336 stuff later.
+ */
+
+ ret = cdio_stream_seek (env->tocent[0].data_source,
+ lsn * CDIO_CD_FRAMESIZE_RAW, SEEK_SET);
+ if (ret!=0) return ret;
+
+ ret = cdio_stream_read (env->tocent[0].data_source, buf,
+ CDIO_CD_FRAMESIZE_RAW, 1);
+ if (ret==0) return ret;
+
+
+ /* See NOTE above. */
+ if (b_form2)
+ memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE,
+ M2RAW_SECTOR_SIZE);
+ else
+ memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode2_sectors_cdrdao (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned int nblocks)
+{
+ _img_private_t *env = user_data;
+ int i;
+ int retval;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode2_sector_cdrdao (env,
+ ((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Return an array of strings giving possible TOC disk images.
+ */
+char **
+cdio_get_devices_cdrdao (void)
+{
+ char **drives = NULL;
+ unsigned int num_files=0;
+#ifdef HAVE_GLOB_H
+ unsigned int i;
+ glob_t globbuf;
+ globbuf.gl_offs = 0;
+ glob("*.toc", GLOB_DOOFFS, NULL, &globbuf);
+ for (i=0; i<globbuf.gl_pathc; i++) {
+ cdio_add_device_list(&drives, globbuf.gl_pathv[i], &num_files);
+ }
+ globfree(&globbuf);
+#else
+ cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &num_files);
+#endif /*HAVE_GLOB_H*/
+ cdio_add_device_list(&drives, NULL, &num_files);
+ return drives;
+}
+
+/*!
+ Return a string containing the default CD device.
+ */
+char *
+cdio_get_default_device_cdrdao(void)
+{
+ char **drives = cdio_get_devices_nrg();
+ char *drive = (drives[0] == NULL) ? NULL : strdup(drives[0]);
+ cdio_free_device_list(drives);
+ return drive;
+}
+
+static bool
+get_hwinfo_cdrdao ( const CdIo *p_cdio, /*out*/ cdio_hwinfo_t *hw_info)
+{
+ strcpy(hw_info->psz_vendor, "libcdio");
+ strcpy(hw_info->psz_model, "cdrdao");
+ strcpy(hw_info->psz_revision, CDIO_VERSION);
+ return true;
+
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_format_t
+_get_track_format_cdrdao(void *user_data, track_t i_track)
+{
+ _img_private_t *env = user_data;
+
+ if (!env->gen.init) _init_cdrdao(env);
+
+ if (i_track > env->gen.i_tracks || i_track == 0)
+ return TRACK_FORMAT_ERROR;
+
+ return env->tocent[i_track-env->gen.i_first_track].track_format;
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_get_track_green_cdrdao(void *user_data, track_t i_track)
+{
+ _img_private_t *env = user_data;
+
+ if (!env->gen.init) _init_cdrdao(env);
+
+ if (i_track > env->gen.i_tracks || i_track == 0)
+ return false;
+
+ return env->tocent[i_track-env->gen.i_first_track].track_green;
+}
+
+/*!
+ Return the starting LSN track number
+ i_track in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using i_track LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static lba_t
+_get_lba_track_cdrdao(void *user_data, track_t i_track)
+{
+ _img_private_t *env = user_data;
+ _init_cdrdao (env);
+
+ if (i_track == CDIO_CDROM_LEADOUT_TRACK)
+ i_track = env->gen.i_tracks+1;
+
+ if (i_track <= env->gen.i_tracks+1 && i_track != 0) {
+ return env->tocent[i_track-1].start_lba;
+ } else
+ return CDIO_INVALID_LBA;
+}
+
+/*!
+ Check that a TOC file is valid. We parse the entire file.
+
+*/
+bool
+cdio_is_tocfile(const char *psz_cue_name)
+{
+ int i;
+
+ if (psz_cue_name == NULL) return false;
+
+ i=strlen(psz_cue_name)-strlen("toc");
+
+ if (i>0) {
+ if ( (psz_cue_name[i]=='t' && psz_cue_name[i+1]=='o' && psz_cue_name[i+2]=='c')
+ || (psz_cue_name[i]=='T' && psz_cue_name[i+1]=='O' && psz_cue_name[i+2]=='C') ) {
+ return parse_tocfile(NULL, psz_cue_name);
+ }
+ }
+ return false;
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_am_cdrdao (const char *psz_source_name, const char *psz_access_mode)
+{
+ if (psz_access_mode != NULL && strcmp(psz_access_mode, "image"))
+ cdio_warn ("there is only one access mode, 'image' for cdrdao. Arg %s ignored",
+ psz_access_mode);
+ return cdio_open_cdrdao(psz_source_name);
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_cdrdao (const char *psz_cue_name)
+{
+ CdIo *ret;
+ _img_private_t *_data;
+
+ cdio_funcs _funcs;
+
+ memset( &_funcs, 0, sizeof(_funcs) );
+
+ _funcs.eject_media = _eject_media_image;
+ _funcs.free = _free_image;
+ _funcs.get_arg = _get_arg_image;
+ _funcs.get_cdtext = get_cdtext_generic;
+ _funcs.get_devices = cdio_get_devices_cdrdao;
+ _funcs.get_default_device = cdio_get_default_device_cdrdao;
+ _funcs.get_discmode = _get_discmode_image;
+ _funcs.get_drive_cap = _get_drive_cap_image;
+ _funcs.get_first_track_num= _get_first_track_num_image;
+ _funcs.get_hwinfo = get_hwinfo_cdrdao;
+ _funcs.get_mcn = _get_mcn_image;
+ _funcs.get_num_tracks = _get_num_tracks_image;
+ _funcs.get_track_format = _get_track_format_cdrdao;
+ _funcs.get_track_green = _get_track_green_cdrdao;
+ _funcs.get_track_lba = _get_lba_track_cdrdao;
+ _funcs.get_track_msf = _get_track_msf_image;
+ _funcs.lseek = _lseek_cdrdao;
+ _funcs.read = _read_cdrdao;
+ _funcs.read_audio_sectors = _read_audio_sectors_cdrdao;
+ _funcs.read_mode1_sector = _read_mode1_sector_cdrdao;
+ _funcs.read_mode1_sectors = _read_mode1_sectors_cdrdao;
+ _funcs.read_mode2_sector = _read_mode2_sector_cdrdao;
+ _funcs.read_mode2_sectors = _read_mode2_sectors_cdrdao;
+ _funcs.set_arg = _set_arg_image;
+ _funcs.stat_size = _stat_size_cdrdao;
+
+ if (NULL == psz_cue_name) return NULL;
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->gen.init = false;
+ _data->psz_cue_name = NULL;
+ _data->gen.data_source = NULL;
+ _data->gen.source_name = NULL;
+
+ ret = cdio_new ((void *)_data, &_funcs);
+
+ if (ret == NULL) {
+ free(_data);
+ return NULL;
+ }
+
+ if (!cdio_is_tocfile(psz_cue_name)) {
+ cdio_debug ("source name %s is not recognized as a TOC file",
+ psz_cue_name);
+ return NULL;
+ }
+
+ _set_arg_image (_data, "cue", psz_cue_name);
+ _set_arg_image (_data, "source", psz_cue_name);
+
+ if (_init_cdrdao(_data)) {
+ return ret;
+ } else {
+ _free_image(_data);
+ free(ret);
+ return NULL;
+ }
+}
+
+bool
+cdio_have_cdrdao (void)
+{
+ return true;
+}
diff --git a/contrib/libcdio/image/nrg.c b/contrib/libcdio/image/nrg.c
new file mode 100644
index 000000000..40e5bbbf7
--- /dev/null
+++ b/contrib/libcdio/image/nrg.c
@@ -0,0 +1,1274 @@
+/*
+ $Id: nrg.c,v 1.3 2005/05/07 22:07:27 rockyb Exp $
+
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+ Copyright (C) 2001, 2003 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*! This code implements low-level access functions for the Nero native
+ CD-image format residing inside a disk file (*.nrg).
+*/
+
+#include "image.h"
+
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_GLOB_H
+#include <glob.h>
+#endif
+
+#include <cdio/bytesex.h>
+#include <cdio/ds.h>
+#include <cdio/logging.h>
+#include <cdio/util.h>
+#include <cdio/version.h>
+#include "cdio_assert.h"
+#include "_cdio_stdio.h"
+#include "nrg.h"
+
+static const char _rcsid[] = "$Id: nrg.c,v 1.3 2005/05/07 22:07:27 rockyb Exp $";
+
+
+/* reader */
+
+#define DEFAULT_CDIO_DEVICE "image.nrg"
+
+/*
+ Link element of track structure as a linked list.
+ Possibly redundant with above track_info_t */
+typedef struct {
+ uint32_t start_lsn;
+ uint32_t sec_count; /* Number of sectors in track. Does not
+ include pregap before next entry. */
+ uint64_t img_offset; /* Bytes offset from beginning of disk image file.*/
+ uint32_t blocksize; /* Number of bytes in a block */
+ int flags; /* don't copy, 4 channel, pre-emphasis */
+} _mapping_t;
+
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+ internal_position_t pos;
+
+ /* This is common to all image drivers... */
+ char *psz_cue_name;
+ char *psz_mcn; /* Media Catalog Number (5.22.3) */
+
+ track_info_t tocent[CDIO_CD_MAX_TRACKS+1]; /* entry info for each track
+ add 1 for leadout. */
+ discmode_t disc_mode;
+
+ /* Nero Specific stuff. Note: for the image_free to work, this *must*
+ be last. */
+ bool is_dao; /* True if some of disk at once. False
+ if some sort of track at once. */
+ uint32_t mtyp; /* Value of MTYP (media type?) tag */
+ uint8_t dtyp; /* Value of DAOX media type tag */
+
+ /* This is a hack because I don't really understnad NERO better. */
+ bool is_cues;
+
+ CdioList *mapping; /* List of track information */
+ uint32_t size;
+} _img_private_t;
+
+static bool parse_nrg (_img_private_t *env, const char *psz_cue_name);
+static uint32_t _stat_size_nrg (void *user_data);
+
+#include "image_common.h"
+
+/* Updates internal track TOC, so we can later
+ simulate ioctl(CDROMREADTOCENTRY).
+ */
+static void
+_register_mapping (_img_private_t *env, lsn_t start_lsn, uint32_t sec_count,
+ uint64_t img_offset, uint32_t blocksize,
+ track_format_t track_format, bool track_green,
+ int flags)
+{
+ const int track_num=env->gen.i_tracks;
+ track_info_t *this_track=&(env->tocent[env->gen.i_tracks]);
+ _mapping_t *_map = _cdio_malloc (sizeof (_mapping_t));
+
+ _map->start_lsn = start_lsn;
+ _map->sec_count = sec_count;
+ _map->img_offset = img_offset;
+ _map->blocksize = blocksize;
+ _map->flags = flags;
+
+ if (!env->mapping) env->mapping = _cdio_list_new ();
+ _cdio_list_append (env->mapping, _map);
+
+ env->size = MAX (env->size, (start_lsn + sec_count));
+
+ /* Update *this_track and track_num. These structures are
+ in a sense redundant witht the obj->mapping list. Perhaps one
+ or the other can be eliminated.
+ */
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(start_lsn), &(this_track->start_msf));
+ this_track->start_lba = cdio_msf_to_lba(&this_track->start_msf);
+ this_track->track_num = track_num+1;
+ this_track->blocksize = blocksize;
+ if (env->is_cues)
+ this_track->datastart = img_offset;
+ else
+ this_track->datastart = 0;
+
+ if (track_green)
+ this_track->datastart += CDIO_CD_SUBHEADER_SIZE;
+
+ this_track->sec_count = sec_count;
+
+ this_track->track_format= track_format;
+ this_track->track_green = track_green;
+
+ switch (this_track->track_format) {
+ case TRACK_FORMAT_AUDIO:
+ this_track->blocksize = CDIO_CD_FRAMESIZE_RAW;
+ this_track->datasize = CDIO_CD_FRAMESIZE_RAW;
+ /*this_track->datastart = 0;*/
+ this_track->endsize = 0;
+ break;
+ case TRACK_FORMAT_CDI:
+ this_track->datasize=CDIO_CD_FRAMESIZE;
+ break;
+ case TRACK_FORMAT_XA:
+ if (track_green) {
+ this_track->blocksize = CDIO_CD_FRAMESIZE;
+ /*this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE;*/
+ this_track->datasize = M2RAW_SECTOR_SIZE;
+ this_track->endsize = 0;
+ } else {
+ /*this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE +
+ CDIO_CD_SUBHEADER_SIZE;*/
+ this_track->datasize = CDIO_CD_FRAMESIZE;
+ this_track->endsize = CDIO_CD_SYNC_SIZE + CDIO_CD_ECC_SIZE;
+ }
+ break;
+ case TRACK_FORMAT_DATA:
+ if (track_green) {
+ /*this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE;*/
+ this_track->datasize = CDIO_CD_FRAMESIZE;
+ this_track->endsize = CDIO_CD_EDC_SIZE + CDIO_CD_M1F1_ZERO_SIZE
+ + CDIO_CD_ECC_SIZE;
+ } else {
+ /* Is the below correct? */
+ /*this_track->datastart = 0;*/
+ this_track->datasize = CDIO_CD_FRAMESIZE;
+ this_track->endsize = 0;
+ }
+ break;
+ default:
+ /*this_track->datasize=CDIO_CD_FRAMESIZE_RAW;*/
+ cdio_warn ("track %d has unknown format %d",
+ env->gen.i_tracks, this_track->track_format);
+ }
+
+ env->gen.i_tracks++;
+
+ cdio_debug ("start lsn: %lu sector count: %0lu -> %8ld (%08lx)",
+ (long unsigned int) start_lsn,
+ (long unsigned int) sec_count,
+ (long unsigned int) img_offset,
+ (long unsigned int) img_offset);
+}
+
+
+/*
+ Disk and track information for a Nero file are located at the end
+ of the file. This routine extracts that information.
+
+ FIXME: right now psz_nrg_name is not used. It will be in the future.
+ */
+static bool
+parse_nrg (_img_private_t *env, const char *psz_nrg_name)
+{
+ long unsigned int footer_start;
+ long unsigned int size;
+ char *footer_buf = NULL;
+ cdio_log_level_t log_level = (NULL == env) ? CDIO_LOG_INFO : CDIO_LOG_WARN;
+
+ size = cdio_stream_stat (env->gen.data_source);
+ if (-1 == size) return false;
+
+ {
+ _footer_t buf;
+ cdio_assert (sizeof (buf) == 12);
+
+ cdio_stream_seek (env->gen.data_source, size - sizeof (buf), SEEK_SET);
+ cdio_stream_read (env->gen.data_source, (void *) &buf, sizeof (buf), 1);
+
+ if (buf.v50.ID == UINT32_TO_BE (NERO_ID)) {
+ cdio_info ("detected Nero version 5.0 (32-bit offsets) NRG magic");
+ footer_start = uint32_to_be (buf.v50.footer_ofs);
+ } else if (buf.v55.ID == UINT32_TO_BE (NER5_ID)) {
+ cdio_info ("detected Nero version 5.5.x (64-bit offsets) NRG magic");
+ footer_start = uint64_from_be (buf.v55.footer_ofs);
+ } else {
+ cdio_log (log_level, "Image not recognized as either version 5.0 or "
+ "version 5.5.x-6.x type NRG");
+ return false;
+ }
+
+ cdio_debug (".NRG footer start = %ld, length = %ld",
+ (long) footer_start, (long) (size - footer_start));
+
+ cdio_assert (IN ((size - footer_start), 0, 4096));
+
+ footer_buf = _cdio_malloc (size - footer_start);
+
+ cdio_stream_seek (env->gen.data_source, footer_start, SEEK_SET);
+ cdio_stream_read (env->gen.data_source, footer_buf,
+ size - footer_start, 1);
+ }
+ {
+ int pos = 0;
+
+ while (pos < size - footer_start) {
+ _chunk_t *chunk = (void *) (footer_buf + pos);
+ uint32_t opcode = UINT32_FROM_BE (chunk->id);
+
+ bool break_out = false;
+
+ switch (opcode) {
+
+ case CUES_ID: /* "CUES" Seems to have sector size 2336 and 150 sector
+ pregap seems to be included at beginning of image.
+ */
+ case CUEX_ID: /* "CUEX" */
+ {
+ unsigned entries = UINT32_FROM_BE (chunk->len);
+ _cuex_array_t *_entries = (void *) chunk->data;
+
+ cdio_assert (env->mapping == NULL);
+
+ cdio_assert ( sizeof (_cuex_array_t) == 8 );
+ cdio_assert ( UINT32_FROM_BE (chunk->len) % sizeof(_cuex_array_t)
+ == 0 );
+
+ entries /= sizeof (_cuex_array_t);
+
+ if (CUES_ID == opcode) {
+ lsn_t lsn = UINT32_FROM_BE (_entries[0].lsn);
+ int idx;
+
+ cdio_info ("CUES type image detected" );
+
+ /* CUES LSN has 150 pregap include at beginning? -/
+ cdio_assert (lsn == 0?);
+ */
+
+ env->is_cues = true; /* HACK alert. */
+ env->gen.i_tracks = 0;
+ env->gen.i_first_track = 1;
+ for (idx = 1; idx < entries-1; idx += 2) {
+ lsn_t sec_count;
+ int addrtype = _entries[idx].addr_ctrl / 16;
+ int control = _entries[idx].addr_ctrl % 16;
+ int flags = 0;
+ if ( 1 == control )
+ flags &= ~CDIO_TRACK_FLAG_COPY_PERMITTED;
+
+ cdio_assert (_entries[idx].track == _entries[idx + 1].track);
+
+ /* lsn and sec_count*2 aren't correct, but it comes closer on the
+ single example I have: svcdgs.nrg
+ We are picking up the wrong fields and/or not interpreting
+ them correctly.
+ */
+
+ switch (addrtype) {
+ case 0:
+ lsn = UINT32_FROM_BE (_entries[idx].lsn);
+ break;
+ case 1:
+ {
+#if 0
+ msf_t msf = (msf_t) _entries[idx].lsn;
+ lsn = cdio_msf_to_lsn(&msf);
+#else
+ lsn = CDIO_INVALID_LSN;
+#endif
+ cdio_warn ("untested (i.e. probably wrong) CUE MSF code");
+ break;
+ }
+ default:
+ lsn = CDIO_INVALID_LSN;
+ cdio_warn("unknown addrtype %d", addrtype);
+ }
+
+ sec_count = UINT32_FROM_BE (_entries[idx + 1].lsn);
+
+ _register_mapping (env, lsn, sec_count*2,
+ (lsn+CDIO_PREGAP_SECTORS) * M2RAW_SECTOR_SIZE,
+ M2RAW_SECTOR_SIZE, TRACK_FORMAT_XA, true,
+ flags);
+ }
+ } else {
+ lsn_t lsn = UINT32_FROM_BE (_entries[0].lsn);
+ int idx;
+
+ cdio_info ("CUEX type image detected");
+
+ /* LSN must start at -150 (LBA 0)? */
+ cdio_assert (lsn == -150);
+
+ for (idx = 2; idx < entries; idx += 2) {
+ lsn_t sec_count;
+ int addrtype = _entries[idx].addr_ctrl >> 4;
+ int control = _entries[idx].addr_ctrl & 0xf;
+ int flags = 0;
+ if ( 1 == control )
+ flags &= ~CDIO_TRACK_FLAG_COPY_PERMITTED;
+
+ /* extractnrg.pl has addrtype for LBA's 0, and
+ for MSF 1. ???
+
+ FIXME: Should decode as appropriate for addrtype.
+ */
+ cdio_assert ( addrtype == 0 || addrtype == 1 );
+
+ cdio_assert (_entries[idx].track != _entries[idx + 1].track);
+
+ lsn = UINT32_FROM_BE (_entries[idx].lsn);
+ sec_count = UINT32_FROM_BE (_entries[idx + 1].lsn);
+
+ _register_mapping (env, lsn, sec_count - lsn,
+ (lsn + CDIO_PREGAP_SECTORS)*M2RAW_SECTOR_SIZE,
+ M2RAW_SECTOR_SIZE, TRACK_FORMAT_XA, true,
+ flags);
+ }
+ }
+ break;
+ }
+
+ case DAOX_ID: /* "DAOX" */
+ case DAOI_ID: /* "DAOI" */
+ {
+ track_format_t track_format;
+ int form2;
+
+ /* We include an extra 0 byte so these can be used as C strings.*/
+ env->psz_mcn = _cdio_malloc (CDIO_MCN_SIZE+1);
+
+ if (DAOX_ID == opcode) {
+ _daox_array_t *_entries = (void *) chunk->data;
+ form2 = _entries->_unknown[1];
+ env->dtyp = _entries->_unknown[19];
+ memcpy(env->psz_mcn, &(_entries->psz_mcn), CDIO_MCN_SIZE);
+ env->psz_mcn[CDIO_MCN_SIZE] = '\0';
+ } else {
+ _daoi_array_t *_entries = (void *) chunk->data;
+ form2 = _entries->_unknown[1];
+ env->dtyp = _entries->_unknown[19];
+ memcpy(env->psz_mcn, &(_entries->psz_mcn), CDIO_MCN_SIZE);
+ env->psz_mcn[CDIO_MCN_SIZE] = '\0';
+ }
+
+ env->is_dao = true;
+ cdio_debug ("DAO%c tag detected, track format %d, form %x\n",
+ opcode==DAOX_ID ? 'X': 'I', env->dtyp, form2);
+ switch (env->dtyp) {
+ case 0:
+ /* Mode 1 */
+ track_format = TRACK_FORMAT_DATA;
+ env->disc_mode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case 2:
+ /* Mode 2 form 1 */
+ form2 = 0;
+ track_format = TRACK_FORMAT_XA;
+ env->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case 3:
+ /* Mode 2 */
+ track_format = TRACK_FORMAT_XA;
+ env->disc_mode = CDIO_DISC_MODE_CD_XA; /* ?? */
+ break;
+ case 0x6:
+ /* Mode2 form mix */
+ track_format = TRACK_FORMAT_XA;
+ env->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ case 0x20: /* ??? Mode2 form 2, Mode2 raw?? */
+ track_format = TRACK_FORMAT_XA;
+ env->disc_mode = CDIO_DISC_MODE_CD_XA; /* ??. */
+ break;
+ case 0x7:
+ track_format = TRACK_FORMAT_AUDIO;
+ env->disc_mode = CDIO_DISC_MODE_CD_DA;
+ break;
+ default:
+ cdio_log (log_level, "Unknown track format %x\n",
+ env->dtyp);
+ track_format = TRACK_FORMAT_AUDIO;
+ }
+ if (0 == form2) {
+ int i;
+ for (i=0; i<env->gen.i_tracks; i++) {
+ cdtext_init (&(env->gen.cdtext_track[i]));
+ env->tocent[i].track_format= track_format;
+ env->tocent[i].datastart = 0;
+ env->tocent[i].track_green = false;
+ if (TRACK_FORMAT_AUDIO == track_format) {
+ env->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW;
+ env->tocent[i].datasize = CDIO_CD_FRAMESIZE_RAW;
+ env->tocent[i].endsize = 0;
+ } else {
+ env->tocent[i].datasize = CDIO_CD_FRAMESIZE;
+ env->tocent[i].datastart = 0;
+ }
+ }
+ } else if (2 == form2) {
+ int i;
+ for (i=0; i<env->gen.i_tracks; i++) {
+ cdtext_init (&(env->gen.cdtext_track[i]));
+ env->tocent[i].track_green = true;
+ env->tocent[i].track_format= track_format;
+ env->tocent[i].datasize = CDIO_CD_FRAMESIZE;
+ if (TRACK_FORMAT_XA == track_format) {
+ env->tocent[i].datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE;
+ env->tocent[i].endsize = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_ECC_SIZE;
+ } else {
+ env->tocent[i].datastart = CDIO_CD_SYNC_SIZE
+ + CDIO_CD_HEADER_SIZE;
+ env->tocent[i].endsize = CDIO_CD_EDC_SIZE
+ + CDIO_CD_M1F1_ZERO_SIZE + CDIO_CD_ECC_SIZE;
+
+ }
+ }
+ } else {
+ cdio_log (log_level, "Don't know if form1 or form2 form2: %x\n",
+ form2);
+ }
+ break;
+ }
+ case NERO_ID:
+ case NER5_ID:
+ cdio_error ("unexpected nrg magic ID NER%c detected",
+ opcode==NERO_ID ? 'O': '5');
+ free(footer_buf);
+ return false;
+ break;
+
+ case END1_ID: /* "END!" */
+ cdio_debug ("nrg end tag detected");
+ break_out = true;
+ break;
+
+ case ETNF_ID: /* "ETNF" */ {
+ unsigned entries = UINT32_FROM_BE (chunk->len);
+ _etnf_array_t *_entries = (void *) chunk->data;
+
+ cdio_assert (env->mapping == NULL);
+
+ cdio_assert ( sizeof (_etnf_array_t) == 20 );
+ cdio_assert ( UINT32_FROM_BE(chunk->len) % sizeof(_etnf_array_t)
+ == 0 );
+
+ entries /= sizeof (_etnf_array_t);
+
+ cdio_info ("SAO type image (ETNF) detected");
+
+ {
+ int idx;
+ for (idx = 0; idx < entries; idx++) {
+ uint32_t _len = UINT32_FROM_BE (_entries[idx].length);
+ uint32_t _start = UINT32_FROM_BE (_entries[idx].start_lsn);
+ uint32_t _start2 = UINT32_FROM_BE (_entries[idx].start);
+ uint32_t track_mode= uint32_from_be (_entries[idx].type);
+ bool track_green = true;
+ track_format_t track_format = TRACK_FORMAT_XA;
+ uint16_t blocksize;
+
+ switch (track_mode) {
+ case 0:
+ /* Mode 1 */
+ track_format = TRACK_FORMAT_DATA;
+ track_green = false; /* ?? */
+ blocksize = CDIO_CD_FRAMESIZE;
+ env->disc_mode = CDIO_DISC_MODE_CD_DATA;
+ break;
+ case 2:
+ /* Mode 2 form 1 */
+ track_format = TRACK_FORMAT_XA;
+ track_green = false; /* ?? */
+ blocksize = CDIO_CD_FRAMESIZE;
+ env->disc_mode = CDIO_DISC_MODE_CD_XA;
+ break;
+ case 3:
+ /* Mode 2 */
+ track_format = TRACK_FORMAT_XA;
+ track_green = true;
+ blocksize = M2RAW_SECTOR_SIZE;
+ env->disc_mode = CDIO_DISC_MODE_CD_XA; /* ?? */
+ break;
+ case 06:
+ /* Mode2 form mix */
+ track_format = TRACK_FORMAT_XA;
+ track_green = true;
+ blocksize = M2RAW_SECTOR_SIZE;
+ env->disc_mode = CDIO_DISC_MODE_CD_MIXED;
+ break;
+ case 0x20: /* ??? Mode2 form 2, Mode2 raw?? */
+ track_format = TRACK_FORMAT_XA;
+ track_green = true;
+ blocksize = M2RAW_SECTOR_SIZE;
+ env->disc_mode = CDIO_DISC_MODE_CD_XA; /* ??. */
+ break;
+ case 7:
+ track_format = TRACK_FORMAT_AUDIO;
+ track_green = false;
+ blocksize = CDIO_CD_FRAMESIZE_RAW;
+ env->disc_mode = CDIO_DISC_MODE_CD_DA;
+ break;
+ default:
+ cdio_log (log_level,
+ "Don't know how to handle track mode (%lu)?",
+ (long unsigned int) track_mode);
+ free(footer_buf);
+ return false;
+ }
+
+ cdio_assert (_len % blocksize == 0);
+
+ _len /= blocksize;
+
+ cdio_assert (_start * blocksize == _start2);
+
+ _start += idx * CDIO_PREGAP_SECTORS;
+ _register_mapping (env, _start, _len, _start2, blocksize,
+ track_format, track_green, 0);
+
+ }
+ }
+ break;
+ }
+
+ case ETN2_ID: { /* "ETN2", same as above, but with 64bit stuff instead */
+ unsigned entries = uint32_from_be (chunk->len);
+ _etn2_array_t *_entries = (void *) chunk->data;
+
+ cdio_assert (env->mapping == NULL);
+
+ cdio_assert (sizeof (_etn2_array_t) == 32);
+ cdio_assert (uint32_from_be (chunk->len) % sizeof (_etn2_array_t) == 0);
+
+ entries /= sizeof (_etn2_array_t);
+
+ cdio_info ("SAO type image (ETN2) detected");
+
+ {
+ int idx;
+ for (idx = 0; idx < entries; idx++) {
+ uint32_t _len = uint64_from_be (_entries[idx].length);
+ uint32_t _start = uint32_from_be (_entries[idx].start_lsn);
+ uint32_t _start2 = uint64_from_be (_entries[idx].start);
+ uint32_t track_mode= uint32_from_be (_entries[idx].type);
+ bool track_green = true;
+ track_format_t track_format = TRACK_FORMAT_XA;
+ uint16_t blocksize;
+
+
+ switch (track_mode) {
+ case 0:
+ track_format = TRACK_FORMAT_DATA;
+ track_green = false; /* ?? */
+ blocksize = CDIO_CD_FRAMESIZE;
+ break;
+ case 2:
+ track_format = TRACK_FORMAT_XA;
+ track_green = false; /* ?? */
+ blocksize = CDIO_CD_FRAMESIZE;
+ break;
+ case 3:
+ track_format = TRACK_FORMAT_XA;
+ track_green = true;
+ blocksize = M2RAW_SECTOR_SIZE;
+ break;
+ case 7:
+ track_format = TRACK_FORMAT_AUDIO;
+ track_green = false;
+ blocksize = CDIO_CD_FRAMESIZE_RAW;
+ break;
+ default:
+ cdio_log (log_level,
+ "Don't know how to handle track mode (%lu)?",
+ (long unsigned int) track_mode);
+ free(footer_buf);
+ return false;
+ }
+
+ if (_len % blocksize != 0) {
+ cdio_log (log_level,
+ "length is not a multiple of blocksize "
+ "len %lu, size %d, rem %lu",
+ (long unsigned int) _len, blocksize,
+ (long unsigned int) _len % blocksize);
+ if (0 == _len % CDIO_CD_FRAMESIZE) {
+ cdio_log(log_level, "Adjusting blocksize to %d",
+ CDIO_CD_FRAMESIZE);
+ blocksize = CDIO_CD_FRAMESIZE;
+ } else if (0 == _len % M2RAW_SECTOR_SIZE) {
+ cdio_log(log_level,
+ "Adjusting blocksize to %d", M2RAW_SECTOR_SIZE);
+ blocksize = M2RAW_SECTOR_SIZE;
+ } else if (0 == _len % CDIO_CD_FRAMESIZE_RAW) {
+ cdio_log(log_level,
+ "Adjusting blocksize to %d", CDIO_CD_FRAMESIZE_RAW);
+ blocksize = CDIO_CD_FRAMESIZE_RAW;
+ }
+ }
+
+ _len /= blocksize;
+
+ if (_start * blocksize != _start2) {
+ cdio_log (log_level,
+ "%lu * %d != %lu",
+ (long unsigned int) _start, blocksize,
+ (long unsigned int) _start2);
+ if (_start * CDIO_CD_FRAMESIZE == _start2) {
+ cdio_log(log_level,
+ "Adjusting blocksize to %d", CDIO_CD_FRAMESIZE);
+ blocksize = CDIO_CD_FRAMESIZE;
+ } else if (_start * M2RAW_SECTOR_SIZE == _start2) {
+ cdio_log(log_level,
+ "Adjusting blocksize to %d", M2RAW_SECTOR_SIZE);
+ blocksize = M2RAW_SECTOR_SIZE;
+ } else if (_start * CDIO_CD_FRAMESIZE_RAW == _start2) {
+ cdio_log(log_level,
+ "Adjusting blocksize to %d", CDIO_CD_FRAMESIZE_RAW);
+ blocksize = CDIO_CD_FRAMESIZE_RAW;
+ }
+ }
+
+ _start += idx * CDIO_PREGAP_SECTORS;
+ _register_mapping (env, _start, _len, _start2, blocksize,
+ track_format, track_green, 0);
+ }
+ }
+ break;
+ }
+
+ case SINF_ID: { /* "SINF" */
+
+ uint32_t *_sessions = (void *) chunk->data;
+
+ cdio_assert (UINT32_FROM_BE (chunk->len) == 4);
+
+ cdio_debug ("SINF: %lu sessions",
+ (long unsigned int) UINT32_FROM_BE (*_sessions));
+ }
+ break;
+
+ case MTYP_ID: { /* "MTYP" */
+ uint32_t *mtyp_p = (void *) chunk->data;
+ uint32_t mtyp = UINT32_FROM_BE (*mtyp_p);
+
+ cdio_assert (UINT32_FROM_BE (chunk->len) == 4);
+
+ cdio_debug ("MTYP: %lu",
+ (long unsigned int) UINT32_FROM_BE (*mtyp_p));
+
+ if (mtyp != MTYP_AUDIO_CD) {
+ cdio_log (log_level,
+ "Unknown MTYP value: %u", (unsigned int) mtyp);
+ }
+ env->mtyp = mtyp;
+ }
+ break;
+
+ case CDTX_ID: { /* "CD TEXT" */
+
+ cdio_log (log_level,
+ "Don't know how to handle CD TEXT yet" );
+ break;
+ }
+
+ default:
+ cdio_log (log_level,
+ "unknown tag %8.8x seen",
+ (unsigned int) UINT32_FROM_BE (chunk->id));
+ break;
+ }
+
+ if (break_out)
+ break;
+
+ pos += 8;
+ pos += UINT32_FROM_BE (chunk->len);
+ }
+ }
+
+ /* Fake out leadout track. */
+ /* Don't use _stat_size_nrg since that will lead to recursion since
+ we haven't fully initialized things yet.
+ */
+ cdio_lsn_to_msf (env->size, &env->tocent[env->gen.i_tracks].start_msf);
+ env->tocent[env->gen.i_tracks].start_lba = cdio_lsn_to_lba(env->size);
+ env->tocent[env->gen.i_tracks-1].sec_count =
+ cdio_lsn_to_lba(env->size - env->tocent[env->gen.i_tracks-1].start_lba);
+
+ env->gen.b_cdtext_init = true;
+ env->gen.b_cdtext_error = false;
+ env->gen.toc_init = true;
+ free(footer_buf);
+ return true;
+}
+
+/*!
+ Initialize image structures.
+ */
+static bool
+_init_nrg (_img_private_t *env)
+{
+ if (env->gen.init) {
+ cdio_error ("init called more than once");
+ return false;
+ }
+
+ if (!(env->gen.data_source = cdio_stdio_new (env->gen.source_name))) {
+ cdio_warn ("can't open nrg image file %s for reading",
+ env->gen.source_name);
+ return false;
+ }
+
+ env->psz_mcn = NULL;
+ env->disc_mode = CDIO_DISC_MODE_NO_INFO;
+
+ cdtext_init (&(env->gen.cdtext));
+
+ if ( !parse_nrg (env, env->gen.source_name) ) {
+ cdio_warn ("image file %s is not a Nero image",
+ env->gen.source_name);
+ return false;
+ }
+
+ env->gen.init = true;
+ return true;
+
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Would be libc's seek() but we have to adjust for the extra track header
+ information in each sector.
+*/
+static off_t
+_lseek_nrg (void *user_data, off_t offset, int whence)
+{
+ _img_private_t *env = user_data;
+
+ /* real_offset is the real byte offset inside the disk image
+ The number below was determined empirically.
+ */
+ off_t real_offset= env->is_dao ? 0x4b000 : 0;
+
+ unsigned int i;
+
+ for (i=0; i<env->gen.i_tracks; i++) {
+ track_info_t *this_track=&(env->tocent[i]);
+ env->pos.index = i;
+ if ( (this_track->sec_count*this_track->datasize) >= offset) {
+ int blocks = offset / this_track->datasize;
+ int rem = offset % this_track->datasize;
+ int block_offset = blocks * this_track->blocksize;
+ real_offset += block_offset + rem;
+ env->pos.buff_offset = rem;
+ env->pos.lba += blocks;
+ break;
+ }
+ real_offset += this_track->sec_count*this_track->blocksize;
+ offset -= this_track->sec_count*this_track->datasize;
+ env->pos.lba += this_track->sec_count;
+ }
+
+ if (i==env->gen.i_tracks) {
+ cdio_warn ("seeking outside range of disk image");
+ return -1;
+ } else
+ real_offset += env->tocent[i].datastart;
+ return cdio_stream_seek(env->gen.data_source, real_offset, whence);
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ FIXME:
+ At present we assume a read doesn't cross sector or track
+ boundaries.
+*/
+static ssize_t
+_read_nrg (void *user_data, void *buf, size_t size)
+{
+ _img_private_t *env = user_data;
+ return cdio_stream_read(env->gen.data_source, buf, size, 1);
+}
+
+static uint32_t
+_stat_size_nrg (void *user_data)
+{
+ _img_private_t *env = user_data;
+
+ return env->size;
+}
+
+/*!
+ Reads a single audio sector from CD device into data starting
+ from LSN. Returns 0 if no error.
+ */
+static int
+_read_audio_sectors_nrg (void *user_data, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *env = user_data;
+
+ CdioListNode *node;
+
+ if (lsn >= env->size)
+ {
+ cdio_warn ("trying to read beyond image size (%lu >= %lu)",
+ (long unsigned int) lsn, (long unsigned int) env->size);
+ return -1;
+ }
+
+ _CDIO_LIST_FOREACH (node, env->mapping) {
+ _mapping_t *_map = _cdio_list_node_data (node);
+
+ if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->sec_count - 1))) {
+ int ret;
+ long int img_offset = _map->img_offset;
+
+ img_offset += (lsn - _map->start_lsn) * CDIO_CD_FRAMESIZE_RAW;
+
+ ret = cdio_stream_seek (env->gen.data_source, img_offset,
+ SEEK_SET);
+ if (ret!=0) return ret;
+ ret = cdio_stream_read (env->gen.data_source, data,
+ CDIO_CD_FRAMESIZE_RAW, nblocks);
+ if (ret==0) return ret;
+ break;
+ }
+ }
+
+ if (!node) cdio_warn ("reading into pre gap (lsn %lu)",
+ (long unsigned int) lsn);
+
+ return 0;
+}
+
+static int
+_read_mode1_sector_nrg (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ _img_private_t *env = user_data;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ CdioListNode *node;
+
+ if (lsn >= env->size)
+ {
+ cdio_warn ("trying to read beyond image size (%lu >= %lu)",
+ (long unsigned int) lsn, (long unsigned int) env->size);
+ return -1;
+ }
+
+ _CDIO_LIST_FOREACH (node, env->mapping) {
+ _mapping_t *_map = _cdio_list_node_data (node);
+
+ if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->sec_count - 1))) {
+ int ret;
+ long int img_offset = _map->img_offset;
+
+ img_offset += (lsn - _map->start_lsn) * _map->blocksize;
+
+ ret = cdio_stream_seek (env->gen.data_source, img_offset,
+ SEEK_SET);
+ if (ret!=0) return ret;
+
+ /* FIXME: Not completely sure the below is correct. */
+ ret = cdio_stream_read (env->gen.data_source,
+ (M2RAW_SECTOR_SIZE == _map->blocksize)
+ ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE)
+ : buf,
+ _map->blocksize, 1);
+ if (ret==0) return ret;
+ break;
+ }
+ }
+
+ if (!node)
+ cdio_warn ("reading into pre gap (lsn %lu)", (long unsigned int) lsn);
+
+ memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE,
+ b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode1_sectors_nrg (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned nblocks)
+{
+ _img_private_t *env = user_data;
+ int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode1_sector_nrg (env,
+ ((char *)data) + (blocksize * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+static int
+_read_mode2_sector_nrg (void *user_data, void *data, lsn_t lsn,
+ bool b_form2)
+{
+ _img_private_t *env = user_data;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ CdioListNode *node;
+
+ if (lsn >= env->size)
+ {
+ cdio_warn ("trying to read beyond image size (%lu >= %lu)",
+ (long unsigned int) lsn, (long unsigned int) env->size);
+ return -1;
+ }
+
+ _CDIO_LIST_FOREACH (node, env->mapping) {
+ _mapping_t *_map = _cdio_list_node_data (node);
+
+ if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->sec_count - 1))) {
+ int ret;
+ long int img_offset = _map->img_offset;
+
+ img_offset += (lsn - _map->start_lsn) * _map->blocksize;
+
+ ret = cdio_stream_seek (env->gen.data_source, img_offset,
+ SEEK_SET);
+ if (ret!=0) return ret;
+ ret = cdio_stream_read (env->gen.data_source,
+ (M2RAW_SECTOR_SIZE == _map->blocksize)
+ ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE)
+ : buf,
+ _map->blocksize, 1);
+ if (ret==0) return ret;
+ break;
+ }
+ }
+
+ if (!node)
+ cdio_warn ("reading into pre gap (lsn %lu)", (long unsigned int) lsn);
+
+ if (b_form2)
+ memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE,
+ M2RAW_SECTOR_SIZE);
+ else
+ memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_read_mode2_sectors_nrg (void *user_data, void *data, lsn_t lsn,
+ bool b_form2, unsigned nblocks)
+{
+ _img_private_t *env = user_data;
+ int i;
+ int retval;
+ unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;
+
+ for (i = 0; i < nblocks; i++) {
+ if ( (retval = _read_mode2_sector_nrg (env,
+ ((char *)data) + (blocksize * i),
+ lsn + i, b_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*
+ Free memory resources associated with NRG object.
+*/
+static void
+_free_nrg (void *user_data)
+{
+ _img_private_t *env = user_data;
+
+ if (NULL == env) return;
+ if (NULL != env->mapping)
+ _cdio_list_free (env->mapping, true);
+
+ /* The remaining part of the image is like the other image drivers,
+ so free that in the same way. */
+ _free_image(user_data);
+}
+
+/*!
+ Eject media -- there's nothing to do here except free resources.
+ We always return 2.
+ */
+static int
+_eject_media_nrg(void *obj)
+{
+ _free_nrg (obj);
+ return 2;
+}
+
+/*!
+ Return an array of strings giving possible NRG disk images.
+ */
+char **
+cdio_get_devices_nrg (void)
+{
+ char **drives = NULL;
+ unsigned int num_files=0;
+#ifdef HAVE_GLOB_H
+ unsigned int i;
+ glob_t globbuf;
+ globbuf.gl_offs = 0;
+ glob("*.nrg", GLOB_DOOFFS, NULL, &globbuf);
+ for (i=0; i<globbuf.gl_pathc; i++) {
+ cdio_add_device_list(&drives, globbuf.gl_pathv[i], &num_files);
+ }
+ globfree(&globbuf);
+#else
+ cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &num_files);
+#endif /*HAVE_GLOB_H*/
+ cdio_add_device_list(&drives, NULL, &num_files);
+ return drives;
+}
+
+/*!
+ Return a string containing the default CD device.
+ */
+char *
+cdio_get_default_device_nrg(void)
+{
+ char **drives = cdio_get_devices_nrg();
+ char *drive = (drives[0] == NULL) ? NULL : strdup(drives[0]);
+ cdio_free_device_list(drives);
+ return drive;
+}
+
+static bool
+get_hwinfo_nrg ( const CdIo *p_cdio, /*out*/ cdio_hwinfo_t *hw_info)
+{
+ strcpy(hw_info->psz_vendor, "libcdio");
+ strcpy(hw_info->psz_model, "Nero");
+ strcpy(hw_info->psz_revision, CDIO_VERSION);
+ return true;
+
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_format_t
+get_track_format_nrg(void *user_data, track_t track_num)
+{
+ _img_private_t *env = user_data;
+
+ if (track_num > env->gen.i_tracks || track_num == 0)
+ return TRACK_FORMAT_ERROR;
+
+ if ( env->dtyp != DTYP_INVALID) {
+ switch (env->dtyp) {
+ case DTYP_MODE2_XA:
+ return TRACK_FORMAT_XA;
+ case DTYP_MODE1:
+ return TRACK_FORMAT_DATA;
+ default: ;
+ }
+ }
+
+ /*if ( MTYP_AUDIO_CD == env->mtyp) return TRACK_FORMAT_AUDIO; */
+ return env->tocent[track_num-1].track_format;
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_get_track_green_nrg(void *user_data, track_t track_num)
+{
+ _img_private_t *env = user_data;
+
+ if (track_num > env->gen.i_tracks || track_num == 0)
+ return false;
+
+ if ( MTYP_AUDIO_CD == env->mtyp) return false;
+ return env->tocent[track_num-1].track_green;
+}
+
+/*!
+ Check that a NRG file is valid.
+
+*/
+/* Later we'll probably do better. For now though, this gets us
+ started for now.
+*/
+bool
+cdio_is_nrg(const char *psz_nrg)
+{
+ unsigned int i;
+
+ if (psz_nrg == NULL) return false;
+
+ i=strlen(psz_nrg)-strlen("nrg");
+
+ if (i>0) {
+ if (psz_nrg[i]=='n' && psz_nrg[i+1]=='r' && psz_nrg[i+2]=='g') {
+ return true;
+ }
+ else if (psz_nrg[i]=='N' && psz_nrg[i+1]=='R' && psz_nrg[i+2]=='G') {
+ return true;
+ }
+ }
+ return false;
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_am_nrg (const char *psz_source_name, const char *psz_access_mode)
+{
+ if (psz_access_mode != NULL && strcmp(psz_access_mode, "image"))
+ cdio_warn ("there is only one access mode for nrg. Arg %s ignored",
+ psz_access_mode);
+ return cdio_open_nrg(psz_source_name);
+}
+
+
+CdIo *
+cdio_open_nrg (const char *psz_source)
+{
+ CdIo *ret;
+ _img_private_t *_data;
+
+ cdio_funcs _funcs;
+
+ memset( &_funcs, 0, sizeof(_funcs) );
+
+ _funcs.eject_media = _eject_media_nrg;
+ _funcs.free = _free_nrg;
+ _funcs.get_arg = _get_arg_image;
+ _funcs.get_cdtext = get_cdtext_generic;
+ _funcs.get_devices = cdio_get_devices_nrg;
+ _funcs.get_default_device = cdio_get_default_device_nrg;
+ _funcs.get_discmode = _get_discmode_image;
+ _funcs.get_drive_cap = _get_drive_cap_image;
+ _funcs.get_first_track_num= _get_first_track_num_image;
+ _funcs.get_hwinfo = get_hwinfo_nrg;
+ _funcs.get_mcn = _get_mcn_image;
+ _funcs.get_num_tracks = _get_num_tracks_image;
+ _funcs.get_track_format = get_track_format_nrg;
+ _funcs.get_track_green = _get_track_green_nrg;
+ _funcs.get_track_lba = NULL; /* Will use generic routine via msf */
+ _funcs.get_track_msf = _get_track_msf_image;
+ _funcs.lseek = _lseek_nrg;
+ _funcs.read = _read_nrg;
+ _funcs.read_audio_sectors = _read_audio_sectors_nrg;
+ _funcs.read_mode1_sector = _read_mode1_sector_nrg;
+ _funcs.read_mode1_sectors = _read_mode1_sectors_nrg;
+ _funcs.read_mode2_sector = _read_mode2_sector_nrg;
+ _funcs.read_mode2_sectors = _read_mode2_sectors_nrg;
+ _funcs.set_arg = _set_arg_image;
+ _funcs.stat_size = _stat_size_nrg;
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->gen.init = false;
+
+ _data->gen.i_tracks = 0;
+ _data->mtyp = 0;
+ _data->dtyp = DTYP_INVALID;
+ _data->gen.i_first_track= 1;
+ _data->is_dao = false;
+ _data->is_cues = false; /* FIXME: remove is_cues. */
+
+ ret = cdio_new ((void *)_data, &_funcs);
+
+ if (ret == NULL) {
+ free(_data);
+ return NULL;
+ }
+
+ _set_arg_image(_data, "source", (NULL == psz_source)
+ ? DEFAULT_CDIO_DEVICE: psz_source);
+
+ _data->psz_cue_name = strdup(_get_arg_image(_data, "source"));
+
+ if (!cdio_is_nrg(_data->psz_cue_name)) {
+ cdio_debug ("source name %s is not recognized as a NRG image",
+ _data->psz_cue_name);
+ _free_nrg(_data);
+ return NULL;
+ }
+
+ _set_arg_image (_data, "cue", _data->psz_cue_name);
+
+ if (_init_nrg(_data))
+ return ret;
+ else {
+ _free_nrg(_data);
+ free(ret);
+ return NULL;
+ }
+
+}
+
+bool
+cdio_have_nrg (void)
+{
+ return true;
+}
diff --git a/contrib/libcdio/image/nrg.h b/contrib/libcdio/image/nrg.h
new file mode 100644
index 000000000..2923c78f1
--- /dev/null
+++ b/contrib/libcdio/image/nrg.h
@@ -0,0 +1,115 @@
+/*
+ $Id: nrg.h,v 1.1 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+ Copyright (C) 2001, 2003 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* NERO (NRG) file format structures. */
+
+/* this ugly image format is typical for lazy win32 programmers... at
+ least structure were set big endian, so at reverse
+ engineering wasn't such a big headache... */
+
+PRAGMA_BEGIN_PACKED
+typedef union {
+ struct {
+ uint32_t __x GNUC_PACKED;
+ uint32_t ID GNUC_PACKED;
+ uint32_t footer_ofs GNUC_PACKED;
+ } v50;
+ struct {
+ uint32_t ID GNUC_PACKED;
+ uint64_t footer_ofs GNUC_PACKED;
+ } v55;
+} _footer_t;
+
+typedef struct {
+ uint32_t start GNUC_PACKED;
+ uint32_t length GNUC_PACKED;
+ uint32_t type GNUC_PACKED; /* 0x0 -> MODE1, 0x2 -> MODE2 form1,
+ 0x3 -> MIXED_MODE2 2336 blocksize
+ */
+ uint32_t start_lsn GNUC_PACKED; /* does not include any pre-gaps! */
+ uint32_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */
+} _etnf_array_t;
+
+/* Finally they realized that 32-bit offsets are a bit outdated for
+ IA64 *eg* */
+typedef struct {
+ uint64_t start GNUC_PACKED;
+ uint64_t length GNUC_PACKED;
+ uint32_t type GNUC_PACKED; /* 0x0 -> MODE1, 0x2 -> MODE2 form1,
+ 0x3 -> MIXED_MODE2 2336 blocksize
+ */
+ uint32_t start_lsn GNUC_PACKED;
+ uint64_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */
+} _etn2_array_t;
+
+typedef struct {
+ uint8_t type GNUC_PACKED; /* has track copy bit and whether audiofile
+ or datafile. Is often 0x41 == 'A' */
+ uint8_t track GNUC_PACKED; /* binary or BCD?? */
+ uint8_t addr_ctrl GNUC_PACKED; /* addresstype: MSF or LBA in lower 4 bits
+ control in upper 4 bits.
+ makes 0->1 transitions */
+ uint8_t res GNUC_PACKED; /* ?? */
+ uint32_t lsn GNUC_PACKED;
+} _cuex_array_t;
+
+typedef struct {
+ uint32_t _unknown1 GNUC_PACKED;
+ char psz_mcn[CDIO_MCN_SIZE] GNUC_PACKED;
+ uint8_t _unknown[64-CDIO_MCN_SIZE-sizeof(uint32_t)] GNUC_PACKED;
+} _daox_array_t;
+
+typedef struct {
+ uint32_t _unknown1 GNUC_PACKED;
+ char psz_mcn[CDIO_MCN_SIZE] GNUC_PACKED;
+ uint8_t _unknown[64-CDIO_MCN_SIZE-sizeof(uint32_t)] GNUC_PACKED;
+} _daoi_array_t;
+
+typedef struct {
+ uint32_t id GNUC_PACKED;
+ uint32_t len GNUC_PACKED;
+ char data[EMPTY_ARRAY_SIZE] GNUC_PACKED;
+} _chunk_t;
+
+PRAGMA_END_PACKED
+
+/* Nero images are Big Endian. */
+#define CDTX_ID 0x43445458 /* CD TEXT */
+#define CUEX_ID 0x43554558 /* Nero version 5.5.x-6.x */
+#define CUES_ID 0x43554553 /* Nero pre version 5.5.x-6.x */
+#define DAOX_ID 0x44414f58 /* Nero version 5.5.x-6.x */
+#define DAOI_ID 0x44414f49
+#define END1_ID 0x454e4421
+#define ETN2_ID 0x45544e32
+#define ETNF_ID 0x45544e46
+#define NER5_ID 0x4e455235 /* Nero version 5.5.x */
+#define NERO_ID 0x4e45524f /* Nero pre 5.5.x */
+#define SINF_ID 0x53494e46 /* Session information */
+#define MTYP_ID 0x4d545950 /* Disc Media type? */
+
+#define MTYP_AUDIO_CD 1 /* This isn't correct. But I don't know the
+ the right thing is and it sometimes works (and
+ sometimes is wrong). */
+
+/* Disk track type Values gleaned from DAOX */
+#define DTYP_MODE1 0
+#define DTYP_MODE2_XA 2
+#define DTYP_INVALID 255
diff --git a/contrib/libcdio/image_common.h b/contrib/libcdio/image_common.h
new file mode 100644
index 000000000..30699c15c
--- /dev/null
+++ b/contrib/libcdio/image_common.h
@@ -0,0 +1,225 @@
+/*
+ $Id: image_common.h,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*! Common image routines.
+
+ Because _img_private_t may vary over image formats, the routines are
+ included into the image drivers after _img_private_t is defined. In
+ order for the below routines to work, there is a large part of
+ _img_private_t that is common among image drivers. For example, see
+ image.h
+*/
+
+#ifndef __CDIO_IMAGE_COMMON_H__
+#define __CDIO_IMAGE_COMMON_H__
+
+#define free_if_notnull(obj) \
+ if (NULL != obj) { free(obj); obj=NULL; };
+
+/*!
+ We don't need the image any more. Free all memory associated with
+ it.
+ */
+static void
+_free_image (void *user_data)
+{
+ _img_private_t *p_env = user_data;
+ track_t i_track;
+
+ if (NULL == p_env) return;
+
+ for (i_track=0; i_track < p_env->gen.i_tracks; i_track++) {
+ free_if_notnull(p_env->tocent[i_track].filename);
+ free_if_notnull(p_env->tocent[i_track].isrc);
+ cdtext_destroy(&(p_env->tocent[i_track].cdtext));
+ }
+
+ free_if_notnull(p_env->psz_mcn);
+ free_if_notnull(p_env->psz_cue_name);
+ cdtext_destroy(&(p_env->gen.cdtext));
+ cdio_generic_stdio_free(p_env);
+ free(p_env);
+}
+
+#ifdef NEED_MEDIA_EJECT_IMAGE
+/*!
+ Eject media -- there's nothing to do here except free resources.
+ We always return 2.
+ */
+static int
+_eject_media_image(void *user_data)
+{
+ _free_image (user_data);
+ return 2;
+}
+#endif
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_get_arg_image (void *user_data, const char key[])
+{
+ _img_private_t *p_env = user_data;
+
+ if (!strcmp (key, "source")) {
+ return p_env->gen.source_name;
+ } else if (!strcmp (key, "cue")) {
+ return p_env->psz_cue_name;
+ } else if (!strcmp(key, "access-mode")) {
+ return "image";
+ }
+ return NULL;
+}
+
+/*!
+ Get disc type associated with cd_obj.
+*/
+static discmode_t
+_get_discmode_image (void *p_user_data)
+{
+ _img_private_t *p_env = p_user_data;
+ return p_env->disc_mode;
+}
+
+/*!
+ Return the media catalog number (MCN) from the CD or NULL if there
+ is none or we don't have the ability to get it.
+
+ Note: string is malloc'd so caller has to free() the returned
+ string when done with it.
+ */
+static char *
+_get_mcn_image(const void *user_data)
+{
+ const _img_private_t *env = user_data;
+
+ if (NULL == env || NULL == env->psz_mcn) return NULL;
+ return strdup(env->psz_mcn);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for the track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+
+*/
+static bool
+_get_track_msf_image(void *user_data, track_t track_num, msf_t *msf)
+{
+ _img_private_t *env = user_data;
+
+ if (NULL == msf) return false;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = env->gen.i_tracks+1;
+
+ if (track_num <= env->gen.i_tracks+1 && track_num != 0) {
+ *msf = env->tocent[track_num-env->gen.i_first_track].start_msf;
+ return true;
+ } else
+ return false;
+}
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_get_first_track_num_image(void *user_data)
+{
+ _img_private_t *env = user_data;
+
+ return env->gen.i_first_track;
+}
+
+/*!
+ Return the number of tracks.
+*/
+static track_t
+_get_num_tracks_image(void *user_data)
+{
+ _img_private_t *env = user_data;
+
+ return env->gen.i_tracks;
+}
+
+/*!
+ Set the arg "key" with "value" in the source device.
+ Currently "source" to set the source device in I/O operations
+ is the only valid key.
+
+ 0 is returned if no error was found, and nonzero if there as an error.
+*/
+static int
+_set_arg_image (void *user_data, const char key[], const char value[])
+{
+ _img_private_t *env = user_data;
+
+ if (!strcmp (key, "source"))
+ {
+ free_if_notnull (env->gen.source_name);
+
+ if (!value)
+ return -2;
+
+ env->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "cue"))
+ {
+ free_if_notnull (env->psz_cue_name);
+
+ if (!value)
+ return -2;
+
+ env->psz_cue_name = strdup (value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Return the the kind of drive capabilities of device.
+
+ */
+static void
+_get_drive_cap_image (const void *user_data,
+ cdio_drive_read_cap_t *p_read_cap,
+ cdio_drive_write_cap_t *p_write_cap,
+ cdio_drive_misc_cap_t *p_misc_cap)
+{
+
+ *p_read_cap = CDIO_DRIVE_CAP_READ_AUDIO
+ | CDIO_DRIVE_CAP_READ_CD_G
+ | CDIO_DRIVE_CAP_READ_CD_R
+ | CDIO_DRIVE_CAP_READ_CD_RW
+ ;
+
+ *p_write_cap = 0;
+
+ /* In the future we may want to simulate
+ LOCK, OPEN_TRAY, CLOSE_TRAY, SELECT_SPEED, etc.
+ */
+ *p_misc_cap = CDIO_DRIVE_CAP_MISC_FILE;
+}
+
+#endif /* __CDIO_IMAGE_COMMON_H__ */
diff --git a/contrib/libcdio/iso9660.c b/contrib/libcdio/iso9660.c
new file mode 100644
index 000000000..91b89d516
--- /dev/null
+++ b/contrib/libcdio/iso9660.c
@@ -0,0 +1,1007 @@
+/*
+ $Id: iso9660.c,v 1.4 2006/09/26 18:13:11 dgp85 Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* Private headers */
+#include "iso9660_private.h"
+#include "cdio_assert.h"
+
+/* Public headers */
+#include <cdio/bytesex.h>
+#include <cdio/iso9660.h>
+#include <cdio/util.h>
+
+#include <time.h>
+#include <ctype.h>
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+
+static const char _rcsid[] = "$Id: iso9660.c,v 1.4 2006/09/26 18:13:11 dgp85 Exp $";
+
+/* some parameters... */
+#define SYSTEM_ID "CD-RTOS CD-BRIDGE"
+#define VOLUME_SET_ID ""
+
+/*!
+ Change trailing blanks in str to nulls. Str has a maximum size of
+ n characters.
+*/
+static char *
+strip_trail (const char str[], size_t n)
+{
+ static char buf[1025];
+ int j;
+
+ cdio_assert (n < 1024);
+
+ strncpy (buf, str, n);
+ buf[n] = '\0';
+
+ for (j = strlen (buf) - 1; j >= 0; j--)
+ {
+ if (buf[j] != ' ')
+ break;
+
+ buf[j] = '\0';
+ }
+
+ return buf;
+}
+
+static void
+pathtable_get_size_and_entries(const void *pt, unsigned int *size,
+ unsigned int *entries);
+
+/*!
+ Get time structure from structure in an ISO 9660 directory index
+ record. Even though tm_wday and tm_yday fields are not explicitly in
+ idr_date, the are calculated from the other fields.
+
+ If tm is to reflect the localtime set b_localtime true, otherwise
+ tm will reported in GMT.
+*/
+void
+iso9660_get_dtime (const iso9660_dtime_t *idr_date, bool b_localtime,
+ /*out*/ struct tm *p_tm)
+{
+ time_t t;
+ struct tm *p_temp_tm;
+
+ if (!idr_date) return;
+
+ memset(p_tm, 0, sizeof(struct tm));
+ p_tm->tm_year = idr_date->dt_year;
+ p_tm->tm_mon = idr_date->dt_month - 1;
+ p_tm->tm_mday = idr_date->dt_day;
+ p_tm->tm_hour = idr_date->dt_hour;
+ p_tm->tm_min = idr_date->dt_minute;
+ p_tm->tm_sec = idr_date->dt_second;
+
+#if defined(HAVE_TM_GMTOFF) && defined(HAVE_TZSET)
+ if (b_localtime) {
+ tzset();
+#if defined(HAVE_TZNAME)
+ p_tm->tm_zone = (char *) tzname;
+#endif
+#if defined(HAVE_DAYLIGHT)
+ p_tm->tm_isdst = daylight;
+ p_tm->tm_gmtoff = timezone;
+#endif
+ }
+#endif
+
+ /* Recompute tm_wday and tm_yday via mktime. */
+ t = mktime(p_tm);
+
+ if (b_localtime)
+ p_temp_tm = localtime(&t);
+ else
+ p_temp_tm = gmtime(&t);
+
+ memcpy(p_tm, p_temp_tm, sizeof(struct tm));
+}
+
+/*!
+ Set time in format used in ISO 9660 directory index record
+ from a Unix time structure. */
+void
+iso9660_set_dtime (const struct tm *p_tm, /*out*/ iso9660_dtime_t *p_idr_date)
+{
+ memset (p_idr_date, 0, 7);
+
+ if (!p_tm) return;
+
+ p_idr_date->dt_year = p_tm->tm_year;
+ p_idr_date->dt_month = p_tm->tm_mon + 1;
+ p_idr_date->dt_day = p_tm->tm_mday;
+ p_idr_date->dt_hour = p_tm->tm_hour;
+ p_idr_date->dt_minute = p_tm->tm_min;
+ p_idr_date->dt_second = p_tm->tm_sec;
+
+#ifdef HAVE_TM_GMTOFF
+ /* The ISO 9660 timezone is in the range -48..+52 and each unit
+ represents a 15-minute interval. */
+ p_idr_date->dt_gmtoff = p_tm->tm_gmtoff / (15 * 60);
+
+ if (p_tm->tm_isdst) p_idr_date->dt_gmtoff -= 4;
+
+ if (p_idr_date->dt_gmtoff < -48 ) {
+
+ cdio_warn ("Converted ISO 9660 timezone %d is less than -48. Adjusted",
+ p_idr_date->dt_gmtoff);
+ p_idr_date->dt_gmtoff = -48;
+ } else if (p_idr_date->dt_gmtoff > 52) {
+ cdio_warn ("Converted ISO 9660 timezone %d is over 52. Adjusted",
+ p_idr_date->dt_gmtoff);
+ p_idr_date->dt_gmtoff = 52;
+ }
+#else
+ p_idr_date->dt_gmtoff = 0;
+#endif
+}
+
+/*!
+ Set "long" time in format used in ISO 9660 primary volume descriptor
+ from a Unix time structure. */
+void
+iso9660_set_ltime (const struct tm *_tm, /*out*/ iso9660_ltime_t *pvd_date)
+{
+ char *_pvd_date = (char *) pvd_date;
+
+ memset (_pvd_date, '0', 16);
+ _pvd_date[16] = (int8_t) 0; /* tz */
+
+ if (!_tm) return;
+
+ snprintf(_pvd_date, 17,
+ "%4.4d%2.2d%2.2d" "%2.2d%2.2d%2.2d" "%2.2d",
+ _tm->tm_year + 1900, _tm->tm_mon + 1, _tm->tm_mday,
+ _tm->tm_hour, _tm->tm_min, _tm->tm_sec,
+ 0 /* 1/100 secs */ );
+
+ _pvd_date[16] = (int8_t) 0; /* tz */
+}
+
+/*!
+ Convert ISO-9660 file name that stored in a directory entry into
+ what's usually listed as the file name in a listing.
+ Lowercase name, and remove trailing ;1's or .;1's and
+ turn the other ;'s into version numbers.
+
+ The length of the translated string is returned.
+*/
+int
+iso9660_name_translate(const char *old, char *new)
+{
+ return iso9660_name_translate_ext(old, new, 0);
+}
+
+/*!
+ Convert ISO-9660 file name that stored in a directory entry into
+ what's usually listed as the file name in a listing. Lowercase
+ name if not using Joliet extension. Remove trailing ;1's or .;1's and
+ turn the other ;'s into version numbers.
+
+ The length of the translated string is returned.
+*/
+int
+iso9660_name_translate_ext(const char *old, char *new, uint8_t i_joliet_level)
+{
+ int len = strlen(old);
+ int i;
+
+ for (i = 0; i < len; i++) {
+ unsigned char c = old[i];
+ if (!c)
+ break;
+
+ /* Lower case, unless we have Joliet extensions. */
+ if (!i_joliet_level && isupper(c)) c = tolower(c);
+
+ /* Drop trailing '.;1' (ISO 9660:1988 7.5.1 requires period) */
+ if (c == '.' && i == len - 3 && old[i + 1] == ';' && old[i + 2] == '1')
+ break;
+
+ /* Drop trailing ';1' */
+ if (c == ';' && i == len - 2 && old[i + 1] == '1')
+ break;
+
+ /* Convert remaining ';' to '.' */
+ if (c == ';')
+ c = '.';
+
+ new[i] = c;
+ }
+ new[i] = '\0';
+ return i;
+}
+
+/*!
+ Pad string src with spaces to size len and copy this to dst. If
+ len is less than the length of src, dst will be truncated to the
+ first len characters of src.
+
+ src can also be scanned to see if it contains only ACHARs, DCHARs,
+ 7-bit ASCII chars depending on the enumeration _check.
+
+ In addition to getting changed, dst is the return value.
+ Note: this string might not be NULL terminated.
+ */
+char *
+iso9660_strncpy_pad(char dst[], const char src[], size_t len,
+ enum strncpy_pad_check _check)
+{
+ size_t rlen;
+
+ cdio_assert (dst != NULL);
+ cdio_assert (src != NULL);
+ cdio_assert (len > 0);
+
+ switch (_check)
+ {
+ int idx;
+ case ISO9660_NOCHECK:
+ break;
+
+ case ISO9660_7BIT:
+ for (idx = 0; src[idx]; idx++)
+ if ((int8_t) src[idx] < 0)
+ {
+ cdio_warn ("string '%s' fails 7bit constraint (pos = %d)",
+ src, idx);
+ break;
+ }
+ break;
+
+ case ISO9660_ACHARS:
+ for (idx = 0; src[idx]; idx++)
+ if (!iso9660_isachar (src[idx]))
+ {
+ cdio_warn ("string '%s' fails a-character constraint (pos = %d)",
+ src, idx);
+ break;
+ }
+ break;
+
+ case ISO9660_DCHARS:
+ for (idx = 0; src[idx]; idx++)
+ if (!iso9660_isdchar (src[idx]))
+ {
+ cdio_warn ("string '%s' fails d-character constraint (pos = %d)",
+ src, idx);
+ break;
+ }
+ break;
+
+ default:
+ cdio_assert_not_reached ();
+ break;
+ }
+
+ rlen = strlen (src);
+
+ if (rlen > len)
+ cdio_warn ("string '%s' is getting truncated to %d characters",
+ src, (unsigned int) len);
+
+ strncpy (dst, src, len);
+ if (rlen < len)
+ memset(dst+rlen, ' ', len-rlen);
+ return dst;
+}
+
+/*!
+ Return true if c is a DCHAR - a valid ISO-9660 level 1 character.
+ These are the ASCSII capital letters A-Z, the digits 0-9 and an
+ underscore.
+*/
+bool
+iso9660_isdchar (int c)
+{
+ if (!IN (c, 0x30, 0x5f)
+ || IN (c, 0x3a, 0x40)
+ || IN (c, 0x5b, 0x5e))
+ return false;
+
+ return true;
+}
+
+
+/*!
+ Return true if c is an ACHAR -
+ These are the DCHAR's plus some ASCII symbols including the space
+ symbol.
+*/
+bool
+iso9660_isachar (int c)
+{
+ if (!IN (c, 0x20, 0x5f)
+ || IN (c, 0x23, 0x24)
+ || c == 0x40
+ || IN (c, 0x5b, 0x5e))
+ return false;
+
+ return true;
+}
+
+void
+iso9660_set_evd(void *pd)
+{
+ struct iso_volume_descriptor ied;
+
+ cdio_assert (sizeof(struct iso_volume_descriptor) == ISO_BLOCKSIZE);
+
+ cdio_assert (pd != NULL);
+
+ memset(&ied, 0, sizeof(ied));
+
+ ied.type = to_711(ISO_VD_END);
+ iso9660_strncpy_pad (ied.id, ISO_STANDARD_ID, sizeof(ied.id), ISO9660_DCHARS);
+ ied.version = to_711(ISO_VERSION);
+
+ memcpy(pd, &ied, sizeof(ied));
+}
+
+void
+iso9660_set_pvd(void *pd,
+ const char volume_id[],
+ const char publisher_id[],
+ const char preparer_id[],
+ const char application_id[],
+ uint32_t iso_size,
+ const void *root_dir,
+ uint32_t path_table_l_extent,
+ uint32_t path_table_m_extent,
+ uint32_t path_table_size,
+ const time_t *pvd_time
+ )
+{
+ iso9660_pvd_t ipd;
+
+ cdio_assert (sizeof(iso9660_pvd_t) == ISO_BLOCKSIZE);
+
+ cdio_assert (pd != NULL);
+ cdio_assert (volume_id != NULL);
+ cdio_assert (application_id != NULL);
+
+ memset(&ipd,0,sizeof(ipd)); /* paranoia? */
+
+ /* magic stuff ... thatis CD XA marker... */
+ strcpy(((char*)&ipd)+ISO_XA_MARKER_OFFSET, ISO_XA_MARKER_STRING);
+
+ ipd.type = to_711(ISO_VD_PRIMARY);
+ iso9660_strncpy_pad (ipd.id, ISO_STANDARD_ID, 5, ISO9660_DCHARS);
+ ipd.version = to_711(ISO_VERSION);
+
+ iso9660_strncpy_pad (ipd.system_id, SYSTEM_ID, 32, ISO9660_ACHARS);
+ iso9660_strncpy_pad (ipd.volume_id, volume_id, 32, ISO9660_DCHARS);
+
+ ipd.volume_space_size = to_733(iso_size);
+
+ ipd.volume_set_size = to_723(1);
+ ipd.volume_sequence_number = to_723(1);
+ ipd.logical_block_size = to_723(ISO_BLOCKSIZE);
+
+ ipd.path_table_size = to_733(path_table_size);
+ ipd.type_l_path_table = to_731(path_table_l_extent);
+ ipd.type_m_path_table = to_732(path_table_m_extent);
+
+ /* root_directory_record doesn't contain the 1-byte filename,
+ so we add one for that. */
+ cdio_assert (sizeof(ipd.root_directory_record) == 33);
+ memcpy(&(ipd.root_directory_record), root_dir,
+ sizeof(ipd.root_directory_record));
+ ipd.root_directory_filename='\0';
+ ipd.root_directory_record.length = 33+1;
+ iso9660_strncpy_pad (ipd.volume_set_id, VOLUME_SET_ID, 128, ISO9660_DCHARS);
+
+ iso9660_strncpy_pad (ipd.publisher_id, publisher_id, 128, ISO9660_ACHARS);
+ iso9660_strncpy_pad (ipd.preparer_id, preparer_id, 128, ISO9660_ACHARS);
+ iso9660_strncpy_pad (ipd.application_id, application_id, 128, ISO9660_ACHARS);
+
+ iso9660_strncpy_pad (ipd.copyright_file_id , "", 37, ISO9660_DCHARS);
+ iso9660_strncpy_pad (ipd.abstract_file_id , "", 37, ISO9660_DCHARS);
+ iso9660_strncpy_pad (ipd.bibliographic_file_id, "", 37, ISO9660_DCHARS);
+
+ iso9660_set_ltime (gmtime (pvd_time), &(ipd.creation_date));
+ iso9660_set_ltime (gmtime (pvd_time), &(ipd.modification_date));
+ iso9660_set_ltime (NULL, &(ipd.expiration_date));
+ iso9660_set_ltime (NULL, &(ipd.effective_date));
+
+ ipd.file_structure_version = to_711(1);
+
+ /* we leave ipd.application_data = 0 */
+
+ memcpy(pd, &ipd, sizeof(ipd)); /* copy stuff to arg ptr */
+}
+
+unsigned int
+iso9660_dir_calc_record_size(unsigned int namelen, unsigned int su_len)
+{
+ unsigned int length;
+
+ length = sizeof(iso9660_dir_t);
+ length += namelen;
+ if (length % 2) /* pad to word boundary */
+ length++;
+ length += su_len;
+ if (length % 2) /* pad to word boundary again */
+ length++;
+
+ return length;
+}
+
+void
+iso9660_dir_add_entry_su(void *dir,
+ const char filename[],
+ uint32_t extent,
+ uint32_t size,
+ uint8_t file_flags,
+ const void *su_data,
+ unsigned int su_size,
+ const time_t *entry_time)
+{
+ iso9660_dir_t *idr = dir;
+ uint8_t *dir8 = dir;
+ unsigned int offset = 0;
+ uint32_t dsize = from_733(idr->size);
+ int length, su_offset;
+ cdio_assert (sizeof(iso9660_dir_t) == 33);
+
+ if (!dsize && !idr->length)
+ dsize = ISO_BLOCKSIZE; /* for when dir lacks '.' entry */
+
+ cdio_assert (dsize > 0 && !(dsize % ISO_BLOCKSIZE));
+ cdio_assert (dir != NULL);
+ cdio_assert (extent > 17);
+ cdio_assert (filename != NULL);
+ cdio_assert (strlen(filename) <= MAX_ISOPATHNAME);
+
+ length = sizeof(iso9660_dir_t);
+ length += strlen(filename);
+ length = _cdio_ceil2block (length, 2); /* pad to word boundary */
+ su_offset = length;
+ length += su_size;
+ length = _cdio_ceil2block (length, 2); /* pad to word boundary again */
+
+ /* find the last entry's end */
+ {
+ unsigned int ofs_last_rec = 0;
+
+ offset = 0;
+ while (offset < dsize)
+ {
+ if (!dir8[offset])
+ {
+ offset++;
+ continue;
+ }
+
+ offset += dir8[offset];
+ ofs_last_rec = offset;
+ }
+
+ cdio_assert (offset == dsize);
+
+ offset = ofs_last_rec;
+ }
+
+ /* be sure we don't cross sectors boundaries */
+ offset = _cdio_ofs_add (offset, length, ISO_BLOCKSIZE);
+ offset -= length;
+
+ cdio_assert (offset + length <= dsize);
+
+ idr = (iso9660_dir_t *) &dir8[offset];
+
+ cdio_assert (offset+length < dsize);
+
+ memset(idr, 0, length);
+
+ idr->length = to_711(length);
+ idr->extent = to_733(extent);
+ idr->size = to_733(size);
+
+ iso9660_set_dtime (gmtime(entry_time), &(idr->recording_time));
+
+ idr->file_flags = to_711(file_flags);
+
+ idr->volume_sequence_number = to_723(1);
+
+ idr->filename_len = to_711(strlen(filename)
+ ? strlen(filename) : 1); /* working hack! */
+
+ memcpy(idr->filename, filename, from_711(idr->filename_len));
+ memcpy(&dir8[offset] + su_offset, su_data, su_size);
+}
+
+void
+iso9660_dir_init_new (void *dir,
+ uint32_t self,
+ uint32_t ssize,
+ uint32_t parent,
+ uint32_t psize,
+ const time_t *dir_time)
+{
+ iso9660_dir_init_new_su (dir, self, ssize, NULL, 0, parent, psize, NULL,
+ 0, dir_time);
+}
+
+void
+iso9660_dir_init_new_su (void *dir,
+ uint32_t self,
+ uint32_t ssize,
+ const void *ssu_data,
+ unsigned int ssu_size,
+ uint32_t parent,
+ uint32_t psize,
+ const void *psu_data,
+ unsigned int psu_size,
+ const time_t *dir_time)
+{
+ cdio_assert (ssize > 0 && !(ssize % ISO_BLOCKSIZE));
+ cdio_assert (psize > 0 && !(psize % ISO_BLOCKSIZE));
+ cdio_assert (dir != NULL);
+
+ memset (dir, 0, ssize);
+
+ /* "\0" -- working hack due to padding */
+ iso9660_dir_add_entry_su (dir, "\0", self, ssize, ISO_DIRECTORY, ssu_data,
+ ssu_size, dir_time);
+
+ iso9660_dir_add_entry_su (dir, "\1", parent, psize, ISO_DIRECTORY, psu_data,
+ psu_size, dir_time);
+}
+
+/* Zero's out pathable. Do this first. */
+void
+iso9660_pathtable_init (void *pt)
+{
+ cdio_assert (sizeof (struct iso_path_table) == 8);
+
+ cdio_assert (pt != NULL);
+
+ memset (pt, 0, ISO_BLOCKSIZE); /* fixme */
+}
+
+static const struct iso_path_table*
+pathtable_get_entry (const void *pt, unsigned int entrynum)
+{
+ const uint8_t *tmp = pt;
+ unsigned int offset = 0;
+ unsigned int count = 0;
+
+ cdio_assert (pt != NULL);
+
+ while (from_711 (*tmp))
+ {
+ if (count == entrynum)
+ break;
+
+ cdio_assert (count < entrynum);
+
+ offset += sizeof (struct iso_path_table);
+ offset += from_711 (*tmp);
+ if (offset % 2)
+ offset++;
+ tmp = (uint8_t *)pt + offset;
+ count++;
+ }
+
+ if (!from_711 (*tmp))
+ return NULL;
+
+ return (const void *) tmp;
+}
+
+void
+pathtable_get_size_and_entries (const void *pt,
+ unsigned int *size,
+ unsigned int *entries)
+{
+ const uint8_t *tmp = pt;
+ unsigned int offset = 0;
+ unsigned int count = 0;
+
+ cdio_assert (pt != NULL);
+
+ while (from_711 (*tmp))
+ {
+ offset += sizeof (struct iso_path_table);
+ offset += from_711 (*tmp);
+ if (offset % 2)
+ offset++;
+ tmp = (uint8_t *)pt + offset;
+ count++;
+ }
+
+ if (size)
+ *size = offset;
+
+ if (entries)
+ *entries = count;
+}
+
+unsigned int
+iso9660_pathtable_get_size (const void *pt)
+{
+ unsigned int size = 0;
+ pathtable_get_size_and_entries (pt, &size, NULL);
+ return size;
+}
+
+uint16_t
+iso9660_pathtable_l_add_entry (void *pt,
+ const char name[],
+ uint32_t extent,
+ uint16_t parent)
+{
+ struct iso_path_table *ipt =
+ (struct iso_path_table*)((char *)pt + iso9660_pathtable_get_size (pt));
+ size_t name_len = strlen (name) ? strlen (name) : 1;
+ unsigned int entrynum = 0;
+
+ cdio_assert (iso9660_pathtable_get_size (pt) < ISO_BLOCKSIZE); /*fixme */
+
+ memset (ipt, 0, sizeof (struct iso_path_table) + name_len); /* paranoia */
+
+ ipt->name_len = to_711 (name_len);
+ ipt->extent = to_731 (extent);
+ ipt->parent = to_721 (parent);
+ memcpy (ipt->name, name, name_len);
+
+ pathtable_get_size_and_entries (pt, NULL, &entrynum);
+
+ if (entrynum > 1)
+ {
+ const struct iso_path_table *ipt2
+ = pathtable_get_entry (pt, entrynum - 2);
+
+ cdio_assert (ipt2 != NULL);
+
+ cdio_assert (from_721 (ipt2->parent) <= parent);
+ }
+
+ return entrynum;
+}
+
+uint16_t
+iso9660_pathtable_m_add_entry (void *pt,
+ const char name[],
+ uint32_t extent,
+ uint16_t parent)
+{
+ struct iso_path_table *ipt =
+ (struct iso_path_table*)((char *)pt + iso9660_pathtable_get_size (pt));
+ size_t name_len = strlen (name) ? strlen (name) : 1;
+ unsigned int entrynum = 0;
+
+ cdio_assert (iso9660_pathtable_get_size(pt) < ISO_BLOCKSIZE); /* fixme */
+
+ memset(ipt, 0, sizeof (struct iso_path_table) + name_len); /* paranoia */
+
+ ipt->name_len = to_711 (name_len);
+ ipt->extent = to_732 (extent);
+ ipt->parent = to_722 (parent);
+ memcpy (ipt->name, name, name_len);
+
+ pathtable_get_size_and_entries (pt, NULL, &entrynum);
+
+ if (entrynum > 1)
+ {
+ const struct iso_path_table *ipt2
+ = pathtable_get_entry (pt, entrynum - 2);
+
+ cdio_assert (ipt2 != NULL);
+
+ cdio_assert (from_722 (ipt2->parent) <= parent);
+ }
+
+ return entrynum;
+}
+
+/*!
+ Check that pathname is a valid ISO-9660 directory name.
+
+ A valid directory name should not start out with a slash (/),
+ dot (.) or null byte, should be less than 37 characters long,
+ have no more than 8 characters in a directory component
+ which is separated by a /, and consist of only DCHARs.
+ */
+bool
+iso9660_dirname_valid_p (const char pathname[])
+{
+ const char *p = pathname;
+ int len;
+
+ cdio_assert (pathname != NULL);
+
+ if (*p == '/' || *p == '.' || *p == '\0')
+ return false;
+
+ if (strlen (pathname) > MAX_ISOPATHNAME)
+ return false;
+
+ len = 0;
+ for (; *p; p++)
+ if (iso9660_isdchar (*p))
+ {
+ len++;
+ if (len > 8)
+ return false;
+ }
+ else if (*p == '/')
+ {
+ if (!len)
+ return false;
+ len = 0;
+ }
+ else
+ return false; /* unexpected char */
+
+ if (!len)
+ return false; /* last char may not be '/' */
+
+ return true;
+}
+
+/*!
+ Check that pathname is a valid ISO-9660 pathname.
+
+ A valid pathname contains a valid directory name, if one appears and
+ the filename portion should be no more than 8 characters for the
+ file prefix and 3 characters in the extension (or portion after a
+ dot). There should be exactly one dot somewhere in the filename
+ portion and the filename should be composed of only DCHARs.
+
+ True is returned if pathname is valid.
+ */
+bool
+iso9660_pathname_valid_p (const char pathname[])
+{
+ const char *p = NULL;
+
+ cdio_assert (pathname != NULL);
+
+ if ((p = strrchr (pathname, '/')))
+ {
+ bool rc;
+ char *_tmp = strdup (pathname);
+
+ *strrchr (_tmp, '/') = '\0';
+
+ rc = iso9660_dirname_valid_p (_tmp);
+
+ free (_tmp);
+
+ if (!rc)
+ return false;
+
+ p++;
+ }
+ else
+ p = pathname;
+
+ if (strlen (pathname) > (MAX_ISOPATHNAME - 6))
+ return false;
+
+ {
+ int len = 0;
+ int dots = 0;
+
+ for (; *p; p++)
+ if (iso9660_isdchar (*p))
+ {
+ len++;
+ if (dots == 0 ? len > 8 : len > 3)
+ return false;
+ }
+ else if (*p == '.')
+ {
+ dots++;
+ if (dots > 1)
+ return false;
+ if (!len)
+ return false;
+ len = 0;
+ }
+ else
+ return false;
+
+ if (dots != 1)
+ return false;
+ }
+
+ return true;
+}
+
+/*!
+ Take pathname and a version number and turn that into a ISO-9660
+ pathname. (That's just the pathname followd by ";" and the version
+ number. For example, mydir/file.ext -> mydir/file.ext;1 for version
+ 1. The resulting ISO-9660 pathname is returned.
+*/
+char *
+iso9660_pathname_isofy (const char pathname[], uint16_t version)
+{
+ char tmpbuf[1024] = { 0, };
+
+ cdio_assert (strlen (pathname) < (sizeof (tmpbuf) - sizeof (";65535")));
+
+ snprintf (tmpbuf, sizeof(tmpbuf), "%s;%d", pathname, version);
+
+ return strdup (tmpbuf);
+}
+
+/*!
+ Return the PVD's application ID.
+ NULL is returned if there is some problem in getting this.
+*/
+char *
+iso9660_get_application_id(iso9660_pvd_t *p_pvd)
+{
+ if (NULL==p_pvd) return NULL;
+ return strdup(strip_trail(p_pvd->application_id, ISO_MAX_APPLICATION_ID));
+}
+
+#if FIXME
+lsn_t
+iso9660_get_dir_extent(const iso9660_dir_t *idr)
+{
+ if (NULL == idr) return 0;
+ return from_733(idr->extent);
+}
+#endif
+
+uint8_t
+iso9660_get_dir_len(const iso9660_dir_t *idr)
+{
+ if (NULL == idr) return 0;
+ return idr->length;
+}
+
+#if FIXME
+uint8_t
+iso9660_get_dir_size(const iso9660_dir_t *idr)
+{
+ if (NULL == idr) return 0;
+ return from_733(idr->size);
+}
+#endif
+
+uint8_t
+iso9660_get_pvd_type(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return 255;
+ return(pvd->type);
+}
+
+const char *
+iso9660_get_pvd_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return "ERR";
+ return(pvd->id);
+}
+
+int
+iso9660_get_pvd_space_size(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return 0;
+ return from_733(pvd->volume_space_size);
+}
+
+int
+iso9660_get_pvd_block_size(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return 0;
+ return from_723(pvd->logical_block_size);
+}
+
+/*! Return the primary volume id version number (of pvd).
+ If there is an error 0 is returned.
+ */
+int
+iso9660_get_pvd_version(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return 0;
+ return pvd->version;
+}
+
+/*! Return the LSN of the root directory for pvd.
+ If there is an error CDIO_INVALID_LSN is returned.
+ */
+lsn_t
+iso9660_get_root_lsn(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd)
+ return CDIO_INVALID_LSN;
+ else {
+ const iso9660_dir_t *idr = &(pvd->root_directory_record);
+ if (NULL == idr) return CDIO_INVALID_LSN;
+ return(from_733 (idr->extent));
+ }
+}
+
+/*!
+ Return a string containing the preparer id with trailing
+ blanks removed.
+*/
+char *
+iso9660_get_preparer_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL==pvd) return NULL;
+ return strdup(strip_trail(pvd->preparer_id, ISO_MAX_PREPARER_ID));
+}
+
+/*!
+ Return a string containing the publisher id with trailing
+ blanks removed.
+*/
+char *
+iso9660_get_publisher_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL==pvd) return NULL;
+ return strdup(strip_trail(pvd->publisher_id, ISO_MAX_PUBLISHER_ID));
+}
+
+/*!
+ Return a string containing the PVD's system id with trailing
+ blanks removed.
+*/
+char *
+iso9660_get_system_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL==pvd) return NULL;
+ return strdup(strip_trail(pvd->system_id, ISO_MAX_SYSTEM_ID));
+}
+
+/*!
+ Return the PVD's volume ID.
+*/
+char *
+iso9660_get_volume_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return NULL;
+ return strdup(strip_trail(pvd->volume_id, ISO_MAX_VOLUME_ID));
+}
+
+/*!
+ Return the PVD's volumeset ID.
+ NULL is returned if there is some problem in getting this.
+*/
+char *
+iso9660_get_volumeset_id(const iso9660_pvd_t *pvd)
+{
+ if ( NULL == pvd ) return NULL;
+ return strdup(strip_trail(pvd->volume_set_id, ISO_MAX_VOLUMESET_ID));
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/iso9660_fs.c b/contrib/libcdio/iso9660_fs.c
new file mode 100644
index 000000000..fff12cd7c
--- /dev/null
+++ b/contrib/libcdio/iso9660_fs.c
@@ -0,0 +1,1260 @@
+/*
+ $Id: iso9660_fs.c,v 1.7 2006/12/08 16:26:10 mshopf Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifdef HAVE_STRING_H
+# include <string.h>
+#endif
+
+#ifdef HAVE_ERRNO_H
+# include <errno.h>
+#endif
+
+#ifdef HAVE_ICONV
+# include <iconv.h>
+#endif
+
+#ifdef HAVE_NL_LANGINFO
+#include <langinfo.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/bytesex.h>
+#include <cdio/iso9660.h>
+#include <cdio/util.h>
+
+/* Private headers */
+#include "cdio_assert.h"
+#include "_cdio_stdio.h"
+#include "cdio_private.h"
+
+#include <stdio.h>
+
+static const char _rcsid[] = "$Id: iso9660_fs.c,v 1.7 2006/12/08 16:26:10 mshopf Exp $";
+
+/* Implementation of iso9660_t type */
+struct _iso9660 {
+ CdioDataSource *stream; /* Stream pointer */
+ bool b_xa; /* true if has XA attributes. */
+ uint8_t i_joliet_level;/* 0 = no Joliet extensions.
+ 1-3: Joliet level. */
+ iso9660_pvd_t pvd;
+ iso9660_svd_t svd;
+ iso_extension_mask_t iso_extension_mask; /* What extensions we
+ tolerate. */
+};
+
+/*!
+ Open an ISO 9660 image for reading. Maybe in the future we will have
+ a mode. NULL is returned on error.
+*/
+iso9660_t *
+iso9660_open (const char *pathname /*, mode*/)
+{
+ return iso9660_open_ext(pathname, ISO_EXTENSION_NONE);
+}
+
+/*!
+ Open an ISO 9660 image for reading. Maybe in the future we will have
+ a mode. NULL is returned on error.
+*/
+iso9660_t *
+iso9660_open_ext (const char *pathname,
+ iso_extension_mask_t iso_extension_mask)
+{
+ iso9660_t *p_iso = (iso9660_t *) _cdio_malloc(sizeof(struct _iso9660)) ;
+
+ if (NULL == p_iso) return NULL;
+
+ p_iso->stream = cdio_stdio_new( pathname );
+ if (NULL == p_iso->stream)
+ goto error;
+
+ if ( !iso9660_ifs_read_superblock(p_iso, iso_extension_mask) )
+ goto error;
+
+ /* Determine if image has XA attributes. */
+
+ p_iso->b_xa = !strncmp ((char *) &(p_iso->pvd) + ISO_XA_MARKER_OFFSET,
+ ISO_XA_MARKER_STRING,
+ strlen (ISO_XA_MARKER_STRING));
+ p_iso->iso_extension_mask = iso_extension_mask;
+ return p_iso;
+
+ error:
+ free(p_iso);
+ return NULL;
+}
+
+
+
+/*!
+ Close previously opened ISO 9660 image.
+ True is unconditionally returned. If there was an error false would
+ be returned.
+*/
+bool
+iso9660_close (iso9660_t *p_iso)
+{
+ if (NULL != p_iso) {
+ cdio_stdio_destroy(p_iso->stream);
+ free(p_iso);
+ }
+ return true;
+}
+
+static bool
+check_pvd (const iso9660_pvd_t *p_pvd)
+{
+ if ( ISO_VD_PRIMARY != from_711(p_pvd->type) ) {
+ cdio_warn ("unexpected PVD type %d", p_pvd->type);
+ return false;
+ }
+
+ if (strncmp (p_pvd->id, ISO_STANDARD_ID, strlen (ISO_STANDARD_ID)))
+ {
+ cdio_warn ("unexpected ID encountered (expected `"
+ ISO_STANDARD_ID "', got `%.5s'", p_pvd->id);
+ return false;
+ }
+ return true;
+}
+
+#ifdef HAVE_JOLIET
+static bool
+ucs2be_to_locale(ICONV_CONST char *psz_ucs2be, size_t i_inlen,
+ char **p_psz_out, size_t i_outlen)
+{
+
+ iconv_t ic =
+#if defined(HAVE_NL_LANGINFO)
+ iconv_open(nl_langinfo(CODESET), "UCS-2BE");
+#else
+ iconv_open("ASCII", "UCS-2BE");
+#endif
+
+ int rc;
+ char *psz_buf = NULL;
+ char *psz_buf2;
+ int i_outlen_max = i_outlen;
+ int i_outlen_actual;
+
+ if (-1 == (size_t) ic) {
+#if defined(HAVE_NL_LANGINFO)
+ cdio_info("Failed to get conversion table for locale, trying ASCII");
+ ic = iconv_open("ASCII", "UCS-2BE");
+ if (-1 == (size_t) ic) {
+ cdio_info("Failed to get conversion table for ASCII too");
+ return false;
+ }
+#else
+ cdio_info("Failed to get conversion table for locale");
+ return false;
+#endif
+ }
+
+ psz_buf = (char *) realloc(psz_buf, i_outlen);
+ psz_buf2 = psz_buf;
+ if (!psz_buf) {
+ /* XXX: report out of memory error */
+ goto error;
+ }
+ rc = iconv(ic, &psz_ucs2be, &i_inlen, &psz_buf2, &i_outlen);
+ iconv_close(ic);
+ if ((rc == -1) && (errno != E2BIG)) {
+ /* conversion failed */
+ goto error;
+ }
+ i_outlen_actual = i_outlen_max - i_outlen;
+ *p_psz_out = malloc(i_outlen_actual + 1);
+ memcpy(*p_psz_out, psz_buf, i_outlen_actual);
+ *(*p_psz_out + i_outlen_actual) = '\0';
+ free(psz_buf);
+ return true;
+ error:
+ free(psz_buf);
+ *p_psz_out = NULL;
+ return false;
+}
+#endif /*HAVE_JOLIET*/
+
+/*!
+ Return the application ID. NULL is returned in psz_app_id if there
+ is some problem in getting this.
+*/
+bool
+iso9660_ifs_get_application_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_app_id)
+{
+ if (!p_iso) {
+ *p_psz_app_id = NULL;
+ return false;
+ }
+
+#ifdef HAVE_JOLIET
+ if (p_iso->i_joliet_level) {
+ /* TODO: check that we haven't reached the maximum size.
+ If we have, perhaps we've truncated and if we can get
+ longer results *and* have the same character using
+ the PVD, do that.
+ */
+ if ( ucs2be_to_locale(p_iso->svd.application_id,
+ ISO_MAX_APPLICATION_ID,
+ p_psz_app_id,
+ ISO_MAX_APPLICATION_ID))
+ return true;
+ }
+#endif /*HAVE_JOLIET*/
+ *p_psz_app_id = iso9660_get_application_id( &(p_iso->pvd) );
+ return *p_psz_app_id != NULL && strlen(*p_psz_app_id);
+}
+
+/*!
+ Return the Joliet level recognaized for p_iso.
+*/
+uint8_t iso9660_ifs_get_joliet_level(iso9660_t *p_iso)
+{
+ if (!p_iso) return 0;
+ return p_iso->i_joliet_level;
+}
+
+/*!
+ Return a string containing the preparer id with trailing
+ blanks removed.
+*/
+bool
+iso9660_ifs_get_preparer_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_preparer_id)
+{
+ if (!p_iso) {
+ *p_psz_preparer_id = NULL;
+ return false;
+ }
+
+#ifdef HAVE_JOLIET
+ if (p_iso->i_joliet_level) {
+ /* TODO: check that we haven't reached the maximum size.
+ If we have, perhaps we've truncated and if we can get
+ longer results *and* have the same character using
+ the PVD, do that.
+ */
+ if ( ucs2be_to_locale(p_iso->svd.preparer_id, ISO_MAX_PREPARER_ID,
+ p_psz_preparer_id, ISO_MAX_PREPARER_ID) )
+ return true;
+ }
+#endif /*HAVE_JOLIET*/
+ *p_psz_preparer_id = iso9660_get_preparer_id( &(p_iso->pvd) );
+ return *p_psz_preparer_id != NULL && strlen(*p_psz_preparer_id);
+}
+
+/*!
+ Return a string containing the PVD's publisher id with trailing
+ blanks removed.
+*/
+bool iso9660_ifs_get_publisher_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_publisher_id)
+{
+ if (!p_iso) {
+ *p_psz_publisher_id = NULL;
+ return false;
+ }
+
+#ifdef HAVE_JOLIET
+ if (p_iso->i_joliet_level) {
+ /* TODO: check that we haven't reached the maximum size.
+ If we have, perhaps we've truncated and if we can get
+ longer results *and* have the same character using
+ the PVD, do that.
+ */
+ if( ucs2be_to_locale(p_iso->svd.publisher_id, ISO_MAX_PUBLISHER_ID,
+ p_psz_publisher_id, ISO_MAX_PUBLISHER_ID) )
+ return true;
+ }
+#endif /*HAVE_JOLIET*/
+ *p_psz_publisher_id = iso9660_get_publisher_id( &(p_iso->pvd) );
+ return *p_psz_publisher_id != NULL && strlen(*p_psz_publisher_id);
+}
+
+
+/*!
+ Return a string containing the PVD's publisher id with trailing
+ blanks removed.
+*/
+bool iso9660_ifs_get_system_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_system_id)
+{
+ if (!p_iso) {
+ *p_psz_system_id = NULL;
+ return false;
+ }
+
+#ifdef HAVE_JOLIET
+ if (p_iso->i_joliet_level) {
+ /* TODO: check that we haven't reached the maximum size.
+ If we have, perhaps we've truncated and if we can get
+ longer results *and* have the same character using
+ the PVD, do that.
+ */
+ if ( ucs2be_to_locale(p_iso->svd.system_id, ISO_MAX_SYSTEM_ID,
+ p_psz_system_id, ISO_MAX_SYSTEM_ID) )
+ return true;
+ }
+#endif /*HAVE_JOLIET*/
+ *p_psz_system_id = iso9660_get_system_id( &(p_iso->pvd) );
+ return *p_psz_system_id != NULL && strlen(*p_psz_system_id);
+}
+
+
+/*!
+ Return a string containing the PVD's publisher id with trailing
+ blanks removed.
+*/
+bool iso9660_ifs_get_volume_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_volume_id)
+{
+ if (!p_iso) {
+ *p_psz_volume_id = NULL;
+ return false;
+ }
+
+#ifdef HAVE_JOLIET
+ if (p_iso->i_joliet_level) {
+ /* TODO: check that we haven't reached the maximum size.
+ If we have, perhaps we've truncated and if we can get
+ longer results *and* have the same character using
+ the PVD, do that.
+ */
+ if ( ucs2be_to_locale(p_iso->svd.volume_id, ISO_MAX_VOLUME_ID,
+ p_psz_volume_id, ISO_MAX_VOLUME_ID) )
+ return true;
+ }
+#endif /* HAVE_JOLIET */
+ *p_psz_volume_id = iso9660_get_volume_id( &(p_iso->pvd) );
+ return *p_psz_volume_id != NULL && strlen(*p_psz_volume_id);
+}
+
+
+/*!
+ Return a string containing the PVD's publisher id with trailing
+ blanks removed.
+*/
+bool iso9660_ifs_get_volumeset_id(iso9660_t *p_iso,
+ /*out*/ char **p_psz_volumeset_id)
+{
+ if (!p_iso) {
+ *p_psz_volumeset_id = NULL;
+ return false;
+ }
+
+#ifdef HAVE_JOLIET
+ if (p_iso->i_joliet_level) {
+ /* TODO: check that we haven't reached the maximum size.
+ If we have, perhaps we've truncated and if we can get
+ longer results *and* have the same character using
+ the PVD, do that.
+ */
+ if ( ucs2be_to_locale(p_iso->svd.volume_set_id,
+ ISO_MAX_VOLUMESET_ID,
+ p_psz_volumeset_id,
+ ISO_MAX_VOLUMESET_ID) )
+ return true;
+ }
+#endif /*HAVE_JOLIET*/
+ *p_psz_volumeset_id = iso9660_get_volume_id( &(p_iso->pvd) );
+ return *p_psz_volumeset_id != NULL && strlen(*p_psz_volumeset_id);
+}
+
+
+/*!
+ Read the Primary Volume Descriptor for an ISO 9660 image.
+ True is returned if read, and false if there was an error.
+*/
+bool
+iso9660_ifs_read_pvd (const iso9660_t *p_iso, /*out*/ iso9660_pvd_t *p_pvd)
+{
+ if (0 == iso9660_iso_seek_read (p_iso, p_pvd, ISO_PVD_SECTOR, 1)) {
+ cdio_warn ("error reading PVD sector (%d)", ISO_PVD_SECTOR);
+ return false;
+ }
+ return check_pvd(p_pvd);
+}
+
+
+/*!
+ Read the Super block of an ISO 9660 image. This is the
+ Primary Volume Descriptor (PVD) and perhaps a Supplemental Volume
+ Descriptor if (Joliet) extensions are acceptable.
+*/
+bool
+iso9660_ifs_read_superblock (iso9660_t *p_iso,
+ iso_extension_mask_t iso_extension_mask)
+{
+ iso9660_svd_t *p_svd; /* Secondary volume descriptor. */
+
+ if (!p_iso || !iso9660_ifs_read_pvd(p_iso, &(p_iso->pvd)))
+ return false;
+
+ p_svd = &(p_iso->svd);
+ p_iso->i_joliet_level = 0;
+
+ if (0 != iso9660_iso_seek_read (p_iso, p_svd, ISO_PVD_SECTOR+1, 1)) {
+ if ( ISO_VD_SUPPLEMENTARY == from_711(p_svd->type) ) {
+ if (p_svd->escape_sequences[0] == 0x25
+ && p_svd->escape_sequences[1] == 0x2f) {
+ switch (p_svd->escape_sequences[2]) {
+ case 0x40:
+ if (iso_extension_mask & ISO_EXTENSION_JOLIET_LEVEL1)
+ p_iso->i_joliet_level = 1;
+ break;
+ case 0x43:
+ if (iso_extension_mask & ISO_EXTENSION_JOLIET_LEVEL2)
+ p_iso->i_joliet_level = 2;
+ break;
+ case 0x45:
+ if (iso_extension_mask & ISO_EXTENSION_JOLIET_LEVEL3)
+ p_iso->i_joliet_level = 3;
+ break;
+ default:
+ cdio_info("Supplementary Volume Descriptor found, but not Joliet");
+ }
+ if (p_iso->i_joliet_level > 0) {
+ cdio_info("Found Extension: Joliet Level %d", p_iso->i_joliet_level);
+ }
+ }
+ }
+ }
+
+ return true;
+}
+
+
+/*!
+ Read the Primary Volume Descriptor for of CD.
+*/
+bool
+iso9660_fs_read_pvd(const CdIo *p_cdio, /*out*/ iso9660_pvd_t *p_pvd)
+{
+ /* A bit of a hack, we'll assume track 1 contains ISO_PVD_SECTOR.*/
+ bool b_mode2;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ int i_rc;
+
+ switch(cdio_get_track_format(p_cdio, 1)) {
+ case TRACK_FORMAT_CDI:
+ case TRACK_FORMAT_XA:
+ b_mode2 = true;
+ break;
+ case TRACK_FORMAT_DATA:
+ b_mode2 = false;
+ break;
+ case TRACK_FORMAT_AUDIO:
+ case TRACK_FORMAT_PSX:
+ case TRACK_FORMAT_ERROR:
+ default:
+ return false;
+ }
+
+ i_rc = b_mode2
+ ? cdio_read_mode2_sector (p_cdio, buf, ISO_PVD_SECTOR, false)
+ : cdio_read_mode1_sector (p_cdio, buf, ISO_PVD_SECTOR, false);
+
+ if (i_rc) {
+ cdio_warn ("error reading PVD sector (%d)", ISO_PVD_SECTOR);
+ return false;
+ }
+
+ /* The size of a PVD or SVD is smaller than a sector. So we
+ allocated a bigger block above (buf) and now we'll copy just
+ the part we need to save.
+ */
+ cdio_assert (sizeof(buf) >= sizeof (iso9660_pvd_t));
+ memcpy(p_pvd, buf, sizeof(iso9660_pvd_t));
+
+ return check_pvd(p_pvd);
+}
+
+
+/*!
+ Read the Super block of an ISO 9660 image. This is the
+ Primary Volume Descriptor (PVD) and perhaps a Supplemental Volume
+ Descriptor if (Joliet) extensions are acceptable.
+*/
+bool
+iso9660_fs_read_superblock (CdIo *p_cdio,
+ iso_extension_mask_t iso_extension_mask)
+{
+ if (!p_cdio) return false;
+
+ {
+ generic_img_private_t *p_env = (generic_img_private_t *) p_cdio->env;
+ iso9660_pvd_t *p_pvd = &(p_env->pvd);
+ iso9660_svd_t *p_svd = &(p_env->svd);
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ bool b_mode2;
+ int i_rc;
+
+ /* A bit of a hack, we'll assume track 1 contains ISO_PVD_SECTOR.*/
+ switch(cdio_get_track_format(p_cdio, 1)) {
+ case TRACK_FORMAT_CDI:
+ case TRACK_FORMAT_XA:
+ b_mode2 = true;
+ break;
+ case TRACK_FORMAT_DATA:
+ b_mode2 = false;
+ break;
+ case TRACK_FORMAT_AUDIO:
+ case TRACK_FORMAT_PSX:
+ case TRACK_FORMAT_ERROR:
+ default:
+ return false;
+ }
+
+ if ( !iso9660_fs_read_pvd(p_cdio, p_pvd) )
+ return false;
+
+ p_env->i_joliet_level = 0;
+
+ i_rc = (b_mode2)
+ ? cdio_read_mode2_sector (p_cdio, buf, ISO_PVD_SECTOR+1, false)
+ : cdio_read_mode1_sector (p_cdio, buf, ISO_PVD_SECTOR+1, false);
+
+ if (0 == i_rc) {
+ /* The size of a PVD or SVD is smaller than a sector. So we
+ allocated a bigger block above (buf) and now we'll copy just
+ the part we need to save.
+ */
+ cdio_assert (sizeof(buf) >= sizeof (iso9660_svd_t));
+ memcpy(p_svd, buf, sizeof(iso9660_svd_t));
+
+ if ( ISO_VD_SUPPLEMENTARY == from_711(p_svd->type) ) {
+ if (p_svd->escape_sequences[0] == 0x25
+ && p_svd->escape_sequences[1] == 0x2f) {
+ switch (p_svd->escape_sequences[2]) {
+ case 0x40:
+ if (iso_extension_mask & ISO_EXTENSION_JOLIET_LEVEL1)
+ p_env->i_joliet_level = 1;
+ break;
+ case 0x43:
+ if (iso_extension_mask & ISO_EXTENSION_JOLIET_LEVEL2)
+ p_env->i_joliet_level = 2;
+ break;
+ case 0x45:
+ if (iso_extension_mask & ISO_EXTENSION_JOLIET_LEVEL3)
+ p_env->i_joliet_level = 3;
+ break;
+ default:
+ cdio_info("Supplementary Volume Descriptor found, but not Joliet");
+ }
+ if (p_env->i_joliet_level > 0) {
+ cdio_info("Found Extension: Joliet Level %d",
+ p_env->i_joliet_level);
+ }
+ }
+ }
+ }
+ }
+
+ return true;
+}
+
+/*!
+ Seek to a position and then read n blocks. Size read is returned.
+*/
+long int
+iso9660_iso_seek_read (const iso9660_t *p_iso, void *ptr, lsn_t start,
+ long int size)
+{
+ long int ret;
+ if (NULL == p_iso) return 0;
+ ret = cdio_stream_seek (p_iso->stream, start * ISO_BLOCKSIZE, SEEK_SET);
+ if (ret!=0) return 0;
+ return cdio_stream_read (p_iso->stream, ptr, ISO_BLOCKSIZE, size);
+}
+
+
+static iso9660_stat_t *
+_iso9660_dir_to_statbuf (iso9660_dir_t *p_iso9660_dir,
+ bool b_mode2, uint8_t i_joliet_level)
+{
+ iso9660_xa_t *xa_data = NULL;
+ uint8_t dir_len= iso9660_get_dir_len(p_iso9660_dir);
+ unsigned int filename_len;
+ unsigned int stat_len;
+ iso9660_stat_t *stat;
+
+ if (!dir_len) return NULL;
+
+ filename_len = from_711(p_iso9660_dir->filename_len);
+
+ /* .. string in statbuf is one longer than in p_iso9660_dir's listing '\1' */
+ stat_len = sizeof(iso9660_stat_t)+filename_len+2;
+
+ stat = _cdio_malloc(stat_len);
+ stat->type = (p_iso9660_dir->file_flags & ISO_DIRECTORY)
+ ? _STAT_DIR : _STAT_FILE;
+ stat->lsn = from_733 (p_iso9660_dir->extent);
+ stat->size = from_733 (p_iso9660_dir->size);
+ stat->secsize = _cdio_len2blocks (stat->size, ISO_BLOCKSIZE);
+
+ if ('\0' == p_iso9660_dir->filename[0] && 1 == filename_len)
+ strcpy (stat->filename, ".");
+ else if ('\1' == p_iso9660_dir->filename[0] && 1 == filename_len)
+ strcpy (stat->filename, "..");
+ else {
+#ifdef HAVE_JOLIET
+ if (i_joliet_level) {
+ int i_inlen = filename_len;
+ int i_outlen = (i_inlen / 2);
+ char *p_psz_out = NULL;
+ ucs2be_to_locale(p_iso9660_dir->filename, i_inlen,
+ &p_psz_out, i_outlen);
+ strncpy(stat->filename, p_psz_out, filename_len);
+ free(p_psz_out);
+ } else
+#endif /*HAVE_JOLIET*/
+ strncpy (stat->filename, p_iso9660_dir->filename, filename_len);
+ }
+
+
+ iso9660_get_dtime(&(p_iso9660_dir->recording_time), true, &(stat->tm));
+
+ cdio_assert (dir_len >= sizeof (iso9660_dir_t));
+
+ if (b_mode2) {
+ int su_length = iso9660_get_dir_len(p_iso9660_dir)
+ - sizeof (iso9660_dir_t);
+ su_length -= filename_len;
+
+ if (su_length % 2)
+ su_length--;
+
+ if (su_length < 0 || su_length < sizeof (iso9660_xa_t))
+ return stat;
+
+ xa_data = (void *) (((char *) p_iso9660_dir)
+ + (iso9660_get_dir_len(p_iso9660_dir) - su_length));
+
+ if (xa_data->signature[0] != 'X'
+ || xa_data->signature[1] != 'A')
+ {
+ cdio_warn ("XA signature not found in ISO9660's system use area;"
+ " ignoring XA attributes for this file entry.");
+ cdio_debug ("%d %d %d, '%c%c' (%d, %d)",
+ iso9660_get_dir_len(p_iso9660_dir),
+ filename_len,
+ su_length,
+ xa_data->signature[0], xa_data->signature[1],
+ xa_data->signature[0], xa_data->signature[1]);
+ return stat;
+ }
+ stat->xa = *xa_data;
+ }
+ return stat;
+
+}
+
+/*!
+ Return the directory name stored in the iso9660_dir_t
+
+ A string is allocated: the caller must deallocate.
+ */
+char *
+iso9660_dir_to_name (const iso9660_dir_t *iso9660_dir)
+{
+ char namebuf[256] = { 0, };
+ uint8_t len=iso9660_get_dir_len(iso9660_dir);
+
+ if (!len) return NULL;
+
+ cdio_assert (len >= sizeof (iso9660_dir_t));
+
+ /* (iso9660_dir->file_flags & ISO_DIRECTORY) */
+
+ if (iso9660_dir->filename[0] == '\0')
+ strcpy (namebuf, ".");
+ else if (iso9660_dir->filename[0] == '\1')
+ strcpy (namebuf, "..");
+ else
+ strncpy (namebuf, iso9660_dir->filename, iso9660_dir->filename_len);
+
+ return strdup (namebuf);
+}
+
+/*
+ Return a pointer to a ISO 9660 stat buffer or NULL if there's an error
+*/
+static iso9660_stat_t *
+_fs_stat_root (CdIo *p_cdio)
+{
+
+ if (!p_cdio) return NULL;
+
+ {
+ iso_extension_mask_t iso_extension_mask = ISO_EXTENSION_ALL;
+ generic_img_private_t *p_env = (generic_img_private_t *) p_cdio->env;
+ bool b_mode2 = cdio_get_track_green(p_cdio, 1);
+ iso9660_dir_t *p_iso9660_dir;
+ iso9660_stat_t *p_stat;
+
+ if (!p_env->i_joliet_level)
+ iso_extension_mask &= ~ISO_EXTENSION_JOLIET;
+
+ /* FIXME try also with Joliet.*/
+ if ( !iso9660_fs_read_superblock (p_cdio, iso_extension_mask) ) {
+ cdio_warn("Could not read ISO-9660 Superblock.");
+ return NULL;
+ }
+
+#ifdef HAVE_JOLIET
+ p_iso9660_dir = p_env->i_joliet_level
+ ? &(p_env->svd.root_directory_record)
+ : &(p_env->pvd.root_directory_record) ;
+#else
+ p_iso9660_dir = &(p_env->pvd.root_directory_record) ;
+#endif
+
+ p_stat = _iso9660_dir_to_statbuf (p_iso9660_dir, b_mode2,
+ p_env->i_joliet_level);
+ return p_stat;
+ }
+
+}
+
+static iso9660_stat_t *
+_fs_stat_iso_root (iso9660_t *p_iso)
+{
+ iso9660_stat_t *p_stat;
+ iso9660_dir_t *p_iso9660_dir;
+
+#ifdef HAVE_JOLIET
+ p_iso9660_dir = p_iso->i_joliet_level
+ ? &(p_iso->svd.root_directory_record)
+ : &(p_iso->pvd.root_directory_record) ;
+#else
+ p_iso9660_dir = &(p_iso->pvd.root_directory_record) ;
+#endif
+
+ p_stat = _iso9660_dir_to_statbuf (p_iso9660_dir, true,
+ p_iso->i_joliet_level);
+ return p_stat;
+}
+
+static iso9660_stat_t *
+_fs_stat_traverse (const CdIo *p_cdio, const iso9660_stat_t *_root,
+ char **splitpath, bool b_mode2, bool translate)
+{
+ unsigned offset = 0;
+ uint8_t *_dirbuf = NULL;
+ iso9660_stat_t *p_stat;
+ generic_img_private_t *p_env = (generic_img_private_t *) p_cdio->env;
+
+ if (!splitpath[0])
+ {
+ unsigned int len=sizeof(iso9660_stat_t) + strlen(_root->filename)+1;
+ p_stat = _cdio_malloc(len);
+ memcpy(p_stat, _root, len);
+ return p_stat;
+ }
+
+ if (_root->type == _STAT_FILE)
+ return NULL;
+
+ cdio_assert (_root->type == _STAT_DIR);
+
+ if (_root->size != ISO_BLOCKSIZE * _root->secsize)
+ {
+ cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!",
+ (unsigned) _root->size,
+ (unsigned long int) ISO_BLOCKSIZE * _root->secsize);
+ }
+
+ _dirbuf = _cdio_malloc (_root->secsize * ISO_BLOCKSIZE);
+
+ if (b_mode2) {
+ if (cdio_read_mode2_sectors (p_cdio, _dirbuf, _root->lsn, false,
+ _root->secsize))
+ return NULL;
+ } else {
+ if (cdio_read_mode1_sectors (p_cdio, _dirbuf, _root->lsn, false,
+ _root->secsize))
+ return NULL;
+ }
+
+ while (offset < (_root->secsize * ISO_BLOCKSIZE))
+ {
+ iso9660_dir_t *p_iso9660_dir = (void *) &_dirbuf[offset];
+ iso9660_stat_t *p_stat;
+ int cmp;
+
+ if (!iso9660_get_dir_len(p_iso9660_dir))
+ {
+ offset++;
+ continue;
+ }
+
+ p_stat = _iso9660_dir_to_statbuf (p_iso9660_dir, b_mode2,
+ p_env->i_joliet_level);
+
+ if (translate) {
+ char *trans_fname = malloc(strlen(p_stat->filename));
+ int trans_len;
+
+ if (trans_fname == NULL) {
+ cdio_warn("can't allocate %lu bytes",
+ (long unsigned int) strlen(p_stat->filename));
+ return NULL;
+ }
+ trans_len = iso9660_name_translate_ext(p_stat->filename, trans_fname,
+ p_env->i_joliet_level);
+ cmp = strcmp(splitpath[0], trans_fname);
+ free(trans_fname);
+ } else {
+ cmp = strcmp(splitpath[0], p_stat->filename);
+ }
+
+ if (!cmp) {
+ iso9660_stat_t *ret_stat
+ = _fs_stat_traverse (p_cdio, p_stat, &splitpath[1], b_mode2,
+ translate);
+ free(p_stat);
+ free (_dirbuf);
+ return ret_stat;
+ }
+
+ free(p_stat);
+
+ offset += iso9660_get_dir_len(p_iso9660_dir);
+ }
+
+ cdio_assert (offset == (_root->secsize * ISO_BLOCKSIZE));
+
+ /* not found */
+ free (_dirbuf);
+ return NULL;
+}
+
+static iso9660_stat_t *
+_fs_iso_stat_traverse (iso9660_t *p_iso, const iso9660_stat_t *_root,
+ char **splitpath, bool translate)
+{
+ unsigned offset = 0;
+ uint8_t *_dirbuf = NULL;
+ int ret;
+
+ if (!splitpath[0])
+ {
+ iso9660_stat_t *p_stat;
+ unsigned int len=sizeof(iso9660_stat_t) + strlen(_root->filename)+1;
+ p_stat = _cdio_malloc(len);
+ memcpy(p_stat, _root, len);
+ return p_stat;
+ }
+
+ if (_root->type == _STAT_FILE)
+ return NULL;
+
+ cdio_assert (_root->type == _STAT_DIR);
+
+ if (_root->size != ISO_BLOCKSIZE * _root->secsize)
+ {
+ cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!",
+ (unsigned) _root->size,
+ (unsigned long int) ISO_BLOCKSIZE * _root->secsize);
+ }
+
+ _dirbuf = _cdio_malloc (_root->secsize * ISO_BLOCKSIZE);
+
+ ret = iso9660_iso_seek_read (p_iso, _dirbuf, _root->lsn, _root->secsize);
+ if (ret!=ISO_BLOCKSIZE*_root->secsize) return NULL;
+
+ while (offset < (_root->secsize * ISO_BLOCKSIZE))
+ {
+ iso9660_dir_t *p_iso9660_dir = (void *) &_dirbuf[offset];
+ iso9660_stat_t *p_stat;
+ int cmp;
+
+ if (!iso9660_get_dir_len(p_iso9660_dir))
+ {
+ offset++;
+ continue;
+ }
+
+ p_stat = _iso9660_dir_to_statbuf (p_iso9660_dir, true,
+ p_iso->i_joliet_level);
+
+ if (translate) {
+ char *trans_fname = malloc(strlen(p_stat->filename)+1);
+ int trans_len;
+
+ if (trans_fname == NULL) {
+ cdio_warn("can't allocate %lu bytes",
+ (long unsigned int) strlen(p_stat->filename));
+ return NULL;
+ }
+ trans_len = iso9660_name_translate_ext(p_stat->filename, trans_fname,
+ p_iso->i_joliet_level);
+ cmp = strcmp(splitpath[0], trans_fname);
+ free(trans_fname);
+ } else {
+ cmp = strcmp(splitpath[0], p_stat->filename);
+ }
+
+ if (!cmp) {
+ iso9660_stat_t *ret_stat
+ = _fs_iso_stat_traverse (p_iso, p_stat, &splitpath[1], translate);
+ free(p_stat);
+ free (_dirbuf);
+ return ret_stat;
+ }
+
+ free(p_stat);
+
+ offset += iso9660_get_dir_len(p_iso9660_dir);
+ }
+
+ cdio_assert (offset == (_root->secsize * ISO_BLOCKSIZE));
+
+ /* not found */
+ free (_dirbuf);
+ return NULL;
+}
+
+/*!
+ Get file status for pathname into stat. NULL is returned on error.
+ */
+iso9660_stat_t *
+iso9660_fs_stat (CdIo *p_cdio, const char pathname[])
+{
+ iso9660_stat_t *p_root;
+ char **p_psz_splitpath;
+ iso9660_stat_t *p_stat;
+ /* A bit of a hack, we'll assume track 1 contains ISO_PVD_SECTOR.*/
+ bool b_mode2;
+
+ if (!p_cdio) return NULL;
+ if (!pathname) return NULL;
+
+ p_root = _fs_stat_root (p_cdio);
+ if (!p_root) return NULL;
+
+ b_mode2 = cdio_get_track_green(p_cdio, 1);
+ p_psz_splitpath = _cdio_strsplit (pathname, '/');
+ p_stat = _fs_stat_traverse (p_cdio, p_root, p_psz_splitpath, b_mode2, false);
+ free(p_root);
+ _cdio_strfreev (p_psz_splitpath);
+
+ return p_stat;
+}
+
+/*!
+ Get file status for pathname into stat. NULL is returned on error.
+ pathname version numbers in the ISO 9660
+ name are dropped, i.e. ;1 is removed and if level 1 ISO-9660 names
+ are lowercased.
+ */
+iso9660_stat_t *
+iso9660_fs_stat_translate (CdIo *p_cdio, const char pathname[],
+ bool b_mode2)
+{
+ iso9660_stat_t *p_root;
+ char **p_psz_splitpath;
+ iso9660_stat_t *p_stat;
+
+ if (!p_cdio) return NULL;
+ if (pathname) return NULL;
+
+ p_root = _fs_stat_root (p_cdio);
+ if (!p_root) return NULL;
+
+ p_psz_splitpath = _cdio_strsplit (pathname, '/');
+ p_stat = _fs_stat_traverse (p_cdio, p_root, p_psz_splitpath, b_mode2, true);
+ free(p_root);
+ _cdio_strfreev (p_psz_splitpath);
+
+ return p_stat;
+}
+
+/*!
+ Get file status for pathname into stat. NULL is returned on error.
+ */
+iso9660_stat_t *
+iso9660_ifs_stat (iso9660_t *p_iso, const char pathname[])
+{
+ iso9660_stat_t *p_root;
+ char **splitpath;
+ iso9660_stat_t *stat;
+
+ if (!p_iso) return NULL;
+ if (!pathname) return NULL;
+
+ p_root = _fs_stat_iso_root (p_iso);
+ if (!p_root) return NULL;
+
+ splitpath = _cdio_strsplit (pathname, '/');
+ stat = _fs_iso_stat_traverse (p_iso, p_root, splitpath, false);
+ free(p_root);
+ /*** FIXME _cdio_strfreev (splitpath); ***/
+
+ return stat;
+}
+
+/*!
+ Get file status for pathname into stat. NULL is returned on error.
+ pathname version numbers in the ISO 9660
+ name are dropped, i.e. ;1 is removed and if level 1 ISO-9660 names
+ are lowercased.
+ */
+iso9660_stat_t *
+iso9660_ifs_stat_translate (iso9660_t *p_iso, const char pathname[])
+{
+ iso9660_stat_t *p_root;
+ char **p_psz_splitpath;
+ iso9660_stat_t *p_stat;
+
+ if (!p_iso) return NULL;
+ if (!pathname) return NULL;
+
+ p_root = _fs_stat_iso_root (p_iso);
+ if (NULL == p_root) return NULL;
+
+ p_psz_splitpath = _cdio_strsplit (pathname, '/');
+ p_stat = _fs_iso_stat_traverse (p_iso, p_root, p_psz_splitpath, true);
+ free(p_root);
+ _cdio_strfreev (p_psz_splitpath);
+
+ return p_stat;
+}
+
+/*!
+ Read pathname (a directory) and return a list of iso9660_stat_t
+ of the files inside that. The caller must free the returned result.
+*/
+CdioList *
+iso9660_fs_readdir (CdIo *p_cdio, const char pathname[], bool b_mode2)
+{
+ generic_img_private_t *p_env;
+ iso9660_stat_t *p_stat;
+
+ if (!p_cdio) return NULL;
+ if (!pathname) return NULL;
+
+ p_env = (generic_img_private_t *) p_cdio->env;
+
+ p_stat = iso9660_fs_stat (p_cdio, pathname);
+ if (!p_stat) return NULL;
+
+ if (p_stat->type != _STAT_DIR) {
+ free(p_stat);
+ return NULL;
+ }
+
+ {
+ unsigned offset = 0;
+ uint8_t *_dirbuf = NULL;
+ CdioList *retval = _cdio_list_new ();
+
+ if (p_stat->size != ISO_BLOCKSIZE * p_stat->secsize)
+ {
+ cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!",
+ (unsigned) p_stat->size,
+ (unsigned long int) ISO_BLOCKSIZE * p_stat->secsize);
+ }
+
+ _dirbuf = _cdio_malloc (p_stat->secsize * ISO_BLOCKSIZE);
+
+ if (b_mode2) {
+ if (cdio_read_mode2_sectors (p_cdio, _dirbuf, p_stat->lsn, false,
+ p_stat->secsize))
+ cdio_assert_not_reached ();
+ } else {
+ if (cdio_read_mode1_sectors (p_cdio, _dirbuf, p_stat->lsn, false,
+ p_stat->secsize))
+ cdio_assert_not_reached ();
+ }
+
+ while (offset < (p_stat->secsize * ISO_BLOCKSIZE))
+ {
+ iso9660_dir_t *p_iso9660_dir = (void *) &_dirbuf[offset];
+ iso9660_stat_t *p_iso9660_stat;
+
+ if (!iso9660_get_dir_len(p_iso9660_dir))
+ {
+ offset++;
+ continue;
+ }
+
+ p_iso9660_stat = _iso9660_dir_to_statbuf(p_iso9660_dir, b_mode2,
+ p_env->i_joliet_level);
+ _cdio_list_append (retval, p_iso9660_stat);
+
+ offset += iso9660_get_dir_len(p_iso9660_dir);
+ }
+
+ cdio_assert (offset == (p_stat->secsize * ISO_BLOCKSIZE));
+
+ free (_dirbuf);
+ free (p_stat);
+ return retval;
+ }
+}
+
+/*!
+ Read pathname (a directory) and return a list of iso9660_stat_t
+ of the files inside that. The caller must free the returned result.
+*/
+CdioList *
+iso9660_ifs_readdir (iso9660_t *p_iso, const char pathname[])
+{
+ iso9660_stat_t *p_stat;
+
+ if (!p_iso) return NULL;
+ if (!pathname) return NULL;
+
+ p_stat = iso9660_ifs_stat (p_iso, pathname);
+ if (!p_stat) return NULL;
+
+ if (p_stat->type != _STAT_DIR) {
+ free(p_stat);
+ return NULL;
+ }
+
+ {
+ long int ret;
+ unsigned offset = 0;
+ uint8_t *_dirbuf = NULL;
+ CdioList *retval = _cdio_list_new ();
+
+ if (p_stat->size != ISO_BLOCKSIZE * p_stat->secsize)
+ {
+ cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!",
+ (unsigned int) p_stat->size,
+ (unsigned long int) ISO_BLOCKSIZE * p_stat->secsize);
+ }
+
+ _dirbuf = _cdio_malloc (p_stat->secsize * ISO_BLOCKSIZE);
+
+ ret = iso9660_iso_seek_read (p_iso, _dirbuf, p_stat->lsn, p_stat->secsize);
+ if (ret != ISO_BLOCKSIZE*p_stat->secsize) return NULL;
+
+ while (offset < (p_stat->secsize * ISO_BLOCKSIZE))
+ {
+ iso9660_dir_t *p_iso9660_dir = (void *) &_dirbuf[offset];
+ iso9660_stat_t *p_iso9660_stat;
+
+ if (!iso9660_get_dir_len(p_iso9660_dir))
+ {
+ offset++;
+ continue;
+ }
+
+ p_iso9660_stat = _iso9660_dir_to_statbuf(p_iso9660_dir, true,
+ p_iso->i_joliet_level);
+ _cdio_list_append (retval, p_iso9660_stat);
+
+ offset += iso9660_get_dir_len(p_iso9660_dir);
+ }
+
+ cdio_assert (offset == (p_stat->secsize * ISO_BLOCKSIZE));
+
+ free (_dirbuf);
+ free (p_stat);
+ return retval;
+ }
+}
+
+static iso9660_stat_t *
+find_fs_lsn_recurse (CdIo *p_cdio, const char pathname[], lsn_t lsn)
+{
+ CdioList *entlist = iso9660_fs_readdir (p_cdio, pathname, true);
+ CdioList *dirlist = _cdio_list_new ();
+ CdioListNode *entnode;
+
+ cdio_assert (entlist != NULL);
+
+ /* iterate over each entry in the directory */
+
+ _CDIO_LIST_FOREACH (entnode, entlist)
+ {
+ iso9660_stat_t *statbuf = _cdio_list_node_data (entnode);
+ char _fullname[4096] = { 0, };
+ char *filename = (char *) statbuf->filename;
+
+ snprintf (_fullname, sizeof (_fullname), "%s%s/", pathname, filename);
+
+ if (statbuf->type == _STAT_DIR
+ && strcmp ((char *) statbuf->filename, ".")
+ && strcmp ((char *) statbuf->filename, ".."))
+ _cdio_list_append (dirlist, strdup (_fullname));
+
+ if (statbuf->lsn == lsn) {
+ unsigned int len=sizeof(iso9660_stat_t)+strlen(statbuf->filename)+1;
+ iso9660_stat_t *ret_stat = _cdio_malloc(len);
+ memcpy(ret_stat, statbuf, len);
+ _cdio_list_free (entlist, true);
+ _cdio_list_free (dirlist, true);
+ return ret_stat;
+ }
+
+ }
+
+ _cdio_list_free (entlist, true);
+
+ /* now recurse/descend over directories encountered */
+
+ _CDIO_LIST_FOREACH (entnode, dirlist)
+ {
+ char *_fullname = _cdio_list_node_data (entnode);
+ iso9660_stat_t *ret_stat = find_fs_lsn_recurse (p_cdio, _fullname, lsn);
+
+ if (NULL != ret_stat) {
+ _cdio_list_free (dirlist, true);
+ return ret_stat;
+ }
+ }
+
+ _cdio_list_free (dirlist, true);
+ return NULL;
+}
+
+/*!
+ Given a directory pointer, find the filesystem entry that contains
+ lsn and return information about it.
+
+ Returns stat_t of entry if we found lsn, or NULL otherwise.
+ */
+iso9660_stat_t *
+iso9660_find_fs_lsn(CdIo *p_cdio, lsn_t i_lsn)
+{
+ return find_fs_lsn_recurse (p_cdio, "/", i_lsn);
+}
+
+/*!
+ Return true if ISO 9660 image has extended attrributes (XA).
+*/
+bool
+iso9660_ifs_is_xa (const iso9660_t * p_iso)
+{
+ if (!p_iso) return false;
+ return p_iso->b_xa;
+}
diff --git a/contrib/libcdio/iso9660_private.h b/contrib/libcdio/iso9660_private.h
new file mode 100644
index 000000000..9a10950aa
--- /dev/null
+++ b/contrib/libcdio/iso9660_private.h
@@ -0,0 +1,85 @@
+/*
+ $Id: iso9660_private.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ See also iso9660.h by Eric Youngdale (1993).
+
+ Copyright 1993 Yggdrasil Computing, Incorporated
+ Copyright (c) 1999,2000 J. Schilling
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_ISO9660_PRIVATE_H__
+#define __CDIO_ISO9660_PRIVATE_H__
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/types.h>
+
+#define ISO_VERSION 1
+
+PRAGMA_BEGIN_PACKED
+
+struct iso_volume_descriptor {
+ uint8_t type; /* 711 */
+ char id[5];
+ uint8_t version; /* 711 */
+ char data[2041];
+} GNUC_PACKED;
+
+#define struct_iso_volume_descriptor_SIZEOF ISO_BLOCKSIZE
+
+#define struct_iso9660_pvd_SIZEOF ISO_BLOCKSIZE
+
+/*
+ * XXX JS: The next structure has an odd length!
+ * Some compilers (e.g. on Sun3/mc68020) padd the structures to even length.
+ * For this reason, we cannot use sizeof (struct iso_path_table) or
+ * sizeof (struct iso_directory_record) to compute on disk sizes.
+ * Instead, we use offsetof(..., name) and add the name size.
+ * See mkisofs.h
+ */
+
+/* We use this to help us look up the parent inode numbers. */
+
+struct iso_path_table {
+ uint8_t name_len; /* 711 */
+ uint8_t xa_len; /* 711 */
+ uint32_t extent; /* 731/732 */
+ uint16_t parent; /* 721/722 */
+ char name[EMPTY_ARRAY_SIZE];
+} GNUC_PACKED;
+
+#define struct_iso_path_table_SIZEOF 8
+
+#define struct_iso9660_dir_SIZEOF 33
+
+PRAGMA_END_PACKED
+
+#endif /* __CDIO_ISO9660_PRIVATE_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/logging.c b/contrib/libcdio/logging.c
new file mode 100644
index 000000000..8d561debe
--- /dev/null
+++ b/contrib/libcdio/logging.c
@@ -0,0 +1,142 @@
+/*
+ $Id: logging.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <cdio/logging.h>
+#include "cdio_assert.h"
+
+static const char _rcsid[] = "$Id: logging.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $";
+
+cdio_log_level_t cdio_loglevel_default = CDIO_LOG_WARN;
+
+static void
+default_cdio_log_handler (cdio_log_level_t level, const char message[])
+{
+ switch (level)
+ {
+ case CDIO_LOG_ERROR:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stderr, "**ERROR: %s\n", message);
+ fflush (stderr);
+ }
+ exit (EXIT_FAILURE);
+ break;
+ case CDIO_LOG_DEBUG:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stdout, "--DEBUG: %s\n", message);
+ }
+ break;
+ case CDIO_LOG_WARN:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stdout, "++ WARN: %s\n", message);
+ }
+ break;
+ case CDIO_LOG_INFO:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stdout, " INFO: %s\n", message);
+ }
+ break;
+ case CDIO_LOG_ASSERT:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stderr, "!ASSERT: %s\n", message);
+ fflush (stderr);
+ }
+ abort ();
+ break;
+ default:
+ cdio_assert_not_reached ();
+ break;
+ }
+
+ fflush (stdout);
+}
+
+static cdio_log_handler_t _handler = default_cdio_log_handler;
+
+cdio_log_handler_t
+cdio_log_set_handler (cdio_log_handler_t new_handler)
+{
+ cdio_log_handler_t old_handler = _handler;
+
+ _handler = new_handler;
+
+ return old_handler;
+}
+
+static void
+cdio_logv (cdio_log_level_t level, const char format[], va_list args)
+{
+ char buf[1024] = { 0, };
+ static int in_recursion = 0;
+
+ if (in_recursion)
+ cdio_assert_not_reached ();
+
+ in_recursion = 1;
+
+ vsnprintf(buf, sizeof(buf)-1, format, args);
+
+ _handler(level, buf);
+
+ in_recursion = 0;
+}
+
+void
+cdio_log (cdio_log_level_t level, const char format[], ...)
+{
+ va_list args;
+ va_start (args, format);
+ cdio_logv (level, format, args);
+ va_end (args);
+}
+
+#define CDIO_LOG_TEMPLATE(level, LEVEL) \
+void \
+cdio_ ## level (const char format[], ...) \
+{ \
+ va_list args; \
+ va_start (args, format); \
+ cdio_logv (CDIO_LOG_ ## LEVEL, format, args); \
+ va_end (args); \
+}
+
+CDIO_LOG_TEMPLATE(debug, DEBUG)
+CDIO_LOG_TEMPLATE(info, INFO)
+CDIO_LOG_TEMPLATE(warn, WARN)
+CDIO_LOG_TEMPLATE(error, ERROR)
+
+#undef CDIO_LOG_TEMPLATE
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/portable.h b/contrib/libcdio/portable.h
new file mode 100644
index 000000000..3da436245
--- /dev/null
+++ b/contrib/libcdio/portable.h
@@ -0,0 +1,74 @@
+/*
+ $Id: portable.h,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*
+ This file contains definitions to fill in for differences or
+ deficiencies to OS or compiler irregularities. If this file is
+ included other routines can be more portable.
+*/
+
+#ifndef __CDIO_PORTABLE_H__
+#define __CDIO_PORTABLE_H__
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#if !defined(HAVE_FTRUNCATE)
+# if defined ( WIN32 )
+# define ftruncate chsize
+# endif
+#endif /*HAVE_FTRUNCATE*/
+
+#if !defined(HAVE_SNPRINTF)
+# if defined ( MSVC )
+# define snprintf _snprintf
+# endif
+#endif /*HAVE_SNPRINTF*/
+
+#if !defined(HAVE_VSNPRINTF)
+# if defined ( MSVC )
+# define snprintf _vsnprintf
+# endif
+#endif /*HAVE_SNPRINTF*/
+
+#ifdef MSVC
+# include <io.h>
+
+# ifndef S_ISBLK
+# define _S_IFBLK 0060000 /* Block Special */
+# define S_ISBLK(x) (x & _S_IFBLK)
+# endif
+
+# ifndef S_ISCHR
+# define _S_IFCHR 0020000 /* character special */
+# define S_ISCHR(x) (x & _S_IFCHR)
+# endif
+#endif /*MSVC*/
+
+#ifdef HAVE_MEMSET
+# define BZERO(ptr, size) memset(ptr, 0, size)
+#elif HAVE_BZERO
+# define BZERO(ptr, size) bzero(ptr, size)
+#else
+ Error -- you need either memset or bzero
+#endif
+
+#endif /* __CDIO_PORTABLE_H__ */
diff --git a/contrib/libcdio/scsi_mmc.c b/contrib/libcdio/scsi_mmc.c
new file mode 100644
index 000000000..9b4a456a5
--- /dev/null
+++ b/contrib/libcdio/scsi_mmc.c
@@ -0,0 +1,589 @@
+/* Common SCSI Multimedia Command (MMC) routines.
+
+ $Id: scsi_mmc.c,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/logging.h>
+#include <cdio/scsi_mmc.h>
+#include "cdio_private.h"
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
+/*!
+ On input a MODE_SENSE command was issued and we have the results
+ in p. We interpret this and return a bit mask set according to the
+ capabilities.
+ */
+void
+scsi_mmc_get_drive_cap_buf(const uint8_t *p,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap)
+{
+ /* Reader */
+ if (p[2] & 0x01) *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_R;
+ if (p[2] & 0x02) *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_RW;
+ if (p[2] & 0x08) *p_read_cap |= CDIO_DRIVE_CAP_READ_DVD_ROM;
+ if (p[4] & 0x01) *p_read_cap |= CDIO_DRIVE_CAP_READ_AUDIO;
+ if (p[5] & 0x01) *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_DA;
+ if (p[5] & 0x10) *p_read_cap |= CDIO_DRIVE_CAP_READ_C2_ERRS;
+
+ /* Writer */
+ if (p[3] & 0x01) *p_write_cap |= CDIO_DRIVE_CAP_WRITE_CD_R;
+ if (p[3] & 0x02) *p_write_cap |= CDIO_DRIVE_CAP_WRITE_CD_RW;
+ if (p[3] & 0x10) *p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_R;
+ if (p[3] & 0x20) *p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_RAM;
+ if (p[4] & 0x80) *p_misc_cap |= CDIO_DRIVE_CAP_WRITE_BURN_PROOF;
+
+ /* Misc */
+ if (p[4] & 0x40) *p_misc_cap |= CDIO_DRIVE_CAP_MISC_MULTI_SESSION;
+ if (p[6] & 0x01) *p_misc_cap |= CDIO_DRIVE_CAP_MISC_LOCK;
+ if (p[6] & 0x08) *p_misc_cap |= CDIO_DRIVE_CAP_MISC_EJECT;
+ if (p[6] >> 5 != 0)
+ *p_misc_cap |= CDIO_DRIVE_CAP_MISC_CLOSE_TRAY;
+}
+
+/*!
+ Return the number of length in bytes of the Command Descriptor
+ buffer (CDB) for a given SCSI MMC command. The length will be
+ either 6, 10, or 12.
+*/
+uint8_t
+scsi_mmc_get_cmd_len(uint8_t scsi_cmd)
+{
+ static const uint8_t scsi_cdblen[8] = {6, 10, 10, 12, 12, 12, 10, 10};
+ return scsi_cdblen[((scsi_cmd >> 5) & 7)];
+}
+
+/*!
+ Run a SCSI MMC command.
+
+ cdio CD structure set by cdio_open().
+ i_timeout time in milliseconds we will wait for the command
+ to complete. If this value is -1, use the default
+ time-out value.
+ buf Buffer for data, both sending and receiving
+ len Size of buffer
+ e_direction direction the transfer is to go
+ cdb CDB bytes. All values that are needed should be set on
+ input. We'll figure out what the right CDB length should be.
+
+ We return 0 if command completed successfully and 1 if not.
+ */
+int
+scsi_mmc_run_cmd( const CdIo *p_cdio, unsigned int i_timeout_ms,
+ const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction, unsigned int i_buf,
+ /*in/out*/ void *p_buf )
+{
+ if (p_cdio && p_cdio->op.run_scsi_mmc_cmd) {
+ return p_cdio->op.run_scsi_mmc_cmd(p_cdio->env, i_timeout_ms,
+ scsi_mmc_get_cmd_len(p_cdb->field[0]),
+ p_cdb, e_direction, i_buf, p_buf);
+ } else
+ return 1;
+}
+
+#define DEFAULT_TIMEOUT_MS 6000
+
+/*!
+ * Eject using SCSI MMC commands. Return 0 if successful.
+ */
+int
+scsi_mmc_eject_media( const CdIo *cdio )
+{
+ int i_status;
+ scsi_mmc_cdb_t cdb = {{0, }};
+ uint8_t buf[1];
+ scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd;
+
+ if ( ! cdio || ! cdio->op.run_scsi_mmc_cmd )
+ return -2;
+
+ run_scsi_mmc_cmd = cdio->op.run_scsi_mmc_cmd;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_ALLOW_MEDIUM_REMOVAL);
+
+ i_status = run_scsi_mmc_cmd (cdio->env, DEFAULT_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]), &cdb,
+ SCSI_MMC_DATA_WRITE, 0, &buf);
+ if (0 != i_status)
+ return i_status;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_START_STOP);
+ cdb.field[4] = 1;
+ i_status = run_scsi_mmc_cmd (cdio->env, DEFAULT_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]), &cdb,
+ SCSI_MMC_DATA_WRITE, 0, &buf);
+ if (0 != i_status)
+ return i_status;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_START_STOP);
+ cdb.field[4] = 2; /* eject */
+
+ return run_scsi_mmc_cmd (cdio->env, DEFAULT_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]), &cdb,
+ SCSI_MMC_DATA_WRITE, 0, &buf);
+
+}
+
+/*! Packet driver to read mode2 sectors.
+ Can read only up to 25 blocks.
+*/
+int
+scsi_mmc_read_sectors ( const CdIo *cdio, void *p_buf, lba_t lba,
+ int sector_type, unsigned int nblocks )
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+
+ scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd;
+
+ if ( ! cdio || ! cdio->op.run_scsi_mmc_cmd )
+ return -2;
+
+ run_scsi_mmc_cmd = cdio->op.run_scsi_mmc_cmd;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_CD);
+ CDIO_MMC_SET_READ_TYPE (cdb.field, sector_type);
+ CDIO_MMC_SET_READ_LBA (cdb.field, lba);
+ CDIO_MMC_SET_READ_LENGTH24(cdb.field, nblocks);
+ CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(cdb.field,
+ CDIO_MMC_MCSB_ALL_HEADERS);
+
+ return run_scsi_mmc_cmd (cdio->env, DEFAULT_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]), &cdb,
+ SCSI_MMC_DATA_READ,
+ CDIO_CD_FRAMESIZE_RAW * nblocks,
+ p_buf);
+}
+
+int
+scsi_mmc_set_blocksize_private ( const void *p_env,
+ const scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd,
+ unsigned int bsize)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+
+ struct
+ {
+ uint8_t reserved1;
+ uint8_t medium;
+ uint8_t reserved2;
+ uint8_t block_desc_length;
+ uint8_t density;
+ uint8_t number_of_blocks_hi;
+ uint8_t number_of_blocks_med;
+ uint8_t number_of_blocks_lo;
+ uint8_t reserved3;
+ uint8_t block_length_hi;
+ uint8_t block_length_med;
+ uint8_t block_length_lo;
+ } mh;
+
+ if ( ! p_env || ! run_scsi_mmc_cmd )
+ return -2;
+
+ memset (&mh, 0, sizeof (mh));
+ mh.block_desc_length = 0x08;
+ mh.block_length_hi = (bsize >> 16) & 0xff;
+ mh.block_length_med = (bsize >> 8) & 0xff;
+ mh.block_length_lo = (bsize >> 0) & 0xff;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_MODE_SELECT_6);
+
+ cdb.field[1] = 1 << 4;
+ cdb.field[4] = 12;
+
+ return run_scsi_mmc_cmd (p_env, DEFAULT_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]), &cdb,
+ SCSI_MMC_DATA_WRITE, sizeof(mh), &mh);
+}
+
+int
+scsi_mmc_set_blocksize ( const CdIo *cdio, unsigned int bsize)
+{
+ if ( ! cdio ) return -2;
+ return
+ scsi_mmc_set_blocksize_private (cdio->env, cdio->op.run_scsi_mmc_cmd,
+ bsize);
+}
+
+
+/*!
+ Return the the kind of drive capabilities of device.
+ */
+void
+scsi_mmc_get_drive_cap_private (const void *p_env,
+ const scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap)
+{
+ /* Largest buffer size we use. */
+#define BUF_MAX 2048
+ uint8_t buf[BUF_MAX] = { 0, };
+
+ scsi_mmc_cdb_t cdb = {{0, }};
+ int i_status;
+ uint16_t i_data = BUF_MAX;
+
+ if ( ! p_env || ! run_scsi_mmc_cmd )
+ return;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_MODE_SENSE_10);
+ cdb.field[1] = 0x0;
+ cdb.field[2] = CDIO_MMC_ALL_PAGES;
+
+ retry:
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, 8);
+
+ /* In the first run we run MODE SENSE 10 we are trying to get the
+ length of the data features. */
+ i_status = run_scsi_mmc_cmd (p_env, DEFAULT_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ sizeof(buf), &buf);
+ if (0 == i_status) {
+ uint16_t i_data_try = (uint16_t) CDIO_MMC_GET_LEN16(buf);
+ if (i_data_try < BUF_MAX) i_data = i_data_try;
+ }
+
+ /* Now try getting all features with length set above, possibly
+ truncated or the default length if we couldn't get the proper
+ length. */
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, i_data);
+
+ i_status = run_scsi_mmc_cmd (p_env, DEFAULT_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ sizeof(buf), &buf);
+
+ if (0 != i_status && CDIO_MMC_CAPABILITIES_PAGE != cdb.field[2]) {
+ cdb.field[2] = CDIO_MMC_CAPABILITIES_PAGE;
+ goto retry;
+ }
+
+ if (0 == i_status) {
+ uint8_t *p;
+ uint8_t *p_max = buf + 256;
+
+ *p_read_cap = 0;
+ *p_write_cap = 0;
+ *p_misc_cap = 0;
+
+ /* set to first sense mask, and then walk through the masks */
+ p = buf + 8;
+ while( (p < &(buf[2+i_data])) && (p < p_max) ) {
+ uint8_t which_page;
+
+ which_page = p[0] & 0x3F;
+ switch( which_page )
+ {
+ case CDIO_MMC_AUDIO_CTL_PAGE:
+ case CDIO_MMC_R_W_ERROR_PAGE:
+ case CDIO_MMC_CDR_PARMS_PAGE:
+ /* Don't handle these yet. */
+ break;
+ case CDIO_MMC_CAPABILITIES_PAGE:
+ scsi_mmc_get_drive_cap_buf(p, p_read_cap, p_write_cap, p_misc_cap);
+ break;
+ default: ;
+ }
+ p += (p[1] + 2);
+ }
+ } else {
+ cdio_info("%s: %s\n", "error in MODE_SELECT", strerror(errno));
+ *p_read_cap = CDIO_DRIVE_CAP_ERROR;
+ *p_write_cap = CDIO_DRIVE_CAP_ERROR;
+ *p_misc_cap = CDIO_DRIVE_CAP_ERROR;
+ }
+ return;
+}
+
+void
+scsi_mmc_get_drive_cap (const CdIo *p_cdio,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap)
+{
+ if ( ! p_cdio ) return;
+ scsi_mmc_get_drive_cap_private (p_cdio->env,
+ p_cdio->op.run_scsi_mmc_cmd,
+ p_read_cap, p_write_cap, p_misc_cap);
+}
+
+void
+scsi_mmc_get_drive_cap_generic (const void *p_user_data,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap)
+{
+ const generic_img_private_t *p_env = p_user_data;
+ scsi_mmc_get_drive_cap( p_env->cdio,
+ p_read_cap, p_write_cap, p_misc_cap );
+}
+
+
+/*!
+ Get the DVD type associated with cd object.
+*/
+discmode_t
+scsi_mmc_get_dvd_struct_physical_private ( void *p_env, const
+ scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd,
+ cdio_dvd_struct_t *s)
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+ unsigned char buf[4 + 4 * 20], *base;
+ int i_status;
+ uint8_t layer_num = s->physical.layer_num;
+
+ cdio_dvd_layer_t *layer;
+
+ if ( ! p_env || ! run_scsi_mmc_cmd )
+ return -2;
+
+ if (layer_num >= CDIO_DVD_MAX_LAYERS)
+ return -EINVAL;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_DVD_STRUCTURE);
+ cdb.field[6] = layer_num;
+ cdb.field[7] = CDIO_DVD_STRUCT_PHYSICAL;
+ cdb.field[9] = sizeof(buf) & 0xff;
+
+ i_status = run_scsi_mmc_cmd(p_env, DEFAULT_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ sizeof(buf), &buf);
+ if (0 != i_status)
+ return CDIO_DISC_MODE_ERROR;
+
+ base = &buf[4];
+ layer = &s->physical.layer[layer_num];
+
+ /*
+ * place the data... really ugly, but at least we won't have to
+ * worry about endianess in userspace.
+ */
+ memset(layer, 0, sizeof(*layer));
+ layer->book_version = base[0] & 0xf;
+ layer->book_type = base[0] >> 4;
+ layer->min_rate = base[1] & 0xf;
+ layer->disc_size = base[1] >> 4;
+ layer->layer_type = base[2] & 0xf;
+ layer->track_path = (base[2] >> 4) & 1;
+ layer->nlayers = (base[2] >> 5) & 3;
+ layer->track_density = base[3] & 0xf;
+ layer->linear_density = base[3] >> 4;
+ layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
+ layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
+ layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
+ layer->bca = base[16] >> 7;
+
+ return 0;
+}
+
+
+/*!
+ Get the DVD type associated with cd object.
+*/
+discmode_t
+scsi_mmc_get_dvd_struct_physical ( const CdIo *p_cdio, cdio_dvd_struct_t *s)
+{
+ if ( ! p_cdio ) return -2;
+ return
+ scsi_mmc_get_dvd_struct_physical_private (p_cdio->env,
+ p_cdio->op.run_scsi_mmc_cmd,
+ s);
+}
+
+/*!
+ Get the CD-ROM hardware info via a SCSI MMC INQUIRY command.
+ False is returned if we had an error getting the information.
+*/
+bool
+scsi_mmc_get_hwinfo ( const CdIo *p_cdio,
+ /*out*/ cdio_hwinfo_t *hw_info )
+{
+ int i_status; /* Result of SCSI MMC command */
+ char buf[36] = { 0, }; /* Place to hold returned data */
+ scsi_mmc_cdb_t cdb = {{0, }}; /* Command Descriptor Block */
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_INQUIRY);
+ cdb.field[4] = sizeof(buf);
+
+ if (! p_cdio || ! hw_info ) return false;
+
+ i_status = scsi_mmc_run_cmd(p_cdio, DEFAULT_TIMEOUT_MS,
+ &cdb, SCSI_MMC_DATA_READ,
+ sizeof(buf), &buf);
+ if (i_status == 0) {
+
+ memcpy(hw_info->psz_vendor,
+ buf + 8,
+ sizeof(hw_info->psz_vendor)-1);
+ hw_info->psz_vendor[sizeof(hw_info->psz_vendor)-1] = '\0';
+ memcpy(hw_info->psz_model,
+ buf + 8 + CDIO_MMC_HW_VENDOR_LEN,
+ sizeof(hw_info->psz_model)-1);
+ hw_info->psz_model[sizeof(hw_info->psz_model)-1] = '\0';
+ memcpy(hw_info->psz_revision,
+ buf + 8 + CDIO_MMC_HW_VENDOR_LEN + CDIO_MMC_HW_MODEL_LEN,
+ sizeof(hw_info->psz_revision)-1);
+ hw_info->psz_revision[sizeof(hw_info->psz_revision)-1] = '\0';
+ return true;
+ }
+ return false;
+}
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+char *
+scsi_mmc_get_mcn_private ( void *p_env,
+ const scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd
+ )
+{
+ scsi_mmc_cdb_t cdb = {{0, }};
+ char buf[28] = { 0, };
+ int i_status;
+
+ if ( ! p_env || ! run_scsi_mmc_cmd )
+ return NULL;
+
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_SUBCHANNEL);
+ cdb.field[1] = 0x0;
+ cdb.field[2] = 0x40;
+ cdb.field[3] = CDIO_SUBCHANNEL_MEDIA_CATALOG;
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, sizeof(buf));
+
+ i_status = run_scsi_mmc_cmd(p_env, DEFAULT_TIMEOUT_MS,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ sizeof(buf), buf);
+ if(i_status == 0) {
+ return strdup(&buf[9]);
+ }
+ return NULL;
+}
+
+char *
+scsi_mmc_get_mcn ( const CdIo *p_cdio )
+{
+ if ( ! p_cdio ) return NULL;
+ return scsi_mmc_get_mcn_private (p_cdio->env,
+ p_cdio->op.run_scsi_mmc_cmd );
+}
+
+char *
+scsi_mmc_get_mcn_generic (const void *p_user_data)
+{
+ const generic_img_private_t *p_env = p_user_data;
+ return scsi_mmc_get_mcn( p_env->cdio );
+}
+
+/*
+ Read cdtext information for a CdIo object .
+
+ return true on success, false on error or CD-Text information does
+ not exist.
+*/
+bool
+scsi_mmc_init_cdtext_private ( void *p_user_data,
+ const scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd,
+ set_cdtext_field_fn_t set_cdtext_field_fn
+ )
+{
+
+ generic_img_private_t *p_env = p_user_data;
+ scsi_mmc_cdb_t cdb = {{0, }};
+ unsigned char wdata[5000] = { 0, };
+ int i_status, i_errno;
+
+ if ( ! p_env || ! run_scsi_mmc_cmd || p_env->b_cdtext_error )
+ return false;
+
+ /* Operation code */
+ CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_TOC);
+
+ cdb.field[1] = CDIO_CDROM_MSF;
+ /* Format */
+ cdb.field[2] = CDIO_MMC_READTOC_FMT_CDTEXT;
+
+ /* Setup to read header, to get length of data */
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, 4);
+
+ errno = 0;
+
+/* Set read timeout 3 minues. */
+#define READ_TIMEOUT 3*60*1000
+
+ /* We may need to give CD-Text a little more time to complete. */
+ /* First off, just try and read the size */
+ i_status = run_scsi_mmc_cmd (p_env, READ_TIMEOUT,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ 4, &wdata);
+
+ if (i_status != 0) {
+ cdio_info ("CD-Text read failed for header: %s\n", strerror(errno));
+ i_errno = errno;
+ p_env->b_cdtext_error = true;
+ return false;
+ } else {
+ /* Now read the CD-Text data */
+ int i_cdtext = CDIO_MMC_GET_LEN16(wdata);
+
+ if (i_cdtext > sizeof(wdata)) i_cdtext = sizeof(wdata);
+
+ CDIO_MMC_SET_READ_LENGTH16(cdb.field, i_cdtext);
+ i_status = run_scsi_mmc_cmd (p_env, READ_TIMEOUT,
+ scsi_mmc_get_cmd_len(cdb.field[0]),
+ &cdb, SCSI_MMC_DATA_READ,
+ i_cdtext, &wdata);
+ if (i_status != 0) {
+ cdio_info ("CD-Text read for text failed: %s\n", strerror(errno));
+ i_errno = errno;
+ p_env->b_cdtext_error = true;
+ return false;
+ }
+ p_env->b_cdtext_init = true;
+ return cdtext_data_init(p_env, p_env->i_first_track, wdata,
+ set_cdtext_field_fn);
+ }
+}
+
diff --git a/contrib/libcdio/scsi_mmc.h b/contrib/libcdio/scsi_mmc.h
new file mode 100644
index 000000000..b6ea05b7a
--- /dev/null
+++ b/contrib/libcdio/scsi_mmc.h
@@ -0,0 +1,63 @@
+/*
+ $Id: scsi_mmc.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*
+ This file contains common definitions/routines for SCSI MMC
+ (Multimedia commands).
+*/
+
+#ifndef __SCSI_MMC_H__
+#define __SCSI_MMC_H__
+
+/* Leval values that can go into READ_CD */
+#define CDIO_MMC_READ_TYPE_ANY 0 /* All types */
+#define CDIO_MMC_READ_TYPE_CDDA 1 /* Only CD-DA sectors */
+#define CDIO_MMC_READ_TYPE_MODE1 2 /* Only mode1 sectors (user data = 2048) */
+#define CDIO_MMC_READ_TYPE_MODE2 3 /* mode2 sectors form1 or form2 */
+#define CDIO_MMC_READ_TYPE_M2F1 4 /* mode2 sectors form1 */
+#define CDIO_MMC_READ_TYPE_M2F2 5 /* mode2 sectors form2 */
+
+/* The generic packet command opcodes for CD/DVD Logical Units,
+ * From Table 57 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */
+#define CDIO_MMC_GPCMD_READ_CD 0xbe
+#define CDIO_MMC_GPCMD_READ_10 0x28
+#define CDIO_MMC_GPCMD_READ_12 0xa8
+
+#define CDIO_MMC_SET_READ_TYPE(rec, sector_type) \
+ rec[1] = (sector_type << 2)
+
+
+#define CDIO_MMC_SET_READ_LBA(rec, lba) \
+ rec[2] = (lba >> 24) & 0xff; \
+ rec[3] = (lba >> 16) & 0xff; \
+ rec[4] = (lba >> 8) & 0xff; \
+ rec[5] = (lba ) & 0xff
+
+#define CDIO_MMC_SET_READ_LENGTH(rec, len) \
+ rec[6] = (len >> 16) & 0xff; \
+ rec[7] = (len >> 8) & 0xff; \
+ rec[8] = (len ) & 0xff
+
+#define CDIO_MMC_MCSB_ALL_HEADERS 0x78
+
+#define CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(rec, val) \
+ rec[9] = val;
+
+#endif /* __SCSI_MMC_H__ */
diff --git a/contrib/libcdio/scsi_mmc_private.h b/contrib/libcdio/scsi_mmc_private.h
new file mode 100644
index 000000000..b3f1e7061
--- /dev/null
+++ b/contrib/libcdio/scsi_mmc_private.h
@@ -0,0 +1,105 @@
+/* private MMC helper routines.
+
+ $Id: scsi_mmc_private.h,v 1.1 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#include <cdio/scsi_mmc.h>
+#include "cdtext_private.h"
+
+/*! Convert milliseconds to seconds taking the ceiling value, i.e.
+ 1002 milliseconds gets rounded to 2 seconds.
+*/
+#define SECS2MSECS 1000
+static inline unsigned int
+msecs2secs(unsigned int msecs)
+{
+ return (msecs+(SECS2MSECS-1)) / SECS2MSECS;
+}
+#undef SECS2MSECS
+
+typedef
+int (*scsi_mmc_run_cmd_fn_t) ( const void *p_user_data,
+ unsigned int i_timeout_ms,
+ unsigned int i_cdb,
+ const scsi_mmc_cdb_t *p_cdb,
+ scsi_mmc_direction_t e_direction,
+ unsigned int i_buf, /*in/out*/ void *p_buf );
+
+int scsi_mmc_set_blocksize_mmc_private ( const void *p_env, const
+ scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd,
+ unsigned int bsize );
+
+/*!
+ Get the DVD type associated with cd object.
+*/
+discmode_t
+scsi_mmc_get_dvd_struct_physical_private ( void *p_env, const
+ scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd,
+ cdio_dvd_struct_t *s );
+
+
+int
+scsi_mmc_set_blocksize_private ( const void *p_env,
+ const scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd,
+ unsigned int bsize);
+
+char *scsi_mmc_get_mcn_private ( void *p_env,
+ const scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd
+ );
+
+char *scsi_mmc_get_mcn_generic (const void *p_user_data);
+
+bool scsi_mmc_init_cdtext_private ( void *user_data, const
+ scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd,
+ set_cdtext_field_fn_t set_cdtext_field_fn
+ );
+
+/*!
+ On input a MODE_SENSE command was issued and we have the results
+ in p. We interpret this and return a bit mask set according to the
+ capabilities.
+ */
+void scsi_mmc_get_drive_cap_buf(const uint8_t *p,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap);
+
+/*!
+ Return the the kind of drive capabilities of device.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+void
+scsi_mmc_get_drive_cap_private (const void *p_env,
+ const scsi_mmc_run_cmd_fn_t run_scsi_mmc_cmd,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap);
+void
+scsi_mmc_get_drive_cap_generic (const void *p_user_data,
+ /*out*/ cdio_drive_read_cap_t *p_read_cap,
+ /*out*/ cdio_drive_write_cap_t *p_write_cap,
+ /*out*/ cdio_drive_misc_cap_t *p_misc_cap);
+
+
+
+
+
diff --git a/contrib/libcdio/sector.c b/contrib/libcdio/sector.c
new file mode 100644
index 000000000..da49e9908
--- /dev/null
+++ b/contrib/libcdio/sector.c
@@ -0,0 +1,289 @@
+/*
+ $Id: sector.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include <cdio/logging.h>
+#include "cdio_assert.h"
+
+#include <stdio.h>
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include <ctype.h>
+
+static const char _rcsid[] = "$Id: sector.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $";
+
+lba_t
+cdio_lba_to_lsn (lba_t lba)
+{
+ if (CDIO_INVALID_LBA == lba) return CDIO_INVALID_LSN;
+ return lba - CDIO_PREGAP_SECTORS;
+}
+
+/*
+ The below is adapted from cdparanoia code which claims it is
+ straight from the MMC3 spec.
+*/
+
+void
+cdio_lsn_to_msf (lsn_t lsn, msf_t *msf)
+{
+ int m, s, f;
+
+ cdio_assert (msf != 0);
+
+ if ( lsn >= -CDIO_PREGAP_SECTORS ){
+ m = (lsn + CDIO_PREGAP_SECTORS) / CDIO_CD_FRAMES_PER_MIN;
+ lsn -= m * CDIO_CD_FRAMES_PER_MIN;
+ s = (lsn + CDIO_PREGAP_SECTORS) / CDIO_CD_FRAMES_PER_SEC;
+ lsn -= s * CDIO_CD_FRAMES_PER_SEC;
+ f = lsn + CDIO_PREGAP_SECTORS;
+ } else {
+ m = (lsn + CDIO_CD_MAX_LSN) / CDIO_CD_FRAMES_PER_MIN;
+ lsn -= m * (CDIO_CD_FRAMES_PER_MIN);
+ s = (lsn+CDIO_CD_MAX_LSN) / CDIO_CD_FRAMES_PER_SEC;
+ lsn -= s * CDIO_CD_FRAMES_PER_SEC;
+ f = lsn + CDIO_CD_MAX_LSN;
+ }
+
+ if (m > 99) {
+ cdio_warn ("number of minutes (%d) truncated to 99.", m);
+ m = 99;
+ }
+
+ msf->m = cdio_to_bcd8 (m);
+ msf->s = cdio_to_bcd8 (s);
+ msf->f = cdio_to_bcd8 (f);
+}
+
+/*!
+ Convert an LBA into a string representation of the MSF.
+ \warning cdio_lba_to_msf_str returns new allocated string */
+char *
+cdio_lba_to_msf_str (lba_t lba)
+{
+
+ if (CDIO_INVALID_LBA == lba) {
+ return strdup("*INVALID");
+ } else {
+ msf_t msf;
+ msf.m = msf.s = msf.f = 0;
+ cdio_lba_to_msf (lba, &msf);
+ return cdio_msf_to_str(&msf);
+ }
+}
+
+/*!
+ Convert an LSN into the corresponding LBA.
+ CDIO_INVALID_LBA is returned if there is an error.
+*/
+lba_t
+cdio_lsn_to_lba (lsn_t lsn)
+{
+ if (CDIO_INVALID_LSN == lsn) return CDIO_INVALID_LBA;
+ return lsn + CDIO_PREGAP_SECTORS;
+}
+
+/*!
+ Convert an LBA into the corresponding MSF.
+*/
+void
+cdio_lba_to_msf (lba_t lba, msf_t *msf)
+{
+ cdio_assert (msf != 0);
+ cdio_lsn_to_msf(cdio_lba_to_lsn(lba), msf);
+}
+
+/*!
+ Convert a MSF into the corresponding LBA.
+ CDIO_INVALID_LBA is returned if there is an error.
+*/
+lba_t
+cdio_msf_to_lba (const msf_t *msf)
+{
+ uint32_t lba = 0;
+
+ cdio_assert (msf != 0);
+
+ lba = cdio_from_bcd8 (msf->m);
+ lba *= CDIO_CD_SECS_PER_MIN;
+
+ lba += cdio_from_bcd8 (msf->s);
+ lba *= CDIO_CD_FRAMES_PER_SEC;
+
+ lba += cdio_from_bcd8 (msf->f);
+
+ return lba;
+}
+
+/*!
+ Convert a MSF into the corresponding LSN.
+ CDIO_INVALID_LSN is returned if there is an error.
+*/
+lba_t
+cdio_msf_to_lsn (const msf_t *msf)
+{
+ return cdio_lba_to_lsn(cdio_msf_to_lba (msf));
+}
+
+/*!
+ Convert an LBA into a string representation of the MSF.
+ \warning cdio_lba_to_msf_str returns new allocated string */
+char *
+cdio_msf_to_str (const msf_t *msf)
+{
+ char buf[16];
+
+ snprintf (buf, sizeof (buf), "%2.2x:%2.2x:%2.2x", msf->m, msf->s, msf->f);
+ return strdup (buf);
+}
+
+/*!
+ Convert a MSF - broken out as 3 integer components into the
+ corresponding LBA.
+ CDIO_INVALID_LBA is returned if there is an error.
+*/
+lba_t
+cdio_msf3_to_lba (unsigned int minutes, unsigned int seconds,
+ unsigned int frames)
+{
+ return ((minutes * CDIO_CD_SECS_PER_MIN + seconds) * CDIO_CD_FRAMES_PER_SEC
+ + frames);
+}
+
+/*!
+ Convert a string of the form MM:SS:FF into the corresponding LBA.
+ CDIO_INVALID_LBA is returned if there is an error.
+*/
+lba_t
+cdio_mmssff_to_lba (const char *psz_mmssff)
+{
+ int psz_field;
+ lba_t ret;
+ char c;
+
+ if (0 == strcmp (psz_mmssff, "0"))
+ return 0;
+
+ c = *psz_mmssff++;
+ if(c >= '0' && c <= '9')
+ psz_field = (c - '0');
+ else
+ return CDIO_INVALID_LBA;
+ while(':' != (c = *psz_mmssff++)) {
+ if(c >= '0' && c <= '9')
+ psz_field = psz_field * 10 + (c - '0');
+ else
+ return CDIO_INVALID_LBA;
+ }
+
+ ret = cdio_msf3_to_lba (psz_field, 0, 0);
+
+ c = *psz_mmssff++;
+ if(c >= '0' && c <= '9')
+ psz_field = (c - '0');
+ else
+ return CDIO_INVALID_LBA;
+ if(':' != (c = *psz_mmssff++)) {
+ if(c >= '0' && c <= '9') {
+ psz_field = psz_field * 10 + (c - '0');
+ c = *psz_mmssff++;
+ if(c != ':')
+ return CDIO_INVALID_LBA;
+ }
+ else
+ return CDIO_INVALID_LBA;
+ }
+
+ if(psz_field >= CDIO_CD_SECS_PER_MIN)
+ return CDIO_INVALID_LBA;
+
+ ret += cdio_msf3_to_lba (0, psz_field, 0);
+
+ c = *psz_mmssff++;
+ if (isdigit(c))
+ psz_field = (c - '0');
+ else
+ return -1;
+ if('\0' != (c = *psz_mmssff++)) {
+ if (isdigit(c)) {
+ psz_field = psz_field * 10 + (c - '0');
+ c = *psz_mmssff++;
+ }
+ else
+ return CDIO_INVALID_LBA;
+ }
+
+ if('\0' != c)
+ return CDIO_INVALID_LBA;
+
+ if(psz_field >= CDIO_CD_FRAMES_PER_SEC)
+ return CDIO_INVALID_LBA;
+
+ ret += psz_field;
+
+ return ret;
+}
+
+bool
+cdio_is_discmode_cdrom(discmode_t discmode)
+{
+ switch (discmode) {
+ case CDIO_DISC_MODE_CD_DA:
+ case CDIO_DISC_MODE_CD_DATA:
+ case CDIO_DISC_MODE_CD_XA:
+ case CDIO_DISC_MODE_CD_MIXED:
+ case CDIO_DISC_MODE_NO_INFO:
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool
+cdio_is_discmode_dvd(discmode_t discmode)
+{
+ switch (discmode) {
+ case CDIO_DISC_MODE_DVD_ROM:
+ case CDIO_DISC_MODE_DVD_RAM:
+ case CDIO_DISC_MODE_DVD_R:
+ case CDIO_DISC_MODE_DVD_RW:
+ case CDIO_DISC_MODE_DVD_PR:
+ case CDIO_DISC_MODE_DVD_PRW:
+ return true;
+ default:
+ return false;
+ }
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/util.c b/contrib/libcdio/util.c
new file mode 100644
index 000000000..9c646daf6
--- /dev/null
+++ b/contrib/libcdio/util.c
@@ -0,0 +1,197 @@
+/*
+ $Id: util.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#ifdef HAVE_INTTYPES_H
+#include "inttypes.h"
+#endif
+
+#include "cdio_assert.h"
+#include <cdio/types.h>
+#include <cdio/util.h>
+
+static const char _rcsid[] = "$Id: util.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $";
+
+size_t
+_cdio_strlenv(char **str_array)
+{
+ size_t n = 0;
+
+ cdio_assert (str_array != NULL);
+
+ while(str_array[n])
+ n++;
+
+ return n;
+}
+
+void
+_cdio_strfreev(char **strv)
+{
+ int n;
+
+ cdio_assert (strv != NULL);
+
+ for(n = 0; strv[n]; n++)
+ free(strv[n]);
+
+ free(strv);
+}
+
+char *
+_cdio_strjoin (char *strv[], unsigned count, const char delim[])
+{
+ size_t len;
+ char *new_str;
+ unsigned n;
+
+ cdio_assert (strv != NULL);
+ cdio_assert (delim != NULL);
+
+ len = (count-1) * strlen (delim);
+
+ for (n = 0;n < count;n++)
+ len += strlen (strv[n]);
+
+ len++;
+
+ new_str = _cdio_malloc (len);
+ new_str[0] = '\0';
+
+ for (n = 0;n < count;n++)
+ {
+ if (n)
+ strcat (new_str, delim);
+ strcat (new_str, strv[n]);
+ }
+
+ return new_str;
+}
+
+char **
+_cdio_strsplit(const char str[], char delim) /* fixme -- non-reentrant */
+{
+ int n;
+ char **strv = NULL;
+ char *_str, *p;
+ char _delim[2] = { 0, 0 };
+
+ cdio_assert (str != NULL);
+
+ _str = strdup(str);
+ _delim[0] = delim;
+
+ cdio_assert (_str != NULL);
+
+ n = 1;
+ p = _str;
+ while(*p)
+ if (*(p++) == delim)
+ n++;
+
+ strv = _cdio_malloc (sizeof (char *) * (n+1));
+
+ n = 0;
+ while((p = strtok(n ? NULL : _str, _delim)) != NULL)
+ strv[n++] = strdup(p);
+
+ free(_str);
+
+ return strv;
+}
+
+void *
+_cdio_malloc (size_t size)
+{
+ void *new_mem = malloc (size);
+
+ cdio_assert (new_mem != NULL);
+
+ memset (new_mem, 0, size);
+
+ return new_mem;
+}
+
+void *
+_cdio_memdup (const void *mem, size_t count)
+{
+ void *new_mem = NULL;
+
+ if (mem)
+ {
+ new_mem = _cdio_malloc (count);
+ memcpy (new_mem, mem, count);
+ }
+
+ return new_mem;
+}
+
+char *
+_cdio_strdup_upper (const char str[])
+{
+ char *new_str = NULL;
+
+ if (str)
+ {
+ char *p;
+
+ p = new_str = strdup (str);
+
+ while (*p)
+ {
+ *p = toupper (*p);
+ p++;
+ }
+ }
+
+ return new_str;
+}
+
+uint8_t
+cdio_to_bcd8 (uint8_t n)
+{
+ /*cdio_assert (n < 100);*/
+
+ return ((n/10)<<4) | (n%10);
+}
+
+uint8_t
+cdio_from_bcd8(uint8_t p)
+{
+ return (0xf & p)+(10*(p >> 4));
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libcdio/xa.c b/contrib/libcdio/xa.c
new file mode 100644
index 000000000..f811f7ebd
--- /dev/null
+++ b/contrib/libcdio/xa.c
@@ -0,0 +1,136 @@
+/*
+ $Id: xa.c,v 1.3 2005/01/01 02:43:57 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+/* Public headers */
+#include <cdio/iso9660.h>
+#include <cdio/util.h>
+#include <cdio/bytesex.h>
+
+/* Private headers */
+#include "cdio_assert.h"
+
+#define BUF_COUNT 16
+#define BUF_SIZE 80
+
+/* Return a pointer to a internal free buffer */
+static char *
+_getbuf (void)
+{
+ static char _buf[BUF_COUNT][BUF_SIZE];
+ static int _num = -1;
+
+ _num++;
+ _num %= BUF_COUNT;
+
+ memset (_buf[_num], 0, BUF_SIZE);
+
+ return _buf[_num];
+}
+
+/*!
+ Returns a string which interpreting the extended attribute xa_attr.
+ For example:
+ \verbatim
+ d---1xrxrxr
+ ---2--r-r-r
+ -a--1xrxrxr
+ \endverbatim
+
+ A description of the characters in the string follows
+ The 1st character is either "d" if the entry is a directory, or "-" if not.
+ The 2nd character is either "a" if the entry is CDDA (audio), or "-" if not.
+ The 3rd character is either "i" if the entry is interleaved, or "-" if not.
+ The 4th character is either "2" if the entry is mode2 form2 or "-" if not.
+ The 5th character is either "1" if the entry is mode2 form1 or "-" if not.
+ Note that an entry will either be in mode2 form1 or mode form2. That
+ is you will either see "2-" or "-1" in the 4th & 5th positions.
+
+ The 6th and 7th characters refer to permissions for a user while the
+ the 8th and 9th characters refer to permissions for a group while, and
+ the 10th and 11th characters refer to permissions for a others.
+
+ In each of these pairs the first character (6, 8, 10) is "x" if the
+ entry is executable. For a directory this means the directory is
+ allowed to be listed or "searched".
+ The second character of a pair (7, 9, 11) is "r" if the entry is allowed
+ to be read.
+*/
+
+const char *
+iso9660_get_xa_attr_str (uint16_t xa_attr)
+{
+ char *result = _getbuf();
+
+ xa_attr = uint16_from_be (xa_attr);
+
+ result[ 0] = (xa_attr & XA_ATTR_DIRECTORY) ? 'd' : '-';
+ result[ 1] = (xa_attr & XA_ATTR_CDDA) ? 'a' : '-';
+ result[ 2] = (xa_attr & XA_ATTR_INTERLEAVED) ? 'i' : '-';
+ result[ 3] = (xa_attr & XA_ATTR_MODE2FORM2) ? '2' : '-';
+ result[ 4] = (xa_attr & XA_ATTR_MODE2FORM1) ? '1' : '-';
+
+ result[ 5] = (xa_attr & XA_PERM_XUSR) ? 'x' : '-';
+ result[ 6] = (xa_attr & XA_PERM_RUSR) ? 'r' : '-';
+
+ result[ 7] = (xa_attr & XA_PERM_XGRP) ? 'x' : '-';
+ result[ 8] = (xa_attr & XA_PERM_RGRP) ? 'r' : '-';
+
+ /* Hack alert: wonder if this should be ROTH and XOTH? */
+ result[ 9] = (xa_attr & XA_PERM_XSYS) ? 'x' : '-';
+ result[10] = (xa_attr & XA_PERM_RSYS) ? 'r' : '-';
+
+ result[11] = '\0';
+
+ return result;
+}
+
+iso9660_xa_t *
+iso9660_xa_init (iso9660_xa_t *_xa, uint16_t uid, uint16_t gid, uint16_t attr,
+ uint8_t filenum)
+{
+ cdio_assert (_xa != NULL);
+
+ _xa->user_id = uint16_to_be (uid);
+ _xa->group_id = uint16_to_be (gid);
+ _xa->attributes = uint16_to_be (attr);
+
+ _xa->signature[0] = 'X';
+ _xa->signature[1] = 'A';
+
+ _xa->filenum = filenum;
+
+ _xa->reserved[0]
+ = _xa->reserved[1]
+ = _xa->reserved[2]
+ = _xa->reserved[3]
+ = _xa->reserved[4] = 0x00;
+
+ return _xa;
+}
diff --git a/contrib/libvcd/Makefile.am b/contrib/libvcd/Makefile.am
new file mode 100644
index 000000000..c922685e3
--- /dev/null
+++ b/contrib/libvcd/Makefile.am
@@ -0,0 +1,57 @@
+include $(top_srcdir)/misc/Makefile.common
+
+AM_CFLAGS = $(DEFAULT_OCFLAGS) $(VISIBILITY_FLAG)
+
+INCLUDES = -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/lib -I$(top_builddir)/lib
+
+SUBDIRS = libvcd
+
+noinst_HEADERS = \
+ vcd_assert.h \
+ data_structures.h \
+ info_private.h \
+ pbc.h \
+ stream_stdio.h \
+ bitvec.h \
+ dict.h \
+ mpeg.h \
+ salloc.h \
+ util.h \
+ bytesex_asm.h \
+ directory.h \
+ mpeg_stream.h \
+ sector_private.h \
+ vcd.h \
+ bytesex.h \
+ image_sink.h \
+ obj.h \
+ stream.h \
+ vcd_read.h
+
+noinst_LTLIBRARIES = libvcd.la libvcdinfo.la
+libvcd_la_SOURCES = \
+ vcd.c \
+ data_structures.c \
+ directory.c \
+ files.c \
+ image.c \
+ image_bincue.c \
+ image_cdrdao.c \
+ image_nrg.c \
+ logging.c \
+ mpeg.c \
+ mpeg_stream.c \
+ pbc.c \
+ salloc.c \
+ sector.c \
+ stream.c \
+ stream_stdio.c \
+ util.c
+libvcd_la_CFLAGS = $(AM_CFLAGS) $(LIBCDIO_CFLAGS)
+
+libvcdinfo_la_SOURCES = \
+ info.c \
+ inf.c \
+ info_private.c \
+ vcd_read.c
+libvcdinfo_la_CFLAGS = $(AM_CFLAGS) $(LIBCDIO_CFLAGS)
diff --git a/contrib/libvcd/bitvec.h b/contrib/libvcd/bitvec.h
new file mode 100644
index 000000000..f45059db0
--- /dev/null
+++ b/contrib/libvcd/bitvec.h
@@ -0,0 +1,131 @@
+/*
+ $Id: bitvec.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_BITVEC_H__
+#define __VCD_BITVEC_H__
+
+#include <libvcd/types.h>
+
+#include "vcd_assert.h"
+
+static inline bool
+_vcd_bit_set_p (const uint32_t n, const unsigned bit)
+{
+ return ((n >> bit) & 0x1) == 0x1;
+}
+
+static inline int
+vcd_bitvec_align (int value, const int boundary)
+{
+ if (value % boundary)
+ value += (boundary - (value % boundary));
+
+ return value;
+}
+
+/*
+ * PEEK
+ */
+
+#define vcd_bitvec_peek_bits16(bitvec, offset) \
+ vcd_bitvec_peek_bits ((bitvec), (offset), 16)
+
+static inline uint32_t
+vcd_bitvec_peek_bits (const uint8_t bitvec[],
+ const unsigned offset,
+ const unsigned bits)
+{
+ uint32_t result = 0;
+ unsigned i = offset;
+
+ vcd_assert (bits > 0 && bits <= 32);
+
+#if 0
+ j = 0;
+ while (j < bits)
+ if (i % 8 || (bits - j) < 8)
+ {
+ result <<= 1;
+ if (_vcd_bit_set_p (bitvec[i >> 3], 7 - (i % 8)))
+ result |= 0x1;
+ j++, i++;
+ }
+ else
+ {
+ result <<= 8;
+ result |= bitvec[i >> 3];
+ j += 8, i += 8;
+ }
+#else
+ if (!(offset % 8) && !(bits % 8)) /* optimization */
+ for (i = offset; i < (offset + bits); i+= 8)
+ {
+ result <<= 8;
+ result |= bitvec[i >> 3];
+ }
+ else /* general case */
+ for (i = offset; i < (offset + bits); i++)
+ {
+ result <<= 1;
+ if (_vcd_bit_set_p (bitvec[i >> 3], 7 - (i % 8)))
+ result |= 0x1;
+ }
+#endif
+
+ return result;
+}
+
+static inline uint32_t
+vcd_bitvec_peek_bits32 (const uint8_t bitvec[], unsigned offset)
+{
+ if (offset % 8)
+ return vcd_bitvec_peek_bits (bitvec, offset, 32);
+
+ offset >>= 3;
+
+ return (bitvec[offset] << 24
+ | bitvec[offset + 1] << 16
+ | bitvec[offset + 2] << 8
+ | bitvec[offset + 3]);
+}
+
+/*
+ * READ
+ */
+
+static inline uint32_t
+vcd_bitvec_read_bits (const uint8_t bitvec[], unsigned *offset, const unsigned bits)
+{
+ const unsigned i = *offset;
+
+ *offset += bits;
+
+ return vcd_bitvec_peek_bits (bitvec, i, bits);
+}
+
+static inline bool
+vcd_bitvec_read_bit (const uint8_t bitvec[], unsigned *offset)
+{
+ const unsigned i = (*offset)++;
+
+ return _vcd_bit_set_p (bitvec[i >> 3], 7 - (i % 8));
+}
+
+#endif /* __VCD_BITVEC_H__ */
diff --git a/contrib/libvcd/bytesex.h b/contrib/libvcd/bytesex.h
new file mode 100644
index 000000000..9a212693d
--- /dev/null
+++ b/contrib/libvcd/bytesex.h
@@ -0,0 +1,145 @@
+/*
+ $Id: bytesex.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_BYTESEX_H__
+#define __VCD_BYTESEX_H__
+
+#include <cdio/cdio.h>
+#include <libvcd/types.h>
+#include <libvcd/logging.h>
+
+/* Private includes */
+#include "bytesex_asm.h"
+
+/* generic byteswap routines */
+
+#define UINT16_SWAP_LE_BE_C(val) ((uint16_t) ( \
+ (((uint16_t) (val) & (uint16_t) 0x00ffU) << 8) | \
+ (((uint16_t) (val) & (uint16_t) 0xff00U) >> 8)))
+
+#define UINT32_SWAP_LE_BE_C(val) ((uint32_t) ( \
+ (((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
+ (((uint32_t) (val) & (uint32_t) 0x0000ff00U) << 8) | \
+ (((uint32_t) (val) & (uint32_t) 0x00ff0000U) >> 8) | \
+ (((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24)))
+
+#define UINT64_SWAP_LE_BE_C(val) ((uint64_t) ( \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000ff000000)) << 8) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000ff00000000)) >> 8) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56)))
+
+#ifndef UINT16_SWAP_LE_BE
+# define UINT16_SWAP_LE_BE UINT16_SWAP_LE_BE_C
+#endif
+
+#ifndef UINT32_SWAP_LE_BE
+# define UINT32_SWAP_LE_BE UINT32_SWAP_LE_BE_C
+#endif
+
+#ifndef UINT64_SWAP_LE_BE
+# define UINT64_SWAP_LE_BE UINT64_SWAP_LE_BE_C
+#endif
+
+inline static
+uint16_t uint16_swap_le_be (const uint16_t val)
+{
+ return UINT16_SWAP_LE_BE (val);
+}
+
+inline static
+uint32_t uint32_swap_le_be (const uint32_t val)
+{
+ return UINT32_SWAP_LE_BE (val);
+}
+
+inline static
+uint64_t uint64_swap_le_be (const uint64_t val)
+{
+ return UINT64_SWAP_LE_BE (val);
+}
+
+# define UINT8_TO_BE(val) ((uint8_t) (val))
+# define UINT8_TO_LE(val) ((uint8_t) (val))
+#ifdef WORDS_BIGENDIAN
+# define UINT16_TO_BE(val) ((uint16_t) (val))
+# define UINT16_TO_LE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
+
+# define UINT32_TO_BE(val) ((uint32_t) (val))
+# define UINT32_TO_LE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
+
+# define UINT64_TO_BE(val) ((uint64_t) (val))
+# define UINT64_TO_LE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
+#else
+# define UINT16_TO_BE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
+# define UINT16_TO_LE(val) ((uint16_t) (val))
+
+# define UINT32_TO_BE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
+# define UINT32_TO_LE(val) ((uint32_t) (val))
+
+# define UINT64_TO_BE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
+# define UINT64_TO_LE(val) ((uint64_t) (val))
+#endif
+
+/* symmetric conversions */
+#define UINT8_FROM_BE(val) (UINT8_TO_BE (val))
+#define UINT8_FROM_LE(val) (UINT8_TO_LE (val))
+#define UINT16_FROM_BE(val) (UINT16_TO_BE (val))
+#define UINT16_FROM_LE(val) (UINT16_TO_LE (val))
+#define UINT32_FROM_BE(val) (UINT32_TO_BE (val))
+#define UINT32_FROM_LE(val) (UINT32_TO_LE (val))
+#define UINT64_FROM_BE(val) (UINT64_TO_BE (val))
+#define UINT64_FROM_LE(val) (UINT64_TO_LE (val))
+
+/* converter function template */
+#define CVT_TO_FUNC(bits) \
+ static inline uint ## bits ## _t \
+ uint ## bits ## _to_be (uint ## bits ## _t val) \
+ { return UINT ## bits ## _TO_BE (val); } \
+ static inline uint ## bits ## _t \
+ uint ## bits ## _to_le (uint ## bits ## _t val) \
+ { return UINT ## bits ## _TO_LE (val); } \
+
+CVT_TO_FUNC(8)
+CVT_TO_FUNC(16)
+CVT_TO_FUNC(32)
+CVT_TO_FUNC(64)
+
+#undef CVT_TO_FUNC
+
+#define uint16_from_be(val) (uint16_to_be (val))
+#define uint16_from_le(val) (uint16_to_le (val))
+#define uint32_from_be(val) (uint32_to_be (val))
+#define uint32_from_le(val) (uint32_to_le (val))
+
+#endif /* __VCD_BYTESEX_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/bytesex_asm.h b/contrib/libvcd/bytesex_asm.h
new file mode 100644
index 000000000..3265c592b
--- /dev/null
+++ b/contrib/libvcd/bytesex_asm.h
@@ -0,0 +1,123 @@
+/*
+ $Id: bytesex_asm.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2001 Sven Ottemann <ac-logic@freenet.de>
+ 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_BYTESEX_ASM_H__
+#define __VCD_BYTESEX_ASM_H__
+#if !defined(DISABLE_ASM_OPTIMIZE)
+
+#include <libvcd/types.h>
+
+#if defined(__powerpc__) && defined(__GNUC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(const uint32_t a)
+{
+ uint32_t b;
+
+ __asm__ ("lwbrx %0,0,%1"
+ :"=r"(b)
+ :"r"(&a), "m"(a));
+
+ return b;
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(const uint16_t a)
+{
+ uint32_t b;
+
+ __asm__ ("lhbrx %0,0,%1"
+ :"=r"(b)
+ :"r"(&a), "m"(a));
+
+ return b;
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#elif defined(__mc68000__) && defined(__STORMGCC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(uint32_t a __asm__("d0"))
+{
+ /* __asm__("rolw #8,%0; swap %0; rolw #8,%0" : "=d" (val) : "0" (val)); */
+
+ __asm__("move.l %1,d0;rol.w #8,d0;swap d0;rol.w #8,d0;move.l d0,%0"
+ :"=r"(a)
+ :"r"(a));
+
+ return(a);
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(uint16_t a __asm__("d0"))
+{
+ __asm__("move.l %1,d0;rol.w #8,d0;move.l d0,%0"
+ :"=r"(a)
+ :"r"(a));
+
+ return(a);
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#elif 0 && defined(__i386__) && defined(__GNUC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(uint32_t a)
+{
+ __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
+ "rorl $16,%0\n\t" /* swap words */
+ "xchgb %b0,%h0" /* swap higher bytes */
+ :"=q" (a)
+ : "0" (a));
+
+ return(a);
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(uint16_t a)
+{
+ __asm__("xchgb %b0,%h0" /* swap bytes */
+ : "=q" (a)
+ : "0" (a));
+
+ return(a);
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#endif
+
+#endif /* !defined(DISABLE_ASM_OPTIMIZE) */
+#endif /* __VCD_BYTESEX_ASM_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/data_structures.c b/contrib/libvcd/data_structures.c
new file mode 100644
index 000000000..1fdca95c9
--- /dev/null
+++ b/contrib/libvcd/data_structures.c
@@ -0,0 +1,342 @@
+/*
+ $Id: data_structures.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <cdio/cdio.h>
+
+/* Public headers */
+#include <libvcd/types.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "data_structures.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: data_structures.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $";
+
+struct _CdioList
+{
+ unsigned length;
+
+ CdioListNode *begin;
+ CdioListNode *end;
+};
+
+struct _CdioListNode
+{
+ CdioList *list;
+
+ CdioListNode *next;
+
+ void *data;
+};
+
+/* impl */
+
+static bool
+_bubble_sort_iteration (CdioList *list, _cdio_list_cmp_func cmp_func)
+{
+ CdioListNode **pnode;
+ bool changed = false;
+
+ for (pnode = &(list->begin);
+ (*pnode) != NULL && (*pnode)->next != NULL;
+ pnode = &((*pnode)->next))
+ {
+ CdioListNode *node = *pnode;
+
+ if (cmp_func (node->data, node->next->data) <= 0)
+ continue; /* n <= n->next */
+
+ /* exch n n->next */
+ *pnode = node->next;
+ node->next = node->next->next;
+ (*pnode)->next = node;
+
+ changed = true;
+
+ if (node->next == NULL)
+ list->end = node;
+ }
+
+ return changed;
+}
+
+void _vcd_list_sort (CdioList *list, _cdio_list_cmp_func cmp_func)
+{
+ /* fixme -- this is bubble sort -- worst sorting algo... */
+
+ vcd_assert (list != NULL);
+ vcd_assert (cmp_func != 0);
+
+ while (_bubble_sort_iteration (list, cmp_func));
+}
+
+/* node ops */
+
+CdioListNode *
+_vcd_list_at (CdioList *list, int idx)
+{
+ CdioListNode *node = _cdio_list_begin (list);
+
+ if (idx < 0)
+ return _vcd_list_at (list, _cdio_list_length (list) + idx);
+
+ vcd_assert (idx >= 0);
+
+ while (node && idx)
+ {
+ node = _cdio_list_node_next (node);
+ idx--;
+ }
+
+ return node;
+}
+
+/*
+ * n-way tree based on list -- somewhat inefficent
+ */
+
+struct _VcdTree
+{
+ VcdTreeNode *root;
+};
+
+struct _VcdTreeNode
+{
+ void *data;
+
+ CdioListNode *listnode;
+ VcdTree *tree;
+ VcdTreeNode *parent;
+ CdioList *children;
+};
+
+VcdTree *
+_vcd_tree_new (void *root_data)
+{
+ VcdTree *new_tree;
+
+ new_tree = _vcd_malloc (sizeof (VcdTree));
+
+ new_tree->root = _vcd_malloc (sizeof (VcdTreeNode));
+
+ new_tree->root->data = root_data;
+ new_tree->root->tree = new_tree;
+ new_tree->root->parent = NULL;
+ new_tree->root->children = NULL;
+ new_tree->root->listnode = NULL;
+
+ return new_tree;
+}
+
+void
+_vcd_tree_destroy (VcdTree *tree, bool free_data)
+{
+ _vcd_tree_node_destroy (tree->root, free_data);
+
+ free (tree->root);
+ free (tree);
+}
+
+void
+_vcd_tree_node_destroy (VcdTreeNode *node, bool free_data)
+{
+ VcdTreeNode *child, *nxt_child;
+
+ vcd_assert (node != NULL);
+
+ child = _vcd_tree_node_first_child (node);
+ while(child) {
+ nxt_child = _vcd_tree_node_next_sibling (child);
+ _vcd_tree_node_destroy (child, free_data);
+ child = nxt_child;
+ }
+
+ if (node->children)
+ {
+ vcd_assert (_cdio_list_length (node->children) == 0);
+ _cdio_list_free (node->children, true);
+ node->children = NULL;
+ }
+
+ if (free_data)
+ free (_vcd_tree_node_set_data (node, NULL));
+
+ if (node->parent)
+ _cdio_list_node_free (node->listnode, true);
+ else
+ _vcd_tree_node_set_data (node, NULL);
+}
+
+VcdTreeNode *
+_vcd_tree_root (VcdTree *tree)
+{
+ return tree->root;
+}
+
+void *
+_vcd_tree_node_data (VcdTreeNode *node)
+{
+ return node->data;
+}
+
+void *
+_vcd_tree_node_set_data (VcdTreeNode *node, void *new_data)
+{
+ void *old_data = node->data;
+
+ node->data = new_data;
+
+ return old_data;
+}
+
+VcdTreeNode *
+_vcd_tree_node_append_child (VcdTreeNode *pnode, void *cdata)
+{
+ VcdTreeNode *nnode;
+
+ vcd_assert (pnode != NULL);
+
+ if (!pnode->children)
+ pnode->children = _cdio_list_new ();
+
+ nnode = _vcd_malloc (sizeof (VcdTreeNode));
+
+ _cdio_list_append (pnode->children, nnode);
+
+ nnode->data = cdata;
+ nnode->parent = pnode;
+ nnode->tree = pnode->tree;
+ nnode->listnode = _cdio_list_end (pnode->children);
+
+ return nnode;
+}
+
+VcdTreeNode *
+_vcd_tree_node_first_child (VcdTreeNode *node)
+{
+ vcd_assert (node != NULL);
+
+ if (!node->children)
+ return NULL;
+
+ return _cdio_list_node_data (_cdio_list_begin (node->children));
+}
+
+VcdTreeNode *
+_vcd_tree_node_next_sibling (VcdTreeNode *node)
+{
+ vcd_assert (node != NULL);
+
+ return _cdio_list_node_data (_cdio_list_node_next (node->listnode));
+}
+
+void
+_vcd_tree_node_sort_children (VcdTreeNode *node, _vcd_tree_node_cmp_func cmp_func)
+{
+ vcd_assert (node != NULL);
+
+ if (node->children)
+ _vcd_list_sort (node->children, (_cdio_list_cmp_func) cmp_func);
+}
+
+void
+_vcd_tree_node_traverse (VcdTreeNode *node,
+ _vcd_tree_node_traversal_func trav_func,
+ void *user_data) /* pre-order */
+{
+ VcdTreeNode *child;
+
+ vcd_assert (node != NULL);
+
+ trav_func (node, user_data);
+
+ _VCD_CHILD_FOREACH (child, node)
+ {
+ _vcd_tree_node_traverse (child, trav_func, user_data);
+ }
+}
+
+void
+_vcd_tree_node_traverse_bf (VcdTreeNode *node,
+ _vcd_tree_node_traversal_func trav_func,
+ void *user_data) /* breath-first */
+{
+ CdioList *queue;
+
+ vcd_assert (node != NULL);
+
+ queue = _cdio_list_new ();
+
+ _cdio_list_prepend (queue, node);
+
+ while (_cdio_list_length (queue))
+ {
+ CdioListNode *lastnode = _cdio_list_end (queue);
+ VcdTreeNode *treenode = _cdio_list_node_data (lastnode);
+ VcdTreeNode *childnode;
+
+ _cdio_list_node_free (lastnode, false);
+
+ trav_func (treenode, user_data);
+
+ _VCD_CHILD_FOREACH (childnode, treenode)
+ {
+ _cdio_list_prepend (queue, childnode);
+ }
+ }
+
+ _cdio_list_free (queue, false);
+}
+
+VcdTreeNode *_vcd_tree_node_parent (VcdTreeNode *node)
+{
+ return node->parent;
+}
+
+VcdTreeNode *_vcd_tree_node_root (VcdTreeNode *node)
+{
+ return node->tree->root;
+}
+
+bool _vcd_tree_node_is_root (VcdTreeNode *node)
+{
+ return (node->parent == NULL);
+}
+
+/* eof */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
+
diff --git a/contrib/libvcd/data_structures.h b/contrib/libvcd/data_structures.h
new file mode 100644
index 000000000..70fe88045
--- /dev/null
+++ b/contrib/libvcd/data_structures.h
@@ -0,0 +1,92 @@
+/*
+ $Id: data_structures.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_DATA_STRUCTURES_H__
+#define __VCD_DATA_STRUCTURES_H__
+
+#include <cdio/ds.h>
+#include <libvcd/types.h>
+
+/* node ops */
+
+CdioListNode *_vcd_list_at (CdioList *list, int idx);
+
+void _vcd_list_sort (CdioList *list, _cdio_list_cmp_func cmp_func);
+
+/* n-way tree */
+
+typedef struct _VcdTree VcdTree;
+typedef struct _VcdTreeNode VcdTreeNode;
+
+#define _VCD_CHILD_FOREACH(child, parent) \
+ for (child = _vcd_tree_node_first_child (parent); child; child = _vcd_tree_node_next_sibling (child))
+
+typedef int (*_vcd_tree_node_cmp_func) (VcdTreeNode *node1,
+ VcdTreeNode *node2);
+
+typedef void (*_vcd_tree_node_traversal_func) (VcdTreeNode *node,
+ void *user_data);
+
+VcdTree *_vcd_tree_new (void *root_data);
+
+void _vcd_tree_destroy (VcdTree *tree, bool free_data);
+
+VcdTreeNode *_vcd_tree_root (VcdTree *tree);
+
+void _vcd_tree_node_sort_children (VcdTreeNode *node,
+ _vcd_tree_node_cmp_func cmp_func);
+
+void *_vcd_tree_node_data (VcdTreeNode *node);
+
+void _vcd_tree_node_destroy (VcdTreeNode *node, bool free_data);
+
+void *_vcd_tree_node_set_data (VcdTreeNode *node, void *new_data);
+
+VcdTreeNode *_vcd_tree_node_append_child (VcdTreeNode *pnode, void *cdata);
+
+VcdTreeNode *_vcd_tree_node_first_child (VcdTreeNode *node);
+
+VcdTreeNode *_vcd_tree_node_next_sibling (VcdTreeNode *node);
+
+VcdTreeNode *_vcd_tree_node_parent (VcdTreeNode *node);
+
+VcdTreeNode *_vcd_tree_node_root (VcdTreeNode *node);
+
+bool _vcd_tree_node_is_root (VcdTreeNode *node);
+
+void _vcd_tree_node_traverse (VcdTreeNode *node,
+ _vcd_tree_node_traversal_func trav_func,
+ void *user_data);
+
+void
+_vcd_tree_node_traverse_bf (VcdTreeNode *node,
+ _vcd_tree_node_traversal_func trav_func,
+ void *user_data);
+
+#endif /* __VCD_DATA_STRUCTURES_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
+
diff --git a/contrib/libvcd/dict.h b/contrib/libvcd/dict.h
new file mode 100644
index 000000000..229ad5706
--- /dev/null
+++ b/contrib/libvcd/dict.h
@@ -0,0 +1,165 @@
+/*
+ $Id: dict.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_DICT_H__
+#define __VCD_DICT_H__
+
+/* Public headers */
+#include <libvcd/types.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "obj.h"
+#include "util.h"
+
+struct _dict_t
+{
+ char *key;
+ uint32_t sector;
+ uint32_t length;
+ void *buf;
+ uint8_t flags;
+};
+
+static void
+_dict_insert (VcdObj *obj, const char key[], uint32_t sector, uint32_t length,
+ uint8_t end_flags)
+{
+ struct _dict_t *_new_node;
+
+ vcd_assert (key != NULL);
+ vcd_assert (length > 0);
+
+ if ((sector =_vcd_salloc (obj->iso_bitmap, sector, length)) == SECTOR_NIL)
+ vcd_assert_not_reached ();
+
+ _new_node = _vcd_malloc (sizeof (struct _dict_t));
+
+ _new_node->key = strdup (key);
+ _new_node->sector = sector;
+ _new_node->length = length;
+ _new_node->buf = _vcd_malloc (length * ISO_BLOCKSIZE);
+ _new_node->flags = end_flags;
+
+ _cdio_list_prepend (obj->buffer_dict_list, _new_node);
+}
+
+static
+int _dict_key_cmp (struct _dict_t *a, char *b)
+{
+ vcd_assert (a != NULL);
+ vcd_assert (b != NULL);
+
+ return !strcmp (a->key, b);
+}
+
+static
+int _dict_sector_cmp (struct _dict_t *a, uint32_t *b)
+{
+ vcd_assert (a != NULL);
+ vcd_assert (b != NULL);
+
+ return (a->sector <= *b && (*b - a->sector) < a->length);
+}
+
+static const struct _dict_t *
+_dict_get_bykey (VcdObj *obj, const char key[])
+{
+ CdioListNode *node;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (key != NULL);
+
+ node = _cdio_list_find (obj->buffer_dict_list,
+ (_cdio_list_iterfunc) _dict_key_cmp,
+ (char *) key);
+
+ if (node)
+ return _cdio_list_node_data (node);
+
+ return NULL;
+}
+
+static const struct _dict_t *
+_dict_get_bysector (VcdObj *obj, uint32_t sector)
+{
+ CdioListNode *node;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (sector != SECTOR_NIL);
+
+ node = _cdio_list_find (obj->buffer_dict_list,
+ (_cdio_list_iterfunc) _dict_sector_cmp,
+ &sector);
+
+ if (node)
+ return _cdio_list_node_data (node);
+
+ return NULL;
+}
+
+static uint8_t
+_dict_get_sector_flags (VcdObj *obj, uint32_t sector)
+{
+ const struct _dict_t *p;
+
+ vcd_assert (sector != SECTOR_NIL);
+
+ p = _dict_get_bysector (obj, sector);
+
+ if (p)
+ return (((sector - p->sector)+1 == p->length)
+ ? p->flags : 0);
+
+ return 0;
+}
+
+static void *
+_dict_get_sector (VcdObj *obj, uint32_t sector)
+{
+ const struct _dict_t *p;
+
+ vcd_assert (sector != SECTOR_NIL);
+
+ p = _dict_get_bysector (obj, sector);
+
+ if (p)
+ return ((char *)p->buf) + ((sector - p->sector) * ISO_BLOCKSIZE);
+
+ return NULL;
+}
+
+static void
+_dict_clean (VcdObj *obj)
+{
+ CdioListNode *node;
+
+ while ((node = _cdio_list_begin (obj->buffer_dict_list)))
+ {
+ struct _dict_t *p = _cdio_list_node_data (node);
+
+ free (p->key);
+ free (p->buf);
+
+ _cdio_list_node_free (node, true);
+ }
+}
+
+#endif /* __VCD_DICT_H__ */
diff --git a/contrib/libvcd/directory.c b/contrib/libvcd/directory.c
new file mode 100644
index 000000000..992d728c6
--- /dev/null
+++ b/contrib/libvcd/directory.c
@@ -0,0 +1,507 @@
+/*
+ $Id: directory.c,v 1.4 2006/09/26 19:26:57 dgp85 Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+/* Public headers */
+#include <cdio/bytesex.h>
+#include <cdio/iso9660.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "directory.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: directory.c,v 1.4 2006/09/26 19:26:57 dgp85 Exp $";
+
+/* CD-ROM XA */
+
+/* tree data structure */
+
+typedef struct
+{
+ bool is_dir;
+ char *name;
+ uint16_t version;
+ uint16_t xa_attributes;
+ uint8_t xa_filenum;
+ uint32_t extent;
+ uint32_t size;
+ unsigned pt_id;
+} data_t;
+
+typedef VcdTreeNode VcdDirNode;
+
+#define EXTENT(anode) (DATAP(anode)->extent)
+#define SIZE(anode) (DATAP(anode)->size)
+#define PT_ID(anode) (DATAP(anode)->pt_id)
+
+#define DATAP(anode) ((data_t*) _vcd_tree_node_data (anode))
+
+/* important date to celebrate (for me at least =)
+ -- until user customization is implemented... */
+static const time_t _vcd_time = 269222400L;
+
+/* implementation */
+
+static void
+traverse_get_dirsizes(VcdDirNode *node, void *data)
+{
+ data_t *d = DATAP(node);
+ unsigned *sum = data;
+
+ if (d->is_dir)
+ {
+ vcd_assert (d->size % ISO_BLOCKSIZE == 0);
+
+ *sum += (d->size / ISO_BLOCKSIZE);
+ }
+}
+
+static unsigned
+get_dirsizes (VcdDirNode* dirnode)
+{
+ unsigned result = 0;
+
+ _vcd_tree_node_traverse (dirnode, traverse_get_dirsizes, &result);
+
+ return result;
+}
+
+static void
+traverse_update_dirextents (VcdDirNode *dirnode, void *data)
+{
+ data_t *d = DATAP(dirnode);
+
+ if (d->is_dir)
+ {
+ VcdDirNode* child;
+ unsigned dirextent = d->extent;
+
+ vcd_assert (d->size % ISO_BLOCKSIZE == 0);
+
+ dirextent += d->size / ISO_BLOCKSIZE;
+
+ _VCD_CHILD_FOREACH (child, dirnode)
+ {
+ data_t *d = DATAP(child);
+
+ vcd_assert (d != NULL);
+
+ if (d->is_dir)
+ {
+ d->extent = dirextent;
+ dirextent += get_dirsizes (child);
+ }
+ }
+ }
+}
+
+static void
+update_dirextents (VcdDirectory *dir, uint32_t extent)
+{
+ data_t *dirdata = DATAP(_vcd_tree_root (dir));
+
+ dirdata->extent = extent;
+ _vcd_tree_node_traverse (_vcd_tree_root (dir),
+ traverse_update_dirextents, NULL);
+}
+
+static void
+traverse_update_sizes(VcdDirNode *node, void *data)
+{
+ data_t *dirdata = DATAP(node);
+
+ if (dirdata->is_dir)
+ {
+ VcdDirNode* child;
+ unsigned offset = 0;
+
+ offset += iso9660_dir_calc_record_size (1, sizeof(iso9660_xa_t)); /* '.' */
+ offset += iso9660_dir_calc_record_size (1, sizeof(iso9660_xa_t)); /* '..' */
+
+ _VCD_CHILD_FOREACH (child, node)
+ {
+ data_t *d = DATAP(child);
+ unsigned reclen;
+ char *pathname = (d->is_dir
+ ? strdup (d->name)
+ : iso9660_pathname_isofy (d->name, d->version));
+
+ vcd_assert (d != NULL);
+
+ reclen = iso9660_dir_calc_record_size (strlen (pathname),
+ sizeof (iso9660_xa_t));
+
+ free (pathname);
+
+ offset = _vcd_ofs_add (offset, reclen, ISO_BLOCKSIZE);
+ }
+
+ vcd_assert (offset > 0);
+
+ dirdata->size = _vcd_ceil2block (offset, ISO_BLOCKSIZE);
+ }
+}
+
+static void
+update_sizes (VcdDirectory *dir)
+{
+ _vcd_tree_node_traverse (_vcd_tree_root(dir), traverse_update_sizes, NULL);
+}
+
+
+/* exported stuff: */
+
+VcdDirectory *
+_vcd_directory_new (void)
+{
+ data_t *data;
+ VcdDirectory *dir = NULL;
+
+ vcd_assert (sizeof(iso9660_xa_t) == 14);
+
+ data = _vcd_malloc (sizeof (data_t));
+ dir = _vcd_tree_new (data);
+
+ data->is_dir = true;
+ data->name = _vcd_memdup("\0", 2);
+ data->xa_attributes = XA_FORM1_DIR;
+ data->xa_filenum = 0x00;
+
+ return dir;
+}
+
+static void
+traverse_vcd_directory_done (VcdDirNode *node, void *data)
+{
+ data_t *dirdata = DATAP (node);
+
+ free (dirdata->name);
+}
+
+void
+_vcd_directory_destroy (VcdDirectory *dir)
+{
+ vcd_assert (dir != NULL);
+
+ _vcd_tree_node_traverse (_vcd_tree_root (dir),
+ traverse_vcd_directory_done, NULL);
+
+ _vcd_tree_destroy (dir, true);
+}
+
+static VcdDirNode*
+lookup_child (VcdDirNode* node, const char name[])
+{
+ VcdDirNode* child;
+
+ _VCD_CHILD_FOREACH (child, node)
+ {
+ data_t *d = DATAP(child);
+
+ if (!strcmp (d->name, name))
+ return child;
+ }
+
+ return child; /* NULL */
+}
+
+static int
+_iso_dir_cmp (VcdDirNode *node1, VcdDirNode *node2)
+{
+ data_t *d1 = DATAP(node1);
+ data_t *d2 = DATAP(node2);
+ int result = 0;
+
+ result = strcmp (d1->name, d2->name);
+
+ return result;
+}
+
+int
+_vcd_directory_mkdir (VcdDirectory *dir, const char pathname[])
+{
+ char **splitpath;
+ unsigned level, n;
+ VcdDirNode* pdir = _vcd_tree_root (dir);
+
+ vcd_assert (dir != NULL);
+ vcd_assert (pathname != NULL);
+
+ splitpath = _vcd_strsplit (pathname, '/');
+
+ level = _vcd_strlenv (splitpath);
+
+ for (n = 0; n < level-1; n++)
+ if (!(pdir = lookup_child(pdir, splitpath[n])))
+ {
+ vcd_error("mkdir: parent dir `%s' (level=%d) for `%s' missing!",
+ splitpath[n], n, pathname);
+ vcd_assert_not_reached ();
+ }
+
+ if (lookup_child (pdir, splitpath[level-1]))
+ {
+ vcd_error ("mkdir: `%s' already exists", pathname);
+ vcd_assert_not_reached ();
+ }
+
+ {
+ data_t *data = _vcd_malloc (sizeof (data_t));
+
+ _vcd_tree_node_append_child (pdir, data);
+
+ data->is_dir = true;
+ data->name = strdup(splitpath[level-1]);
+ data->xa_attributes = XA_FORM1_DIR;
+ data->xa_filenum = 0x00;
+ /* .. */
+ }
+
+ _vcd_tree_node_sort_children (pdir, _iso_dir_cmp);
+
+ _vcd_strfreev (splitpath);
+
+ return 0;
+}
+
+int
+_vcd_directory_mkfile (VcdDirectory *dir, const char pathname[],
+ uint32_t start, uint32_t size,
+ bool form2_flag, uint8_t filenum)
+{
+ char **splitpath;
+ unsigned level, n;
+ const int file_version = 1;
+
+ VcdDirNode* pdir = NULL;
+
+ vcd_assert (dir != NULL);
+ vcd_assert (pathname != NULL);
+
+ splitpath = _vcd_strsplit (pathname, '/');
+
+ level = _vcd_strlenv (splitpath);
+
+ while (!pdir)
+ {
+ pdir = _vcd_tree_root (dir);
+
+ for (n = 0; n < level-1; n++)
+ if (!(pdir = lookup_child (pdir, splitpath[n])))
+ {
+ char *newdir = _vcd_strjoin (splitpath, n+1, "/");
+
+ vcd_info ("autocreating directory `%s' for file `%s'",
+ newdir, pathname);
+
+ _vcd_directory_mkdir (dir, newdir);
+
+ free (newdir);
+
+ vcd_assert (pdir == NULL);
+
+ break;
+ }
+ else if (!DATAP(pdir)->is_dir)
+ {
+ char *newdir = _vcd_strjoin (splitpath, n+1, "/");
+
+ vcd_error ("mkfile: `%s' not a directory", newdir);
+
+ free (newdir);
+
+ return -1;
+ }
+
+ }
+
+ if (lookup_child (pdir, splitpath[level-1]))
+ {
+ vcd_error ("mkfile: `%s' already exists", pathname);
+ _vcd_strfreev(splitpath);
+ return -1;
+ }
+
+ {
+ data_t *data = _vcd_malloc (sizeof (data_t));
+
+ _vcd_tree_node_append_child (pdir, data);
+
+ data->is_dir = false;
+ data->name = strdup (splitpath[level-1]);
+ data->version = file_version;
+ data->xa_attributes = form2_flag ? XA_FORM2_FILE : XA_FORM1_FILE;
+ data->xa_filenum = filenum;
+ data->size = size;
+ data->extent = start;
+ /* .. */
+ }
+
+ _vcd_tree_node_sort_children (pdir, _iso_dir_cmp);
+
+ _vcd_strfreev (splitpath);
+
+ return 0;
+}
+
+uint32_t
+_vcd_directory_get_size (VcdDirectory *dir)
+{
+ vcd_assert (dir != NULL);
+
+ update_sizes (dir);
+ return get_dirsizes (_vcd_tree_root (dir));
+}
+
+static void
+traverse_vcd_directory_dump_entries (VcdDirNode *node, void *data)
+{
+ data_t *d = DATAP(node);
+ iso9660_xa_t xa_su;
+
+ uint32_t root_extent = EXTENT(_vcd_tree_node_root (node));
+
+ uint32_t parent_extent =
+ (!_vcd_tree_node_is_root (node))
+ ? EXTENT(_vcd_tree_node_parent (node))
+ : EXTENT(node);
+
+ uint32_t parent_size =
+ (!_vcd_tree_node_is_root (node))
+ ? SIZE(_vcd_tree_node_parent (node))
+ : SIZE(node);
+
+ void *dirbufp = (char*) data + ISO_BLOCKSIZE * (parent_extent - root_extent);
+
+ iso9660_xa_init (&xa_su, 0, 0, d->xa_attributes, d->xa_filenum);
+
+ if (!_vcd_tree_node_is_root (node))
+ {
+ char *pathname = (d->is_dir
+ ? strdup (d->name)
+ : iso9660_pathname_isofy (d->name, d->version));
+
+ iso9660_dir_add_entry_su (dirbufp, pathname, d->extent, d->size,
+ d->is_dir ? ISO_DIRECTORY : ISO_FILE,
+ &xa_su, sizeof (xa_su),
+ &_vcd_time);
+
+ free (pathname);
+ }
+
+ /* if this is a directory, create the new directory node */
+ if (d->is_dir)
+ {
+ dirbufp = (char*)data + ISO_BLOCKSIZE * (d->extent - root_extent);
+
+ iso9660_dir_init_new_su (dirbufp,
+ d->extent, d->size, &xa_su, sizeof (xa_su),
+ parent_extent, parent_size, &xa_su,
+ sizeof (xa_su), &_vcd_time);
+ }
+}
+
+void
+_vcd_directory_dump_entries (VcdDirectory *dir, void *buf, uint32_t extent)
+{
+ vcd_assert (dir != NULL);
+
+ update_sizes (dir); /* better call it one time more than one less */
+ update_dirextents (dir, extent);
+
+ _vcd_tree_node_traverse (_vcd_tree_root (dir),
+ traverse_vcd_directory_dump_entries, buf);
+}
+
+typedef struct
+{
+ void *ptl;
+ void *ptm;
+} _vcd_directory_dump_pathtables_t;
+
+static void
+_dump_pathtables_helper (_vcd_directory_dump_pathtables_t *args,
+ data_t *d, uint16_t parent_id)
+{
+ uint16_t id_l, id_m;
+
+ vcd_assert (args != NULL);
+ vcd_assert (d != NULL);
+
+ vcd_assert (d->is_dir);
+
+ id_l = iso9660_pathtable_l_add_entry (args->ptl, d->name, d->extent,
+ parent_id);
+
+ id_m = iso9660_pathtable_m_add_entry (args->ptm, d->name, d->extent,
+ parent_id);
+
+ vcd_assert (id_l == id_m);
+
+ d->pt_id = id_m;
+}
+
+static void
+traverse_vcd_directory_dump_pathtables (VcdDirNode *node, void *data)
+{
+ _vcd_directory_dump_pathtables_t *args = data;
+
+ if (DATAP (node)->is_dir)
+ {
+ VcdDirNode* parent = _vcd_tree_node_parent (node);
+ uint16_t parent_id = parent ? PT_ID (parent) : 1;
+
+ _dump_pathtables_helper (args, DATAP (node), parent_id);
+ }
+}
+
+void
+_vcd_directory_dump_pathtables (VcdDirectory *dir, void *ptl, void *ptm)
+{
+ _vcd_directory_dump_pathtables_t args;
+
+ vcd_assert (dir != NULL);
+
+ iso9660_pathtable_init (ptl);
+ iso9660_pathtable_init (ptm);
+
+ args.ptl = ptl;
+ args.ptm = ptm;
+
+ _vcd_tree_node_traverse_bf (_vcd_tree_root (dir),
+ traverse_vcd_directory_dump_pathtables, &args);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/directory.h b/contrib/libvcd/directory.h
new file mode 100644
index 000000000..c4cba9efc
--- /dev/null
+++ b/contrib/libvcd/directory.h
@@ -0,0 +1,64 @@
+/*
+ $Id: directory.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef _DIRECTORY_H_
+#define _DIRECTORY_H_
+
+#include <libvcd/types.h>
+
+/* Private headers */
+#include "data_structures.h"
+
+/* opaque data structure representing the ISO directory tree */
+typedef VcdTree VcdDirectory;
+
+VcdDirectory *
+_vcd_directory_new (void);
+
+void
+_vcd_directory_destroy (VcdDirectory *dir);
+
+int
+_vcd_directory_mkdir (VcdDirectory *dir, const char pathname[]);
+
+int
+_vcd_directory_mkfile (VcdDirectory *dir, const char pathname[],
+ uint32_t start, uint32_t size,
+ bool form2_flag, uint8_t filenum);
+
+uint32_t
+_vcd_directory_get_size (VcdDirectory *dir);
+
+void
+_vcd_directory_dump_entries (VcdDirectory *dir, void *buf, uint32_t extent);
+
+void
+_vcd_directory_dump_pathtables (VcdDirectory *dir, void *ptl, void *ptm);
+
+#endif /* _DIRECTORY_H_ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/files.c b/contrib/libvcd/files.c
new file mode 100644
index 000000000..38780edf0
--- /dev/null
+++ b/contrib/libvcd/files.c
@@ -0,0 +1,1020 @@
+/*
+ $Id: files.c,v 1.4 2006/09/26 21:18:18 dgp85 Exp $
+
+ Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <math.h>
+
+#include <cdio/cdio.h>
+#include <cdio/bytesex.h>
+#include <cdio/util.h>
+
+/* Public headers */
+#include <libvcd/files.h>
+#include <libvcd/types.h>
+#include <libvcd/logging.h>
+
+/* FIXME! Make this local */
+#include <libvcd/files_private.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "mpeg_stream.h"
+#include "obj.h"
+#include "pbc.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: files.c,v 1.4 2006/09/26 21:18:18 dgp85 Exp $";
+
+inline static bool
+_pal_p (const struct vcd_mpeg_stream_vid_info *_info)
+{
+ return (_info->vsize == 288 || _info->vsize == 576);
+}
+
+static int
+_derive_vid_type (const struct vcd_mpeg_stream_info *_info, bool svcd)
+{
+ if (_info->shdr[0].seen)
+ return _pal_p (&_info->shdr[0]) ? 0x7 : 0x3;
+
+ if (_info->shdr[2].seen)
+ {
+ if (svcd)
+ vcd_warn ("stream with 0xE2 still stream id not allowed for IEC62107 compliant SVCDs");
+ return _pal_p (&_info->shdr[2]) ? 0x6 : 0x2;
+ }
+
+ if (_info->shdr[1].seen)
+ return _pal_p (&_info->shdr[1]) ? 0x5 : 0x1;
+
+ return 0;
+}
+
+static int
+_derive_ogt_type (const struct vcd_mpeg_stream_info *_info, bool svcd)
+{
+
+ if (!svcd)
+ return 0;
+
+ if ((_info->ogt[3] || _info->ogt[2])
+ && _info->ogt[1] && _info->ogt[0])
+ return 0x3;
+
+ if (_info->ogt[1] && _info->ogt[0])
+ return 0x2;
+
+ if (_info->ogt[0])
+ return 0x1;
+
+ vcd_debug ("OGT streams available: %d %d %d %d",
+ _info->ogt[0], _info->ogt[1],
+ _info->ogt[2], _info->ogt[3]);
+
+ return 0x0;
+}
+
+static int
+_derive_aud_type (const struct vcd_mpeg_stream_info *_info, bool svcd)
+{
+ if (!_info->ahdr[0].seen)
+ return 0; /* no MPEG audio */
+
+ if (svcd)
+ {
+ if (_info->ahdr[2].seen)
+ return 3; /* MC */
+
+ if (_info->ahdr[1].seen)
+ return 2; /* 2 streams */
+
+ return 1; /* just one stream */
+ }
+ else
+ switch (_info->ahdr[0].mode)
+ {
+ case MPEG_SINGLE_CHANNEL:
+ return 1;
+ break;
+
+ case MPEG_STEREO:
+ case MPEG_JOINT_STEREO:
+ return 2;
+ break;
+
+ case MPEG_DUAL_CHANNEL:
+ return 3;
+ break;
+ }
+
+ return 0;
+}
+
+void
+set_entries_vcd (VcdObj *obj, void *buf)
+{
+ CdioListNode *node = NULL;
+ int idx = 0;
+ int track_idx = 0;
+ EntriesVcd_t entries_vcd;
+
+ vcd_assert (sizeof(EntriesVcd_t) == 2048);
+
+ vcd_assert (_cdio_list_length (obj->mpeg_track_list) <= MAX_ENTRIES);
+ vcd_assert (_cdio_list_length (obj->mpeg_track_list) > 0);
+
+ memset(&entries_vcd, 0, sizeof(entries_vcd)); /* paranoia / fixme */
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ strncpy(entries_vcd.ID, ENTRIES_ID_VCD, 8);
+ entries_vcd.version = ENTRIES_VERSION_VCD;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_VCD;
+ break;
+
+ case VCD_TYPE_VCD11:
+ strncpy(entries_vcd.ID, ENTRIES_ID_VCD, 8);
+ entries_vcd.version = ENTRIES_VERSION_VCD11;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_VCD11;
+ break;
+
+ case VCD_TYPE_VCD2:
+ strncpy(entries_vcd.ID, ENTRIES_ID_VCD, 8);
+ entries_vcd.version = ENTRIES_VERSION_VCD2;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_VCD2;
+ break;
+
+ case VCD_TYPE_SVCD:
+ if (!obj->svcd_vcd3_entrysvd)
+ strncpy(entries_vcd.ID, ENTRIES_ID_SVCD, 8);
+ else
+ {
+ vcd_warn ("setting ENTRYSVD signature for *DEPRECATED* VCD 3.0 type SVCD");
+ strncpy(entries_vcd.ID, ENTRIES_ID_VCD3, 8);
+ }
+ entries_vcd.version = ENTRIES_VERSION_SVCD;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_SVCD;
+ break;
+
+ case VCD_TYPE_HQVCD:
+ strncpy(entries_vcd.ID, ENTRIES_ID_SVCD, 8);
+ entries_vcd.version = ENTRIES_VERSION_HQVCD;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_HQVCD;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ idx = 0;
+ track_idx = 2;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *track = _cdio_list_node_data (node);
+ uint32_t lsect = track->relative_start_extent;
+ CdioListNode *node2;
+
+ lsect += obj->iso_size;
+
+ entries_vcd.entry[idx].n = cdio_to_bcd8(track_idx);
+ cdio_lba_to_msf(cdio_lsn_to_lba(lsect),
+ &(entries_vcd.entry[idx].msf));
+
+ idx++;
+ lsect += obj->track_front_margin;
+
+ _CDIO_LIST_FOREACH (node2, track->entry_list)
+ {
+ entry_t *_entry = _cdio_list_node_data (node2);
+ /* additional entries */
+
+ vcd_assert (idx < MAX_ENTRIES);
+
+ entries_vcd.entry[idx].n = cdio_to_bcd8(track_idx);
+ cdio_lba_to_msf(lsect + cdio_lsn_to_lba(_entry->aps.packet_no),
+ &(entries_vcd.entry[idx].msf));
+
+ idx++;
+ }
+
+ track_idx++;
+ }
+
+ entries_vcd.entry_count = uint16_to_be (idx);
+
+ memcpy(buf, &entries_vcd, sizeof(entries_vcd));
+}
+
+static void
+_set_bit (uint8_t bitset[], unsigned bitnum)
+{
+ unsigned _byte = bitnum / 8;
+ unsigned _bit = bitnum % 8;
+
+ bitset[_byte] |= (1 << _bit);
+}
+
+uint32_t
+get_psd_size (VcdObj *obj, bool extended)
+{
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ if (!_vcd_pbc_available (obj))
+ return 0;
+
+ if (extended)
+ return obj->psdx_size;
+
+ return obj->psd_size;
+}
+
+void
+set_psd_vcd (VcdObj *obj, void *buf, bool extended)
+{
+ CdioListNode *node;
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ vcd_assert (_vcd_pbc_available (obj));
+
+ _CDIO_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _cdio_list_node_data (node);
+ char *_buf = buf;
+ unsigned offset = (extended ? _pbc->offset_ext : _pbc->offset);
+
+ vcd_assert (offset % INFO_OFFSET_MULT == 0);
+
+ _vcd_pbc_node_write (obj, _pbc, _buf + offset, extended);
+ }
+}
+
+void
+set_lot_vcd(VcdObj *obj, void *buf, bool extended)
+{
+ LotVcd_t *lot_vcd = NULL;
+ CdioListNode *node;
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ vcd_assert (_vcd_pbc_available (obj));
+
+ lot_vcd = _vcd_malloc (sizeof (LotVcd_t));
+ memset(lot_vcd, 0xff, sizeof(LotVcd_t));
+
+ lot_vcd->reserved = 0x0000;
+
+ _CDIO_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _cdio_list_node_data (node);
+ unsigned int offset = extended ? _pbc->offset_ext : _pbc->offset;
+
+ vcd_assert (offset % INFO_OFFSET_MULT == 0);
+
+ if (_pbc->rejected)
+ continue;
+
+ offset /= INFO_OFFSET_MULT;
+
+ lot_vcd->offset[_pbc->lid - 1] = uint16_to_be (offset);
+ }
+
+ memcpy(buf, lot_vcd, sizeof(LotVcd_t));
+ free(lot_vcd);
+}
+
+void
+set_info_vcd(VcdObj *obj, void *buf)
+{
+ InfoVcd_t info_vcd;
+ CdioListNode *node = NULL;
+ int n = 0;
+
+ vcd_assert (sizeof (InfoVcd_t) == 2048);
+ vcd_assert (_cdio_list_length (obj->mpeg_track_list) <= 98);
+
+ memset (&info_vcd, 0, sizeof (info_vcd));
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ strncpy (info_vcd.ID, INFO_ID_VCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_VCD;
+ info_vcd.sys_prof_tag = INFO_SPTAG_VCD;
+ break;
+
+ case VCD_TYPE_VCD11:
+ strncpy (info_vcd.ID, INFO_ID_VCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_VCD11;
+ info_vcd.sys_prof_tag = INFO_SPTAG_VCD11;
+ break;
+
+ case VCD_TYPE_VCD2:
+ strncpy (info_vcd.ID, INFO_ID_VCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_VCD2;
+ info_vcd.sys_prof_tag = INFO_SPTAG_VCD2;
+ break;
+
+ case VCD_TYPE_SVCD:
+ strncpy (info_vcd.ID, INFO_ID_SVCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_SVCD;
+ info_vcd.sys_prof_tag = INFO_SPTAG_SVCD;
+ break;
+
+ case VCD_TYPE_HQVCD:
+ strncpy (info_vcd.ID, INFO_ID_HQVCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_HQVCD;
+ info_vcd.sys_prof_tag = INFO_SPTAG_HQVCD;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ iso9660_strncpy_pad (info_vcd.album_desc,
+ obj->info_album_id,
+ sizeof(info_vcd.album_desc), ISO9660_DCHARS);
+ /* fixme, maybe it's VCD_ACHARS? */
+
+ info_vcd.vol_count = uint16_to_be (obj->info_volume_count);
+ info_vcd.vol_id = uint16_to_be (obj->info_volume_number);
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PAL_BITS))
+ {
+ /* NTSC/PAL bitset */
+
+ n = 0;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _cdio_list_node_data (node);
+
+ const struct vcd_mpeg_stream_vid_info *_info = &track->info->shdr[0];
+
+ if (vcd_mpeg_get_norm (_info) == MPEG_NORM_PAL
+ || vcd_mpeg_get_norm (_info) == MPEG_NORM_PAL_S)
+ _set_bit(info_vcd.pal_flags, n);
+ else if (_pal_p (_info))
+ {
+ vcd_warn ("INFO.{VCD,SVD}: assuming PAL-type resolution for track #%d"
+ " -- are we creating a X(S)VCD?", n);
+ _set_bit(info_vcd.pal_flags, n);
+ }
+
+ n++;
+ }
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC))
+ {
+ info_vcd.flags.restriction = obj->info_restriction;
+ info_vcd.flags.use_lid2 = obj->info_use_lid2;
+ info_vcd.flags.use_track3 = obj->info_use_seq2;
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X)
+ &&_vcd_pbc_available (obj))
+ info_vcd.flags.pbc_x = true;
+
+ info_vcd.psd_size = uint32_to_be (get_psd_size (obj, false));
+ info_vcd.offset_mult = _vcd_pbc_available (obj) ? INFO_OFFSET_MULT : 0;
+ info_vcd.lot_entries = uint16_to_be (_vcd_pbc_max_lid (obj));
+
+ if (_cdio_list_length (obj->mpeg_segment_list))
+ {
+ unsigned segments = 0;
+
+ if (!_vcd_pbc_available (obj))
+ vcd_warn ("segment items available, but no PBC items set!"
+ " SPIs will be unreachable");
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *segment = _cdio_list_node_data (node);
+ unsigned idx;
+ InfoSpiContents contents = { 0, };
+
+ contents.video_type =
+ _derive_vid_type (segment->info,
+ _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ contents.audio_type =
+ _derive_aud_type (segment->info,
+ _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ contents.ogt =
+ _derive_ogt_type (segment->info,
+ _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ if (!contents.video_type && !contents.audio_type)
+ vcd_warn ("segment item '%s' seems contains neither video nor audio",
+ segment->id);
+
+ for (idx = 0; idx < segment->segment_count; idx++)
+ {
+ vcd_assert (segments + idx < MAX_SEGMENTS);
+
+ info_vcd.spi_contents[segments + idx] = contents;
+
+ if (!contents.item_cont)
+ contents.item_cont = true;
+ }
+
+ segments += idx;
+ }
+
+ info_vcd.item_count = uint16_to_be (segments);
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(obj->mpeg_segment_start_extent),
+ &info_vcd.first_seg_addr);
+ }
+ }
+
+ memcpy(buf, &info_vcd, sizeof(info_vcd));
+}
+
+static void
+set_tracks_svd_v30 (VcdObj *obj, void *buf)
+{
+ char tracks_svd_buf[ISO_BLOCKSIZE] = { 0, };
+ TracksSVD_v30 *tracks_svd = (void *) tracks_svd_buf;
+ CdioListNode *node;
+ double playtime;
+ int n;
+
+ strncpy (tracks_svd->file_id, TRACKS_SVD_FILE_ID,
+ sizeof (TRACKS_SVD_FILE_ID)-1);
+ tracks_svd->version = TRACKS_SVD_VERSION;
+ tracks_svd->tracks = _cdio_list_length (obj->mpeg_track_list);
+
+ n = 0;
+ playtime = 0;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _cdio_list_node_data (node);
+ int i;
+
+ playtime += track->info->playing_time;
+
+ tracks_svd->track[n].audio_info = track->info->ahdr[0].seen ? 0x2 : 0x0; /* fixme */
+ tracks_svd->track[n].audio_info |= track->info->ahdr[1].seen ? 0x20 : 0x0; /* fixme */
+
+ tracks_svd->track[n].ogt_info = 0x0;
+ for (i = 0; i < 4; i++)
+ if (track->info->ogt[i])
+ tracks_svd->track[n].ogt_info |= 1 << (i * 2); /* fixme */
+
+ /* setting playtime */
+
+ {
+ double i, f;
+
+ while (playtime >= 6000.0)
+ playtime -= 6000.0;
+
+ f = modf(playtime, &i);
+
+ cdio_lba_to_msf (i * 75, &tracks_svd->track[n].cum_playing_time);
+ tracks_svd->track[n].cum_playing_time.f =
+ cdio_to_bcd8 (floor (f * 75.0));
+ }
+
+ n++;
+ }
+
+ memcpy (buf, &tracks_svd_buf, sizeof(tracks_svd_buf));
+}
+
+void
+set_tracks_svd (VcdObj *obj, void *buf)
+{
+ char tracks_svd[ISO_BLOCKSIZE] = { 0, };
+ TracksSVD *tracks_svd1 = (void *) tracks_svd;
+ TracksSVD2 *tracks_svd2;
+ CdioListNode *node;
+ int n;
+
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ if (obj->svcd_vcd3_tracksvd)
+ {
+ set_tracks_svd_v30 (obj, buf);
+ return;
+ }
+
+ vcd_assert (sizeof (SVDTrackContent) == 1);
+
+ strncpy (tracks_svd1->file_id, TRACKS_SVD_FILE_ID, sizeof (TRACKS_SVD_FILE_ID)-1);
+ tracks_svd1->version = TRACKS_SVD_VERSION;
+
+ tracks_svd1->tracks = _cdio_list_length (obj->mpeg_track_list);
+
+ tracks_svd2 = (void *) &(tracks_svd1->playing_time[tracks_svd1->tracks]);
+
+ n = 0;
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _cdio_list_node_data (node);
+ const double playtime = track->info->playing_time;
+
+ int _video;
+
+ _video = tracks_svd2->contents[n].video =
+ _derive_vid_type (track->info, true);
+
+ tracks_svd2->contents[n].audio =
+ _derive_aud_type (track->info, true);
+
+ tracks_svd2->contents[n].ogt =
+ _derive_ogt_type (track->info, true);
+
+ if (_video != 0x3 && _video != 0x7)
+ vcd_warn("SVCD/TRACKS.SVCD: No MPEG motion video for track #%d?", n);
+
+ /* setting playtime */
+
+ {
+ double i, f;
+
+ f = modf(playtime, &i);
+
+ if (playtime >= 6000.0)
+ {
+ vcd_warn ("SVCD/TRACKS.SVD: playing time value (%d seconds) to great,"
+ " clipping to 100 minutes", (int) i);
+ i = 5999.0;
+ f = 74.0 / 75.0;
+ }
+
+ cdio_lba_to_msf (i * 75, &(tracks_svd1->playing_time[n]));
+ tracks_svd1->playing_time[n].f = cdio_to_bcd8 (floor (f * 75.0));
+ }
+
+ n++;
+ }
+
+ memcpy (buf, &tracks_svd, sizeof(tracks_svd));
+}
+
+static double
+_get_cumulative_playing_time (const VcdObj *obj, unsigned up_to_track_no)
+{
+ double result = 0;
+ CdioListNode *node;
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _cdio_list_node_data (node);
+
+ if (!up_to_track_no)
+ break;
+
+ result += track->info->playing_time;
+ up_to_track_no--;
+ }
+
+ if (up_to_track_no)
+ vcd_warn ("internal error...");
+
+ return result;
+}
+
+static unsigned
+_get_scanpoint_count (const VcdObj *obj)
+{
+ double total_playing_time;
+
+ total_playing_time = _get_cumulative_playing_time (obj, _cdio_list_length (obj->mpeg_track_list));
+
+ return ceil (total_playing_time * 2.0);
+}
+
+uint32_t
+get_search_dat_size (const VcdObj *obj)
+{
+ return sizeof (SearchDat)
+ + (_get_scanpoint_count (obj) * sizeof (msf_t));
+}
+
+static CdioList *
+_make_track_scantable (const VcdObj *obj)
+{
+ CdioList *all_aps = _cdio_list_new ();
+ CdioList *scantable = _cdio_list_new ();
+ unsigned scanpoints = _get_scanpoint_count (obj);
+ unsigned track_no;
+ CdioListNode *node;
+
+ track_no = 0;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _cdio_list_node_data (node);
+ CdioListNode *node2;
+
+ _CDIO_LIST_FOREACH (node2, track->info->shdr[0].aps_list)
+ {
+ struct aps_data *_data = _vcd_malloc (sizeof (struct aps_data));
+
+ *_data = *(struct aps_data *)_cdio_list_node_data (node2);
+
+ _data->timestamp += _get_cumulative_playing_time (obj, track_no);
+ _data->packet_no += obj->iso_size + track->relative_start_extent;
+ _data->packet_no += obj->track_front_margin;
+
+ _cdio_list_append (all_aps, _data);
+ }
+ track_no++;
+ }
+
+ {
+ CdioListNode *aps_node = _cdio_list_begin (all_aps);
+ CdioListNode *n;
+ struct aps_data *_data;
+ double aps_time;
+ double playing_time;
+ int aps_packet;
+ double t;
+
+ playing_time = scanpoints;
+ playing_time /= 2;
+
+ vcd_assert (aps_node != NULL);
+
+ _data = _cdio_list_node_data (aps_node);
+ aps_time = _data->timestamp;
+ aps_packet = _data->packet_no;
+
+ for (t = 0; t < playing_time; t += 0.5)
+ {
+ for(n = _cdio_list_node_next (aps_node); n;
+ n = _cdio_list_node_next (n))
+ {
+ _data = _cdio_list_node_data (n);
+
+ if (fabs (_data->timestamp - t) < fabs (aps_time - t))
+ {
+ aps_node = n;
+ aps_time = _data->timestamp;
+ aps_packet = _data->packet_no;
+ }
+ else
+ break;
+ }
+
+ {
+ uint32_t *lsect = _vcd_malloc (sizeof (uint32_t));
+
+ *lsect = aps_packet;
+ _cdio_list_append (scantable, lsect);
+ }
+
+ }
+
+ }
+
+ _cdio_list_free (all_aps, true);
+
+ vcd_assert (scanpoints == _cdio_list_length (scantable));
+
+ return scantable;
+}
+
+void
+set_search_dat (VcdObj *obj, void *buf)
+{
+ CdioList *scantable;
+ CdioListNode *node;
+ SearchDat search_dat;
+ unsigned n;
+
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+ /* vcd_assert (sizeof (SearchDat) == ?) */
+
+ memset (&search_dat, 0, sizeof (search_dat));
+
+ strncpy (search_dat.file_id, SEARCH_FILE_ID, sizeof (SEARCH_FILE_ID)-1);
+
+ search_dat.version = SEARCH_VERSION;
+ search_dat.scan_points = uint16_to_be (_get_scanpoint_count (obj));
+ search_dat.time_interval = SEARCH_TIME_INTERVAL;
+
+ memcpy (buf, &search_dat, sizeof (search_dat));
+
+ scantable = _make_track_scantable (obj);
+
+ n = 0;
+ _CDIO_LIST_FOREACH (node, scantable)
+ {
+ SearchDat *search_dat2 = buf;
+ uint32_t sect = *(uint32_t *) _cdio_list_node_data (node);
+
+ cdio_lba_to_msf(cdio_lsn_to_lba(sect), &(search_dat2->points[n]));
+ n++;
+ }
+
+ vcd_assert (n = _get_scanpoint_count (obj));
+
+ _cdio_list_free (scantable, true);
+}
+
+static uint32_t
+_get_scandata_count (const struct vcd_mpeg_stream_info *info)
+{
+ return ceil (info->playing_time * 2.0);
+}
+
+static uint32_t *
+_get_scandata_table (const struct vcd_mpeg_stream_info *info)
+{
+ CdioListNode *n, *aps_node = _cdio_list_begin (info->shdr[0].aps_list);
+ struct aps_data *_data;
+ double aps_time, t;
+ int aps_packet;
+ uint32_t *retval;
+ unsigned i;
+
+ retval = _vcd_malloc (_get_scandata_count (info) * sizeof (uint32_t));
+
+ _data = _cdio_list_node_data (aps_node);
+ aps_time = _data->timestamp;
+ aps_packet = _data->packet_no;
+
+ for (t = 0, i = 0; t < info->playing_time; t += 0.5, i++)
+ {
+ for(n = _cdio_list_node_next (aps_node); n; n = _cdio_list_node_next (n))
+ {
+ _data = _cdio_list_node_data (n);
+
+ if (fabs (_data->timestamp - t) < fabs (aps_time - t))
+ {
+ aps_node = n;
+ aps_time = _data->timestamp;
+ aps_packet = _data->packet_no;
+ }
+ else
+ break;
+ }
+
+ /* vcd_debug ("%f %f %d", t, aps_time, aps_packet); */
+
+ vcd_assert (i < _get_scandata_count (info));
+
+ retval[i] = aps_packet;
+ }
+
+ vcd_assert (i = _get_scandata_count (info));
+
+ return retval;
+}
+
+uint32_t
+get_scandata_dat_size (const VcdObj *obj)
+{
+ uint32_t retval = 0;
+
+ /* struct 1 */
+ retval += sizeof (ScandataDat1);
+ retval += sizeof (msf_t) * _cdio_list_length (obj->mpeg_track_list);
+
+ /* struct 2 */
+ /* vcd_assert (sizeof (ScandataDat2) == 0);
+ retval += sizeof (ScandataDat2); */
+ retval += sizeof (uint16_t) * 0;
+
+ /* struct 3 */
+ retval += sizeof (ScandataDat3);
+ retval += (sizeof (uint8_t) + sizeof (uint16_t)) * _cdio_list_length (obj->mpeg_track_list);
+
+ /* struct 4 */
+ /* vcd_assert (sizeof (ScandataDat4) == 0);
+ retval += sizeof (ScandataDat4); */
+ {
+ CdioListNode *node;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ const mpeg_track_t *track = _cdio_list_node_data (node);
+
+ retval += sizeof (msf_t) * _get_scandata_count (track->info);
+ }
+ }
+
+ return retval;
+}
+
+void
+set_scandata_dat (VcdObj *obj, void *buf)
+{
+ const unsigned tracks = _cdio_list_length (obj->mpeg_track_list);
+
+ ScandataDat1 *scandata_dat1 = (ScandataDat1 *) buf;
+ ScandataDat2 *scandata_dat2 =
+ (ScandataDat2 *) &(scandata_dat1->cum_playtimes[tracks]);
+ ScandataDat3 *scandata_dat3 =
+ (ScandataDat3 *) &(scandata_dat2->spi_indexes[0]);
+ ScandataDat4 *scandata_dat4 =
+ (ScandataDat4 *) &(scandata_dat3->mpeg_track_offsets[tracks]);
+
+ const uint16_t _begin_offset =
+ __cd_offsetof (ScandataDat3, mpeg_track_offsets[tracks])
+ - __cd_offsetof (ScandataDat3, mpeg_track_offsets);
+
+ CdioListNode *node;
+ unsigned n;
+ uint16_t _tmp_offset;
+
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ /* memset (buf, 0, get_scandata_dat_size (obj)); */
+
+ /* struct 1 */
+ strncpy (scandata_dat1->file_id, SCANDATA_FILE_ID, sizeof (SCANDATA_FILE_ID)-1);
+
+ scandata_dat1->version = SCANDATA_VERSION_SVCD;
+ scandata_dat1->reserved = 0x00;
+ scandata_dat1->scandata_count = uint16_to_be (_get_scanpoint_count (obj));
+
+ scandata_dat1->track_count = uint16_to_be (tracks);
+ scandata_dat1->spi_count = uint16_to_be (0);
+
+ for (n = 0; n < tracks; n++)
+ {
+ double playtime = _get_cumulative_playing_time (obj, n + 1);
+ double i = 0, f = 0;
+
+ f = modf(playtime, &i);
+
+ while (i >= (60 * 100))
+ i -= (60 * 100);
+
+ vcd_assert (i >= 0);
+
+ cdio_lba_to_msf (i * 75, &(scandata_dat1->cum_playtimes[n]));
+ scandata_dat1->cum_playtimes[n].f = cdio_to_bcd8 (floor (f * 75.0));
+ }
+
+ /* struct 2 -- nothing yet */
+
+ /* struct 3/4 */
+
+ vcd_assert ((_begin_offset % sizeof (msf_t) == 0)
+ && _begin_offset > 0);
+
+ _tmp_offset = 0;
+
+ scandata_dat3->mpegtrack_start_index = uint16_to_be (_begin_offset);
+
+ n = 0;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ const mpeg_track_t *track = _cdio_list_node_data (node);
+ uint32_t *_table;
+ const unsigned scanpoints = _get_scandata_count (track->info);
+ const unsigned _table_ofs =
+ (_tmp_offset * sizeof (msf_t)) + _begin_offset;
+ unsigned point;
+
+ scandata_dat3->mpeg_track_offsets[n].track_num = n + 2;
+ scandata_dat3->mpeg_track_offsets[n].table_offset = uint16_to_be (_table_ofs);
+
+ _table = _get_scandata_table (track->info);
+
+ for (point = 0; point < scanpoints; point++)
+ {
+ uint32_t lsect = _table[point];
+
+ lsect += obj->iso_size;
+ lsect += track->relative_start_extent;
+ lsect += obj->track_front_margin;
+
+ /* vcd_debug ("lsect %d %d", point, lsect); */
+
+ cdio_lba_to_msf(cdio_lsn_to_lba(lsect),
+ &(scandata_dat4->scandata_table[_tmp_offset + point]));
+ }
+
+ free (_table);
+
+ _tmp_offset += scanpoints;
+ n++;
+ }
+
+ /* struct 4 */
+
+
+}
+
+vcd_type_t
+vcd_files_info_detect_type (const void *info_buf)
+{
+ const InfoVcd_t *_info = info_buf;
+ vcd_type_t _type = VCD_TYPE_INVALID;
+
+ vcd_assert (info_buf != NULL);
+
+ if (!strncmp (_info->ID, INFO_ID_VCD, sizeof (_info->ID)))
+ switch (_info->version)
+ {
+ case INFO_VERSION_VCD2:
+ if (_info->sys_prof_tag != INFO_SPTAG_VCD2)
+ vcd_warn ("INFO.VCD: unexpected system profile tag %d encountered",
+ _info->version);
+ _type = VCD_TYPE_VCD2;
+ break;
+
+ case INFO_VERSION_VCD:
+ /* case INFO_VERSION_VCD11: */
+ switch (_info->sys_prof_tag)
+ {
+ case INFO_SPTAG_VCD:
+ _type = VCD_TYPE_VCD;
+ break;
+ case INFO_SPTAG_VCD11:
+ _type = VCD_TYPE_VCD11;
+ break;
+ default:
+ vcd_warn ("INFO.VCD: unexpected system profile tag %d "
+ "encountered, assuming VCD 1.1", _info->sys_prof_tag);
+ break;
+ }
+ break;
+
+ default:
+ vcd_warn ("unexpected VCD version %d encountered -- assuming VCD 2.0",
+ _info->version);
+ break;
+ }
+ else if (!strncmp (_info->ID, INFO_ID_SVCD, sizeof (_info->ID)))
+ switch (_info->version)
+ {
+ case INFO_VERSION_SVCD:
+ if (_info->sys_prof_tag != INFO_SPTAG_SVCD)
+ vcd_warn ("INFO.SVD: unexpected system profile tag value %d "
+ "-- assuming SVCD", _info->sys_prof_tag);
+ _type = VCD_TYPE_SVCD;
+ break;
+
+ default:
+ vcd_warn ("INFO.SVD: unexpected version value %d seen "
+ " -- still assuming SVCD", _info->version);
+ _type = VCD_TYPE_SVCD;
+ break;
+ }
+ else if (!strncmp (_info->ID, INFO_ID_HQVCD, sizeof (_info->ID)))
+ switch (_info->version)
+ {
+ case INFO_VERSION_HQVCD:
+ if (_info->sys_prof_tag != INFO_SPTAG_HQVCD)
+ vcd_warn ("INFO.SVD: unexpected system profile tag value -- assuming hqvcd");
+ _type = VCD_TYPE_HQVCD;
+ break;
+
+ default:
+ vcd_warn ("INFO.SVD: unexpected version value %d seen "
+ "-- still assuming HQVCD", _info->version);
+ _type = VCD_TYPE_HQVCD;
+ break;
+ }
+ else
+ vcd_warn ("INFO.SVD: signature not found");
+
+ return _type;
+}
+
+/* eof */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/image.c b/contrib/libvcd/image.c
new file mode 100644
index 000000000..d26bb0910
--- /dev/null
+++ b/contrib/libvcd/image.c
@@ -0,0 +1,108 @@
+/*
+ $Id: image.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ 2002 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+
+/* Public headers */
+#include <libvcd/sector.h>
+#include <cdio/iso9660.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "image_sink.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: image.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $";
+
+/*
+ * VcdImageSink routines next.
+ */
+
+struct _VcdImageSink {
+ void *user_data;
+ vcd_image_sink_funcs op;
+};
+
+VcdImageSink *
+vcd_image_sink_new (void *user_data, const vcd_image_sink_funcs *funcs)
+{
+ VcdImageSink *new_obj;
+
+ new_obj = _vcd_malloc (sizeof (VcdImageSink));
+
+ new_obj->user_data = user_data;
+ new_obj->op = *funcs;
+
+ return new_obj;
+}
+
+void
+vcd_image_sink_destroy (VcdImageSink *obj)
+{
+ vcd_assert (obj != NULL);
+
+ obj->op.free (obj->user_data);
+ free (obj);
+}
+
+int
+vcd_image_sink_set_cuesheet (VcdImageSink *obj, const CdioList *vcd_cue_list)
+{
+ vcd_assert (obj != NULL);
+
+ return obj->op.set_cuesheet (obj->user_data, vcd_cue_list);
+}
+
+int
+vcd_image_sink_write (VcdImageSink *obj, void *buf, lsn_t lsn)
+{
+ vcd_assert (obj != NULL);
+
+ return obj->op.write (obj->user_data, buf, lsn);
+}
+
+/*!
+ Set the arg "key" with "value" in the target device.
+*/
+
+int
+vcd_image_sink_set_arg (VcdImageSink *obj, const char key[],
+ const char value[])
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->op.set_arg != NULL);
+ vcd_assert (key != NULL);
+
+ return obj->op.set_arg (obj->user_data, key, value);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/image_bincue.c b/contrib/libvcd/image_bincue.c
new file mode 100644
index 000000000..9c447a42a
--- /dev/null
+++ b/contrib/libvcd/image_bincue.c
@@ -0,0 +1,259 @@
+/*
+ $Id: image_bincue.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2001, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <cdio/cdio.h>
+#include <cdio/bytesex.h>
+#include <cdio/iso9660.h>
+
+/* Public headers */
+#include <libvcd/sector.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "image_sink.h"
+#include "stream_stdio.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: image_bincue.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $";
+
+/* reader */
+
+#define DEFAULT_VCD_DEVICE "videocd.bin"
+
+/****************************************************************************
+ * writer
+ */
+
+typedef struct {
+ bool sector_2336_flag;
+ VcdDataSink *bin_snk;
+ VcdDataSink *cue_snk;
+ char *bin_fname;
+ char *cue_fname;
+
+ bool init;
+} _img_bincue_snk_t;
+
+static void
+_sink_init (_img_bincue_snk_t *_obj)
+{
+ if (_obj->init)
+ return;
+
+ if (!(_obj->bin_snk = vcd_data_sink_new_stdio (_obj->bin_fname)))
+ vcd_error ("init failed");
+
+ if (!(_obj->cue_snk = vcd_data_sink_new_stdio (_obj->cue_fname)))
+ vcd_error ("init failed");
+
+ _obj->init = true;
+}
+
+static void
+_sink_free (void *user_data)
+{
+ _img_bincue_snk_t *_obj = user_data;
+
+ vcd_data_sink_destroy (_obj->bin_snk);
+ vcd_data_sink_destroy (_obj->cue_snk);
+ free (_obj->bin_fname);
+ free (_obj->cue_fname);
+ free (_obj);
+}
+
+static int
+_set_cuesheet (void *user_data, const CdioList *vcd_cue_list)
+{
+ _img_bincue_snk_t *_obj = user_data;
+ CdioListNode *node;
+ int track_no, index_no;
+ const vcd_cue_t *_last_cue = 0;
+
+ _sink_init (_obj);
+
+ vcd_data_sink_printf (_obj->cue_snk, "FILE \"%s\" BINARY\r\n",
+ _obj->bin_fname);
+
+ track_no = 0;
+ index_no = 0;
+ _CDIO_LIST_FOREACH (node, (CdioList *) vcd_cue_list)
+ {
+ const vcd_cue_t *_cue = _cdio_list_node_data (node);
+ char *psz_msf;
+
+ msf_t _msf = { 0, 0, 0 };
+
+ switch (_cue->type)
+ {
+ case VCD_CUE_TRACK_START:
+ track_no++;
+ index_no = 0;
+
+ vcd_data_sink_printf (_obj->cue_snk,
+ " TRACK %2.2d MODE2/%d\r\n"
+ " FLAGS DCP\r\n",
+ track_no, (_obj->sector_2336_flag ? 2336 : 2352));
+
+ if (_last_cue && _last_cue->type == VCD_CUE_PREGAP_START)
+ {
+ cdio_lba_to_msf (_last_cue->lsn, &_msf);
+ psz_msf = cdio_msf_to_str(&_msf);
+
+ vcd_data_sink_printf (_obj->cue_snk,
+ " INDEX %2.2d %s\r\n",
+ index_no, psz_msf);
+ free(psz_msf);
+ }
+
+ index_no++;
+
+ cdio_lba_to_msf (_cue->lsn, &_msf);
+ psz_msf = cdio_msf_to_str(&_msf);
+
+ vcd_data_sink_printf (_obj->cue_snk,
+ " INDEX %2.2d %s\r\n",
+ index_no, psz_msf);
+ free(psz_msf);
+ break;
+
+ case VCD_CUE_PREGAP_START:
+ /* handled in next iteration */
+ break;
+
+ case VCD_CUE_SUBINDEX:
+ vcd_assert (_last_cue != 0);
+
+ index_no++;
+ vcd_assert (index_no <= CDIO_CD_MAX_TRACKS);
+
+ cdio_lba_to_msf (_cue->lsn, &_msf);
+ psz_msf = cdio_msf_to_str(&_msf);
+
+ vcd_data_sink_printf (_obj->cue_snk,
+ " INDEX %2.2d %s\r\n",
+ index_no, psz_msf);
+ free(psz_msf);
+ break;
+
+ case VCD_CUE_END:
+ vcd_data_sink_close (_obj->cue_snk);
+ return 0;
+ break;
+
+ case VCD_CUE_LEADIN:
+ break;
+ }
+
+ _last_cue = _cue;
+ }
+
+ vcd_assert_not_reached ();
+
+ return -1;
+}
+
+static int
+_vcd_image_bincue_write (void *user_data, const void *data, lsn_t lsn)
+{
+ const char *buf = data;
+ _img_bincue_snk_t *_obj = user_data;
+ long offset = lsn;
+
+ _sink_init (_obj);
+
+ offset *= _obj->sector_2336_flag ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW;
+
+ vcd_data_sink_seek(_obj->bin_snk, offset);
+
+ if (_obj->sector_2336_flag)
+ vcd_data_sink_write(_obj->bin_snk, buf + 12 + 4, M2RAW_SECTOR_SIZE, 1);
+ else
+ vcd_data_sink_write(_obj->bin_snk, buf, CDIO_CD_FRAMESIZE_RAW, 1);
+
+ return 0;
+}
+
+static int
+_sink_set_arg (void *user_data, const char key[], const char value[])
+{
+ _img_bincue_snk_t *_obj = user_data;
+
+ if (!strcmp (key, "bin"))
+ {
+ free (_obj->bin_fname);
+
+ if (!value)
+ return -2;
+
+ _obj->bin_fname = strdup (value);
+ }
+ else if (!strcmp (key, "cue"))
+ {
+ free (_obj->cue_fname);
+
+ if (!value)
+ return -2;
+
+ _obj->cue_fname = strdup (value);
+ }
+ else if (!strcmp (key, "sector"))
+ {
+ if (!strcmp (value, "2336"))
+ _obj->sector_2336_flag = true;
+ else if (!strcmp (value, "2352"))
+ _obj->sector_2336_flag = false;
+ else
+ return -2;
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+VcdImageSink *
+vcd_image_sink_new_bincue (void)
+{
+ _img_bincue_snk_t *_data;
+
+ vcd_image_sink_funcs _funcs = {
+ .set_cuesheet = _set_cuesheet,
+ .write = _vcd_image_bincue_write,
+ .free = _sink_free,
+ .set_arg = _sink_set_arg
+ };
+
+ _data = _vcd_malloc (sizeof (_img_bincue_snk_t));
+
+ _data->bin_fname = strdup ("videocd.bin");
+ _data->cue_fname = strdup ("videocd.cue");
+
+ return vcd_image_sink_new (_data, &_funcs);
+}
+
diff --git a/contrib/libvcd/image_cdrdao.c b/contrib/libvcd/image_cdrdao.c
new file mode 100644
index 000000000..5e00c14ee
--- /dev/null
+++ b/contrib/libvcd/image_cdrdao.c
@@ -0,0 +1,313 @@
+/*
+ $Id: image_cdrdao.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* Public headers */
+#include <cdio/iso9660.h>
+#include <cdio/bytesex.h>
+#include <libvcd/sector.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "image_sink.h"
+#include "stream_stdio.h"
+#include "util.h"
+#include "vcd.h"
+
+static const char _rcsid[] = "$Id: image_cdrdao.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $";
+
+/* reader */
+
+/****************************************************************************
+ * writer
+ */
+
+typedef struct {
+ bool sector_2336_flag;
+ char *toc_fname;
+ char *img_base;
+
+ VcdDataSink *last_bin_snk;
+ int last_snk_idx;
+ bool last_pause;
+
+ CdioList *vcd_cue_list;
+} _img_cdrdao_snk_t;
+
+static void
+_sink_free (void *user_data)
+{
+ _img_cdrdao_snk_t *_obj = user_data;
+
+ /* fixme -- destroy cue list */
+
+ vcd_data_sink_destroy (_obj->last_bin_snk);
+ free (_obj->toc_fname);
+ free (_obj->img_base);
+ free (_obj);
+}
+
+static int
+_set_cuesheet (void *user_data, const CdioList *vcd_cue_list)
+{
+ _img_cdrdao_snk_t *_obj = user_data;
+ VcdDataSink *toc_snk = vcd_data_sink_new_stdio (_obj->toc_fname);
+
+ CdioListNode *node;
+
+ int track_no, index_no;
+ const vcd_cue_t *_last_cue = 0;
+ unsigned last_track_lsn = 0;
+
+ vcd_data_sink_printf (toc_snk,
+ "// CDRDAO TOC\n"
+ "// generated by %s\n\n"
+ "CD_ROM_XA\n", vcd_version_string (false));
+
+ _obj->vcd_cue_list = _cdio_list_new ();
+
+ index_no = track_no = 0;
+ _CDIO_LIST_FOREACH (node, (CdioList *) vcd_cue_list)
+ {
+ const vcd_cue_t *_cue = _cdio_list_node_data (node);
+
+ /* copy cue list while traversing */
+ {
+ vcd_cue_t *_cue2 = _vcd_malloc (sizeof (vcd_cue_t));
+ *_cue2 = *_cue;
+ _cdio_list_append (_obj->vcd_cue_list, _cue2);
+ }
+
+ switch (_cue->type)
+ {
+ case VCD_CUE_TRACK_START:
+ track_no++;
+ index_no = 0;
+
+ last_track_lsn = _cue->lsn;
+
+ vcd_data_sink_printf (toc_snk,
+ "\n// Track %d\n"
+ "TRACK %s\n COPY\n",
+ track_no,
+ (_obj->sector_2336_flag ? "MODE2_FORM_MIX" : "MODE2_RAW"));
+
+ if (_last_cue && _last_cue->type == VCD_CUE_PREGAP_START)
+ vcd_data_sink_printf (toc_snk,
+ " DATAFILE \"%s_%.2d_pregap.img\"\n"
+ " START\n",
+ _obj->img_base, track_no);
+
+ index_no++;
+
+ vcd_data_sink_printf (toc_snk,
+ " DATAFILE \"%s_%.2d.img\"\n",
+ _obj->img_base, track_no);
+ break;
+
+ case VCD_CUE_PREGAP_START:
+ /* handled in next iteration */
+ break;
+
+ case VCD_CUE_SUBINDEX:
+ index_no++;
+
+ {
+ msf_t _msf = { 0, 0, 0 };
+ char *psz_msf;
+
+ cdio_lba_to_msf (_cue->lsn - last_track_lsn, &_msf);
+ psz_msf = cdio_msf_to_str(&_msf);
+
+ vcd_data_sink_printf (toc_snk, " INDEX %s\n", psz_msf);
+ free(psz_msf);
+ }
+ break;
+
+ case VCD_CUE_LEADIN:
+ break;
+
+ case VCD_CUE_END:
+ vcd_data_sink_printf (toc_snk, "\n// EOF\n");
+
+ vcd_data_sink_close (toc_snk);
+ vcd_data_sink_destroy (toc_snk);
+
+ return 0;
+ break;
+ }
+
+ _last_cue = _cue;
+ }
+
+ vcd_assert_not_reached ();
+
+ return -1;
+}
+
+static int
+_vcd_image_cdrdao_write (void *user_data, const void *data, lsn_t lsn)
+{
+ const char *buf = data;
+ _img_cdrdao_snk_t *_obj = user_data;
+ long offset;
+
+ {
+ CdioListNode *node;
+ uint32_t _last = 0;
+ uint32_t _ofs = 0;
+ bool _lpregap = false;
+ bool _pregap = false;
+
+ int num = 0, in_track = 0;
+ _CDIO_LIST_FOREACH (node, _obj->vcd_cue_list)
+ {
+ const vcd_cue_t *_cue = _cdio_list_node_data (node);
+
+ switch (_cue->type)
+ {
+ case VCD_CUE_PREGAP_START:
+ case VCD_CUE_END:
+ case VCD_CUE_TRACK_START:
+ if (_cue->lsn && IN (lsn, _last, _cue->lsn - 1))
+ {
+ vcd_assert (in_track == 0);
+ in_track = num;
+ _ofs = _last;
+ _pregap = _lpregap;
+ }
+
+ _last = _cue->lsn;
+ _lpregap = (_cue->type == VCD_CUE_PREGAP_START);
+
+ if (_cue->type == VCD_CUE_TRACK_START)
+ num++;
+ break;
+
+ default:
+ /* noop */
+ break;
+ }
+ }
+
+ vcd_assert (in_track != 0);
+ vcd_assert (_obj->last_snk_idx <= in_track);
+
+ if (_obj->last_snk_idx != in_track
+ || _obj->last_pause != _pregap)
+ {
+ char buf[4096] = { 0, };
+
+ if (_obj->last_bin_snk)
+ vcd_data_sink_destroy (_obj->last_bin_snk);
+
+ snprintf (buf, sizeof (buf),
+ "%s_%.2d%s.img",
+ _obj->img_base,
+ (_pregap ? in_track + 1 : in_track),
+ (_pregap ? "_pregap" : ""));
+
+ _obj->last_bin_snk = vcd_data_sink_new_stdio (buf);
+ _obj->last_snk_idx = in_track;
+ _obj->last_pause = _pregap;
+ }
+
+ vcd_assert (lsn >= _ofs);
+ offset = lsn - _ofs;
+ }
+
+ offset *= _obj->sector_2336_flag ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW;
+
+ vcd_data_sink_seek(_obj->last_bin_snk, offset);
+
+ if (_obj->sector_2336_flag)
+ vcd_data_sink_write(_obj->last_bin_snk, buf + 12 + 4, M2RAW_SECTOR_SIZE, 1);
+ else
+ vcd_data_sink_write(_obj->last_bin_snk, buf, CDIO_CD_FRAMESIZE_RAW, 1);
+
+ return 0;
+}
+
+static int
+_sink_set_arg (void *user_data, const char key[], const char value[])
+{
+ _img_cdrdao_snk_t *_obj = user_data;
+
+ if (!strcmp (key, "toc"))
+ {
+ free (_obj->toc_fname);
+
+ if (!value)
+ return -2;
+
+ _obj->toc_fname = strdup (value);
+ }
+ else if (!strcmp (key, "img_base"))
+ {
+ free (_obj->img_base);
+
+ if (!value)
+ return -2;
+
+ _obj->img_base = strdup (value);
+ }
+ else if (!strcmp (key, "sector"))
+ {
+ if (!strcmp (value, "2336"))
+ _obj->sector_2336_flag = true;
+ else if (!strcmp (value, "2352"))
+ _obj->sector_2336_flag = false;
+ else
+ return -2;
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+VcdImageSink *
+vcd_image_sink_new_cdrdao (void)
+{
+ _img_cdrdao_snk_t *_data;
+
+ vcd_image_sink_funcs _funcs = {
+ .set_cuesheet = _set_cuesheet,
+ .write = _vcd_image_cdrdao_write,
+ .free = _sink_free,
+ .set_arg = _sink_set_arg
+ };
+
+ _data = _vcd_malloc (sizeof (_img_cdrdao_snk_t));
+
+ _data->toc_fname = strdup ("videocd.toc");
+ _data->img_base = strdup ("videocd");
+
+ return vcd_image_sink_new (_data, &_funcs);
+}
+
diff --git a/contrib/libvcd/image_nrg.c b/contrib/libvcd/image_nrg.c
new file mode 100644
index 000000000..1faeb98a9
--- /dev/null
+++ b/contrib/libvcd/image_nrg.c
@@ -0,0 +1,352 @@
+/*
+ $Id: image_nrg.c,v 1.4 2006/09/27 05:41:40 dgp85 Exp $
+
+ Copyright (C) 2001, 2003, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*! This code implements low-level access functions for Nero's native
+ CD-image format residing inside a disk file (*.nrg).
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <cdio/cdio.h>
+#include <cdio/bytesex.h>
+#include <cdio/iso9660.h>
+
+/* Public headers */
+#include <libvcd/sector.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "image_sink.h"
+#include "stream_stdio.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: image_nrg.c,v 1.4 2006/09/27 05:41:40 dgp85 Exp $";
+
+/* structures used */
+
+/* this ugly image format is typical for lazy win32 programmers... at
+ least structure were set big endian, so at reverse
+ engineering wasn't such a big headache... */
+
+PRAGMA_BEGIN_PACKED
+typedef struct {
+ uint32_t start GNUC_PACKED;
+ uint32_t length GNUC_PACKED;
+ uint32_t type GNUC_PACKED; /* only 0x3 seen so far...
+ -> MIXED_MODE2 2336 blocksize */
+ uint32_t start_lsn GNUC_PACKED; /* does not include any pre-gaps! */
+ uint32_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */
+} _etnf_array_t;
+
+/* finally they realized that 32bit offsets are a bit outdated for IA64 *eg* */
+typedef struct {
+ uint64_t start GNUC_PACKED;
+ uint64_t length GNUC_PACKED;
+ uint32_t type GNUC_PACKED;
+ uint32_t start_lsn GNUC_PACKED;
+ uint64_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */
+} _etn2_array_t;
+
+typedef struct {
+ uint8_t _unknown1 GNUC_PACKED; /* 0x41 == 'A' */
+ uint8_t track GNUC_PACKED; /* binary or BCD?? */
+ uint8_t index GNUC_PACKED; /* makes 0->1 transitions */
+ uint8_t _unknown2 GNUC_PACKED; /* ?? */
+ uint32_t lsn GNUC_PACKED;
+} _cuex_array_t;
+
+typedef struct {
+ uint32_t id GNUC_PACKED;
+ uint32_t len GNUC_PACKED;
+ char data[EMPTY_ARRAY_SIZE] GNUC_PACKED;
+} _chunk_t;
+
+PRAGMA_END_PACKED
+
+/* to be converted into BE */
+#define CUEX_ID 0x43554558
+#define CUES_ID 0x43554553
+#define DAOX_ID 0x44414f58
+#define DAOI_ID 0x44414f49
+#define END1_ID 0x454e4421
+#define ETN2_ID 0x45544e32
+#define ETNF_ID 0x45544e46
+#define NER5_ID 0x4e455235
+#define NERO_ID 0x4e45524f
+#define SINF_ID 0x53494e46
+
+/****************************************************************************
+ * writer
+ */
+
+typedef struct {
+ VcdDataSink *nrg_snk;
+ char *nrg_fname;
+
+ CdioList *vcd_cue_list;
+ int tracks;
+ uint32_t cue_end_lsn;
+
+ bool init;
+} _img_nrg_snk_t;
+
+static void
+_sink_init (_img_nrg_snk_t *_obj)
+{
+ if (_obj->init)
+ return;
+
+ if (!(_obj->nrg_snk = vcd_data_sink_new_stdio (_obj->nrg_fname)))
+ vcd_error ("init failed");
+
+ _obj->init = true;
+}
+
+
+static void
+_sink_free (void *user_data)
+{
+ _img_nrg_snk_t *_obj = user_data;
+
+ free (_obj->nrg_fname);
+ vcd_data_sink_destroy (_obj->nrg_snk);
+
+ free (_obj);
+}
+
+static int
+_set_cuesheet (void *user_data, const CdioList *vcd_cue_list)
+{
+ _img_nrg_snk_t *_obj = user_data;
+ CdioListNode *node;
+ int num;
+
+ _sink_init (_obj);
+
+ _obj->vcd_cue_list = _cdio_list_new ();
+
+ num = 0;
+ _CDIO_LIST_FOREACH (node, (CdioList *) vcd_cue_list)
+ {
+ const vcd_cue_t *_cue = _cdio_list_node_data (node);
+ vcd_cue_t *_cue2 = _vcd_malloc (sizeof (vcd_cue_t));
+ *_cue2 = *_cue;
+ _cdio_list_append (_obj->vcd_cue_list, _cue2);
+
+ if (_cue->type == VCD_CUE_TRACK_START)
+ num++;
+
+ if (_cue->type == VCD_CUE_END)
+ _obj->cue_end_lsn = _cue->lsn;
+ }
+
+ _obj->tracks = num;
+
+ vcd_assert (CDIO_CD_MIN_TRACK_NO >= 1 && num <= CDIO_CD_MAX_TRACKS);
+
+ return 0;
+}
+
+static uint32_t
+_map (_img_nrg_snk_t *_obj, uint32_t lsn)
+{
+ CdioListNode *node;
+ uint32_t result = lsn;
+ vcd_cue_t *_cue = NULL, *_last = NULL;
+
+ vcd_assert (_obj->cue_end_lsn > lsn);
+
+ _CDIO_LIST_FOREACH (node, _obj->vcd_cue_list)
+ {
+ _cue = _cdio_list_node_data (node);
+
+ if (lsn < _cue->lsn)
+ break;
+
+ switch (_cue->type)
+ {
+ case VCD_CUE_TRACK_START:
+ result -= _cue->lsn;
+ break;
+ case VCD_CUE_PREGAP_START:
+ result += _cue->lsn;
+ break;
+ default:
+ break;
+ }
+
+ _last = _cue;
+ }
+
+ vcd_assert (node != NULL);
+ vcd_assert (_last != NULL);
+
+ switch (_last->type)
+ {
+ case VCD_CUE_TRACK_START:
+ return result;
+ break;
+
+ case VCD_CUE_PREGAP_START:
+ return -1;
+ break;
+
+ default:
+ case VCD_CUE_END:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return -1;
+}
+
+static int
+_write_tail (_img_nrg_snk_t *_obj, uint32_t offset)
+{
+ CdioListNode *node;
+ int _size;
+ _chunk_t _chunk;
+
+ vcd_data_sink_seek (_obj->nrg_snk, offset);
+
+ _size = _obj->tracks * sizeof (_etnf_array_t);
+ _chunk.id = UINT32_TO_BE (ETNF_ID);
+ _chunk.len = uint32_to_be (_size);
+
+ vcd_data_sink_write (_obj->nrg_snk, &_chunk, sizeof (_chunk_t), 1);
+
+ _CDIO_LIST_FOREACH (node, _obj->vcd_cue_list)
+ {
+ vcd_cue_t *_cue = _cdio_list_node_data (node);
+
+ if (_cue->type == VCD_CUE_TRACK_START)
+ {
+ vcd_cue_t *_cue2 =
+ _cdio_list_node_data (_cdio_list_node_next (node));
+
+ _etnf_array_t _etnf = { 0, };
+
+ _etnf.type = UINT32_TO_BE (0x3);
+ _etnf.start_lsn = uint32_to_be (_map (_obj, _cue->lsn));
+ _etnf.start = uint32_to_be (_map (_obj, _cue->lsn) * M2RAW_SECTOR_SIZE);
+
+ _etnf.length = uint32_to_be ((_cue2->lsn - _cue->lsn) * M2RAW_SECTOR_SIZE);
+
+ vcd_data_sink_write (_obj->nrg_snk, &_etnf, sizeof (_etnf_array_t), 1);
+ }
+
+ }
+
+ {
+ uint32_t tracks = uint32_to_be (_obj->tracks);
+
+ _chunk.id = UINT32_TO_BE (SINF_ID);
+ _chunk.len = UINT32_TO_BE (sizeof (uint32_t));
+ vcd_data_sink_write (_obj->nrg_snk, &_chunk, sizeof (_chunk_t), 1);
+
+ vcd_data_sink_write (_obj->nrg_snk, &tracks, sizeof (uint32_t), 1);
+ }
+
+ _chunk.id = UINT32_TO_BE (END1_ID);
+ _chunk.len = UINT32_TO_BE (0);
+ vcd_data_sink_write (_obj->nrg_snk, &_chunk, sizeof (_chunk_t), 1);
+
+ _chunk.id = UINT32_TO_BE (NERO_ID);
+ _chunk.len = uint32_to_be (offset);
+ vcd_data_sink_write (_obj->nrg_snk, &_chunk, sizeof (_chunk_t), 1);
+
+ return 0;
+}
+
+static int
+_vcd_image_nrg_write (void *user_data, const void *data, lsn_t lsn)
+{
+ const char *buf = data;
+ _img_nrg_snk_t *_obj = user_data;
+ uint32_t _lsn = _map (_obj, lsn);
+
+ _sink_init (_obj);
+
+ if (_lsn == -1)
+ {
+ /* vcd_debug ("ignoring %d", lsn); */
+ return 0;
+ }
+
+ vcd_data_sink_seek(_obj->nrg_snk, _lsn * M2RAW_SECTOR_SIZE);
+ vcd_data_sink_write(_obj->nrg_snk, buf + 12 + 4, M2RAW_SECTOR_SIZE, 1);
+
+ if (_obj->cue_end_lsn - 1 == lsn)
+ {
+ vcd_debug ("ENDLSN reached! (%lu == %lu)",
+ (long unsigned int) lsn, (long unsigned int) _lsn);
+ return _write_tail (_obj, (_lsn + 1) * M2RAW_SECTOR_SIZE);
+ }
+
+ return 0;
+}
+
+static int
+_sink_set_arg (void *user_data, const char key[], const char value[])
+{
+ _img_nrg_snk_t *_obj = user_data;
+
+ if (!strcmp (key, "nrg"))
+ {
+ free (_obj->nrg_fname);
+
+ if (!value)
+ return -2;
+
+ _obj->nrg_fname = strdup (value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+VcdImageSink *
+vcd_image_sink_new_nrg (void)
+{
+ _img_nrg_snk_t *_data;
+
+ vcd_image_sink_funcs _funcs = {
+ .set_cuesheet = _set_cuesheet,
+ .write = _vcd_image_nrg_write,
+ .free = _sink_free,
+ .set_arg = _sink_set_arg
+ };
+
+ _data = _vcd_malloc (sizeof (_img_nrg_snk_t));
+ _data->nrg_fname = strdup ("videocd.nrg");
+
+ vcd_warn ("opening TAO NRG image for writing; TAO (S)VCD are NOT 100%% compliant!");
+
+ return vcd_image_sink_new (_data, &_funcs);
+}
+
diff --git a/contrib/libvcd/image_sink.h b/contrib/libvcd/image_sink.h
new file mode 100644
index 000000000..a71e30199
--- /dev/null
+++ b/contrib/libvcd/image_sink.h
@@ -0,0 +1,80 @@
+/*
+ $Id: image_sink.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_IMAGE_SINK_H__
+#define __VCD_IMAGE_SINK_H__
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+#include <libvcd/types.h>
+
+/* Private includes */
+#include "data_structures.h"
+#include "stream.h"
+
+/* VcdImageSink ( --> image writer) */
+
+typedef struct _VcdImageSink VcdImageSink;
+
+typedef struct {
+ uint32_t lsn;
+ enum {
+ VCD_CUE_TRACK_START = 1, /* INDEX 0 -> 1 transition, TOC entry */
+ VCD_CUE_PREGAP_START, /* INDEX = 0 start */
+ VCD_CUE_SUBINDEX, /* INDEX++; sub-index */
+ VCD_CUE_END, /* lead-out start */
+ VCD_CUE_LEADIN, /* lead-in start */
+ } type;
+} vcd_cue_t;
+
+typedef struct {
+ int (*set_cuesheet) (void *user_data, const CdioList *vcd_cue_list);
+ int (*write) (void *user_data, const void *buf, lsn_t lsn);
+ void (*free) (void *user_data);
+ int (*set_arg) (void *user_data, const char key[], const char value[]);
+} vcd_image_sink_funcs;
+
+VcdImageSink *
+vcd_image_sink_new (void *user_data, const vcd_image_sink_funcs *funcs);
+
+void
+vcd_image_sink_destroy (VcdImageSink *obj);
+
+int
+vcd_image_sink_set_cuesheet (VcdImageSink *obj, const CdioList *vcd_cue_list);
+
+int
+vcd_image_sink_write (VcdImageSink *obj, void *buf, lsn_t lsn);
+
+/*!
+ Set the arg "key" with "value" in the target device.
+*/
+int
+vcd_image_sink_set_arg (VcdImageSink *obj, const char key[],
+ const char value[]);
+
+VcdImageSink * vcd_image_sink_new_nrg (void);
+VcdImageSink * vcd_image_sink_new_bincue (void);
+VcdImageSink * vcd_image_sink_new_cdrdao (void);
+
+#endif /* __VCD_IMAGE_SINK_H__ */
diff --git a/contrib/libvcd/inf.c b/contrib/libvcd/inf.c
new file mode 100644
index 000000000..588c8e5f7
--- /dev/null
+++ b/contrib/libvcd/inf.c
@@ -0,0 +1,531 @@
+/*
+ $Id: inf.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*
+ Things here refer to lower-level structures using a structure other
+ than vcdinfo_t. For higher-level structures via the vcdinfo_t, see
+ info.c
+*/
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stddef.h>
+#include <errno.h>
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/bytesex.h>
+#include <cdio/util.h>
+
+/* Eventually move above libvcd includes but having vcdinfo including. */
+#include <libvcd/info.h>
+
+/* Private headers */
+#include "info_private.h"
+#include "pbc.h"
+
+static const char _rcsid[] = "$Id: inf.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $";
+
+#define BUF_COUNT 16
+#define BUF_SIZE 80
+
+/* Return a pointer to a internal free buffer */
+static char *
+_getbuf (void)
+{
+ static char _buf[BUF_COUNT][BUF_SIZE];
+ static int _num = -1;
+
+ _num++;
+ _num %= BUF_COUNT;
+
+ memset (_buf[_num], 0, BUF_SIZE);
+
+ return _buf[_num];
+}
+
+const char *
+vcdinf_area_str (const struct psd_area_t *_area)
+{
+ char *buf;
+
+ if (!_area->x1
+ && !_area->y1
+ && !_area->x2
+ && !_area->y2)
+ return "disabled";
+
+ buf = _getbuf ();
+
+ snprintf (buf, BUF_SIZE, "[%3d,%3d] - [%3d,%3d]",
+ _area->x1, _area->y1,
+ _area->x2, _area->y2);
+
+ return buf;
+}
+
+/*!
+ Return a string containing the VCD album id, or NULL if there is
+ some problem in getting this.
+*/
+const char *
+vcdinf_get_album_id(const InfoVcd_t *info)
+{
+ if (NULL==info) return NULL;
+ return vcdinfo_strip_trail (info->album_desc, MAX_ALBUM_LEN);
+}
+
+/*!
+ Get autowait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+*/
+int
+vcdinf_get_autowait_time (const PsdPlayListDescriptor_t *d)
+{
+ return vcdinfo_get_wait_time (d->atime);
+}
+
+/*!
+ Return the base selection number. VCD_INVALID_BSN is returned if there
+ is an error.
+*/
+unsigned int
+vcdinf_get_bsn(const PsdSelectionListDescriptor_t *psd)
+{
+ if (NULL==psd) return VCDINFO_INVALID_BSN;
+ return(psd->bsn);
+}
+
+/*!
+ Return a string giving VCD format (VCD 1.0 VCD 1.1, SVCD, ...
+ for this object.
+*/
+const char *
+vcdinf_get_format_version_str (vcd_type_t vcd_type)
+{
+ switch (vcd_type)
+ {
+ case VCD_TYPE_VCD:
+ return ("VCD 1.0");
+ break;
+ case VCD_TYPE_VCD11:
+ return ("VCD 1.1");
+ break;
+ case VCD_TYPE_VCD2:
+ return ("VCD 2.0");
+ break;
+ case VCD_TYPE_SVCD:
+ return ("SVCD");
+ break;
+ case VCD_TYPE_HQVCD:
+ return ("HQVCD");
+ break;
+ case VCD_TYPE_INVALID:
+ return ("INVALID");
+ break;
+ default:
+ return ( "????");
+ }
+}
+
+/*!
+ Return loop count. 0 is infinite loop.
+*/
+uint16_t
+vcdinf_get_loop_count (const PsdSelectionListDescriptor_t *psd)
+{
+ return 0x7f & psd->loop;
+}
+
+/*!
+ Return LOT offset
+*/
+uint16_t
+vcdinf_get_lot_offset (const LotVcd_t *lot, unsigned int n)
+{
+ return uint16_from_be (lot->offset[n]);
+}
+
+/*!
+ Return the number of entries in the VCD.
+*/
+unsigned int
+vcdinf_get_num_entries(const EntriesVcd_t *entries)
+{
+ if (NULL==entries) return 0;
+ return (uint16_from_be (entries->entry_count));
+}
+
+/*!
+ Return the number of segments in the VCD.
+*/
+segnum_t
+vcdinf_get_num_segments(const InfoVcd_t *info)
+{
+ if (NULL==info) return 0;
+ return (uint16_from_be (info->item_count));
+}
+
+/*!
+ Return number of LIDs.
+*/
+lid_t
+vcdinf_get_num_LIDs (const InfoVcd_t *info)
+{
+ if (NULL==info) return 0;
+ /* Should probably use _vcd_pbc_max_lid instead? */
+ return uint16_from_be (info->lot_entries);
+}
+
+/*!
+ Return the number of menu selections for selection list descriptor psd.
+*/
+unsigned int
+vcdinf_get_num_selections(const PsdSelectionListDescriptor_t *psd)
+{
+ return psd->nos;
+}
+
+/*!
+ Get play time value for PsdPlayListDescriptor *d.
+ Time is in 1/15-second units.
+*/
+uint16_t
+vcdinf_get_play_time (const PsdPlayListDescriptor_t *d)
+{
+ if (NULL==d) return 0;
+ return uint16_from_be (d->ptime);
+}
+
+/*!
+ Return number of bytes in PSD.
+*/
+uint32_t
+vcdinf_get_psd_size (const InfoVcd_t *info)
+{
+ if (NULL==info) return 0;
+ return uint32_from_be (info->psd_size);
+}
+
+/*!
+ Get timeout wait time value for PsdPlayListDescriptor *d.
+ Return VCDINFO_INVALID_OFFSET if d is NULL;
+ Time is in seconds unless it is -1 (unlimited).
+*/
+uint16_t
+vcdinf_get_timeout_offset (const PsdSelectionListDescriptor_t *d)
+{
+ if (NULL == d) return VCDINFO_INVALID_OFFSET;
+ return uint16_from_be (d->timeout_ofs);
+}
+
+/*!
+ Get timeout wait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+*/
+int
+vcdinf_get_timeout_time (const PsdSelectionListDescriptor_t *d)
+{
+ return vcdinfo_get_wait_time (d->totime);
+}
+
+/*!
+ Return the track number for entry n in obj. The first track starts
+ at 1. Note this is one less than the track number reported in vcddump.
+ (We don't count the header track?)
+*/
+track_t
+vcdinf_get_track(const EntriesVcd_t *entries, const unsigned int entry_num)
+{
+ const unsigned int entry_count = uint16_from_be (entries->entry_count);
+ /* Note entry_num is 0 origin. */
+ return entry_num < entry_count ?
+ cdio_from_bcd8 (entries->entry[entry_num].n):
+ VCDINFO_INVALID_TRACK;
+}
+
+/*!
+ Return the VCD volume count - the number of CD's in the collection.
+*/
+unsigned int
+vcdinf_get_volume_count(const InfoVcd_t *info)
+{
+ if (NULL==info) return 0;
+ return(uint16_from_be( info->vol_count));
+}
+
+/*!
+ Return the VCD volume num - the number of the CD in the collection.
+ This is a number between 1 and the volume count.
+*/
+unsigned int
+vcdinf_get_volume_num(const InfoVcd_t *info)
+{
+ if (NULL == info) return 0;
+ return uint16_from_be(info->vol_id);
+}
+
+/*!
+ Get wait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+*/
+int
+vcdinf_get_wait_time (const PsdPlayListDescriptor_t *d)
+{
+ return vcdinfo_get_wait_time (d->wtime);
+}
+
+/*!
+ Return true if loop has a jump delay
+*/
+bool
+vcdinf_has_jump_delay (const PsdSelectionListDescriptor_t *psd)
+{
+ if (NULL==psd) return false;
+ return ((0x80 & psd->loop) != 0);
+}
+
+/*!
+ Comparison routine used in sorting. We compare LIDs and if those are
+ equal, use the offset.
+ Note: we assume an unassigned LID is 0 and this compares as a high value.
+
+ NOTE: Consider making static.
+*/
+int
+vcdinf_lid_t_cmp (vcdinfo_offset_t *a, vcdinfo_offset_t *b)
+{
+ if (a->lid && b->lid)
+ {
+ if (a->lid > b->lid) return +1;
+ if (a->lid < b->lid) return -1;
+ vcd_warn ("LID %d at offset %d has same nunber as LID of offset %d",
+ a->lid, a->offset, b->offset);
+ }
+ else if (a->lid) return -1;
+ else if (b->lid) return +1;
+
+ /* Failed to sort on LID, try offset now. */
+
+ if (a->offset > b->offset) return +1;
+ if (a->offset < b->offset) return -1;
+
+ /* LIDS and offsets are equal. */
+ return 0;
+}
+
+/* Get the LID from a given play-list descriptor.
+ VCDINFO_REJECTED_MASK is returned d on error or pld is NULL.
+*/
+lid_t
+vcdinf_pld_get_lid(const PsdPlayListDescriptor_t *pld)
+{
+ return (pld != NULL)
+ ? uint16_from_be (pld->lid) & VCDINFO_LID_MASK
+ : VCDINFO_REJECTED_MASK;
+}
+
+/**
+ \fn vcdinfo_pld_get_next_offset(const PsdPlayListDescriptor *pld);
+ \brief Get next offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no "next"
+ entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_pld_get_next_offset(const PsdPlayListDescriptor_t *pld)
+{
+ if (NULL == pld) return VCDINFO_INVALID_OFFSET;
+ return uint16_from_be (pld->next_ofs);
+}
+
+/*!
+ Return number of items in LIDs. Return 0 if error or not found.
+*/
+int
+vcdinf_pld_get_noi (const PsdPlayListDescriptor_t *pld)
+{
+ if ( NULL == pld ) return 0;
+ return pld->noi;
+}
+
+/*!
+ Return the playlist item i in d.
+*/
+uint16_t
+vcdinf_pld_get_play_item(const PsdPlayListDescriptor_t *pld, unsigned int i)
+{
+ if (NULL==pld) return 0;
+ return uint16_from_be(pld->itemid[i]);
+}
+
+/**
+ \fn vcdinf_pld_get_prev_offset(const PsdPlayListDescriptor *pld);
+ \brief Get prev offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no "prev"
+ entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_pld_get_prev_offset(const PsdPlayListDescriptor_t *pld)
+{
+ return (pld != NULL) ?
+ uint16_from_be (pld->prev_ofs) : VCDINFO_INVALID_OFFSET;
+}
+
+/**
+ \fn vcdinf_pld_get_return_offset(const PsdPlayListDescriptor *pld);
+ \brief Get return offset for a given PLD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "return" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_pld_get_return_offset(const PsdPlayListDescriptor_t *pld)
+{
+ return (pld != NULL) ?
+ uint16_from_be (pld->return_ofs) : VCDINFO_INVALID_OFFSET;
+}
+
+/**
+ * \fn vcdinfo_psd_get_default_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get next offset for a given PSD selector descriptor.
+ * \return VCDINFO_INVALID_OFFSET is returned on error or if psd is
+ * NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_default_offset(const PsdSelectionListDescriptor_t *psd)
+{
+ if (NULL == psd) return VCDINFO_INVALID_OFFSET;
+ return uint16_from_be (psd->default_ofs);
+}
+
+/*!
+ Get the item id for a given selection-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or if psd is NULL.
+*/
+uint16_t
+vcdinf_psd_get_itemid(const PsdSelectionListDescriptor_t *psd)
+{
+ return (psd != NULL) ? uint16_from_be(psd->itemid) : VCDINFO_REJECTED_MASK;
+}
+
+/*!
+ Get the LID from a given selection-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or psd is NULL.
+*/
+lid_t
+vcdinf_psd_get_lid(const PsdSelectionListDescriptor_t *psd)
+{
+ return (psd != NULL)
+ ? uint16_from_be (psd->lid) & VCDINFO_LID_MASK
+ : VCDINFO_REJECTED_MASK;
+}
+
+/*!
+ Get the LID rejected status for a given PSD selector descriptor.
+ true is also returned d is NULL.
+*/
+bool
+vcdinf_psd_get_lid_rejected(const PsdSelectionListDescriptor_t *psd)
+{
+ return (psd != NULL)
+ ? vcdinfo_is_rejected(uint16_from_be(psd->lid))
+ : true;
+}
+
+/**
+ * \fn vcdinf_psd_get_next_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get "next" offset for a given PSD selector descriptor.
+ * \return VCDINFO_INVALID_OFFSET is returned on error or if psd is
+ * NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_next_offset(const PsdSelectionListDescriptor_t *psd)
+{
+ if (NULL == psd) return VCDINFO_INVALID_OFFSET;
+ return uint16_from_be (psd->next_ofs);
+}
+
+/**
+ * \fn vcdinf_psd_get_offset(const PsdSelectionListDescriptor *d,
+ * unsigned int entry_num);
+ * \brief Get offset entry_num for a given PSD selector descriptor.
+ * \return VCDINFO_INVALID_OFFSET is returned if d on error or d is
+ * NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_offset(const PsdSelectionListDescriptor_t *psd,
+ unsigned int entry_num)
+{
+ return (psd != NULL && entry_num < vcdinf_get_num_selections(psd))
+ ? uint16_from_be (psd->ofs[entry_num]) : VCDINFO_INVALID_OFFSET;
+}
+
+/**
+ \fn vcdinf_psd_get_prev_offset(const PsdSelectionListDescriptor *psd);
+ \brief Get "prev" offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no "prev"
+ entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_prev_offset(const PsdSelectionListDescriptor_t *psd)
+{
+ return (psd != NULL) ?
+ uint16_from_be (psd->prev_ofs) : VCDINFO_INVALID_OFFSET;
+}
+
+/**
+ * \fn vcdinf_psd_get_return_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get return offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no
+ "return" entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_return_offset(const PsdSelectionListDescriptor_t *psd)
+{
+ return (psd != NULL) ?
+ uint16_from_be (psd->return_ofs) : VCDINFO_INVALID_OFFSET;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/info.c b/contrib/libvcd/info.c
new file mode 100644
index 000000000..b01bd6eee
--- /dev/null
+++ b/contrib/libvcd/info.c
@@ -0,0 +1,2103 @@
+/*
+ $Id: info.c,v 1.8 2007/03/23 21:47:31 dsalt Exp $
+
+ Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*
+ Things here refer to higher-level structures usually accessed via
+ vcdinfo_t. For lower-level access which generally use
+ structures other than vcdinfo_t, see inf.c
+*/
+
+
+/* Private headers */
+#include "info_private.h"
+#include "vcd_assert.h"
+#include "pbc.h"
+#include "util.h"
+#include "vcd_read.h"
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/bytesex.h>
+#include <cdio/cd_types.h>
+#include <cdio/util.h>
+
+/* Eventually move above libvcd includes but having vcdinfo including. */
+#include <libvcd/info.h>
+
+#include <stdio.h>
+#include <stddef.h>
+#include <errno.h>
+
+static const char _rcsid[] = "$Id: info.c,v 1.8 2007/03/23 21:47:31 dsalt Exp $";
+
+#define BUF_COUNT 16
+#define BUF_SIZE 80
+
+/* Return a pointer to a internal free buffer */
+static char *
+_getbuf (void)
+{
+ static char _buf[BUF_COUNT][BUF_SIZE];
+ static int _num = -1;
+
+ _num++;
+ _num %= BUF_COUNT;
+
+ memset (_buf[_num], 0, BUF_SIZE);
+
+ return _buf[_num];
+}
+
+/*
+ Initialize/allocate segment portion of vcdinfo_obj_t.
+
+ Getting exact segments sizes is done in a rather complicated way.
+ A simple approach would be to use the fixed size allocated on disk,
+ but players have trouble with the internal fragmentation padding.
+ More accurate results are obtained by consulting with ISO 9660
+ information for the corresponding file entry.
+
+ Another approach to get segment sizes is to read/scan the
+ MPEGs. That would be rather slow.
+*/
+static void
+_init_segments (vcdinfo_obj_t *obj)
+{
+ InfoVcd_t *info = vcdinfo_get_infoVcd(obj);
+ segnum_t num_segments = vcdinfo_get_num_segments(obj);
+ CdioListNode *entnode;
+ CdioList *entlist;
+ int i;
+ lsn_t last_lsn=0;
+
+ obj->first_segment_lsn = cdio_msf_to_lsn(&info->first_seg_addr);
+ obj->seg_sizes = _vcd_malloc( num_segments * sizeof(uint32_t *));
+
+ if (NULL == obj->seg_sizes || 0 == num_segments) return;
+
+ entlist = iso9660_fs_readdir(obj->img, "SEGMENT", true);
+
+ i=0;
+ _CDIO_LIST_FOREACH (entnode, entlist) {
+ iso9660_stat_t *statbuf = _cdio_list_node_data (entnode);
+
+ if (statbuf->type == _STAT_DIR) continue;
+
+ while(info->spi_contents[i].item_cont) {
+ obj->seg_sizes[i] = VCDINFO_SEGMENT_SECTOR_SIZE;
+ i++;
+ }
+
+ /* Should have an entry in the ISO 9660 filesystem. Get and save
+ in statbuf.secsize this size.
+ */
+ obj->seg_sizes[i] = statbuf->secsize;
+
+ if (last_lsn >= statbuf->lsn)
+ vcd_warn ("Segments if ISO 9660 directory out of order lsn %ul >= %ul",
+ (unsigned int) last_lsn, (unsigned int) statbuf->lsn);
+ last_lsn = statbuf->lsn;
+
+ i++;
+ }
+
+ while(i < num_segments && info->spi_contents[i].item_cont) {
+ obj->seg_sizes[i] = VCDINFO_SEGMENT_SECTOR_SIZE;
+ i++;
+ }
+
+ if (i != num_segments)
+ vcd_warn ("Number of segments found %d is not number of segments %d",
+ i, num_segments);
+
+ _cdio_list_free (entlist, true);
+
+
+#if 0
+ /* Figure all of the segment sector sizes */
+ for (i=0; i < num_segments; i++) {
+
+ obj->seg_sizes[i] = VCDINFO_SEGMENT_SECTOR_SIZE;
+
+ if ( !info->spi_contents[i].item_cont ) {
+ /* Should have an entry in the ISO 9660 filesystem. Get and save
+ in statbuf.secsize this size.
+ */
+ lsn_t lsn = vcdinfo_get_seg_lsn(obj, i);
+ iso9660_stat_t *statbuf =iso9660_find_fs_lsn(obj->img, lsn);
+ if (NULL != statbuf) {
+ obj->seg_sizes[i] = statbuf->secsize;
+ free(statbuf);
+ } else {
+ vcd_warn ("Trouble finding ISO 9660 size for segment %d.", i);
+ }
+ }
+ }
+#endif
+
+}
+
+/*!
+ Return the number of audio channels implied by "audio_type".
+ 0 is returned on error.
+*/
+unsigned int
+vcdinfo_audio_type_num_channels(const vcdinfo_obj_t *obj,
+ unsigned int audio_type)
+{
+ const int audio_types[2][5] =
+ {
+ { /* VCD 2.0 */
+ 0, /* no audio*/
+ 1, /* single channel */
+ 1, /* stereo */
+ 2, /* dual channel */
+ 0}, /* error */
+
+ { /* SVCD, HQVCD */
+ 0, /* no stream */
+ 1, /* 1 stream */
+ 2, /* 2 streams */
+ 1, /* 1 multi-channel stream (surround sound) */
+ 0} /* error */
+ };
+
+ /* We should also check that the second index is in range too. */
+ if (audio_type > 4) {
+ return 0;
+ }
+
+ /* Get first index entry into above audio_type array from vcd_type */
+ switch (obj->vcd_type) {
+
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ return 1;
+
+ case VCD_TYPE_VCD2:
+ return 3;
+ break;
+
+ case VCD_TYPE_HQVCD:
+ case VCD_TYPE_SVCD:
+ return audio_types[1][audio_type];
+ break;
+
+ case VCD_TYPE_INVALID:
+ default:
+ /* We have an invalid entry. Set to handle below. */
+ return 0;
+ }
+}
+
+/*!
+ Return a string describing an audio type.
+*/
+const char *
+vcdinfo_audio_type2str(const vcdinfo_obj_t *obj, unsigned int audio_type)
+{
+ const char *audio_types[3][5] =
+ {
+ /* INVALID, VCD 1.0, or VCD 1.1 */
+ { "unknown", "invalid", "", "", "" },
+
+ /*VCD 2.0 */
+ { "no audio", "single channel", "stereo", "dual channel", "error" },
+
+ /* SVCD, HQVCD */
+ { "no stream", "1 stream", "2 streams",
+ "1 multi-channel stream (surround sound)", "error"},
+ };
+
+ unsigned int first_index = 0;
+
+ /* Get first index entry into above audio_type array from vcd_type */
+ switch (obj->vcd_type) {
+
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ first_index=1;
+ break;
+
+ case VCD_TYPE_HQVCD:
+ case VCD_TYPE_SVCD:
+ first_index=2;
+ break;
+
+ case VCD_TYPE_INVALID:
+ default:
+ /* We have an invalid entry. Set to handle below. */
+ audio_type=4;
+ }
+
+ /* We should also check that the second index is in range too. */
+ if (audio_type > 3) {
+ first_index=0;
+ audio_type=1;
+ }
+
+ return audio_types[first_index][audio_type];
+}
+
+/*!
+ Note first seg_num is 0!
+*/
+const char *
+vcdinfo_ogt2str(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ const InfoVcd_t *info = &obj->info;
+ const char *ogt_str[] =
+ {
+ "None",
+ "1 available",
+ "0 & 1 available",
+ "all 4 available"
+ };
+
+ return ogt_str[info->spi_contents[seg_num].ogt];
+}
+
+
+const char *
+vcdinfo_video_type2str(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ const char *video_types[] =
+ {
+ "no stream",
+ "NTSC still",
+ "NTSC still (lo+hires)",
+ "NTSC motion",
+ "reserved (0x4)",
+ "PAL still",
+ "PAL still (lo+hires)",
+ "PAL motion",
+ "INVALID ENTRY"
+ };
+
+ return video_types[vcdinfo_get_video_type(obj, seg_num)];
+}
+
+/*!
+ \brief Classify itemid_num into the kind of item it is: track #, entry #,
+ segment #.
+ \param itemid is set to contain this classification an the converted
+ entry number.
+*/
+void
+vcdinfo_classify_itemid (uint16_t itemid_num,
+ /*out*/ vcdinfo_itemid_t *itemid)
+{
+
+ itemid->num = itemid_num;
+ if (itemid_num < 2)
+ itemid->type = VCDINFO_ITEM_TYPE_NOTFOUND;
+ else if (itemid_num < MIN_ENCODED_TRACK_NUM) {
+ itemid->type = VCDINFO_ITEM_TYPE_TRACK;
+ itemid->num--;
+ } else if (itemid_num < 600) {
+ itemid->type = VCDINFO_ITEM_TYPE_ENTRY;
+ itemid->num -= MIN_ENCODED_TRACK_NUM;
+ } else if (itemid_num < MIN_ENCODED_SEGMENT_NUM)
+ itemid->type = VCDINFO_ITEM_TYPE_LID;
+ else if (itemid_num <= MAX_ENCODED_SEGMENT_NUM) {
+ itemid->type = VCDINFO_ITEM_TYPE_SEGMENT;
+ itemid->num -= (MIN_ENCODED_SEGMENT_NUM);
+ } else
+ itemid->type = VCDINFO_ITEM_TYPE_SPAREID2;
+}
+
+const char *
+vcdinfo_pin2str (uint16_t itemid_num)
+{
+ char *buf = _getbuf ();
+ vcdinfo_itemid_t itemid;
+
+ vcdinfo_classify_itemid(itemid_num, &itemid);
+ strcpy (buf, "??");
+
+ switch(itemid.type) {
+ case VCDINFO_ITEM_TYPE_NOTFOUND:
+ snprintf (buf, BUF_SIZE, "play nothing (0x%4.4x)", itemid.num);
+ break;
+ case VCDINFO_ITEM_TYPE_TRACK:
+ snprintf (buf, BUF_SIZE, "SEQUENCE[%d] (0x%4.4x)", itemid.num-1,
+ itemid_num);
+ break;
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ snprintf (buf, BUF_SIZE, "ENTRY[%d] (0x%4.4x)", itemid.num, itemid_num);
+ break;
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ snprintf (buf, BUF_SIZE, "SEGMENT[%d] (0x%4.4x)", itemid.num, itemid_num);
+ break;
+ case VCDINFO_ITEM_TYPE_LID:
+ snprintf (buf, BUF_SIZE, "spare id (0x%4.4x)", itemid.num);
+ break;
+ case VCDINFO_ITEM_TYPE_SPAREID2:
+ snprintf (buf, BUF_SIZE, "spare id2 (0x%4.4x)", itemid.num);
+ break;
+ }
+
+ return buf;
+}
+
+/*!
+ Return a string containing the VCD album id, or NULL if there is
+ some problem in getting this.
+*/
+const char *
+vcdinfo_get_album_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return vcdinf_get_album_id(&obj->info);
+}
+
+/*!
+ Return the VCD ID.
+ NULL is returned if there is some problem in getting this.
+*/
+char *
+vcdinfo_get_application_id(vcdinfo_obj_t *p_obj)
+{
+ if ( NULL == p_obj ) return (NULL);
+ return iso9660_get_application_id(&p_obj->pvd);
+}
+
+/*!
+ Return a pointer to the cdio structure for the CD image opened or
+ NULL if error.
+*/
+CdIo *
+vcdinfo_get_cd_image (const vcdinfo_obj_t *vcd_obj)
+{
+ if (NULL == vcd_obj) return NULL;
+ return vcd_obj->img;
+}
+
+
+/*!
+ \fn vcdinfo_selection_get_lid(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int selection);
+
+ \brief Get offset of a selection for a given lid.
+
+ Return the LID offset associated with a the selection number of the
+ passed-in LID parameter.
+
+ \return VCDINFO_INVALID_LID is returned if obj on error or obj
+ is NULL. Otherwise the LID offset is returned.
+*/
+lid_t vcdinfo_selection_get_lid(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int selection)
+{
+ unsigned int offset;
+
+ if (NULL == obj) return VCDINFO_INVALID_LID;
+
+ offset = vcdinfo_selection_get_offset(obj, lid, selection);
+ switch (offset) {
+ case VCDINFO_INVALID_OFFSET:
+ case PSD_OFS_MULTI_DEF:
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ return VCDINFO_INVALID_LID;
+ default:
+ {
+ vcdinfo_offset_t *ofs = vcdinfo_get_offset_t(obj, offset);
+ return ofs->lid;
+ }
+ }
+}
+
+/*!
+ \fn vcdinfo_selection_get_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int selection);
+
+ \brief Get offset of a selection for a given lid.
+
+ Return the LID offset associated with a the selection number of the
+ passed-in LID parameter.
+
+ \return VCDINFO_INVALID_OFFSET is returned if error, obj is NULL or
+ the lid is not some type of selection list. Otherwise the LID offset
+ is returned.
+*/
+uint16_t vcdinfo_selection_get_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int selection)
+{
+ unsigned int bsn;
+
+ PsdListDescriptor_t pxd;
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+ if (pxd.descriptor_type != PSD_TYPE_SELECTION_LIST &&
+ pxd.descriptor_type != PSD_TYPE_EXT_SELECTION_LIST) {
+ vcd_warn( "Requesting selection of LID %i which not a selection list -"
+ " type is 0x%x",
+ lid, pxd.descriptor_type );
+ return VCDINFO_INVALID_OFFSET;
+ }
+
+ bsn=vcdinf_get_bsn(pxd.psd);
+
+ if ( (selection - bsn + 1) > 0) {
+ return vcdinfo_lid_get_offset(obj, lid, selection-bsn+1);
+ } else {
+ vcd_warn( "Selection number %u too small. bsn %u", selection, bsn );
+ return VCDINFO_INVALID_OFFSET;
+ }
+}
+
+/**
+ \fn vcdinfo_get_default_offset(const vcdinfo_obj_t *obj, unsinged int lid);
+ \brief Get return offset for a given PLD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "return" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinfo_get_default_offset(const vcdinfo_obj_t *obj, lid_t lid)
+{
+ if (NULL != obj) {
+
+ PsdListDescriptor_t pxd;
+
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+
+ switch (pxd.descriptor_type) {
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ case PSD_TYPE_SELECTION_LIST:
+ return vcdinf_psd_get_default_offset(pxd.psd);
+ break;
+ case PSD_TYPE_PLAY_LIST:
+ case PSD_TYPE_END_LIST:
+ case PSD_TYPE_COMMAND_LIST:
+ break;
+ }
+ }
+ return VCDINFO_INVALID_OFFSET;
+}
+
+/*!
+ \brief Get default or multi-default LID.
+
+ Return the LID offset associated with a the "default" entry of the
+ passed-in LID parameter. Note "default" entries are associated
+ with PSDs that are (extended) selection lists. If the "default"
+ offset is a multi-default, we use entry_num to find the proper
+ "default" LID. Otherwise this routine is exactly like
+ vcdinfo_get_default_lid with the exception of requiring an
+ additional "entry_num" parameter.
+
+ \return VCDINFO_INVALID_LID is returned on error, or if the LID
+ is not a selection list or no "default" entry. Otherwise the LID
+ offset is returned.
+*/
+lid_t
+vcdinfo_get_multi_default_lid(const vcdinfo_obj_t *obj, lid_t lid,
+ lsn_t lsn)
+{
+ unsigned int offset;
+ unsigned int entry_num;
+
+ entry_num = vcdinfo_lsn_get_entry(obj, lsn);
+ offset = vcdinfo_get_multi_default_offset(obj, lid, entry_num);
+
+ switch (offset) {
+ case VCDINFO_INVALID_OFFSET:
+ case PSD_OFS_MULTI_DEF:
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ return VCDINFO_INVALID_LID;
+ default:
+ {
+ vcdinfo_offset_t *ofs = vcdinfo_get_offset_t(obj, offset);
+ return ofs->lid;
+ }
+ }
+}
+
+/*!
+ \brief Get default or multi-default LID offset.
+
+ Return the LID offset associated with a the "default" entry of the
+ passed-in LID parameter. Note "default" entries are associated
+ with PSDs that are (extended) selection lists. If the "default"
+ offset is a multi-default, we use entry_num to find the proper
+ "default" offset. Otherwise this routine is exactly like
+ vcdinfo_get_default_offset with the exception of requiring an
+ additional "entry_num" parameter.
+
+ \return VCDINFO_INVALID_OFFSET is returned on error, or if the LID
+ is not a selection list or no "default" entry. Otherwise the LID
+ offset is returned.
+*/
+uint16_t
+vcdinfo_get_multi_default_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int entry_num)
+{
+ uint16_t offset=vcdinfo_get_default_offset(obj, lid);
+
+ switch (offset) {
+ case PSD_OFS_MULTI_DEF:
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ {
+ /* Have some work todo... Figure the selection number. */
+ PsdListDescriptor_t pxd;
+
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+
+ switch (pxd.descriptor_type) {
+
+ case PSD_TYPE_SELECTION_LIST:
+ case PSD_TYPE_EXT_SELECTION_LIST: {
+ vcdinfo_itemid_t selection_itemid;
+ uint16_t selection_itemid_num;
+ unsigned int start_entry_num;
+
+ if (pxd.psd == NULL) return VCDINFO_INVALID_OFFSET;
+ selection_itemid_num = vcdinf_psd_get_itemid(pxd.psd);
+ vcdinfo_classify_itemid(selection_itemid_num, &selection_itemid);
+ if (selection_itemid.type != VCDINFO_ITEM_TYPE_TRACK) {
+ return VCDINFO_INVALID_OFFSET;
+ }
+
+ start_entry_num = vcdinfo_track_get_entry(obj, selection_itemid.num);
+ return vcdinfo_selection_get_offset(obj, lid,
+ entry_num-start_entry_num);
+ }
+ default: ;
+ }
+ }
+ default:
+ return VCDINFO_INVALID_OFFSET;
+ }
+}
+
+/*!
+ Return a string containing the default VCD device if none is specified.
+ Return NULL we can't get this information.
+*/
+char *
+vcdinfo_get_default_device (const vcdinfo_obj_t *vcd_obj)
+{
+
+ /* If device not already open, then we'll open it temporarily and
+ let CdIo select a driver, get the default for that and then
+ close/destroy the temporary we created.
+ */
+ CdIo *cdio=NULL;
+ if (vcd_obj != NULL && vcd_obj->img != NULL)
+ cdio = vcd_obj->img;
+
+ return cdio_get_default_device(cdio);
+}
+
+/*!
+ Return number of sector units in of an entry. 0 is returned if entry_num
+ is out of range.
+ The first entry number is 0.
+*/
+uint32_t
+vcdinfo_get_entry_sect_count (const vcdinfo_obj_t *obj, unsigned int entry_num)
+{
+ const EntriesVcd_t *entries = &obj->entries;
+ const unsigned int entry_count = vcdinf_get_num_entries(entries);
+ if (entry_num > entry_count)
+ return 0;
+ else {
+ const lsn_t this_lsn = vcdinfo_get_entry_lsn(obj, entry_num);
+ lsn_t next_lsn;
+ if (entry_num < entry_count-1) {
+ track_t track=vcdinfo_get_track(obj, entry_num);
+ track_t next_track=vcdinfo_get_track(obj, entry_num+1);
+ next_lsn = vcdinfo_get_entry_lsn(obj, entry_num+1);
+ /* If we've changed tracks, don't include pregap sector between
+ tracks.
+ */
+ if (track != next_track) next_lsn -= CDIO_PREGAP_SECTORS;
+ } else {
+ /* entry_num == entry_count -1. Or the last entry.
+ This is really really ugly. There's probably a better
+ way to do it.
+ Below we get the track of the current entry and then the LBA of the
+ beginning of the following (leadout?) track.
+
+ Wait! It's uglier than that! Since VCD's can be created
+ *without* a pregap to the leadout track, we try not to use
+ that if we can get the entry from the ISO 9660 filesystem.
+ */
+ track_t track = vcdinfo_get_track(obj, entry_num);
+ if (track != VCDINFO_INVALID_TRACK) {
+ iso9660_stat_t *statbuf;
+ const lsn_t lsn = vcdinfo_get_track_lsn(obj, track);
+
+ /* Try to get the sector count from the ISO 9660 filesystem */
+ statbuf = iso9660_find_fs_lsn(obj->img, lsn);
+
+ if (NULL != statbuf) {
+ next_lsn = lsn + statbuf->secsize;
+ free(statbuf);
+ } else {
+ /* Failed on ISO 9660 filesystem. Use next track or
+ LEADOUT track. */
+ next_lsn = vcdinfo_get_track_lsn(obj, track+1);
+ }
+ if (next_lsn == VCDINFO_NULL_LSN)
+ return 0;
+ } else {
+ /* Something went wrong. Set up size to zero. */
+ return 0;
+ }
+ }
+ return (next_lsn - this_lsn);
+ }
+}
+
+/*! Return the starting MSF (minutes/secs/frames) for sequence
+ entry_num in obj. NULL is returned if there is no entry.
+ The first entry number is 0.
+*/
+const msf_t *
+vcdinfo_get_entry_msf(const vcdinfo_obj_t *obj, unsigned int entry_num)
+{
+ const EntriesVcd_t *entries = &obj->entries;
+ return vcdinf_get_entry_msf(entries, entry_num);
+}
+
+/*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+*/
+lba_t
+vcdinfo_get_entry_lba(const vcdinfo_obj_t *obj, unsigned int entry_num)
+{
+ if ( NULL == obj ) return VCDINFO_NULL_LBA;
+ else {
+ const msf_t *msf = vcdinfo_get_entry_msf(obj, entry_num);
+ msf = vcdinfo_get_entry_msf(obj, entry_num);
+ return (msf != NULL) ? cdio_msf_to_lba(msf) : VCDINFO_NULL_LBA;
+ }
+}
+
+/*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+*/
+lsn_t
+vcdinfo_get_entry_lsn(const vcdinfo_obj_t *obj, unsigned int entry_num)
+{
+ if ( NULL == obj ) return VCDINFO_NULL_LBA;
+ else {
+ const msf_t *msf = vcdinfo_get_entry_msf(obj, entry_num);
+ return (msf != NULL) ? cdio_msf_to_lsn(msf) : VCDINFO_NULL_LSN;
+ }
+}
+
+EntriesVcd_t *
+vcdinfo_get_entriesVcd (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return &obj->entries;
+}
+
+/*!
+ Get the VCD format (VCD 1.0 VCD 1.1, SVCD, ... for this object.
+ The type is also set inside obj.
+*/
+vcd_type_t
+vcdinfo_get_format_version (vcdinfo_obj_t *obj)
+{
+ return obj->vcd_type;
+}
+
+/*!
+ Return a string giving VCD format (VCD 1.0 VCD 1.1, SVCD, ...
+ for this object.
+*/
+const char *
+vcdinfo_get_format_version_str (const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return "*Uninitialized*";
+ return vcdinf_get_format_version_str(obj->vcd_type);
+}
+
+InfoVcd_t *
+vcdinfo_get_infoVcd (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return &obj->info;
+}
+
+/*! Return the entry number closest and before the given LSN.
+ */
+unsigned int
+vcdinfo_lsn_get_entry(const vcdinfo_obj_t *obj, lsn_t lsn)
+{
+
+ /* Do a binary search to find the entry. */
+ unsigned int i = 0;
+ unsigned int j = vcdinfo_get_num_entries(obj);
+ unsigned int mid;
+ unsigned int mid_lsn;
+ do {
+ mid = (i+j)/2;
+ mid_lsn = vcdinfo_get_entry_lsn(obj, mid);
+ if ( lsn <= mid_lsn ) j = mid-1;
+ if ( lsn >= mid_lsn ) i = mid+1;
+ } while (i <= j);
+
+ /* We want the entry closest but before. */
+ return (lsn == mid_lsn) ? mid : mid-1;
+}
+
+
+void *
+vcdinfo_get_pvd (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return &obj->pvd;
+}
+
+void *
+vcdinfo_get_scandata (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->scandata_buf;
+}
+
+void *
+vcdinfo_get_searchDat (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->search_buf;
+}
+
+void *
+vcdinfo_get_tracksSVD (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->tracks_buf;
+}
+
+/*!
+ Get the itemid for a given list ID.
+ VCDINFO_REJECTED_MASK is returned on error or if obj is NULL.
+*/
+uint16_t
+vcdinfo_lid_get_itemid(const vcdinfo_obj_t *obj, lid_t lid)
+{
+ PsdListDescriptor_t pxd;
+
+ if (obj == NULL) return VCDINFO_REJECTED_MASK;
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+ switch (pxd.descriptor_type) {
+ case PSD_TYPE_SELECTION_LIST:
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ if (pxd.psd == NULL) return VCDINFO_REJECTED_MASK;
+ return vcdinf_psd_get_itemid(pxd.psd);
+ break;
+ case PSD_TYPE_PLAY_LIST:
+ /* FIXME: There is an array of items */
+ case PSD_TYPE_END_LIST:
+ case PSD_TYPE_COMMAND_LIST:
+ return VCDINFO_REJECTED_MASK;
+ }
+
+ return VCDINFO_REJECTED_MASK;
+
+}
+
+/*!
+ Get the LOT pointer.
+*/
+LotVcd_t *
+vcdinfo_get_lot(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->lot;
+}
+
+/*!
+ Get the extended LOT pointer.
+*/
+LotVcd_t *
+vcdinfo_get_lot_x(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->lot_x;
+}
+
+/*!
+ Return number of LIDs.
+*/
+lid_t
+vcdinfo_get_num_LIDs (const vcdinfo_obj_t *obj)
+{
+ /* Should probably use _vcd_pbc_max_lid instead? */
+ if (NULL==obj) return 0;
+ return vcdinf_get_num_LIDs(&obj->info);
+}
+
+/*!
+ Return the number of entries in the VCD.
+*/
+unsigned int
+vcdinfo_get_num_entries(const vcdinfo_obj_t *obj)
+{
+ const EntriesVcd_t *entries = &obj->entries;
+ return vcdinf_get_num_entries(entries);
+}
+
+/*!
+ Return the number of segments in the VCD. Return 0 if there is some
+ problem.
+*/
+segnum_t
+vcdinfo_get_num_segments(const vcdinfo_obj_t *obj)
+{
+ if (NULL==obj) return 0;
+ return vcdinf_get_num_segments(&obj->info);
+}
+
+/*!
+ \fn vcdinfo_get_offset_lid(const vcdinfo_obj_t *obj, unsigned int entry_num);
+ \brief Get offset entry_num for a given LID.
+ \return VCDINFO_INVALID_OFFSET is returned if obj on error or obj
+ is NULL. Otherwise the LID offset is returned.
+*/
+uint16_t
+vcdinfo_lid_get_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int entry_num)
+{
+ PsdListDescriptor_t pxd;
+
+ if (obj == NULL) return VCDINFO_INVALID_OFFSET;
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+
+ switch (pxd.descriptor_type) {
+ case PSD_TYPE_SELECTION_LIST:
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ if (pxd.psd == NULL) return VCDINFO_INVALID_OFFSET;
+ return vcdinf_psd_get_offset(pxd.psd, entry_num-1);
+ break;
+ case PSD_TYPE_PLAY_LIST:
+ /* FIXME: There is an array of items */
+ case PSD_TYPE_END_LIST:
+ case PSD_TYPE_COMMAND_LIST:
+ return VCDINFO_INVALID_OFFSET;
+ }
+ return VCDINFO_INVALID_OFFSET;
+
+}
+
+/*!
+ NULL is returned on error.
+*/
+static vcdinfo_offset_t *
+_vcdinfo_get_offset_t (const vcdinfo_obj_t *obj, unsigned int offset, bool ext)
+{
+ CdioListNode *node;
+ CdioList *offset_list = ext ? obj->offset_x_list : obj->offset_list;
+
+ switch (offset) {
+ case PSD_OFS_DISABLED:
+ case PSD_OFS_MULTI_DEF:
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ return NULL;
+ default: ;
+ }
+
+ _CDIO_LIST_FOREACH (node, offset_list)
+ {
+ vcdinfo_offset_t *ofs = _cdio_list_node_data (node);
+ if (offset == ofs->offset)
+ return ofs;
+ }
+ return NULL;
+}
+
+/*!
+ Get the VCD info list.
+*/
+CdioList *
+vcdinfo_get_offset_list(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->offset_list;
+}
+
+
+/*!
+ Get the VCD info extended offset list.
+*/
+CdioList *
+vcdinfo_get_offset_x_list(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->offset_x_list;
+}
+
+/*!
+ Get the VCD info offset multiplier.
+*/
+unsigned int vcdinfo_get_offset_mult(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return 0xFFFF;
+ return obj->info.offset_mult;
+}
+
+/*!
+ Get entry in offset list for the item that has offset. This entry
+ has for example the LID. NULL is returned on error.
+*/
+vcdinfo_offset_t *
+vcdinfo_get_offset_t (const vcdinfo_obj_t *obj, unsigned int offset)
+{
+ vcdinfo_offset_t *off_p= _vcdinfo_get_offset_t (obj, offset, true);
+ if (NULL != off_p)
+ return off_p;
+ return _vcdinfo_get_offset_t (obj, offset, false);
+}
+
+/*!
+ Return a string containing the VCD publisher id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_preparer_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return iso9660_get_preparer_id(&obj->pvd);
+}
+
+/*!
+ Get the PSD.
+*/
+uint8_t *
+vcdinfo_get_psd(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return obj->psd;
+}
+
+/*!
+ Get the extended PSD.
+*/
+uint8_t *
+vcdinfo_get_psd_x(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return obj->psd_x;
+}
+
+/*!
+ Return number of bytes in PSD. Return 0 if there's an error.
+*/
+uint32_t
+vcdinfo_get_psd_size (const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return 0;
+ return vcdinf_get_psd_size(&obj->info);
+}
+
+/*!
+ Return number of bytes in the extended PSD. Return 0 if there's an error.
+*/
+uint32_t
+vcdinfo_get_psd_x_size (const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return 0;
+ return obj->psd_x_size;
+}
+
+/*!
+ Return a string containing the VCD publisher id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+*/
+char *
+vcdinfo_get_publisher_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return iso9660_get_publisher_id(&obj->pvd);
+}
+
+/*!
+ Get the PSD Selection List Descriptor for a given lid.
+ NULL is returned if error or not found.
+*/
+static bool
+_vcdinfo_lid_get_pxd(const vcdinfo_obj_t *obj, PsdListDescriptor_t *pxd,
+ uint16_t lid, bool ext)
+{
+ CdioListNode *node;
+ unsigned mult = obj->info.offset_mult;
+ const uint8_t *psd = ext ? obj->psd_x : obj->psd;
+ CdioList *offset_list = ext ? obj->offset_x_list : obj->offset_list;
+
+ if (offset_list == NULL) return false;
+
+ _CDIO_LIST_FOREACH (node, offset_list)
+ {
+ vcdinfo_offset_t *ofs = _cdio_list_node_data (node);
+ unsigned _rofs = ofs->offset * mult;
+
+ pxd->descriptor_type = psd[_rofs];
+
+ switch (pxd->descriptor_type)
+ {
+ case PSD_TYPE_PLAY_LIST:
+ {
+ pxd->pld = (PsdPlayListDescriptor_t *) (psd + _rofs);
+ if (vcdinf_pld_get_lid(pxd->pld) == lid) {
+ return true;
+ }
+ break;
+ }
+
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ case PSD_TYPE_SELECTION_LIST:
+ {
+ pxd->psd = (PsdSelectionListDescriptor_t *) (psd + _rofs);
+ if (vcdinf_psd_get_lid(pxd->psd) == lid) {
+ return true;
+ }
+ break;
+ }
+ default: ;
+ }
+ }
+ return false;
+}
+
+/*!
+ Get the PSD Selection List Descriptor for a given lid.
+ False is returned if not found.
+*/
+bool
+vcdinfo_lid_get_pxd(const vcdinfo_obj_t *obj, PsdListDescriptor_t *pxd,
+ uint16_t lid)
+{
+ if (_vcdinfo_lid_get_pxd(obj, pxd, lid, true))
+ return true;
+ return _vcdinfo_lid_get_pxd(obj, pxd, lid, false);
+}
+
+/**
+ \fn vcdinfo_get_return_offset(const vcdinfo_obj_t *obj);
+ \brief Get return offset for a given LID.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if LID has no
+ "return" entry. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinfo_get_return_offset(const vcdinfo_obj_t *obj, lid_t lid)
+{
+ if (NULL != obj) {
+
+ PsdListDescriptor_t pxd;
+
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+
+ switch (pxd.descriptor_type) {
+ case PSD_TYPE_PLAY_LIST:
+ return vcdinf_pld_get_return_offset(pxd.pld);
+ case PSD_TYPE_SELECTION_LIST:
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ return vcdinf_psd_get_return_offset(pxd.psd);
+ break;
+ case PSD_TYPE_END_LIST:
+ case PSD_TYPE_COMMAND_LIST:
+ break;
+ }
+ }
+
+ return VCDINFO_INVALID_OFFSET;
+}
+
+/*!
+ Return the audio type for a given segment.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+*/
+unsigned int
+vcdinfo_get_seg_audio_type(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if ( NULL == obj || NULL == &obj->info
+ || seg_num >= vcdinfo_get_num_segments(obj) )
+ return VCDINFO_INVALID_AUDIO_TYPE;
+ return(obj->info.spi_contents[seg_num].audio_type);
+}
+
+/*!
+ Return true if this segment is supposed to continue to the next one,
+ (is part of an "item" or listing in the ISO 9660 filesystem).
+*/
+bool
+vcdinfo_get_seg_continue(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if ( NULL == obj || NULL == &obj->info
+ || seg_num >= vcdinfo_get_num_segments(obj) )
+ return false;
+ return(obj->info.spi_contents[seg_num].item_cont);
+}
+
+/*! Return the starting LBA (logical block address) for segment
+ entry_num in obj. VCDINFO_LBA_NULL is returned if there is no entry.
+
+ Note first seg_num is 0.
+*/
+lba_t
+vcdinfo_get_seg_lba(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if (obj == NULL) return VCDINFO_NULL_LBA;
+ return cdio_lsn_to_lba(vcdinfo_get_seg_lba(obj, seg_num));
+}
+
+/*! Return the starting LBA (logical block address) for segment
+ entry_num in obj. VCDINFO_LSN_NULL is returned if there is no entry.
+
+ Note first seg_num is 0.
+*/
+lsn_t
+vcdinfo_get_seg_lsn(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if (obj == NULL || seg_num >= vcdinfo_get_num_segments(obj))
+ return VCDINFO_NULL_LSN;
+ return obj->first_segment_lsn + (VCDINFO_SEGMENT_SECTOR_SIZE * seg_num);
+}
+
+/*! Return the starting MSF (minutes/secs/frames) for segment
+ entry_num in obj. NULL is returned if there is no entry.
+
+ Note first seg_num is 0!
+*/
+const msf_t *
+vcdinfo_get_seg_msf(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if (obj == NULL || seg_num >= vcdinfo_get_num_segments(obj))
+ return NULL;
+ else {
+ lsn_t lsn = vcdinfo_get_seg_lsn(obj, seg_num);
+ static msf_t msf;
+ cdio_lsn_to_msf(lsn, &msf);
+ return &msf;
+ }
+}
+
+/*! Return the x-y resolution for a given segment.
+ Note first i_seg is 0.
+*/
+void
+vcdinfo_get_seg_resolution(const vcdinfo_obj_t *p_vcdinfo, segnum_t i_seg,
+ /*out*/ uint16_t *max_x, /*out*/ uint16_t *max_y)
+{
+ vcdinfo_video_segment_type_t segtype
+ = vcdinfo_get_video_type(p_vcdinfo, i_seg);
+ segnum_t i_segs = vcdinfo_get_num_segments(p_vcdinfo);
+
+ if (i_seg >= i_segs) return;
+
+ switch (segtype) {
+ case VCDINFO_FILES_VIDEO_NTSC_STILL:
+ *max_x = 704;
+ *max_y = 480;
+ break;
+ case VCDINFO_FILES_VIDEO_NTSC_STILL2:
+ *max_x = 352;
+ *max_y = 240;
+ break;
+ case VCDINFO_FILES_VIDEO_PAL_STILL:
+ *max_x = 704;
+ *max_y = 576;
+ break;
+ case VCDINFO_FILES_VIDEO_PAL_STILL2:
+ *max_x = 352;
+ *max_y = 288;
+ break;
+ default:
+ /* */
+ switch (vcdinfo_get_format_version(p_vcdinfo)) {
+ case VCD_TYPE_VCD:
+ *max_x = 352;
+ *max_y = 240;
+ break;
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ *max_x = 352;
+ switch(segtype) {
+ case VCDINFO_FILES_VIDEO_NTSC_MOTION:
+ *max_y = 240;
+ break;
+ case VCDINFO_FILES_VIDEO_PAL_MOTION:
+ *max_y = 288;
+ default:
+ *max_y = 289;
+ }
+ break;
+ default: ;
+ }
+ }
+}
+
+
+
+/*!
+ Return the number of sectors for segment
+ entry_num in obj. 0 is returned if there is no entry.
+
+ Use this routine to figure out the actual number of bytes a physical
+ region of a disk or CD takes up for a segment.
+
+ If an item has been broken up into a number of "continued" segments,
+ we will report the item size for the first segment and 0 for the
+ remaining ones. We may revisit this decision later.
+*/
+uint32_t
+vcdinfo_get_seg_sector_count(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if (obj == NULL || seg_num >= vcdinfo_get_num_segments(obj))
+ return 0;
+ return obj->seg_sizes[seg_num];
+}
+
+/*!
+ Return a string containing the VCD system id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_system_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj || NULL == &obj->pvd ) return (NULL);
+ return(iso9660_get_system_id(&obj->pvd));
+}
+
+/*!
+ Return the track number for entry n in obj.
+
+ In contrast to libcdio we start numbering at 0 which is the
+ ISO9660 and metadata information for the Video CD. Thus track
+ 1 is the first track the first complete MPEG track generally.
+*/
+track_t
+vcdinfo_get_track(const vcdinfo_obj_t *obj, const unsigned int entry_num)
+{
+ const EntriesVcd_t *entries = &obj->entries;
+ const unsigned int entry_count = vcdinf_get_num_entries(entries);
+ /* Note entry_num is 0 origin. */
+ return entry_num < entry_count ?
+ vcdinf_get_track(entries, entry_num)-1: VCDINFO_INVALID_TRACK;
+}
+
+/*!
+ Return the audio type for a given track.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+
+ Note: track 1 is usually the first track.
+*/
+unsigned int
+vcdinfo_get_track_audio_type(const vcdinfo_obj_t *obj, track_t track_num)
+{
+ TracksSVD *tracks;
+ TracksSVD2 *tracks2;
+ if ( NULL == obj || NULL == &obj->info ) return VCDINFO_INVALID_AUDIO_TYPE;
+ tracks = obj->tracks_buf;
+
+ if ( NULL == tracks ) return 0;
+ tracks2 = (TracksSVD2 *) &(tracks->playing_time[tracks->tracks]);
+ return(tracks2->contents[track_num-1].audio);
+}
+
+/*!
+ Return the highest track number in the current medium.
+
+ Because we track start numbering at 0 (which is the ISO 9660 track
+ containing Video CD naviagion and disk information), this is one
+ less than the number of tracks.
+
+ If there are no tracks, we return -1.
+*/
+unsigned int
+vcdinfo_get_num_tracks(const vcdinfo_obj_t *obj)
+{
+ if (obj == NULL || obj->img == NULL) return 0;
+
+ return cdio_get_num_tracks(obj->img)-1;
+}
+
+
+/*!
+ Return the starting LBA (logical block address) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+*/
+lba_t
+vcdinfo_get_track_lba(const vcdinfo_obj_t *obj, track_t track_num)
+{
+ if (NULL == obj || NULL == obj->img)
+ return VCDINFO_NULL_LBA;
+
+
+ /* CdIo tracks start at 1 rather than 0. */
+ return cdio_get_track_lba(obj->img, track_num+1);
+}
+
+/*!
+ Return the starting LSN (logical sector number) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+*/
+lsn_t
+vcdinfo_get_track_lsn(const vcdinfo_obj_t *obj, track_t track_num)
+{
+ if (NULL == obj || NULL == obj->img)
+ return VCDINFO_NULL_LSN;
+
+ /* CdIo tracks start at 1 rather than 0. */
+ return cdio_get_track_lsn(obj->img, track_num+1);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+*/
+int
+vcdinfo_get_track_msf(const vcdinfo_obj_t *obj, track_t track_num,
+ uint8_t *min, uint8_t *sec, uint8_t *frame)
+{
+ msf_t msf;
+
+ if (NULL == obj || NULL == obj->img)
+ return 1;
+
+ /* CdIo tracks start at 1 rather than 0. */
+ if (cdio_get_track_msf(obj->img, track_num+1, &msf)) {
+ *min = cdio_from_bcd8(msf.m);
+ *sec = cdio_from_bcd8(msf.s);
+ *frame = cdio_from_bcd8(msf.f);
+ return 0;
+ }
+
+ return 1;
+}
+
+/*!
+ Return the size in sectors for track n.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ FIXME: Whether we count the track pregap sectors is a bit haphazard.
+ We should add a parameter to indicate whether this is wanted or not.
+
+*/
+unsigned int
+vcdinfo_get_track_sect_count(const vcdinfo_obj_t *obj, const track_t track_num)
+{
+ if (NULL == obj || VCDINFO_INVALID_TRACK == track_num)
+ return 0;
+
+ {
+ iso9660_stat_t *statbuf;
+ const lsn_t lsn = vcdinfo_get_track_lsn(obj, track_num);
+
+ /* Try to get the sector count from the ISO 9660 filesystem */
+ if (obj->has_xa && (statbuf = iso9660_find_fs_lsn(obj->img, lsn))) {
+ unsigned int secsize = statbuf->secsize;
+ free(statbuf);
+ return secsize;
+ } else {
+ const lsn_t next_lsn=vcdinfo_get_track_lsn(obj, track_num+1);
+ /* Failed on ISO 9660 filesystem. Use track information. */
+ return next_lsn > lsn ? next_lsn - lsn : 0;
+ }
+ }
+ return 0;
+}
+
+/*!
+ Return size in bytes for track number for entry n in obj.
+
+ The IS0-9660 filesystem track has number 1. Tracks associated
+ with playable entries numbers start at 2.
+
+ FIXME: Whether we count the track pregap sectors is a bit haphazard.
+ We should add a parameter to indicate whether this is wanted or not.
+*/
+unsigned int
+vcdinfo_get_track_size(const vcdinfo_obj_t *obj, track_t track_num)
+{
+ if (NULL == obj || VCDINFO_INVALID_TRACK == track_num)
+ return 0;
+
+ {
+ iso9660_stat_t statbuf;
+ const lsn_t lsn = cdio_lba_to_lsn(vcdinfo_get_track_lba(obj, track_num));
+
+ /* Try to get the sector count from the ISO 9660 filesystem */
+ if (obj->has_xa && iso9660_find_fs_lsn(obj->img, lsn)) {
+ return statbuf.size;
+ }
+#if 0
+ else {
+ /* Failed on ISO 9660 filesystem. Use track information. */
+ if (obj->img != NULL)
+ return cdio_get_track_size(obj->img);
+ }
+#endif
+ }
+ return 0;
+}
+
+/*!
+ \brief Get the kind of video stream segment of segment seg_num in obj.
+ \return VCDINFO_FILES_VIDEO_INVALID is returned if on error or obj is
+ null. Otherwise the enumeration type.
+
+ Note first seg_num is 0!
+*/
+vcdinfo_video_segment_type_t
+vcdinfo_get_video_type(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ const InfoVcd_t *info;
+ if (obj == NULL) return VCDINFO_FILES_VIDEO_INVALID;
+ info = &obj->info;
+ if (info == NULL) return VCDINFO_FILES_VIDEO_INVALID;
+ return info->spi_contents[seg_num].video_type;
+}
+
+/*!
+ \brief Get the kind of VCD that obj refers to.
+*/
+vcd_type_t
+vcdinfo_get_VCD_type(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return VCD_TYPE_INVALID;
+ return obj->vcd_type;
+}
+
+
+/*!
+ Return the VCD volume count - the number of CD's in the collection.
+ O is returned if there is some problem in getting this.
+*/
+unsigned int
+vcdinfo_get_volume_count(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return 0;
+ return vcdinf_get_volume_count(&obj->info);
+}
+
+/*!
+ Return the VCD ID.
+ NULL is returned if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_volume_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj || NULL == &obj->pvd ) return (NULL);
+ return(iso9660_get_volume_id(&obj->pvd));
+}
+
+/*!
+ Return the VCD volumeset ID.
+ NULL is returned if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_volumeset_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj || NULL == &obj->pvd ) return (NULL);
+ return(vcdinfo_strip_trail(obj->pvd.volume_set_id, ISO_MAX_VOLUMESET_ID));
+}
+
+/*!
+ Return the VCD volume num - the number of the CD in the collection.
+ This is a number between 1 and the volume count.
+ O is returned if there is some problem in getting this.
+*/
+unsigned int
+vcdinfo_get_volume_num(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return 0;
+ return(uint16_from_be( obj->info.vol_id));
+}
+
+int
+vcdinfo_get_wait_time (uint16_t wtime)
+{
+ /* Note: this doesn't agree exactly with _wtime */
+ if (wtime < 61)
+ return wtime;
+ else if (wtime < 255)
+ return (wtime - 60) * 10 + 60;
+ else
+ return -1;
+}
+
+/*!
+ Return true is there is playback control.
+*/
+bool
+vcdinfo_has_pbc (const vcdinfo_obj_t *obj)
+{
+ return (obj && obj->info.psd_size!=0);
+}
+
+/*!
+ Return true if VCD has "extended attributes" (XA). Extended attributes
+ add meta-data attributes to a entries of file describing the file.
+ See also cdio_get_xa_attr_str() which returns a string similar to
+ a string you might get on a Unix filesystem listing ("ls").
+*/
+bool
+vcdinfo_has_xa(const vcdinfo_obj_t *obj)
+{
+ return obj->has_xa;
+}
+
+/*!
+ Add one to the MSF.
+*/
+void
+vcdinfo_inc_msf (uint8_t *min, uint8_t *sec, int8_t *frame)
+{
+ (*frame)++;
+ if (*frame>=CDIO_CD_FRAMES_PER_SEC) {
+ *frame = 0;
+ (*sec)++;
+ if (*sec>=CDIO_CD_SECS_PER_MIN) {
+ *sec = 0;
+ (*min)++;
+ }
+ }
+}
+
+/*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical block address (or LBA).
+ See also cdio_msf_to_lba which uses msf_t as its single parameter.
+*/
+lba_t
+vcdinfo_msf2lba (uint8_t min, uint8_t sec, int8_t frame)
+{
+ return CDIO_CD_FRAMES_PER_SEC*(CDIO_CD_SECS_PER_MIN*min + sec) + frame;
+}
+
+/*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical block address (or LBA).
+ See also cdio_msf_to_lba which uses msf_t as its single parameter.
+*/
+void
+vcdinfo_lba2msf (lba_t lba, uint8_t *min, uint8_t *sec, uint8_t *frame)
+{
+ *min = lba / (60*75);
+ lba %= (60*75);
+ *sec = lba / 75;
+ *frame = lba % 75;
+}
+
+/*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical sector number (or LSN).
+*/
+lsn_t
+vcdinfo_msf2lsn (uint8_t min, uint8_t sec, int8_t frame)
+{
+ lba_t lba=75*(60*min + sec) + frame;
+ if (lba < CDIO_PREGAP_SECTORS) {
+ vcd_error ("lba (%u) less than pregap sector (%u)",
+ (unsigned int) lba, CDIO_PREGAP_SECTORS);
+ return lba;
+ }
+ return lba - CDIO_PREGAP_SECTORS;
+}
+
+const char *
+vcdinfo_ofs2str (const vcdinfo_obj_t *obj, unsigned int offset, bool ext)
+{
+ vcdinfo_offset_t *ofs;
+ char *buf;
+
+ switch (offset) {
+ case PSD_OFS_DISABLED:
+ return "disabled";
+ case PSD_OFS_MULTI_DEF:
+ return "multi-default";
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ return "multi_def_no_num";
+ default: ;
+ }
+
+ buf = _getbuf ();
+ ofs = _vcdinfo_get_offset_t(obj, offset, ext);
+ if (ofs != NULL) {
+ if (ofs->lid)
+ snprintf (buf, BUF_SIZE, "LID[%d] @0x%4.4x",
+ ofs->lid, ofs->offset);
+ else
+ snprintf (buf, BUF_SIZE, "PSD[?] @0x%4.4x",
+ ofs->offset);
+ } else {
+ snprintf (buf, BUF_SIZE, "? @0x%4.4x", offset);
+ }
+ return buf;
+}
+
+bool
+vcdinfo_read_psd (vcdinfo_obj_t *obj)
+{
+ unsigned psd_size = vcdinfo_get_psd_size (obj);
+
+ if (psd_size)
+ {
+ if (psd_size > 256*1024)
+ {
+ vcd_error ("weird psd size (%u) -- aborting", psd_size);
+ return false;
+ }
+
+ obj->lot = _vcd_malloc (ISO_BLOCKSIZE * LOT_VCD_SIZE);
+ obj->psd = _vcd_malloc (ISO_BLOCKSIZE * _vcd_len2blocks (psd_size,
+ ISO_BLOCKSIZE));
+
+ if (cdio_read_mode2_sectors (obj->img, (void *) obj->lot, LOT_VCD_SECTOR,
+ false, LOT_VCD_SIZE))
+ return false;
+
+ if (cdio_read_mode2_sectors (obj->img, (void *) obj->psd, PSD_VCD_SECTOR,
+ false, _vcd_len2blocks (psd_size,
+ ISO_BLOCKSIZE)))
+ return false;
+
+ } else {
+ return false;
+ }
+ return true;
+}
+
+/*! Return the entry number for the given track. */
+unsigned int
+vcdinfo_track_get_entry(const vcdinfo_obj_t *obj, track_t i_track)
+{
+ /* FIXME: Add structure to directly map track to first entry number.
+ Until then...
+ */
+ lsn_t lsn= vcdinfo_get_track_lsn(obj, i_track);
+ return vcdinfo_lsn_get_entry(obj, lsn);
+}
+
+/*!
+ Calls recursive routine to populate obj->offset_list or obj->offset_x_list
+ by going through LOT.
+
+ Returns false if there was some error.
+*/
+bool
+vcdinfo_visit_lot (vcdinfo_obj_t *obj, bool extended)
+{
+ struct _vcdinf_pbc_ctx pbc_ctx;
+ bool ret;
+
+ pbc_ctx.psd_size = vcdinfo_get_psd_size (obj);
+ pbc_ctx.psd_x_size = obj->psd_x_size;
+ pbc_ctx.offset_mult = 8;
+ pbc_ctx.maximum_lid = vcdinfo_get_num_LIDs(obj);
+ pbc_ctx.offset_x_list = NULL;
+ pbc_ctx.offset_list = NULL;
+ pbc_ctx.psd = obj->psd;
+ pbc_ctx.psd_x = obj->psd_x;
+ pbc_ctx.lot = obj->lot;
+ pbc_ctx.lot_x = obj->lot_x;
+ pbc_ctx.extended = extended;
+
+ ret = vcdinf_visit_lot(&pbc_ctx);
+ if (NULL != obj->offset_x_list)
+ _cdio_list_free(obj->offset_x_list, true);
+ obj->offset_x_list = pbc_ctx.offset_x_list;
+ if (NULL != obj->offset_list)
+ _cdio_list_free(obj->offset_list, true);
+ obj->offset_list = pbc_ctx.offset_list;
+ return ret;
+}
+
+/*!
+ Change trailing blanks in str to nulls. Str has a maximum size of
+ n characters.
+*/
+const char *
+vcdinfo_strip_trail (const char str[], size_t n)
+{
+ static char buf[1025];
+ int j;
+
+ vcd_assert (n < 1024);
+
+ strncpy (buf, str, n);
+ buf[n] = '\0';
+
+ for (j = strlen (buf) - 1; j >= 0; j--)
+ {
+ if (buf[j] != ' ')
+ break;
+
+ buf[j] = '\0';
+ }
+
+ return buf;
+}
+
+/*!
+ Return true if offset is "rejected". That is shouldn't be displayed
+ in a list of entries.
+*/
+bool
+vcdinfo_is_rejected(uint16_t offset)
+{
+ return (offset & VCDINFO_REJECTED_MASK) != 0;
+}
+
+/*!
+ Nulls/zeros vcdinfo_obj_t structures; The caller should have
+ ensured that obj != NULL.
+ routines using obj are called.
+*/
+static void
+_vcdinfo_zero(vcdinfo_obj_t *obj)
+{
+ memset(obj, 0, sizeof(vcdinfo_obj_t));
+ obj->vcd_type = VCD_TYPE_INVALID;
+ obj->img = NULL;
+ obj->lot = NULL;
+ obj->source_name = NULL;
+ obj->seg_sizes = NULL;
+}
+
+/*!
+ Initialize the vcdinfo structure "obj". Should be done before other
+ routines using obj are called.
+*/
+bool
+vcdinfo_init(vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return false;
+ _vcdinfo_zero(obj);
+ return cdio_init();
+}
+
+/*!
+ Set up vcdinfo structure "obj" for reading from a particular
+ medium. This should be done before after initialization but before
+ any routines that need to retrieve data.
+
+ source_name is the device or file to use for inspection, and
+ source_type indicates what driver to use or class of drivers in the
+ case of DRIVER_DEVICE.
+ access_mode gives the CD access method for reading should the driver
+ allow for more than one kind of access method (e.g. MMC versus ioctl
+ on GNU/Linux)
+
+ If source_name is NULL we'll fill in the appropriate default device
+ name for the given source_type. However if in addtion source_type is
+ DRIVER_UNKNOWN, then we'll scan for a drive containing a VCD.
+
+ VCDINFO_OPEN_VCD is returned if everything went okay;
+ VCDINFO_OPEN_ERROR if there was an error and VCDINFO_OPEN_OTHER if the
+ medium is something other than a VCD.
+ */
+vcdinfo_open_return_t
+vcdinfo_open(vcdinfo_obj_t **obj_p, char *source_name[],
+ driver_id_t source_type, const char access_mode[])
+{
+ CdIo *img;
+ vcdinfo_obj_t *obj = _vcd_malloc(sizeof(vcdinfo_obj_t));
+ iso9660_stat_t *statbuf;
+
+
+ /* If we don't specify a driver_id or a source_name, scan the
+ system for a CD that contains a VCD.
+ */
+ if (NULL == *source_name && source_type == DRIVER_UNKNOWN) {
+ char **cd_drives=NULL;
+ cd_drives = cdio_get_devices_with_cap_ret(NULL,
+ (CDIO_FS_ANAL_SVCD|CDIO_FS_ANAL_CVD|CDIO_FS_ANAL_VIDEOCD
+ |CDIO_FS_UNKNOWN),
+ true, &source_type);
+ if ( NULL == cd_drives || NULL == cd_drives[0] ) {
+ return VCDINFO_OPEN_ERROR;
+ }
+ *source_name = strdup(cd_drives[0]);
+ cdio_free_device_list(cd_drives);
+ }
+
+ img = cdio_open(*source_name, source_type);
+ if (NULL == img) {
+ return VCDINFO_OPEN_ERROR;
+ }
+
+ *obj_p = obj;
+
+ if (access_mode != NULL)
+ cdio_set_arg (img, "access-mode", access_mode);
+
+ if (NULL == *source_name) {
+ *source_name = cdio_get_default_device(img);
+ if (NULL == *source_name) return VCDINFO_OPEN_ERROR;
+ }
+
+ memset (obj, 0, sizeof (vcdinfo_obj_t));
+ obj->img = img; /* Note we do this after the above wipeout! */
+
+ if (!iso9660_fs_read_pvd(obj->img, &(obj->pvd))) {
+ return VCDINFO_OPEN_ERROR;
+ }
+
+ /* Determine if VCD has XA attributes. */
+ {
+
+ iso9660_pvd_t const *pvd = &obj->pvd;
+
+ obj->has_xa = !strncmp ((char *) pvd + ISO_XA_MARKER_OFFSET,
+ ISO_XA_MARKER_STRING,
+ strlen (ISO_XA_MARKER_STRING));
+ }
+
+ if (!read_info(obj->img, &(obj->info), &(obj->vcd_type)) ||
+ vcdinfo_get_format_version (obj) == VCD_TYPE_INVALID ||
+ !read_entries(obj->img, &(obj->entries))) {
+ free (obj); /* match 0.7.23's behaviour */
+ return VCDINFO_OPEN_OTHER;
+ }
+
+ {
+ size_t len = strlen(*source_name)+1;
+ obj->source_name = (char *) malloc(len * sizeof(char));
+ strncpy(obj->source_name, *source_name, len);
+ }
+
+ if (obj->vcd_type == VCD_TYPE_SVCD || obj->vcd_type == VCD_TYPE_HQVCD) {
+ statbuf = iso9660_fs_stat (obj->img, "MPEGAV");
+
+ if (NULL != statbuf) {
+ vcd_warn ("non compliant /MPEGAV folder detected!");
+ free(statbuf);
+ }
+
+
+ statbuf = iso9660_fs_stat (obj->img, "SVCD/TRACKS.SVD;1");
+ if (NULL != statbuf) {
+ lsn_t lsn = statbuf->lsn;
+ if (statbuf->size != ISO_BLOCKSIZE)
+ vcd_warn ("TRACKS.SVD filesize != %d!", ISO_BLOCKSIZE);
+
+ obj->tracks_buf = _vcd_malloc (ISO_BLOCKSIZE);
+
+ free(statbuf);
+ if (cdio_read_mode2_sector (obj->img, obj->tracks_buf, lsn, false))
+ return VCDINFO_OPEN_ERROR;
+ }
+ }
+
+ _init_segments (obj);
+
+ switch (obj->vcd_type) {
+ case VCD_TYPE_VCD2: {
+ /* FIXME: Can reduce CD reads by using
+ iso9660_fs_readdir(img, "EXT", true) and then scanning for
+ the files listed below.
+ */
+ statbuf = iso9660_fs_stat (img, "EXT/PSD_X.VCD;1");
+ if (NULL != statbuf) {
+ lsn_t lsn = statbuf->lsn;
+ uint32_t secsize = statbuf->secsize;
+
+ obj->psd_x = _vcd_malloc (ISO_BLOCKSIZE * secsize);
+ obj->psd_x_size = statbuf->size;
+
+ vcd_debug ("found /EXT/PSD_X.VCD at sector %lu",
+ (long unsigned int) lsn);
+
+ free(statbuf);
+ if (cdio_read_mode2_sectors (img, obj->psd_x, lsn, false, secsize))
+ return VCDINFO_OPEN_ERROR;
+ }
+
+ statbuf = iso9660_fs_stat (img, "EXT/LOT_X.VCD;1");
+ if (NULL != statbuf) {
+ lsn_t lsn = statbuf->lsn;
+ uint32_t secsize = statbuf->secsize;
+ obj->lot_x = _vcd_malloc (ISO_BLOCKSIZE * secsize);
+
+ vcd_debug ("found /EXT/LOT_X.VCD at sector %lu",
+ (unsigned long int) lsn);
+
+ if (statbuf->size != LOT_VCD_SIZE * ISO_BLOCKSIZE)
+ vcd_warn ("LOT_X.VCD size != 65535");
+
+ free(statbuf);
+ if (cdio_read_mode2_sectors (img, obj->lot_x, lsn, false, secsize))
+ return VCDINFO_OPEN_ERROR;
+
+ }
+ break;
+ }
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD: {
+ /* FIXME: Can reduce CD reads by using
+ iso9660_fs_readdir(img, "SVCD", true) and then scanning for
+ the files listed below.
+ */
+ statbuf = iso9660_fs_stat (img, "MPEGAV");
+ if (NULL != statbuf) {
+ vcd_warn ("non compliant /MPEGAV folder detected!");
+ free(statbuf);
+ }
+
+ statbuf = iso9660_fs_stat (img, "SVCD/TRACKS.SVD;1");
+ if (NULL == statbuf)
+ vcd_warn ("mandatory /SVCD/TRACKS.SVD not found!");
+ else {
+ vcd_debug ("found TRACKS.SVD signature at sector %lu",
+ (unsigned long int) statbuf->lsn);
+ free(statbuf);
+ }
+
+ statbuf = iso9660_fs_stat (img, "SVCD/SEARCH.DAT;1");
+ if (NULL == statbuf)
+ vcd_warn ("mandatory /SVCD/SEARCH.DAT not found!");
+ else {
+ lsn_t lsn = statbuf->lsn;
+ uint32_t secsize = statbuf->secsize;
+ uint32_t stat_size = statbuf->size;
+ uint32_t size;
+
+ vcd_debug ("found SEARCH.DAT at sector %lu", (unsigned long int) lsn);
+
+ obj->search_buf = _vcd_malloc (ISO_BLOCKSIZE * secsize);
+
+ if (cdio_read_mode2_sectors (img, obj->search_buf, lsn, false, secsize))
+ return VCDINFO_OPEN_ERROR;
+
+ size = (3 * uint16_from_be (((SearchDat *)obj->search_buf)->scan_points))
+ + sizeof (SearchDat);
+
+ free(statbuf);
+ if (size > stat_size) {
+ vcd_warn ("number of scanpoints leads to bigger size than "
+ "file size of SEARCH.DAT! -- rereading");
+
+ free (obj->search_buf);
+ obj->search_buf = _vcd_malloc (ISO_BLOCKSIZE
+ * _vcd_len2blocks(size, ISO_BLOCKSIZE));
+
+ if (cdio_read_mode2_sectors (img, obj->search_buf, lsn, false,
+ secsize))
+ return VCDINFO_OPEN_ERROR;
+ }
+ }
+ break;
+ }
+ default:
+ ;
+ }
+
+ statbuf = iso9660_fs_stat (img, "EXT/SCANDATA.DAT;1");
+ if (statbuf != NULL) {
+ lsn_t lsn = statbuf->lsn;
+ uint32_t secsize = statbuf->secsize;
+
+ vcd_debug ("found /EXT/SCANDATA.DAT at sector %u", (unsigned int) lsn);
+
+ obj->scandata_buf = _vcd_malloc (ISO_BLOCKSIZE * secsize);
+
+ free(statbuf);
+ if (cdio_read_mode2_sectors (img, obj->scandata_buf, lsn, false, secsize))
+ return VCDINFO_OPEN_ERROR;
+ }
+
+ return VCDINFO_OPEN_VCD;
+
+}
+
+/*!
+ Dispose of any resources associated with vcdinfo structure "obj".
+ Call this when "obj" it isn't needed anymore.
+
+ True is returned is everything went okay, and false if not.
+*/
+bool
+vcdinfo_close(vcdinfo_obj_t *obj)
+{
+ if (obj != NULL) {
+ if (obj->offset_list != NULL)
+ _cdio_list_free(obj->offset_list, true);
+ if (obj->offset_x_list != NULL)
+ _cdio_list_free(obj->offset_x_list, true);
+ free(obj->seg_sizes);
+ free(obj->lot);
+ free(obj->lot_x);
+ if (obj->psd_x) free(obj->psd_x);
+ if (obj->psd) free(obj->psd);
+ if (obj->scandata_buf) free(obj->scandata_buf);
+ free(obj->tracks_buf);
+ free(obj->search_buf);
+ free(obj->source_name);
+
+ if (obj->img != NULL) cdio_destroy (obj->img);
+ _vcdinfo_zero(obj);
+ }
+
+ free(obj);
+ return(true);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/info_private.c b/contrib/libvcd/info_private.c
new file mode 100644
index 000000000..9f89a3dcb
--- /dev/null
+++ b/contrib/libvcd/info_private.c
@@ -0,0 +1,328 @@
+/*
+ $Id: info_private.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*
+ Like vcdinfo but exposes more of the internal structure. It is probably
+ better to use vcdinfo, when possible.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stddef.h>
+#include <errno.h>
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/bytesex.h>
+#include <cdio/util.h>
+
+#include <libvcd/types.h>
+#include <libvcd/files.h>
+
+#include <libvcd/info.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "data_structures.h"
+#include "info_private.h"
+#include "pbc.h"
+
+static const char _rcsid[] = "$Id: info_private.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $";
+
+/*
+ This fills in unassigned LIDs in the offset table. Due to
+ "rejected" LOT entries, some of these might not have gotten filled
+ in while scanning PBC (if in fact there even was a PBC).
+
+ Note: We assume that an unassigned LID is one whose value is 0.
+ */
+static void
+vcdinf_update_offset_list(struct _vcdinf_pbc_ctx *obj, bool extended)
+{
+ if (NULL==obj) return;
+ {
+ CdioListNode *node;
+ CdioList *unused_lids = _cdio_list_new();
+ CdioListNode *next_unused_node = _cdio_list_begin(unused_lids);
+
+ unsigned int last_lid=0;
+ CdioList *offset_list = extended ? obj->offset_x_list : obj->offset_list;
+
+ lid_t max_seen_lid=0;
+
+ _CDIO_LIST_FOREACH (node, offset_list)
+ {
+ vcdinfo_offset_t *ofs = _cdio_list_node_data (node);
+ if (!ofs->lid) {
+ /* We have a customer! Assign a LID from the free pool
+ or take one from the end if no skipped LIDs.
+ */
+ CdioListNode *node=_cdio_list_node_next(next_unused_node);
+ if (node != NULL) {
+ lid_t *next_unused_lid=_cdio_list_node_data(node);
+ ofs->lid = *next_unused_lid;
+ next_unused_node=node;
+ } else {
+ max_seen_lid++;
+ ofs->lid = max_seen_lid;
+ }
+ } else {
+ /* See if we've skipped any LID numbers. */
+ last_lid++;
+ while (last_lid != ofs->lid ) {
+ lid_t * lid=_vcd_malloc (sizeof(lid_t));
+ *lid = last_lid;
+ _cdio_list_append(unused_lids, lid);
+ }
+ if (last_lid > max_seen_lid) max_seen_lid=last_lid;
+ }
+ }
+ _cdio_list_free(unused_lids, true);
+ }
+}
+
+/*!
+ Calls recursive routine to populate obj->offset_list or obj->offset_x_list
+ by going through LOT.
+
+ Returns false if there was some error.
+*/
+bool
+vcdinf_visit_lot (struct _vcdinf_pbc_ctx *obj)
+{
+ const LotVcd_t *lot = obj->extended ? obj->lot_x : obj->lot;
+ unsigned int n, tmp;
+ bool ret=true;
+
+ if (obj->extended) {
+ if (!obj->psd_x_size) return false;
+ } else if (!obj->psd_size) return false;
+
+ for (n = 0; n < LOT_VCD_OFFSETS; n++)
+ if ((tmp = vcdinf_get_lot_offset(lot, n)) != PSD_OFS_DISABLED)
+ ret &= vcdinf_visit_pbc (obj, n + 1, tmp, true);
+
+ _vcd_list_sort (obj->extended ? obj->offset_x_list : obj->offset_list,
+ (_cdio_list_cmp_func) vcdinf_lid_t_cmp);
+
+ /* Now really complete the offset table with LIDs. This routine
+ might obviate the need for vcdinf_visit_pbc() or some of it which is
+ more complex. */
+ vcdinf_update_offset_list(obj, obj->extended);
+ return ret;
+}
+
+/*!
+ Recursive routine to populate obj->offset_list or obj->offset_x_list
+ by reading playback control entries referred to via lid.
+
+ Returns false if there was some error.
+*/
+bool
+vcdinf_visit_pbc (struct _vcdinf_pbc_ctx *obj, lid_t lid, unsigned int offset,
+ bool in_lot)
+{
+ CdioListNode *node;
+ vcdinfo_offset_t *ofs;
+ unsigned int psd_size = obj->extended ? obj->psd_x_size : obj->psd_size;
+ const uint8_t *psd = obj->extended ? obj->psd_x : obj->psd;
+ unsigned int _rofs = offset * obj->offset_mult;
+ CdioList *offset_list;
+ bool ret=true;
+
+ vcd_assert (psd_size % 8 == 0);
+
+ switch (offset)
+ {
+ case PSD_OFS_DISABLED:
+ case PSD_OFS_MULTI_DEF:
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ return true;
+
+ default:
+ break;
+ }
+
+ if (_rofs >= psd_size)
+ {
+ if (obj->extended)
+ vcd_warn ("psd offset out of range in extended PSD (%d >= %d)",
+ _rofs, psd_size);
+ else
+ vcd_warn ("psd offset out of range (%d >= %d)", _rofs, psd_size);
+ return false;
+ }
+
+ if (!obj->offset_list)
+ obj->offset_list = _cdio_list_new ();
+
+ if (!obj->offset_x_list)
+ obj->offset_x_list = _cdio_list_new ();
+
+ if (obj->extended) {
+ offset_list = obj->offset_x_list;
+ } else
+ offset_list = obj->offset_list;
+
+ _CDIO_LIST_FOREACH (node, offset_list)
+ {
+ ofs = _cdio_list_node_data (node);
+
+ if (offset == ofs->offset)
+ {
+ if (in_lot)
+ ofs->in_lot = true;
+
+ if (lid) {
+ /* Our caller thinks she knows what our LID is.
+ This should help out getting the LID for end descriptors
+ if not other things as well.
+ */
+ ofs->lid = lid;
+ }
+
+ ofs->ext = obj->extended;
+
+ return true; /* already been there... */
+ }
+ }
+
+ ofs = _vcd_malloc (sizeof (vcdinfo_offset_t));
+
+ ofs->ext = obj->extended;
+ ofs->in_lot = in_lot;
+ ofs->lid = lid;
+ ofs->offset = offset;
+ ofs->type = psd[_rofs];
+
+ switch (ofs->type)
+ {
+ case PSD_TYPE_PLAY_LIST:
+ _cdio_list_append (offset_list, ofs);
+ {
+ const PsdPlayListDescriptor_t *d = (const void *) (psd + _rofs);
+ const lid_t lid = vcdinf_pld_get_lid(d);
+
+ if (!ofs->lid)
+ ofs->lid = lid;
+ else
+ if (ofs->lid != lid)
+ vcd_warn ("LOT entry assigned LID %d, but descriptor has LID %d",
+ ofs->lid, lid);
+
+ ret &= vcdinf_visit_pbc (obj, 0, vcdinf_pld_get_prev_offset(d), false);
+ ret &= vcdinf_visit_pbc (obj, 0, vcdinf_pld_get_next_offset(d), false);
+ ret &= vcdinf_visit_pbc (obj, 0, vcdinf_pld_get_return_offset(d),
+ false);
+ }
+ break;
+
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ case PSD_TYPE_SELECTION_LIST:
+ _cdio_list_append (offset_list, ofs);
+ {
+ const PsdSelectionListDescriptor_t *d =
+ (const void *) (psd + _rofs);
+
+ int idx;
+
+ if (!ofs->lid)
+ ofs->lid = uint16_from_be (d->lid) & 0x7fff;
+ else
+ if (ofs->lid != (uint16_from_be (d->lid) & 0x7fff))
+ vcd_warn ("LOT entry assigned LID %d, but descriptor has LID %d",
+ ofs->lid, uint16_from_be (d->lid) & 0x7fff);
+
+ ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_prev_offset(d), false);
+ ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_next_offset(d), false);
+ ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_return_offset(d),
+ false);
+ ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_default_offset(d),
+ false);
+ ret &= vcdinf_visit_pbc (obj, 0, uint16_from_be (d->timeout_ofs),
+ false);
+
+ for (idx = 0; idx < vcdinf_get_num_selections(d); idx++)
+ ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_offset(d, idx),
+ false);
+ }
+ break;
+
+ case PSD_TYPE_END_LIST:
+ _cdio_list_append (offset_list, ofs);
+ break;
+
+ default:
+ vcd_warn ("corrupt PSD???????");
+ free (ofs);
+ return false;
+ break;
+ }
+ return ret;
+}
+
+/*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+*/
+lba_t
+vcdinf_get_entry_lba(const EntriesVcd_t *entries, unsigned int entry_num)
+{
+ const msf_t *msf = vcdinf_get_entry_msf(entries, entry_num);
+ return (msf != NULL) ? cdio_msf_to_lba(msf) : VCDINFO_NULL_LBA;
+}
+
+/*! Return the starting MSF (minutes/secs/frames) for sequence
+ entry_num in obj. NULL is returned if there is no entry.
+ The first entry number is 0.
+*/
+const msf_t *
+vcdinf_get_entry_msf(const EntriesVcd_t *entries, unsigned int entry_num)
+{
+ const unsigned int entry_count = uint16_from_be (entries->entry_count);
+ return entry_num < entry_count ?
+ &(entries->entry[entry_num].msf)
+ : NULL;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/info_private.h b/contrib/libvcd/info_private.h
new file mode 100644
index 000000000..d17b6680b
--- /dev/null
+++ b/contrib/libvcd/info_private.h
@@ -0,0 +1,118 @@
+/*!
+ \file vcdinf.h
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ \verbatim
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+ Like vcdinfo but exposes more of the internal structure. It is probably
+ better to use vcdinfo, when possible.
+ \endverbatim
+*/
+
+#ifndef _VCD_INFO_PRIVATE_H
+#define _VCD_INFO_PRIVATE_H
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/ds.h>
+#include <cdio/iso9660.h>
+#include <libvcd/types.h>
+#include <libvcd/files_private.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ struct _VcdInfo {
+ vcd_type_t vcd_type;
+
+ CdIo *img;
+
+ iso9660_pvd_t pvd;
+
+ InfoVcd_t info;
+ EntriesVcd_t entries;
+
+ CdioList *offset_list;
+ CdioList *offset_x_list;
+ uint32_t *seg_sizes;
+ lsn_t first_segment_lsn;
+
+ LotVcd_t *lot;
+ LotVcd_t *lot_x;
+ uint8_t *psd;
+ uint8_t *psd_x;
+ unsigned int psd_x_size;
+ bool extended;
+
+ bool has_xa; /* True if has extended attributes (XA) */
+
+ void *tracks_buf;
+ void *search_buf;
+ void *scandata_buf;
+
+ char *source_name; /* VCD device or file currently open */
+
+ };
+
+ /*! Return the starting MSF (minutes/secs/frames) for sequence
+ entry_num in obj. NULL is returned if there is no entry.
+ The first entry number is 0.
+ */
+ const msf_t * vcdinf_get_entry_msf(const EntriesVcd_t *entries,
+ unsigned int entry_num);
+
+ struct _vcdinf_pbc_ctx {
+ unsigned int psd_size;
+ lid_t maximum_lid;
+ unsigned offset_mult;
+ CdioList *offset_x_list;
+ CdioList *offset_list;
+
+ LotVcd_t *lot;
+ LotVcd_t *lot_x;
+ uint8_t *psd;
+ uint8_t *psd_x;
+ unsigned int psd_x_size;
+ bool extended;
+ };
+
+ /*!
+ Calls recursive routine to populate obj->offset_list or obj->offset_x_list
+ by going through LOT.
+
+ Returns false if there was some error.
+ */
+ bool vcdinf_visit_lot (struct _vcdinf_pbc_ctx *obj);
+
+ /*!
+ Recursive routine to populate obj->offset_list or obj->offset_x_list
+ by reading playback control entries referred to via lid.
+
+ Returns false if there was some error.
+ */
+ bool vcdinf_visit_pbc (struct _vcdinf_pbc_ctx *obj, lid_t lid,
+ unsigned int offset, bool in_lot);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /*_VCD_INFO_PRIVATE_H*/
diff --git a/contrib/libvcd/libvcd/Makefile.am b/contrib/libvcd/libvcd/Makefile.am
new file mode 100644
index 000000000..ea29859f6
--- /dev/null
+++ b/contrib/libvcd/libvcd/Makefile.am
@@ -0,0 +1,3 @@
+include $(top_srcdir)/misc/Makefile.common
+
+noinst_HEADERS = files.h inf.h logging.h types.h files_private.h info.h sector.h version.h
diff --git a/contrib/libvcd/libvcd/files.h b/contrib/libvcd/libvcd/files.h
new file mode 100644
index 000000000..91eeafad8
--- /dev/null
+++ b/contrib/libvcd/libvcd/files.h
@@ -0,0 +1,101 @@
+/*
+ $Id: files.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef VCDFILES_H
+#define VCDFILES_H
+
+#include <libvcd/types.h>
+
+#define INFO_VCD_SECTOR 150
+#define ENTRIES_VCD_SECTOR 151
+#define LOT_VCD_SECTOR 152
+#define LOT_VCD_SIZE 32
+#define PSD_VCD_SECTOR (LOT_VCD_SECTOR+LOT_VCD_SIZE)
+
+#define MAX_SEGMENTS 1980
+#define MAX_ENTRIES 500
+#define MAX_SEQ_ENTRIES 99
+
+/* these are used for SVCDs only */
+#define TRACKS_SVD_SECTOR (PSD_VCD_SECTOR+1)
+#define SEARCH_DAT_SECTOR (TRACKS_SVD_SECTOR+1)
+
+/* Maximum index of optional LOT.VCD (the List ID Offset Table.) */
+#define LOT_VCD_OFFSETS ((1 << 15)-1)
+
+typedef enum {
+ PSD_TYPE_PLAY_LIST = 0x10, /* Play List */
+ PSD_TYPE_SELECTION_LIST = 0x18, /* Selection List (+Ext. for SVCD) */
+ PSD_TYPE_EXT_SELECTION_LIST = 0x1a, /* Extended Selection List (VCD2.0) */
+ PSD_TYPE_END_LIST = 0x1f, /* End List */
+ PSD_TYPE_COMMAND_LIST = 0x20 /* Command List */
+} psd_descriptor_types;
+
+#define ENTRIES_ID_VCD "ENTRYVCD"
+#define ENTRIES_ID_VCD3 "ENTRYSVD"
+#define ENTRIES_ID_SVCD "ENTRYVCD" /* not ENTRYSVD! */
+
+#define SCANDATA_VERSION_VCD2 0x02
+#define SCANDATA_VERSION_SVCD 0x01
+
+void
+set_entries_vcd(VcdObj *obj, void *buf);
+
+void
+set_info_vcd (VcdObj *obj, void *buf);
+
+uint32_t
+get_psd_size (VcdObj *obj, bool extended);
+
+void
+set_lot_vcd (VcdObj *obj, void *buf, bool extended);
+
+void
+set_psd_vcd (VcdObj *obj, void *buf, bool extended);
+
+void
+set_tracks_svd (VcdObj *obj, void *buf);
+
+uint32_t
+get_search_dat_size (const VcdObj *obj);
+
+void
+set_search_dat (VcdObj *obj, void *buf);
+
+uint32_t
+get_scandata_dat_size (const VcdObj *obj);
+
+void
+set_scandata_dat (VcdObj *obj, void *buf);
+
+
+vcd_type_t
+vcd_files_info_detect_type (const void *info_buf);
+
+#endif /* VCDFILES_H */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/libvcd/files_private.h b/contrib/libvcd/libvcd/files_private.h
new file mode 100644
index 000000000..aabb6ab0c
--- /dev/null
+++ b/contrib/libvcd/libvcd/files_private.h
@@ -0,0 +1,562 @@
+/*
+ $Id: files_private.h,v 1.3 2006/09/26 21:16:59 dgp85 Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ (C) 2000 Jens B. Jorgensen <jbj1@ultraemail.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_FILES_PRIVATE_H__
+#define __VCD_FILES_PRIVATE_H__
+
+#include <libvcd/files.h>
+#include <libvcd/types.h>
+
+/* random note: most stuff is big endian here */
+
+#define ENTRIES_ID_VCD "ENTRYVCD"
+#define ENTRIES_ID_VCD3 "ENTRYSVD"
+#define ENTRIES_ID_SVCD "ENTRYVCD" /* not ENTRYSVD! */
+
+#define ENTRIES_VERSION_VCD 0x01
+#define ENTRIES_SPTAG_VCD 0x00
+
+#define ENTRIES_VERSION_VCD11 0x01
+#define ENTRIES_SPTAG_VCD11 0x00
+
+#define ENTRIES_VERSION_VCD2 0x02
+#define ENTRIES_SPTAG_VCD2 0x00
+
+#define ENTRIES_VERSION_SVCD 0x01
+#define ENTRIES_SPTAG_SVCD 0x00
+
+#define ENTRIES_VERSION_HQVCD 0x01
+#define ENTRIES_SPTAG_HQVCD 0x00
+
+PRAGMA_BEGIN_PACKED
+
+typedef struct _EntriesVcd_tag {
+ char ID[8]; /* "ENTRYVCD" */
+ uint8_t version; /* 0x02 --- VCD2.0
+ 0x01 --- SVCD, should be
+ same as version in
+ INFO.SVD */
+ uint8_t sys_prof_tag; /* 0x01 if VCD1.1
+ 0x00 else */
+ uint16_t entry_count; /* 1 <= tracks <= 500 */
+ struct { /* all fields are BCD */
+ uint8_t n; /* cd track no 2 <= n <= 99 */
+ msf_t msf;
+ } GNUC_PACKED entry[MAX_ENTRIES];
+ uint8_t reserved2[36]; /* RESERVED, must be 0x00 */
+} GNUC_PACKED _EntriesVcd; /* sector 00:04:01 */
+
+#define EntriesVcd_SIZEOF ISO_BLOCKSIZE
+
+
+#define INFO_ID_VCD "VIDEO_CD"
+#define INFO_ID_SVCD "SUPERVCD"
+#define INFO_ID_HQVCD "HQ-VCD "
+
+#define INFO_VERSION_VCD 0x01
+#define INFO_SPTAG_VCD 0x00
+
+#define INFO_VERSION_VCD11 0x01
+#define INFO_SPTAG_VCD11 0x01
+
+#define INFO_VERSION_VCD2 0x02
+#define INFO_SPTAG_VCD2 0x00
+
+#define INFO_VERSION_SVCD 0x01
+#define INFO_SPTAG_SVCD 0x00
+
+#define INFO_VERSION_HQVCD 0x01
+#define INFO_SPTAG_HQVCD 0x01
+
+#define INFO_OFFSET_MULT 0x08
+
+/* This one-byte field describes certain characteristics of the disc */
+typedef struct {
+#if defined(BITFIELD_LSBF)
+ bool reserved1 : 1; /* Reserved, must be zero */
+ bitfield_t restriction : 2; /* restriction, eg. "unsuitable
+ for kids":
+ 0x0 ==> unrestricted,
+ 0x1 ==> restricted category 1,
+ 0x2 ==> restricted category 2,
+ 0x3 ==> restricted category 3 */
+ bool special_info : 1; /* Special Information is encoded
+ in the pictures */
+ bool user_data_cc : 1; /* MPEG User Data is used
+ for Closed Caption */
+ bool use_lid2 : 1; /* If == 1 and the PSD is
+ interpreted and the next
+ disc has the same album
+ id then start the next
+ disc at List ID #2,
+ otherwise List ID #1 */
+ bool use_track3 : 1; /* If == 1 and the PSD is
+ not interpreted and
+ next disc has same album
+ id, then start next disc
+ with track 3, otherwise
+ start with track 2 */
+ bool pbc_x : 1; /* extended PBC available */
+#else
+ bool pbc_x : 1;
+ bool use_track3 : 1;
+ bool use_lid2 : 1;
+ bool user_data_cc : 1;
+ bool special_info : 1;
+ bitfield_t restriction : 2;
+ bool reserved1 : 1;
+#endif
+} GNUC_PACKED InfoStatusFlags;
+
+#define InfoStatusFlags_SIZEOF 1
+
+enum {
+ VCD_FILES_VIDEO_NOSTREAM = 0,
+ VCD_FILES_VIDEO_NTSC_STILL = 1,
+ VCD_FILES_VIDEO_NTSC_STILL2 = 2,
+ VCD_FILES_VIDEO_NTSC_MOTION = 3,
+ VCD_FILES_VIDEO_PAL_STILL = 5,
+ VCD_FILES_VIDEO_PAL_STILL2 = 6,
+ VCD_FILES_VIDEO_PAL_MOTION = 7
+};
+
+typedef struct
+{
+#if defined(BITFIELD_LSBF)
+ bitfield_t audio_type : 2; /* Audio characteristics:
+ 0x0 - No MPEG audio stream
+ 0x1 - One MPEG1 or MPEG2 audio
+ stream without extension
+ 0x2 - Two MPEG1 or MPEG2 audio
+ streams without extension
+ 0x3 - One MPEG2 multi-channel
+ audio stream w/ extension*/
+ bitfield_t video_type : 3; /* Video characteristics:
+ 0x0 - No MPEG video data
+ 0x1 - NTSC still picture
+ 0x2 - Reserved (NTSC hires?)
+ 0x3 - NTSC motion picture
+ 0x4 - Reserved
+ 0x5 - PAL still picture
+ 0x6 - Reserved (PAL hires?)
+ 0x7 - PAL motion picture */
+ bool item_cont : 1; /* Indicates segment is continuation
+ 0x0 - 1st or only segment of item
+ 0x1 - 2nd or later
+ segment of item */
+ bitfield_t ogt : 2; /* 0x0 - no OGT substream
+ 0x1 - sub-stream 0 available
+ 0x2 - sub-stream 0 & 1 available
+ 0x3 - all OGT sub-substreams
+ available */
+#else
+ bitfield_t ogt : 2;
+ bool item_cont : 1;
+ bitfield_t video_type : 3;
+ bitfield_t audio_type : 2;
+#endif
+} GNUC_PACKED InfoSpiContents;
+
+#define InfoSpiContents_SIZEOF 1
+
+typedef struct _InfoVcd_tag {
+ char ID[8]; /* const "VIDEO_CD" for
+ VCD, "SUPERVCD" or
+ "HQ-VCD " for SVCD */
+ uint8_t version; /* 0x02 -- VCD2.0,
+ 0x01 for SVCD and VCD1.x */
+ uint8_t sys_prof_tag; /* System Profile Tag, used
+ to define the set of
+ mandatory parts to be
+ applied for compatibility;
+ 0x00 for "SUPERVCD",
+ 0x01 for "HQ-VCD ",
+ 0x0n for VCDx.n */
+ char album_desc[16]; /* album identification/desc. */
+ uint16_t vol_count; /* number of volumes in album */
+ uint16_t vol_id; /* number id of this volume in album */
+ uint8_t pal_flags[13]; /* bitset of 98 PAL(=set)/NTSC flags */
+ InfoStatusFlags flags; /* status flags bit field */
+ uint32_t psd_size; /* size of PSD.VCD file */
+ msf_t first_seg_addr; /* first segment addresses,
+ coded BCD The location
+ of the first sector of
+ the Segment Play Item
+ Area, in the form
+ mm:ss:00. Must be
+ 00:00:00 if the PSD size
+ is 0. */
+ uint8_t offset_mult; /* offset multiplier, must be 8 */
+ uint16_t lot_entries; /* offsets in lot */
+ uint16_t item_count; /* segments used for segmentitems */
+ InfoSpiContents spi_contents[MAX_SEGMENTS]; /* The next 1980 bytes
+ contain one byte for each possible
+ segment play item. Each byte indicates
+ contents. */
+
+ uint16_t playing_time[5]; /* in seconds */
+ char reserved[2]; /* Reserved, must be zero */
+} GNUC_PACKED _InfoVcd;
+
+#define InfoVcd_SIZEOF ISO_BLOCKSIZE
+
+/* LOT.VCD
+ This optional file is only necessary if the PSD size is not zero.
+ This List ID Offset Table allows you to start playing the PSD from
+ lists other than the default List ID number. This table has a fixed length
+ of 32 sectors and maps List ID numbers into List Offsets. It's got
+ an entry for each List ID Number with the 16-bit offset. Note that
+ List ID 1 has an offset of 0x0000. All unused or non-user-accessible
+ entries must be 0xffff. */
+
+#define LOT_VCD_OFFSETS ((1 << 15)-1)
+
+typedef struct _LotVcd_tag {
+ uint16_t reserved; /* Reserved, must be zero */
+ uint16_t offset[LOT_VCD_OFFSETS]; /* offset given in 8 byte units */
+} GNUC_PACKED _LotVcd;
+
+#define LotVcd_SIZEOF (32*ISO_BLOCKSIZE)
+
+/* PSD.VCD
+ The PSD controls the "user interaction" mode which can be used to make
+ menus, etc. The PSD contains a set of Lists. Each List defines a set of
+ Items which are played in sequence. An Item can be an mpeg track (in whole
+ or part) or a Segment Play Item which can subsequently be mpeg video
+ with or without audio, one more more mpeg still pictures (with or without
+ audio) or mpeg audio only.
+
+ The Selection List defines the action to be taken in response to a set
+ of defined user actions: Next, Previous, Default Select, Numeric, Return.
+
+ The End List terminates the control flow or switches to the next
+ disc volume.
+
+ Each list has a unique list id number. The first must be 1, the others can
+ be anything (up to 32767).
+
+ References to PSD list addresses are expressed as an offset into the PSD
+ file. The offset indicated in the file must be multiplied by the Offset
+ Multiplier found in the info file (although this seems to always have to
+ be 8). Unused areas are filled with zeros. List ID 1 starts at offset 0.
+*/
+
+/* ...difficult to represent as monolithic C struct... */
+
+typedef struct {
+ uint8_t type; /* PSD_TYPE_END_LIST */
+ uint8_t next_disc; /* 0x00 to stop PBC or 0xnn to switch to disc no nn */
+ uint16_t change_pic; /* 0 or 1000..2979, should be still image */
+ uint8_t reserved[4]; /* padded with 0x00 */
+} GNUC_PACKED PsdEndListDescriptor;
+
+#define PsdEndListDescriptor_SIZEOF 8
+
+typedef struct {
+#if defined(BITFIELD_LSBF)
+ bool SelectionAreaFlag : 1;
+ bool CommandListFlag : 1;
+ bitfield_t reserved : 6;
+#else
+ bitfield_t reserved : 6;
+ bool CommandListFlag : 1;
+ bool SelectionAreaFlag : 1;
+#endif
+} GNUC_PACKED PsdSelectionListFlags;
+
+#define PsdSelectionListFlags_SIZEOF 1
+
+typedef struct _PsdSelectionListDescriptor_tag {
+ uint8_t type;
+ PsdSelectionListFlags flags;
+ uint8_t nos;
+ uint8_t bsn;
+ uint16_t lid;
+ uint16_t prev_ofs;
+ uint16_t next_ofs;
+ uint16_t return_ofs;
+ uint16_t default_ofs;
+ uint16_t timeout_ofs;
+ uint8_t totime;
+ uint8_t loop;
+ uint16_t itemid;
+ uint16_t ofs[EMPTY_ARRAY_SIZE]; /* variable length */
+ /* PsdSelectionListDescriptorExtended */
+} GNUC_PACKED _PsdSelectionListDescriptor;
+
+#define PsdSelectionListDescriptor_SIZEOF 20
+
+typedef struct {
+ struct psd_area_t prev_area;
+ struct psd_area_t next_area;
+ struct psd_area_t return_area;
+ struct psd_area_t default_area;
+ struct psd_area_t area[EMPTY_ARRAY_SIZE]; /* variable length */
+} GNUC_PACKED PsdSelectionListDescriptorExtended;
+
+#define PsdSelectionListDescriptorExtended_SIZEOF 16
+
+typedef struct {
+ uint8_t type;
+ uint16_t command_count;
+ uint16_t lid;
+ uint16_t command[EMPTY_ARRAY_SIZE]; /* variable length */
+} GNUC_PACKED PsdCommandListDescriptor;
+
+#define PsdCommandListDescriptor_SIZEOF 5
+
+typedef struct _PsdPlayListDescriptor_tag {
+ uint8_t type;
+ uint8_t noi; /* number of items */
+ uint16_t lid; /* list id: high-bit means this list is rejected in
+ the LOT (also, can't use 0) */
+ uint16_t prev_ofs; /* previous list offset (0xffff disables) */
+ uint16_t next_ofs; /* next list offset (0xffff disables) */
+ uint16_t return_ofs; /* return list offset (0xffff disables) */
+ uint16_t ptime; /* play time in 1/15 s, 0x0000 meaning full item */
+ uint8_t wtime; /* delay after, in seconds, if 1 <= wtime <= 60 wait
+ is wtime else if 61 <= wtime <= 254 wait is
+ (wtime-60) * 10 + 60 else wtime == 255 wait is
+ infinite */
+ uint8_t atime; /* auto pause wait time calculated same as wtime,
+ used for each item in list if the auto pause flag
+ in a sector is true */
+ uint16_t itemid[EMPTY_ARRAY_SIZE]; /* item number
+ 0 <= n <= 1 - play nothing
+ 2 <= n <= 99 - play track n
+ 100 <= n <= 599 - play entry
+ (n - 99) from entries
+ table to end of track
+ 600 <= n <= 999 - reserved
+ 1000 <= n <= 2979 - play segment
+ play item (n - 999)
+ 2980 <= n <= 0xffff - reserved */
+} GNUC_PACKED _PsdPlayListDescriptor;
+
+#define PsdPlayListDescriptor_SIZEOF 14
+
+/* TRACKS.SVD
+ SVCD\TRACKS.SVD is a mandatory file which describes the numbers and types
+ of MPEG tracks on the disc. */
+
+/* SVDTrackContent indicates the audio/video content of an MPEG Track */
+
+typedef struct {
+#if defined(BITFIELD_LSBF)
+ bitfield_t audio : 2; /* Audio Content
+ 0x00 : No MPEG audio stream
+ 0x01 : One MPEG{1|2} audio stream
+ 0x02 : Two MPEG{1|2} streams
+ 0x03 : One MPEG2 multi-channel
+ audio stream with
+ extension */
+ bitfield_t video : 3; /* Video Content
+ 0x00 : No MPEG video
+ 0x03 : NTSC video
+ 0x07 : PAL video */
+ bool reserved1 : 1; /* Reserved, must be zero */
+ bitfield_t ogt : 2; /* 0x0 - no OGT substream
+ 0x1 - sub-stream 0 available
+ 0x2 - sub-stream 0 & 1 available
+ 0x3 - all OGT sub-substreams
+ available */
+#else
+ bitfield_t ogt : 2;
+ bool reserved1 : 1;
+ bitfield_t video : 3;
+ bitfield_t audio : 2;
+#endif
+} GNUC_PACKED SVDTrackContent;
+
+#define SVDTrackContent_SIZEOF 1
+
+/* The file contains a series of structures, one for each
+ track, which indicates the track's playing time (in sectors, not actually
+ real time) and contents. */
+
+#define TRACKS_SVD_FILE_ID "TRACKSVD"
+#define TRACKS_SVD_VERSION 0x01
+
+typedef struct {
+ char file_id[sizeof(TRACKS_SVD_FILE_ID)-1]; /* == "TRACKSVD" */
+ uint8_t version; /* == 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint8_t tracks; /* number of MPEG tracks */
+ msf_t playing_time[EMPTY_ARRAY_SIZE]; /* per track, BCD coded
+ mm:ss:ff */
+} GNUC_PACKED TracksSVD;
+
+#define TracksSVD_SIZEOF 11
+
+typedef struct {
+ /* TracksSVD tracks_svd; */
+ SVDTrackContent contents[1]; /* should be [], but C99 doesn't allow it
+ indicates track contents */
+} GNUC_PACKED TracksSVD2;
+
+#define TracksSVD2_SIZEOF SVDTrackContent_SIZEOF
+
+/* VCD30 tracks svd */
+
+typedef struct {
+ char file_id[sizeof(TRACKS_SVD_FILE_ID)-1]; /* == "TRACKSVD" */
+ uint8_t version; /* == 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint8_t tracks; /* number of MPEG tracks */
+ struct {
+ msf_t cum_playing_time; /* BCD coded mm:ss:ff */
+ uint8_t ogt_info;
+ uint8_t audio_info;
+ } GNUC_PACKED track[EMPTY_ARRAY_SIZE];
+} GNUC_PACKED TracksSVD_v30;
+
+#define TracksSVD_v30_SIZEOF 11
+
+/* SEARCH.DAT
+ This file defines where the scan points are. It covers all mpeg tracks
+ together. A scan point at time T is the nearest I-picture in the MPEG
+ stream to the given time T. Scan points are given at every half-second
+ for the entire duration of the disc. */
+
+#define SEARCH_FILE_ID "SEARCHSV"
+#define SEARCH_VERSION 0x01
+#define SEARCH_TIME_INTERVAL 0x01
+
+typedef struct {
+ char file_id[sizeof(SEARCH_FILE_ID)-1]; /* = "SEARCHSV" */
+ uint8_t version; /* = 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint16_t scan_points; /* the number of scan points */
+ uint8_t time_interval; /* The interval of time in
+ between scan points, in units
+ of 0.5 seconds, must be 0x01 */
+ msf_t points[EMPTY_ARRAY_SIZE]; /* The series of scan points */
+} GNUC_PACKED SearchDat;
+
+#define SearchDat_SIZEOF 13
+
+/* SPICONTX.SVD
+ */
+
+#define SPICONTX_FILE_ID "SPICONSV"
+#define SPICONTX_VERSION 0x01
+
+typedef struct {
+ char file_id[sizeof(SPICONTX_FILE_ID)-1]; /* = "SPICONSV" */
+ uint8_t version; /* = 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ struct {
+ uint8_t ogt_info;
+ uint8_t audio_info;
+ } GNUC_PACKED spi[MAX_SEGMENTS];
+ uint8_t reserved2[126]; /* 0x00 */
+} GNUC_PACKED SpicontxSvd;
+
+#define SpicontxSvd_SIZEOF (2*ISO_BLOCKSIZE)
+
+/* SCANDATA.DAT for VCD 2.0 */
+
+#define SCANDATA_FILE_ID "SCAN_VCD"
+#define SCANDATA_VERSION_VCD2 0x02
+#define SCANDATA_VERSION_SVCD 0x01
+
+typedef struct {
+ char file_id[sizeof(SCANDATA_FILE_ID)-1]; /* = "SCAN_VCD" */
+ uint8_t version; /* = 0x02 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint16_t scan_points; /* the number of scan points */
+ msf_t points[EMPTY_ARRAY_SIZE]; /* actual scan points
+ points[time(iframe)/0.5] */
+} GNUC_PACKED ScandataDat_v2;
+
+#define ScandataDat_v2_SIZEOF 12
+
+/* SCANDATA.DAT for SVCD
+ This file fulfills much the same purpose of the SEARCH.DAT file except
+ that this file is mandatory only if the System Profile Tag of the
+ INFO.SVD file is 0x01 (HQ-VCD) and also that it contains sector addresses
+ also for each video Segment Play Items in addition to the regular MPEG
+ tracks. */
+
+typedef struct {
+ char file_id[sizeof(SCANDATA_FILE_ID)-1]; /* = "SCAN_VCD" */
+ uint8_t version; /* = 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint16_t scandata_count; /* number of 3-byte entries in
+ the table */
+ uint16_t track_count; /* number of mpeg tracks on disc */
+ uint16_t spi_count; /* number of consecutively recorded
+ play item segments (as opposed
+ to the number of segment play
+ items). */
+ msf_t cum_playtimes[EMPTY_ARRAY_SIZE]; /* cumulative playing
+ time up to track
+ N. Track time just wraps
+ at 99:59:74 */
+} GNUC_PACKED ScandataDat1;
+
+#define ScandataDat1_SIZEOF 16
+
+typedef struct {
+ /* ScandataDat head; */
+ uint16_t spi_indexes[1]; /* should be [], but C doesn't allow that;
+ Indexes into the following scandata
+ table */
+} GNUC_PACKED ScandataDat2;
+
+#define ScandataDat2_SIZEOF sizeof(uint16_t)
+
+typedef struct {
+ /* ScandataDat2 head; */
+ uint16_t mpegtrack_start_index; /* Index into the
+ following scandata table
+ where the MPEG track
+ scan points start */
+
+ /* The scandata table starts here */
+ struct {
+ uint8_t track_num; /* Track number as in TOC */
+ uint16_t table_offset; /* Index into scandata table */
+ } GNUC_PACKED mpeg_track_offsets[EMPTY_ARRAY_SIZE];
+} GNUC_PACKED ScandataDat3;
+
+#define ScandataDat3_SIZEOF 2
+
+typedef struct {
+ /* ScandataDat3 head; */
+ msf_t scandata_table[1]; /* should be [] but C99 doesn't allow that */
+} GNUC_PACKED ScandataDat4;
+
+#define ScandataDat4_SIZEOF msf_t_SIZEOF
+
+PRAGMA_END_PACKED
+
+#endif /* __VCD_FILES_PRIVATE_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/libvcd/inf.h b/contrib/libvcd/libvcd/inf.h
new file mode 100644
index 000000000..fcd9098e2
--- /dev/null
+++ b/contrib/libvcd/libvcd/inf.h
@@ -0,0 +1,260 @@
+/*!
+ \file inf.h
+
+ Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ \verbatim
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ \endverbatim
+*/
+
+/*
+ Things here refer to lower-level structures using a structure other
+ than vcdinfo_t. For higher-level structures via the vcdinfo_t, see
+ info.h
+*/
+
+#ifndef _VCD_INF_H
+#define _VCD_INF_H
+
+#include <libvcd/info.h>
+
+ const char * vcdinf_area_str (const struct psd_area_t *_area);
+
+ /*!
+ Return a string containing the VCD album id.
+ */
+ const char * vcdinf_get_album_id(const InfoVcd_t *info);
+
+ /*!
+ Get autowait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+ */
+ int vcdinf_get_autowait_time (const PsdPlayListDescriptor_t *d);
+
+ /*!
+ Return the base selection number. VCD_INVALID_BSN is returned if there
+ is an error.
+ */
+ unsigned int vcdinf_get_bsn(const PsdSelectionListDescriptor_t *psd);
+
+ /*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+ The first entry number is 0.
+ */
+ lba_t vcdinf_get_entry_lba(const EntriesVcd_t *entries,
+ unsigned int entry_num);
+
+ const char * vcdinf_get_format_version_str (vcd_type_t vcd_type);
+
+ /*!
+ Return loop count. 0 is infinite loop.
+ */
+ uint16_t vcdinf_get_loop_count (const PsdSelectionListDescriptor_t *psd);
+
+ /*!
+ Return LOT offset
+ */
+ uint16_t vcdinf_get_lot_offset (const LotVcd_t *lot, unsigned int n);
+
+ /*!
+ Return number of bytes in PSD.
+ */
+ uint32_t vcdinf_get_psd_size (const InfoVcd_t *info);
+
+ /*!
+ Return the number of segments in the VCD.
+ */
+ unsigned int vcdinf_get_num_entries(const EntriesVcd_t *entries);
+
+ /*!
+ Return number of LIDs.
+ */
+ lid_t vcdinf_get_num_LIDs (const InfoVcd_t *info);
+
+ /*!
+ Return the number of segments in the VCD.
+ */
+ segnum_t vcdinf_get_num_segments(const InfoVcd_t *info);
+
+ /*!
+ Return the number of menu selections for selection-list descriptor d.
+ */
+ unsigned int vcdinf_get_num_selections(const PsdSelectionListDescriptor_t *d);
+
+ /*!
+ Get play-time value for PsdPlayListDescriptor *d.
+ Time is in 1/15-second units.
+ */
+ uint16_t vcdinf_get_play_time (const PsdPlayListDescriptor_t *d);
+
+ /*!
+ Get timeout offset for PsdPlayListDescriptor *d. Return
+ VCDINFO_INVALID_OFFSET if d is NULL;
+ Time is in seconds unless it is -1 (unlimited).
+ */
+ uint16_t vcdinf_get_timeout_offset (const PsdSelectionListDescriptor_t *d);
+
+ /*!
+ Get timeout wait value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+ */
+ int vcdinf_get_timeout_time (const PsdSelectionListDescriptor_t *d);
+
+ /*!
+ Return the track number for entry n in obj. The first track starts
+ at 1.
+ */
+ track_t vcdinf_get_track(const EntriesVcd_t *entries,
+ const unsigned int entry_num);
+
+ /*!
+ Return the VCD volume num - the number of the CD in the collection.
+ This is a number between 1 and the volume count.
+ */
+ unsigned int vcdinf_get_volume_num(const InfoVcd_t *info);
+
+ /*!
+ Return the VCD volume count - the number of CD's in the collection.
+ */
+ unsigned int vcdinf_get_volume_count(const InfoVcd_t *info);
+
+ /*!
+ Get wait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+ */
+ int vcdinf_get_wait_time (const PsdPlayListDescriptor_t *d);
+
+ /*!
+ Return true if loop has a jump delay
+ */
+ bool vcdinf_has_jump_delay (const PsdSelectionListDescriptor_t *psd);
+
+ /*!
+ Comparison routine used in sorting. We compare LIDs and if those are
+ equal, use the offset.
+ Note: we assume an unassigned LID is 0 and this compares as a high value.
+
+ NOTE: Consider making static.
+ */
+ int vcdinf_lid_t_cmp (vcdinfo_offset_t *a, vcdinfo_offset_t *b);
+
+ /**
+ \fn vcdinf_pld_get_next_offset(const PsdPlayListDescriptor *pld);
+ \brief Get next offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "next" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_pld_get_next_offset(const PsdPlayListDescriptor_t *pld);
+
+ /*!
+ Get the LID from a given play-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or pld is NULL.
+ */
+ uint16_t vcdinf_pld_get_lid(const PsdPlayListDescriptor_t *pld);
+
+ /*!
+ Return the playlist item i in d.
+ */
+ uint16_t vcdinf_pld_get_play_item(const PsdPlayListDescriptor_t *pld,
+ unsigned int i);
+
+ /**
+ \fn vcdinf_pld_get_prev_offset(const PsdSelectionListDescriptor *pld);
+ \brief Get prev offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "prev" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_pld_get_prev_offset(const PsdPlayListDescriptor_t *pld);
+
+ /**
+ \fn vcdinf_pld_get_return_offset(const PsdPlayListDescriptor *pld);
+ \brief Get return offset for a given PLD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "return" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_pld_get_return_offset(const PsdPlayListDescriptor_t *pld);
+
+ /*!
+ Return number of items in LIDs. Return 0 if error or not found.
+ */
+ int vcdinf_pld_get_noi (const PsdPlayListDescriptor_t *pld);
+
+ /**
+ * \fn vcdinfo_psd_get_default_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get next offset for a given PSD selector descriptor.
+ * \return VCDINFO_INVALID_OFFSET is returned on error or if psd is
+ * NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_psd_get_default_offset(const PsdSelectionListDescriptor_t *psd);
+
+ /*!
+ Get the item id for a given selection-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or if psd is NULL.
+ */
+ uint16_t vcdinf_psd_get_itemid(const PsdSelectionListDescriptor_t *psd);
+
+ /*!
+ Get the LID from a given selection-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or psd is NULL.
+ */
+ uint16_t vcdinf_psd_get_lid(const PsdSelectionListDescriptor_t *psd);
+
+ /*!
+ Get the LID rejected status for a given selection-list descriptor.
+ true is also returned d is NULL.
+ */
+ bool
+ vcdinf_psd_get_lid_rejected(const PsdSelectionListDescriptor_t *psd);
+
+ /**
+ \fn vcdinf_psd_get_next_offset(const PsdSelectionListDescriptor *psd);
+ \brief Get "next" offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no
+ "next" entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+ lid_t vcdinf_psd_get_next_offset(const PsdSelectionListDescriptor_t *psd);
+
+ /*!
+ \brief Get offset entry_num for a given PSD selector descriptor.
+ \param d PSD selector containing the entry_num we query
+ \param entry_num entry number that we want the LID offset for.
+ \return VCDINFO_INVALID_OFFSET is returned if d on error or d is
+ NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_psd_get_offset(const PsdSelectionListDescriptor_t *d,
+ unsigned int entry_num);
+ /**
+ \fn vcdinf_psd_get_prev_offset(const PsdPlayListDescriptor *psd);
+ \brief Get "prev" offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no
+ "prev"
+ entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_psd_get_prev_offset(const PsdSelectionListDescriptor_t *psd);
+
+ /**
+ * \fn vcdinf_psd_get_return_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get "return" offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no
+ "return" entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_psd_get_return_offset(const PsdSelectionListDescriptor_t *psd);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /*_VCD_INF_H*/
diff --git a/contrib/libvcd/libvcd/info.h b/contrib/libvcd/libvcd/info.h
new file mode 100644
index 000000000..91b25c956
--- /dev/null
+++ b/contrib/libvcd/libvcd/info.h
@@ -0,0 +1,860 @@
+/*!
+ \file info.h
+
+ Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com>
+
+ \verbatim
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ \endverbatim
+*/
+/*
+ Things here refer to higher-level structures usually accessed via
+ vcdinfo_t. For lower-level access which generally use
+ structures other than vcdinfo_t, see inf.h
+*/
+
+
+#ifndef _VCD_INFO_H
+#define _VCD_INFO_H
+
+#include <libvcd/version.h>
+#include <libvcd/types.h>
+#include <libvcd/files.h>
+#include <cdio/cdio.h>
+#include <cdio/ds.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/*========== Move somewhere else? ================*/
+
+/*! \def Max # characters in an album id. */
+#define MAX_ALBUM_LEN 16
+
+/*! \def Max # of selections allowed in a PBC selection list. */
+#define MAX_PBC_SELECTIONS 99
+
+#define MIN_ENCODED_TRACK_NUM 100
+#define MIN_ENCODED_SEGMENT_NUM 1000
+#define MAX_ENCODED_SEGMENT_NUM 2979
+
+/*!
+ Invalid LBA, Note: VCD player uses the fact that this is a very high
+ value.
+ */
+#define VCDINFO_NULL_LBA CDIO_INVALID_LBA
+
+/*!
+ Invalid LSN, Note: VCD player uses the fact that this is a very high
+ value.
+ */
+#define VCDINFO_NULL_LSN VCDINFO_NULL_LBA
+
+/*========== End move somewhere else? ================*/
+
+/*!
+ Portion of uint16_t which determines whether offset is
+ rejected or not.
+*/
+#define VCDINFO_REJECTED_MASK (0x8000)
+
+/*!
+ Portion of uint16_t which contains the offset.
+*/
+#define VCDINFO_OFFSET_MASK (VCDINFO_REJECTED_MASK-1)
+
+/*!
+ Portion of uint16_t which contains the lid.
+*/
+#define VCDINFO_LID_MASK (VCDINFO_REJECTED_MASK-1)
+
+/*!
+ Constant for invalid track number
+*/
+#define VCDINFO_INVALID_TRACK 0xFF
+
+/*!
+ Constant for invalid LID offset.
+*/
+#define VCDINFO_INVALID_OFFSET 0xFFFF
+
+/*!
+ Constant for ending or "leadout" track.
+*/
+#define VCDINFO_LEADOUT_TRACK 0xaa
+
+/*!
+ Constant for invalid sequence entry.
+*/
+#define VCDINFO_INVALID_ENTRY 0xFFFF
+
+/*!
+ Constant for invalid LID.
+ FIXME: player needs these to be the same.
+ VCDimager code requres 0 for an UNINITIALIZED LID.
+
+*/
+#define VCDINFO_INVALID_LID VCDINFO_INVALID_ENTRY
+#define VCDINFO_UNINIT_LID 0
+
+/*!
+ Constant for invalid itemid
+*/
+#define VCDINFO_INVALID_ITEMID 0xFFFF
+
+/*!
+ Constant for invalid audio type
+*/
+#define VCDINFO_INVALID_AUDIO_TYPE 4
+
+/*!
+ Constant for invalid base selection number (BSN)
+*/
+#define VCDINFO_INVALID_BSN 200
+
+/* The number of sectors allocated in a Video CD segment is a fixed: 150.
+
+ NOTE: The actual number of sectors used is often less and can sometimes
+ be gleaned by looking at the correspoinding ISO 9660 file entry (or
+ by scanning the MPEG segment which may be slow).
+ Some media players get confused by or complain about padding at the end
+ a segment.
+*/
+#define VCDINFO_SEGMENT_SECTOR_SIZE 150
+
+ /* Opaque type used in most routines below. */
+ typedef struct _VcdInfo vcdinfo_obj_t;
+
+ /* See enum in vcd_files_private.h */
+ typedef enum {
+ VCDINFO_FILES_VIDEO_NOSTREAM = 0,
+ VCDINFO_FILES_VIDEO_NTSC_STILL = 1,
+ VCDINFO_FILES_VIDEO_NTSC_STILL2 = 2, /* lo+hires*/
+ VCDINFO_FILES_VIDEO_NTSC_MOTION = 3,
+ VCDINFO_FILES_VIDEO_PAL_STILL = 5,
+ VCDINFO_FILES_VIDEO_PAL_STILL2 = 6, /* lo+hires*/
+ VCDINFO_FILES_VIDEO_PAL_MOTION = 7,
+ VCDINFO_FILES_VIDEO_INVALID = 8
+ } vcdinfo_video_segment_type_t;
+
+ /*!
+ Used in working with LOT - list of offsets and lid's
+ */
+ typedef struct {
+ uint8_t type;
+ lid_t lid;
+ uint16_t offset;
+ bool in_lot; /* Is listed in LOT. */
+ bool ext; /* True if entry comes from offset_x_list. */
+ } vcdinfo_offset_t;
+
+ /*!
+ The kind of entry associated with an selection-item id
+ */
+ /* See corresponding enum in vcd_pbc.h. */
+ typedef enum {
+ VCDINFO_ITEM_TYPE_TRACK,
+ VCDINFO_ITEM_TYPE_ENTRY,
+ VCDINFO_ITEM_TYPE_SEGMENT,
+ VCDINFO_ITEM_TYPE_LID,
+ VCDINFO_ITEM_TYPE_SPAREID2,
+ VCDINFO_ITEM_TYPE_NOTFOUND
+ } vcdinfo_item_enum_t;
+
+ typedef struct {
+ uint16_t num;
+ vcdinfo_item_enum_t type;
+ } vcdinfo_itemid_t;
+
+ typedef enum {
+ VCDINFO_OPEN_ERROR, /* Error */
+ VCDINFO_OPEN_VCD, /* Is VCD of some sort */
+ VCDINFO_OPEN_OTHER /* Is not VCD but something else */
+ } vcdinfo_open_return_t;
+
+ typedef struct
+ {
+
+ psd_descriptor_types descriptor_type;
+ /* Only one of pld or psd is used below. Not all
+ C compiler accept the anonymous unions commented out below. */
+ /* union { */
+ PsdPlayListDescriptor_t *pld;
+ PsdSelectionListDescriptor_t *psd;
+ /* }; */
+
+ } PsdListDescriptor_t;
+
+ /* For backwards compatibility. Don't use PsdListDescriptor. */
+#define PsdListDescriptor PsdListDescriptor_t
+
+ /*!
+ Return the number of audio channels implied by "audio_type".
+ 0 is returned on error.
+ */
+ unsigned int
+ vcdinfo_audio_type_num_channels(const vcdinfo_obj_t *obj,
+ unsigned int audio_type);
+
+ /*!
+ Return a string describing an audio type.
+ */
+ const char * vcdinfo_audio_type2str(const vcdinfo_obj_t *obj,
+ unsigned int audio_type);
+
+ /*!
+ Note first seg_num is 0!
+ */
+ const char *
+ vcdinfo_ogt2str(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ Note first seg_num is 0!
+ */
+ const char *
+ vcdinfo_video_type2str(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ const char *
+ vcdinfo_pin2str (uint16_t itemid);
+
+ /*!
+ \brief Classify itemid_num into the kind of item it is: track #, entry #,
+ segment #.
+ \param itemid is set to contain this classifcation an the converted
+ entry number.
+ */
+ void
+ vcdinfo_classify_itemid (uint16_t itemid_num,
+ /*out*/ vcdinfo_itemid_t *itemid);
+
+ /*!
+ Return a string containing the VCD album id, or NULL if there is
+ some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_album_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD application ID.
+ NULL is returned if there is some problem in getting this.
+ */
+ char *
+ vcdinfo_get_application_id(vcdinfo_obj_t *obj);
+
+ /*!
+ Return a pointer to the cdio structure for the CD image opened or
+ NULL if error.
+ */
+ CdIo *
+ vcdinfo_get_cd_image (const vcdinfo_obj_t *vcd_obj);
+
+ /*!
+ Return a string containing the default VCD device if none is specified.
+ This might be something like "/dev/cdrom" on Linux or
+ "/vol/dev/aliases/cdrom0" on Solaris, or maybe "VIDEOCD.CUE" for
+ if bin/cue I/O routines are in effect.
+
+ Return NULL we can't get this information.
+ */
+ char *
+ vcdinfo_get_default_device (const vcdinfo_obj_t *vcd_obj);
+
+ /*!
+ \brief Get default LID offset.
+
+ Return the LID offset associated with a the "default" entry of the
+ passed-in LID parameter. Note "default" entries are associated with
+ PSDs that are (extended) selection lists.
+
+ \return VCDINFO_INVALID_OFFSET is returned on error, or if the LID
+ is not a selection list or no "default" entry. Otherwise the LID
+ offset is returned.
+ */
+ uint16_t
+ vcdinfo_get_default_offset(const vcdinfo_obj_t *obj, lid_t lid);
+
+ /*!
+ Return number of sector units in of an entry. 0 is returned if
+ entry_num is invalid.
+ */
+ uint32_t
+ vcdinfo_get_entry_sect_count (const vcdinfo_obj_t *obj,
+ unsigned int entry_num);
+
+ /*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+ The first entry number is 0.
+ */
+ lba_t
+ vcdinfo_get_entry_lba(const vcdinfo_obj_t *obj, unsigned int entry_num);
+
+ /*! Return the starting LSN (logical sector number) for sequence
+ entry_num in obj. VCDINFO_NULL_LSN is returned if there is no entry.
+ The first entry number is 0.
+ */
+ lsn_t
+ vcdinfo_get_entry_lsn(const vcdinfo_obj_t *obj, unsigned int entry_num);
+
+ /*! Return the starting MSF (minutes/secs/frames) for sequence
+ entry_num in obj. NULL is returned if there is no entry.
+ The first entry number is 0.
+ */
+ const msf_t *
+ vcdinfo_get_entry_msf(const vcdinfo_obj_t *obj, unsigned int entry_num);
+
+ /*!
+ Get the VCD format (VCD 1.0 VCD 1.1, SVCD, ... for this object.
+ The type is also set inside obj.
+ The first entry number is 0.
+ */
+ vcd_type_t
+ vcdinfo_get_format_version (vcdinfo_obj_t *obj);
+
+ /*!
+ Return a string giving VCD format (VCD 1.0 VCD 1.1, SVCD, ...
+ for this object.
+ */
+ const char *
+ vcdinfo_get_format_version_str (const vcdinfo_obj_t *obj);
+
+ EntriesVcd_t * vcdinfo_get_entriesVcd (vcdinfo_obj_t *obj);
+
+ InfoVcd_t * vcdinfo_get_infoVcd (vcdinfo_obj_t *obj);
+
+ /*!
+ \brief Get default or multi-default LID.
+
+ Return the LID offset associated with a the "default" entry of the
+ passed-in LID parameter. Note "default" entries are associated
+ with PSDs that are (extended) selection lists. If the "default"
+ is a multi-default, we use entry_num to find the proper
+ "default" LID. Otherwise this routine is exactly like
+ vcdinfo_get_default_offset with the exception of requiring an
+ additional "entry_num" parameter.
+
+ \return VCDINFO_INVALID_LID is returned on error, or if the LID
+ is not a selection list or no "default" entry. Otherwise the LID
+ offset is returned.
+ */
+ lid_t
+ vcdinfo_get_multi_default_lid(const vcdinfo_obj_t *obj, lid_t lid,
+ lsn_t lsn);
+
+ /*!
+ \brief Get default or multi-default LID offset.
+
+ Return the LID offset associated with a the "default" entry of the
+ passed-in LID parameter. Note "default" entries are associated
+ with PSDs that are (extended) selection lists. If the "default"
+ is a multi-default, we use entry_num to find the proper
+ "default" offset. Otherwise this routine is exactly like
+ vcdinfo_get_default_offset with the exception of requiring an
+ additional "entry_num" parameter.
+
+ \return VCDINFO_INVALID_OFFSET is returned on error, or if the LID
+ is not a selection list or no "default" entry. Otherwise the LID
+ offset is returned.
+ */
+ uint16_t
+ vcdinfo_get_multi_default_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int selection);
+
+ void * vcdinfo_get_pvd (vcdinfo_obj_t *obj);
+
+ void * vcdinfo_get_scandata (vcdinfo_obj_t *obj);
+
+ void * vcdinfo_get_searchDat (vcdinfo_obj_t *obj);
+
+ void * vcdinfo_get_tracksSVD (vcdinfo_obj_t *obj);
+
+ /*!
+ Get the LOT pointer.
+ */
+ LotVcd_t *
+ vcdinfo_get_lot(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the extended LOT pointer.
+ */
+ LotVcd_t *
+ vcdinfo_get_lot_x(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return Number of LIDs.
+ */
+ lid_t
+ vcdinfo_get_num_LIDs (const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the audio type for a given track.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+ */
+ unsigned int
+ vcdinfo_get_num_audio_channels(unsigned int audio_type);
+
+ /*!
+ Return the number of entries in the VCD.
+ */
+ unsigned int
+ vcdinfo_get_num_entries(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the number of segments in the VCD.
+ */
+ segnum_t
+ vcdinfo_get_num_segments(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the highest track number in the current medium.
+
+ Because we track start numbering at 0 (which is the ISO 9660 track
+ containing Video CD naviagion and disk information), this is one
+ less than the number of tracks.
+
+ If there are no tracks, we return -1.
+ */
+ unsigned int
+ vcdinfo_get_num_tracks(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the VCD info list.
+ */
+ CdioList *vcdinfo_get_offset_list(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the VCD info extended offset list.
+ */
+ CdioList *vcdinfo_get_offset_x_list(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the VCD info offset multiplier.
+ */
+ unsigned int vcdinfo_get_offset_mult(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get entry in offset list for the item that has offset. This entry
+ has for example the LID. NULL is returned on error.
+ */
+ vcdinfo_offset_t *
+ vcdinfo_get_offset_t (const vcdinfo_obj_t *obj, unsigned int offset);
+
+ /*!
+ Return a string containing the VCD preparer id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_preparer_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the PSD.
+ */
+ uint8_t *vcdinfo_get_psd(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the extended PSD.
+ */
+ uint8_t *vcdinfo_get_psd_x(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return number of bytes in PSD.
+ */
+ uint32_t vcdinfo_get_psd_size (const vcdinfo_obj_t *obj);
+
+ /*!
+ Return number of bytes in the extended PSD.
+ */
+ uint32_t vcdinfo_get_psd_x_size (const vcdinfo_obj_t *obj);
+
+ /*!
+ Return a string containing the VCD publisher id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+ */
+ char * vcdinfo_get_publisher_id(const vcdinfo_obj_t *obj);
+
+ /**
+ \fn vcdinfo_get_return_offset(const vcdinfo_obj_t *obj);
+ \brief Get return offset for a given LID.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if LID has no
+ "return" entry. Otherwise the LID offset is returned.
+ */
+ lid_t
+ vcdinfo_get_return_offset(const vcdinfo_obj_t *obj, lid_t lid);
+
+ /*!
+ Return the audio type for a given segment.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+ */
+ unsigned int
+ vcdinfo_get_seg_audio_type(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ Return true if this segment is supposed to continue to the next one,
+ (is part of an "item" or listing in the ISO 9660 filesystem).
+ */
+ bool vcdinfo_get_seg_continue(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*! Return the starting LBA (logical block address) for segment
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+
+ Note first seg_num is 0.
+ */
+ lba_t
+ vcdinfo_get_seg_lba(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*! Return the starting LSN (logical sector number) for segment
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+
+ Note first seg_num is 0.
+ */
+ lsn_t
+ vcdinfo_get_seg_lsn(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*! Return the starting MSF (minutes/secs/frames) for segment
+ entry_num in obj. NULL is returned if there is no entry.
+
+ Note first seg_num is 0.
+ */
+ const msf_t *
+ vcdinfo_get_seg_msf(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ Return the number of sectors for segment
+ entry_num in obj. 0 is returned if there is no entry.
+
+ Use this routine to figure out the actual number of bytes a physical
+ region of a disk or CD takes up for a segment.
+
+ If an item has been broken up into a number of "continued" segments,
+ we will report the item size for the first segment and 0 for the
+ remaining ones. We may revisit this decision later.
+ */
+ uint32_t
+ vcdinfo_get_seg_sector_count(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ Return a string containing the VCD system id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_system_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the track number for entry n in obj.
+
+ In contrast to libcdio we start numbering at 0 which is the
+ ISO9660 and metadata information for the Video CD. Thus track
+ 1 is the first track the first complete MPEG track generally.
+ */
+ track_t
+ vcdinfo_get_track(const vcdinfo_obj_t *obj, const unsigned int entry_num);
+
+ /*!
+ Return the audio type for a given track.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+
+ Note: track 1 is usually the first track.
+ */
+ unsigned int
+ vcdinfo_get_track_audio_type(const vcdinfo_obj_t *obj, track_t track_num);
+
+ /*!
+ Return the starting LBA (logical block address) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+ */
+ lba_t
+ vcdinfo_get_track_lba(const vcdinfo_obj_t *obj, track_t track_num);
+
+ /*!
+ Return the starting LSN (logical sector number) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+ */
+ lsn_t
+ vcdinfo_get_track_lsn(const vcdinfo_obj_t *obj, track_t track_num);
+
+ /*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+ */
+ int
+ vcdinfo_get_track_msf(const vcdinfo_obj_t *obj, track_t track_num,
+ uint8_t *min, uint8_t *sec, uint8_t *frame);
+
+ /*!
+ Return the size in sectors for track n.
+
+ The IS0-9660 filesystem track has number 1. Tracks associated
+ with playable entries numbers start at 2.
+
+ FIXME: Whether we count the track pregap sectors is a bit haphazard.
+ We should add a parameter to indicate whether this is wanted or not.
+ */
+ unsigned int
+ vcdinfo_get_track_sect_count(const vcdinfo_obj_t *obj,
+ const track_t track_num);
+
+ /*!
+ Return size in bytes for track number for entry n in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ FIXME: Do we count the track pregap sectors is a bit haphazard.
+ We should add a parameter to indicate whether this is wanted or not.
+ */
+ unsigned int
+ vcdinfo_get_track_size(const vcdinfo_obj_t *obj, track_t track_num);
+
+ /*!
+ \brief Get the kind of video stream segment of segment seg_num in obj.
+ \return VCDINFO_FILES_VIDEO_INVALID is returned if on error or obj is
+ null. Otherwise the enumeration type.
+
+ Note first seg_num is 0!
+ */
+ vcdinfo_video_segment_type_t
+ vcdinfo_get_video_type(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ \brief Get the kind of VCD that obj refers to.
+ */
+ vcd_type_t
+ vcdinfo_get_VCD_type(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD volume count - the number of CD's in the collection.
+ O is returned if there is some problem in getting this.
+ */
+ unsigned int
+ vcdinfo_get_volume_count(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD ID.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_volume_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD volumeset ID.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_volumeset_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD volume num - the number of the CD in the collection.
+ This is a number between 1 and the volume count.
+ O is returned if there is some problem in getting this.
+ */
+ unsigned int
+ vcdinfo_get_volume_num(const vcdinfo_obj_t *obj);
+
+ int vcdinfo_get_wait_time (uint16_t wtime);
+
+ /*!
+ Return true if there is playback control.
+ */
+ bool vcdinfo_has_pbc (const vcdinfo_obj_t *obj);
+
+ /*!
+ Return true if VCD has "extended attributes" (XA). Extended attributes
+ add meta-data attributes to a entries of file describing the file.
+ See also cdio_get_xa_attr_str() which returns a string similar to
+ a string you might get on a Unix filesystem listing ("ls").
+ */
+ bool vcdinfo_has_xa(const vcdinfo_obj_t *obj);
+
+ /*!
+ Add one to the MSF.
+ */
+ void vcdinfo_inc_msf (uint8_t *min, uint8_t *sec, int8_t *frame);
+
+ /*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical block address (or LBA).
+ See also msf_to_lba which uses msf_t as its single parameter.
+ */
+ void
+ vcdinfo_lba2msf (lba_t lba, uint8_t *min, uint8_t *sec, uint8_t *frame);
+
+ /*!
+ Get the item id for a given list ID.
+ VCDINFO_REJECTED_MASK is returned on error or if obj is NULL.
+ */
+ uint16_t
+ vcdinfo_lid_get_itemid(const vcdinfo_obj_t *obj, lid_t lid);
+
+ /*!
+ \fn vcdinfo_lid_get_offset(const vcdinfo_obj_t *obj,
+ unsigned int entry_num);
+ \brief Get offset entry_num for a given LID.
+ \return VCDINFO_INVALID_OFFSET is returned if obj on error or obj
+ is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinfo_lid_get_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int entry_num);
+
+ /*!
+ Get the PSD Selection List Descriptor for a given lid.
+ False is returned if not found.
+ */
+ bool vcdinfo_lid_get_pxd(const vcdinfo_obj_t *obj, PsdListDescriptor_t *pxd,
+ uint16_t lid);
+
+ /*! Return the entry number closest and before the given LSN.
+ */
+ unsigned int
+ vcdinfo_lsn_get_entry(const vcdinfo_obj_t *obj, lsn_t lsn);
+
+ /*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical block address (or LBA).
+ See also msf_to_lba which uses msf_t as its single parameter.
+ */
+ lba_t vcdinfo_msf2lba (uint8_t min, uint8_t sec, int8_t frame);
+
+ /*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical sector number (or LSN).
+ */
+ lsn_t vcdinfo_msf2lsn (uint8_t min, uint8_t sec, int8_t frame);
+
+ const char *
+ vcdinfo_ofs2str (const vcdinfo_obj_t *obj, unsigned int offset, bool ext);
+
+ /*!
+ Calls recursive routine to populate obj->offset_list or obj->offset_x_list
+ by going through LOT.
+
+ Returns false if there was some error.
+ */
+ bool vcdinfo_visit_lot (vcdinfo_obj_t *obj, bool extended);
+
+ bool vcdinfo_read_psd (vcdinfo_obj_t *obj);
+
+ /*!
+ \fn vcdinfo_selection_get_lid(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int selection);
+ \brief Get the "default" lid of a selection for a given lid.
+
+ Return the LID offset associated with a the selection number of the
+ passed-in LID parameter.
+
+ \return VCDINFO_INVALID_LID is returned if obj on error or obj
+ is NULL. Otherwise the LID offset is returned.
+ */
+ lid_t vcdinfo_selection_get_lid(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int selection);
+
+ /*!
+ \fn vcdinfo_selection_get_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int selection);
+ \brief Get offset of a selection for a given LID.
+
+ Return the LID offset associated with a the selection number of the
+ passed-in LID parameter.
+
+ \return VCDINFO_INVALID_OFFSET is returned if obj on error or obj
+ is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinfo_selection_get_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int selection);
+
+ /*!
+ Change trailing blanks in str to nulls. Str has a maximum size of
+ n characters.
+ */
+ const char * vcdinfo_strip_trail (const char str[], size_t n);
+
+ /*! Return the entry number for the given track.
+ */
+ unsigned int
+ vcdinfo_track_get_entry(const vcdinfo_obj_t *obj, track_t track);
+
+ /*!
+ Initialize the vcdinfo structure "obj". Should be done before other
+ routines using obj are called.
+ */
+ bool vcdinfo_init(vcdinfo_obj_t *obj);
+
+ /*!
+ Set up vcdinfo structure "obj" for reading from a particular
+ medium. This should be done before after initialization but before
+ any routines that need to retrieve data.
+
+ source_name is the device or file to use for inspection, and
+ source_type indicates what driver to use or class of drivers in the
+ case of DRIVER_DEVICE.
+ access_mode gives the CD access method for reading should the driver
+ allow for more than one kind of access method (e.g. MMC versus ioctl
+ on GNU/Linux)
+
+ If source_name is NULL we'll fill in the appropriate default device
+ name for the given source_type. However if in addtion source_type is
+ DRIVER_UNKNOWN, then we'll scan for a drive containing a VCD.
+
+ VCDINFO_OPEN_VCD is returned if everything went okay;
+ VCDINFO_OPEN_ERROR if there was an error and VCDINFO_OPEN_OTHER if the
+ medium is something other than a VCD.
+ */
+ vcdinfo_open_return_t
+ vcdinfo_open(vcdinfo_obj_t **p_obj, char *source_name[],
+ driver_id_t source_type, const char access_mode[]);
+
+
+ /*!
+ Dispose of any resources associated with vcdinfo structure "obj".
+ Call this when "obj" it isn't needed anymore.
+
+ True is returned is everything went okay, and false if not.
+ */
+ bool vcdinfo_close(vcdinfo_obj_t *obj);
+
+ /*!
+ Return true if offset is "rejected". That is shouldn't be displayed
+ in a list of entries.
+ */
+ bool vcdinfo_is_rejected(uint16_t offset);
+
+/* Include lower-level access as well. */
+#include <libvcd/inf.h>
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /*_VCD_INFO_H*/
diff --git a/contrib/libvcd/libvcd/logging.h b/contrib/libvcd/libvcd/logging.h
new file mode 100644
index 000000000..bc088ed1c
--- /dev/null
+++ b/contrib/libvcd/libvcd/logging.h
@@ -0,0 +1,137 @@
+/*
+ $Id: logging.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_LOGGING_H__
+#define __VCD_LOGGING_H__
+
+#include <libvcd/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/**
+ * The different log levels supported.
+ */
+typedef enum {
+ VCD_LOG_DEBUG = 1, /**< Debug-level messages - helps debug what's up. */
+ VCD_LOG_INFO, /**< Informational - indicates perhaps something of
+ interest. */
+ VCD_LOG_WARN, /**< Warning conditions - something that looks funny. */
+ VCD_LOG_ERROR, /**< Error conditions - may terminate program. */
+ VCD_LOG_ASSERT /**< Critical conditions - may abort program. */
+} vcd_log_level_t;
+
+/**
+ * The place to save the preference concerning how much verbosity
+ * is desired. This is used by the internal default log handler, but
+ * it could be use by applications which provide their own log handler.
+ */
+extern vcd_log_level_t vcd_loglevel_default;
+
+/**
+ * This type defines the signature of a log handler. For every
+ * message being logged, the handler will receive the log level and
+ * the message string.
+ *
+ * @see vcd_log_set_handler
+ * @see vcd_log_level_t
+ *
+ * @param level The log level.
+ * @param message The log message.
+ */
+typedef void (*vcd_log_handler_t) (vcd_log_level_t level,
+ const char message[]);
+
+/**
+ * Set a custom log handler for libcdio. The return value is the log
+ * handler being replaced. If the provided parameter is NULL, then
+ * the handler will be reset to the default handler.
+ *
+ * @see vcd_log_handler_t
+ *
+ * @param new_handler The new log handler.
+ * @return The previous log handler.
+ */
+vcd_log_handler_t
+vcd_log_set_handler (vcd_log_handler_t new_handler);
+
+/**
+ * Handle an message with the given log level
+ *
+ * @see vcd_debug
+ * @see vcd_info
+ * @see vcd_warn
+ * @see vcd_error
+
+ * @param level The log level.
+ * @param format printf-style format string
+ * @param ... remaining arguments needed by format string
+ */
+void
+vcd_log (vcd_log_level_t level, const char format[], ...) GNUC_PRINTF(2, 3);
+
+/**
+ * Handle a debugging message.
+ *
+ * @see vcd_log for a more generic routine
+ */
+void
+vcd_debug (const char format[], ...) GNUC_PRINTF(1,2);
+
+/**
+ * Handle an informative message.
+ *
+ * @see vcd_log for a more generic routine
+ */
+void
+vcd_info (const char format[], ...) GNUC_PRINTF(1,2);
+
+/**
+ * Handle a warning message.
+ *
+ * @see vcd_log for a more generic routine
+ */
+void
+vcd_warn (const char format[], ...) GNUC_PRINTF(1,2);
+
+/**
+ * Handle an error message.
+ *
+ * @see vcd_log for a more generic routine.
+ */
+void
+vcd_error (const char format[], ...) GNUC_PRINTF(1,2);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __VCD_LOGGING_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/libvcd/sector.h b/contrib/libvcd/libvcd/sector.h
new file mode 100644
index 000000000..32c2cc89a
--- /dev/null
+++ b/contrib/libvcd/libvcd/sector.h
@@ -0,0 +1,118 @@
+/*
+ $Id: sector.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef _VCD_SECTOR_H_
+#define _VCD_SECTOR_H_
+
+#include <libvcd/types.h>
+
+/* subheader */
+
+/*
+
+ SVCD 1.0
+ ~~~~~~~~
+
+ empty sector: fn=0 cn=0 sm=%00100000 ci=0
+ data sector: fn=0 cn=0 sm=%x0001000 ci=0
+ mpeg sector: fn=1 cn=1 sm=%x11x001x ci=0x80
+
+ VCD 2.0
+ ~~~~~~~
+
+ /MPEGAV/AVSEQyy.DAT
+ empty sector: fn=yy cn=0 sm=%x11x000x ci=0
+ video sector: fn=yy cn=1 sm=%x11x001x ci=0x0f
+ audio sector: fn=yy cn=1 sm=%x11x010x ci=0x7f
+
+ /SEGMENT/ITEMzzzz.DAT
+ empty sector: fn=1 cn=0 sm=%x11x000x ci=0
+ video sector: fn=1 cn=1 sm=%x11x001x ci=0x0f
+ lores still: fn=1 cn=2 sm=%x11x001x ci=0x1f
+ hires still: fn=1 cn=3 sm=%x11x001x ci=0x3f
+ audio sector: fn=1 cn=1 sm=%x11x010x ci=0x7f
+
+ /VCD/ *.VCD
+ data sector: fn=0 cn=0 sm=%x000100x ci=0
+
+ *.*
+ data sector: fn=1 cn=0 sm=%x0001000 ci=0
+
+*/
+
+/* file numbers */
+
+/* dynamic */
+
+/* channel numbers */
+#define CN_VIDEO 0x01
+#define CN_STILL 0x02
+#define CN_STILL2 0x03
+#define CN_AUDIO 0x01
+#define CN_AUDIO2 0x02
+#define CN_OGT 0x02 /* fixme -- is it 0x04 ?? */
+#define CN_PAD 0x00
+#define CN_EMPTY 0x00
+
+/* submode byte */
+#define SM_EOF (1<<7)
+#define SM_REALT (1<<6)
+#define SM_FORM2 (1<<5)
+#define SM_TRIG (1<<4)
+#define SM_DATA (1<<3)
+#define SM_AUDIO (1<<2)
+#define SM_VIDEO (1<<1)
+#define SM_EOR (1<<0)
+
+/* coding information */
+#define CI_VIDEO 0x0f
+#define CI_STILL 0x1f
+#define CI_STILL2 0x3f
+#define CI_AUDIO 0x7f
+#define CI_AUDIO2 0x7f
+#define CI_OGT 0x0f
+#define CI_PAD 0x1f
+#define CI_MPEG2 0x80
+#define CI_EMPTY 0x00
+
+/* make mode 2 form 1/2 sector
+ *
+ * data must be a buffer of size 2048 or 2324 for SM_FORM2
+ * raw_sector must be a writable buffer of size 2352
+ */
+void
+_vcd_make_mode2 (void *raw_sector, const void *data, uint32_t extent,
+ uint8_t fnum, uint8_t cnum, uint8_t sm, uint8_t ci);
+
+/* ...data must be a buffer of size 2336 */
+
+void
+_vcd_make_raw_mode2 (void *raw_sector, const void *data, uint32_t extent);
+
+#endif /* _VCD_SECTOR_H_ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/libvcd/types.h b/contrib/libvcd/libvcd/types.h
new file mode 100644
index 000000000..56a07d447
--- /dev/null
+++ b/contrib/libvcd/libvcd/types.h
@@ -0,0 +1,97 @@
+/*
+ $Id: types.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_TYPES_H__
+#define __VCD_TYPES_H__
+
+#include <cdio/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /* Opaque types ... */
+
+ /* Defined fully in data_structures.c */
+ typedef struct _VcdList VcdList;
+ typedef struct _VcdListNode VcdListNode;
+
+ /* Defined fully in files_private.h */
+ typedef struct _InfoVcd_tag InfoVcd_t;
+ typedef struct _EntriesVcd_tag EntriesVcd_t;
+ typedef struct _LotVcd_tag LotVcd_t;
+
+ typedef struct _PsdPlayListDescriptor_tag PsdPlayListDescriptor_t;
+ typedef struct _PsdSelectionListDescriptor_tag PsdSelectionListDescriptor_t;
+
+ /* Overall data structure representing a VideoCD object.
+ Defined fully in info_private.h.
+ */
+ typedef struct _VcdObj VcdObj;
+
+ /* enum defining supported VideoCD types */
+ typedef enum
+ {
+ VCD_TYPE_INVALID = 0,
+ VCD_TYPE_VCD,
+ VCD_TYPE_VCD11,
+ VCD_TYPE_VCD2,
+ VCD_TYPE_SVCD,
+ VCD_TYPE_HQVCD
+ }
+ vcd_type_t;
+
+ /* The type of an playback control list ID (LID). */
+ typedef uint16_t lid_t;
+
+ /* The type of a segment number 0..1980 segment items possible. */
+ typedef uint16_t segnum_t;
+
+ /* (0,0) == upper left , (255,255) == lower right
+ setting all to zero disables area */
+ PRAGMA_BEGIN_PACKED
+ struct psd_area_t
+ {
+ uint8_t x1; /* upper left */
+ uint8_t y1; /* upper left */
+ uint8_t x2; /* lower right */
+ uint8_t y2; /* lower right */
+ } GNUC_PACKED;
+ PRAGMA_END_PACKED
+
+#define struct_psd_area_t_SIZEOF 4
+
+#define PSD_OFS_DISABLED 0xffff
+#define PSD_OFS_MULTI_DEF 0xfffe
+#define PSD_OFS_MULTI_DEF_NO_NUM 0xfffd
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __VCD_TYPES_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/libvcd/version.h b/contrib/libvcd/libvcd/version.h
new file mode 100644
index 000000000..0cd2096d3
--- /dev/null
+++ b/contrib/libvcd/libvcd/version.h
@@ -0,0 +1,11 @@
+/* $Id: version.h,v 1.4 2005/04/27 23:28:42 rockyb Exp $ */
+/** \file version.h
+ * \brief A file simply containing the library version number.
+ */
+
+/*! LIBVCD_VERSION can as a string in programs to show what version is used. */
+#define LIBVCD_VERSION "0.7.21-"
+
+/*! LIBCDIO_VERSION_NUM can be used for testing in the C preprocessor */
+#define LIBVCD_VERSION_NUM 21
+
diff --git a/contrib/libvcd/logging.c b/contrib/libvcd/logging.c
new file mode 100644
index 000000000..652a8090b
--- /dev/null
+++ b/contrib/libvcd/logging.c
@@ -0,0 +1,145 @@
+/*
+ $Id: logging.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+/* Public headers */
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+
+static const char _rcsid[] = "$Id: logging.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $";
+
+vcd_log_level_t vcd_loglevel_default = VCD_LOG_WARN;
+
+static void
+default_vcd_log_handler (vcd_log_level_t level, const char message[])
+{
+ switch (level)
+ {
+ case VCD_LOG_ERROR:
+ if (level >= vcd_loglevel_default) {
+ fprintf (stderr, "**ERROR: %s\n", message);
+ fflush (stderr);
+ exit (EXIT_FAILURE);
+ }
+ break;
+ case VCD_LOG_DEBUG:
+ if (level >= vcd_loglevel_default) {
+ fprintf (stdout, "--DEBUG: %s\n", message);
+ }
+ break;
+ case VCD_LOG_WARN:
+ if (level >= vcd_loglevel_default) {
+ fprintf (stdout, "++ WARN: %s\n", message);
+ }
+ break;
+ case VCD_LOG_INFO:
+ if (level >= vcd_loglevel_default) {
+ fprintf (stdout, " INFO: %s\n", message);
+ }
+ break;
+ case VCD_LOG_ASSERT:
+ if (level >= vcd_loglevel_default) {
+ fprintf (stderr, "!ASSERT: %s\n", message);
+ fflush (stderr);
+ }
+ abort ();
+ break;
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ fflush (stdout);
+}
+
+static vcd_log_handler_t _handler = default_vcd_log_handler;
+
+vcd_log_handler_t
+vcd_log_set_handler (vcd_log_handler_t new_handler)
+{
+ vcd_log_handler_t old_handler = _handler;
+
+ _handler = new_handler;
+
+ return old_handler;
+}
+
+static void
+vcd_logv (vcd_log_level_t level, const char format[], va_list args)
+{
+ char buf[1024] = { 0, };
+ static int in_recursion = 0;
+
+ if (in_recursion)
+ vcd_assert_not_reached ();
+
+ in_recursion = 1;
+
+ vsnprintf(buf, sizeof(buf)-1, format, args);
+
+ _handler(level, buf);
+
+ in_recursion = 0;
+}
+
+void
+vcd_log (vcd_log_level_t level, const char format[], ...)
+{
+ va_list args;
+ va_start (args, format);
+ vcd_logv (level, format, args);
+ va_end (args);
+}
+
+#define VCD_LOG_TEMPLATE(level, LEVEL) \
+void \
+vcd_ ## level (const char format[], ...) \
+{ \
+ va_list args; \
+ va_start (args, format); \
+ vcd_logv (VCD_LOG_ ## LEVEL, format, args); \
+ va_end (args); \
+}
+
+VCD_LOG_TEMPLATE(debug, DEBUG)
+VCD_LOG_TEMPLATE(info, INFO)
+VCD_LOG_TEMPLATE(warn, WARN)
+VCD_LOG_TEMPLATE(error, ERROR)
+
+#undef VCD_LOG_TEMPLATE
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/mpeg.c b/contrib/libvcd/mpeg.c
new file mode 100644
index 000000000..545a44fbc
--- /dev/null
+++ b/contrib/libvcd/mpeg.c
@@ -0,0 +1,1177 @@
+/*
+ $Id: mpeg.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+
+#include <cdio/cdio.h>
+
+/* Private headers */
+#include "bitvec.h"
+#include "mpeg.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: mpeg.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $";
+
+#define MPEG_START_CODE_PATTERN ((uint32_t) 0x00000100)
+#define MPEG_START_CODE_MASK ((uint32_t) 0xffffff00)
+
+#define MPEG_PICTURE_CODE ((uint32_t) 0x00000100)
+/* [...slice codes... 0x1a7] */
+
+#define MPEG_USER_CODE ((uint32_t) 0x000001b2)
+#define MPEG_SEQUENCE_CODE ((uint32_t) 0x000001b3)
+#define MPEG_EXT_CODE ((uint32_t) 0x000001b5)
+#define MPEG_SEQ_END_CODE ((uint32_t) 0x000001b7)
+#define MPEG_GOP_CODE ((uint32_t) 0x000001b8)
+#define MPEG_PROGRAM_END_CODE ((uint32_t) 0x000001b9)
+#define MPEG_PACK_HEADER_CODE ((uint32_t) 0x000001ba)
+#define MPEG_SYSTEM_HEADER_CODE ((uint32_t) 0x000001bb)
+#define MPEG_PRIVATE_1_CODE ((uint32_t) 0x000001bd)
+#define MPEG_PAD_CODE ((uint32_t) 0x000001be)
+
+#define MPEG_AUDIO_C0_CODE ((uint32_t) 0x000001c0) /* default */
+#define MPEG_AUDIO_C1_CODE ((uint32_t) 0x000001c1) /* 2nd audio stream id (dual channel) */
+#define MPEG_AUDIO_C2_CODE ((uint32_t) 0x000001c2) /* 3rd audio stream id (surround sound) */
+
+#define MPEG_VIDEO_E0_CODE ((uint32_t) 0x000001e0) /* motion */
+#define MPEG_VIDEO_E1_CODE ((uint32_t) 0x000001e1) /* lowres still */
+#define MPEG_VIDEO_E2_CODE ((uint32_t) 0x000001e2) /* hires still */
+
+#define PICT_TYPE_I 1
+#define PICT_TYPE_P 2
+#define PICT_TYPE_B 3
+#define PICT_TYPE_D 4
+
+static struct {
+ mpeg_norm_t norm;
+ unsigned hsize;
+ unsigned vsize;
+ int frate_idx;
+} const norm_table[] = {
+ { MPEG_NORM_FILM, 352, 240, 1 },
+ { MPEG_NORM_PAL, 352, 288, 3 },
+ { MPEG_NORM_NTSC, 352, 240, 4 },
+ { MPEG_NORM_PAL_S, 480, 576, 3 },
+ { MPEG_NORM_NTSC_S, 480, 480, 4 },
+ { MPEG_NORM_OTHER, }
+};
+
+static const double frame_rates[16] = {
+ 0.0, 24000.0/1001, 24.0, 25.0,
+ 30000.0/1001, 30.0, 50.0, 60000.0/1001,
+ 60.0, 0.0,
+};
+
+#ifdef DEBUG
+# define MARKER(buf, offset) \
+ vcd_assert (vcd_bitvec_read_bit (buf, offset) == 1)
+#else
+# define MARKER(buf, offset) \
+ { if (GNUC_UNLIKELY (vcd_bitvec_read_bit (buf, offset) != 1)) vcd_debug ("mpeg: some marker is not set..."); }
+#endif
+
+static inline bool
+_start_code_p (uint32_t code)
+{
+ return (code & MPEG_START_CODE_MASK) == MPEG_START_CODE_PATTERN;
+}
+
+static inline int
+_vid_streamid_idx (uint8_t streamid)
+{
+ switch (streamid | MPEG_START_CODE_PATTERN)
+ {
+ case MPEG_VIDEO_E0_CODE:
+ return 0;
+ break;
+
+ case MPEG_VIDEO_E1_CODE:
+ return 1;
+ break;
+
+ case MPEG_VIDEO_E2_CODE:
+ return 2;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return -1;
+}
+
+static inline int
+_aud_streamid_idx (uint8_t streamid)
+{
+ switch (streamid | MPEG_START_CODE_PATTERN)
+ {
+ case MPEG_AUDIO_C0_CODE:
+ return 0;
+ break;
+
+ case MPEG_AUDIO_C1_CODE:
+ return 1;
+ break;
+
+ case MPEG_AUDIO_C2_CODE:
+ return 2;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return -1;
+}
+
+/* used for SCR, PTS and DTS */
+static inline uint64_t
+_parse_timecode (const uint8_t *buf, unsigned *offset)
+{
+ uint64_t _retval;
+
+ _retval = vcd_bitvec_read_bits (buf, offset, 3);
+
+ MARKER (buf, offset);
+
+ _retval <<= 15;
+ _retval |= vcd_bitvec_read_bits (buf, offset, 15);
+
+ MARKER (buf, offset);
+
+ _retval <<= 15;
+ _retval |= vcd_bitvec_read_bits (buf, offset, 15);
+
+ MARKER (buf, offset);
+
+ return _retval;
+}
+
+static void
+_parse_sequence_header (uint8_t streamid, const uint8_t *buf,
+ VcdMpegStreamCtx *state)
+{
+ unsigned offset = 0;
+ unsigned hsize, vsize, aratio, frate, brate, bufsize, constr;
+ const uint8_t *data = buf;
+ const int vid_idx = _vid_streamid_idx (streamid);
+
+ const double aspect_ratios[16] =
+ {
+ 0.0000, 1.0000, 0.6735, 0.7031,
+ 0.7615, 0.8055, 0.8437, 0.8935,
+ 0.9375, 0.9815, 1.0255, 1.0695,
+ 1.1250, 1.1575, 1.2015, 0.0000
+ };
+
+ if (state->stream.shdr[vid_idx].seen) /* we have it already */
+ return;
+
+ hsize = vcd_bitvec_read_bits (data, &offset, 12);
+
+ vsize = vcd_bitvec_read_bits (data, &offset, 12);
+
+ aratio = vcd_bitvec_read_bits (data, &offset, 4);
+
+ frate = vcd_bitvec_read_bits (data, &offset, 4);
+
+ brate = vcd_bitvec_read_bits (data, &offset, 18);
+
+ MARKER (data, &offset);
+
+ bufsize = vcd_bitvec_read_bits (data, &offset, 10);
+
+ constr = vcd_bitvec_read_bits (data, &offset, 1);
+
+ /* skip intra quantizer matrix */
+
+ if (vcd_bitvec_read_bits (data, &offset, 1))
+ offset += 64 << 3;
+
+ /* skip non-intra quantizer matrix */
+
+ if (vcd_bitvec_read_bits (data, &offset, 1))
+ offset += 64 << 3;
+
+ state->stream.shdr[vid_idx].hsize = hsize;
+ state->stream.shdr[vid_idx].vsize = vsize;
+ state->stream.shdr[vid_idx].aratio = aspect_ratios[aratio];
+ state->stream.shdr[vid_idx].frate = frame_rates[frate];
+ state->stream.shdr[vid_idx].bitrate = 400 * brate;
+ state->stream.shdr[vid_idx].vbvsize = bufsize * 16 * 1024;
+ state->stream.shdr[vid_idx].constrained_flag = (constr != 0);
+
+ state->stream.shdr[vid_idx].seen = true;
+}
+
+static void
+_parse_gop_header (uint8_t streamid, const uint8_t *buf,
+ VcdMpegStreamCtx *state)
+{
+ const uint8_t *data = buf;
+ unsigned offset = 0;
+
+ bool drop_flag;
+ /* bool close_gop; */
+ /* bool broken_link; */
+
+ unsigned hour, minute, second, frame;
+
+ drop_flag = vcd_bitvec_read_bits(data, &offset, 1) != 0;
+
+ hour = vcd_bitvec_read_bits(data, &offset, 5);
+
+ minute = vcd_bitvec_read_bits(data, &offset, 6);
+
+ MARKER (data, &offset);
+
+ second = vcd_bitvec_read_bits(data, &offset, 6);
+
+ frame = vcd_bitvec_read_bits(data, &offset, 6);
+
+ /* close_gop = vcd_bitvec_read_bits(data, &offset, 1) != 0; */
+
+ /* broken_link = vcd_bitvec_read_bits(data, &offset, 1) != 0; */
+
+ state->packet.gop = true;
+ state->packet.gop_timecode.h = hour;
+ state->packet.gop_timecode.m = minute;
+ state->packet.gop_timecode.s = second;
+ state->packet.gop_timecode.f = frame;
+}
+
+static inline void
+_check_scan_data (const char str[], const msf_t *msf,
+ VcdMpegStreamCtx *state)
+{
+ char tmp[16];
+
+ if (state->stream.scan_data_warnings > VCD_MPEG_SCAN_DATA_WARNS)
+ return;
+
+ if (state->stream.scan_data_warnings == VCD_MPEG_SCAN_DATA_WARNS)
+ {
+ vcd_warn ("mpeg user scan data: from now on, scan information "
+ "data errors will not be reported anymore---consider"
+ " enabling the 'update scan offsets' option, "
+ "if it is not enabled already!");
+ state->stream.scan_data_warnings++;
+ return;
+ }
+
+ if (msf->m == 0xff
+ && msf->s == 0xff
+ && msf->f == 0xff)
+ return;
+
+ if ((msf->s & 0x80) == 0
+ || (msf->f & 0x80) == 0)
+ {
+ snprintf (tmp, sizeof (tmp), "%.2x:%.2x.%.2x", msf->m, msf->s, msf->f);
+
+ vcd_warn ("mpeg user scan data: msb of second or frame field "
+ "not set for '%s': [%s]", str, tmp);
+
+ state->stream.scan_data_warnings++;
+
+ return;
+ }
+
+ if ((msf->m >> 4) > 9
+ || ((0x80 ^ msf->s) >> 4) > 9
+ || ((0x80 ^ msf->f) >> 4) > 9
+ || (msf->m & 0xf) > 9
+ || (msf->s & 0xf) > 9
+ || (msf->f & 0xf) > 9)
+ {
+ snprintf (tmp, sizeof (tmp), "%.2x:%.2x.%.2x",
+ msf->m, 0x80 ^ msf->s, 0x80 ^ msf->f);
+
+ vcd_warn ("mpeg user scan data: one or more BCD fields out of range "
+ "for '%s': [%s]", str, tmp);
+
+ state->stream.scan_data_warnings++;
+ }
+}
+
+static void
+_parse_user_data (uint8_t streamid, const void *buf, unsigned len,
+ unsigned offset,
+ VcdMpegStreamCtx *state)
+{
+ unsigned pos = 0;
+ PRAGMA_BEGIN_PACKED
+ struct {
+ uint8_t tag;
+ uint8_t len;
+ uint8_t data[EMPTY_ARRAY_SIZE];
+ } GNUC_PACKED const *udg = buf;
+ PRAGMA_END_PACKED
+
+ if (udg->tag == 0x00) /* if first tag's already 0x00 */
+ {
+ vcd_debug ("strange (possibly non-compliant) user_data seen...");
+ }
+ else while (pos + 2 < len)
+ {
+ if (udg->tag == 0x00)
+ break;
+
+ if (pos + udg->len >= len)
+ break;
+
+ if (udg->len < 2)
+ break;
+
+ switch (udg->tag)
+ {
+ case 0x00:
+ vcd_assert_not_reached ();
+ break;
+
+ case 0x10: /* scan information */
+ {
+ struct vcd_mpeg_scan_data_t *usdi = (void *) udg;
+ vcd_assert (sizeof (struct vcd_mpeg_scan_data_t) == 14);
+
+ if (GNUC_UNLIKELY (usdi->len != 14))
+ {
+ vcd_warn ("invalid user scan data length (%d != 14)", usdi->len);
+ break;
+ }
+
+ vcd_assert (usdi->len == 14);
+ _check_scan_data ("previous_I_offset", &usdi->prev_ofs, state);
+ _check_scan_data ("next_I_offset ", &usdi->next_ofs, state);
+ _check_scan_data ("backward_I_offset", &usdi->back_ofs, state);
+ _check_scan_data ("forward_I_offset ", &usdi->forw_ofs, state);
+
+ state->packet.scan_data_ptr = usdi;
+ state->stream.scan_data++;
+ }
+ break;
+
+ case 0x11: /* closed caption data */
+ vcd_debug ("closed caption data seen -- not supported yet (len = %d)", udg->len);
+ break;
+
+ default:
+ vcd_warn ("unknown user data tag id 0x%.2x encountered", udg->tag);
+ return; /* since we cannot rely on udg->len anymore... */
+ break;
+ }
+
+
+ pos += udg->len;
+ vcd_assert (udg->len >= 2);
+ udg = (void *) &udg->data[udg->len - 2];
+ }
+
+ vcd_assert (pos <= len);
+}
+
+static int
+_analyze_pes_header (const uint8_t *buf, int len,
+ VcdMpegStreamCtx *state)
+{
+ bool _has_pts = false;
+ bool _has_dts = false;
+ int64_t pts = 0;
+ mpeg_vers_t pes_mpeg_ver = MPEG_VERS_INVALID;
+
+ int pos;
+
+ if (vcd_bitvec_peek_bits (buf, 0, 2) == 2) /* %10 - ISO13818-1 */
+ {
+ unsigned pos2 = 0;
+
+ pes_mpeg_ver = MPEG_VERS_MPEG2;
+
+ pos2 += 2;
+
+ pos2 += 2; /* PES_scrambling_control */
+ pos2++; /* PES_priority */
+ pos2++; /* data_alignment_indicator */
+ pos2++; /* copyright */
+ pos2++; /* original_or_copy */
+
+ switch (vcd_bitvec_read_bits (buf, &pos2, 2)) /* PTS_DTS_flags */
+ {
+ case 2: /* %10 */
+ _has_pts = true;
+ break;
+
+ case 3: /* %11 */
+ _has_dts = _has_pts = true;
+ break;
+
+ default:
+ /* NOOP */
+ break;
+ }
+
+ pos2++; /* ESCR_flag */
+
+ pos2++; /* */
+ pos2++; /* */
+ pos2++; /* */
+ pos2++; /* */
+
+ pos2++; /* PES_extension_flag */
+
+ pos = vcd_bitvec_read_bits (buf, &pos2, 8); /* PES_header_data_length */
+ pos += pos2 >> 3;
+
+ if (_has_pts && _has_dts)
+ {
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 4) == 3); /* %0011 */
+ pos2 += 4;
+
+ pts = _parse_timecode (buf, &pos2);
+
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 4) == 1); /* %0001 */
+ pos2 += 4;
+
+ /* dts = */ _parse_timecode (buf, &pos2);
+ }
+ else if (_has_pts)
+ {
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 4) == 2); /* %0010 */
+ pos2 += 4;
+
+ pts = _parse_timecode (buf, &pos2);
+ }
+ }
+ else /* ISO11172-1 */
+ {
+ unsigned pos2 = 0;
+
+ pes_mpeg_ver = MPEG_VERS_MPEG1;
+
+ /* get rid of stuffing bytes */
+ while (((pos2 + 8) < (len << 3))
+ && vcd_bitvec_peek_bits (buf, pos2, 8) == 0xff)
+ pos2 += 8;
+
+ if (vcd_bitvec_peek_bits (buf, pos2, 2) == 1) /* %01 */
+ {
+ pos2 += 2;
+
+ pos2++; /* STD_buffer_scale */
+ pos2 += 13; /* STD_buffer_size */
+ }
+
+ switch (vcd_bitvec_peek_bits (buf, pos2, 4))
+ {
+ case 0x2: /* %0010 */
+ pos2 += 4;
+ _has_pts = true;
+
+ pts = _parse_timecode (buf, &pos2);
+ break;
+
+ case 0x3: /* %0011 */
+ pos2 += 4;
+
+ _has_dts = _has_pts = true;
+ pts = _parse_timecode (buf, &pos2);
+
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 4) == 1); /* %0001 */
+ pos2 += 4;
+
+ /* dts = */ _parse_timecode (buf, &pos2);
+ break;
+
+ case 0x0: /* %0000 */
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 8) == 0x0f);
+ pos2 += 8;
+ break;
+
+ case 0xf: /* %1111 - actually a syntax error! */
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 8) == 0xff);
+ vcd_warn ("Unexpected stuffing byte noticed in ISO11172 PES header!");
+ pos2 += 8;
+ break;
+
+ default:
+ vcd_error ("Error in ISO11172 PES header");
+ break;
+ }
+
+ pos = pos2 >> 3;
+ }
+
+ if (_has_pts)
+ {
+ double pts2;
+
+ pts2 = (double) pts / 90000.0;
+
+ if (!state->stream.seen_pts)
+ {
+ state->stream.max_pts = state->stream.min_pts = pts2;
+ state->stream.seen_pts = true;
+ }
+ else
+ {
+ state->stream.max_pts = MAX (state->stream.max_pts, pts2);
+ state->stream.min_pts = MIN (state->stream.min_pts, pts2);
+ }
+
+ state->packet.has_pts = true;
+ state->packet.pts = pts2;
+ }
+
+ if (state->stream.version != pes_mpeg_ver)
+ vcd_warn ("pack header mpeg version does not match pes header mpeg version");
+
+ return pos;
+}
+
+static void
+_analyze_audio_pes (uint8_t streamid, const uint8_t *buf, int len, bool only_pts,
+ VcdMpegStreamCtx *state)
+{
+ const int aud_idx = _aud_streamid_idx (streamid);
+ unsigned bitpos;
+
+ vcd_assert (aud_idx != -1);
+
+ bitpos = _analyze_pes_header (buf, len, state);
+
+ /* if only pts extraction was needed, we are done here... */
+ if (only_pts)
+ return;
+
+ if (state->stream.ahdr[aud_idx].seen)
+ return;
+
+ bitpos <<= 3;
+
+ while (bitpos <= (len << 3))
+ {
+ unsigned syncword = vcd_bitvec_peek_bits (buf, bitpos, 12);
+
+ if (syncword != 0xfff)
+ {
+ bitpos += 8;
+ continue;
+ }
+
+ bitpos += 12;
+
+ if (GNUC_UNLIKELY (!vcd_bitvec_read_bits (buf, &bitpos, 1)))
+ {
+ vcd_debug ("non-MPEG1 audio stream header seen");
+ break;
+ }
+
+ switch (vcd_bitvec_read_bits (buf, &bitpos, 2)) /* layer */
+ {
+ case 3: /* %11 */
+ state->stream.ahdr[aud_idx].layer = 1;
+ break;
+ case 2: /* %10 */
+ state->stream.ahdr[aud_idx].layer = 2;
+ break;
+ case 1: /* %01 */
+ state->stream.ahdr[aud_idx].layer = 3;
+ break;
+ case 0: /* %00 */
+ state->stream.ahdr[aud_idx].layer = 0;
+ break;
+ }
+
+ bitpos++; /* protection_bit */
+
+ {
+ const int bits = vcd_bitvec_read_bits (buf, &bitpos, 4);
+
+ const unsigned bit_rates[4][16] = {
+ {0, },
+ {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
+ {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
+ {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
+ };
+
+ vcd_assert (IN(state->stream.ahdr[aud_idx].layer, 0, 3));
+ vcd_assert (IN(bits, 0, 15));
+
+ state->stream.ahdr[aud_idx].bitrate = 1024 * bit_rates[state->stream.ahdr[aud_idx].layer][bits];
+ }
+
+ switch (vcd_bitvec_read_bits (buf, &bitpos, 2)) /* sampling_frequency */
+ {
+ case 0: /* %00 */
+ state->stream.ahdr[aud_idx].sampfreq = 44100;
+ break;
+ case 1: /* %01 */
+ state->stream.ahdr[aud_idx].sampfreq = 48000;
+ break;
+ case 2: /* %10 */
+ state->stream.ahdr[aud_idx].sampfreq = 32000;
+ break;
+ case 3: /* %11 */
+ state->stream.ahdr[aud_idx].sampfreq = 0;
+ break;
+ }
+
+ bitpos++; /* padding_bit */
+
+ bitpos++; /* private_bit */
+
+ state->stream.ahdr[aud_idx].mode = 1 + vcd_bitvec_read_bits (buf, &bitpos, 2); /* mode */
+
+ state->stream.ahdr[aud_idx].seen = true;
+
+ /* we got the info, let's jump outta here */
+ break;
+ }
+}
+
+static void
+_analyze_video_pes (uint8_t streamid, const uint8_t *buf, int len, bool only_pts,
+ VcdMpegStreamCtx *state)
+{
+ const int vid_idx = _vid_streamid_idx (streamid);
+
+ int pos, pes_header;
+ int sequence_header_pos = -1;
+ int gop_header_pos = -1;
+ int ipicture_header_pos = -1;
+
+ vcd_assert (vid_idx != -1);
+
+ pes_header = pos = _analyze_pes_header (buf, len, state);
+
+ /* if only pts extraction was needed, we are done here... */
+ if (only_pts)
+ return;
+
+ while (pos + 4 <= len)
+ {
+ uint32_t code = vcd_bitvec_peek_bits32 (buf, pos << 3);
+
+ if (!_start_code_p (code))
+ {
+ pos++;
+ continue;
+ }
+
+ switch (code)
+ {
+ case MPEG_PICTURE_CODE:
+ pos += 4;
+
+ if (vcd_bitvec_peek_bits (buf, (pos << 3) + 10, 3) == 1)
+ ipicture_header_pos = pos;
+ break;
+
+ case MPEG_SEQUENCE_CODE:
+ pos += 4;
+ sequence_header_pos = pos;
+ _parse_sequence_header (streamid, buf + pos, state);
+ break;
+
+ case MPEG_GOP_CODE:
+ pos += 4;
+ if (pos + 4 > len)
+ break;
+ gop_header_pos = pos;
+ _parse_gop_header (streamid, buf + pos, state);
+ state->packet.gop = true;
+ break;
+
+ case MPEG_USER_CODE:
+ pos += 4;
+ if (pos + 4 > len)
+ break;
+ _parse_user_data (streamid, buf + pos, len - pos, pos, state);
+ break;
+
+ case MPEG_EXT_CODE:
+ default:
+ pos += 4;
+ break;
+ }
+ }
+
+ /* decide whether this packet qualifies as access point */
+ state->packet.aps = APS_NONE; /* paranoia */
+
+ if (state->packet.has_pts
+ && ipicture_header_pos != -1)
+ {
+ enum aps_t _aps_type = APS_NONE;
+
+ switch (state->stream.version)
+ {
+ case MPEG_VERS_MPEG1:
+ case MPEG_VERS_MPEG2:
+ if (sequence_header_pos != -1
+ && sequence_header_pos < gop_header_pos
+ && gop_header_pos < ipicture_header_pos)
+ _aps_type = (sequence_header_pos - 4 == pes_header) ? APS_ASGI : APS_SGI;
+ else if (gop_header_pos != 1
+ && gop_header_pos < ipicture_header_pos)
+ _aps_type = APS_GI;
+ else
+ _aps_type = APS_I;
+
+ break;
+
+ default:
+ break;
+ }
+
+ if (_aps_type)
+ {
+ const double pts2 = state->packet.pts;
+
+ if (state->stream.shdr[vid_idx].last_aps_pts > pts2)
+ vcd_warn ("APS' pts seems out of order (actual pts %f, last seen pts %f) "
+ "-- ignoring this aps",
+ pts2, state->stream.shdr[vid_idx].last_aps_pts);
+ else
+ {
+ state->packet.aps_idx = vid_idx;
+ state->packet.aps = _aps_type;
+ state->packet.aps_pts = pts2;
+ state->stream.shdr[vid_idx].last_aps_pts = pts2;
+ }
+ }
+ }
+}
+
+static void
+_register_streamid (uint8_t streamid, VcdMpegStreamCtx *state)
+{
+ const uint32_t code = MPEG_START_CODE_PATTERN | streamid;
+
+ switch (code)
+ {
+ case MPEG_VIDEO_E0_CODE:
+ case MPEG_VIDEO_E1_CODE:
+ case MPEG_VIDEO_E2_CODE:
+ state->packet.video[_vid_streamid_idx (streamid)] = true;
+ break;
+
+ case MPEG_AUDIO_C0_CODE:
+ case MPEG_AUDIO_C1_CODE:
+ case MPEG_AUDIO_C2_CODE:
+ state->packet.audio[_aud_streamid_idx (streamid)] = true;
+ break;
+
+ case MPEG_PAD_CODE:
+ state->packet.padding = true;
+ break;
+
+ case MPEG_SYSTEM_HEADER_CODE:
+ state->packet.system_header = true;
+ break;
+ }
+}
+
+static void
+_analyze_system_header (const uint8_t *buf, int len,
+ VcdMpegStreamCtx *state)
+{
+ unsigned bitpos = 0;
+
+ MARKER (buf, &bitpos);
+
+ bitpos += 22; /* rate_bound */
+
+ MARKER (buf, &bitpos);
+
+ bitpos += 6; /* audio_bound */
+
+ bitpos++; /* fixed_flag */
+ bitpos++; /* CSPS_flag */
+ bitpos++; /* system_audio_lock_flag */
+ bitpos++; /* system_video_lock_flag */
+
+ MARKER (buf, &bitpos);
+
+ bitpos += 5; /* video_bound */
+
+ bitpos += 1; /* packet_rate_restriction_flag -- only ISO 13818-1 */
+ bitpos += 7; /* reserved */
+
+ while (vcd_bitvec_peek_bits (buf, bitpos, 1) == 1
+ && bitpos <= (len << 3))
+ {
+ const uint8_t stream_id = vcd_bitvec_read_bits (buf, &bitpos, 8);
+
+ bitpos += 2; /* %11 */
+
+ bitpos++; /* P-STD_buffer_bound_scale */
+ bitpos += 13; /* P-STD_buffer_size_bound */
+
+ _register_streamid (stream_id, state);
+ }
+
+ vcd_assert (bitpos <= (len << 3));
+}
+
+static void
+_analyze_private_1_stream (const uint8_t *buf, int len,
+ VcdMpegStreamCtx *state)
+{
+ unsigned bitpos = _analyze_pes_header (buf, len, state);
+ int ogt_idx = -1;
+
+ uint8_t private_data_id;
+
+ bitpos <<= 3;
+
+ private_data_id = vcd_bitvec_read_bits (buf, &bitpos, 8);
+
+ switch (private_data_id)
+ {
+ uint8_t sub_stream_id;
+
+ case 0x00:
+ case 0x01:
+ case 0x02:
+ case 0x03:
+ /* CVD subs */
+ ogt_idx = private_data_id;
+
+ if (!state->stream.ogt[ogt_idx])
+ vcd_debug ("Assuming CVD-style subtitles for data_id 0x%.2x in private stream 1", ogt_idx);
+
+ break;
+
+ case 0x70:
+ /* SVCD OGT */
+ sub_stream_id = vcd_bitvec_read_bits (buf, &bitpos, 8);
+
+ if (sub_stream_id < 4)
+ {
+ ogt_idx = sub_stream_id;
+ if (!state->stream.ogt[ogt_idx])
+ vcd_debug ("subtitles detect for channel 0x%.2x", ogt_idx);
+ }
+ else
+ vcd_warn ("sub_stream_id out of range (0x%.2x)", sub_stream_id);
+ break;
+
+ default:
+ vcd_warn ("unknown private_data_id for private stream 1 seen (0x%.2x)",
+ private_data_id);
+ return;
+ break;
+ }
+
+ if (ogt_idx >= 0)
+ state->stream.ogt[ogt_idx] = state->packet.ogt[ogt_idx] = true;
+}
+
+int
+vcd_mpeg_parse_packet (const void *_buf, unsigned buflen, bool parse_pes,
+ VcdMpegStreamCtx *ctx)
+{
+ const uint8_t *buf = _buf;
+ int pos;
+
+ vcd_assert (buf != NULL);
+ vcd_assert (ctx != NULL);
+
+ /* clear packet info */
+ memset (&(ctx->packet), 0, sizeof (ctx->packet));
+
+ ctx->stream.packets++;
+
+ for (pos = 0; pos < buflen && !buf[pos]; pos++);
+
+ if (pos == buflen)
+ {
+ ctx->packet.zero = true;
+ return buflen;
+ }
+
+ /* verify the packet begins with a pack header */
+ if (vcd_bitvec_peek_bits32 (buf, 0) != MPEG_PACK_HEADER_CODE)
+ {
+ const uint32_t _code = vcd_bitvec_peek_bits32 (buf, 0);
+
+ vcd_warn ("mpeg scan: pack header code (0x%8.8x) expected, "
+ "but 0x%8.8x found (buflen = %d)",
+ (unsigned int) MPEG_PACK_HEADER_CODE,
+ (unsigned int) _code, buflen);
+
+ ctx->stream.packets--;
+
+ if (!ctx->stream.packets)
+ {
+ if (_code == MPEG_SEQUENCE_CODE)
+ vcd_warn ("...this looks like a elementary video stream"
+ " but a multiplexed program stream was required.");
+
+ if ((0xfff00000 & _code) == 0xfff00000)
+ vcd_warn ("...this looks like a elementary audio stream"
+ " but a multiplexed program stream was required.");
+
+ if (_code == 0x52494646)
+ vcd_warn ("...this looks like a RIFF header"
+ " but a plain multiplexed program stream was required.");
+ }
+ else if (_code == MPEG_PROGRAM_END_CODE)
+ vcd_warn ("...PEM (program end marker) found instead of pack header;"
+ " should be in last 4 bytes of pack");
+
+ return 0;
+ }
+
+ /* take a look at the pack header */
+ pos = 0;
+
+ while (pos + 4 <= buflen)
+ {
+ uint32_t code = vcd_bitvec_peek_bits32 (buf, pos << 3);
+
+ /* skip zero bytes... */
+ if (!code)
+ {
+ pos += (pos + 4 == buflen) ? 4 : 2;
+ continue;
+ }
+
+ /* continue until start code seen */
+ if (!_start_code_p (code))
+ {
+ pos++;
+ continue;
+ }
+
+ switch (code)
+ {
+ uint16_t size;
+ int bits;
+ unsigned bitpos;
+
+ case MPEG_PACK_HEADER_CODE:
+ if (pos)
+ return pos;
+
+ pos += 4;
+
+ bitpos = pos << 3;
+ bits = vcd_bitvec_peek_bits (buf, bitpos, 4);
+
+ if (bits == 0x2) /* %0010 ISO11172-1 */
+ {
+ uint64_t _scr;
+ uint32_t _muxrate;
+
+ bitpos += 4;
+
+ if (!ctx->stream.version)
+ ctx->stream.version = MPEG_VERS_MPEG1;
+
+ if (ctx->stream.version != MPEG_VERS_MPEG1)
+ vcd_warn ("mixed mpeg versions?");
+
+ _scr = _parse_timecode (buf, &bitpos);
+
+ MARKER (buf, &bitpos);
+
+ _muxrate = vcd_bitvec_read_bits (buf, &bitpos, 22);
+
+ MARKER (buf, &bitpos);
+
+ vcd_assert (bitpos % 8 == 0);
+ pos = bitpos >> 3;
+
+ ctx->packet.scr = _scr;
+ ctx->stream.muxrate = ctx->packet.muxrate = _muxrate * 50 * 8;
+ }
+ else if (bits >> 2 == 0x1) /* %01xx ISO13818-1 */
+ {
+ uint64_t _scr;
+ uint32_t _muxrate;
+ int tmp;
+
+ bitpos += 2;
+
+ if (!ctx->stream.version)
+ ctx->stream.version = MPEG_VERS_MPEG2;
+
+ if (ctx->stream.version != MPEG_VERS_MPEG2)
+ vcd_warn ("mixed mpeg versions?");
+
+ _scr = _parse_timecode (buf, &bitpos);
+
+ _scr *= 300;
+ _scr += vcd_bitvec_read_bits (buf, &bitpos, 9); /* SCR ext */
+
+ MARKER (buf, &bitpos);
+
+ _muxrate = vcd_bitvec_read_bits (buf, &bitpos, 22);
+
+ MARKER (buf, &bitpos);
+ MARKER (buf, &bitpos);
+
+ bitpos += 5; /* reserved */
+
+ tmp = vcd_bitvec_read_bits (buf, &bitpos, 3) << 3;
+
+ bitpos += tmp;
+
+ vcd_assert (bitpos % 8 == 0);
+ pos = bitpos >> 3;
+
+ ctx->packet.scr = _scr;
+ ctx->stream.muxrate = ctx->packet.muxrate = _muxrate * 50 * 8;
+ }
+ else
+ {
+ vcd_warn ("packet not recognized as either version 1 or 2 (%d)"
+ " -- assuming v1", bits);
+ }
+
+ break;
+
+ case MPEG_SYSTEM_HEADER_CODE:
+ case MPEG_PAD_CODE:
+ case MPEG_PRIVATE_1_CODE:
+ case MPEG_VIDEO_E0_CODE:
+ case MPEG_VIDEO_E1_CODE:
+ case MPEG_VIDEO_E2_CODE:
+ case MPEG_AUDIO_C0_CODE:
+ case MPEG_AUDIO_C1_CODE:
+ case MPEG_AUDIO_C2_CODE:
+ pos += 4;
+ size = vcd_bitvec_peek_bits16 (buf, pos << 3);
+ pos += 2;
+
+ if (pos + size > buflen)
+ {
+ vcd_warn ("packet length beyond buffer"
+ " (pos = %d + size = %d > buflen = %d) "
+ "-- stream may be truncated or packet length > 2324 bytes!",
+ pos, size, buflen);
+ ctx->stream.packets--;
+ return 0;
+ }
+
+ _register_streamid (code & 0xff, ctx);
+
+ switch (code)
+ {
+ case MPEG_SYSTEM_HEADER_CODE:
+ _analyze_system_header (buf + pos, size, ctx);
+ break;
+
+ case MPEG_VIDEO_E0_CODE:
+ case MPEG_VIDEO_E1_CODE:
+ case MPEG_VIDEO_E2_CODE:
+ _analyze_video_pes (code & 0xff, buf + pos, size, !parse_pes, ctx);
+ break;
+
+ case MPEG_AUDIO_C0_CODE:
+ case MPEG_AUDIO_C1_CODE:
+ case MPEG_AUDIO_C2_CODE:
+ _analyze_audio_pes (code & 0xff, buf + pos, size, !parse_pes, ctx);
+ break;
+
+ case MPEG_PRIVATE_1_CODE:
+ _analyze_private_1_stream (buf + pos, size, ctx);
+ break;
+ }
+
+ pos += size;
+ break;
+
+ case MPEG_PROGRAM_END_CODE:
+ ctx->packet.pem = true;
+ pos += 4;
+ break;
+
+ case MPEG_PICTURE_CODE:
+ pos += 3;
+ break;
+
+ default:
+ vcd_debug ("unexpected start code 0x%8.8x", (unsigned int) code);
+ pos += 4;
+ break;
+ }
+ }
+
+ if (pos != buflen)
+ vcd_debug ("pos != buflen (%d != %d)", pos, buflen); /* fixme? */
+
+ return buflen;
+}
+
+mpeg_norm_t
+vcd_mpeg_get_norm (const struct vcd_mpeg_stream_vid_info *_info)
+{
+ int i;
+
+ for (i = 0; norm_table[i].norm != MPEG_NORM_OTHER;i++)
+ if (norm_table[i].hsize == _info->hsize
+ && norm_table[i].vsize == _info->vsize
+ && frame_rates[norm_table[i].frate_idx] == _info->frate)
+ break;
+
+ return norm_table[i].norm;
+}
+
+enum vcd_mpeg_packet_type
+vcd_mpeg_packet_get_type (const struct vcd_mpeg_packet_info *_info)
+{
+ if (_info->video[0]
+ || _info->video[1]
+ || _info->video[2])
+ return PKT_TYPE_VIDEO;
+ else if (_info->audio[0]
+ || _info->audio[1]
+ || _info->audio[2])
+ return PKT_TYPE_AUDIO;
+ else if (_info->zero)
+ return PKT_TYPE_ZERO;
+ else if (_info->ogt[0]
+ || _info->ogt[1]
+ || _info->ogt[2]
+ || _info->ogt[3])
+ return PKT_TYPE_OGT;
+ else if (_info->system_header || _info->padding)
+ return PKT_TYPE_EMPTY;
+
+ return PKT_TYPE_INVALID;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/mpeg.h b/contrib/libvcd/mpeg.h
new file mode 100644
index 000000000..2d28d2aef
--- /dev/null
+++ b/contrib/libvcd/mpeg.h
@@ -0,0 +1,194 @@
+/*
+ $Id: mpeg.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_MPEG_H__
+#define __VCD_MPEG_H__
+
+#include <string.h>
+
+/* Public headers */
+#include <libvcd/logging.h>
+#include <libvcd/types.h>
+
+/* Private headers */
+#include "data_structures.h"
+
+typedef enum {
+ MPEG_VERS_INVALID = 0,
+ MPEG_VERS_MPEG1 = 1,
+ MPEG_VERS_MPEG2 = 2
+} mpeg_vers_t;
+
+PRAGMA_BEGIN_PACKED
+struct vcd_mpeg_scan_data_t {
+ uint8_t tag;
+ uint8_t len;
+ msf_t prev_ofs;
+ msf_t next_ofs;
+ msf_t back_ofs;
+ msf_t forw_ofs;
+} GNUC_PACKED;
+PRAGMA_END_PACKED
+
+#define struct_vcd_mpeg_scan_data_t_SIZEOF 14
+
+#define VCD_MPEG_SCAN_DATA_WARNS 8
+
+typedef struct {
+ struct vcd_mpeg_packet_info {
+ bool video[3];
+ bool audio[3];
+ bool ogt[4];
+
+ bool padding;
+ bool pem;
+ bool zero;
+ bool system_header;
+
+ struct vcd_mpeg_scan_data_t *scan_data_ptr; /* points into actual packet memory! */
+
+ enum aps_t {
+ APS_NONE = 0,
+ APS_I, /* iframe */
+ APS_GI, /* gop + iframe */
+ APS_SGI, /* sequence + gop + iframe */
+ APS_ASGI /* aligned sequence + gop + iframe */
+ } aps;
+ double aps_pts;
+ int aps_idx;
+
+ bool has_pts;
+ double pts;
+
+ uint64_t scr;
+ unsigned muxrate;
+
+ bool gop;
+ struct {
+ uint8_t h, m, s, f;
+ } gop_timecode;
+ } packet;
+
+ struct vcd_mpeg_stream_info {
+ unsigned packets;
+
+ mpeg_vers_t version;
+
+ bool ogt[4];
+
+ struct vcd_mpeg_stream_vid_info {
+ bool seen;
+ unsigned hsize;
+ unsigned vsize;
+ double aratio;
+ double frate;
+ unsigned bitrate;
+ unsigned vbvsize;
+ bool constrained_flag;
+
+ CdioList *aps_list; /* filled up by vcd_mpeg_source */
+ double last_aps_pts; /* temp, see ->packet */
+
+ } shdr[3];
+
+ struct vcd_mpeg_stream_aud_info {
+ bool seen;
+ unsigned layer;
+ unsigned bitrate;
+ unsigned sampfreq;
+ enum {
+ MPEG_STEREO = 1,
+ MPEG_JOINT_STEREO,
+ MPEG_DUAL_CHANNEL,
+ MPEG_SINGLE_CHANNEL
+ } mode;
+ } ahdr[3];
+
+ unsigned muxrate;
+
+ bool seen_pts;
+ double min_pts;
+ double max_pts;
+
+ double playing_time;
+
+ unsigned scan_data;
+ unsigned scan_data_warnings;
+ } stream;
+} VcdMpegStreamCtx;
+
+int
+vcd_mpeg_parse_packet (const void *buf, unsigned buflen, bool parse_pes,
+ VcdMpegStreamCtx *ctx);
+
+typedef enum {
+ MPEG_NORM_OTHER,
+ MPEG_NORM_PAL,
+ MPEG_NORM_NTSC,
+ MPEG_NORM_FILM,
+ MPEG_NORM_PAL_S,
+ MPEG_NORM_NTSC_S
+} mpeg_norm_t;
+
+mpeg_norm_t
+vcd_mpeg_get_norm (const struct vcd_mpeg_stream_vid_info *_info);
+
+enum vcd_mpeg_packet_type {
+ PKT_TYPE_INVALID = 0,
+ PKT_TYPE_VIDEO,
+ PKT_TYPE_AUDIO,
+ PKT_TYPE_OGT,
+ PKT_TYPE_ZERO,
+ PKT_TYPE_EMPTY
+};
+
+enum vcd_mpeg_packet_type
+vcd_mpeg_packet_get_type (const struct vcd_mpeg_packet_info *_info);
+
+struct vcd_mpeg_stream_vid_type {
+ enum {
+ VID_TYPE_NONE = 0,
+ VID_TYPE_MOTION,
+ VID_TYPE_STILL
+ } type;
+ enum {
+ VID_NORM_OTHER = 0,
+ VID_NORM_PAL,
+ VID_NORM_NTSC
+ } norm;
+ enum {
+ VID_RES_OTHER = 0,
+ VID_RES_SIF,
+ VID_RES_HALF_D1,
+ VID_RES_2_3_D1,
+ VID_RES_FULL_D2
+ } resolution;
+};
+
+#endif /* __VCD_MPEG_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/mpeg_stream.c b/contrib/libvcd/mpeg_stream.c
new file mode 100644
index 000000000..e5466766a
--- /dev/null
+++ b/contrib/libvcd/mpeg_stream.c
@@ -0,0 +1,487 @@
+/*
+ $Id: mpeg_stream.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <cdio/cdio.h>
+#include <cdio/bytesex.h>
+
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "mpeg_stream.h"
+#include "data_structures.h"
+#include "mpeg.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: mpeg_stream.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $";
+
+struct _VcdMpegSource
+{
+ VcdDataSource *data_source;
+
+ bool scanned;
+
+ /* _get_packet cache */
+ unsigned _read_pkt_pos;
+ unsigned _read_pkt_no;
+
+ struct vcd_mpeg_stream_info info;
+};
+
+/*
+ * access functions
+ */
+
+VcdMpegSource *
+vcd_mpeg_source_new (VcdDataSource *mpeg_file)
+{
+ VcdMpegSource *new_obj;
+
+ vcd_assert (mpeg_file != NULL);
+
+ new_obj = _vcd_malloc (sizeof (VcdMpegSource));
+
+ new_obj->data_source = mpeg_file;
+ new_obj->scanned = false;
+
+ return new_obj;
+}
+
+void
+vcd_mpeg_source_destroy (VcdMpegSource *obj, bool destroy_file_obj)
+{
+ int i;
+ vcd_assert (obj != NULL);
+
+ if (destroy_file_obj)
+ vcd_data_source_destroy (obj->data_source);
+
+ for (i = 0; i < 3; i++)
+ if (obj->info.shdr[i].aps_list)
+ _cdio_list_free (obj->info.shdr[i].aps_list, true);
+
+ free (obj);
+}
+
+const struct vcd_mpeg_stream_info *
+vcd_mpeg_source_get_info (VcdMpegSource *obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_assert (obj->scanned);
+
+ return &(obj->info);
+}
+
+long
+vcd_mpeg_source_stat (VcdMpegSource *obj)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (!obj->scanned);
+
+ return obj->info.packets * 2324;
+}
+
+void
+vcd_mpeg_source_scan (VcdMpegSource *obj, bool strict_aps, bool fix_scan_info,
+ vcd_mpeg_prog_cb_t callback, void *user_data)
+{
+ unsigned length = 0;
+ unsigned pos = 0;
+ unsigned pno = 0;
+ unsigned padbytes = 0;
+ unsigned padpackets = 0;
+ VcdMpegStreamCtx state;
+ CdioListNode *n;
+ vcd_mpeg_prog_info_t _progress = { 0, };
+
+ vcd_assert (obj != NULL);
+
+ if (obj->scanned)
+ {
+ vcd_debug ("already scanned... not rescanning");
+ return;
+ }
+
+ vcd_assert (!obj->scanned);
+
+ memset (&state, 0, sizeof (state));
+
+ if (fix_scan_info)
+ state.stream.scan_data_warnings = VCD_MPEG_SCAN_DATA_WARNS + 1;
+
+ vcd_data_source_seek (obj->data_source, 0);
+ length = vcd_data_source_stat (obj->data_source);
+
+ if (callback)
+ {
+ _progress.length = length;
+ callback (&_progress, user_data);
+ }
+
+
+ while (pos < length)
+ {
+ char buf[2324] = { 0, };
+ int read_len = MIN (sizeof (buf), (length - pos));
+ int pkt_len;
+
+ vcd_data_source_read (obj->data_source, buf, read_len, 1);
+
+ pkt_len = vcd_mpeg_parse_packet (buf, read_len, true, &state);
+
+ if (!pkt_len)
+ {
+ if (!pno)
+ vcd_error ("input mpeg stream has been deemed invalid -- aborting");
+
+ vcd_warn ("bad packet at packet #%d (stream byte offset %d)"
+ " -- remaining %d bytes of stream will be ignored",
+ pno, pos, length - pos);
+
+ pos = length; /* don't fall into assert... */
+ break;
+ }
+
+ if (callback && (pos - _progress.current_pos) > (length / 100))
+ {
+ _progress.current_pos = pos;
+ _progress.current_pack = pno;
+ callback (&_progress, user_data);
+ }
+
+ switch (state.packet.aps)
+ {
+ case APS_NONE:
+ break;
+
+ case APS_I:
+ case APS_GI:
+ if (strict_aps)
+ break; /* allow only if now strict aps */
+
+ case APS_SGI:
+ case APS_ASGI:
+ {
+ struct aps_data *_data = _vcd_malloc (sizeof (struct aps_data));
+
+ _data->packet_no = pno;
+ _data->timestamp = state.packet.aps_pts;
+
+ if (!state.stream.shdr[state.packet.aps_idx].aps_list)
+ state.stream.shdr[state.packet.aps_idx].aps_list = _cdio_list_new ();
+
+ _cdio_list_append (state.stream.shdr[state.packet.aps_idx].aps_list, _data);
+ }
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ pos += pkt_len;
+ pno++;
+
+ if (pkt_len != read_len)
+ {
+ padbytes += (2324 - pkt_len);
+
+ if (!padpackets)
+ vcd_warn ("mpeg stream will be padded on the fly -- hope that's ok for you!");
+
+ padpackets++;
+
+ vcd_data_source_seek (obj->data_source, pos);
+ }
+ }
+
+ vcd_data_source_close (obj->data_source);
+
+ if (callback)
+ {
+ _progress.current_pos = pos;
+ _progress.current_pack = pno;
+ callback (&_progress, user_data);
+ }
+
+ vcd_assert (pos == length);
+
+ obj->info = state.stream;
+ obj->scanned = true;
+
+ obj->info.playing_time = obj->info.max_pts - obj->info.min_pts;
+
+ if (obj->info.min_pts)
+ vcd_debug ("pts start offset %f (max pts = %f)",
+ obj->info.min_pts, obj->info.max_pts);
+
+ vcd_debug ("playing time %f", obj->info.playing_time);
+
+ if (!state.stream.scan_data && state.stream.version == MPEG_VERS_MPEG2)
+ vcd_warn ("mpeg stream contained no scan information (user) data");
+
+ {
+ int i;
+
+ for (i = 0; i < 3; i++)
+ if (obj->info.shdr[i].aps_list)
+ _CDIO_LIST_FOREACH (n, obj->info.shdr[i].aps_list)
+ {
+ struct aps_data *_data = _cdio_list_node_data (n);
+
+ _data->timestamp -= obj->info.min_pts;
+ }
+ }
+
+ if (padpackets)
+ vcd_warn ("autopadding requires to insert additional %d zero bytes"
+ " into MPEG stream (due to %d unaligned packets of %d total)",
+ padbytes, padpackets, state.stream.packets);
+
+ obj->info.version = state.stream.version;
+}
+
+static double
+_approx_pts (CdioList *aps_list, uint32_t packet_no)
+{
+ double retval = 0;
+ CdioListNode *node;
+
+ struct aps_data *_laps = NULL;
+
+ double last_pts_ratio = 0;
+
+ _CDIO_LIST_FOREACH (node, aps_list)
+ {
+ struct aps_data *_aps = _cdio_list_node_data (node);
+
+ if (_laps)
+ {
+ long p = _aps->packet_no;
+ double t = _aps->timestamp;
+
+ p -= _laps->packet_no;
+ t -= _laps->timestamp;
+
+ last_pts_ratio = t / p;
+ }
+
+ if (_aps->packet_no >= packet_no)
+ break;
+
+ _laps = _aps;
+ }
+
+ retval = packet_no;
+ retval -= _laps->packet_no;
+ retval *= last_pts_ratio;
+ retval += _laps->timestamp;
+
+ return retval;
+}
+
+static void
+_set_scan_msf (msf_t *_msf, long lsn)
+{
+ if (lsn == -1)
+ {
+ _msf->m = _msf->s = _msf->f = 0xff;
+ return;
+ }
+
+ cdio_lba_to_msf (lsn, _msf);
+ _msf->s |= 0x80;
+ _msf->f |= 0x80;
+}
+
+static void
+_fix_scan_info (struct vcd_mpeg_scan_data_t *scan_data_ptr,
+ unsigned packet_no, double pts, CdioList *aps_list)
+{
+ CdioListNode *node;
+ long _next = -1, _prev = -1, _forw = -1, _back = -1;
+
+ _CDIO_LIST_FOREACH (node, aps_list)
+ {
+ struct aps_data *_aps = _cdio_list_node_data (node);
+
+ if (_aps->packet_no == packet_no)
+ continue;
+ else if (_aps->packet_no < packet_no)
+ {
+ _prev = _aps->packet_no;
+
+ if (pts - _aps->timestamp < 10 && _back == -1)
+ _back = _aps->packet_no;
+ }
+ else if (_aps->packet_no > packet_no)
+ {
+ if (_next == -1)
+ _next = _aps->packet_no;
+
+ if (_aps->timestamp - pts < 10)
+ _forw = _aps->packet_no;
+ }
+ }
+
+ if (_back == -1)
+ _back = packet_no;
+
+ if (_forw == -1)
+ _forw = packet_no;
+
+ _set_scan_msf (&scan_data_ptr->prev_ofs, _prev);
+ _set_scan_msf (&scan_data_ptr->next_ofs, _next);
+ _set_scan_msf (&scan_data_ptr->back_ofs, _back);
+ _set_scan_msf (&scan_data_ptr->forw_ofs, _forw);
+}
+
+int
+vcd_mpeg_source_get_packet (VcdMpegSource *obj, unsigned long packet_no,
+ void *packet_buf, struct vcd_mpeg_packet_info *flags,
+ bool fix_scan_info)
+{
+ unsigned length;
+ unsigned pos;
+ unsigned pno;
+ VcdMpegStreamCtx state;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->scanned);
+ vcd_assert (packet_buf != NULL);
+
+ if (packet_no >= obj->info.packets)
+ {
+ vcd_error ("invalid argument");
+ return -1;
+ }
+
+ if (packet_no < obj->_read_pkt_no)
+ {
+ vcd_warn ("rewinding mpeg stream...");
+ obj->_read_pkt_no = 0;
+ obj->_read_pkt_pos = 0;
+ }
+
+ memset (&state, 0, sizeof (state));
+ state.stream.seen_pts = true;
+ state.stream.min_pts = obj->info.min_pts;
+ state.stream.scan_data_warnings = VCD_MPEG_SCAN_DATA_WARNS + 1;
+
+ pos = obj->_read_pkt_pos;
+ pno = obj->_read_pkt_no;
+ length = vcd_data_source_stat (obj->data_source);
+
+ vcd_data_source_seek (obj->data_source, pos);
+
+ while (pos < length)
+ {
+ char buf[2324] = { 0, };
+ int read_len = MIN (sizeof (buf), (length - pos));
+ int pkt_len;
+
+ vcd_data_source_read (obj->data_source, buf, read_len, 1);
+
+ pkt_len = vcd_mpeg_parse_packet (buf, read_len,
+ fix_scan_info, &state);
+
+ vcd_assert (pkt_len > 0);
+
+ if (pno == packet_no)
+ {
+ /* optimized for sequential access,
+ thus save pointer to next mpeg pack */
+ obj->_read_pkt_pos = pos + pkt_len;
+ obj->_read_pkt_no = pno + 1;
+
+ if (fix_scan_info
+ && state.packet.scan_data_ptr
+ && obj->info.version == MPEG_VERS_MPEG2)
+ {
+ int vid_idx = 0;
+ double _pts;
+
+ if (state.packet.video[2])
+ vid_idx = 2;
+ else if (state.packet.video[1])
+ vid_idx = 1;
+ else
+ vid_idx = 0;
+
+ if (state.packet.has_pts)
+ _pts = state.packet.pts - obj->info.min_pts;
+ else
+ _pts = _approx_pts (obj->info.shdr[vid_idx].aps_list, packet_no);
+
+ _fix_scan_info (state.packet.scan_data_ptr, packet_no,
+ _pts, obj->info.shdr[vid_idx].aps_list);
+ }
+
+ memset (packet_buf, 0, 2324);
+ memcpy (packet_buf, buf, pkt_len);
+
+ if (flags)
+ {
+ *flags = state.packet;
+ flags->pts -= obj->info.min_pts;
+ }
+
+ return 0; /* breaking out */
+ }
+
+ pos += pkt_len;
+ pno++;
+
+ if (pkt_len != read_len)
+ vcd_data_source_seek (obj->data_source, pos);
+ }
+
+ vcd_assert (pos == length);
+
+ vcd_error ("shouldnt be reached...");
+
+ return -1;
+}
+
+void
+vcd_mpeg_source_close (VcdMpegSource *obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_data_source_close (obj->data_source);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/mpeg_stream.h b/contrib/libvcd/mpeg_stream.h
new file mode 100644
index 000000000..14f73d928
--- /dev/null
+++ b/contrib/libvcd/mpeg_stream.h
@@ -0,0 +1,116 @@
+/*
+ $Id: mpeg_stream.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_MPEG_STREAM__
+#define __VCD_MPEG_STREAM__
+
+#include <libvcd/types.h>
+
+/* Private includes */
+#include "stream.h"
+#include "data_structures.h"
+#include "mpeg.h"
+
+#define MPEG_PACKET_SIZE 2324
+
+typedef struct _VcdMpegSource VcdMpegSource;
+
+/* used in APS list */
+
+struct aps_data
+{
+ uint32_t packet_no;
+ double timestamp;
+};
+
+/* enums */
+
+typedef enum {
+ MPEG_AUDIO_NOSTREAM = 0,
+ MPEG_AUDIO_1STREAM = 1,
+ MPEG_AUDIO_2STREAM = 2,
+ MPEG_AUDIO_EXT_STREAM = 3
+} mpeg_audio_t;
+
+typedef enum {
+ MPEG_VIDEO_NOSTREAM = 0,
+ MPEG_VIDEO_NTSC_STILL = 1,
+ MPEG_VIDEO_NTSC_STILL2 = 2,
+ MPEG_VIDEO_NTSC_MOTION = 3,
+
+ MPEG_VIDEO_PAL_STILL = 5,
+ MPEG_VIDEO_PAL_STILL2 = 6,
+ MPEG_VIDEO_PAL_MOTION = 7
+} mpeg_video_t;
+
+/* mpeg stream info */
+
+struct vcd_mpeg_stream_info;
+
+/* mpeg packet info */
+
+struct vcd_mpeg_packet_info;
+
+/* access functions */
+
+VcdMpegSource *
+vcd_mpeg_source_new (VcdDataSource *mpeg_file);
+
+/* scan the mpeg file... needed to be called only once */
+typedef struct {
+ long current_pack;
+ long current_pos;
+ long length;
+} vcd_mpeg_prog_info_t;
+
+typedef int (*vcd_mpeg_prog_cb_t) (const vcd_mpeg_prog_info_t *progress_info,
+ void *user_data);
+
+void
+vcd_mpeg_source_scan (VcdMpegSource *obj, bool strict_aps, bool fix_scan_info,
+ vcd_mpeg_prog_cb_t callback, void *user_data);
+
+/* gets the packet at given position */
+int
+vcd_mpeg_source_get_packet (VcdMpegSource *obj, unsigned long packet_no,
+ void *packet_buf, struct vcd_mpeg_packet_info *flags,
+ bool fix_scan_info);
+
+void
+vcd_mpeg_source_close (VcdMpegSource *obj);
+
+const struct vcd_mpeg_stream_info *
+vcd_mpeg_source_get_info (VcdMpegSource *obj);
+
+long
+vcd_mpeg_source_stat (VcdMpegSource *obj);
+
+void
+vcd_mpeg_source_destroy (VcdMpegSource *obj, bool destroy_file_obj);
+
+#endif /* __VCD_MPEG_STREAM__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/obj.h b/contrib/libvcd/obj.h
new file mode 100644
index 000000000..d6849a3ab
--- /dev/null
+++ b/contrib/libvcd/obj.h
@@ -0,0 +1,202 @@
+/*
+ $Id: obj.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_OBJ_H__
+#define __VCD_OBJ_H__
+
+#include <cdio/iso9660.h>
+#include <libvcd/files.h>
+
+/* Private headers */
+#include "data_structures.h"
+#include "directory.h"
+#include "image_sink.h"
+#include "mpeg_stream.h"
+#include "salloc.h"
+#include "vcd.h"
+
+typedef struct {
+ double time;
+ struct aps_data aps;
+ char *id;
+} entry_t;
+
+typedef struct {
+ double time;
+ char *id;
+} pause_t;
+
+typedef struct {
+ VcdMpegSource *source;
+ char *id;
+ const struct vcd_mpeg_stream_info *info;
+
+ CdioList *pause_list; /* pause_t */
+
+ char *default_entry_id;
+ CdioList *entry_list; /* entry_t */
+
+ /* pbc ref check */
+ bool referenced;
+
+ /* computed on sector allocation */
+ unsigned relative_start_extent; /* relative to iso data end */
+} mpeg_sequence_t;
+
+/* work in progress -- fixme rename all occurences */
+#define mpeg_track_t mpeg_sequence_t
+#define mpeg_track_list mpeg_sequence_list
+
+typedef struct {
+ VcdMpegSource *source;
+ char *id;
+ const struct vcd_mpeg_stream_info *info;
+
+ CdioList *pause_list; /* pause_t */
+
+ /* pbc ref check */
+ bool referenced;
+
+ /* computed through info */
+ unsigned segment_count;
+
+ /* computed on sector allocation */
+ unsigned start_extent;
+} mpeg_segment_t;
+
+
+typedef struct {
+ char *iso_pathname;
+ VcdDataSource *file;
+ bool raw_flag;
+
+ uint32_t size;
+ uint32_t start_extent;
+ uint32_t sectors;
+} custom_file_t;
+
+struct _VcdObj {
+ vcd_type_t type;
+
+ /* VCD 3.0 chinese SVCD compat flags */
+ bool svcd_vcd3_mpegav;
+ bool svcd_vcd3_entrysvd;
+ bool svcd_vcd3_tracksvd;
+ bool svcd_vcd3_spiconsv;
+
+ bool update_scan_offsets;
+ bool relaxed_aps;
+
+ unsigned leadout_pregap;
+ unsigned track_pregap;
+ unsigned track_front_margin;
+ unsigned track_rear_margin;
+
+ /* output */
+ VcdImageSink *image_sink;
+
+ /* ... */
+ unsigned iso_size;
+ char *iso_volume_label;
+ char *iso_publisher_id;
+ char *iso_application_id;
+ char *iso_preparer_id;
+
+ char *info_album_id;
+ unsigned info_volume_count;
+ unsigned info_volume_number;
+ unsigned info_restriction;
+ bool info_use_seq2;
+ bool info_use_lid2;
+
+ /* input */
+ unsigned mpeg_segment_start_extent;
+ CdioList *mpeg_segment_list; /* mpeg_segment_t */
+
+ CdioList *mpeg_sequence_list; /* mpeg_sequence_t */
+
+ unsigned relative_end_extent; /* last mpeg sequence track end extent */
+
+ /* PBC */
+ CdioList *pbc_list; /* pbc_t */
+ unsigned psd_size;
+ unsigned psdx_size;
+
+ /* custom files */
+ unsigned ext_file_start_extent;
+ unsigned custom_file_start_extent;
+ CdioList *custom_file_list; /* custom_file_t */
+ CdioList *custom_dir_list; /* char */
+
+ /* dictionary */
+ CdioList *buffer_dict_list;
+
+ /* aggregates */
+ VcdSalloc *iso_bitmap;
+
+ VcdDirectory *dir;
+
+ /* state info */
+ bool in_output;
+
+ unsigned sectors_written;
+ unsigned in_track;
+
+ long last_cb_call;
+
+ progress_callback_t progress_callback;
+ void *callback_user_data;
+};
+
+/* private functions */
+
+mpeg_sequence_t *
+_vcd_obj_get_sequence_by_id (VcdObj *obj, const char sequence_id[]);
+
+mpeg_sequence_t *
+_vcd_obj_get_sequence_by_entry_id (VcdObj *obj, const char entry_id[]);
+
+mpeg_segment_t *
+_vcd_obj_get_segment_by_id (VcdObj *obj, const char segment_id[]);
+
+enum vcd_capability_t {
+ _CAP_VALID,
+ _CAP_MPEG1,
+ _CAP_MPEG2,
+ _CAP_PBC,
+ _CAP_PBC_X,
+ _CAP_TRACK_MARGINS,
+ _CAP_4C_SVCD,
+ _CAP_PAL_BITS
+};
+
+bool
+_vcd_obj_has_cap_p (const VcdObj *obj, enum vcd_capability_t capability);
+
+#endif /* __VCD_OBJ_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/pbc.c b/contrib/libvcd/pbc.c
new file mode 100644
index 000000000..1b4f1a6c1
--- /dev/null
+++ b/contrib/libvcd/pbc.c
@@ -0,0 +1,880 @@
+/*
+ $Id: pbc.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <stddef.h>
+#include <math.h>
+
+#include <cdio/cdio.h>
+#include <cdio/bytesex.h>
+
+/* Public headers */
+#include <libvcd/logging.h>
+#include <libvcd/files.h>
+#include <libvcd/types.h>
+#include <libvcd/info.h>
+
+/* FIXME! Make this really private. */
+#include <libvcd/files_private.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "obj.h"
+#include "pbc.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: pbc.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $";
+
+static uint8_t
+_wtime (int seconds)
+{
+ if (seconds < 0)
+ return 255;
+
+ if (seconds <= 60)
+ return seconds;
+
+ if (seconds <= 2000)
+ {
+ double _tmp;
+
+ _tmp = seconds;
+ _tmp -= 60;
+ _tmp /= 10;
+ _tmp += 60;
+
+ return rint (_tmp);
+ }
+
+ vcd_warn ("wait time of %ds clipped to 2000s", seconds);
+
+ return 254;
+}
+
+static pbc_t *
+_vcd_pbc_byid(const VcdObj *obj, const char item_id[])
+{
+ CdioListNode *node;
+
+ _CDIO_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _cdio_list_node_data (node);
+
+ if (_pbc->id && !strcmp (item_id, _pbc->id))
+ return _pbc;
+ }
+
+ /* not found */
+ return NULL;
+}
+
+unsigned
+_vcd_pbc_lid_lookup (const VcdObj *obj, const char item_id[])
+{
+ CdioListNode *node;
+ unsigned n = 1;
+
+ _CDIO_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _cdio_list_node_data (node);
+
+ vcd_assert (n < 0x8000);
+
+ if (_pbc->id && !strcmp (item_id, _pbc->id))
+ return n;
+
+ n++;
+ }
+
+ /* not found */
+ return 0;
+}
+
+static void
+_set_area_helper (pbc_area_t *dest, const pbc_area_t *src, const char sel_id[])
+{
+ memset (dest, 0, sizeof (pbc_area_t));
+
+ if (src)
+ {
+ if (src->x1 || src->x2 || src->y1 || src->y2) /* not disabled */
+ {
+ if (src->x1 >= src->x2)
+ vcd_error ("selection '%s': area x1 >= x2 (%d >= %d)",
+ sel_id, src->x1, src->x2);
+
+ if (src->y1 >= src->y2)
+ vcd_error ("selection '%s': area y1 >= y2 (%d >= %d)",
+ sel_id, src->y1, src->y2);
+ }
+
+ *dest = *src;
+ }
+}
+
+enum item_type_t
+_vcd_pbc_lookup (const VcdObj *obj, const char item_id[])
+{
+ unsigned id;
+
+ vcd_assert (item_id != NULL);
+
+ if ((id = _vcd_pbc_pin_lookup (obj, item_id)))
+ {
+ if (id < 2)
+ return ITEM_TYPE_NOTFOUND;
+ else if (id < MIN_ENCODED_TRACK_NUM)
+ return ITEM_TYPE_TRACK;
+ else if (id < 600)
+ return ITEM_TYPE_ENTRY;
+ else if (id <= MAX_ENCODED_SEGMENT_NUM)
+ return ITEM_TYPE_SEGMENT;
+ else
+ vcd_assert_not_reached ();
+ }
+ else if (_vcd_pbc_lid_lookup (obj, item_id))
+ return ITEM_TYPE_PBC;
+
+ return ITEM_TYPE_NOTFOUND;
+}
+
+uint16_t
+_vcd_pbc_pin_lookup (const VcdObj *obj, const char item_id[])
+{
+ int n;
+ CdioListNode *node;
+
+ if (!item_id)
+ return 0;
+
+ /* check sequence items */
+
+ n = 0;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _cdio_list_node_data (node);
+
+ vcd_assert (n < 98);
+
+ if (_sequence->id && !strcmp (item_id, _sequence->id))
+ return n + 2;
+
+ n++;
+ }
+
+ /* check entry points */
+
+ n = 0;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _cdio_list_node_data (node);
+ CdioListNode *node2;
+
+ /* default entry point */
+
+ if (_sequence->default_entry_id
+ && !strcmp (item_id, _sequence->default_entry_id))
+ return n + 100;
+ n++;
+
+ /* additional entry points */
+
+ _CDIO_LIST_FOREACH (node2, _sequence->entry_list)
+ {
+ entry_t *_entry = _cdio_list_node_data (node2);
+
+ vcd_assert (n < 500);
+
+ if (_entry->id && !strcmp (item_id, _entry->id))
+ return n + 100;
+
+ n++;
+ }
+ }
+
+ /* check sequence items */
+
+ n = 0;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _cdio_list_node_data (node);
+
+ vcd_assert (n < 1980);
+
+ if (_segment->id && !strcmp (item_id, _segment->id))
+ return n + MIN_ENCODED_SEGMENT_NUM;
+
+ n += _segment->segment_count;
+ }
+
+ return 0;
+}
+
+bool
+_vcd_pbc_available (const VcdObj *obj)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->pbc_list != NULL);
+
+ if (!_cdio_list_length (obj->pbc_list))
+ return false;
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_PBC))
+ {
+ vcd_warn ("PBC list not empty but VCD type not capable of PBC!");
+ return false;
+ }
+
+ return true;
+}
+
+uint16_t
+_vcd_pbc_max_lid (const VcdObj *obj)
+{
+ uint16_t retval = 0;
+
+ if (_vcd_pbc_available (obj))
+ retval = _cdio_list_length (obj->pbc_list);
+
+ return retval;
+}
+
+static size_t
+_vcd_pbc_node_length (const VcdObj *obj, const pbc_t *_pbc, bool extended)
+{
+ size_t retval = 0;
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ switch (_pbc->type)
+ {
+ int n;
+
+ case PBC_PLAYLIST:
+ n = _cdio_list_length (_pbc->item_id_list);
+ retval = __cd_offsetof (_PsdPlayListDescriptor, itemid[n]);
+ break;
+
+ case PBC_SELECTION:
+ n = _cdio_list_length (_pbc->select_id_list);
+
+ retval = __cd_offsetof (PsdSelectionListDescriptor_t, ofs[n]);
+
+ if (extended || _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ retval += __cd_offsetof (PsdSelectionListDescriptorExtended, area[n]);
+ break;
+
+ case PBC_END:
+ retval = sizeof (PsdEndListDescriptor);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return retval;
+}
+
+static uint16_t
+_lookup_psd_offset (const VcdObj *obj, const char item_id[], bool extended)
+{
+ CdioListNode *node;
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ /* disable it */
+ if (!item_id)
+ return PSD_OFS_DISABLED;
+
+ _CDIO_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _cdio_list_node_data (node);
+
+ if (!_pbc->id || strcmp (item_id, _pbc->id))
+ continue;
+
+ return (extended ? _pbc->offset_ext : _pbc->offset) / INFO_OFFSET_MULT;
+ }
+
+ vcd_error ("PSD: referenced PSD '%s' not found", item_id);
+
+ /* not found */
+ return PSD_OFS_DISABLED;
+}
+
+static void
+_vcd_pin_mark_id (const VcdObj *obj, const char _id[])
+{
+ mpeg_sequence_t *_seq;
+ mpeg_segment_t *_seg;
+
+ vcd_assert (obj != NULL);
+
+ if (!_id)
+ return;
+
+ if ((_seq = _vcd_obj_get_sequence_by_id ((VcdObj *) obj, _id)))
+ _seq->referenced = true;
+
+ if ((_seg = _vcd_obj_get_segment_by_id ((VcdObj *) obj, _id)))
+ _seg->referenced = true;
+}
+
+static void
+_vcd_pbc_mark_id (const VcdObj *obj, const char _id[])
+{
+ pbc_t *_pbc;
+
+ vcd_assert (obj != NULL);
+
+ if (!_id)
+ return;
+
+ _pbc = _vcd_pbc_byid (obj, _id);
+
+ if (!_pbc) /* not found */
+ return;
+
+ if (_pbc->referenced) /* already marked */
+ return;
+
+ _pbc->referenced = true;
+
+ switch (_pbc->type)
+ {
+ case PBC_PLAYLIST:
+ {
+ CdioListNode *node;
+
+ _vcd_pbc_mark_id (obj, _pbc->prev_id);
+ _vcd_pbc_mark_id (obj, _pbc->next_id);
+ _vcd_pbc_mark_id (obj, _pbc->retn_id);
+
+ _CDIO_LIST_FOREACH (node, _pbc->item_id_list)
+ {
+ const char *_id = _cdio_list_node_data (node);
+
+ _vcd_pin_mark_id (obj, _id);
+ }
+ }
+ break;
+
+ case PBC_SELECTION:
+ {
+ CdioListNode *node;
+
+ _vcd_pbc_mark_id (obj, _pbc->prev_id);
+ _vcd_pbc_mark_id (obj, _pbc->next_id);
+ _vcd_pbc_mark_id (obj, _pbc->retn_id);
+
+ if (_pbc->selection_type == _SEL_NORMAL)
+ _vcd_pbc_mark_id (obj, _pbc->default_id);
+
+ _vcd_pbc_mark_id (obj, _pbc->timeout_id);
+
+ _vcd_pin_mark_id (obj, _pbc->item_id);
+
+ _CDIO_LIST_FOREACH (node, _pbc->select_id_list)
+ {
+ const char *_id = _cdio_list_node_data (node);
+
+ _vcd_pbc_mark_id (obj, _id);
+ }
+ }
+ break;
+
+ case PBC_END:
+ _vcd_pin_mark_id (obj, _pbc->image_id);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+}
+
+void
+_vcd_pbc_check_unreferenced (const VcdObj *obj)
+{
+ CdioListNode *node;
+
+ /* clear all flags */
+
+ _CDIO_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _cdio_list_node_data (node);
+
+ _pbc->referenced = false;
+ }
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _cdio_list_node_data (node);
+
+ _sequence->referenced = false;
+ }
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _cdio_list_node_data (node);
+
+ _segment->referenced = false;
+ }
+
+ /* start from non-rejected lists */
+
+ _CDIO_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _cdio_list_node_data (node);
+
+ vcd_assert (_pbc->id != NULL);
+
+ if (_pbc->rejected)
+ continue;
+
+ _vcd_pbc_mark_id (obj, _pbc->id);
+ }
+
+ /* collect flags */
+
+ _CDIO_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _cdio_list_node_data (node);
+
+ if (!_pbc->referenced)
+ vcd_warn ("PSD item '%s' is unreachable", _pbc->id);
+ }
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _cdio_list_node_data (node);
+
+ if (!_sequence->referenced)
+ vcd_warn ("sequence '%s' is not reachable by PBC", _sequence->id);
+ }
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _cdio_list_node_data (node);
+
+ if (!_segment->referenced)
+ vcd_warn ("segment item '%s' is unreachable", _segment->id);
+ }
+
+}
+
+void
+_vcd_pbc_node_write (const VcdObj *obj, const pbc_t *_pbc, void *buf,
+ bool extended)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (_pbc != NULL);
+ vcd_assert (buf != NULL);
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ switch (_pbc->type)
+ {
+ case PBC_PLAYLIST:
+ {
+ _PsdPlayListDescriptor *_md = buf;
+ CdioListNode *node;
+ int n;
+
+ _md->type = PSD_TYPE_PLAY_LIST;
+ _md->noi = _cdio_list_length (_pbc->item_id_list);
+
+ vcd_assert (_pbc->lid < 0x8000);
+ _md->lid = uint16_to_be (_pbc->lid | (_pbc->rejected ? 0x8000 : 0));
+
+ _md->prev_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->prev_id, extended));
+ _md->next_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->next_id, extended));
+ _md->return_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->retn_id, extended));
+ _md->ptime = uint16_to_be (rint (_pbc->playing_time * 15.0));
+ _md->wtime = _wtime (_pbc->wait_time);
+ _md->atime = _wtime (_pbc->auto_pause_time);
+
+ n = 0;
+ _CDIO_LIST_FOREACH (node, _pbc->item_id_list)
+ {
+ const char *_id = _cdio_list_node_data (node);
+ uint16_t _pin;
+
+ if (_id)
+ {
+ _pin = _vcd_pbc_pin_lookup (obj, _id);
+
+ if (!_pin)
+ vcd_error ("PSD: referenced play item '%s' not found", _id);
+
+ _md->itemid[n] = uint16_to_be (_pin);
+ }
+ else
+ _md->itemid[n] = 0; /* play nothing */
+
+ n++;
+ }
+ }
+ break;
+
+ case PBC_SELECTION:
+ {
+ PsdSelectionListDescriptor_t *_md = buf;
+
+ const unsigned int _nos = _cdio_list_length (_pbc->select_id_list);
+
+ if (extended)
+ _md->type = PSD_TYPE_EXT_SELECTION_LIST;
+ else
+ _md->type = PSD_TYPE_SELECTION_LIST;
+
+ if (!IN (_pbc->bsn, 1, MAX_PBC_SELECTIONS))
+ vcd_error ("selection '%s': BSN (%d) not in range [1..%d]",
+ _pbc->id, _pbc->bsn, MAX_PBC_SELECTIONS);
+
+ if (!IN (_nos, 0, MAX_PBC_SELECTIONS))
+ vcd_error ("selection '%s': too many selections (%d > %d)",
+ _pbc->id, _nos, MAX_PBC_SELECTIONS);
+
+ if (_nos + _pbc->bsn > 100)
+ vcd_error ("selection '%s': BSN + NOS (%d + %d) > 100",
+ _pbc->id, _pbc->bsn, _nos);
+
+ _md->bsn = _pbc->bsn;
+ _md->nos = _nos;
+
+ vcd_assert (sizeof (PsdSelectionListFlags) == 1);
+
+ /* selection flags */
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ _md->flags.SelectionAreaFlag = true;
+ else
+ _md->flags.SelectionAreaFlag = false;
+
+ _md->flags.CommandListFlag = false;
+
+ vcd_assert (_pbc->lid < 0x8000);
+ _md->lid = uint16_to_be (_pbc->lid | (_pbc->rejected ? 0x8000 : 0));
+
+ _md->prev_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->prev_id, extended));
+ _md->next_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->next_id, extended));
+ _md->return_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->retn_id, extended));
+
+ switch (_pbc->selection_type)
+ {
+ case _SEL_NORMAL:
+ _md->default_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->default_id, extended));
+ break;
+
+ case _SEL_MULTI_DEF:
+ _md->default_ofs = uint16_to_be (PSD_OFS_MULTI_DEF);
+ if (_pbc->default_id)
+ vcd_warn ("ignoring default target '%s' for multi default selection '%s'",
+ _pbc->default_id, _pbc->id);
+ break;
+
+ case _SEL_MULTI_DEF_NO_NUM:
+ _md->default_ofs = uint16_to_be (PSD_OFS_MULTI_DEF_NO_NUM);
+ if (_pbc->default_id)
+ vcd_warn ("ignoring default target '%s' for multi default (w/o num) selection '%s'",
+ _pbc->default_id, _pbc->id);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ _md->timeout_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->timeout_id, extended));
+ _md->totime = _wtime (_pbc->timeout_time);
+
+ if (_pbc->loop_count > 0x7f)
+ vcd_warn ("loop count %d > 127", _pbc->loop_count);
+
+ _md->loop = (_pbc->loop_count > 0x7f) ? 0x7f : _pbc->loop_count;
+
+ if (_pbc->jump_delayed)
+ _md->loop |= 0x80;
+
+ /* timeout related sanity checks */
+ if (_pbc->loop_count > 0
+ && _pbc->timeout_time >= 0
+ && !_pbc->timeout_id
+ && !_nos)
+ vcd_warn ("PSD: selection '%s': neither timeout nor select target available, but neither loop count is infinite nor timeout wait time", _pbc->id);
+
+ if (_pbc->timeout_id && (_pbc->timeout_time < 0 || _pbc->loop_count <= 0))
+ vcd_warn ("PSD: selection '%s': timeout target '%s' is never used due to loop count or timeout wait time given", _pbc->id, _pbc->timeout_id);
+
+ if (_pbc->item_id)
+ {
+ const uint16_t _pin = _vcd_pbc_pin_lookup (obj, _pbc->item_id);
+
+ if (!_pin)
+ vcd_error ("PSD: referenced play item '%s' not found", _pbc->item_id);
+
+ _md->itemid = uint16_to_be (_pin);
+ }
+ else
+ _md->itemid = 0; /* play nothing */
+
+ /* sanity checks */
+ switch (_pbc->selection_type)
+ {
+ case _SEL_NORMAL:
+ break;
+
+ case _SEL_MULTI_DEF:
+ case _SEL_MULTI_DEF_NO_NUM:
+ if (_pbc->jump_delayed)
+ vcd_warn ("selection '%s': jump timing shall be immediate", _pbc->id);
+
+ if (_pbc->bsn != 1)
+ vcd_error ("selection '%s': BSN != 1 for multi default selection",
+ _pbc->id);
+
+ /* checking NOS == NOE */
+ if (!_pbc->item_id)
+ vcd_error ("selection '%s': play nothing play item not allowed for multidefault list",
+ _pbc->id);
+
+ {
+ mpeg_sequence_t *_seq;
+
+ if ((_seq = _vcd_obj_get_sequence_by_id ((VcdObj *) obj, _pbc->item_id))
+ || (_seq = _vcd_obj_get_sequence_by_entry_id ((VcdObj *) obj, _pbc->item_id)))
+ {
+ const unsigned _entries =
+ _cdio_list_length (_seq->entry_list) + 1;
+
+ if (_nos != _entries)
+ vcd_error ("selection '%s': number of entrypoints"
+ " (%d for sequence '%s') != number of selections (%d)",
+ _pbc->id, _entries, _pbc->item_id, _nos);
+ }
+ else
+ vcd_error ("selection '%s': play item '%s'"
+ " is requried to be sequence or entry point"
+ " item for multi default selecton",
+ _pbc->id, _pbc->item_id);
+ }
+
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ /* fill selection array */
+ {
+ CdioListNode *node = NULL;
+ int idx = 0;
+
+ idx = 0;
+ _CDIO_LIST_FOREACH (node, _pbc->select_id_list)
+ {
+ const char *_id = _cdio_list_node_data (node);
+
+ _md->ofs[idx] =
+ uint16_to_be (_lookup_psd_offset (obj, _id, extended));
+
+ idx++;
+ }
+ }
+
+ if (extended || _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ PsdSelectionListDescriptorExtended *_md2;
+ CdioListNode *node;
+ int n;
+
+ /* append extended selection areas */
+
+ _md2 = (void *) &_md->ofs[_nos];
+
+ _set_area_helper (&_md2->next_area, _pbc->next_area, _pbc->id);
+ _set_area_helper (&_md2->prev_area, _pbc->prev_area, _pbc->id);
+ _set_area_helper (&_md2->return_area, _pbc->return_area, _pbc->id);
+
+ _set_area_helper (&_md2->default_area, _pbc->default_area, _pbc->id);
+
+ n = 0;
+ if (_pbc->select_area_list)
+ _CDIO_LIST_FOREACH (node, _pbc->select_area_list)
+ {
+ const pbc_area_t *_area = _cdio_list_node_data (node);
+
+ _set_area_helper (&_md2->area[n], _area, _pbc->id);
+
+ n++;
+ }
+
+ vcd_assert (n == _nos);
+ }
+ }
+ break;
+
+ case PBC_END:
+ {
+ PsdEndListDescriptor *_md = buf;
+
+ _md->type = PSD_TYPE_END_LIST;
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ _md->next_disc = _pbc->next_disc;
+
+ if (_pbc->image_id)
+ {
+ uint16_t _pin = _vcd_pbc_pin_lookup (obj, _pbc->image_id);
+ mpeg_segment_t *_segment;
+
+ if (!_pbc->next_disc)
+ vcd_warn ("PSD: endlist '%s': change disc picture given,"
+ " but next volume is 0", _pbc->id);
+
+ if (!_pin)
+ vcd_error ("PSD: referenced play item '%s' not found",
+ _pbc->item_id);
+
+ _md->change_pic = uint16_to_be (_pin);
+
+ /* sanity checks */
+
+ _segment = _vcd_obj_get_segment_by_id ((VcdObj *) obj,
+ _pbc->image_id);
+
+ if (!_segment)
+ vcd_warn ("PSD: endlist '%s': referenced play item '%s'"
+ " is not a segment play item",
+ _pbc->id, _pbc->image_id);
+ else if (_segment->info->shdr[0].seen
+ || !(_segment->info->shdr[1].seen || _segment->info->shdr[2].seen))
+ vcd_warn ("PSD: endlist '%s': referenced play item '%s'"
+ " should be a still picture",
+ _pbc->id, _pbc->image_id);
+ }
+ }
+ else if (_pbc->next_disc || _pbc->image_id)
+ vcd_warn ("extended end list attributes ignored for non-SVCD");
+ }
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+}
+
+pbc_t *
+vcd_pbc_new (enum pbc_type_t type)
+{
+ pbc_t *_pbc;
+
+ _pbc = _vcd_malloc (sizeof (pbc_t));
+ _pbc->type = type;
+
+ switch (type)
+ {
+ case PBC_PLAYLIST:
+ _pbc->item_id_list = _cdio_list_new ();
+ break;
+
+ case PBC_SELECTION:
+ _pbc->select_id_list = _cdio_list_new ();
+ _pbc->select_area_list = _cdio_list_new ();
+ break;
+
+ case PBC_END:
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return _pbc;
+}
+
+/*
+ */
+
+bool
+_vcd_pbc_finalize (VcdObj *obj)
+{
+ CdioListNode *node;
+ unsigned offset = 0, offset_ext = 0;
+ unsigned lid;
+
+ lid = 1;
+ _CDIO_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _cdio_list_node_data (node);
+ unsigned length, length_ext = 0;
+
+ length = _vcd_pbc_node_length (obj, _pbc, false);
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ length_ext = _vcd_pbc_node_length (obj, _pbc, true);
+
+ /* round them up to... */
+ length = _vcd_ceil2block (length, INFO_OFFSET_MULT);
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ length_ext = _vcd_ceil2block (length_ext, INFO_OFFSET_MULT);
+
+ /* node may not cross sector boundary! */
+ offset = _vcd_ofs_add (offset, length, ISO_BLOCKSIZE);
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ offset_ext = _vcd_ofs_add (offset_ext, length_ext, ISO_BLOCKSIZE);
+
+ /* save start offsets */
+ _pbc->offset = offset - length;
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ _pbc->offset_ext = offset_ext - length_ext;
+
+ _pbc->lid = lid;
+
+ lid++;
+ }
+
+ obj->psd_size = offset;
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ obj->psdx_size = offset_ext;
+
+ vcd_debug ("pbc: psd size %d (extended psd %d)", offset, offset_ext);
+
+ return true;
+}
diff --git a/contrib/libvcd/pbc.h b/contrib/libvcd/pbc.h
new file mode 100644
index 000000000..9972c3143
--- /dev/null
+++ b/contrib/libvcd/pbc.h
@@ -0,0 +1,157 @@
+/*
+ $Id: pbc.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_PBC_H__
+#define __VCD_PBC_H__
+
+#include <libvcd/types.h>
+
+/* Private includes */
+#include "data_structures.h"
+#include "util.h"
+#include "vcd.h"
+
+enum pbc_type_t {
+ PBC_INVALID = 0,
+ PBC_PLAYLIST,
+ PBC_SELECTION,
+ PBC_END
+};
+
+typedef struct psd_area_t pbc_area_t; /* fixme */
+#define pbc_area_t_SIZEOF struct_psd_area_t_SIZEOF
+
+static inline pbc_area_t *
+vcd_pbc_area_new (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)
+{
+ pbc_area_t *_new_area = _vcd_malloc (sizeof (pbc_area_t));
+
+ _new_area->x1 = x1;
+ _new_area->y1 = y1;
+ _new_area->x2 = x2;
+ _new_area->y2 = y2;
+
+ return _new_area;
+}
+
+/* typedef struct _pbc_t pbc_t; */
+
+struct _pbc_t {
+ enum pbc_type_t type;
+
+ char *id;
+
+ bool rejected;
+
+ /* pbc ref check */
+ bool referenced;
+
+ /* used for play/selection lists */
+ char *prev_id;
+ char *next_id;
+ char *retn_id;
+
+ /* used for play lists */
+ double playing_time;
+ int wait_time;
+ int auto_pause_time;
+ CdioList *item_id_list; /* char */
+
+ /* used for selection lists */
+ enum selection_type_t {
+ _SEL_NORMAL = 0,
+ _SEL_MULTI_DEF,
+ _SEL_MULTI_DEF_NO_NUM
+ } selection_type;
+
+ pbc_area_t *prev_area;
+ pbc_area_t *next_area;
+ pbc_area_t *return_area;
+ pbc_area_t *default_area; /* depends on selection_type */
+ CdioList *select_area_list; /* pbc_area_t */
+
+ unsigned bsn;
+ char *default_id;
+ char *timeout_id;
+ int timeout_time;
+ unsigned loop_count;
+ bool jump_delayed;
+ char *item_id;
+ CdioList *select_id_list; /* char */
+
+ /* used for end lists */
+ char *image_id;
+ unsigned next_disc;
+
+ /* computed values */
+ unsigned lid;
+ unsigned offset;
+ unsigned offset_ext;
+};
+
+enum item_type_t {
+ ITEM_TYPE_NOTFOUND = 0,
+ ITEM_TYPE_NOOP,
+ ITEM_TYPE_TRACK,
+ ITEM_TYPE_ENTRY,
+ ITEM_TYPE_SEGMENT,
+ ITEM_TYPE_PBC
+};
+
+/* functions */
+
+pbc_t *
+vcd_pbc_new (enum pbc_type_t type);
+
+pbc_t *
+_vcd_pbc_init (pbc_t *_pbc);
+
+void
+vcd_pbc_destroy (pbc_t *obj);
+
+unsigned
+_vcd_pbc_lid_lookup (const VcdObj *obj, const char item_id[]);
+
+enum item_type_t
+_vcd_pbc_lookup (const VcdObj *obj, const char item_id[]);
+
+uint16_t
+_vcd_pbc_pin_lookup (const VcdObj *obj, const char item_id[]);
+
+unsigned
+_vcd_pbc_list_calc_size (const pbc_t *_pbc, bool extended);
+
+bool
+_vcd_pbc_finalize (VcdObj *obj);
+
+bool
+_vcd_pbc_available (const VcdObj *obj);
+
+uint16_t
+_vcd_pbc_max_lid (const VcdObj *obj);
+
+void
+_vcd_pbc_node_write (const VcdObj *obj, const pbc_t *_pbc, void *buf,
+ bool extended);
+
+void
+_vcd_pbc_check_unreferenced (const VcdObj *obj);
+
+#endif /* __VCD_PBC_H__ */
diff --git a/contrib/libvcd/salloc.c b/contrib/libvcd/salloc.c
new file mode 100644
index 000000000..d6fc41c68
--- /dev/null
+++ b/contrib/libvcd/salloc.c
@@ -0,0 +1,212 @@
+/*
+ $Id: salloc.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+
+#include <cdio/cdio.h>
+
+/* Public headers */
+#include <libvcd/types.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "salloc.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: salloc.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $";
+
+#define VCD_SALLOC_CHUNK_SIZE 16
+
+struct _VcdSalloc
+{
+ uint8_t *data;
+ uint32_t len;
+ uint32_t alloced_chunks;
+};
+
+static void
+_vcd_salloc_set_size (VcdSalloc *bitmap, uint32_t newlen)
+{
+ uint32_t new_alloced_chunks;
+
+ vcd_assert (bitmap != NULL);
+ vcd_assert (newlen >= bitmap->len);
+
+ new_alloced_chunks = newlen / VCD_SALLOC_CHUNK_SIZE;
+ if (newlen % VCD_SALLOC_CHUNK_SIZE)
+ new_alloced_chunks++;
+
+ if (bitmap->alloced_chunks < new_alloced_chunks)
+ {
+ bitmap->data =
+ realloc (bitmap->data, new_alloced_chunks * VCD_SALLOC_CHUNK_SIZE);
+ memset (bitmap->data + (VCD_SALLOC_CHUNK_SIZE * bitmap->alloced_chunks),
+ 0,
+ VCD_SALLOC_CHUNK_SIZE * (new_alloced_chunks -
+ bitmap->alloced_chunks));
+ bitmap->alloced_chunks = new_alloced_chunks;
+ }
+
+ bitmap->len = newlen;
+}
+
+static bool
+_vcd_salloc_is_set (const VcdSalloc *bitmap, uint32_t sector)
+{
+ unsigned _byte = sector / 8;
+ unsigned _bit = sector % 8;
+
+ if (_byte < bitmap->len)
+ return (bitmap->data[_byte] & (1 << _bit)) != 0;
+ else
+ return false;
+}
+
+static void
+_vcd_salloc_set (VcdSalloc *bitmap, uint32_t sector)
+{
+ unsigned _byte = sector / 8;
+ unsigned _bit = sector % 8;
+
+ if (_byte >= bitmap->len)
+ {
+ unsigned oldlen = bitmap->len;
+ _vcd_salloc_set_size (bitmap, _byte + 1);
+ memset (bitmap->data + oldlen, 0x00, _byte + 1 - oldlen);
+ }
+
+ bitmap->data[_byte] |= (1 << _bit);
+}
+
+static void
+_vcd_salloc_unset (VcdSalloc *bitmap, uint32_t sector)
+{
+ unsigned _byte = sector / 8;
+ unsigned _bit = sector % 8;
+
+ if (_byte >= bitmap->len)
+ vcd_assert_not_reached ();
+
+ bitmap->data[_byte] &= ~(1 << _bit);
+}
+
+/* exported */
+
+uint32_t _vcd_salloc (VcdSalloc *bitmap, uint32_t hint, uint32_t size)
+{
+ if (!size)
+ {
+ size++;
+ vcd_warn
+ ("request of 0 sectors allocment fixed up to 1 sector (this is harmless)");
+ }
+
+ vcd_assert (size > 0);
+
+ if (hint != SECTOR_NIL)
+ {
+ uint32_t i;
+ for (i = 0; i < size; i++)
+ if (_vcd_salloc_is_set (bitmap, hint + i))
+ return SECTOR_NIL;
+
+ /* everything's ok for allocing */
+
+ i = size;
+ while (i)
+ _vcd_salloc_set (bitmap, hint + (--i));
+ /* we begin with highest byte, in order to minimizing
+ realloc's in sector_set */
+
+ return hint;
+ }
+
+ /* find the lowest possible ... */
+
+ hint = 0;
+
+ while (_vcd_salloc (bitmap, hint, size) == SECTOR_NIL)
+ hint++;
+
+ return hint;
+}
+
+void
+_vcd_salloc_free (VcdSalloc *bitmap, uint32_t sec, uint32_t size)
+{
+ uint32_t i;
+
+ for (i = 0; i < size; i++)
+ {
+ vcd_assert (_vcd_salloc_is_set (bitmap, sec + i));
+
+ _vcd_salloc_unset (bitmap, sec + i);
+ }
+}
+
+VcdSalloc *
+_vcd_salloc_new (void)
+{
+ VcdSalloc *newobj = _vcd_malloc (sizeof (VcdSalloc));
+ return newobj;
+}
+
+void
+_vcd_salloc_destroy (VcdSalloc *bitmap)
+{
+ vcd_assert (bitmap != NULL);
+
+ free (bitmap->data);
+ free (bitmap);
+}
+
+uint32_t _vcd_salloc_get_highest (const VcdSalloc *bitmap)
+{
+ uint8_t last;
+ unsigned n;
+
+ vcd_assert (bitmap != NULL);
+
+ last = bitmap->data[bitmap->len - 1];
+
+ vcd_assert (last != 0);
+
+ n = 8;
+ while (n)
+ if ((1 << --n) & last)
+ break;
+
+ return (bitmap->len - 1) * 8 + n;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/salloc.h b/contrib/libvcd/salloc.h
new file mode 100644
index 000000000..4676f9b45
--- /dev/null
+++ b/contrib/libvcd/salloc.h
@@ -0,0 +1,56 @@
+/*
+ $Id: salloc.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* sector allocation management */
+
+#ifndef _SALLOC_H_
+#define _SALLOC_H_
+
+#include <libvcd/types.h>
+
+#define SECTOR_NIL ((uint32_t)(-1))
+
+typedef struct _VcdSalloc VcdSalloc;
+
+VcdSalloc *
+_vcd_salloc_new (void);
+
+void
+_vcd_salloc_destroy (VcdSalloc *bitmap);
+
+uint32_t
+_vcd_salloc (VcdSalloc *bitmap, uint32_t hint, uint32_t size);
+
+void
+_vcd_salloc_free (VcdSalloc *bitmap, uint32_t sec, uint32_t size);
+
+uint32_t
+_vcd_salloc_get_highest (const VcdSalloc *bitmap);
+
+#endif /* _SALLOC_H_ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/sector.c b/contrib/libvcd/sector.c
new file mode 100644
index 000000000..d072a4f17
--- /dev/null
+++ b/contrib/libvcd/sector.c
@@ -0,0 +1,268 @@
+/*
+ $Id: sector.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ (C) 1998 Heiko Eissfeldt <heiko@colossus.escape.de>
+ portions used & Chris Smith
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+
+#include <cdio/cdio.h>
+
+#include <libvcd/types.h>
+
+#include <libvcd/sector.h>
+
+/* Private includes */
+#include "vcd_assert.h"
+#include "bytesex.h"
+#include "salloc.h"
+#include "sector_private.h"
+
+static const char _rcsid[] = "$Id: sector.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $";
+
+static const uint8_t sync_pattern[12] = {
+ 0x00, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0x00
+};
+
+static void
+build_address (void *buf, sectortype_t sectortype, uint32_t address)
+{
+ raw_cd_sector_t *sector = buf;
+
+ vcd_assert (sizeof(raw_cd_sector_t) == CDIO_CD_FRAMESIZE_RAW-DATA_LEN);
+
+ cdio_lba_to_msf(address, &(sector->msf));
+
+ switch(sectortype) {
+ case MODE_0:
+ sector->mode = 0;
+ break;
+ case MODE_2:
+ case MODE_2_FORM_1:
+ case MODE_2_FORM_2:
+ sector->mode = 2;
+ break;
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+}
+
+/* From cdrtools-1.11a25 */
+static uint32_t
+build_edc(const uint8_t inout[], int from, int upto)
+{
+ const uint8_t *p = inout+from;
+ uint32_t result = 0;
+
+ upto -= from-1;
+ upto /= 4;
+ while (--upto >= 0) {
+ result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
+ result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
+ result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
+ result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
+ }
+ return (result);
+}
+
+/* From cdrtools-1.11a40 */
+static void
+encode_L2_Q(uint8_t inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q])
+{
+ uint8_t *dps;
+ uint8_t *dp;
+ uint8_t *Q;
+ int i, j;
+
+ Q = inout + 4 + L2_RAW + 4 + 8 + L2_P;
+
+ dps = inout;
+ for (j = 0; j < 26; j++) {
+ uint16_t a, b;
+
+ a = b = 0;
+ dp = dps;
+ for (i = 0; i < 43; i++) {
+
+ /* LSB */
+ a ^= L2sq[i][*dp++];
+
+ /* MSB */
+ b ^= L2sq[i][*dp];
+
+ dp += 2*44-1;
+ if (dp >= &inout[(4 + L2_RAW + 4 + 8 + L2_P)]) {
+ dp -= (4 + L2_RAW + 4 + 8 + L2_P);
+ }
+ }
+ Q[0] = a >> 8;
+ Q[26*2] = a;
+ Q[1] = b >> 8;
+ Q[26*2+1] = b;
+
+ Q += 2;
+ dps += 2*43;
+ }
+}
+
+static void
+encode_L2_P (uint8_t inout[4 + L2_RAW + 4 + 8 + L2_P])
+{
+ uint8_t *dp;
+ unsigned char *P;
+ int i, j;
+
+ P = inout + 4 + L2_RAW + 4 + 8;
+
+ for (j = 0; j < 43; j++) {
+ uint16_t a;
+ uint16_t b;
+
+ a = b = 0;
+ dp = inout;
+ for (i = 19; i < 43; i++) {
+
+ /* LSB */
+ a ^= L2sq[i][*dp++];
+
+ /* MSB */
+ b ^= L2sq[i][*dp];
+
+ dp += 2*43 -1;
+ }
+ P[0] = a >> 8;
+ P[43*2] = a;
+ P[1] = b >> 8;
+ P[43*2+1] = b;
+
+ P += 2;
+ inout += 2;
+ }
+}
+
+/* Layer 2 Product code en/decoder */
+static void
+do_encode_L2 (void *buf, sectortype_t sectortype, uint32_t address)
+{
+ raw_cd_sector_t *raw_sector = buf;
+
+ vcd_assert (buf != NULL);
+
+ vcd_assert (sizeof (sync_pattern) == SYNC_LEN);
+ vcd_assert (sizeof (mode2_form1_sector_t) == CDIO_CD_FRAMESIZE_RAW);
+ vcd_assert (sizeof (mode2_form2_sector_t) == CDIO_CD_FRAMESIZE_RAW);
+ vcd_assert (sizeof (mode0_sector_t) == CDIO_CD_FRAMESIZE_RAW);
+ vcd_assert (sizeof (raw_cd_sector_t) == SYNC_LEN+HEADER_LEN);
+
+ memset (raw_sector, 0, SYNC_LEN+HEADER_LEN);
+ memcpy (raw_sector->sync, sync_pattern, sizeof (sync_pattern));
+
+ switch (sectortype) {
+ case MODE_0:
+ {
+ mode0_sector_t *sector = buf;
+
+ memset(sector->data, 0, sizeof(sector->data));
+ }
+ break;
+ case MODE_2:
+ break;
+ case MODE_2_FORM_1:
+ {
+ mode2_form1_sector_t *sector = buf;
+
+ sector->edc = uint32_to_le(build_edc(buf, 16, 16+8+2048-1));
+
+ encode_L2_P((uint8_t*)buf+SYNC_LEN);
+ encode_L2_Q((uint8_t*)buf+SYNC_LEN);
+ }
+ break;
+ case MODE_2_FORM_2:
+ {
+ mode2_form2_sector_t *sector = buf;
+
+ sector->edc = uint32_to_le(build_edc(buf, 16, 16+8+2324-1));
+ }
+ break;
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ build_address (buf, sectortype, address);
+}
+
+void
+_vcd_make_mode2 (void *raw_sector, const void *data, uint32_t extent,
+ uint8_t fnum, uint8_t cnum, uint8_t sm, uint8_t ci)
+{
+ uint8_t *subhdr = (uint8_t*)raw_sector+16;
+
+ vcd_assert (raw_sector != NULL);
+ vcd_assert (data != NULL);
+ vcd_assert (extent != SECTOR_NIL);
+
+ memset (raw_sector, 0, CDIO_CD_FRAMESIZE_RAW);
+
+ subhdr[0] = subhdr[4] = fnum;
+ subhdr[1] = subhdr[5] = cnum;
+ subhdr[2] = subhdr[6] = sm;
+ subhdr[3] = subhdr[7] = ci;
+
+ if (sm & SM_FORM2)
+ {
+ memcpy ((char*)raw_sector+CDIO_CD_XA_SYNC_HEADER, data,
+ M2F2_SECTOR_SIZE);
+ do_encode_L2 (raw_sector, MODE_2_FORM_2, extent+CDIO_PREGAP_SECTORS);
+ }
+ else
+ {
+ memcpy ((char*)raw_sector+CDIO_CD_XA_SYNC_HEADER, data,
+ CDIO_CD_FRAMESIZE);
+ do_encode_L2 (raw_sector, MODE_2_FORM_1, extent+CDIO_PREGAP_SECTORS);
+ }
+}
+
+void
+_vcd_make_raw_mode2 (void *raw_sector, const void *data, uint32_t extent)
+{
+ vcd_assert (raw_sector != NULL);
+ vcd_assert (data != NULL);
+ vcd_assert (extent != SECTOR_NIL);
+
+ memset (raw_sector, 0, CDIO_CD_FRAMESIZE_RAW);
+
+ memcpy ((char*)raw_sector+12+4, data, M2RAW_SECTOR_SIZE);
+ do_encode_L2 (raw_sector, MODE_2, extent+CDIO_PREGAP_SECTORS);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/sector_private.h b/contrib/libvcd/sector_private.h
new file mode 100644
index 000000000..9411b5420
--- /dev/null
+++ b/contrib/libvcd/sector_private.h
@@ -0,0 +1,1348 @@
+/*
+ $Id: sector_private.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ (C) 1998 Heiko Eissfeldt <heiko@colossus.escape.de>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_CD_SECTOR_PRIVATE_H__
+#define __VCD_CD_SECTOR_PRIVATE_H__
+
+#define RS_L12_BITS 8
+
+#define L2_RAW (1024*2)
+#define L2_Q (26*2*2)
+#define L2_P (43*2*2)
+
+typedef enum {
+ MODE_0,
+ MODE_2,
+ MODE_2_FORM_1,
+ MODE_2_FORM_2
+} sectortype_t;
+
+#define SYNC_LEN 12
+#define DATA_LEN 2336
+#define HEADER_LEN 4
+
+typedef struct {
+ uint8_t sync[SYNC_LEN];
+ msf_t msf;
+ uint8_t mode;
+ uint8_t user_data[EMPTY_ARRAY_SIZE];
+} raw_cd_sector_t;
+
+#define raw_cd_sector_t_SIZEOF (SYNC_LEN+HEADER_LEN)
+
+typedef struct {
+ uint8_t sync[SYNC_LEN];
+ msf_t msf;
+ uint8_t mode;
+} sector_header_t;
+
+#define sector_header_t_SIZEOF (SYNC_LEN+HEADER_LEN)
+
+typedef struct {
+ sector_header_t sector_header;
+ uint8_t data[M2RAW_SECTOR_SIZE];
+} mode0_sector_t;
+
+#define mode0_sector_t_SIZEOF CDIO_CD_FRAMESIZE_RAW
+
+typedef struct {
+ sector_header_t sector_header;
+ uint8_t subheader[CDIO_CD_SUBHEADER_SIZE];
+ uint8_t data[CDIO_CD_FRAMESIZE];
+ uint32_t edc;
+ uint8_t l2_p[L2_P];
+ uint8_t l2_q[L2_Q];
+} mode2_form1_sector_t;
+
+#define mode2_form1_sector_t_SIZEOF CDIO_CD_FRAMESIZE_RAW
+
+typedef struct {
+ sector_header_t sector_header;
+ uint8_t subheader[CDIO_CD_SUBHEADER_SIZE];
+ uint8_t data[M2F2_SECTOR_SIZE];
+ uint32_t edc;
+} mode2_form2_sector_t;
+
+#define mode2_form2_sector_t_SIZEOF CDIO_CD_FRAMESIZE_RAW
+
+/*****************************************************************/
+/* */
+/* CRC LOOKUP TABLE */
+/* ================ */
+/* The following CRC lookup table was generated automagically */
+/* by the Rocksoft^tm Model CRC Algorithm Table Generation */
+/* Program V1.0 using the following model parameters: */
+/* */
+/* Width : 4 bytes. */
+/* Poly : 0x8001801BL */
+/* Reverse : TRUE. */
+/* */
+/* For more information on the Rocksoft^tm Model CRC Algorithm, */
+/* see the document titled "A Painless Guide to CRC Error */
+/* Detection Algorithms" by Ross Williams */
+/* (ross@guest.adelaide.edu.au.). This document is likely to be */
+/* in the FTP archive "ftp.adelaide.edu.au/pub/rocksoft". */
+/* */
+/*****************************************************************/
+
+static const uint32_t EDC_crctable[256] =
+{
+ 0x00000000U, 0x90910101U, 0x91210201U, 0x01B00300U,
+ 0x92410401U, 0x02D00500U, 0x03600600U, 0x93F10701U,
+ 0x94810801U, 0x04100900U, 0x05A00A00U, 0x95310B01U,
+ 0x06C00C00U, 0x96510D01U, 0x97E10E01U, 0x07700F00U,
+ 0x99011001U, 0x09901100U, 0x08201200U, 0x98B11301U,
+ 0x0B401400U, 0x9BD11501U, 0x9A611601U, 0x0AF01700U,
+ 0x0D801800U, 0x9D111901U, 0x9CA11A01U, 0x0C301B00U,
+ 0x9FC11C01U, 0x0F501D00U, 0x0EE01E00U, 0x9E711F01U,
+ 0x82012001U, 0x12902100U, 0x13202200U, 0x83B12301U,
+ 0x10402400U, 0x80D12501U, 0x81612601U, 0x11F02700U,
+ 0x16802800U, 0x86112901U, 0x87A12A01U, 0x17302B00U,
+ 0x84C12C01U, 0x14502D00U, 0x15E02E00U, 0x85712F01U,
+ 0x1B003000U, 0x8B913101U, 0x8A213201U, 0x1AB03300U,
+ 0x89413401U, 0x19D03500U, 0x18603600U, 0x88F13701U,
+ 0x8F813801U, 0x1F103900U, 0x1EA03A00U, 0x8E313B01U,
+ 0x1DC03C00U, 0x8D513D01U, 0x8CE13E01U, 0x1C703F00U,
+ 0xB4014001U, 0x24904100U, 0x25204200U, 0xB5B14301U,
+ 0x26404400U, 0xB6D14501U, 0xB7614601U, 0x27F04700U,
+ 0x20804800U, 0xB0114901U, 0xB1A14A01U, 0x21304B00U,
+ 0xB2C14C01U, 0x22504D00U, 0x23E04E00U, 0xB3714F01U,
+ 0x2D005000U, 0xBD915101U, 0xBC215201U, 0x2CB05300U,
+ 0xBF415401U, 0x2FD05500U, 0x2E605600U, 0xBEF15701U,
+ 0xB9815801U, 0x29105900U, 0x28A05A00U, 0xB8315B01U,
+ 0x2BC05C00U, 0xBB515D01U, 0xBAE15E01U, 0x2A705F00U,
+ 0x36006000U, 0xA6916101U, 0xA7216201U, 0x37B06300U,
+ 0xA4416401U, 0x34D06500U, 0x35606600U, 0xA5F16701U,
+ 0xA2816801U, 0x32106900U, 0x33A06A00U, 0xA3316B01U,
+ 0x30C06C00U, 0xA0516D01U, 0xA1E16E01U, 0x31706F00U,
+ 0xAF017001U, 0x3F907100U, 0x3E207200U, 0xAEB17301U,
+ 0x3D407400U, 0xADD17501U, 0xAC617601U, 0x3CF07700U,
+ 0x3B807800U, 0xAB117901U, 0xAAA17A01U, 0x3A307B00U,
+ 0xA9C17C01U, 0x39507D00U, 0x38E07E00U, 0xA8717F01U,
+ 0xD8018001U, 0x48908100U, 0x49208200U, 0xD9B18301U,
+ 0x4A408400U, 0xDAD18501U, 0xDB618601U, 0x4BF08700U,
+ 0x4C808800U, 0xDC118901U, 0xDDA18A01U, 0x4D308B00U,
+ 0xDEC18C01U, 0x4E508D00U, 0x4FE08E00U, 0xDF718F01U,
+ 0x41009000U, 0xD1919101U, 0xD0219201U, 0x40B09300U,
+ 0xD3419401U, 0x43D09500U, 0x42609600U, 0xD2F19701U,
+ 0xD5819801U, 0x45109900U, 0x44A09A00U, 0xD4319B01U,
+ 0x47C09C00U, 0xD7519D01U, 0xD6E19E01U, 0x46709F00U,
+ 0x5A00A000U, 0xCA91A101U, 0xCB21A201U, 0x5BB0A300U,
+ 0xC841A401U, 0x58D0A500U, 0x5960A600U, 0xC9F1A701U,
+ 0xCE81A801U, 0x5E10A900U, 0x5FA0AA00U, 0xCF31AB01U,
+ 0x5CC0AC00U, 0xCC51AD01U, 0xCDE1AE01U, 0x5D70AF00U,
+ 0xC301B001U, 0x5390B100U, 0x5220B200U, 0xC2B1B301U,
+ 0x5140B400U, 0xC1D1B501U, 0xC061B601U, 0x50F0B700U,
+ 0x5780B800U, 0xC711B901U, 0xC6A1BA01U, 0x5630BB00U,
+ 0xC5C1BC01U, 0x5550BD00U, 0x54E0BE00U, 0xC471BF01U,
+ 0x6C00C000U, 0xFC91C101U, 0xFD21C201U, 0x6DB0C300U,
+ 0xFE41C401U, 0x6ED0C500U, 0x6F60C600U, 0xFFF1C701U,
+ 0xF881C801U, 0x6810C900U, 0x69A0CA00U, 0xF931CB01U,
+ 0x6AC0CC00U, 0xFA51CD01U, 0xFBE1CE01U, 0x6B70CF00U,
+ 0xF501D001U, 0x6590D100U, 0x6420D200U, 0xF4B1D301U,
+ 0x6740D400U, 0xF7D1D501U, 0xF661D601U, 0x66F0D700U,
+ 0x6180D800U, 0xF111D901U, 0xF0A1DA01U, 0x6030DB00U,
+ 0xF3C1DC01U, 0x6350DD00U, 0x62E0DE00U, 0xF271DF01U,
+ 0xEE01E001U, 0x7E90E100U, 0x7F20E200U, 0xEFB1E301U,
+ 0x7C40E400U, 0xECD1E501U, 0xED61E601U, 0x7DF0E700U,
+ 0x7A80E800U, 0xEA11E901U, 0xEBA1EA01U, 0x7B30EB00U,
+ 0xE8C1EC01U, 0x7850ED00U, 0x79E0EE00U, 0xE971EF01U,
+ 0x7700F000U, 0xE791F101U, 0xE621F201U, 0x76B0F300U,
+ 0xE541F401U, 0x75D0F500U, 0x7460F600U, 0xE4F1F701U,
+ 0xE381F801U, 0x7310F900U, 0x72A0FA00U, 0xE231FB01U,
+ 0x71C0FC00U, 0xE151FD01U, 0xE0E1FE01U, 0x7070FF00U,
+};
+
+
+static const uint16_t L2sq[43][256] = {
+ { 0, 44719, 16707, 61420, 33414, 11305, 50117, 28010, 6417, 47038,
+ 22610, 63229, 39831, 13624, 56020, 29819, 12834, 40077, 29537, 56782,
+ 45220, 7691, 61927, 24392, 11059, 34204, 27248, 50399, 43445, 1818,
+ 59638, 18009, 25668, 51947, 9479, 35752, 59074, 18541, 42881, 2350,
+ 32085, 54266, 15382, 37561, 65491, 20860, 48784, 4159, 22118, 63689,
+ 5925, 47498, 54496, 31311, 38307, 15116, 20343, 57816, 3636, 41115,
+ 52721, 25438, 36018, 8733, 51336, 26151, 35275, 10084, 18958, 58529,
+ 2893, 42466, 53657, 32566, 37082, 15989, 21279, 64944, 4700, 48371,
+ 64170, 21509, 48105, 5446, 30764, 54915, 14703, 38848, 58299, 19732,
+ 41720, 3159, 24893, 53138, 8318, 36561, 44236, 611, 60815, 17184,
+ 11850, 32997, 28425, 49574, 46557, 7026, 62622, 23089, 14171, 39412,
+ 30232, 55479, 40686, 12353, 57261, 28930, 7272, 45767, 23851, 62340,
+ 34815, 10576, 50876, 26643, 1401, 43990, 17466, 60053, 36109, 9122,
+ 52302, 25313, 3979, 41252, 20168, 57447, 37916, 15027, 54623, 31728,
+ 5786, 47157, 22489, 63862, 48943, 4480, 65132, 20675, 15785, 37638,
+ 31978, 53829, 42558, 2193, 59261, 18898, 9400, 35351, 26107, 52052,
+ 59721, 18406, 43018, 1701, 27599, 50528, 10892, 33827, 61528, 24311,
+ 45339, 8116, 29406, 56433, 13213, 40242, 56171, 30148, 39464, 13447,
+ 23021, 63298, 6318, 46593, 49786, 27861, 33593, 11670, 16636, 61011,
+ 447, 44816, 17797, 60202, 1222, 43625, 50947, 27052, 34368, 10479,
+ 23700, 62011, 7639, 45944, 56850, 28861, 40785, 12798, 30631, 55560,
+ 14052, 38987, 62753, 23438, 46178, 6861, 28342, 49177, 12277, 33114,
+ 60464, 17055, 44403, 988, 8641, 36718, 24706, 52781, 41799, 3560,
+ 57860, 19627, 14544, 38527, 31123, 55100, 47702, 5369, 64277, 21946,
+ 5091, 48460, 21152, 64527, 37221, 16330, 53286, 32393, 2802, 42077,
+ 19377, 58654, 34932, 9947, 51511, 26520,
+ },
+ { 0, 55768, 44973, 30325, 17223, 39583, 60650, 13618, 34446, 24406,
+ 10531, 61691, 50633, 7185, 27236, 46012, 4353, 51417, 48812, 26484,
+ 21062, 35742, 65003, 9267, 38799, 20055, 14370, 57850, 54472, 3344,
+ 31589, 41661, 8706, 64474, 36271, 21623, 24901, 47261, 52968, 5936,
+ 42124, 32084, 2849, 54009, 59339, 15891, 18534, 37310, 13059, 60123,
+ 40110, 17782, 28740, 43420, 57321, 1585, 46477, 27733, 6688, 50168,
+ 63178, 12050, 22887, 32959, 17412, 40412, 60329, 12913, 1859, 56987,
+ 43246, 28982, 49802, 6994, 27943, 46335, 33229, 22549, 11872, 63416,
+ 21765, 36061, 64168, 9072, 5698, 53146, 47599, 24631, 54155, 2643,
+ 31782, 42494, 37068, 18708, 16225, 59065, 26118, 49118, 51627, 4211,
+ 9537, 64665, 35564, 21300, 57480, 14672, 20261, 38653, 41935, 31255,
+ 3170, 54714, 30471, 44767, 55466, 370, 13376, 60824, 39917, 16949,
+ 61833, 10321, 24100, 34812, 45774, 27414, 7523, 50363, 34824, 20944,
+ 10149, 65149, 52047, 4759, 25826, 48442, 3718, 55134, 41259, 30963,
+ 19905, 37913, 57964, 15284, 39177, 16593, 13988, 61308, 55886, 918,
+ 30179, 44091, 8071, 50783, 45098, 27122, 23744, 34072, 62317, 10933,
+ 43530, 29650, 1447, 56447, 59725, 12437, 18144, 40760, 11396, 62812,
+ 33577, 23281, 28611, 46619, 49262, 6582, 47883, 25299, 5286, 52606,
+ 63564, 8596, 22497, 36409, 15749, 58461, 37416, 19440, 32450, 42778,
+ 53615, 2231, 52236, 5588, 25505, 47737, 36683, 22163, 8422, 63806,
+ 19074, 37722, 58671, 15607, 2501, 53277, 42600, 32688, 56589, 1237,
+ 29344, 43896, 40522, 18322, 12775, 59455, 23427, 33371, 62510, 11766,
+ 6340, 49436, 46953, 28337, 60942, 14294, 16803, 39035, 44361, 29841,
+ 740, 56124, 26752, 45400, 50989, 7925, 11207, 61983, 33898, 23986,
+ 65295, 9943, 20642, 35194, 48200, 26000, 5093, 51773, 31105, 41049,
+ 54828, 4084, 15046, 58142, 38251, 19635,
+ },
+ { 0, 27757, 55514, 46263, 44457, 49604, 30067, 6430, 18255, 11042,
+ 40853, 62456, 60134, 34443, 12860, 24145, 36510, 58099, 22084, 14889,
+ 9015, 20314, 64493, 38784, 51665, 42428, 4363, 32102, 25720, 2069,
+ 48290, 53455, 289, 27980, 55803, 46486, 44168, 49381, 29778, 6207,
+ 18030, 10755, 40628, 62169, 60359, 34730, 13085, 24432, 36799, 58322,
+ 22373, 15112, 8726, 20091, 64204, 38561, 51440, 42141, 4138, 31815,
+ 25945, 2356, 48515, 53742, 578, 28207, 55960, 46837, 45035, 50054,
+ 30513, 7004, 17677, 10592, 40407, 61882, 59556, 33993, 12414, 23571,
+ 36060, 57521, 21510, 14443, 8565, 19736, 63919, 38338, 52115, 43006,
+ 4937, 32548, 26170, 2647, 48864, 53901, 867, 28430, 56249, 47060,
+ 44746, 49831, 30224, 6781, 17452, 10305, 40182, 61595, 59781, 34280,
+ 12639, 23858, 36349, 57744, 21799, 14666, 8276, 19513, 63630, 38115,
+ 51890, 42719, 4712, 32261, 26395, 2934, 49089, 54188, 1156, 26857,
+ 56414, 45107, 43309, 50496, 29175, 7578, 17355, 12198, 39697, 63356,
+ 61026, 33295, 14008, 23253, 35354, 58999, 21184, 16045, 10163, 19422,
+ 65385, 37636, 52565, 41272, 5519, 31202, 24828, 3217, 47142, 54347,
+ 1445, 27080, 56703, 45330, 43020, 50273, 28886, 7355, 17130, 11911,
+ 39472, 63069, 61251, 33582, 14233, 23540, 35643, 59222, 21473, 16268,
+ 9874, 19199, 65096, 37413, 52340, 40985, 5294, 30915, 25053, 3504,
+ 47367, 54634, 1734, 27307, 56860, 45681, 43887, 50946, 29621, 8152,
+ 16777, 11748, 39251, 62782, 60448, 32845, 13562, 22679, 34904, 58421,
+ 20610, 15599, 9713, 18844, 64811, 37190, 53015, 41850, 6093, 31648,
+ 25278, 3795, 47716, 54793, 2023, 27530, 57149, 45904, 43598, 50723,
+ 29332, 7929, 16552, 11461, 39026, 62495, 60673, 33132, 13787, 22966,
+ 35193, 58644, 20899, 15822, 9424, 18621, 64522, 36967, 52790, 41563,
+ 5868, 31361, 25503, 4082, 47941, 55080,
+ },
+ { 0, 47289, 28015, 54742, 56030, 25191, 47025, 3848, 43425, 4376,
+ 50382, 31863, 29567, 52166, 7696, 42665, 20319, 63462, 8752, 39561,
+ 38273, 11576, 63726, 16471, 59134, 24135, 35729, 13096, 15392, 33945,
+ 20815, 59894, 40638, 9735, 62417, 19304, 17504, 64729, 10511, 37302,
+ 14111, 36774, 23152, 58057, 60865, 21880, 32942, 14359, 53729, 26968,
+ 48270, 1079, 2879, 45958, 26192, 57065, 30784, 49401, 5423, 44438,
+ 41630, 6695, 53233, 30536, 8545, 39384, 19470, 62647, 64447, 17158,
+ 38608, 11881, 35008, 12409, 58799, 23830, 21022, 60071, 16241, 34760,
+ 28222, 54919, 849, 48104, 46304, 3161, 55695, 24886, 51103, 32550,
+ 43760, 4681, 7489, 42488, 28718, 51351, 49119, 1894, 53936, 27145,
+ 25857, 56760, 2158, 45271, 5758, 44743, 31505, 50088, 52384, 29721,
+ 41423, 6518, 61568, 18489, 40431, 9558, 10846, 37607, 18225, 65416,
+ 22817, 57752, 13390, 36087, 33791, 15174, 61072, 22057, 17090, 64123,
+ 12205, 38676, 38940, 8357, 62835, 19914, 60259, 21466, 34316, 16053,
+ 12733, 35076, 23762, 58475, 3485, 46372, 24818, 55371, 55107, 28666,
+ 47660, 661, 42044, 7301, 51539, 29162, 32482, 50779, 5005, 43828,
+ 56444, 25797, 45331, 2474, 1698, 48667, 27597, 54132, 30173, 52580,
+ 6322, 40971, 44803, 6074, 49772, 31445, 37667, 11162, 65100, 18165,
+ 18941, 61764, 9362, 39979, 14978, 33339, 22509, 61268, 57436, 22757,
+ 36147, 13706, 25507, 56090, 3788, 46709, 47485, 452, 54290, 27819,
+ 51714, 29371, 42861, 8148, 4316, 43109, 32179, 50442, 11516, 37957,
+ 16787, 63786, 63010, 20123, 39757, 9204, 34141, 15844, 59442, 20619,
+ 24451, 59194, 13036, 35413, 64797, 17828, 36978, 10443, 10179, 40826,
+ 19116, 61973, 21692, 60421, 14803, 33130, 36450, 14043, 58125, 23476,
+ 45634, 2811, 57133, 26516, 26780, 53285, 1523, 48458, 7139, 41818,
+ 30348, 52789, 49469, 31108, 44114, 5355,
+ },
+ { 0, 53971, 47547, 27496, 28523, 48568, 54992, 1027, 57046, 3077,
+ 26477, 46526, 45501, 25454, 2054, 56021, 41393, 29538, 6154, 51929,
+ 52954, 7177, 30561, 42418, 32615, 44468, 50908, 5135, 4108, 49887,
+ 43447, 31588, 24447, 36268, 59076, 13335, 12308, 58055, 35247, 23420,
+ 33193, 21370, 14354, 60097, 61122, 15377, 22393, 34218, 65230, 11293,
+ 18293, 38310, 37285, 17270, 10270, 64205, 8216, 62155, 39331, 19312,
+ 20339, 40352, 63176, 9243, 48894, 27693, 1861, 54678, 53653, 838,
+ 26670, 47869, 24616, 45819, 55699, 2880, 3907, 56720, 46840, 25643,
+ 8015, 52636, 42740, 29735, 28708, 41719, 51615, 6988, 49561, 4938,
+ 30754, 43761, 44786, 31777, 5961, 50586, 57729, 13138, 22586, 35561,
+ 36586, 23609, 14161, 58754, 16215, 60804, 34540, 21567, 20540, 33519,
+ 59783, 15188, 16432, 37603, 63883, 11096, 12123, 64904, 38624, 17459,
+ 40678, 19509, 10077, 62862, 61837, 9054, 18486, 39653, 25057, 45874,
+ 55386, 2697, 3722, 56409, 46897, 26082, 48951, 28132, 1676, 54367,
+ 53340, 655, 27111, 47924, 49232, 4739, 31211, 43832, 44859, 32232,
+ 5760, 50259, 7814, 52309, 42813, 30190, 29165, 41790, 51286, 6789,
+ 16030, 60493, 34597, 22006, 20981, 33574, 59470, 15005, 57416, 12955,
+ 23027, 35616, 36643, 24048, 13976, 58443, 40751, 19964, 9876, 62535,
+ 61508, 8855, 18943, 39724, 16889, 37674, 63554, 10897, 11922, 64577,
+ 38697, 17914, 57119, 3532, 26276, 46199, 45172, 25255, 2511, 56092,
+ 457, 54042, 47218, 27297, 28322, 48241, 55065, 1482, 32430, 44157,
+ 50965, 5574, 4549, 49942, 43134, 31405, 41080, 29355, 6595, 51984,
+ 53011, 7616, 30376, 42107, 32864, 21171, 14811, 60168, 61195, 15832,
+ 22192, 33891, 24246, 35941, 59149, 13790, 12765, 58126, 34918, 23221,
+ 8657, 62210, 39018, 19129, 20154, 40041, 63233, 9682, 65287, 11732,
+ 18108, 37999, 36972, 17087, 10711, 64260,
+ },
+ { 0, 59366, 54225, 13367, 48063, 23641, 26734, 36744, 27491, 35973,
+ 47282, 24404, 53468, 14138, 781, 58603, 54982, 12576, 1303, 58097,
+ 28025, 35487, 48808, 22862, 48549, 23107, 28276, 35218, 1562, 57852,
+ 54731, 12845, 45457, 22135, 25152, 34214, 2606, 60872, 55807, 15897,
+ 56050, 15636, 2339, 61125, 24909, 34475, 45724, 21882, 26455, 32945,
+ 46214, 21344, 56552, 15118, 3897, 59615, 3124, 60370, 57317, 14339,
+ 46987, 20589, 25690, 33724, 32575, 39129, 44270, 19208, 50304, 9062,
+ 5969, 61623, 5212, 62394, 51085, 8299, 45027, 18437, 31794, 39892,
+ 43513, 19999, 31272, 40398, 4678, 62880, 49559, 9841, 49818, 9596,
+ 4427, 63149, 31013, 40643, 43764, 19730, 52910, 10568, 7551, 64153,
+ 29969, 37623, 42688, 16678, 42445, 16939, 30236, 37370, 7794, 63892,
+ 52643, 10821, 6248, 65422, 52153, 11359, 41943, 17457, 28678, 38880,
+ 29451, 38125, 41178, 18236, 51380, 12114, 7013, 64643, 65150, 6552,
+ 11695, 51785, 17857, 41511, 38416, 29174, 38173, 29435, 18124, 41258,
+ 11938, 51524, 64883, 6805, 10424, 53086, 64361, 7311, 37639, 29921,
+ 16598, 42800, 17371, 42045, 36874, 30700, 63588, 8066, 11189, 52307,
+ 20463, 43017, 39998, 31704, 62544, 5046, 10113, 49255, 9356, 50026,
+ 63325, 4283, 40755, 30933, 19682, 43780, 39209, 32463, 19192, 44318,
+ 8854, 50544, 61767, 5793, 62026, 5548, 8603, 50813, 18933, 44563,
+ 39460, 32194, 33089, 26279, 21136, 46454, 15102, 56600, 59695, 3785,
+ 59938, 3524, 14835, 56853, 20893, 46715, 33356, 26026, 22407, 45153,
+ 33878, 25520, 60472, 3038, 16361, 55311, 15588, 56066, 61237, 2259,
+ 34651, 24765, 21642, 45932, 12496, 55094, 58113, 1255, 35695, 27785,
+ 22718, 48984, 23475, 48213, 34914, 28548, 57356, 2026, 13277, 54331,
+ 58902, 496, 13767, 53793, 23977, 47695, 36472, 27038, 36213, 27283,
+ 24228, 47426, 14026, 53548, 58651, 765,
+ },
+ { 0, 29554, 59108, 38294, 53717, 41639, 14129, 17475, 49079, 52421,
+ 22867, 10785, 28258, 7440, 34950, 64500, 25459, 4097, 34199, 63205,
+ 45734, 49620, 21570, 10032, 56516, 44982, 14880, 18770, 3345, 32355,
+ 60405, 39047, 50918, 46484, 8194, 21360, 5939, 25665, 61911, 33445,
+ 31057, 2595, 40885, 60615, 43140, 56310, 20064, 15634, 42389, 55015,
+ 17265, 12291, 29760, 1842, 37540, 57814, 6690, 26960, 64710, 36788,
+ 52215, 47237, 11539, 24161, 37329, 58019, 30517, 1095, 16388, 13174,
+ 42720, 54674, 11878, 23828, 51330, 48112, 65459, 36033, 6487, 27173,
+ 62114, 33232, 5190, 26420, 9079, 20485, 50579, 46817, 19733, 15975,
+ 44017, 55427, 40128, 61362, 31268, 2390, 22327, 9285, 45523, 49825,
+ 34530, 62864, 24582, 4980, 59520, 39922, 3684, 32022, 14677, 18983,
+ 57265, 44227, 13380, 18230, 53920, 41426, 58769, 38627, 885, 28679,
+ 35827, 63617, 27927, 7781, 23078, 10580, 48322, 53168, 16319, 19661,
+ 55643, 43561, 61034, 40216, 2190, 31740, 32776, 62330, 26348, 5534,
+ 20957, 8879, 46905, 50251, 23756, 12222, 47656, 51546, 36121, 65131,
+ 27645, 6287, 58235, 36873, 1439, 30445, 12974, 16860, 54346, 42808,
+ 63833, 35371, 8125, 27855, 10380, 23550, 52840, 48410, 18158, 13724,
+ 40970, 54136, 38715, 58441, 29151, 685, 39466, 59736, 31950, 4028,
+ 19455, 14477, 44315, 56937, 9629, 22255, 50041, 45067, 62536, 34618,
+ 4780, 25054, 44654, 56604, 18570, 15352, 32699, 3273, 39263, 59949,
+ 4569, 25259, 63293, 33871, 49164, 45950, 9960, 21914, 52509, 48751,
+ 11257, 22667, 7368, 28602, 64044, 35166, 29354, 472, 37966, 59196,
+ 41855, 53261, 17819, 14057, 26760, 7162, 36460, 64798, 47453, 51759,
+ 24505, 11467, 55103, 42061, 12763, 17065, 1770, 30104, 57358, 37756,
+ 3067, 30857, 60703, 40557, 55854, 43356, 15562, 20408, 46156, 51006,
+ 21160, 8666, 26009, 5867, 33661, 61455,
+ },
+ { 0, 14648, 29296, 19272, 58592, 56792, 38544, 44968, 54749, 60645,
+ 42925, 40597, 12605, 2053, 17229, 31349, 47015, 36511, 50647, 64751,
+ 21319, 27263, 8503, 6159, 25210, 23362, 4106, 10546, 34458, 49058,
+ 62698, 52690, 29523, 19051, 291, 14363, 38835, 44683, 58819, 56571,
+ 42638, 40886, 54526, 60870, 17006, 31574, 12318, 2342, 50420, 64972,
+ 46724, 36796, 8212, 6444, 21092, 27484, 4393, 10257, 25433, 23137,
+ 62921, 52465, 34745, 48769, 59046, 57246, 38102, 44526, 582, 15230,
+ 28726, 18702, 13179, 2627, 16651, 30771, 55195, 61091, 42475, 40147,
+ 20737, 26681, 9073, 6729, 46561, 36057, 51089, 65193, 34012, 48612,
+ 63148, 53140, 24636, 22788, 4684, 11124, 38389, 44237, 59269, 57021,
+ 28949, 18477, 869, 14941, 16424, 30992, 12888, 2912, 42184, 40432,
+ 54968, 61312, 8786, 7018, 20514, 26906, 50866, 65418, 46274, 36346,
+ 63375, 52919, 34303, 48327, 4975, 10839, 24863, 22567, 53585, 59497,
+ 41761, 39449, 13745, 3209, 18369, 32505, 1164, 15796, 30460, 20420,
+ 57452, 55636, 37404, 43812, 26358, 24526, 5254, 11710, 33302, 47918,
+ 61542, 51550, 45867, 35347, 49499, 63587, 22475, 28403, 9659, 7299,
+ 41474, 39738, 53362, 59722, 18146, 32730, 13458, 3498, 30687, 20199,
+ 1455, 15511, 37695, 43527, 57679, 55415, 5541, 11421, 26581, 24301,
+ 61765, 51325, 33589, 47629, 49272, 63808, 45576, 35632, 9368, 7584,
+ 22248, 28624, 14327, 3791, 17799, 31935, 54039, 59951, 41319, 39007,
+ 57898, 56082, 36954, 43362, 1738, 16370, 29882, 19842, 32848, 47464,
+ 61984, 51992, 25776, 23944, 5824, 12280, 21901, 27829, 10237, 7877,
+ 45421, 34901, 49949, 64037, 17572, 32156, 14036, 4076, 41028, 39292,
+ 53812, 60172, 37241, 43073, 58121, 55857, 30105, 19617, 2025, 16081,
+ 62211, 51771, 33139, 47179, 6115, 11995, 26003, 23723, 9950, 8166,
+ 21678, 28054, 49726, 64262, 45134, 35190,
+ },
+ { 0, 7197, 14394, 9255, 28788, 27753, 18510, 21587, 57576, 64757,
+ 55506, 50383, 37020, 35969, 43174, 46267, 56781, 49616, 58871, 63978,
+ 44473, 45476, 38275, 35230, 15653, 8504, 1311, 6402, 19793, 20812,
+ 30059, 26998, 42887, 48026, 40893, 33696, 55283, 52206, 61385, 62420,
+ 18287, 23410, 32597, 25416, 14107, 11014, 3873, 4924, 31306, 26199,
+ 17008, 24173, 2622, 5667, 12804, 11801, 39586, 34495, 41624, 48773,
+ 60118, 63179, 53996, 52977, 21267, 20238, 27433, 30516, 9063, 16250,
+ 7005, 1856, 46075, 45030, 35777, 38876, 50063, 57234, 64437, 59304,
+ 36574, 37571, 46820, 43769, 65194, 58039, 50832, 55949, 28214, 29227,
+ 22028, 18961, 7746, 607, 9848, 14949, 62612, 59529, 52398, 53427,
+ 34016, 39165, 48346, 41159, 5244, 2145, 11334, 12379, 25608, 30741,
+ 23602, 16431, 10585, 13636, 4451, 3454, 22829, 17712, 24855, 32010,
+ 51633, 54700, 61835, 60822, 47557, 42456, 33279, 40418, 42534, 47675,
+ 40476, 33281, 54866, 51791, 61032, 62069, 18126, 23251, 32500, 25321,
+ 14010, 10919, 3712, 4765, 31723, 26614, 17361, 24524, 2975, 6018,
+ 13221, 12216, 39683, 34590, 41785, 48932, 60279, 63338, 54093, 53072,
+ 417, 7612, 14747, 9606, 29141, 28104, 18927, 22002, 57673, 64852,
+ 55667, 50542, 37181, 36128, 43271, 46362, 56428, 49265, 58454, 63563,
+ 44056, 45061, 37922, 34879, 15492, 8345, 1214, 6307, 19696, 20717,
+ 29898, 26839, 62773, 59688, 52495, 53522, 34113, 39260, 48507, 41318,
+ 5597, 2496, 11751, 12794, 26025, 31156, 23955, 16782, 10488, 13541,
+ 4290, 3295, 22668, 17553, 24758, 31915, 51216, 54285, 61482, 60471,
+ 47204, 42105, 32862, 40003, 21170, 20143, 27272, 30357, 8902, 16091,
+ 6908, 1761, 45658, 44615, 35424, 38525, 49710, 56883, 64020, 58889,
+ 36735, 37730, 46917, 43864, 65291, 58134, 50993, 56108, 28567, 29578,
+ 22445, 19376, 8163, 1022, 10201, 15300,
+ },
+ { 0, 32897, 7455, 40350, 14910, 47807, 10017, 42912, 29820, 62717,
+ 26979, 59874, 20034, 52931, 21341, 54236, 59640, 26745, 62951, 30054,
+ 53958, 21063, 53209, 20312, 40068, 7173, 33179, 282, 42682, 9787,
+ 48037, 15140, 52717, 19820, 53490, 20595, 63443, 30546, 60108, 27213,
+ 47505, 14608, 42126, 9231, 33711, 814, 40624, 7729, 9493, 42388,
+ 14346, 47243, 7979, 40874, 564, 33461, 20841, 53736, 19574, 52471,
+ 27479, 60374, 30280, 63177, 34759, 1862, 39640, 6745, 48633, 15736,
+ 41190, 8295, 62395, 29498, 61092, 28197, 51589, 18692, 54426, 21531,
+ 28479, 61374, 29216, 62113, 21761, 54656, 18462, 51359, 6979, 39874,
+ 1628, 34525, 8573, 41468, 15458, 48355, 18986, 51883, 22325, 55220,
+ 28692, 61589, 27915, 60810, 15958, 48855, 9033, 41928, 1128, 34025,
+ 6519, 39414, 41682, 8787, 49101, 16204, 39148, 6253, 34291, 1394,
+ 54958, 22063, 52145, 19248, 60560, 27665, 61839, 28942, 5011, 37650,
+ 3724, 36365, 10669, 43308, 13490, 46131, 26607, 59246, 31472, 64113,
+ 24017, 56656, 16590, 49231, 64363, 31722, 58996, 26357, 49493, 16852,
+ 56394, 23755, 36631, 3990, 37384, 4745, 46377, 13736, 43062, 10423,
+ 56958, 24319, 50017, 17376, 58432, 25793, 63839, 31198, 43522, 10883,
+ 46877, 14236, 36924, 4285, 36131, 3490, 13958, 46599, 11161, 43800,
+ 3256, 35897, 4519, 37158, 17146, 49787, 24549, 57188, 30916, 63557,
+ 26075, 58714, 37972, 5333, 35147, 2506, 44650, 12011, 45941, 13300,
+ 57384, 24745, 64823, 32182, 55830, 23191, 50953, 18312, 31916, 64557,
+ 25011, 57650, 18066, 50707, 23437, 56076, 2256, 34897, 5583, 38222,
+ 13038, 45679, 12273, 44912, 22969, 55608, 17574, 50215, 25479, 58118,
+ 32408, 65049, 11717, 44356, 12506, 45147, 6139, 38778, 2788, 35429,
+ 45377, 12736, 44126, 11487, 35711, 3070, 38496, 5857, 50493, 17852,
+ 55330, 22691, 65283, 32642, 57884, 25245,
+ },
+ { 0, 52943, 33155, 20300, 7963, 53716, 40600, 20567, 15926, 61689,
+ 49077, 29050, 8493, 61410, 41134, 28257, 31852, 45731, 65007, 13088,
+ 25463, 44472, 58100, 11323, 16986, 35989, 50137, 3350, 23873, 37774,
+ 56514, 4621, 63704, 13847, 31067, 46996, 59331, 10508, 26176, 43151,
+ 50926, 2081, 18285, 35234, 55797, 5946, 22646, 38585, 33972, 19067,
+ 1335, 52216, 39855, 21856, 6700, 54499, 47746, 29773, 15105, 62926,
+ 42393, 27478, 9242, 60117, 60845, 9058, 27694, 41697, 62134, 15481,
+ 29493, 48634, 54171, 7508, 21016, 40151, 52352, 591, 19715, 33740,
+ 37313, 24334, 4162, 56973, 36570, 16405, 3929, 49558, 45047, 24888,
+ 11892, 57531, 45292, 32291, 12655, 65440, 5493, 56250, 38134, 23097,
+ 2670, 50337, 35821, 17698, 11075, 58764, 43712, 25615, 13400, 64151,
+ 46555, 31508, 26905, 42966, 59546, 9813, 30210, 47309, 63361, 14670,
+ 22319, 39392, 54956, 6243, 18484, 34555, 51639, 1912, 51015, 2440,
+ 18116, 34827, 55388, 5779, 23007, 38672, 63857, 14270, 30962, 46653,
+ 58986, 10405, 26601, 43302, 47915, 30180, 15016, 62567, 42032, 27391,
+ 9651, 60284, 34077, 19410, 1182, 51793, 39430, 21705, 7045, 54602,
+ 16287, 61776, 48668, 28883, 8324, 61003, 41223, 28616, 425, 53094,
+ 32810, 20197, 7858, 53373, 40753, 20990, 17395, 36156, 49776, 3263,
+ 23784, 37415, 56683, 5028, 32197, 45834, 64582, 12937, 25310, 44049,
+ 58205, 11666, 10986, 58405, 43881, 26022, 13809, 64318, 46194, 31421,
+ 5340, 55827, 38239, 23440, 3015, 50440, 35396, 17547, 22150, 38985,
+ 55045, 6602, 18845, 34642, 51230, 1745, 26800, 42623, 59699, 10236,
+ 30635, 47460, 63016, 14567, 53810, 7421, 21425, 40318, 52521, 998,
+ 19626, 33381, 60420, 8907, 28039, 41800, 62239, 15824, 29340, 48211,
+ 44638, 24721, 12253, 57618, 45381, 32650, 12486, 65033, 36968, 24231,
+ 4587, 57124, 36723, 16828, 3824, 49215,
+ },
+ { 0, 59880, 53197, 9765, 33671, 27247, 19530, 42402, 6931, 62203,
+ 54494, 15670, 39060, 29052, 22361, 48817, 13862, 57294, 63979, 4099,
+ 46497, 23625, 31340, 37764, 11573, 50397, 58104, 2832, 44722, 18266,
+ 24959, 34967, 27724, 34212, 41857, 19049, 61387, 1571, 8198, 51694,
+ 30559, 40631, 47250, 20858, 62680, 7472, 15125, 54013, 23146, 45954,
+ 38311, 31823, 55789, 12293, 5664, 65480, 16761, 43153, 36532, 26460,
+ 49918, 11030, 3379, 58587, 55448, 12656, 5973, 65213, 23327, 45815,
+ 38098, 32058, 50059, 10851, 3142, 58798, 16396, 43492, 36801, 26153,
+ 61118, 1878, 8563, 51355, 27961, 34001, 41716, 19228, 62893, 7237,
+ 14944, 54152, 30250, 40898, 47591, 20495, 46292, 23868, 31513, 37617,
+ 14163, 57019, 63646, 4470, 44999, 17967, 24586, 35298, 11328, 50600,
+ 58253, 2661, 33522, 27418, 19775, 42199, 373, 59549, 52920, 10064,
+ 39393, 28681, 22060, 49092, 6758, 62350, 54699, 15427, 44333, 17605,
+ 25312, 35592, 11946, 51010, 57703, 2191, 46654, 24534, 31219, 36891,
+ 13753, 56401, 64116, 5020, 39691, 29411, 21702, 48430, 6284, 61796,
+ 55105, 16041, 32792, 27120, 20437, 42557, 927, 60023, 52306, 9658,
+ 49505, 10377, 3756, 59204, 17126, 43790, 36139, 25795, 55922, 13210,
+ 5567, 64599, 23029, 45085, 38456, 32720, 63303, 7855, 14474, 53602,
+ 29888, 40232, 47885, 21221, 60500, 1468, 9113, 51825, 28627, 34363,
+ 40990, 18934, 30133, 40029, 47736, 21392, 63026, 8154, 14847, 53271,
+ 28326, 34638, 41323, 18563, 60705, 1225, 8940, 51972, 17299, 43643,
+ 35934, 26038, 49172, 10748, 4057, 58929, 22656, 45416, 38733, 32421,
+ 56071, 13039, 5322, 64802, 6649, 61457, 54836, 16348, 39550, 29590,
+ 21939, 48219, 746, 60162, 52519, 9423, 33133, 26757, 20128, 42824,
+ 12255, 50743, 57362, 2554, 44120, 17840, 25493, 35453, 13516, 56612,
+ 64257, 4841, 46923, 24227, 30854, 37230,
+ },
+ { 0, 29813, 59626, 40095, 52681, 47548, 9507, 20822, 34703, 62458,
+ 28517, 6928, 19014, 15923, 41644, 55001, 4867, 26486, 64489, 36764,
+ 57034, 43711, 13856, 16981, 38028, 57593, 31846, 2067, 22853, 11568,
+ 45487, 50650, 9734, 21107, 52972, 47769, 60367, 40890, 805, 30544,
+ 41353, 54780, 18787, 15638, 27712, 6197, 33962, 61663, 13573, 16752,
+ 56815, 43418, 63692, 36025, 4134, 25683, 45706, 50943, 23136, 11797,
+ 32579, 2870, 38825, 58332, 19468, 14457, 42214, 53395, 33221, 62896,
+ 26927, 7514, 52099, 49142, 9065, 22300, 1610, 29247, 61088, 39637,
+ 24335, 11130, 47077, 50064, 37574, 59059, 31276, 3673, 55424, 44277,
+ 12394, 17439, 5449, 24892, 64931, 35286, 27146, 7807, 33504, 63125,
+ 42947, 54198, 20265, 15196, 60805, 39408, 1391, 28954, 8268, 21561,
+ 51366, 48339, 30985, 3452, 37347, 58774, 46272, 49333, 23594, 10335,
+ 65158, 35571, 5740, 25113, 13135, 18234, 56229, 45008, 38936, 60525,
+ 28914, 1159, 21969, 8612, 48443, 51534, 8087, 27618, 63357, 33544,
+ 53854, 42539, 15028, 20161, 35611, 65390, 25585, 6020, 18130, 12967,
+ 44600, 55885, 3220, 30945, 58494, 36875, 49501, 46376, 10679, 24002,
+ 48670, 51819, 22260, 8833, 29655, 1954, 39741, 61256, 14737, 19940,
+ 53627, 42254, 62552, 32813, 7346, 26823, 44317, 55656, 17911, 12674,
+ 24788, 5281, 34878, 64587, 10898, 24295, 49784, 46605, 59227, 37678,
+ 4017, 31684, 54292, 41057, 15614, 18571, 6621, 28072, 61751, 34114,
+ 21403, 10222, 47985, 52996, 40530, 59943, 30392, 717, 50967, 45922,
+ 12285, 23432, 2782, 32427, 57908, 38465, 16536, 13549, 43122, 56327,
+ 36177, 63780, 26043, 4558, 61970, 34407, 6904, 28301, 16347, 19374,
+ 55089, 41796, 30109, 488, 40311, 59650, 47188, 52257, 20670, 9419,
+ 57617, 38244, 2555, 32142, 11480, 22701, 50226, 45127, 26270, 4843,
+ 36468, 64001, 43863, 57122, 17341, 14280,
+ },
+ { 0, 46261, 30071, 49602, 60142, 24155, 40857, 11052, 51649, 32116,
+ 48310, 2051, 9007, 38810, 22104, 58093, 36767, 15146, 64232, 20061,
+ 25969, 53700, 4102, 42163, 18014, 62187, 13097, 34716, 44208, 6149,
+ 55751, 28018, 803, 46998, 30292, 49889, 59853, 23928, 40122, 10255,
+ 51938, 32343, 49045, 2848, 8204, 38073, 21883, 57806, 36028, 14345,
+ 63947, 19838, 26194, 53991, 4901, 42896, 17789, 61896, 12298, 33983,
+ 44947, 6950, 56036, 28241, 1606, 45811, 29489, 51076, 60584, 22557,
+ 39391, 11626, 53127, 31538, 47856, 3653, 9577, 37340, 20510, 58539,
+ 35289, 15724, 64686, 18459, 25399, 55170, 5696, 41717, 16408, 62637,
+ 13679, 33242, 43766, 7747, 57217, 27444, 1381, 45520, 28690, 50343,
+ 61323, 23358, 39676, 11849, 52388, 30737, 47571, 3430, 9802, 37631,
+ 21309, 59272, 35578, 15951, 65421, 19256, 24596, 54433, 5475, 41430,
+ 17211, 63374, 13900, 33529, 43477, 7520, 56482, 26647, 3212, 47161,
+ 31227, 52558, 58978, 21207, 37653, 10144, 50509, 29176, 45114, 1167,
+ 12195, 39702, 23252, 61025, 33555, 14246, 63076, 17105, 27133, 56648,
+ 7306, 43071, 19154, 65127, 16293, 35600, 41020, 5257, 54603, 25086,
+ 4015, 47898, 31448, 52845, 58689, 20980, 36918, 9347, 50798, 29403,
+ 45849, 1964, 11392, 38965, 23031, 60738, 32816, 13445, 62791, 16882,
+ 27358, 56939, 8105, 43804, 18929, 64836, 15494, 34867, 41759, 6058,
+ 54888, 25309, 2762, 48767, 32701, 51976, 57380, 21649, 38227, 8678,
+ 49931, 30654, 46716, 713, 10725, 40272, 23698, 59431, 34133, 12768,
+ 61474, 17559, 28603, 56078, 6860, 44665, 19604, 63521, 14819, 36182,
+ 42618, 4815, 54029, 26552, 2537, 48476, 31902, 51243, 58119, 22450,
+ 38512, 8901, 49192, 29853, 46431, 490, 10950, 40563, 24497, 60164,
+ 34422, 12995, 62209, 18356, 27800, 55341, 6639, 44378, 20407, 64258,
+ 15040, 36469, 42329, 4588, 53294, 25755,
+ },
+ { 0, 54485, 46519, 24930, 30579, 41894, 49860, 5649, 61158, 14899,
+ 23377, 36740, 39317, 19776, 11298, 63735, 49617, 5380, 29798, 41139,
+ 46754, 25207, 789, 55232, 12087, 64482, 39552, 20053, 22596, 35985,
+ 60915, 14630, 40895, 19306, 10760, 65245, 59596, 15385, 23931, 35246,
+ 29017, 42380, 50414, 4155, 1578, 54015, 45981, 26440, 24174, 35515,
+ 60377, 16140, 10525, 64968, 40106, 18559, 45192, 25693, 1343, 53738,
+ 51195, 4910, 29260, 42649, 9059, 63414, 38612, 16897, 21520, 32965,
+ 57767, 13682, 52613, 6480, 30770, 44263, 47862, 28195, 3905, 56212,
+ 58034, 13927, 22277, 33744, 38337, 16660, 8310, 62627, 3156, 55425,
+ 47587, 27958, 31527, 45042, 52880, 6725, 48348, 26633, 2411, 56766,
+ 52143, 8058, 32280, 43725, 21050, 34543, 59277, 13144, 9545, 61852,
+ 37118, 17451, 32013, 43480, 51386, 7279, 2686, 57003, 49097, 27420,
+ 37867, 18238, 9820, 62089, 58520, 12365, 20783, 34298, 18118, 37395,
+ 62321, 10148, 12725, 58720, 33794, 20695, 43040, 31989, 7575, 51522,
+ 57171, 2950, 27364, 48689, 34583, 21442, 12960, 58997, 61540, 9393,
+ 17875, 37126, 27121, 48420, 56390, 2195, 7810, 51799, 43829, 32736,
+ 55673, 3500, 27854, 47131, 44554, 31455, 7101, 53096, 14239, 58186,
+ 33320, 22269, 16620, 37945, 62811, 8590, 6312, 52349, 44319, 31178,
+ 28635, 47886, 55916, 3769, 63054, 8859, 17401, 38700, 33085, 21992,
+ 13450, 57439, 26021, 45424, 53266, 1223, 4822, 50691, 42849, 29620,
+ 35651, 24470, 16116, 59937, 64560, 10469, 18823, 40274, 42100, 28833,
+ 4547, 50454, 54023, 2002, 26288, 45669, 19090, 40519, 65317, 11248,
+ 15841, 59700, 34902, 23683, 64026, 11983, 20397, 39800, 36201, 22972,
+ 14558, 60427, 5372, 49193, 41291, 30110, 25487, 46938, 54840, 749,
+ 15307, 61214, 36476, 23209, 19640, 39021, 63759, 11738, 54573, 504,
+ 24730, 46159, 41566, 30347, 6121, 49980,
+ },
+ { 0, 58597, 54743, 12594, 47027, 21334, 25188, 34433, 29563, 38814,
+ 42668, 16969, 50376, 8237, 4383, 62970, 59126, 531, 13089, 55236,
+ 20805, 46496, 33938, 24695, 38285, 29032, 16474, 42175, 8766, 50907,
+ 63465, 4876, 53745, 13588, 1062, 57539, 26178, 33447, 45973, 22384,
+ 41610, 18031, 30557, 37816, 5433, 61916, 49390, 9227, 14087, 54242,
+ 58064, 1589, 32948, 25681, 21859, 45446, 17532, 41113, 37291, 30030,
+ 62415, 5930, 9752, 49917, 49151, 23322, 27176, 36557, 2124, 60585,
+ 56731, 14718, 52356, 10337, 6483, 64950, 31543, 40914, 44768, 18949,
+ 22793, 48620, 36062, 26683, 61114, 2655, 15213, 57224, 10866, 52887,
+ 65445, 6976, 40385, 31012, 18454, 44275, 28174, 35563, 48089, 24380,
+ 55741, 15704, 3178, 59535, 7541, 63888, 51362, 11335, 43718, 20003,
+ 32529, 39924, 35064, 27677, 23855, 47562, 16203, 56238, 60060, 3705,
+ 64387, 8038, 11860, 51889, 19504, 43221, 39399, 32002, 25571, 34566,
+ 46644, 21201, 54352, 12469, 391, 58722, 4248, 62589, 50511, 8618,
+ 42795, 17358, 29436, 38425, 34069, 25072, 20674, 46119, 12966, 54851,
+ 59249, 916, 63086, 4747, 9145, 51036, 16861, 42296, 37898, 28911,
+ 45586, 22263, 26565, 33568, 1441, 57668, 53366, 13459, 49513, 9612,
+ 5310, 61531, 30426, 37439, 41741, 18408, 21732, 45057, 33075, 26070,
+ 58199, 1970, 13952, 53861, 10143, 50042, 62024, 5805, 36908, 29897,
+ 17915, 41246, 56348, 14585, 2507, 60718, 27567, 36682, 48760, 23197,
+ 44903, 19330, 31408, 40533, 6356, 64561, 52483, 10726, 15082, 56847,
+ 61245, 3032, 36185, 27068, 22670, 48235, 18833, 44404, 40006, 30883,
+ 65058, 6855, 11253, 53008, 3565, 59656, 55354, 15583, 47710, 24251,
+ 28553, 35692, 32406, 39539, 43841, 20388, 51493, 11712, 7410, 63511,
+ 60187, 4094, 16076, 55849, 23720, 47181, 35199, 28058, 39008, 31877,
+ 19895, 43346, 12243, 52022, 64004, 7905,
+ },
+ { 0, 64765, 58855, 6426, 55251, 11054, 12852, 52937, 46011, 20294,
+ 22108, 43681, 25704, 39061, 33167, 32114, 31595, 34710, 40588, 25201,
+ 44216, 20549, 18783, 46498, 51408, 13357, 11575, 53706, 7939, 58366,
+ 64228, 1561, 63190, 2603, 4913, 61388, 8453, 56824, 50402, 14367,
+ 17773, 47504, 41098, 23671, 37566, 28227, 30553, 35748, 36285, 28992,
+ 26714, 38055, 23150, 42643, 49033, 17268, 15878, 49915, 56289, 10012,
+ 59861, 5416, 3122, 61647, 61873, 3404, 5206, 59563, 9826, 55967,
+ 50053, 16248, 16906, 48887, 42989, 23312, 38361, 26916, 28734, 36035,
+ 35546, 30247, 28477, 37824, 23817, 41460, 47342, 17427, 14689, 50588,
+ 56454, 8315, 61106, 4687, 2901, 63400, 1895, 64410, 57984, 7805,
+ 53428, 11337, 13651, 51630, 46300, 18465, 20795, 44486, 25359, 40946,
+ 34536, 31253, 31756, 33009, 39403, 25878, 43999, 22306, 20024, 45765,
+ 53175, 13130, 10832, 54957, 6244, 58521, 64899, 382, 65407, 898,
+ 6808, 58981, 10412, 54353, 52555, 12726, 19652, 45113, 43299, 21982,
+ 39703, 26602, 32496, 33293, 33812, 30953, 25075, 40206, 21447, 44858,
+ 46624, 19165, 14255, 52050, 53832, 11957, 57468, 7297, 1435, 63846,
+ 2473, 62804, 60494, 4275, 56954, 8839, 15261, 51040, 47634, 18159,
+ 24565, 41736, 28097, 37180, 34854, 29915, 29378, 36415, 38693, 27608,
+ 42257, 23020, 16630, 48139, 49529, 15748, 9374, 55395, 5802, 59991,
+ 62285, 4016, 3790, 62003, 60201, 6100, 55581, 9696, 15610, 49159,
+ 48501, 16776, 22674, 42095, 27302, 38491, 36673, 29628, 30117, 35160,
+ 36930, 27839, 41590, 24203, 18321, 47980, 50718, 15075, 9209, 57092,
+ 4557, 60720, 62506, 2263, 63512, 1253, 7679, 57602, 12235, 54070,
+ 51756, 14033, 19363, 46942, 44612, 21177, 40048, 24717, 31127, 34154,
+ 33651, 32654, 26260, 39529, 21664, 43101, 45383, 19898, 12488, 52277,
+ 54575, 10706, 59163, 7142, 764, 65025,
+ },
+ { 0, 61681, 65023, 3342, 59363, 5906, 6684, 60141, 54235, 9002,
+ 11812, 57045, 13368, 50377, 51655, 14646, 48043, 19290, 18004, 46757,
+ 23624, 44217, 41399, 20806, 26736, 39041, 38287, 25982, 36755, 32610,
+ 29292, 33437, 27467, 39866, 38580, 26181, 36008, 31833, 29015, 33190,
+ 47248, 18529, 17775, 46494, 24435, 44930, 41612, 21117, 53472, 8209,
+ 11551, 56814, 14083, 51186, 51964, 14861, 827, 62410, 65220, 3637,
+ 58584, 5161, 6439, 59862, 54934, 9831, 11113, 56216, 12661, 49540,
+ 52362, 15483, 1357, 62908, 63666, 2115, 58030, 4703, 8017, 61344,
+ 27965, 40396, 37058, 24627, 35550, 31279, 30497, 34768, 48870, 19991,
+ 17177, 46056, 22789, 43508, 42234, 21515, 48605, 19756, 16418, 45267,
+ 23102, 43727, 42945, 22320, 28166, 40695, 37881, 25352, 35301, 30996,
+ 29722, 34027, 1654, 63111, 64393, 2936, 57749, 4452, 7274, 60571,
+ 54701, 9564, 10322, 55459, 12878, 49855, 53169, 16192, 45361, 16832,
+ 19662, 48191, 22226, 42531, 43821, 23516, 25322, 37403, 40725, 28644,
+ 34057, 30200, 30966, 34823, 2714, 64107, 63333, 1940, 60793, 7560,
+ 4230, 57463, 55617, 10672, 9406, 54351, 16034, 52819, 50013, 13228,
+ 55930, 10891, 10117, 55156, 15769, 52584, 49254, 12439, 2465, 63824,
+ 62558, 1199, 60994, 7859, 5053, 58188, 25041, 37152, 39982, 27871,
+ 34354, 30403, 31693, 35644, 45578, 17147, 20469, 48900, 21993, 42264,
+ 43030, 22759, 26535, 38742, 39512, 27305, 32836, 28853, 32187, 36170,
+ 46204, 17549, 18819, 47474, 21407, 41838, 44640, 24209, 56332, 11517,
+ 8691, 53506, 15343, 51998, 50704, 14049, 4055, 65318, 61992, 729,
+ 59444, 6341, 5579, 58682, 3308, 64541, 61715, 482, 60175, 7166,
+ 5872, 58881, 57143, 12230, 8904, 53817, 14548, 51237, 50475, 13786,
+ 46919, 18358, 19128, 47689, 20644, 41045, 44379, 23978, 25756, 37997,
+ 39267, 27026, 33663, 29582, 32384, 36465,
+ },
+ { 0, 63223, 61939, 1796, 65531, 2316, 3592, 63743, 58347, 5404,
+ 4632, 58607, 7184, 60135, 60899, 6932, 56267, 11580, 10808, 56527,
+ 9264, 53959, 54723, 9012, 14368, 52951, 51667, 16164, 51163, 12588,
+ 13864, 49375, 43915, 23932, 23160, 44175, 21616, 41607, 42371, 21364,
+ 18528, 48791, 47507, 20324, 47003, 16748, 18024, 45215, 28736, 34487,
+ 33203, 30532, 36795, 31052, 32328, 35007, 37803, 25948, 25176, 38063,
+ 27728, 39591, 40355, 27476, 19211, 48636, 47864, 19471, 46320, 16903,
+ 17667, 46068, 43232, 24087, 22803, 45028, 22299, 41452, 42728, 20511,
+ 37056, 26167, 24883, 38852, 28475, 39372, 40648, 26687, 29483, 34268,
+ 33496, 29743, 36048, 31271, 32035, 35796, 57472, 5751, 4467, 59268,
+ 8059, 59788, 61064, 6271, 875, 62876, 62104, 1135, 64656, 2663,
+ 3427, 64404, 15179, 52668, 51896, 15439, 50352, 12871, 13635, 50100,
+ 55456, 11863, 10579, 57252, 10075, 53676, 54952, 8287, 38422, 24801,
+ 26597, 37138, 27117, 40730, 38942, 28393, 30205, 33546, 33806, 29433,
+ 35334, 31985, 31733, 36098, 19933, 47914, 48174, 19161, 45606, 17617,
+ 17365, 46370, 44598, 22721, 24517, 43314, 20941, 42810, 41022, 22217,
+ 15773, 52074, 52334, 15001, 49766, 13457, 13205, 50530, 56950, 10369,
+ 12165, 55666, 8589, 55162, 53374, 9865, 58966, 4257, 6053, 57682,
+ 6573, 61274, 59486, 7849, 1469, 62282, 62542, 697, 64070, 3249,
+ 2997, 64834, 56605, 11242, 11502, 55833, 8934, 54289, 54037, 9698,
+ 16118, 51201, 52997, 14834, 49421, 14330, 12542, 50697, 1750, 61473,
+ 63269, 466, 63789, 4058, 2270, 65065, 58685, 5066, 5326, 57913,
+ 6854, 60465, 60213, 7618, 30358, 32865, 34661, 29074, 35181, 32666,
+ 30878, 36457, 38269, 25482, 25742, 37497, 27270, 40049, 39797, 28034,
+ 44381, 23466, 23726, 43609, 21158, 42065, 41813, 21922, 20150, 47169,
+ 48965, 18866, 45389, 18362, 16574, 46665,
+ },
+ { 0, 62964, 63477, 513, 62455, 1539, 1026, 61942, 64499, 3591,
+ 3078, 63986, 2052, 65008, 65521, 2565, 60411, 7695, 7182, 59898,
+ 6156, 60920, 61433, 6669, 4104, 58876, 59389, 4617, 58367, 5643,
+ 5130, 57854, 52203, 15903, 15390, 51690, 14364, 52712, 53225, 14877,
+ 12312, 50668, 51181, 12825, 50159, 13851, 13338, 49646, 8208, 54756,
+ 55269, 8721, 54247, 9747, 9234, 53734, 56291, 11799, 11286, 55778,
+ 10260, 56800, 57313, 10773, 35787, 32319, 31806, 35274, 30780, 36296,
+ 36809, 31293, 28728, 34252, 34765, 29241, 33743, 30267, 29754, 33230,
+ 24624, 38340, 38853, 25137, 37831, 26163, 25650, 37318, 39875, 28215,
+ 27702, 39362, 26676, 40384, 40897, 27189, 16416, 46548, 47061, 16929,
+ 46039, 17955, 17442, 45526, 48083, 20007, 19494, 47570, 18468, 48592,
+ 49105, 18981, 43995, 24111, 23598, 43482, 22572, 44504, 45017, 23085,
+ 20520, 42460, 42973, 21033, 41951, 22059, 21546, 41438, 2955, 65151,
+ 64638, 2442, 63612, 3464, 3977, 64125, 61560, 1420, 1933, 62073,
+ 911, 63099, 62586, 398, 57456, 5508, 6021, 57969, 4999, 58995,
+ 58482, 4486, 7043, 61047, 60534, 6530, 59508, 7552, 8065, 60021,
+ 49248, 13716, 14229, 49761, 13207, 50787, 50274, 12694, 15251, 52839,
+ 52326, 14738, 51300, 15760, 16273, 51813, 11163, 56943, 56430, 10650,
+ 55404, 11672, 12185, 55917, 53352, 9628, 10141, 53865, 9119, 54891,
+ 54378, 8606, 32832, 30132, 30645, 33345, 29623, 34371, 33858, 29110,
+ 31667, 36423, 35910, 31154, 34884, 32176, 32689, 35397, 27579, 40527,
+ 40014, 27066, 38988, 28088, 28601, 39501, 36936, 26044, 26557, 37449,
+ 25535, 38475, 37962, 25022, 19371, 48735, 48222, 18858, 47196, 19880,
+ 20393, 47709, 45144, 17836, 18349, 45657, 17327, 46683, 46170, 16814,
+ 41040, 21924, 22437, 41553, 21415, 42579, 42066, 20902, 23459, 44631,
+ 44118, 22946, 43092, 23968, 24481, 43605,
+ },
+ { 0, 31355, 62710, 36493, 62961, 36746, 263, 31612, 63487, 36228,
+ 777, 31090, 526, 30837, 63224, 35971, 62435, 35224, 1813, 32110,
+ 1554, 31849, 62180, 34975, 1052, 32359, 61674, 35473, 61933, 35734,
+ 1307, 32608, 64475, 33184, 3885, 30038, 3626, 29777, 64220, 32935,
+ 3108, 30303, 63698, 33449, 63957, 33710, 3363, 30552, 2104, 29251,
+ 64718, 34485, 64969, 34738, 2367, 29508, 65479, 34236, 2865, 29002,
+ 2614, 28749, 65216, 33979, 60331, 37328, 8029, 25894, 7770, 25633,
+ 60076, 37079, 7252, 26159, 59554, 37593, 59813, 37854, 7507, 26408,
+ 6216, 25139, 60606, 38597, 60857, 38850, 6479, 25396, 61367, 38348,
+ 6977, 24890, 6726, 24637, 61104, 38091, 4208, 27147, 58502, 40701,
+ 58753, 40954, 4471, 27404, 59279, 40436, 4985, 26882, 4734, 26629,
+ 59016, 40179, 58259, 39400, 5989, 27934, 5730, 27673, 58004, 39151,
+ 5228, 28183, 57498, 39649, 57757, 39910, 5483, 28432, 52043, 45360,
+ 16317, 17862, 16058, 17601, 51788, 45111, 15540, 18127, 51266, 45625,
+ 51525, 45886, 15795, 18376, 14504, 17107, 52318, 46629, 52569, 46882,
+ 14767, 17364, 53079, 46380, 15265, 16858, 15014, 16605, 52816, 46123,
+ 12432, 19179, 50278, 48669, 50529, 48922, 12695, 19436, 51055, 48404,
+ 13209, 18914, 12958, 18661, 50792, 48147, 50035, 47368, 14213, 19966,
+ 13954, 19705, 49780, 47119, 13452, 20215, 49274, 47617, 49533, 47878,
+ 13707, 20464, 8416, 23195, 54294, 44653, 54545, 44906, 8679, 23452,
+ 55071, 44388, 9193, 22930, 8942, 22677, 54808, 44131, 54019, 43384,
+ 10229, 23950, 9970, 23689, 53764, 43135, 9468, 24199, 53258, 43633,
+ 53517, 43894, 9723, 24448, 56123, 41280, 12237, 21942, 11978, 21681,
+ 55868, 41031, 11460, 22207, 55346, 41545, 55605, 41806, 11715, 22456,
+ 10456, 21155, 56366, 42581, 56617, 42834, 10719, 21412, 57127, 42332,
+ 11217, 20906, 10966, 20653, 56864, 42075,
+ },
+ { 0, 46002, 31609, 51403, 63218, 17728, 36235, 15929, 61945, 16971,
+ 35456, 14642, 1803, 46265, 31858, 53184, 65519, 19549, 33942, 14116,
+ 2333, 47791, 29284, 49622, 3606, 48548, 30063, 50909, 63716, 19286,
+ 33693, 12335, 58307, 20593, 39098, 11016, 5425, 42627, 28232, 56826,
+ 4666, 41352, 26947, 56049, 58568, 22394, 40881, 11267, 7212, 44958,
+ 26453, 54503, 60126, 22892, 37287, 8725, 60885, 24167, 38572, 9502,
+ 6951, 43157, 24670, 54252, 56219, 26665, 41186, 4944, 11625, 40667,
+ 22032, 58786, 10850, 39376, 20763, 58025, 56464, 28450, 42985, 5211,
+ 9332, 38854, 24333, 60607, 53894, 24884, 43519, 6733, 54669, 26175,
+ 44788, 7494, 9087, 37069, 22534, 60340, 14424, 35818, 17185, 61587,
+ 52906, 32024, 46547, 1633, 51617, 31251, 45784, 362, 16211, 36065,
+ 17450, 63384, 51127, 29701, 48334, 3964, 12613, 33527, 19004, 63886,
+ 13902, 34300, 19767, 65157, 49340, 29454, 48069, 2167, 43819, 6297,
+ 53330, 25568, 24025, 61035, 9888, 38162, 23250, 59744, 8619, 37401,
+ 44064, 8082, 55129, 25835, 21700, 59254, 12221, 39951, 41526, 4484,
+ 55631, 27389, 42301, 5775, 56900, 28150, 21455, 57469, 10422, 39684,
+ 18664, 64346, 13201, 32803, 48666, 3496, 50531, 30417, 47377, 2723,
+ 49768, 29146, 20451, 64593, 13466, 34600, 46855, 1205, 52350, 32716,
+ 16885, 62023, 14988, 35134, 18174, 62796, 15751, 36405, 45068, 958,
+ 52085, 30919, 28848, 49922, 3017, 47227, 34370, 13808, 64827, 20105,
+ 33097, 13051, 64048, 18818, 30651, 50185, 3266, 49008, 36703, 15597,
+ 62502, 18324, 31149, 51743, 724, 45414, 32422, 52500, 1503, 46701,
+ 34900, 15334, 62253, 16543, 37747, 8385, 59402, 23480, 25985, 54835,
+ 7928, 44362, 25226, 53560, 6643, 43585, 38008, 10186, 61185, 23731,
+ 27804, 57134, 6117, 42071, 39534, 10716, 57623, 21157, 40293, 11991,
+ 58908, 21934, 27543, 55333, 4334, 41820,
+ },
+ { 0, 22872, 45744, 60392, 31101, 8229, 52173, 37525, 62202, 43938,
+ 16458, 6418, 35719, 53983, 14647, 24687, 63977, 41137, 19289, 4609,
+ 32916, 55756, 12836, 27516, 2835, 21067, 47523, 57595, 29294, 11062,
+ 49374, 39302, 61391, 46743, 23935, 1063, 38578, 53226, 9218, 32090,
+ 7477, 17517, 44933, 63197, 25672, 15632, 55032, 36768, 5670, 20350,
+ 42134, 64974, 28507, 13827, 56811, 33971, 58588, 48516, 22124, 3892,
+ 40353, 50425, 12049, 30281, 50051, 39643, 28979, 10347, 47870, 58278,
+ 2126, 20758, 12665, 26657, 33737, 55953, 18436, 4444, 64180, 41964,
+ 14954, 25394, 35034, 53634, 17175, 6735, 61863, 43263, 51344, 37320,
+ 31264, 9080, 45549, 59573, 861, 23045, 11340, 29972, 40700, 51108,
+ 21809, 3177, 59265, 48857, 57014, 34798, 27654, 13662, 42955, 65171,
+ 5499, 19491, 54693, 36093, 26389, 15949, 44248, 62848, 7784, 18224,
+ 10079, 32263, 38383, 52407, 24098, 1914, 60562, 46538, 39707, 49731,
+ 10667, 28915, 57958, 47934, 20694, 2446, 27105, 12473, 56145, 33289,
+ 4252, 18884, 41516, 64372, 25330, 15274, 53314, 35098, 7055, 17111,
+ 43327, 61543, 36872, 51536, 8888, 31712, 59765, 45101, 23493, 669,
+ 29908, 11660, 50788, 40764, 3497, 21745, 48921, 58945, 34350, 57206,
+ 13470, 28102, 65363, 42507, 19939, 5307, 36157, 54373, 16269, 26325,
+ 62528, 44312, 18160, 8104, 32711, 9887, 52599, 37935, 1722, 24546,
+ 46090, 60754, 22680, 448, 59944, 45936, 8677, 30909, 37717, 51725,
+ 43618, 62266, 6354, 16778, 54047, 35399, 25007, 14583, 41329, 63529,
+ 5057, 19097, 55308, 33108, 27324, 13284, 21387, 2771, 57659, 47203,
+ 10998, 29614, 38982, 49438, 46935, 60943, 1511, 23743, 52778, 38770,
+ 31898, 9666, 17837, 7413, 63261, 44613, 15568, 25992, 36448, 55096,
+ 20158, 6118, 64526, 42326, 14275, 28315, 34163, 56363, 48196, 58652,
+ 3828, 22444, 50489, 40033, 30601, 11985,
+ },
+ { 0, 11309, 22618, 29815, 45236, 40089, 59630, 50371, 32117, 20824,
+ 9519, 2306, 52673, 57836, 38299, 47542, 64234, 54983, 41648, 36509,
+ 19038, 26227, 4612, 15913, 34719, 43954, 57285, 62440, 14123, 6918,
+ 28529, 17244, 59849, 50660, 45459, 40382, 22909, 30032, 295, 11530,
+ 38076, 47249, 52454, 57547, 9224, 2085, 31826, 20607, 4899, 16142,
+ 19321, 26452, 41879, 36794, 64461, 55264, 28246, 17019, 13836, 6689,
+ 57058, 62159, 34488, 43669, 53135, 58274, 38869, 48120, 32571, 21270,
+ 10081, 2892, 45818, 40663, 60064, 50829, 590, 11875, 23060, 30265,
+ 13669, 6472, 27967, 16658, 34257, 43516, 56715, 61862, 18448, 25661,
+ 4170, 15463, 63652, 54409, 41214, 36051, 9798, 2667, 32284, 21041,
+ 38642, 47839, 52904, 57989, 23347, 30494, 873, 12100, 60295, 51114,
+ 46045, 40944, 56492, 61569, 34038, 43227, 27672, 16437, 13378, 6255,
+ 41433, 36340, 63875, 54702, 4461, 15680, 18743, 25882, 33539, 44846,
+ 56153, 63348, 13239, 8090, 27629, 18368, 65142, 53851, 42540, 35329,
+ 20162, 25327, 5784, 15029, 31209, 21956, 8627, 3486, 51549, 58736,
+ 37127, 48426, 1180, 10417, 23750, 28907, 46120, 38917, 60530, 49247,
+ 27338, 18151, 12944, 7869, 55934, 63059, 33316, 44553, 6079, 15250,
+ 20453, 25544, 42763, 35622, 65361, 54140, 36896, 48141, 51322, 58455,
+ 8340, 3257, 30926, 21731, 60757, 49528, 46351, 39202, 24033, 29132,
+ 1467, 10646, 19596, 24737, 5334, 14587, 64568, 53269, 42082, 34895,
+ 12793, 7636, 27043, 17806, 33101, 44384, 55575, 62778, 46694, 39499,
+ 60988, 49681, 1746, 11007, 24200, 29349, 51987, 59198, 37705, 48996,
+ 31655, 22410, 9213, 4048, 42309, 35176, 64799, 53554, 5617, 14812,
+ 19883, 24966, 55344, 62493, 32874, 44103, 26756, 17577, 12510, 7411,
+ 24495, 29570, 2037, 11224, 61211, 49974, 46913, 39788, 8922, 3831,
+ 31360, 22189, 37486, 48707, 51764, 58905,
+ },
+ { 0, 39065, 11567, 46518, 23134, 49863, 30577, 61416, 46268, 11301,
+ 39315, 266, 61154, 30331, 50125, 23380, 30053, 60924, 22602, 49363,
+ 12091, 47010, 532, 39565, 49625, 22848, 60662, 29807, 39815, 798,
+ 46760, 11825, 60106, 29267, 51173, 24444, 45204, 10253, 40379, 1314,
+ 24182, 50927, 29529, 60352, 1064, 40113, 10503, 45470, 40879, 1846,
+ 45696, 10777, 50673, 23912, 59614, 28743, 11027, 45962, 1596, 40613,
+ 29005, 59860, 23650, 50427, 51593, 20752, 58534, 31807, 37847, 2894,
+ 48888, 9825, 32053, 58796, 20506, 51331, 10091, 49138, 2628, 37597,
+ 48364, 9333, 37315, 2394, 59058, 32299, 52125, 21252, 2128, 37065,
+ 9599, 48614, 21006, 51863, 32545, 59320, 9027, 48090, 3692, 38645,
+ 31005, 57732, 21554, 52395, 38911, 3942, 47824, 8777, 52641, 21816,
+ 57486, 30743, 22054, 52927, 31497, 58256, 3192, 38113, 8535, 47566,
+ 58010, 31235, 53173, 22316, 47300, 8285, 38379, 3442, 36623, 6038,
+ 41504, 15033, 54609, 19912, 63614, 24807, 15283, 41770, 5788, 36357,
+ 25069, 63860, 19650, 54363, 64106, 25331, 55109, 20444, 41012, 14509,
+ 36123, 5506, 20182, 54863, 25593, 64352, 5256, 35857, 14759, 41278,
+ 26053, 64860, 18666, 53363, 16283, 42754, 4788, 35373, 53625, 18912,
+ 64598, 25807, 35623, 5054, 42504, 16017, 4256, 34873, 15759, 42262,
+ 19198, 53863, 26577, 65352, 42012, 15493, 35123, 4522, 65090, 26331,
+ 54125, 19444, 18054, 56863, 27561, 62256, 7384, 33857, 12791, 43374,
+ 62010, 27299, 57109, 18316, 43108, 12541, 34123, 7634, 13283, 43898,
+ 7884, 34389, 27069, 61732, 17554, 56331, 34655, 8134, 43632, 13033,
+ 56577, 17816, 61486, 26807, 44108, 13525, 33123, 6650, 62994, 28299,
+ 56125, 17316, 6384, 32873, 13791, 44358, 17070, 55863, 28545, 63256,
+ 55593, 16816, 62470, 27807, 33655, 7150, 44632, 14017, 28053, 62732,
+ 16570, 55331, 14283, 44882, 6884, 33405,
+ },
+ { 0, 49859, 39323, 23384, 12075, 60904, 46768, 29811, 24150, 40085,
+ 51149, 1294, 29053, 46014, 59622, 10789, 48300, 32367, 9527, 59380,
+ 37767, 20804, 2588, 51423, 58106, 8249, 31585, 47522, 52689, 3858,
+ 21578, 38537, 25925, 42886, 64734, 15901, 19054, 34989, 54261, 4406,
+ 15123, 63952, 41608, 24651, 5176, 55035, 36259, 20320, 55785, 6954,
+ 16498, 33457, 63170, 13313, 28505, 44442, 34751, 17788, 7716, 56551,
+ 43156, 27223, 12559, 62412, 51850, 2121, 21265, 37330, 58785, 10082,
+ 31802, 48889, 38108, 22047, 3399, 53124, 48119, 31028, 8812, 57519,
+ 30246, 46309, 61373, 11646, 22797, 39886, 49302, 597, 10352, 60083,
+ 45547, 29480, 1883, 50584, 40640, 23555, 45007, 27916, 13908, 62615,
+ 32996, 16935, 6527, 56252, 61849, 13146, 26626, 43713, 57010, 7281,
+ 18217, 34282, 4963, 53664, 35576, 18491, 15432, 65163, 42451, 26384,
+ 19765, 36854, 54446, 5741, 25118, 41181, 64389, 14662, 35081, 19402,
+ 4242, 53841, 42530, 25825, 16313, 64890, 55135, 5532, 20164, 35847,
+ 63604, 15031, 25071, 41772, 13733, 63334, 44094, 28413, 6798, 55373,
+ 33557, 16854, 27635, 43312, 62056, 12459, 17624, 34331, 56643, 8064,
+ 60492, 11919, 30167, 46868, 50023, 420, 23292, 38975, 45594, 28889,
+ 11137, 59714, 40241, 24562, 1194, 50793, 20704, 37411, 51579, 3000,
+ 32715, 48392, 58960, 9363, 3766, 52341, 38701, 21998, 8605, 58206,
+ 47110, 31429, 17283, 33088, 55832, 6363, 27816, 44651, 62771, 14320,
+ 7637, 57110, 33870, 18061, 13054, 61501, 43877, 27046, 65327, 15852,
+ 26292, 42103, 53252, 4807, 18847, 35676, 41337, 25530, 14562, 64033,
+ 36434, 19601, 6089, 54538, 9926, 58373, 48989, 32158, 2541, 52014,
+ 36982, 21173, 30864, 47699, 57611, 9160, 22459, 38264, 52768, 3299,
+ 39530, 22697, 1009, 49458, 46401, 30594, 11482, 60953, 50236, 1791,
+ 23975, 40804, 60183, 10708, 29324, 45135,
+ },
+ { 0, 61422, 50113, 11311, 39839, 29809, 22622, 47024, 11043, 50381,
+ 59618, 1804, 45244, 24402, 29565, 40083, 22086, 47528, 38279, 31337,
+ 52697, 8759, 3608, 57846, 32101, 37515, 48804, 20810, 59130, 2324,
+ 9531, 51925, 44172, 17250, 28493, 32931, 14099, 55549, 62674, 6972,
+ 34735, 26689, 17518, 43904, 7216, 62430, 57329, 12319, 64202, 5412,
+ 14603, 55013, 24917, 36539, 41620, 19834, 53737, 15879, 4648, 64966,
+ 19062, 42392, 35255, 26201, 17669, 43755, 34500, 26922, 56986, 12660,
+ 7515, 62133, 28198, 33224, 44519, 16905, 62905, 6743, 13944, 55702,
+ 4931, 64685, 53378, 16236, 35036, 26418, 19229, 42227, 14432, 55182,
+ 64417, 5199, 41983, 19473, 24638, 36816, 59785, 1639, 10824, 50598,
+ 29206, 40440, 45527, 24121, 49834, 11588, 363, 61061, 22837, 46811,
+ 39668, 29978, 49103, 20513, 31758, 37856, 9296, 52158, 59281, 2175,
+ 38124, 31490, 22317, 47299, 3955, 57501, 52402, 9052, 35338, 26084,
+ 18891, 42533, 4501, 65147, 53844, 15802, 41257, 20167, 25320, 36102,
+ 15030, 54616, 63863, 5785, 56396, 13218, 8077, 61539, 18387, 43069,
+ 33810, 27644, 63343, 6273, 13486, 56128, 27888, 33566, 44849, 16607,
+ 9862, 51560, 58695, 2729, 48409, 21239, 32472, 37174, 3493, 57931,
+ 52836, 8586, 38458, 31188, 22011, 47637, 28864, 40750, 45825, 23791,
+ 60255, 1201, 10398, 51056, 23523, 46093, 38946, 30668, 49276, 12178,
+ 957, 60499, 53007, 8417, 3278, 58144, 21648, 47998, 38737, 30911,
+ 58412, 3010, 10221, 51203, 32691, 36957, 48242, 21404, 39241, 30375,
+ 23176, 46438, 726, 60728, 49431, 12025, 45674, 23940, 29099, 40517,
+ 10741, 50715, 59956, 1498, 25475, 35949, 41026, 20396, 63516, 6130,
+ 15325, 54323, 18592, 42830, 35681, 25743, 54079, 15569, 4350, 65296,
+ 13765, 55851, 62980, 6634, 44634, 16820, 28059, 33397, 7910, 61704,
+ 56615, 13001, 34169, 27287, 18104, 43350,
+ },
+ { 0, 30582, 61164, 39322, 49605, 46771, 12073, 22623, 40855, 59617,
+ 29051, 1549, 24146, 10532, 45246, 51144, 9011, 21573, 52703, 47785,
+ 58102, 38272, 3098, 31596, 48292, 52178, 21064, 9534, 32097, 2583,
+ 37773, 58619, 18022, 12560, 43146, 57340, 34723, 61653, 26959, 7737,
+ 55793, 44679, 14109, 16491, 6196, 28482, 63192, 33198, 25941, 4643,
+ 35769, 64719, 42128, 54246, 19068, 15626, 64194, 36276, 5166, 25432,
+ 15111, 19569, 54763, 41629, 36044, 64442, 25120, 5462, 19721, 14975,
+ 41957, 54419, 4955, 25645, 64951, 35521, 53918, 42472, 15474, 19204,
+ 45055, 55433, 16659, 13925, 28218, 6476, 32982, 63392, 12392, 18206,
+ 56964, 43506, 61869, 34523, 8001, 26679, 51882, 48604, 9286, 21296,
+ 2927, 31769, 58755, 37621, 21821, 8779, 48081, 52391, 38136, 58254,
+ 31252, 3426, 59801, 40687, 1909, 28675, 10332, 24362, 50864, 45510,
+ 30222, 376, 39138, 61332, 47051, 49341, 22823, 11857, 1413, 29427,
+ 60265, 39967, 50240, 45878, 10924, 24026, 39442, 60772, 29950, 904,
+ 23511, 11425, 46395, 49741, 9910, 20928, 51290, 48940, 59251, 36869,
+ 2463, 32489, 47393, 52823, 22477, 8379, 30948, 3986, 38408, 57726,
+ 17379, 13461, 44303, 55929, 33318, 62800, 27850, 7100, 56436, 43778,
+ 12952, 17902, 7601, 27335, 62301, 33835, 24784, 6054, 36412, 63818,
+ 41237, 54883, 20473, 14479, 65351, 34865, 4523, 26333, 16002, 18932,
+ 53358, 42776, 35145, 65087, 26533, 4307, 18572, 16378, 42592, 53526,
+ 5854, 25000, 63538, 36676, 55067, 41069, 14839, 20097, 43642, 56588,
+ 17558, 13280, 27583, 7369, 34131, 61989, 13805, 17051, 56065, 44151,
+ 62504, 33630, 6852, 28082, 53039, 47193, 8643, 22197, 3818, 31132,
+ 57350, 38768, 20664, 10190, 48724, 51490, 37245, 58891, 32657, 2279,
+ 60444, 39786, 752, 30086, 11737, 23215, 49973, 46147, 29579, 1277,
+ 40295, 59921, 45646, 50488, 23714, 11220,
+ },
+ { 0, 15162, 30324, 19790, 60648, 55250, 39580, 41382, 50637, 65271,
+ 46009, 34947, 10533, 4639, 24401, 25707, 38791, 44221, 57843, 56009,
+ 31599, 16469, 3355, 13857, 21066, 26992, 9278, 7940, 48802, 34200,
+ 51414, 62444, 13075, 2089, 17767, 32349, 57339, 58561, 43407, 37557,
+ 63198, 52708, 32938, 48016, 6710, 8460, 27714, 22392, 42132, 40878,
+ 53984, 59866, 18556, 29510, 15880, 1330, 24921, 23139, 5933, 11287,
+ 36273, 46731, 64453, 49407, 26150, 23836, 4178, 11112, 35534, 45556,
+ 64698, 51072, 41963, 39121, 54687, 61093, 20227, 29753, 14711, 589,
+ 61857, 51867, 34773, 48367, 7497, 9843, 27453, 20487, 13420, 3926,
+ 16920, 31010, 55428, 58302, 44784, 38346, 21813, 28175, 9025, 6267,
+ 47581, 33511, 53161, 62611, 37112, 43970, 59020, 56758, 31760, 18218,
+ 2660, 12638, 49842, 63880, 46278, 36860, 11866, 5472, 22574, 25364,
+ 1919, 15429, 28939, 18993, 60311, 53421, 40419, 42713, 52300, 63350,
+ 47672, 33026, 8356, 7070, 22224, 28138, 2433, 12987, 32757, 17615,
+ 58729, 56915, 37661, 43047, 23499, 24817, 11711, 5765, 46883, 35865,
+ 49495, 64109, 40454, 42300, 59506, 54088, 29422, 18900, 1178, 16288,
+ 65375, 50277, 35115, 45585, 5047, 10381, 26051, 24313, 14994, 424,
+ 19686, 30684, 54906, 60736, 40974, 39732, 26840, 21474, 7852, 9622,
+ 33840, 48906, 62020, 51582, 44309, 38447, 56161, 57435, 16893, 31431,
+ 14217, 3251, 43626, 37200, 56350, 59172, 18050, 32184, 12534, 3020,
+ 28583, 21661, 6611, 8937, 33615, 47221, 62779, 52737, 15853, 1751,
+ 19353, 28835, 53509, 59967, 42865, 40011, 63520, 49946, 36436, 46446,
+ 5320, 12274, 25276, 22918, 39289, 41539, 61197, 54327, 30097, 20139,
+ 997, 14559, 23732, 26510, 10944, 4602, 45148, 35686, 50728, 64786,
+ 3838, 13764, 30858, 17328, 57878, 55596, 37986, 44888, 52019, 61449,
+ 48455, 34429, 10203, 7393, 20911, 27285,
+ },
+ { 0, 7452, 14904, 10020, 29808, 26988, 20040, 21332, 59616, 62972,
+ 53976, 53188, 40080, 33164, 42664, 48052, 52701, 53441, 63461, 60153,
+ 47533, 42161, 33685, 40585, 9533, 14369, 7941, 537, 20813, 19537,
+ 27509, 30313, 34727, 39611, 48543, 41091, 62423, 61131, 51695, 54515,
+ 28487, 29275, 21887, 18531, 6967, 1579, 8463, 15379, 19066, 22374,
+ 28738, 27998, 15882, 8982, 1074, 6446, 41626, 49030, 39074, 34238,
+ 55018, 52214, 60626, 61902, 4947, 3663, 10603, 13431, 26403, 31295,
+ 23835, 16391, 64435, 59055, 49547, 56471, 36803, 37599, 46587, 43239,
+ 56974, 50066, 58550, 63914, 43774, 47074, 37062, 36314, 13934, 11122,
+ 3158, 4426, 16926, 24322, 30758, 25914, 38132, 35304, 44748, 46032,
+ 57476, 64920, 55996, 51104, 31764, 24840, 17964, 23344, 2148, 5496,
+ 12892, 12096, 22825, 17461, 25361, 32269, 11609, 12357, 5985, 2685,
+ 45513, 44245, 35825, 38637, 50617, 55461, 65409, 58013, 9894, 15290,
+ 7326, 386, 21206, 20426, 26862, 30194, 52806, 54106, 62590, 59746,
+ 47670, 42794, 32782, 40210, 60283, 63079, 53571, 52319, 40715, 33303,
+ 42291, 47151, 923, 7815, 14755, 9407, 30699, 27383, 19923, 20687,
+ 41217, 48157, 39737, 34341, 54641, 51309, 61257, 62037, 18913, 21757,
+ 29657, 28357, 15761, 8333, 1961, 6837, 27868, 29120, 22244, 19448,
+ 6316, 1456, 8852, 16264, 33852, 39200, 48644, 41752, 61516, 60752,
+ 51828, 55144, 13813, 10473, 4045, 4817, 16773, 23705, 31677, 26273,
+ 56597, 49161, 59181, 64049, 43365, 46201, 37725, 36417, 63528, 58676,
+ 49680, 57100, 35928, 37188, 46688, 43900, 4296, 3540, 10992, 14316,
+ 25784, 31140, 24192, 17308, 45650, 44878, 34922, 38262, 50722, 56126,
+ 64538, 57606, 23218, 18350, 24714, 32150, 11970, 13278, 5370, 2534,
+ 32655, 25235, 17847, 22699, 3071, 5859, 12743, 11483, 38767, 35443,
+ 44375, 45131, 58143, 65027, 55591, 50235,
+ },
+ { 0, 3599, 7198, 4625, 14396, 13875, 9250, 10797, 28792, 32375,
+ 27750, 25193, 18500, 17995, 21594, 23125, 57584, 61183, 64750, 62177,
+ 55500, 54979, 50386, 51933, 37000, 40583, 35990, 33433, 43188, 42683,
+ 46250, 47781, 56829, 54258, 49635, 53228, 58817, 60366, 63967, 63440,
+ 44421, 41866, 45467, 49044, 38329, 39862, 35239, 34728, 15629, 13058,
+ 8467, 12060, 1329, 2878, 6447, 5920, 19829, 17274, 20843, 24420,
+ 30025, 31558, 26967, 26456, 42983, 43496, 48121, 46582, 40923, 37332,
+ 33733, 36298, 55199, 55696, 52097, 50574, 61347, 57772, 62397, 64946,
+ 18199, 18712, 23305, 21766, 32555, 28964, 25397, 27962, 14191, 14688,
+ 11121, 9598, 3923, 348, 4941, 7490, 31258, 29717, 26116, 26635,
+ 16934, 19497, 24120, 20535, 2658, 1133, 5756, 6259, 12894, 15441,
+ 11840, 8271, 39658, 38117, 34548, 35067, 41686, 44249, 48840, 45255,
+ 60050, 58525, 63116, 63619, 53934, 56481, 52912, 49343, 21459, 24028,
+ 20429, 16834, 27631, 26080, 30705, 31230, 9131, 11684, 16309, 12730,
+ 7063, 5528, 1929, 2438, 45859, 48428, 44861, 41266, 35615, 34064,
+ 38657, 39182, 50011, 52564, 57157, 53578, 64359, 62824, 59257, 59766,
+ 36398, 32801, 37424, 39999, 46610, 47133, 43532, 41987, 65110, 61529,
+ 57928, 60487, 50794, 51301, 55924, 54395, 28382, 24785, 29376, 31951,
+ 22242, 22765, 19196, 17651, 7846, 4265, 696, 3255, 9882, 10389,
+ 14980, 13451, 62516, 64059, 59434, 58917, 52232, 49671, 53270, 56857,
+ 33868, 35395, 38994, 38493, 48240, 45695, 41070, 44641, 5316, 6859,
+ 2266, 1749, 11512, 8951, 12518, 16105, 25788, 27315, 30882, 30381,
+ 23680, 21135, 16542, 20113, 10697, 10182, 13783, 15320, 4597, 8186,
+ 3563, 996, 22961, 22462, 17839, 19360, 24973, 28546, 32147, 29596,
+ 51513, 50998, 54567, 56104, 61701, 65290, 60699, 58132, 47425, 46926,
+ 42335, 43856, 33149, 36722, 40291, 37740,
+ },
+ { 0, 35208, 3853, 34437, 7706, 38802, 4375, 39071, 15412, 46524,
+ 13113, 47793, 8750, 43942, 11555, 42155, 30824, 61920, 30565, 65261,
+ 26226, 61434, 27007, 57591, 17500, 52692, 19281, 49881, 23110, 54222,
+ 21835, 56515, 61648, 31064, 65501, 30293, 61130, 26434, 57799, 26703,
+ 52452, 17772, 50153, 19041, 54014, 23414, 56819, 21627, 35000, 304,
+ 34741, 3645, 38562, 7978, 39343, 4135, 46220, 15620, 48001, 12809,
+ 43670, 8990, 42395, 11283, 64957, 29749, 62128, 31544, 58279, 27183,
+ 60586, 25890, 49545, 18433, 52868, 18188, 57235, 22043, 53406, 22806,
+ 34261, 3165, 35544, 848, 39887, 4679, 38082, 7498, 47585, 12393,
+ 46828, 16228, 43003, 11891, 43254, 8574, 3437, 34021, 608, 35816,
+ 4983, 39679, 7290, 38386, 12633, 47313, 15956, 47068, 12099, 42699,
+ 8270, 43462, 29957, 64653, 31240, 62336, 27423, 58007, 25618, 60826,
+ 18737, 49337, 17980, 53172, 22315, 56995, 22566, 53678, 59239, 28399,
+ 59498, 25058, 63869, 28917, 63088, 32760, 56147, 21211, 54366, 24022,
+ 50505, 19649, 51780, 17356, 40719, 5767, 36866, 6538, 33045, 2205,
+ 36376, 1936, 41787, 10931, 44086, 9662, 48417, 13481, 45612, 15268,
+ 6071, 40511, 6330, 37170, 2477, 32805, 1696, 36648, 11139, 41483,
+ 9358, 44294, 13721, 48145, 14996, 45852, 28639, 58967, 24786, 59738,
+ 29125, 63565, 32456, 63296, 21483, 55907, 23782, 54638, 19953, 50297,
+ 17148, 52084, 6874, 37714, 5591, 40031, 1216, 36168, 3021, 33349,
+ 9966, 44902, 10723, 41067, 14580, 45436, 14329, 48753, 25266, 60218,
+ 28095, 58423, 31912, 62752, 29605, 64045, 24198, 55054, 20875, 55299,
+ 16540, 51476, 20369, 50713, 59914, 25474, 58631, 27791, 62480, 32152,
+ 64285, 29333, 54846, 24502, 55603, 20667, 51236, 16812, 50985, 20129,
+ 37474, 7146, 40303, 5351, 35960, 1520, 33653, 2813, 44630, 10206,
+ 41307, 10451, 45132, 14788, 48961, 14025,
+ },
+ { 0, 17477, 34954, 52431, 3337, 18764, 34179, 49606, 6674, 24151,
+ 37528, 55005, 5915, 21342, 40849, 56276, 13348, 28769, 48302, 63723,
+ 14637, 32104, 45479, 62946, 11830, 27251, 42684, 58105, 9023, 26490,
+ 43957, 61424, 26696, 11277, 57538, 42119, 25921, 8452, 60875, 43406,
+ 29274, 13855, 64208, 48789, 32595, 15126, 63449, 45980, 23660, 6185,
+ 54502, 37027, 20837, 5408, 55791, 40362, 18046, 571, 52980, 35505,
+ 19319, 3890, 50173, 34744, 53392, 38101, 22554, 7263, 56729, 39388,
+ 21779, 4438, 51842, 36551, 16904, 1613, 51083, 33742, 20225, 2884,
+ 58548, 41201, 27710, 10363, 59837, 44536, 24887, 9586, 65190, 47843,
+ 30252, 12905, 62383, 47082, 31525, 16224, 47320, 64669, 12370, 29719,
+ 46545, 61844, 15707, 31006, 41674, 59023, 10816, 28165, 44995, 60294,
+ 10057, 25356, 36092, 51385, 1142, 16435, 33269, 50608, 2431, 19770,
+ 38638, 53931, 7780, 23073, 39911, 57250, 4973, 22312, 48445, 63864,
+ 13751, 29170, 45108, 62577, 14526, 31995, 42799, 58218, 12197, 27616,
+ 43558, 61027, 8876, 26345, 35097, 52572, 403, 17878, 33808, 49237,
+ 3226, 18655, 37643, 55118, 7041, 24516, 40450, 55879, 5768, 21197,
+ 54645, 37168, 24063, 6586, 55420, 39993, 20726, 5299, 53095, 35618,
+ 18413, 936, 49774, 34347, 19172, 3745, 57681, 42260, 27099, 11678,
+ 60504, 43037, 25810, 8343, 64323, 48902, 29641, 14220, 63050, 45583,
+ 32448, 14981, 28077, 10728, 58663, 41314, 24740, 9441, 59438, 44139,
+ 30655, 13306, 65333, 47984, 31414, 16115, 62012, 46713, 22921, 7628,
+ 53507, 38214, 21632, 4293, 56330, 38991, 17307, 2014, 51985, 36692,
+ 20114, 2775, 50712, 33373, 1509, 16800, 36207, 51498, 2284, 19625,
+ 32870, 50211, 8183, 23474, 38781, 54072, 4862, 22203, 39540, 56881,
+ 12737, 30084, 47435, 64782, 15560, 30861, 46146, 61447, 11219, 28566,
+ 41817, 59164, 9946, 25247, 44624, 59925,
+ },
+ { 0, 44205, 17735, 59882, 35470, 9763, 53193, 25444, 2305, 42412,
+ 19526, 57579, 33679, 12066, 50888, 27237, 4610, 48815, 22341, 64488,
+ 39052, 13345, 56779, 29030, 6915, 47022, 24132, 62185, 37261, 15648,
+ 54474, 30823, 9220, 34985, 24899, 52718, 44682, 551, 60365, 18272,
+ 11525, 33192, 26690, 50415, 42891, 2854, 58060, 20065, 13830, 39595,
+ 29505, 57324, 48264, 4133, 63951, 21858, 16135, 37802, 31296, 55021,
+ 46473, 6436, 61646, 23651, 18440, 58533, 3407, 41442, 49798, 28203,
+ 34753, 11116, 16649, 60836, 1102, 43235, 52103, 26410, 36544, 8813,
+ 23050, 63143, 8013, 46048, 53380, 31785, 38339, 14702, 21259, 65446,
+ 5708, 47841, 55685, 29992, 40130, 12399, 27660, 49313, 10571, 34278,
+ 59010, 18991, 41925, 3944, 25869, 51616, 8266, 36071, 61315, 17198,
+ 43716, 1641, 32270, 53923, 15177, 38884, 62592, 22573, 45511, 7530,
+ 30479, 56226, 12872, 40677, 64897, 20780, 47302, 5227, 36880, 15549,
+ 54615, 31226, 6814, 46643, 24537, 62324, 39185, 13756, 56406, 28923,
+ 5023, 48946, 22232, 64117, 33298, 11967, 51029, 27640, 2204, 42033,
+ 19931, 57718, 35603, 10174, 52820, 25337, 413, 44336, 17626, 59511,
+ 46100, 6329, 61779, 24062, 16026, 37431, 31709, 55152, 48405, 4536,
+ 63570, 21759, 14235, 39734, 29404, 56945, 42518, 2747, 58193, 20476,
+ 11416, 32821, 27103, 50546, 44823, 954, 59984, 18173, 9625, 35124,
+ 24798, 52339, 55320, 29877, 40287, 12786, 21142, 65083, 6097, 47996,
+ 53529, 32180, 37982, 14579, 23447, 63290, 7888, 45693, 51738, 26295,
+ 36701, 9200, 16532, 60473, 1491, 43390, 49947, 28598, 34396, 10993,
+ 18837, 58680, 3282, 41087, 64540, 20657, 47451, 5622, 30354, 55871,
+ 13269, 40824, 62749, 22960, 45146, 7415, 32659, 54078, 15060, 38521,
+ 60958, 17075, 43865, 2036, 25744, 51261, 8663, 36218, 59167, 19378,
+ 41560, 3829, 28049, 49468, 10454, 33915,
+ },
+ { 0, 55513, 44463, 30070, 18243, 40858, 60140, 12853, 36486, 22111,
+ 9001, 64496, 51653, 4380, 25706, 48307, 273, 55752, 44222, 29799,
+ 18002, 40587, 60413, 13092, 36759, 22350, 8760, 64225, 51412, 4109,
+ 25979, 48546, 546, 56059, 44941, 30548, 17761, 40376, 59598, 12311,
+ 36004, 21629, 8459, 63954, 52199, 4926, 26184, 48785, 819, 56298,
+ 44700, 30277, 17520, 40105, 59871, 12550, 36277, 21868, 8218, 63683,
+ 51958, 4655, 26457, 49024, 1092, 56477, 43499, 28978, 17159, 39902,
+ 61096, 13937, 35522, 21019, 10093, 65460, 52609, 5464, 24622, 47351,
+ 1365, 56716, 43258, 28707, 16918, 39631, 61369, 14176, 35795, 21258,
+ 9852, 65189, 52368, 5193, 24895, 47590, 1638, 57023, 43977, 29456,
+ 16677, 39420, 60554, 13395, 35040, 20537, 9551, 64918, 53155, 6010,
+ 25100, 47829, 1911, 57262, 43736, 29185, 16436, 39149, 60827, 13634,
+ 35313, 20776, 9310, 64647, 52914, 5739, 25373, 48068, 2184, 53329,
+ 42279, 32254, 20427, 38674, 57956, 15037, 34318, 24279, 11169, 62328,
+ 49485, 6548, 27874, 46139, 2457, 53568, 42038, 31983, 20186, 38403,
+ 58229, 15276, 34591, 24518, 10928, 62057, 49244, 6277, 28147, 46378,
+ 2730, 53875, 42757, 32732, 19945, 38192, 57414, 14495, 33836, 23797,
+ 10627, 61786, 50031, 7094, 28352, 46617, 3003, 54114, 42516, 32461,
+ 19704, 37921, 57687, 14734, 34109, 24036, 10386, 61515, 49790, 6823,
+ 28625, 46856, 3276, 54293, 41315, 31162, 19343, 37718, 58912, 16121,
+ 33354, 23187, 12261, 63292, 50441, 7632, 26790, 45183, 3549, 54532,
+ 41074, 30891, 19102, 37447, 59185, 16360, 33627, 23426, 12020, 63021,
+ 50200, 7361, 27063, 45422, 3822, 54839, 41793, 31640, 18861, 37236,
+ 58370, 15579, 32872, 22705, 11719, 62750, 50987, 8178, 27268, 45661,
+ 4095, 55078, 41552, 31369, 18620, 36965, 58643, 15818, 33145, 22944,
+ 11478, 62479, 50746, 7907, 27541, 45900,
+ },
+ { 0, 58083, 55771, 15160, 44971, 19784, 30320, 38035, 17227, 41384,
+ 39568, 30835, 60640, 3587, 13627, 55256, 34454, 25717, 24397, 48558,
+ 10557, 52190, 61670, 4613, 50653, 10046, 7174, 65253, 27254, 34965,
+ 45997, 20814, 4401, 62418, 51434, 10761, 48794, 23673, 26433, 34210,
+ 21114, 45209, 35745, 26946, 64977, 7986, 9226, 50921, 38823, 30020,
+ 20092, 44191, 14348, 56047, 57815, 820, 54508, 13839, 3383, 61396,
+ 31559, 39332, 41628, 16511, 8802, 49281, 64441, 6490, 36297, 28458,
+ 21522, 46833, 24873, 33738, 47346, 23057, 52866, 11361, 5977, 62906,
+ 42228, 17943, 32047, 40908, 2911, 59836, 53892, 12391, 59327, 1372,
+ 15972, 56455, 18452, 43767, 37327, 29484, 13139, 53680, 60040, 2155,
+ 40184, 32283, 17699, 42944, 28696, 37627, 43459, 19232, 57267, 15696,
+ 1640, 58507, 46533, 22310, 27678, 36605, 6766, 63629, 50101, 8534,
+ 63118, 5229, 12117, 52662, 22821, 48070, 33022, 25117, 17604, 42535,
+ 40223, 32764, 60271, 2444, 12980, 53335, 1935, 58732, 56916, 15543,
+ 43044, 19143, 29183, 37660, 49746, 8369, 7049, 63850, 28153, 36634,
+ 46114, 22209, 33049, 25594, 22722, 47649, 11954, 52305, 63337, 5514,
+ 22005, 46870, 35886, 28365, 64094, 6333, 9093, 49510, 5822, 62557,
+ 53093, 11654, 47381, 23542, 24782, 33325, 54115, 12672, 2744, 59483,
+ 31944, 40491, 42259, 18416, 36904, 29387, 18931, 43792, 16259, 56672,
+ 58968, 1211, 26278, 33861, 49021, 23966, 51469, 11246, 4310, 62005,
+ 9709, 50958, 64566, 7893, 35398, 26789, 21405, 45438, 57392, 723,
+ 14827, 56072, 20379, 44408, 38464, 29859, 41851, 16792, 31392, 38979,
+ 3280, 60979, 54539, 14312, 30615, 38260, 44620, 19631, 55356, 15071,
+ 487, 58116, 13532, 54847, 60679, 4068, 39799, 31124, 17068, 41039,
+ 61697, 5090, 10458, 51769, 24234, 48201, 34673, 26002, 45642, 20649,
+ 27537, 35186, 7649, 65282, 50234, 9945,
+ },
+ { 0, 65534, 58337, 7199, 56287, 9249, 14398, 51136, 43939, 21597,
+ 18498, 47036, 28796, 36738, 37789, 27747, 19291, 46245, 43194, 22340,
+ 36996, 28538, 29541, 35995, 57592, 7942, 793, 64743, 15143, 50393,
+ 55494, 10040, 38582, 26952, 30039, 35497, 19817, 45719, 44680, 20854,
+ 15637, 49899, 57076, 8458, 59082, 6452, 1323, 64213, 56813, 8723,
+ 15884, 49650, 1586, 63948, 58835, 6701, 30286, 35248, 38319, 27217,
+ 44433, 21103, 20080, 45454, 12657, 52879, 53904, 11630, 60078, 5456,
+ 2383, 63153, 39634, 25900, 31027, 34509, 16653, 48883, 41708, 23826,
+ 31274, 34260, 39371, 26165, 41461, 24075, 16916, 48618, 53641, 11895,
+ 12904, 52630, 2646, 62888, 59831, 5705, 42951, 22585, 17446, 48088,
+ 31768, 33766, 40953, 24583, 3172, 62362, 61317, 4219, 55227, 10309,
+ 13402, 52132, 60572, 4962, 3965, 61571, 14147, 51389, 54434, 11100,
+ 18239, 47297, 42206, 23328, 40160, 25374, 32513, 33023, 25314, 40220,
+ 33027, 32509, 47421, 18115, 23260, 42274, 51521, 14015, 10912, 54622,
+ 4766, 60768, 61823, 3713, 10681, 54855, 51800, 13734, 62054, 3480,
+ 4487, 61049, 33306, 32228, 25083, 40453, 22981, 42555, 47652, 17882,
+ 62548, 2986, 6069, 59467, 12171, 53365, 52330, 13204, 24567, 40969,
+ 48150, 17384, 33832, 31702, 26569, 38967, 48911, 16625, 23790, 41744,
+ 25808, 39726, 34609, 30927, 5292, 60242, 63309, 2227, 53107, 12429,
+ 11410, 54124, 21395, 44141, 45170, 20364, 34892, 30642, 27565, 37971,
+ 63536, 1998, 7121, 58415, 9199, 56337, 49166, 16368, 6344, 59190,
+ 64297, 1239, 49943, 15593, 8438, 57096, 45931, 19605, 20618, 44916,
+ 26804, 38730, 35669, 29867, 50469, 15067, 9924, 55610, 7930, 57604,
+ 64795, 741, 28294, 37240, 36199, 29337, 46425, 19111, 22200, 43334,
+ 36478, 29056, 28063, 37473, 21921, 43615, 46656, 18878, 9693, 55843,
+ 50748, 14786, 65026, 508, 7651, 57885,
+ },
+ { 0, 32638, 65276, 33154, 57829, 40603, 7961, 24679, 57303, 41129,
+ 8491, 24149, 15922, 16716, 49358, 49072, 41907, 56525, 23887, 8753,
+ 16982, 15656, 48298, 50132, 31844, 794, 33432, 64998, 40321, 58111,
+ 25469, 7171, 23419, 9221, 42375, 56057, 47774, 50656, 17506, 15132,
+ 33964, 64466, 31312, 1326, 25929, 6711, 39861, 58571, 63688, 34742,
+ 1588, 31050, 6445, 26195, 59345, 39087, 10015, 22625, 55779, 42653,
+ 50938, 47492, 14342, 18296, 46838, 51592, 18442, 14196, 22291, 10349,
+ 43503, 54929, 26913, 5727, 38877, 59555, 35012, 63418, 30264, 2374,
+ 5445, 27195, 60345, 38087, 62624, 35806, 2652, 29986, 51858, 46572,
+ 13422, 19216, 11127, 21513, 54667, 43765, 60813, 37619, 4977, 27663,
+ 3176, 29462, 62100, 36330, 12890, 19748, 52390, 46040, 54207, 44225,
+ 11587, 21053, 20030, 12608, 45250, 53180, 45019, 53413, 20775, 11865,
+ 37353, 61079, 28437, 4203, 28684, 3954, 36592, 61838, 29169, 3727,
+ 36621, 61555, 36884, 61290, 28392, 4502, 44582, 53592, 20698, 12196,
+ 20419, 12477, 45375, 52801, 53826, 44348, 11454, 21440, 13223, 19673,
+ 52571, 45605, 3477, 29419, 62313, 35863, 60528, 37646, 4748, 28146,
+ 10890, 22004, 54390, 43784, 52079, 46097, 13715, 19181, 62813, 35363,
+ 2977, 29919, 5304, 27590, 59972, 38202, 35129, 63047, 30661, 2235,
+ 26844, 6050, 38432, 59742, 22254, 10640, 43026, 55148, 46859, 51317,
+ 18935, 13961, 50951, 47225, 14843, 18053, 9954, 22940, 55326, 42848,
+ 6352, 26542, 58924, 39250, 63797, 34379, 1993, 30903, 25780, 7114,
+ 39496, 58678, 34129, 64047, 31661, 1235, 47971, 50205, 17823, 15073,
+ 23174, 9720, 42106, 56068, 40060, 58114, 25216, 7678, 32153, 743,
+ 33637, 64539, 17323, 15573, 48471, 49705, 41550, 56624, 23730, 9164,
+ 16335, 16561, 49459, 48717, 56874, 41300, 8406, 24488, 57368, 40806,
+ 7908, 24986, 509, 32387, 65281, 32895,
+ },
+ { 0, 16190, 32380, 16706, 64760, 50118, 33412, 48570, 58861, 56019,
+ 39825, 42159, 6421, 9771, 26473, 22615, 55239, 59641, 43451, 38533,
+ 11071, 5121, 21827, 27261, 12842, 3348, 19542, 29544, 52946, 61932,
+ 45230, 36752, 45971, 36013, 52719, 62161, 20331, 28757, 12567, 3625,
+ 22142, 26944, 10242, 5948, 43654, 38328, 54522, 60356, 25684, 23402,
+ 6696, 9494, 39084, 42898, 59088, 55790, 33209, 48775, 65477, 49403,
+ 32065, 17023, 829, 15363, 31547, 17413, 1351, 14969, 34755, 47357,
+ 63935, 50817, 40662, 41448, 57514, 57236, 25134, 23824, 7250, 9068,
+ 44284, 37826, 53888, 60862, 20484, 28474, 11896, 4422, 18705, 30255,
+ 14189, 2131, 46569, 35543, 52117, 62635, 51368, 63382, 46804, 35306,
+ 13392, 2926, 18988, 29970, 11589, 4731, 21305, 27655, 53693, 61059,
+ 44993, 37119, 8047, 8273, 24851, 24109, 58263, 56489, 40427, 41685,
+ 64130, 50620, 34046, 48064, 1658, 14660, 30726, 18232, 63094, 51528,
+ 34826, 46900, 2702, 13744, 29938, 19404, 5019, 11429, 28135, 21209,
+ 61283, 53341, 37151, 44577, 8625, 7823, 24525, 24819, 56649, 57975,
+ 41781, 39947, 50268, 64354, 47648, 34078, 14500, 1946, 18136, 31206,
+ 17893, 31451, 15257, 1191, 47389, 34339, 51041, 63583, 40968, 40758,
+ 56948, 57674, 23792, 25550, 8844, 7602, 37410, 44316, 60510, 54112,
+ 28378, 20964, 4262, 12184, 30671, 18673, 2483, 13965, 35639, 46089,
+ 62795, 51829, 36173, 45683, 62257, 52239, 29109, 20107, 4041, 12535,
+ 26784, 22430, 5852, 10722, 37976, 43878, 59940, 54554, 23178, 26036,
+ 9462, 7112, 42610, 39244, 55310, 59184, 48999, 32857, 49435, 65061,
+ 17311, 31905, 15843, 733, 16094, 480, 16546, 32668, 49702, 64792,
+ 48218, 33636, 56115, 58381, 42319, 39537, 10187, 6389, 22967, 26249,
+ 59673, 54823, 38757, 43099, 5601, 10975, 27549, 21667, 3316, 13258,
+ 29320, 19894, 61452, 53042, 36464, 45390,
+ },
+ { 0, 7966, 15932, 8482, 31864, 25446, 16964, 23898, 63728, 59374,
+ 50892, 55762, 33928, 39830, 47796, 42410, 60925, 62179, 54209, 52447,
+ 37253, 36507, 44985, 45223, 5389, 2579, 11057, 13359, 26997, 30315,
+ 22345, 18519, 51175, 55545, 63963, 59077, 48031, 42113, 34211, 39613,
+ 16151, 8201, 299, 7733, 17263, 23665, 32083, 25165, 10778, 13572,
+ 5158, 2872, 22114, 18812, 26718, 30528, 53994, 52724, 60630, 62408,
+ 44690, 45452, 37038, 36784, 37843, 36045, 44527, 45809, 61355, 61621,
+ 53655, 52873, 27427, 29757, 21791, 18945, 5979, 2117, 10599, 13945,
+ 32302, 24880, 16402, 24332, 598, 7496, 15466, 9076, 34526, 39360,
+ 47330, 43004, 64166, 58808, 50330, 56196, 21556, 19242, 27144, 29974,
+ 10316, 14162, 5744, 2414, 44228, 46042, 37624, 36326, 53436, 53154,
+ 61056, 61854, 47561, 42711, 34805, 39147, 50609, 55983, 64397, 58515,
+ 16697, 24103, 32517, 24603, 15681, 8799, 893, 7267, 15291, 9381,
+ 1415, 6809, 18371, 22749, 31231, 26337, 49995, 56405, 64887, 57961,
+ 48947, 41005, 33039, 40465, 54854, 51544, 59514, 63332, 43582, 46368,
+ 37890, 35612, 11958, 12712, 4234, 3988, 21198, 19920, 27890, 29676,
+ 64604, 58178, 49760, 56702, 32804, 40762, 48664, 41222, 1196, 7090,
+ 14992, 9614, 30932, 26570, 18152, 23030, 4513, 3775, 12189, 12419,
+ 28121, 29383, 21477, 19707, 59729, 63055, 55149, 51315, 38185, 35383,
+ 43797, 46091, 43112, 46966, 38484, 35146, 54288, 51982, 59948, 62770,
+ 20632, 20358, 28324, 29114, 11488, 13310, 4828, 3522, 17813, 23179,
+ 31657, 25783, 14829, 9971, 2001, 6351, 48485, 41595, 33625, 40007,
+ 49437, 56835, 65313, 57407, 28559, 28817, 20915, 20141, 5111, 3305,
+ 11723, 13013, 38783, 34913, 43331, 46685, 60167, 62489, 54587, 51749,
+ 33394, 40300, 48206, 41808, 65034, 57620, 49206, 57128, 31362, 26012,
+ 17598, 23456, 1786, 6628, 14534, 10200,
+ },
+ { 0, 3854, 7708, 4370, 15416, 13110, 8740, 11562, 30832, 30590,
+ 26220, 26978, 17480, 19270, 23124, 21850, 61664, 65518, 61180, 57842,
+ 52440, 50134, 53956, 56778, 34960, 34718, 38540, 39298, 46248, 48038,
+ 43700, 42426, 64989, 62163, 58305, 60623, 49637, 52971, 57337, 53495,
+ 34221, 35491, 39857, 38079, 47509, 46747, 42889, 43143, 3389, 563,
+ 4897, 7215, 12549, 15883, 12057, 8215, 30029, 31299, 27473, 25695,
+ 18805, 18043, 22377, 22631, 59303, 59561, 63931, 63157, 56223, 54417,
+ 50563, 51853, 40919, 37081, 33227, 36549, 41967, 44257, 48627, 45821,
+ 5959, 6217, 2395, 1621, 11135, 9329, 13667, 14957, 28471, 24633,
+ 28971, 32293, 21263, 23553, 19731, 16925, 6778, 5492, 1126, 2920,
+ 9794, 10572, 14430, 14160, 25098, 27908, 31766, 29464, 24114, 20796,
+ 16430, 20256, 60058, 58772, 62598, 64392, 54946, 55724, 51390, 51120,
+ 37610, 40420, 36086, 33784, 44754, 41436, 45262, 49088, 54099, 56413,
+ 52559, 49729, 61291, 57445, 61815, 65145, 43811, 42029, 46399, 47665,
+ 38683, 38933, 35079, 34313, 9139, 11453, 15791, 12961, 8075, 4229,
+ 407, 3737, 23491, 21709, 17887, 19153, 26619, 26869, 31207, 30441,
+ 11918, 8576, 12434, 16284, 4790, 7608, 3242, 932, 22270, 23024,
+ 18658, 18412, 27334, 26056, 29914, 31700, 56942, 53600, 49266, 53116,
+ 57942, 60760, 64586, 62276, 42526, 43280, 47106, 46860, 39462, 38184,
+ 33850, 35636, 13556, 15354, 10984, 9702, 2252, 1986, 5840, 6622,
+ 19588, 17290, 21144, 23958, 28860, 32690, 28320, 25006, 50196, 51994,
+ 55816, 54534, 63532, 63266, 58928, 59710, 48228, 45930, 41592, 44406,
+ 32860, 36690, 40512, 37198, 51497, 50727, 55093, 55355, 62737, 64031,
+ 60173, 58371, 45401, 48727, 44869, 41035, 36193, 33391, 37757, 40051,
+ 14793, 14023, 10197, 10459, 1521, 2815, 7149, 5347, 16825, 20151,
+ 24485, 20651, 32129, 29327, 25501, 27795,
+ },
+ { 0, 1798, 3596, 2314, 7192, 6942, 4628, 5394, 14384, 16182,
+ 13884, 12602, 9256, 9006, 10788, 11554, 28768, 30566, 32364, 31082,
+ 27768, 27518, 25204, 25970, 18512, 20310, 18012, 16730, 21576, 21326,
+ 23108, 23874, 57536, 59334, 61132, 59850, 64728, 64478, 62164, 62930,
+ 55536, 57334, 55036, 53754, 50408, 50158, 51940, 52706, 37024, 38822,
+ 40620, 39338, 36024, 35774, 33460, 34226, 43152, 44950, 42652, 41370,
+ 46216, 45966, 47748, 48514, 56733, 55963, 54161, 54423, 49541, 50819,
+ 53129, 51343, 58797, 58027, 60321, 60583, 63925, 65203, 63417, 61631,
+ 44541, 43771, 41969, 42231, 45541, 46819, 49129, 47343, 38349, 37579,
+ 39873, 40135, 35285, 36563, 34777, 32991, 15709, 14939, 13137, 13399,
+ 8517, 9795, 12105, 10319, 1389, 619, 2913, 3175, 6517, 7795,
+ 6009, 4223, 19773, 19003, 17201, 17463, 20773, 22051, 24361, 22575,
+ 29965, 29195, 31489, 31751, 26901, 28179, 26393, 24607, 42791, 40993,
+ 43307, 44589, 47935, 48185, 46387, 45621, 40727, 38929, 37147, 38429,
+ 33551, 33801, 36099, 35333, 55111, 53313, 55627, 56909, 52063, 52313,
+ 50515, 49749, 61303, 59505, 57723, 59005, 62319, 62569, 64867, 64101,
+ 18407, 16609, 18923, 20205, 23551, 23801, 22003, 21237, 32727, 30929,
+ 29147, 30429, 25551, 25801, 28099, 27333, 14215, 12417, 14731, 16013,
+ 11167, 11417, 9619, 8853, 4023, 2225, 443, 1725, 5039, 5289,
+ 7587, 6821, 31418, 32188, 29878, 29616, 26274, 24996, 26798, 28584,
+ 17034, 17804, 19590, 19328, 24210, 22932, 20638, 22424, 2778, 3548,
+ 1238, 976, 5826, 4548, 6350, 8136, 13034, 13804, 15590, 15328,
+ 12018, 10740, 8446, 10232, 39546, 40316, 38006, 37744, 34402, 33124,
+ 34926, 36712, 41546, 42316, 44102, 43840, 48722, 47444, 45150, 46936,
+ 59930, 60700, 58390, 58128, 62978, 61700, 63502, 65288, 53802, 54572,
+ 56358, 56096, 52786, 51508, 49214, 51000,
+ },
+ { 0, 770, 1540, 1286, 3080, 3850, 2572, 2318, 6160, 6930,
+ 7700, 7446, 5144, 5914, 4636, 4382, 12320, 13090, 13860, 13606,
+ 15400, 16170, 14892, 14638, 10288, 11058, 11828, 11574, 9272, 10042,
+ 8764, 8510, 24640, 25410, 26180, 25926, 27720, 28490, 27212, 26958,
+ 30800, 31570, 32340, 32086, 29784, 30554, 29276, 29022, 20576, 21346,
+ 22116, 21862, 23656, 24426, 23148, 22894, 18544, 19314, 20084, 19830,
+ 17528, 18298, 17020, 16766, 49280, 50050, 50820, 50566, 52360, 53130,
+ 51852, 51598, 55440, 56210, 56980, 56726, 54424, 55194, 53916, 53662,
+ 61600, 62370, 63140, 62886, 64680, 65450, 64172, 63918, 59568, 60338,
+ 61108, 60854, 58552, 59322, 58044, 57790, 41152, 41922, 42692, 42438,
+ 44232, 45002, 43724, 43470, 47312, 48082, 48852, 48598, 46296, 47066,
+ 45788, 45534, 37088, 37858, 38628, 38374, 40168, 40938, 39660, 39406,
+ 35056, 35826, 36596, 36342, 34040, 34810, 33532, 33278, 40221, 40479,
+ 39705, 38939, 37141, 37399, 38673, 37907, 34061, 34319, 33545, 32779,
+ 35077, 35335, 36609, 35843, 44349, 44607, 43833, 43067, 41269, 41527,
+ 42801, 42035, 46381, 46639, 45865, 45099, 47397, 47655, 48929, 48163,
+ 64861, 65119, 64345, 63579, 61781, 62039, 63313, 62547, 58701, 58959,
+ 58185, 57419, 59717, 59975, 61249, 60483, 52605, 52863, 52089, 51323,
+ 49525, 49783, 51057, 50291, 54637, 54895, 54121, 53355, 55653, 55911,
+ 57185, 56419, 23965, 24223, 23449, 22683, 20885, 21143, 22417, 21651,
+ 17805, 18063, 17289, 16523, 18821, 19079, 20353, 19587, 28093, 28351,
+ 27577, 26811, 25013, 25271, 26545, 25779, 30125, 30383, 29609, 28843,
+ 31141, 31399, 32673, 31907, 15837, 16095, 15321, 14555, 12757, 13015,
+ 14289, 13523, 9677, 9935, 9161, 8395, 10693, 10951, 12225, 11459,
+ 3581, 3839, 3065, 2299, 501, 759, 2033, 1267, 5613, 5871,
+ 5097, 4331, 6629, 6887, 8161, 7395,
+ },
+};
+
+#endif /* __VCD_CD_SECTOR_PRIVATE_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/stream.c b/contrib/libvcd/stream.c
new file mode 100644
index 000000000..2840a05d6
--- /dev/null
+++ b/contrib/libvcd/stream.c
@@ -0,0 +1,276 @@
+/*
+ $Id: stream.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include <cdio/cdio.h>
+
+/* #define STREAM_DEBUG */
+
+/* Public headers */
+
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "vcd_assert.h"
+#include "stream.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: stream.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $";
+
+/*
+ * DataSource implementations
+ */
+
+struct _VcdDataSink {
+ void* user_data;
+ vcd_data_sink_io_functions op;
+ int is_open;
+ long position;
+};
+
+static void
+_vcd_data_sink_open_if_necessary(VcdDataSink *obj)
+{
+ vcd_assert (obj != NULL);
+
+ if (!obj->is_open) {
+ if (obj->op.open(obj->user_data))
+ vcd_error("could not opening output stream...");
+ else {
+ obj->is_open = 1;
+ obj->position = 0;
+ }
+ }
+}
+
+VcdDataSink*
+vcd_data_sink_new(void *user_data, const vcd_data_sink_io_functions *funcs)
+{
+ VcdDataSink *new_obj;
+
+ new_obj = _vcd_malloc(sizeof(VcdDataSink));
+
+ new_obj->user_data = user_data;
+ memcpy(&(new_obj->op), funcs, sizeof(vcd_data_sink_io_functions));
+
+ return new_obj;
+}
+
+long
+vcd_data_sink_seek(VcdDataSink* obj, long offset)
+{
+ vcd_assert (obj != NULL);
+
+ _vcd_data_sink_open_if_necessary(obj);
+
+ if (obj->position != offset) {
+ vcd_warn("had to reposition DataSink from %ld to %ld!", obj->position, offset);
+ obj->position = offset;
+ return obj->op.seek(obj->user_data, offset);
+ }
+
+ return 0;
+}
+
+long
+vcd_data_sink_write(VcdDataSink* obj, const void *ptr, long size, long nmemb)
+{
+ long written;
+
+ vcd_assert (obj != NULL);
+
+ _vcd_data_sink_open_if_necessary(obj);
+
+ written = obj->op.write(obj->user_data, ptr, size*nmemb);
+ obj->position += written;
+
+ return written;
+}
+
+long
+vcd_data_sink_printf (VcdDataSink *obj, const char format[], ...)
+{
+ char buf[4096] = { 0, };
+ long retval;
+ int len;
+
+ va_list args;
+ va_start (args, format);
+
+ len = vsnprintf (buf, sizeof(buf), format, args);
+
+ if (len < 0 || len > (sizeof (buf) - 1))
+ vcd_error ("vsnprintf() returned %d", len);
+
+ retval = vcd_data_sink_write (obj, buf, 1, len);
+
+ va_end (args);
+
+ return retval;
+}
+
+void
+vcd_data_sink_close(VcdDataSink* obj)
+{
+ vcd_assert (obj != NULL);
+
+ if (obj->is_open) {
+ obj->op.close(obj->user_data);
+ obj->is_open = 0;
+ obj->position = 0;
+ }
+}
+
+void
+vcd_data_sink_destroy(VcdDataSink* obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_data_sink_close(obj);
+
+ obj->op.free(obj->user_data);
+}
+
+/*
+ * DataSource implementations
+ */
+
+struct _VcdDataSource {
+ void* user_data;
+ vcd_data_source_io_functions op;
+ int is_open;
+ long position;
+};
+
+static void
+_vcd_data_source_open_if_necessary(VcdDataSource *obj)
+{
+ vcd_assert (obj != NULL);
+
+ if (!obj->is_open) {
+ if (obj->op.open(obj->user_data))
+ vcd_error ("could not opening input stream...");
+ else {
+#ifdef STREAM_DEBUG
+ vcd_debug ("opened source...");
+#endif
+ obj->is_open = 1;
+ obj->position = 0;
+ }
+ }
+}
+
+long
+vcd_data_source_seek(VcdDataSource* obj, long offset)
+{
+ vcd_assert (obj != NULL);
+
+ _vcd_data_source_open_if_necessary(obj);
+
+ if (obj->position != offset) {
+#ifdef STREAM_DEBUG
+ vcd_warn("had to reposition DataSource from %ld to %ld!", obj->position, offset);
+#endif
+ obj->position = offset;
+ return obj->op.seek(obj->user_data, offset);
+ }
+
+ return 0;
+}
+
+VcdDataSource*
+vcd_data_source_new(void *user_data, const vcd_data_source_io_functions *funcs)
+{
+ VcdDataSource *new_obj;
+
+ new_obj = _vcd_malloc (sizeof (VcdDataSource));
+
+ new_obj->user_data = user_data;
+ memcpy(&(new_obj->op), funcs, sizeof(vcd_data_source_io_functions));
+
+ return new_obj;
+}
+
+long
+vcd_data_source_read(VcdDataSource* obj, void *ptr, long size, long nmemb)
+{
+ long read_bytes;
+
+ vcd_assert (obj != NULL);
+
+ _vcd_data_source_open_if_necessary(obj);
+
+ read_bytes = obj->op.read(obj->user_data, ptr, size*nmemb);
+ obj->position += read_bytes;
+
+ return read_bytes;
+}
+
+long
+vcd_data_source_stat(VcdDataSource* obj)
+{
+ vcd_assert (obj != NULL);
+
+ _vcd_data_source_open_if_necessary(obj);
+
+ return obj->op.stat(obj->user_data);
+}
+
+void
+vcd_data_source_close(VcdDataSource* obj)
+{
+ vcd_assert (obj != NULL);
+
+ if (obj->is_open) {
+#ifdef STREAM_DEBUG
+ vcd_debug ("closed source...");
+#endif
+ obj->op.close(obj->user_data);
+ obj->is_open = 0;
+ obj->position = 0;
+ }
+}
+
+void
+vcd_data_source_destroy(VcdDataSource* obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_data_source_close(obj);
+
+ obj->op.free(obj->user_data);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/stream.h b/contrib/libvcd/stream.h
new file mode 100644
index 000000000..43b8c911a
--- /dev/null
+++ b/contrib/libvcd/stream.h
@@ -0,0 +1,123 @@
+/*
+ $Id: stream.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifndef __VCD_STREAM_H__
+#define __VCD_STREAM_H__
+
+#include <libvcd/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* typedef'ed IO functions prototypes */
+
+typedef int(*vcd_data_open_t)(void *user_data);
+
+typedef long(*vcd_data_read_t)(void *user_data, void *buf, long count);
+
+typedef long(*vcd_data_write_t)(void *user_data, const void *buf,
+ long count);
+
+typedef long(*vcd_data_seek_t)(void *user_data, long offset);
+
+typedef long(*vcd_data_stat_t)(void *user_data);
+
+typedef int(*vcd_data_close_t)(void *user_data);
+
+typedef void(*vcd_data_free_t)(void *user_data);
+
+
+/* abstract data sink */
+
+typedef struct _VcdDataSink VcdDataSink;
+
+typedef struct {
+ vcd_data_open_t open;
+ vcd_data_seek_t seek;
+ vcd_data_write_t write;
+ vcd_data_close_t close;
+ vcd_data_free_t free;
+} vcd_data_sink_io_functions;
+
+VcdDataSink*
+vcd_data_sink_new(void *user_data, const vcd_data_sink_io_functions *funcs);
+
+long
+vcd_data_sink_write(VcdDataSink* obj, const void *ptr, long size, long nmemb);
+
+long
+vcd_data_sink_printf (VcdDataSink *obj, const char format[], ...) GNUC_PRINTF(2, 3);
+
+long
+vcd_data_sink_seek(VcdDataSink* obj, long offset);
+
+void
+vcd_data_sink_destroy(VcdDataSink* obj);
+
+void
+vcd_data_sink_close(VcdDataSink* obj);
+
+/* abstract data source */
+
+typedef struct _VcdDataSource VcdDataSource;
+
+typedef struct {
+ vcd_data_open_t open;
+ vcd_data_seek_t seek;
+ vcd_data_stat_t stat;
+ vcd_data_read_t read;
+ vcd_data_close_t close;
+ vcd_data_free_t free;
+} vcd_data_source_io_functions;
+
+VcdDataSource*
+vcd_data_source_new(void *user_data, const vcd_data_source_io_functions *funcs);
+
+long
+vcd_data_source_read(VcdDataSource* obj, void *ptr, long size, long nmemb);
+
+long
+vcd_data_source_seek(VcdDataSource* obj, long offset);
+
+long
+vcd_data_source_stat(VcdDataSource* obj);
+
+void
+vcd_data_source_destroy(VcdDataSource* obj);
+
+void
+vcd_data_source_close(VcdDataSource* obj);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __VCD_STREAM_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/stream_stdio.c b/contrib/libvcd/stream_stdio.c
new file mode 100644
index 000000000..4eac0f6b3
--- /dev/null
+++ b/contrib/libvcd/stream_stdio.c
@@ -0,0 +1,236 @@
+/*
+ $Id: stream_stdio.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include <cdio/cdio.h>
+
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "stream_stdio.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: stream_stdio.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $";
+
+#define VCD_STREAM_STDIO_BUFSIZE (128*1024)
+
+typedef struct {
+ char *pathname;
+ FILE *fd;
+ char *fd_buf;
+ off_t st_size; /* used only for source */
+} _UserData;
+
+static int
+_stdio_open_source (void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if ((ud->fd = fopen (ud->pathname, "rb")))
+ {
+ ud->fd_buf = _vcd_malloc (VCD_STREAM_STDIO_BUFSIZE);
+ setvbuf (ud->fd, ud->fd_buf, _IOFBF, VCD_STREAM_STDIO_BUFSIZE);
+ }
+
+ return (ud->fd == NULL);
+}
+
+static int
+_stdio_open_sink (void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if ((ud->fd = fopen (ud->pathname, "wb")))
+ {
+ ud->fd_buf = _vcd_malloc (VCD_STREAM_STDIO_BUFSIZE);
+ setvbuf (ud->fd, ud->fd_buf, _IOFBF, VCD_STREAM_STDIO_BUFSIZE);
+ }
+
+ return (ud->fd == NULL);
+}
+
+static int
+_stdio_close(void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if (fclose (ud->fd))
+ vcd_error ("fclose (): %s", strerror (errno));
+
+ ud->fd = NULL;
+
+ free (ud->fd_buf);
+ ud->fd_buf = NULL;
+
+ return 0;
+}
+
+static void
+_stdio_free(void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if (ud->pathname)
+ free(ud->pathname);
+
+ if (ud->fd) /* should be NULL anyway... */
+ _stdio_close(user_data);
+
+ free(ud);
+}
+
+static long
+_stdio_seek(void *user_data, long offset)
+{
+ _UserData *const ud = user_data;
+
+ if (fseek (ud->fd, offset, SEEK_SET))
+ vcd_error ("fseek (): %s", strerror (errno));
+
+ return offset;
+}
+
+static long
+_stdio_stat(void *user_data)
+{
+ const _UserData *const ud = user_data;
+
+ return ud->st_size;
+}
+
+static long
+_stdio_read(void *user_data, void *buf, long count)
+{
+ _UserData *const ud = user_data;
+ long read;
+
+ read = fread(buf, 1, count, ud->fd);
+
+ if (read != count)
+ { /* fixme -- ferror/feof */
+ if (feof (ud->fd))
+ {
+ vcd_debug ("fread (): EOF encountered");
+ clearerr (ud->fd);
+ }
+ else if (ferror (ud->fd))
+ {
+ vcd_error ("fread (): %s", strerror (errno));
+ clearerr (ud->fd);
+ }
+ else
+ vcd_debug ("fread (): short read and no EOF?!?");
+ }
+
+ return read;
+}
+
+static long
+_stdio_write(void *user_data, const void *buf, long count)
+{
+ _UserData *const ud = user_data;
+ long written;
+
+ written = fwrite(buf, 1, count, ud->fd);
+
+ if (written != count)
+ vcd_error ("fwrite (): %s", strerror (errno));
+
+ return written;
+}
+
+VcdDataSource*
+vcd_data_source_new_stdio(const char pathname[])
+{
+ VcdDataSource *new_obj = NULL;
+ vcd_data_source_io_functions funcs = { 0, };
+ _UserData *ud = NULL;
+ struct stat statbuf;
+
+ if (stat (pathname, &statbuf) == -1)
+ {
+ vcd_error ("could not stat() file `%s': %s", pathname, strerror (errno));
+ return NULL;
+ }
+
+ ud = _vcd_malloc (sizeof (_UserData));
+
+ ud->pathname = strdup(pathname);
+ ud->st_size = statbuf.st_size; /* let's hope it doesn't change... */
+
+ funcs.open = _stdio_open_source;
+ funcs.seek = _stdio_seek;
+ funcs.stat = _stdio_stat;
+ funcs.read = _stdio_read;
+ funcs.close = _stdio_close;
+ funcs.free = _stdio_free;
+
+ new_obj = vcd_data_source_new(ud, &funcs);
+
+ return new_obj;
+}
+
+
+VcdDataSink*
+vcd_data_sink_new_stdio(const char pathname[])
+{
+ VcdDataSink *new_obj = NULL;
+ vcd_data_sink_io_functions funcs;
+ _UserData *ud = NULL;
+ struct stat statbuf;
+
+ if (stat (pathname, &statbuf) != -1)
+ vcd_warn ("file `%s' exist already, will get overwritten!", pathname);
+
+ ud = _vcd_malloc (sizeof (_UserData));
+
+ memset (&funcs, 0, sizeof (funcs));
+
+ ud->pathname = strdup (pathname);
+
+ funcs.open = _stdio_open_sink;
+ funcs.seek = _stdio_seek;
+ funcs.write = _stdio_write;
+ funcs.close = _stdio_close;
+ funcs.free = _stdio_free;
+
+ new_obj = vcd_data_sink_new (ud, &funcs);
+
+ return new_obj;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/stream_stdio.h b/contrib/libvcd/stream_stdio.h
new file mode 100644
index 000000000..46bca3140
--- /dev/null
+++ b/contrib/libvcd/stream_stdio.h
@@ -0,0 +1,43 @@
+/*
+ $Id: stream_stdio.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifndef __VCD_STREAM_STDIO_H__
+#define __VCD_STREAM_STDIO_H__
+
+/* Private headers */
+#include "stream.h"
+
+VcdDataSink*
+vcd_data_sink_new_stdio(const char pathname[]);
+
+VcdDataSource*
+vcd_data_source_new_stdio(const char pathname[]);
+
+#endif /* __VCD_STREAM_STDIO_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/util.c b/contrib/libvcd/util.c
new file mode 100644
index 000000000..a1e5c57d4
--- /dev/null
+++ b/contrib/libvcd/util.c
@@ -0,0 +1,179 @@
+/*
+ $Id: util.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+/* Private includes */
+#include "vcd_assert.h"
+#include "bytesex.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: util.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $";
+
+size_t
+_vcd_strlenv(char **str_array)
+{
+ size_t n = 0;
+
+ vcd_assert (str_array != NULL);
+
+ while(str_array[n])
+ n++;
+
+ return n;
+}
+
+void
+_vcd_strfreev(char **strv)
+{
+ int n;
+
+ vcd_assert (strv != NULL);
+
+ for(n = 0; strv[n]; n++)
+ free(strv[n]);
+
+ free(strv);
+}
+
+char *
+_vcd_strjoin (char *strv[], unsigned count, const char delim[])
+{
+ size_t len;
+ char *new_str;
+ unsigned n;
+
+ vcd_assert (strv != NULL);
+ vcd_assert (delim != NULL);
+
+ len = (count-1) * strlen (delim);
+
+ for (n = 0;n < count;n++)
+ len += strlen (strv[n]);
+
+ len++;
+
+ new_str = _vcd_malloc (len);
+ new_str[0] = '\0';
+
+ for (n = 0;n < count;n++)
+ {
+ if (n)
+ strcat (new_str, delim);
+ strcat (new_str, strv[n]);
+ }
+
+ return new_str;
+}
+
+char **
+_vcd_strsplit(const char str[], char delim) /* fixme -- non-reentrant */
+{
+ int n;
+ char **strv = NULL;
+ char *_str, *p;
+ char _delim[2] = { 0, 0 };
+
+ vcd_assert (str != NULL);
+
+ _str = strdup(str);
+ _delim[0] = delim;
+
+ vcd_assert (_str != NULL);
+
+ n = 1;
+ p = _str;
+ while(*p)
+ if (*(p++) == delim)
+ n++;
+
+ strv = _vcd_malloc (sizeof (char *) * (n+1));
+
+ n = 0;
+ while((p = strtok(n ? NULL : _str, _delim)) != NULL)
+ strv[n++] = strdup(p);
+
+ free(_str);
+
+ return strv;
+}
+
+void *
+_vcd_malloc (size_t size)
+{
+ void *new_mem = malloc (size);
+
+ vcd_assert (new_mem != NULL);
+
+ memset (new_mem, 0, size);
+
+ return new_mem;
+}
+
+void *
+_vcd_memdup (const void *mem, size_t count)
+{
+ void *new_mem = NULL;
+
+ if (mem)
+ {
+ new_mem = _vcd_malloc (count);
+ memcpy (new_mem, mem, count);
+ }
+
+ return new_mem;
+}
+
+char *
+_vcd_strdup_upper (const char str[])
+{
+ char *new_str = NULL;
+
+ if (str)
+ {
+ char *p;
+
+ p = new_str = strdup (str);
+
+ while (*p)
+ {
+ *p = toupper (*p);
+ p++;
+ }
+ }
+
+ return new_str;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/util.h b/contrib/libvcd/util.h
new file mode 100644
index 000000000..c5a9ead7c
--- /dev/null
+++ b/contrib/libvcd/util.h
@@ -0,0 +1,108 @@
+/*
+ $Id: util.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_UTIL_H__
+#define __VCD_UTIL_H__
+
+#include <stdlib.h>
+#include <libvcd/types.h>
+
+#ifndef __CDIO_UTIL_H__
+#undef MAX
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
+#undef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+
+#undef IN
+#define IN(x, low, high) ((x) >= (low) && (x) <= (high))
+
+#undef CLAMP
+#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
+
+#endif
+
+static inline unsigned
+_vcd_len2blocks (unsigned len, int blocksize)
+{
+ unsigned blocks;
+
+ blocks = len / blocksize;
+ if (len % blocksize)
+ blocks++;
+
+ return blocks;
+}
+
+/* round up to next block boundary */
+static inline unsigned
+_vcd_ceil2block (unsigned offset, int blocksize)
+{
+ return _vcd_len2blocks (offset, blocksize) * blocksize;
+}
+
+static inline unsigned
+_vcd_ofs_add (unsigned offset, unsigned length, int blocksize)
+{
+ if (blocksize - (offset % blocksize) < length)
+ offset = _vcd_ceil2block (offset, blocksize);
+
+ offset += length;
+
+ return offset;
+}
+
+size_t
+_vcd_strlenv(char **str_array);
+
+char *
+_vcd_strjoin (char *strv[], unsigned count, const char delim[]);
+
+char **
+_vcd_strsplit(const char str[], char delim);
+
+void
+_vcd_strfreev(char **strv);
+
+void *
+_vcd_malloc (size_t size);
+
+void *
+_vcd_memdup (const void *mem, size_t count);
+
+char *
+_vcd_strdup_upper (const char str[]);
+
+static inline const char *
+_vcd_bool_str (bool b)
+{
+ return b ? "yes" : "no";
+}
+
+#endif /* __VCD_UTIL_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/vcd.c b/contrib/libvcd/vcd.c
new file mode 100644
index 000000000..0772149ec
--- /dev/null
+++ b/contrib/libvcd/vcd.c
@@ -0,0 +1,2412 @@
+/*
+ $Id: vcd.c,v 1.4 2006/12/08 16:26:10 mshopf Exp $
+
+ Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <math.h>
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+
+/* public headers */
+#include <libvcd/types.h>
+#include <libvcd/info.h>
+
+#include <libvcd/files.h>
+#include <libvcd/sector.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+#include "dict.h"
+#include "directory.h"
+#include "obj.h"
+#include "pbc.h"
+#include "salloc.h"
+#include "util.h"
+#include "vcd.h"
+
+static const char _rcsid[] = "$Id: vcd.c,v 1.4 2006/12/08 16:26:10 mshopf Exp $";
+
+static const char zero[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+#define DEFAULT_ISO_PREPARER_ID "GNU VCDImager " VERSION " " HOST_ARCH
+
+/* exported private functions
+ */
+
+mpeg_sequence_t *
+_vcd_obj_get_sequence_by_id (VcdObj *obj, const char sequence_id[])
+{
+ CdioListNode *node;
+
+ vcd_assert (sequence_id != NULL);
+ vcd_assert (obj != NULL);
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _cdio_list_node_data (node);
+
+ if (_sequence->id && !strcmp (sequence_id, _sequence->id))
+ return _sequence;
+ }
+
+ return NULL;
+}
+
+mpeg_sequence_t *
+_vcd_obj_get_sequence_by_entry_id (VcdObj *obj, const char entry_id[])
+{
+ CdioListNode *node;
+
+ vcd_assert (entry_id != NULL);
+ vcd_assert (obj != NULL);
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _cdio_list_node_data (node);
+ CdioListNode *node2;
+
+ /* default entry point */
+ if (_sequence->default_entry_id
+ && !strcmp (entry_id, _sequence->default_entry_id))
+ return _sequence;
+
+ /* additional entry points */
+ _CDIO_LIST_FOREACH (node2, _sequence->entry_list)
+ {
+ entry_t *_entry = _cdio_list_node_data (node2);
+
+ if (_entry->id
+ && !strcmp (entry_id, _entry->id))
+ return _sequence;
+ }
+ }
+
+ /* not found */
+
+ return NULL;
+}
+
+mpeg_segment_t *
+_vcd_obj_get_segment_by_id (VcdObj *obj, const char segment_id[])
+{
+ CdioListNode *node;
+
+ vcd_assert (segment_id != NULL);
+ vcd_assert (obj != NULL);
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _cdio_list_node_data (node);
+
+ if (_segment->id && !strcmp (segment_id, _segment->id))
+ return _segment;
+ }
+
+ return NULL;
+}
+
+bool
+_vcd_obj_has_cap_p (const VcdObj *obj, enum vcd_capability_t capability)
+{
+ switch (capability)
+ {
+ case _CAP_VALID:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return true;
+ break;
+
+ case VCD_TYPE_INVALID:
+ return false;
+ break;
+ }
+ break;
+
+ case _CAP_MPEG2:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ case VCD_TYPE_INVALID:
+ return false;
+ break;
+
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return true;
+ break;
+ }
+ break;
+
+ case _CAP_PBC:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_INVALID:
+ return false;
+ break;
+
+ case VCD_TYPE_VCD2:
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return true;
+ break;
+ }
+ break;
+
+ case _CAP_PBC_X:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_INVALID:
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return false;
+ break;
+
+ case VCD_TYPE_VCD2:
+ return true;
+ break;
+ }
+ break;
+
+ case _CAP_4C_SVCD:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_INVALID:
+ case VCD_TYPE_VCD2:
+ return false;
+ break;
+
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return true;
+ break;
+ }
+ break;
+
+ case _CAP_PAL_BITS:
+ return _vcd_obj_has_cap_p (obj, _CAP_PBC); /* for now */
+ break;
+
+ case _CAP_MPEG1:
+ return !_vcd_obj_has_cap_p (obj, _CAP_MPEG2); /* for now */
+ break;
+
+ case _CAP_TRACK_MARGINS:
+ return !_vcd_obj_has_cap_p (obj, _CAP_MPEG2); /* for now */
+ break;
+ }
+
+ vcd_assert_not_reached ();
+ return false;
+}
+
+/*
+ * public methods
+ */
+
+VcdObj *
+vcd_obj_new (vcd_type_t vcd_type)
+{
+ VcdObj *new_obj = NULL;
+ static bool _first = true;
+
+ if (_first)
+ {
+#if defined(_DEVELOPMENT_)
+ vcd_warn ("initializing libvcd %s [%s]", VERSION, HOST_ARCH);
+ vcd_warn (" ");
+ vcd_warn (" this is the UNSTABLE development branch!");
+ vcd_warn (" use only if you know what you are doing");
+ vcd_warn (" see http://www.hvrlab.org/~hvr/vcdimager/ for more information");
+ vcd_warn (" ");
+#else
+ vcd_debug ("initializing libvcd %s [%s]", VERSION, HOST_ARCH);
+#endif
+ _first = false;
+ }
+
+ new_obj = _vcd_malloc (sizeof (VcdObj));
+ new_obj->type = vcd_type;
+
+ if (!_vcd_obj_has_cap_p (new_obj, _CAP_VALID))
+ {
+ vcd_error ("VCD type not supported");
+ free (new_obj);
+ return NULL;
+ }
+
+ if (vcd_type == VCD_TYPE_VCD)
+ vcd_warn ("VCD 1.0 support is experimental -- user feedback needed!");
+
+ new_obj->iso_volume_label = strdup ("");
+ new_obj->iso_publisher_id = strdup ("");
+ new_obj->iso_application_id = strdup ("");
+ new_obj->iso_preparer_id = _vcd_strdup_upper (DEFAULT_ISO_PREPARER_ID);
+ new_obj->info_album_id = strdup ("");
+ new_obj->info_volume_count = 1;
+ new_obj->info_volume_number = 1;
+
+ new_obj->custom_file_list = _cdio_list_new ();
+ new_obj->custom_dir_list = _cdio_list_new ();
+
+
+ new_obj->mpeg_sequence_list = _cdio_list_new ();
+
+ new_obj->mpeg_segment_list = _cdio_list_new ();
+
+ new_obj->pbc_list = _cdio_list_new ();
+
+ /* gap's defined by IEC-10149 / ECMA-130 */
+
+ /* pre-gap's for tracks but the first one */
+ new_obj->track_pregap = CDIO_PREGAP_SECTORS;
+ /* post-gap after last track */
+ new_obj->leadout_pregap = CDIO_POSTGAP_SECTORS;
+
+ if (_vcd_obj_has_cap_p (new_obj, _CAP_TRACK_MARGINS))
+ {
+ new_obj->track_front_margin = 30;
+ new_obj->track_rear_margin = 45;
+ }
+ else
+ {
+ new_obj->track_front_margin = 0;
+ new_obj->track_rear_margin = 0;
+ }
+
+ return new_obj;
+}
+
+int
+vcd_obj_remove_item (VcdObj *obj, const char id[])
+{
+ vcd_warn ("vcd_obj_remove_item('%s') not implemented yet!", id);
+
+ return -1;
+}
+
+static void
+_vcd_obj_remove_mpeg_track (VcdObj *obj, int track_id)
+{
+ int length;
+ mpeg_sequence_t *track = NULL;
+ CdioListNode *node = NULL;
+
+ vcd_assert (track_id >= 0);
+
+ node = _vcd_list_at (obj->mpeg_sequence_list, track_id);
+
+ vcd_assert (node != NULL);
+
+ track = (mpeg_sequence_t *) _cdio_list_node_data (node);
+
+ vcd_mpeg_source_destroy (track->source, true);
+
+ length = track->info->packets;
+ length += obj->track_pregap + obj->track_front_margin + 0 + obj->track_rear_margin;
+
+ /* fixup offsets */
+ {
+ CdioListNode *node2 = node;
+ while ((node2 = _cdio_list_node_next (node2)) != NULL)
+ ((mpeg_sequence_t *) _cdio_list_node_data (node))->relative_start_extent -= length;
+ }
+
+ obj->relative_end_extent -= length;
+
+ /* shift up */
+ _cdio_list_node_free (node, true);
+}
+
+int
+vcd_obj_append_segment_play_item (VcdObj *obj, VcdMpegSource *mpeg_source,
+ const char item_id[])
+{
+ mpeg_segment_t *segment = NULL;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (mpeg_source != NULL);
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_PBC))
+ {
+ vcd_error ("segment play items not supported for this vcd type");
+ return -1;
+ }
+
+ if (!item_id)
+ {
+ vcd_error ("no id given for segment play item");
+ return -1;
+ }
+
+ if (_vcd_pbc_lookup (obj, item_id))
+ {
+ vcd_error ("item id (%s) exists already", item_id);
+ return -1;
+ }
+
+ vcd_info ("scanning mpeg segment item #%d for scanpoints...",
+ _cdio_list_length (obj->mpeg_segment_list));
+
+ vcd_mpeg_source_scan (mpeg_source, !obj->relaxed_aps,
+ obj->update_scan_offsets, NULL, NULL);
+
+ if (vcd_mpeg_source_get_info (mpeg_source)->packets == 0)
+ {
+ vcd_error ("mpeg is empty?");
+ return -1;
+ }
+
+ /* create list node */
+
+ segment = _vcd_malloc (sizeof (mpeg_sequence_t));
+
+ segment->source = mpeg_source;
+
+ segment->id = strdup (item_id);
+
+ segment->info = vcd_mpeg_source_get_info (mpeg_source);
+ segment->segment_count = _vcd_len2blocks (segment->info->packets, 150);
+
+ segment->pause_list = _cdio_list_new ();
+
+ vcd_debug ("SPI length is %d sector(s), allocated %d segment(s)",
+ segment->info->packets,
+ segment->segment_count);
+
+ _cdio_list_append (obj->mpeg_segment_list, segment);
+
+ return 0;
+}
+
+int
+vcd_obj_append_sequence_play_item (VcdObj *obj, VcdMpegSource *mpeg_source,
+ const char item_id[],
+ const char default_entry_id[])
+{
+ unsigned length;
+ mpeg_sequence_t *sequence = NULL;
+ int track_no = _cdio_list_length (obj->mpeg_sequence_list);
+
+ vcd_assert (obj != NULL);
+ vcd_assert (mpeg_source != NULL);
+
+ if (item_id && _vcd_pbc_lookup (obj, item_id))
+ {
+ vcd_error ("item id (%s) exist already", item_id);
+ return -1;
+ }
+
+ if (default_entry_id && _vcd_pbc_lookup (obj, default_entry_id))
+ {
+ vcd_error ("default entry id (%s) exist already", default_entry_id);
+ return -1;
+ }
+
+ if (default_entry_id && item_id && !strcmp (item_id, default_entry_id))
+ {
+ vcd_error ("default entry id == item id (%s)", item_id);
+ return -1;
+ }
+
+ vcd_info ("scanning mpeg sequence item #%d for scanpoints...", track_no);
+ vcd_mpeg_source_scan (mpeg_source, !obj->relaxed_aps,
+ obj->update_scan_offsets, NULL, NULL);
+
+ sequence = _vcd_malloc (sizeof (mpeg_sequence_t));
+
+ sequence->source = mpeg_source;
+
+ if (item_id)
+ sequence->id = strdup (item_id);
+
+ if (default_entry_id)
+ sequence->default_entry_id = strdup (default_entry_id);
+
+ sequence->info = vcd_mpeg_source_get_info (mpeg_source);
+ length = sequence->info->packets;
+
+ sequence->entry_list = _cdio_list_new ();
+ sequence->pause_list = _cdio_list_new ();
+
+ obj->relative_end_extent += obj->track_pregap;
+ sequence->relative_start_extent = obj->relative_end_extent;
+
+ obj->relative_end_extent += obj->track_front_margin + length + obj->track_rear_margin;
+
+ /* sanity checks */
+
+ if (length < 75)
+ vcd_warn ("mpeg stream shorter than 75 sectors");
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_PAL_BITS)
+ && vcd_mpeg_get_norm (&sequence->info->shdr[0]) != MPEG_NORM_FILM
+ && vcd_mpeg_get_norm (&sequence->info->shdr[0]) != MPEG_NORM_NTSC)
+ vcd_warn ("VCD 1.x should contain only NTSC/FILM video (may work with PAL nevertheless)");
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_MPEG1)
+ && sequence->info->version == MPEG_VERS_MPEG1)
+ vcd_warn ("this VCD type should not contain MPEG1 streams");
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_MPEG2)
+ && sequence->info->version == MPEG_VERS_MPEG2)
+ vcd_warn ("this VCD type should not contain MPEG2 streams");
+
+ if (!sequence->info->shdr[0].seen
+ || sequence->info->shdr[1].seen
+ || sequence->info->shdr[2].seen)
+ vcd_warn ("sequence items should contain a motion video stream!");
+
+ {
+ int i;
+
+ for (i = 0; i < 3; i++)
+ {
+ if (sequence->info->ahdr[i].seen)
+ {
+ if (i && !_vcd_obj_has_cap_p (obj, _CAP_MPEG2))
+ vcd_warn ("audio stream #%d not supported by this VCD type", i);
+
+ if (sequence->info->ahdr[i].sampfreq != 44100)
+ vcd_warn ("audio stream #%d has sampling frequency %d Hz (should be 44100 Hz)",
+ i, sequence->info->ahdr[i].sampfreq);
+
+ if (sequence->info->ahdr[i].layer != 2)
+ vcd_warn ("audio stream #%d is not layer II", i);
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_MPEG1)
+ && sequence->info->ahdr[i].bitrate != 224*1024)
+ vcd_warn ("audio stream #%d has bitrate %d kbps (should be 224 kbps for this vcd type)",
+ i, sequence->info->ahdr[i].bitrate);
+ }
+ else if (!i && !_vcd_obj_has_cap_p (obj, _CAP_MPEG2))
+ {
+ vcd_warn ("this VCD type requires an audio stream to be present");
+ }
+ }
+ }
+
+ /* vcd_debug ("track# %d's detected playing time: %.2f seconds", */
+ /* track_no, sequence->info->playing_time); */
+
+ _cdio_list_append (obj->mpeg_sequence_list, sequence);
+
+ return track_no;
+}
+
+static int
+_pause_cmp (pause_t *ent1, pause_t *ent2)
+{
+ if (ent1->time < ent2->time)
+ return -1;
+
+ if (ent1->time > ent2->time)
+ return 1;
+
+ return 0;
+}
+
+int
+vcd_obj_add_sequence_pause (VcdObj *obj, const char sequence_id[],
+ double pause_time, const char pause_id[])
+{
+ mpeg_sequence_t *_sequence;
+
+ vcd_assert (obj != NULL);
+
+ if (sequence_id)
+ _sequence = _vcd_obj_get_sequence_by_id (obj, sequence_id);
+ else
+ _sequence =
+ _cdio_list_node_data (_cdio_list_end (obj->mpeg_sequence_list));
+
+ if (!_sequence)
+ {
+ vcd_error ("sequence id `%s' not found", sequence_id);
+ return -1;
+ }
+
+ if (pause_id)
+ vcd_warn ("pause id ignored...");
+
+ {
+ pause_t *_pause = _vcd_malloc (sizeof (pause_t));
+
+ if (pause_id)
+ _pause->id = strdup (pause_id);
+ _pause->time = pause_time;
+
+ _cdio_list_append (_sequence->pause_list, _pause);
+ }
+
+ _vcd_list_sort (_sequence->pause_list,
+ (_cdio_list_cmp_func) _pause_cmp);
+
+ vcd_debug ("added autopause point at %f", pause_time);
+
+ return 0;
+}
+
+int
+vcd_obj_add_segment_pause (VcdObj *obj, const char segment_id[],
+ double pause_time, const char pause_id[])
+{
+ mpeg_segment_t *_segment;
+
+ vcd_assert (obj != NULL);
+
+ if (segment_id)
+ _segment = _vcd_obj_get_segment_by_id (obj, segment_id);
+ else
+ _segment = _cdio_list_node_data (_cdio_list_end (obj->mpeg_segment_list));
+
+ if (!_segment)
+ {
+ vcd_error ("segment id `%s' not found", segment_id);
+ return -1;
+ }
+
+ if (pause_id)
+ vcd_warn ("pause id ignored...");
+
+ {
+ pause_t *_pause = _vcd_malloc (sizeof (pause_t));
+
+ if (pause_id)
+ _pause->id = strdup (pause_id);
+ _pause->time = pause_time;
+
+ _cdio_list_append (_segment->pause_list, _pause);
+ }
+
+ _vcd_list_sort (_segment->pause_list,
+ (_cdio_list_cmp_func) _pause_cmp);
+
+ vcd_debug ("added autopause point at %f", pause_time);
+
+ return 0;
+}
+
+static int
+_entry_cmp (entry_t *ent1, entry_t *ent2)
+{
+ if (ent1->time < ent2->time)
+ return -1;
+
+ if (ent1->time > ent2->time)
+ return 1;
+
+ return 0;
+}
+
+int
+vcd_obj_add_sequence_entry (VcdObj *obj, const char sequence_id[],
+ double entry_time, const char entry_id[])
+{
+ mpeg_sequence_t *_sequence;
+
+ vcd_assert (obj != NULL);
+
+ if (sequence_id)
+ _sequence = _vcd_obj_get_sequence_by_id (obj, sequence_id);
+ else
+ _sequence =
+ _cdio_list_node_data (_cdio_list_end (obj->mpeg_sequence_list));
+
+ if (!_sequence)
+ {
+ vcd_error ("sequence id `%s' not found", sequence_id);
+ return -1;
+ }
+
+ if (_cdio_list_length (_sequence->entry_list) >= MAX_SEQ_ENTRIES)
+ {
+ vcd_error ("only %d entries per sequence allowed!", MAX_SEQ_ENTRIES);
+ return -1;
+ }
+
+ if (entry_id && _vcd_pbc_lookup (obj, entry_id))
+ {
+ vcd_error ("item id (%s) exists already", entry_id);
+ return -1;
+ }
+
+ {
+ entry_t *_entry = _vcd_malloc (sizeof (entry_t));
+
+ if (entry_id)
+ _entry->id = strdup (entry_id);
+ _entry->time = entry_time;
+
+ _cdio_list_append (_sequence->entry_list, _entry);
+ }
+
+ _vcd_list_sort (_sequence->entry_list,
+ (_cdio_list_cmp_func) _entry_cmp);
+
+ return 0;
+}
+
+void
+vcd_obj_destroy (VcdObj *obj)
+{
+ CdioListNode *node;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (!obj->in_output);
+
+ free (obj->iso_volume_label);
+ free (obj->iso_application_id);
+
+ _CDIO_LIST_FOREACH (node, obj->custom_file_list)
+ {
+ custom_file_t *p = _cdio_list_node_data (node);
+
+ free (p->iso_pathname);
+ }
+
+ _cdio_list_free (obj->custom_file_list, true);
+
+ _cdio_list_free (obj->custom_dir_list, true);
+
+ while (_cdio_list_length (obj->mpeg_sequence_list))
+ _vcd_obj_remove_mpeg_track (obj, 0);
+ _cdio_list_free (obj->mpeg_sequence_list, true);
+
+ free (obj);
+}
+
+int
+vcd_obj_set_param_uint (VcdObj *obj, vcd_parm_t param, unsigned arg)
+{
+ vcd_assert (obj != NULL);
+
+ switch (param)
+ {
+ case VCD_PARM_VOLUME_COUNT:
+ obj->info_volume_count = arg;
+ if (!IN (obj->info_volume_count, 1, 65535))
+ {
+ obj->info_volume_count = CLAMP (obj->info_volume_count, 1, 65535);
+ vcd_warn ("volume count out of range, clamping to range");
+ }
+ vcd_debug ("changed volume count to %u", obj->info_volume_count);
+ break;
+
+ case VCD_PARM_VOLUME_NUMBER:
+ obj->info_volume_number = arg;
+ if (!IN (obj->info_volume_number, 0, 65534))
+ {
+ obj->info_volume_number = CLAMP (obj->info_volume_number, 0, 65534);
+ vcd_warn ("volume number out of range, clamping to range");
+ }
+ vcd_debug ("changed volume number to %u", obj->info_volume_number);
+ break;
+
+ case VCD_PARM_RESTRICTION:
+ obj->info_restriction = arg;
+ if (!IN (obj->info_restriction, 0, 3))
+ {
+ obj->info_restriction = CLAMP (obj->info_restriction, 0, 65534);
+ vcd_warn ("restriction out of range, clamping to range");
+ }
+ vcd_debug ("changed restriction number to %u", obj->info_restriction);
+ break;
+
+ case VCD_PARM_LEADOUT_PREGAP:
+ obj->leadout_pregap = arg;
+ if (!IN (obj->leadout_pregap, 0, 300))
+ {
+ obj->leadout_pregap = CLAMP (obj->leadout_pregap, 0, 300);
+ vcd_warn ("ledout pregap out of range, clamping to allowed range");
+ }
+ if (obj->leadout_pregap < CDIO_PREGAP_SECTORS)
+ vcd_warn ("track leadout pregap set below %d sectors; created (s)vcd may be non-working",
+ CDIO_PREGAP_SECTORS);
+
+ vcd_debug ("changed leadout pregap to %u", obj->leadout_pregap);
+ break;
+
+ case VCD_PARM_TRACK_PREGAP:
+ obj->track_pregap = arg;
+ if (!IN (obj->track_pregap, 1, 300))
+ {
+ obj->track_pregap = CLAMP (obj->track_pregap, 1, 300);
+ vcd_warn ("track pregap out of range, clamping to allowed range");
+ }
+ if (obj->track_pregap < CDIO_PREGAP_SECTORS)
+ vcd_warn ("track pre gap set below %d sectors; created (S)VCD may be non-working",
+ CDIO_PREGAP_SECTORS);
+ vcd_debug ("changed track pregap to %u", obj->track_pregap);
+ break;
+
+ case VCD_PARM_TRACK_FRONT_MARGIN:
+ obj->track_front_margin = arg;
+ if (!IN (obj->track_front_margin, 0, CDIO_PREGAP_SECTORS))
+ {
+ obj->track_front_margin = CLAMP (obj->track_front_margin, 0,
+ CDIO_PREGAP_SECTORS);
+ vcd_warn ("front margin out of range, clamping to allowed range");
+ }
+ if (_vcd_obj_has_cap_p (obj, _CAP_TRACK_MARGINS)
+ && obj->track_front_margin < 15)
+ vcd_warn ("front margin set smaller than recommended (%d < 15 sectors) for disc type used",
+ obj->track_front_margin);
+
+ vcd_debug ("changed front margin to %u", obj->track_front_margin);
+ break;
+
+ case VCD_PARM_TRACK_REAR_MARGIN:
+ obj->track_rear_margin = arg;
+ if (!IN (obj->track_rear_margin, 0, CDIO_POSTGAP_SECTORS))
+ {
+ obj->track_rear_margin = CLAMP (obj->track_rear_margin, 0,
+ CDIO_POSTGAP_SECTORS);
+ vcd_warn ("rear margin out of range, clamping to allowed range");
+ }
+ if (_vcd_obj_has_cap_p (obj, _CAP_TRACK_MARGINS)
+ && obj->track_rear_margin < 15)
+ vcd_warn ("rear margin set smaller than recommended (%d < 15 sectors) for disc type used",
+ obj->track_rear_margin);
+ vcd_debug ("changed rear margin to %u", obj->track_rear_margin);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return 0;
+}
+
+int
+vcd_obj_set_param_str (VcdObj *obj, vcd_parm_t param, const char *arg)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (arg != NULL);
+
+ switch (param)
+ {
+ case VCD_PARM_VOLUME_ID:
+ free (obj->iso_volume_label);
+ obj->iso_volume_label = strdup (arg);
+ if (strlen (obj->iso_volume_label) > 32)
+ {
+ obj->iso_volume_label[32] = '\0';
+ vcd_warn ("Volume label too long, will be truncated");
+ }
+ vcd_debug ("changed volume label to `%s'", obj->iso_volume_label);
+ break;
+
+ case VCD_PARM_PUBLISHER_ID:
+ free (obj->iso_publisher_id);
+ obj->iso_publisher_id = strdup (arg);
+ if (strlen (obj->iso_publisher_id) > 128)
+ {
+ obj->iso_publisher_id[128] = '\0';
+ vcd_warn ("Publisher ID too long, will be truncated");
+ }
+ vcd_debug ("changed publisher id to `%s'", obj->iso_publisher_id);
+ break;
+
+ case VCD_PARM_PREPARER_ID:
+ free (obj->iso_preparer_id);
+ obj->iso_preparer_id = strdup (arg);
+ if (strlen (obj->iso_preparer_id) > 128)
+ {
+ obj->iso_preparer_id[128] = '\0';
+ vcd_warn ("Preparer ID too long, will be truncated");
+ }
+ vcd_debug ("changed preparer id to `%s'", obj->iso_preparer_id);
+ break;
+
+ case VCD_PARM_APPLICATION_ID:
+ free (obj->iso_application_id);
+ obj->iso_application_id = strdup (arg);
+ if (strlen (obj->iso_application_id) > 128)
+ {
+ obj->iso_application_id[128] = '\0';
+ vcd_warn ("Application ID too long, will be truncated");
+ }
+ vcd_debug ("changed application id to `%s'", obj->iso_application_id);
+ break;
+
+ case VCD_PARM_ALBUM_ID:
+ free (obj->info_album_id);
+ obj->info_album_id = strdup (arg);
+ if (strlen (obj->info_album_id) > 16)
+ {
+ obj->info_album_id[16] = '\0';
+ vcd_warn ("Album ID too long, will be truncated");
+ }
+ vcd_debug ("changed album id to `%s'", obj->info_album_id);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return 0;
+}
+
+int
+vcd_obj_set_param_bool (VcdObj *obj, vcd_parm_t param, bool arg)
+{
+ vcd_assert (obj != NULL);
+
+ switch (param)
+ {
+ case VCD_PARM_RELAXED_APS:
+ obj->relaxed_aps = arg ? true : false;
+ vcd_debug ("changing 'relaxed aps' to %d", obj->relaxed_aps);
+ break;
+
+ case VCD_PARM_NEXT_VOL_LID2:
+ obj->info_use_lid2 = arg ? true : false;
+ vcd_debug ("changing 'next volume use lid 2' to %d", obj->info_use_lid2);
+ break;
+
+ case VCD_PARM_NEXT_VOL_SEQ2:
+ obj->info_use_seq2 = arg ? true : false;
+ vcd_debug ("changing 'next volume use sequence 2' to %d", obj->info_use_seq2);
+ break;
+
+ case VCD_PARM_SVCD_VCD3_MPEGAV:
+ if (obj->type == VCD_TYPE_SVCD)
+ {
+ if ((obj->svcd_vcd3_mpegav = arg ? true : false))
+ vcd_warn ("!! enabling deprecated VCD3.0 MPEGAV folder --"
+ " SVCD will not be IEC62107 compliant !!");
+ }
+ else
+ vcd_error ("parameter not applicable for vcd type");
+ break;
+
+ case VCD_PARM_SVCD_VCD3_ENTRYSVD:
+ if (obj->type == VCD_TYPE_SVCD)
+ {
+ if ((obj->svcd_vcd3_entrysvd = arg ? true : false))
+ vcd_warn ("!! enabling deprecated VCD3.0 ENTRYSVD signature --"
+ " SVCD will not be IEC62107 compliant !!");
+ }
+ else
+ vcd_error ("parameter not applicable for vcd type");
+ break;
+
+ case VCD_PARM_SVCD_VCD3_TRACKSVD:
+ if (obj->type == VCD_TYPE_SVCD)
+ {
+ if ((obj->svcd_vcd3_tracksvd = arg ? true : false))
+ vcd_warn ("!! enabling deprecated VCD3.0 TRACK.SVD format --"
+ " SVCD will not be IEC62107 compliant !!");
+ }
+ else
+ vcd_error ("parameter not applicable for vcd type");
+ break;
+
+ case VCD_PARM_UPDATE_SCAN_OFFSETS:
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ obj->update_scan_offsets = arg ? true : false;
+ vcd_debug ("changing 'update scan offsets' to %d", obj->update_scan_offsets);
+ }
+ else
+ vcd_error ("parameter not applicable for vcd type");
+ break;
+
+ case VCD_PARM_LEADOUT_PAUSE:
+ vcd_warn ("use of 'leadout pause' is deprecated and may be removed in later releases;"
+ " use 'leadout pregap' instead");
+ vcd_obj_set_param_uint (obj, VCD_PARM_LEADOUT_PREGAP,
+ (arg ? CDIO_PREGAP_SECTORS : 0));
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return 0;
+}
+
+int
+vcd_obj_add_dir (VcdObj *obj, const char iso_pathname[])
+{
+ char *_iso_pathname;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (iso_pathname != NULL);
+
+ _iso_pathname = _vcd_strdup_upper (iso_pathname);
+
+ if (!iso9660_dirname_valid_p (_iso_pathname))
+ {
+ vcd_error("pathname `%s' is not a valid iso pathname",
+ _iso_pathname);
+ free (_iso_pathname);
+ return 1;
+ }
+
+ _cdio_list_append (obj->custom_dir_list, _iso_pathname);
+
+ _vcd_list_sort (obj->custom_dir_list,
+ (_cdio_list_cmp_func) strcmp);
+
+ return 0;
+}
+
+int
+vcd_obj_add_file (VcdObj *obj, const char iso_pathname[],
+ VcdDataSource *file, bool raw_flag)
+{
+ uint32_t size = 0, sectors = 0;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (file != NULL);
+ vcd_assert (iso_pathname != NULL);
+ vcd_assert (strlen (iso_pathname) > 0);
+ vcd_assert (file != NULL);
+
+ size = vcd_data_source_stat (file);
+
+ /* close file to save file descriptors */
+ vcd_data_source_close (file);
+
+ if (raw_flag)
+ {
+ if (!size)
+ {
+ vcd_error("raw mode2 file must not be empty\n");
+ return 1;
+ }
+
+ sectors = size / M2RAW_SECTOR_SIZE;
+
+ if (size % M2RAW_SECTOR_SIZE)
+ {
+ vcd_error("raw mode2 file must have size multiple of %d \n",
+ M2RAW_SECTOR_SIZE);
+ return 1;
+ }
+ }
+ else
+ sectors = _vcd_len2blocks (size, CDIO_CD_FRAMESIZE);
+
+ {
+ custom_file_t *p;
+ char *_iso_pathname = _vcd_strdup_upper (iso_pathname);
+
+ if (!iso9660_pathname_valid_p (_iso_pathname))
+ {
+ vcd_error("pathname `%s' is not a valid iso pathname",
+ _iso_pathname);
+ free (_iso_pathname);
+ return 1;
+ }
+
+ p = _vcd_malloc (sizeof (custom_file_t));
+
+ p->file = file;
+ p->iso_pathname = _iso_pathname;
+ p->raw_flag = raw_flag;
+
+ p->size = size;
+ p->start_extent = 0;
+ p->sectors = sectors;
+
+ _cdio_list_append (obj->custom_file_list, p);
+ }
+
+ return 0;
+}
+
+static void
+_finalize_vcd_iso_track_allocation (VcdObj *obj)
+{
+ int n;
+ CdioListNode *node;
+
+ uint32_t dir_secs = SECTOR_NIL;
+
+ _dict_clean (obj);
+
+ /* pre-alloc 16 blocks of ISO9660 required silence */
+ if (_vcd_salloc (obj->iso_bitmap, 0, 16) == SECTOR_NIL)
+ vcd_assert_not_reached ();
+
+ /* keep karaoke sectors blank -- well... guess I'm too paranoid :) */
+ if (_vcd_salloc (obj->iso_bitmap, 75, 75) == SECTOR_NIL)
+ vcd_assert_not_reached ();
+
+ /* pre-alloc descriptors, PVD */
+ _dict_insert (obj, "pvd", ISO_PVD_SECTOR, 1, SM_EOR); /* EOR */
+ /* EVD */
+ _dict_insert (obj, "evd", ISO_EVD_SECTOR, 1, SM_EOR|SM_EOF); /* EOR+EOF */
+
+ /* reserve for iso directory */
+ dir_secs = _vcd_salloc (obj->iso_bitmap, 18, 75-18);
+
+ /* VCD information area */
+
+ _dict_insert (obj, "info", INFO_VCD_SECTOR, 1, SM_EOF); /* INFO.VCD */ /* EOF */
+ _dict_insert (obj, "entries", ENTRIES_VCD_SECTOR, 1, SM_EOF); /* ENTRIES.VCD */ /* EOF */
+
+ /* PBC */
+
+ if (_vcd_pbc_available (obj))
+ {
+ _dict_insert (obj, "lot", LOT_VCD_SECTOR, LOT_VCD_SIZE, SM_EOF); /* LOT.VCD */ /* EOF */
+ _dict_insert (obj, "psd", PSD_VCD_SECTOR,
+ _vcd_len2blocks (get_psd_size (obj, false), ISO_BLOCKSIZE), SM_EOF); /* PSD.VCD */ /* EOF */
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ _dict_insert (obj, "tracks", SECTOR_NIL, 1, SM_EOF); /* TRACKS.SVD */
+ _dict_insert (obj, "search", SECTOR_NIL,
+ _vcd_len2blocks (get_search_dat_size (obj), ISO_BLOCKSIZE), SM_EOF); /* SEARCH.DAT */
+
+ vcd_assert (_dict_get_bykey (obj, "tracks")->sector > INFO_VCD_SECTOR);
+ vcd_assert (_dict_get_bykey (obj, "search")->sector > INFO_VCD_SECTOR);
+ }
+
+ /* done with primary information area */
+
+ obj->mpeg_segment_start_extent =
+ _vcd_len2blocks (_vcd_salloc_get_highest (obj->iso_bitmap) + 1, 75) * 75;
+
+ /* salloc up to end of vcd sector */
+ for(n = 0;n < obj->mpeg_segment_start_extent;n++)
+ _vcd_salloc (obj->iso_bitmap, n, 1);
+
+ vcd_assert (_vcd_salloc_get_highest (obj->iso_bitmap) + 1 == obj->mpeg_segment_start_extent);
+
+ /* insert segments */
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _cdio_list_node_data (node);
+
+ _segment->start_extent =
+ _vcd_salloc (obj->iso_bitmap, SECTOR_NIL,
+ _segment->segment_count * VCDINFO_SEGMENT_SECTOR_SIZE);
+
+ vcd_assert (_segment->start_extent % 75 == 0);
+ vcd_assert (_vcd_salloc_get_highest (obj->iso_bitmap) + 1
+ == _segment->start_extent
+ + _segment->segment_count * VCDINFO_SEGMENT_SECTOR_SIZE);
+ }
+
+ obj->ext_file_start_extent = _vcd_salloc_get_highest (obj->iso_bitmap) + 1;
+
+ vcd_assert (obj->ext_file_start_extent % 75 == 0);
+
+ /* go on with EXT area */
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ _dict_insert (obj, "scandata", SECTOR_NIL,
+ _vcd_len2blocks (get_scandata_dat_size (obj),
+ ISO_BLOCKSIZE),
+ SM_EOF);
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X)
+ &&_vcd_pbc_available (obj))
+ {
+ _dict_insert (obj, "lot_x", SECTOR_NIL, LOT_VCD_SIZE, SM_EOF);
+
+ _dict_insert (obj, "psd_x", SECTOR_NIL,
+ _vcd_len2blocks (get_psd_size (obj, true), ISO_BLOCKSIZE),
+ SM_EOF);
+ }
+
+
+ obj->custom_file_start_extent =
+ _vcd_salloc_get_highest (obj->iso_bitmap) + 1;
+
+ /* now for the custom files */
+
+ _CDIO_LIST_FOREACH (node, obj->custom_file_list)
+ {
+ custom_file_t *p = _cdio_list_node_data (node);
+
+ if (p->sectors)
+ {
+ p->start_extent =
+ _vcd_salloc(obj->iso_bitmap, SECTOR_NIL, p->sectors);
+ vcd_assert (p->start_extent != SECTOR_NIL);
+ }
+ else /* zero sized files -- set dummy extent */
+ p->start_extent = obj->custom_file_start_extent;
+ }
+
+ /* calculate iso size -- after this point no sector shall be
+ allocated anymore */
+
+ obj->iso_size =
+ MAX (MIN_ISO_SIZE, _vcd_salloc_get_highest (obj->iso_bitmap) + 1);
+
+ vcd_debug ("iso9660: highest alloced sector is %lu (using %d as isosize)",
+ (unsigned long int) _vcd_salloc_get_highest (obj->iso_bitmap),
+ obj->iso_size);
+
+ /* after this point the ISO9660's size is frozen */
+}
+
+static void
+_finalize_vcd_iso_track_filesystem (VcdObj *obj)
+{
+ int n;
+ CdioListNode *node;
+
+ /* create filesystem entries */
+
+ switch (obj->type) {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ /* add only necessary directories! */
+ /* _vcd_directory_mkdir (obj->dir, "CDDA"); */
+ /* _vcd_directory_mkdir (obj->dir, "CDI"); */
+ _vcd_directory_mkdir (obj->dir, "EXT");
+ /* _vcd_directory_mkdir (obj->dir, "KARAOKE"); */
+ _vcd_directory_mkdir (obj->dir, "MPEGAV");
+ _vcd_directory_mkdir (obj->dir, "VCD");
+
+ /* add segment dir only when there are actually segment play items */
+ if (_cdio_list_length (obj->mpeg_segment_list))
+ _vcd_directory_mkdir (obj->dir, "SEGMENT");
+
+ _vcd_directory_mkfile (obj->dir, "VCD/ENTRIES.VCD",
+ _dict_get_bykey (obj, "entries")->sector,
+ ISO_BLOCKSIZE, false, 0);
+ _vcd_directory_mkfile (obj->dir, "VCD/INFO.VCD",
+ _dict_get_bykey (obj, "info")->sector,
+ ISO_BLOCKSIZE, false, 0);
+
+ /* only for vcd2.0 */
+ if (_vcd_pbc_available (obj))
+ {
+ _vcd_directory_mkfile (obj->dir, "VCD/LOT.VCD",
+ _dict_get_bykey (obj, "lot")->sector,
+ ISO_BLOCKSIZE*LOT_VCD_SIZE, false, 0);
+ _vcd_directory_mkfile (obj->dir, "VCD/PSD.VCD",
+ _dict_get_bykey (obj, "psd")->sector,
+ get_psd_size (obj, false), false, 0);
+ }
+ break;
+
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ _vcd_directory_mkdir (obj->dir, "EXT");
+
+ if (!obj->svcd_vcd3_mpegav)
+ _vcd_directory_mkdir (obj->dir, "MPEG2");
+ else
+ {
+ vcd_warn ("adding MPEGAV dir for *DEPRECATED* SVCD VCD30 mode");
+ _vcd_directory_mkdir (obj->dir, "MPEGAV");
+ }
+
+ /* add segment dir only when there are actually segment play items */
+ if (_cdio_list_length (obj->mpeg_segment_list))
+ _vcd_directory_mkdir (obj->dir, "SEGMENT");
+
+ _vcd_directory_mkdir (obj->dir, "SVCD");
+
+ _vcd_directory_mkfile (obj->dir, "SVCD/ENTRIES.SVD",
+ _dict_get_bykey (obj, "entries")->sector,
+ ISO_BLOCKSIZE, false, 0);
+ _vcd_directory_mkfile (obj->dir, "SVCD/INFO.SVD",
+ _dict_get_bykey (obj, "info")->sector,
+ ISO_BLOCKSIZE, false, 0);
+
+ if (_vcd_pbc_available (obj))
+ {
+ _vcd_directory_mkfile (obj->dir, "SVCD/LOT.SVD",
+ _dict_get_bykey (obj, "lot")->sector,
+ ISO_BLOCKSIZE*LOT_VCD_SIZE, false, 0);
+ _vcd_directory_mkfile (obj->dir, "SVCD/PSD.SVD",
+ _dict_get_bykey (obj, "psd")->sector,
+ get_psd_size (obj, false), false, 0);
+ }
+
+ _vcd_directory_mkfile (obj->dir, "SVCD/SEARCH.DAT",
+ _dict_get_bykey (obj, "search")->sector,
+ get_search_dat_size (obj), false, 0);
+ _vcd_directory_mkfile (obj->dir, "SVCD/TRACKS.SVD",
+ _dict_get_bykey (obj, "tracks")->sector,
+ ISO_BLOCKSIZE, false, 0);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ /* SEGMENTS */
+
+ n = 1;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *segment = _cdio_list_node_data (node);
+ char segment_pathname[128] = { 0, };
+ const char *fmt = NULL;
+ uint8_t fnum = 0;
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD2:
+ fmt = "SEGMENT/ITEM%4.4d.DAT";
+ fnum = 1;
+ break;
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ fmt = "SEGMENT/ITEM%4.4d.MPG";
+ fnum = 0;
+ break;
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ snprintf (segment_pathname, sizeof (segment_pathname), fmt, n);
+
+ _vcd_directory_mkfile (obj->dir, segment_pathname, segment->start_extent,
+ segment->info->packets * ISO_BLOCKSIZE,
+ true, fnum);
+
+ vcd_assert (n <= MAX_SEGMENTS);
+
+ n += segment->segment_count;
+ }
+
+ /* EXT files */
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X)
+ &&_vcd_pbc_available (obj))
+ {
+ /* psd_x -- extended PSD */
+ _vcd_directory_mkfile (obj->dir, "EXT/PSD_X.VCD",
+ _dict_get_bykey (obj, "psd_x")->sector,
+ get_psd_size (obj, true), false, 1);
+
+ /* lot_x -- extended LOT */
+ _vcd_directory_mkfile (obj->dir, "EXT/LOT_X.VCD",
+ _dict_get_bykey (obj, "lot_x")->sector,
+ ISO_BLOCKSIZE*LOT_VCD_SIZE, false, 1);
+
+ vcd_assert (obj->type == VCD_TYPE_VCD2);
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ /* scandata.dat -- scanpoints */
+ _vcd_directory_mkfile (obj->dir, "EXT/SCANDATA.DAT",
+ _dict_get_bykey (obj, "scandata")->sector,
+ get_scandata_dat_size (obj), false, 0);
+ }
+
+ /* custom files/dirs */
+ _CDIO_LIST_FOREACH (node, obj->custom_dir_list)
+ {
+ char *p = _cdio_list_node_data (node);
+ _vcd_directory_mkdir (obj->dir, p);
+ }
+
+ _CDIO_LIST_FOREACH (node, obj->custom_file_list)
+ {
+ custom_file_t *p = _cdio_list_node_data (node);
+
+ _vcd_directory_mkfile (obj->dir, p->iso_pathname, p->start_extent,
+ (p->raw_flag
+ ? (ISO_BLOCKSIZE * (p->size / M2RAW_SECTOR_SIZE))
+ : p->size),
+ p->raw_flag, 1);
+ }
+
+
+ n = 0;
+ _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ char avseq_pathname[128] = { 0, };
+ const char *fmt = NULL;
+ mpeg_sequence_t *_sequence = _cdio_list_node_data (node);
+ uint32_t extent = _sequence->relative_start_extent;
+ uint8_t file_num = 0;
+
+ extent += obj->iso_size;
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ fmt = "MPEGAV/MUSIC%2.2d.DAT";
+ file_num = n + 1;
+ break;
+
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ fmt = "MPEGAV/AVSEQ%2.2d.DAT";
+ file_num = n + 1;
+ break;
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ fmt = "MPEG2/AVSEQ%2.2d.MPG";
+ file_num = 0;
+
+ /* if vcd3 compat mode, override */
+ if (obj->svcd_vcd3_mpegav)
+ {
+ fmt = "MPEGAV/AVSEQ%2.2d.MPG";
+ file_num = n + 1;
+ }
+
+ break;
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ vcd_assert (n < 98);
+
+ snprintf (avseq_pathname, sizeof (avseq_pathname), fmt, n + 1);
+
+ /* file entry contains front margin, mpeg stream and rear margin */
+ _vcd_directory_mkfile (obj->dir, avseq_pathname, extent,
+ (obj->track_front_margin
+ + _sequence->info->packets
+ + obj->track_rear_margin) * ISO_BLOCKSIZE,
+ true, file_num);
+
+ n++;
+ }
+
+ /* register isofs dir structures */
+ {
+ uint32_t dirs_size = _vcd_directory_get_size (obj->dir);
+
+ /* be sure to stay out of information areas */
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ /* karaoke area starts at 03:00 */
+ if (16 + 2 + dirs_size + 2 >= 75)
+ vcd_error ("directory section to big for a VCD");
+ break;
+
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ /* since no karaoke exists the next fixed area starts at 04:00 */
+ if (16 + 2 + dirs_size + 2 >= 150)
+ vcd_error ("directory section to big for a SVCD");
+ break;
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ /* un-alloc small area */
+
+ _vcd_salloc_free (obj->iso_bitmap, 18, dirs_size + 2);
+
+ /* alloc it again! */
+
+ _dict_insert (obj, "dir", 18, dirs_size, SM_EOR|SM_EOF);
+ _dict_insert (obj, "ptl", 18 + dirs_size, 1, SM_EOR|SM_EOF);
+ _dict_insert (obj, "ptm", 18 + dirs_size + 1, 1, SM_EOR|SM_EOF);
+ }
+}
+
+static void
+_finalize_vcd_iso_track (VcdObj *obj)
+{
+ _vcd_pbc_finalize (obj);
+ _finalize_vcd_iso_track_allocation (obj);
+ _finalize_vcd_iso_track_filesystem (obj);
+}
+
+static int
+_callback_wrapper (VcdObj *obj, int force)
+{
+ const int cb_frequency = 75;
+
+ if (obj->last_cb_call + cb_frequency > obj->sectors_written && !force)
+ return 0;
+
+ obj->last_cb_call = obj->sectors_written;
+
+ if (obj->progress_callback) {
+ progress_info_t _pi;
+
+ _pi.sectors_written = obj->sectors_written;
+ _pi.total_sectors = obj->relative_end_extent + obj->iso_size;
+ _pi.in_track = obj->in_track;
+ _pi.total_tracks = _cdio_list_length (obj->mpeg_sequence_list) + 1;
+
+ return obj->progress_callback (&_pi, obj->callback_user_data);
+ }
+ else
+ return 0;
+}
+
+static int
+_write_m2_image_sector (VcdObj *obj, const void *data, uint32_t extent,
+ uint8_t fnum, uint8_t cnum, uint8_t sm, uint8_t ci)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ vcd_assert (extent == obj->sectors_written);
+
+ _vcd_make_mode2(buf, data, extent, fnum, cnum, sm, ci);
+
+ vcd_image_sink_write (obj->image_sink, buf, extent);
+
+ obj->sectors_written++;
+
+ return _callback_wrapper (obj, false);
+}
+
+static int
+_write_m2_raw_image_sector (VcdObj *obj, const void *data, uint32_t extent)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ vcd_assert (extent == obj->sectors_written);
+
+ _vcd_make_raw_mode2(buf, data, extent);
+
+ vcd_image_sink_write (obj->image_sink, buf, extent);
+
+ obj->sectors_written++;
+
+ return _callback_wrapper (obj, false);
+}
+
+static void
+_write_source_mode2_raw (VcdObj *obj, VcdDataSource *source, uint32_t extent)
+{
+ int n;
+ uint32_t sectors;
+
+ sectors = vcd_data_source_stat (source) / M2RAW_SECTOR_SIZE;
+
+ vcd_data_source_seek (source, 0);
+
+ for (n = 0;n < sectors;n++) {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+
+ vcd_data_source_read (source, buf, M2RAW_SECTOR_SIZE, 1);
+
+ if (_write_m2_raw_image_sector (obj, buf, extent+n))
+ break;
+ }
+
+ vcd_data_source_close (source);
+}
+
+static void
+_write_source_mode2_form1 (VcdObj *obj, VcdDataSource *source, uint32_t extent)
+{
+ int n;
+ uint32_t sectors, size, last_block_size;
+
+ size = vcd_data_source_stat (source);
+
+ sectors = _vcd_len2blocks (size, CDIO_CD_FRAMESIZE);
+
+ last_block_size = size % CDIO_CD_FRAMESIZE;
+ if (!last_block_size)
+ last_block_size = CDIO_CD_FRAMESIZE;
+
+ vcd_data_source_seek (source, 0);
+
+ for (n = 0;n < sectors;n++) {
+ char buf[CDIO_CD_FRAMESIZE] = { 0, };
+
+ vcd_data_source_read (source, buf,
+ ((n + 1 == sectors)
+ ? last_block_size
+ : CDIO_CD_FRAMESIZE), 1);
+
+ if (_write_m2_image_sector (obj, buf, extent+n, 1, 0,
+ ((n+1 < sectors)
+ ? SM_DATA
+ : SM_DATA |SM_EOF),
+ 0))
+ break;
+ }
+
+ vcd_data_source_close (source);
+}
+
+static int
+_write_sequence (VcdObj *obj, int track_idx)
+{
+ mpeg_sequence_t *track =
+ _cdio_list_node_data (_vcd_list_at (obj->mpeg_sequence_list, track_idx));
+ CdioListNode *pause_node;
+ int n, lastsect = obj->sectors_written;
+ char buf[2324];
+ struct {
+ int audio;
+ int video;
+ int zero;
+ int ogt;
+ int unknown;
+ } mpeg_packets = {0, };
+
+
+ {
+ char *norm_str = NULL;
+ const struct vcd_mpeg_stream_vid_info *_info = &track->info->shdr[0];
+
+ switch (vcd_mpeg_get_norm (_info)) {
+ case MPEG_NORM_PAL:
+ norm_str = strdup ("PAL SIF (352x288/25fps)");
+ break;
+ case MPEG_NORM_NTSC:
+ norm_str = strdup ("NTSC SIF (352x240/29.97fps)");
+ break;
+ case MPEG_NORM_FILM:
+ norm_str = strdup ("FILM SIF (352x240/24fps)");
+ break;
+ case MPEG_NORM_PAL_S:
+ norm_str = strdup ("PAL 2/3 D1 (480x576/25fps)");
+ break;
+ case MPEG_NORM_NTSC_S:
+ norm_str = strdup ("NTSC 2/3 D1 (480x480/29.97fps)");
+ break;
+
+ case MPEG_NORM_OTHER:
+ {
+ char buf[1024] = { 0, };
+ switch (_info->vsize)
+ {
+ case 480:
+ case 240:
+ snprintf (buf, sizeof (buf), "NTSC UNKNOWN (%dx%d/%2.2ffps)",
+ _info->hsize, _info->vsize, _info->frate);
+ break;
+ case 288:
+ case 576:
+ snprintf (buf, sizeof (buf), "PAL UNKNOWN (%dx%d/%2.2ffps)",
+ _info->hsize, _info->vsize, _info->frate);
+ break;
+ default:
+ snprintf (buf, sizeof (buf), "UNKNOWN (%dx%d/%2.2ffps)",
+ _info->hsize, _info->vsize, _info->frate);
+ break;
+ }
+ norm_str = strdup (buf);
+ }
+ break;
+ }
+
+ {
+ char buf[1024] = { 0, }, buf2[1024] = { 0, };
+ int i;
+
+ for (i = 0; i < 3; i++)
+ if (track->info->ahdr[i].seen)
+ {
+ const char *_mode_str[] = {
+ 0,
+ "stereo",
+ "jstereo",
+ "dual",
+ "single",
+ 0
+ };
+
+ snprintf (buf, sizeof (buf), "audio[%d]: l%d/%2.1fkHz/%dkbps/%s ",
+ i,
+ track->info->ahdr[i].layer,
+ track->info->ahdr[i].sampfreq / 1000.0,
+ track->info->ahdr[i].bitrate / 1024,
+ _mode_str[track->info->ahdr[i].mode]);
+
+ strncat (buf2, buf, sizeof(buf2) - strlen(buf2) - 1);
+ }
+
+ vcd_info ("writing track %d, %s, %s, %s...", track_idx + 2,
+ (track->info->version == MPEG_VERS_MPEG1 ? "MPEG1" : "MPEG2"),
+ norm_str, buf2);
+ }
+
+ free (norm_str);
+ }
+
+ for (n = 0; n < obj->track_pregap; n++)
+ _write_m2_image_sector (obj, zero, lastsect++, 0, 0, SM_FORM2, 0);
+
+ for (n = 0; n < obj->track_front_margin;n++)
+ _write_m2_image_sector (obj, zero, lastsect++, track_idx + 1,
+ 0, SM_FORM2|SM_REALT, 0);
+
+ pause_node = _cdio_list_begin (track->pause_list);
+
+ for (n = 0; n < track->info->packets; n++) {
+ int ci = 0, sm = 0, cnum = 0, fnum = 0;
+ struct vcd_mpeg_packet_info pkt_flags;
+ bool set_trigger = false;
+
+ vcd_mpeg_source_get_packet (track->source, n, buf, &pkt_flags,
+ obj->update_scan_offsets);
+
+ while (pause_node)
+ {
+ pause_t *_pause = _cdio_list_node_data (pause_node);
+
+ if (!pkt_flags.has_pts)
+ break; /* no pts */
+
+ if (pkt_flags.pts < _pause->time)
+ break; /* our time has not come yet */
+
+ /* seems it's time to trigger! */
+ set_trigger = true;
+
+ vcd_debug ("setting auto pause trigger for time %f (pts %f) @%d",
+ _pause->time, pkt_flags.pts, n);
+
+ pause_node = _cdio_list_node_next (pause_node);
+ }
+
+ switch (vcd_mpeg_packet_get_type (&pkt_flags))
+ {
+ case PKT_TYPE_VIDEO:
+ mpeg_packets.video++;
+ sm = SM_FORM2|SM_REALT|SM_VIDEO;
+ ci = CI_VIDEO;
+ cnum = CN_VIDEO;
+ break;
+
+ case PKT_TYPE_OGT:
+ mpeg_packets.ogt++;
+ sm = SM_FORM2|SM_REALT|SM_VIDEO;
+ ci = CI_OGT;
+ cnum = CN_OGT;
+ break;
+
+ case PKT_TYPE_AUDIO:
+ mpeg_packets.audio++;
+ sm = SM_FORM2|SM_REALT|SM_AUDIO;
+ ci = CI_AUDIO;
+ cnum = CN_AUDIO;
+ if (pkt_flags.audio[1] || pkt_flags.audio[2])
+ {
+ ci = CI_AUDIO2;
+ cnum = CN_AUDIO2;
+ }
+ break;
+
+
+ case PKT_TYPE_ZERO:
+ mpeg_packets.zero++;
+ mpeg_packets.unknown--;
+ case PKT_TYPE_EMPTY:
+ mpeg_packets.unknown++;
+ sm = SM_FORM2|SM_REALT;
+ ci = CI_EMPTY;
+ cnum = CN_EMPTY;
+ break;
+
+ case PKT_TYPE_INVALID:
+ vcd_error ("invalid mpeg packet found at packet# %d"
+ " -- please fix this mpeg file!", n);
+ vcd_mpeg_source_close (track->source);
+ return 1;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ if (n == track->info->packets - 1)
+ {
+ sm |= SM_EOR;
+ if (!obj->track_rear_margin) /* if no rear margin... */
+ sm |= SM_EOF;
+ }
+
+ if (set_trigger)
+ sm |= SM_TRIG;
+
+ fnum = track_idx + 1;
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD)
+ && !obj->svcd_vcd3_mpegav) /* IEC62107 SVCDs have a
+ simplified subheader */
+ {
+ fnum = 1;
+ ci = CI_MPEG2;
+ }
+
+ if (_write_m2_image_sector (obj, buf, lastsect++, fnum, cnum, sm, ci))
+ break;
+ }
+
+ vcd_mpeg_source_close (track->source);
+
+ for (n = 0; n < obj->track_rear_margin; n++)
+ {
+ const uint8_t ci = 0, cnum = 0;
+ uint8_t fnum = track_idx + 1;
+ uint8_t sm = SM_FORM2 | SM_REALT;
+
+ if (n + 1 == obj->track_rear_margin)
+ sm |= SM_EOF;
+
+ _write_m2_image_sector (obj, zero, lastsect++, fnum, cnum, sm, ci);
+ }
+
+ vcd_debug ("MPEG packet statistics: %d video, %d audio, %d zero, %d ogt, %d unknown",
+ mpeg_packets.video, mpeg_packets.audio, mpeg_packets.zero, mpeg_packets.ogt,
+ mpeg_packets.unknown);
+
+ return 0;
+}
+
+static int
+_write_segment (VcdObj *obj, mpeg_segment_t *_segment)
+{
+ CdioListNode *pause_node;
+ unsigned packet_no;
+ int n = obj->sectors_written;
+
+ vcd_assert (_segment->start_extent == n);
+
+ pause_node = _cdio_list_begin (_segment->pause_list);
+
+ for (packet_no = 0;
+ packet_no < (_segment->segment_count * VCDINFO_SEGMENT_SECTOR_SIZE);
+ packet_no++)
+ {
+ uint8_t buf[M2F2_SECTOR_SIZE] = { 0, };
+ uint8_t fn, cn, sm, ci;
+
+ if (packet_no < _segment->info->packets)
+ {
+ struct vcd_mpeg_packet_info pkt_flags;
+ bool set_trigger = false;
+ bool _need_eor = false;
+
+ vcd_mpeg_source_get_packet (_segment->source, packet_no,
+ buf, &pkt_flags, obj->update_scan_offsets);
+
+ fn = 1;
+ cn = CN_EMPTY;
+ sm = SM_FORM2 | SM_REALT;
+ ci = CI_EMPTY;
+
+ while (pause_node)
+ {
+ pause_t *_pause = _cdio_list_node_data (pause_node);
+
+ if (!pkt_flags.has_pts)
+ break; /* no pts */
+
+ if (pkt_flags.pts < _pause->time)
+ break; /* our time has not come yet */
+
+ /* seems it's time to trigger! */
+ set_trigger = true;
+
+ vcd_debug ("setting auto pause trigger for time %f (pts %f) @%d",
+ _pause->time, pkt_flags.pts, n);
+
+ pause_node = _cdio_list_node_next (pause_node);
+ }
+
+ switch (vcd_mpeg_packet_get_type (&pkt_flags))
+ {
+ case PKT_TYPE_VIDEO:
+ sm = SM_FORM2 | SM_REALT | SM_VIDEO;
+
+ ci = CI_VIDEO;
+ cn = CN_VIDEO;
+
+ if (pkt_flags.video[1])
+ ci = CI_STILL, cn = CN_STILL;
+ else if (pkt_flags.video[2])
+ ci = CI_STILL2, cn = CN_STILL2;
+
+ if (pkt_flags.video[1] || pkt_flags.video[2])
+ { /* search for endcode -- hack */
+ int idx;
+
+ for (idx = 0; idx <= 2320; idx++)
+ if (buf[idx] == 0x00
+ && buf[idx + 1] == 0x00
+ && buf[idx + 2] == 0x01
+ && buf[idx + 3] == 0xb7)
+ {
+ _need_eor = true;
+ break;
+ }
+ }
+ break;
+
+ case PKT_TYPE_AUDIO:
+ sm = SM_FORM2 | SM_REALT | SM_AUDIO;
+
+ ci = CI_AUDIO;
+ cn = CN_AUDIO;
+ break;
+
+ case PKT_TYPE_EMPTY:
+ ci = CI_EMPTY;
+ cn = CN_EMPTY;
+ break;
+
+ default:
+ /* fixme -- check.... */
+ break;
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ cn = 1;
+ sm = SM_FORM2 | SM_REALT | SM_VIDEO;
+ ci = CI_MPEG2;
+ }
+
+ if (packet_no + 1 == _segment->info->packets)
+ sm |= SM_EOF;
+
+ if (set_trigger)
+ sm |= SM_TRIG;
+
+ if (_need_eor)
+ {
+ vcd_debug ("setting EOR for SeqEnd at packet# %d ('%s')",
+ packet_no, _segment->id);
+ sm |= SM_EOR;
+ }
+ }
+ else
+ {
+ fn = 1;
+ cn = CN_EMPTY;
+ sm = SM_FORM2 | SM_REALT;
+ ci = CI_EMPTY;
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ fn = 0;
+ sm = SM_FORM2;
+ }
+
+ }
+
+ _write_m2_image_sector (obj, buf, n, fn, cn, sm, ci);
+
+ n++;
+ }
+
+ vcd_mpeg_source_close (_segment->source);
+
+ return 0;
+}
+
+static uint32_t
+_get_closest_aps (const struct vcd_mpeg_stream_info *_mpeg_info, double t,
+ struct aps_data *_best_aps)
+{
+ CdioListNode *node;
+ struct aps_data best_aps;
+ bool first = true;
+
+ vcd_assert (_mpeg_info != NULL);
+ vcd_assert (_mpeg_info->shdr[0].aps_list != NULL);
+
+ _CDIO_LIST_FOREACH (node, _mpeg_info->shdr[0].aps_list)
+ {
+ struct aps_data *_aps = _cdio_list_node_data (node);
+
+ if (first)
+ {
+ best_aps = *_aps;
+ first = false;
+ }
+ else if (fabs (_aps->timestamp - t) < fabs (best_aps.timestamp - t))
+ best_aps = *_aps;
+ else
+ break;
+ }
+
+ if (_best_aps)
+ *_best_aps = best_aps;
+
+ return best_aps.packet_no;
+}
+
+static void
+_update_entry_points (VcdObj *obj)
+{
+ CdioListNode *sequence_node;
+
+ _CDIO_LIST_FOREACH (sequence_node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _cdio_list_node_data (sequence_node);
+ CdioListNode *entry_node;
+ unsigned last_packet_no = 0;
+
+ _CDIO_LIST_FOREACH (entry_node, _sequence->entry_list)
+ {
+ entry_t *_entry = _cdio_list_node_data (entry_node);
+
+ _get_closest_aps (_sequence->info, _entry->time, &_entry->aps);
+
+ vcd_log ((fabs (_entry->aps.timestamp - _entry->time) > 1
+ ? VCD_LOG_WARN
+ : VCD_LOG_DEBUG),
+ "requested entry point (id=%s) at %f, "
+ "closest possible entry point at %f",
+ _entry->id, _entry->time, _entry->aps.timestamp);
+
+ if (last_packet_no == _entry->aps.packet_no)
+ vcd_warn ("entry point '%s' falls into same sector as previous one!",
+ _entry->id);
+
+ last_packet_no = _entry->aps.packet_no;
+ }
+ }
+}
+
+static int
+_write_vcd_iso_track (VcdObj *obj, const time_t *create_time)
+{
+ CdioListNode *node;
+ int n;
+
+ /* generate dir sectors */
+
+ _vcd_directory_dump_entries (obj->dir,
+ _dict_get_bykey (obj, "dir")->buf,
+ _dict_get_bykey (obj, "dir")->sector);
+
+ _vcd_directory_dump_pathtables (obj->dir,
+ _dict_get_bykey (obj, "ptl")->buf,
+ _dict_get_bykey (obj, "ptm")->buf);
+
+ /* generate PVD and EVD at last... */
+ iso9660_set_pvd (_dict_get_bykey (obj, "pvd")->buf,
+ obj->iso_volume_label,
+ obj->iso_publisher_id,
+ obj->iso_preparer_id,
+ obj->iso_application_id,
+ obj->iso_size,
+ _dict_get_bykey (obj, "dir")->buf,
+ _dict_get_bykey (obj, "ptl")->sector,
+ _dict_get_bykey (obj, "ptm")->sector,
+ iso9660_pathtable_get_size (_dict_get_bykey (obj, "ptm")->buf),
+ create_time
+);
+
+ iso9660_set_evd (_dict_get_bykey (obj, "evd")->buf);
+
+ /* fill VCD relevant files with data */
+
+ set_info_vcd (obj, _dict_get_bykey (obj, "info")->buf);
+ set_entries_vcd (obj, _dict_get_bykey (obj, "entries")->buf);
+
+ if (_vcd_pbc_available (obj))
+ {
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ {
+ set_lot_vcd (obj, _dict_get_bykey (obj, "lot_x")->buf, true);
+ set_psd_vcd (obj, _dict_get_bykey (obj, "psd_x")->buf, true);
+ }
+
+ _vcd_pbc_check_unreferenced (obj);
+
+ set_lot_vcd (obj, _dict_get_bykey (obj, "lot")->buf, false);
+ set_psd_vcd (obj, _dict_get_bykey (obj, "psd")->buf, false);
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ set_tracks_svd (obj, _dict_get_bykey (obj, "tracks")->buf);
+ set_search_dat (obj, _dict_get_bykey (obj, "search")->buf);
+ set_scandata_dat (obj, _dict_get_bykey (obj, "scandata")->buf);
+ }
+
+ /* start actually writing stuff */
+
+ vcd_info ("writing track 1 (ISO9660)...");
+
+ /* 00:02:00 -> 00:04:74 */
+ for (n = 0;n < obj->mpeg_segment_start_extent; n++)
+ {
+ const void *content = NULL;
+ uint8_t flags = SM_DATA;
+
+ content = _dict_get_sector (obj, n);
+ flags |= _dict_get_sector_flags (obj, n);
+
+ if (content == NULL)
+ content = zero;
+
+ _write_m2_image_sector (obj, content, n, 0, 0, flags, 0);
+ }
+
+ /* SEGMENTS */
+
+ vcd_assert (n == obj->mpeg_segment_start_extent);
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _cdio_list_node_data (node);
+
+ _write_segment (obj, _segment);
+ }
+
+ n = obj->sectors_written;
+
+ /* EXT stuff */
+
+ vcd_assert (n == obj->ext_file_start_extent);
+
+ for (;n < obj->custom_file_start_extent; n++)
+ {
+ const void *content = NULL;
+ uint8_t flags = SM_DATA;
+ uint8_t fileno = _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD) ? 0 : 1;
+
+ content = _dict_get_sector (obj, n);
+ flags |= _dict_get_sector_flags (obj, n);
+
+ if (content == NULL)
+ {
+ vcd_debug ("unexpected empty EXT sector");
+ content = zero;
+ }
+
+ _write_m2_image_sector (obj, content, n, fileno, 0, flags, 0);
+ }
+
+ /* write custom files */
+
+ vcd_assert (n == obj->custom_file_start_extent);
+
+ _CDIO_LIST_FOREACH (node, obj->custom_file_list)
+ {
+ custom_file_t *p = _cdio_list_node_data (node);
+
+ vcd_info ("writing file `%s' (%lu bytes%s)",
+ p->iso_pathname, (unsigned long) p->size,
+ p->raw_flag ? ", raw sectors file": "");
+ if (p->raw_flag)
+ _write_source_mode2_raw (obj, p->file, p->start_extent);
+ else
+ _write_source_mode2_form1 (obj, p->file, p->start_extent);
+ }
+
+ /* blank unalloced tracks */
+ while ((n = _vcd_salloc (obj->iso_bitmap, SECTOR_NIL, 1)) < obj->iso_size)
+ _write_m2_image_sector (obj, zero, n, 0, 0, SM_DATA, 0);
+
+ return 0;
+}
+
+
+long
+vcd_obj_get_image_size (VcdObj *obj)
+{
+ long size_sectors = -1;
+
+ vcd_assert (!obj->in_output);
+
+ if (_cdio_list_length (obj->mpeg_sequence_list) > 0)
+ {
+ /* fixme -- make this efficient */
+ size_sectors = vcd_obj_begin_output (obj);
+ vcd_obj_end_output (obj);
+ }
+
+ return size_sectors;
+}
+
+long
+vcd_obj_begin_output (VcdObj *obj)
+{
+ uint32_t image_size;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (_cdio_list_length (obj->mpeg_sequence_list) > 0);
+
+ vcd_assert (!obj->in_output);
+ obj->in_output = true;
+
+ obj->in_track = 1;
+ obj->sectors_written = 0;
+
+ obj->iso_bitmap = _vcd_salloc_new ();
+
+ obj->dir = _vcd_directory_new ();
+
+ obj->buffer_dict_list = _cdio_list_new ();
+
+ _finalize_vcd_iso_track (obj);
+
+ _update_entry_points (obj);
+
+ image_size = obj->relative_end_extent + obj->iso_size;
+
+ image_size += obj->leadout_pregap;
+
+ if (image_size > CDIO_CD_MAX_SECTORS)
+ vcd_error ("image too big (%d sectors > %d sectors)",
+ (unsigned) image_size, (unsigned) CDIO_CD_MAX_SECTORS);
+
+ {
+ char *_tmp = cdio_lba_to_msf_str (image_size);
+
+ if (image_size > CDIO_CD_74MIN_SECTORS)
+ vcd_warn ("generated image (%d sectors [%s]) may not fit "
+ "on 74min CDRs (%d sectors)",
+ (unsigned) image_size, _tmp, (unsigned) CDIO_CD_74MIN_SECTORS);
+
+ free (_tmp);
+ }
+
+ return image_size;
+}
+
+
+void
+vcd_obj_end_output (VcdObj *obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_assert (obj->in_output);
+ obj->in_output = false;
+
+ _vcd_directory_destroy (obj->dir);
+ _vcd_salloc_destroy (obj->iso_bitmap);
+
+ _dict_clean (obj);
+ _cdio_list_free (obj->buffer_dict_list, true);
+}
+
+int
+vcd_obj_append_pbc_node (VcdObj *obj, struct _pbc_t *_pbc)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (_pbc != NULL);
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_PBC))
+ {
+ vcd_error ("PBC not supported for current VCD type");
+ return -1;
+ }
+
+ if (_pbc->item_id && _vcd_pbc_lookup (obj, _pbc->item_id))
+ {
+ vcd_error ("item id (%s) exists already", _pbc->item_id);
+ return -1;
+ }
+
+ _cdio_list_append (obj->pbc_list, _pbc);
+
+ return 0;
+}
+
+int
+vcd_obj_write_image (VcdObj *obj, VcdImageSink *image_sink,
+ progress_callback_t callback, void *user_data,
+ const time_t *create_time)
+{
+ CdioListNode *node;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->in_output);
+
+ if (!image_sink)
+ return -1;
+
+ /* start with meta info */
+
+ {
+ CdioList *cue_list;
+ vcd_cue_t *_cue;
+
+ cue_list = _cdio_list_new ();
+
+ _cdio_list_append (cue_list, (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = 0;
+ _cue->type = VCD_CUE_TRACK_START;
+
+ _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *track = _cdio_list_node_data (node);
+ CdioListNode *entry_node;
+
+ _cdio_list_append (cue_list,
+ (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = track->relative_start_extent + obj->iso_size;
+ _cue->lsn -= obj->track_pregap;
+ _cue->type = VCD_CUE_PREGAP_START;
+
+ _cdio_list_append (cue_list,
+ (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = track->relative_start_extent + obj->iso_size;
+ _cue->type = VCD_CUE_TRACK_START;
+
+ _CDIO_LIST_FOREACH (entry_node, track->entry_list)
+ {
+ entry_t *_entry = _cdio_list_node_data (entry_node);
+
+ _cdio_list_append (cue_list,
+ (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = obj->iso_size;
+ _cue->lsn += track->relative_start_extent;
+ _cue->lsn += obj->track_front_margin;
+ _cue->lsn += _entry->aps.packet_no;
+
+ _cue->type = VCD_CUE_SUBINDEX;
+ }
+ }
+
+ /* add last one... */
+
+ _cdio_list_append (cue_list, (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = obj->relative_end_extent + obj->iso_size;
+
+ _cue->lsn += obj->leadout_pregap;
+
+ _cue->type = VCD_CUE_END;
+
+ /* send it to image object */
+
+ vcd_image_sink_set_cuesheet (image_sink, cue_list);
+
+ _cdio_list_free (cue_list, true);
+ }
+
+ /* and now for the pay load */
+
+ {
+ unsigned track;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->sectors_written == 0);
+
+ vcd_assert (obj->in_output);
+
+ obj->progress_callback = callback;
+ obj->callback_user_data = user_data;
+ obj->image_sink = image_sink;
+
+ if (_callback_wrapper (obj, true))
+ return 1;
+
+ if (_write_vcd_iso_track (obj, create_time))
+ return 1;
+
+ if (obj->update_scan_offsets)
+ vcd_info ("'update scan offsets' option enabled for the following tracks!");
+
+ for (track = 0;track < _cdio_list_length (obj->mpeg_sequence_list);track++)
+ {
+ obj->in_track++;
+
+ if (_callback_wrapper (obj, true))
+ return 1;
+
+ if (_write_sequence (obj, track))
+ return 1;
+ }
+
+ if (obj->leadout_pregap)
+ {
+ int n, lastsect = obj->sectors_written;
+
+ vcd_debug ("writting post-gap ('leadout pregap')...");
+
+ for (n = 0; n < obj->leadout_pregap; n++)
+ _write_m2_image_sector (obj, zero, lastsect++, 0, 0, SM_FORM2, 0);
+ }
+
+ if (_callback_wrapper (obj, true))
+ return 1;
+
+ obj->image_sink = NULL;
+
+ vcd_image_sink_destroy (image_sink);
+
+ return 0; /* ok */
+ }
+}
+
+const char *
+vcd_version_string (bool full_text)
+{
+ if (!full_text)
+ return ("GNU VCDImager " VERSION " [" HOST_ARCH "]");
+
+ return ("%s (GNU VCDImager) " VERSION "\n"
+ "Written by Herbert Valerio Riedel and Rocky Bernstein.\n"
+ "\n"
+ "http://www.gnu.org/software/vcdimager/\n"
+ "\n"
+ "Copyright (C) 2000-2003 Herbert Valerio Riedel <hvr@gnu.org>\n"
+ " 2003 Rocky Bernstein <rocky@panix.com>\n"
+ "\n"
+ "This is free software; see the source for copying conditions. There is NO\n"
+ "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/vcd.h b/contrib/libvcd/vcd.h
new file mode 100644
index 000000000..7c001d282
--- /dev/null
+++ b/contrib/libvcd/vcd.h
@@ -0,0 +1,176 @@
+/*
+ $Id: vcd.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* libvcd main header */
+
+#ifndef __VCD_H__
+#define __VCD_H__
+
+/* Private headers */
+#include "image_sink.h"
+#include "mpeg_stream.h"
+#include "stream.h"
+
+#include <libvcd/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* allocates and initializes a new VideoCD object */
+VcdObj *
+vcd_obj_new (vcd_type_t vcd_type);
+
+/* VideoCD parameters */
+typedef enum {
+ VCD_PARM_INVALID = 0,
+ VCD_PARM_VOLUME_ID, /* char * max length 32 */
+ VCD_PARM_PUBLISHER_ID, /* char * max length 128 */
+ VCD_PARM_PREPARER_ID, /* char * max length 128 */
+ VCD_PARM_ALBUM_ID, /* char * max length 16 */
+ VCD_PARM_VOLUME_COUNT, /* unsigned [1..65535] */
+ VCD_PARM_VOLUME_NUMBER, /* unsigned [0..65535] */
+ VCD_PARM_RESTRICTION, /* unsigned [0..3] */
+ VCD_PARM_NEXT_VOL_LID2, /* bool */
+ VCD_PARM_NEXT_VOL_SEQ2, /* bool */
+ VCD_PARM_APPLICATION_ID, /* char * max length 128 */
+ VCD_PARM_SEC_TYPE, /* unsigned [2336, 2352] */
+ VCD_PARM_SVCD_VCD3_MPEGAV, /* bool */
+ VCD_PARM_SVCD_VCD3_ENTRYSVD, /* bool */
+ VCD_PARM_SVCD_VCD3_TRACKSVD, /* bool */
+ VCD_PARM_UPDATE_SCAN_OFFSETS, /* bool */
+ VCD_PARM_RELAXED_APS, /* bool */
+ VCD_PARM_LEADOUT_PAUSE, /* bool */
+ VCD_PARM_LEADOUT_PREGAP, /* unsigned [0..300] */
+ VCD_PARM_TRACK_PREGAP, /* unsigned [1..300] */
+ VCD_PARM_TRACK_FRONT_MARGIN, /* unsigned [0..150] */
+ VCD_PARM_TRACK_REAR_MARGIN /* unsigned [0..150] */
+} vcd_parm_t;
+
+/* sets VideoCD parameter */
+int
+vcd_obj_set_param_uint (VcdObj *obj, vcd_parm_t param, unsigned arg);
+
+int
+vcd_obj_set_param_str (VcdObj *obj, vcd_parm_t param, const char *arg);
+
+int
+vcd_obj_set_param_bool (VcdObj *obj, vcd_parm_t param, bool arg);
+
+/* add custom files; if raw_flag set, the data source has to include a
+ mode2 subheader, and thus needs to be a multiple of 2336 byte blocksize */
+int
+vcd_obj_add_file (VcdObj *obj, const char iso_pathname[],
+ VcdDataSource *file, bool raw_flag);
+
+int
+vcd_obj_add_dir (VcdObj *obj, const char iso_pathname[]);
+
+/* this is for actually adding mpeg items to VCD, returns
+ a negative value for error.. */
+
+int
+vcd_obj_append_sequence_play_item (VcdObj *obj, VcdMpegSource *mpeg_source,
+ const char item_id[],
+ const char default_entry_id[]);
+
+int
+vcd_obj_add_sequence_entry (VcdObj *obj, const char sequence_id[],
+ double entry_time, const char entry_id[]);
+
+int
+vcd_obj_add_sequence_pause (VcdObj *obj, const char sequence_id[],
+ double pause_timestamp, const char pause_id[]);
+
+int
+vcd_obj_add_segment_pause (VcdObj *obj, const char segment_id[],
+ double pause_timestamp, const char pause_id[]);
+
+int
+vcd_obj_append_segment_play_item (VcdObj *obj, VcdMpegSource *mpeg_source,
+ const char item_id[]);
+
+/* warning -- api will change for pbc */
+typedef struct _pbc_t pbc_t;
+
+int
+vcd_obj_append_pbc_node (VcdObj *obj, struct _pbc_t *_pbc);
+
+/* removes item (sequence, entry, segment, ...) by id, returns
+ negative value on error */
+int
+vcd_obj_remove_item (VcdObj *obj, const char id[]);
+
+/* returns image size in sectors */
+long
+vcd_obj_get_image_size (VcdObj *obj);
+
+/* this one is to be called when every parameter has been set and the
+ image is about to be written. returns sectors to be written... */
+long
+vcd_obj_begin_output (VcdObj *obj);
+
+/* callback hook called every few (>|<) iterations, if it returns a value != 0
+ the writing process gets aborted */
+typedef struct
+{
+ long sectors_written;
+ long total_sectors;
+ int in_track;
+ int total_tracks;
+}
+progress_info_t;
+
+typedef int (*progress_callback_t) (const progress_info_t *progress_info,
+ void *user_data);
+
+/* writes the actual bin image file; a return value != 0 means the
+ action was aborted by user or some other error has occured... */
+int
+vcd_obj_write_image (VcdObj *obj, VcdImageSink *image_sink,
+ progress_callback_t callback, void *user_data,
+ const time_t *create_time);
+
+/* this should be called writing the bin and/or cue file is done---even if
+ an error occurred */
+void
+vcd_obj_end_output (VcdObj *obj);
+
+/* destructor for VideoCD objects; call this to destory a VideoCD
+ object created by vcd_obj_new () */
+void
+vcd_obj_destroy (VcdObj *obj);
+
+const char *
+vcd_version_string (bool full_text);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __VCD_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/contrib/libvcd/vcd_assert.h b/contrib/libvcd/vcd_assert.h
new file mode 100644
index 000000000..c096b3677
--- /dev/null
+++ b/contrib/libvcd/vcd_assert.h
@@ -0,0 +1,55 @@
+/*
+ $Id: vcd_assert.h,v 1.1 2004/04/11 12:20:32 miguelfreitas Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_ASSERT_H__
+#define __VCD_ASSERT_H__
+
+#if defined(__GNUC__)
+
+#include <libvcd/types.h>
+#include <libvcd/logging.h>
+
+#define vcd_assert(expr) \
+ { \
+ if (GNUC_UNLIKELY (!(expr))) vcd_log (VCD_LOG_ASSERT, \
+ "file %s: line %d (%s): assertion failed: (%s)", \
+ __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
+ }
+
+#define vcd_assert_not_reached() \
+ { \
+ vcd_log (VCD_LOG_ASSERT, \
+ "file %s: line %d (%s): should not be reached", \
+ __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ }
+
+#else /* non GNU C */
+
+#include <assert.h>
+
+#define vcd_assert(expr) \
+ assert(expr)
+
+#define vcd_assert_not_reached() \
+ assert(0)
+
+#endif
+
+#endif /* __VCD_ASSERT_H__ */
diff --git a/contrib/libvcd/vcd_read.c b/contrib/libvcd/vcd_read.c
new file mode 100644
index 000000000..dd9d30cfa
--- /dev/null
+++ b/contrib/libvcd/vcd_read.c
@@ -0,0 +1,108 @@
+/*
+ $Id: vcd_read.c,v 1.4 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2001,2003 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#include "vcd_read.h"
+#include "vcd_assert.h"
+#include <libvcd/inf.h>
+#include <libvcd/files.h>
+#include <libvcd/logging.h>
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+bool
+read_pvd(CdIo *cdio, iso9660_pvd_t *pvd)
+{
+ if (cdio_read_mode2_sector (cdio, pvd, ISO_PVD_SECTOR, false)) {
+ vcd_error ("error reading PVD sector (%d)", ISO_PVD_SECTOR);
+ return false;
+ }
+
+ if (pvd->type != ISO_VD_PRIMARY) {
+ vcd_error ("unexpected PVD type %d", pvd->type);
+ return false;
+ }
+
+ if (strncmp (pvd->id, ISO_STANDARD_ID, strlen (ISO_STANDARD_ID)))
+ {
+ vcd_error ("unexpected ID encountered (expected `"
+ ISO_STANDARD_ID "', got `%.5s'", pvd->id);
+ return false;
+ }
+ return true;
+}
+
+bool
+read_entries(CdIo *cdio, EntriesVcd_t *entries)
+{
+ if (cdio_read_mode2_sector (cdio, entries, ENTRIES_VCD_SECTOR, false)) {
+ vcd_error ("error reading Entries sector (%d)", ENTRIES_VCD_SECTOR);
+ return false;
+ }
+
+ /* analyze signature/type */
+
+ if (!strncmp (entries->ID, ENTRIES_ID_VCD, sizeof (entries->ID)))
+ return true;
+ else if (!strncmp (entries->ID, "ENTRYSVD", sizeof (entries->ID))) {
+ vcd_warn ("found (non-compliant) SVCD ENTRIES.SVD signature");
+ return true;
+ } else {
+ vcd_error ("unexpected ID signature encountered `%.8s'", entries->ID);
+ return false;
+ }
+}
+
+bool
+read_info(CdIo *cdio, InfoVcd_t *info, vcd_type_t *vcd_type)
+{
+ if (cdio_read_mode2_sector (cdio, info, INFO_VCD_SECTOR, false)) {
+ vcd_error ("error reading Info sector (%d)", INFO_VCD_SECTOR);
+ return false;
+ }
+
+ *vcd_type = vcd_files_info_detect_type (info);
+
+ /* analyze signature/type */
+
+ switch (*vcd_type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ vcd_debug ("%s detected", vcdinf_get_format_version_str(*vcd_type));
+ break;
+ case VCD_TYPE_INVALID:
+ vcd_error ("unknown ID encountered -- maybe not a proper (S)VCD?");
+ return false;
+ break;
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return true;
+}
+
diff --git a/contrib/libvcd/vcd_read.h b/contrib/libvcd/vcd_read.h
new file mode 100644
index 000000000..fb9e1509b
--- /dev/null
+++ b/contrib/libvcd/vcd_read.h
@@ -0,0 +1,36 @@
+/*
+ $Id: vcd_read.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+
+/* FIXME: make this really private: */
+#include <libvcd/files_private.h>
+
+bool read_pvd(CdIo *cdio, iso9660_pvd_t *pvd);
+bool read_entries(CdIo *cdio, EntriesVcd_t *entries);
+bool read_info(CdIo *cdio, InfoVcd_t *info, vcd_type_t *vcd_type);
+
+
+