/* $Id: sector.c,v 1.10 2004/06/05 02:49:21 rocky Exp $ Copyright (C) 2004 Rocky Bernstein Copyright (C) 2000 Herbert Valerio Riedel 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 #include #include #include "cdio_assert.h" #include #ifdef HAVE_STRING_H #include #endif static const char _rcsid[] = "$Id: sector.c,v 1.10 2004/06/05 02:49:21 rocky 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 = to_bcd8 (m); msf->s = to_bcd8 (s); msf->f = to_bcd8 (f); } /* warning, 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 = { .m = 0, .s = 0, .f = 0 }; cdio_lba_to_msf (lba, &msf); return cdio_msf_to_str(&msf); } } lba_t cdio_lsn_to_lba (lsn_t lsn) { if (CDIO_INVALID_LSN == lsn) return CDIO_INVALID_LBA; return lsn + CDIO_PREGAP_SECTORS; } 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); } lba_t cdio_msf_to_lba (const msf_t *msf) { uint32_t lba = 0; cdio_assert (msf != 0); lba = from_bcd8 (msf->m); lba *= CDIO_CD_SECS_PER_MIN; lba += from_bcd8 (msf->s); lba *= CDIO_CD_FRAMES_PER_SEC; lba += from_bcd8 (msf->f); return lba; } lba_t cdio_msf_to_lsn (const msf_t *msf) { return cdio_lba_to_lsn(cdio_msf_to_lba (msf)); } /* warning, returns new allocated string */ char * cdio_msf_to_str (const msf_t *msf) { char buf[16]; snprintf (buf, sizeof (buf), "%.2x:%.2x.%.2x", msf->m, msf->s, msf->f); return strdup (buf); } /* * Local variables: * c-file-style: "gnu" * tab-width: 8 * indent-tabs-mode: nil * End: */