1 /*
2   Copyright (C) 2004, 2005, 2011, 2012, 2014, 2016
3   Rocky Bernstein <rocky@gnu.org>
4   Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
5 
6   This program is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 # define __CDIO_CONFIG_H__ 1
23 #endif
24 
25 #include <cdio/sector.h>
26 #include <cdio/util.h>
27 #include <cdio/logging.h>
28 #include "cdio_assert.h"
29 #include "portable.h"
30 
31 #ifdef HAVE_STDIO_H
32 #include <stdio.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 
38 #include <ctype.h>
39 
40 /*! String of bytes used to identify the beginning of a Mode 1 or
41   Mode 2 sector. */
42 const uint8_t CDIO_SECTOR_SYNC_HEADER[CDIO_CD_SYNC_SIZE] =
43   {0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0};
44 
45 /* Variables to hold debugger-helping enumerations */
46 enum cdio_cd_enums;
47 enum m2_sector_enums;
48 
49 lba_t
cdio_lba_to_lsn(lba_t lba)50 cdio_lba_to_lsn (lba_t lba)
51 {
52   if (CDIO_INVALID_LBA     == lba) return CDIO_INVALID_LSN;
53   return lba - CDIO_PREGAP_SECTORS;
54 }
55 
56 /*
57    The below is adapted from cdparanoia code which claims it is
58    straight from the MMC3 spec.
59 */
60 
61 void
cdio_lsn_to_msf(lsn_t lsn,msf_t * msf)62 cdio_lsn_to_msf (lsn_t lsn, msf_t *msf)
63 {
64   int m, s, f;
65 
66   cdio_assert (msf != 0);
67 
68   if ( lsn >= -CDIO_PREGAP_SECTORS ){
69     m    = (lsn + CDIO_PREGAP_SECTORS) / CDIO_CD_FRAMES_PER_MIN;
70     lsn -= m * CDIO_CD_FRAMES_PER_MIN;
71     s    = (lsn + CDIO_PREGAP_SECTORS) / CDIO_CD_FRAMES_PER_SEC;
72     lsn -= s * CDIO_CD_FRAMES_PER_SEC;
73     f    = lsn + CDIO_PREGAP_SECTORS;
74   } else {
75     m    = (lsn + CDIO_CD_MAX_LSN)     / CDIO_CD_FRAMES_PER_MIN;
76     lsn -= m * (CDIO_CD_FRAMES_PER_MIN);
77     s    = (lsn+CDIO_CD_MAX_LSN)       / CDIO_CD_FRAMES_PER_SEC;
78     lsn -= s * CDIO_CD_FRAMES_PER_SEC;
79     f    = lsn + CDIO_CD_MAX_LSN;
80   }
81 
82   if (m > 99) {
83     cdio_warn ("number of minutes (%d) truncated to 99.", m);
84     m = 99;
85   }
86 
87   msf->m = cdio_to_bcd8 (m);
88   msf->s = cdio_to_bcd8 (s);
89   msf->f = cdio_to_bcd8 (f);
90 }
91 
92 /*!
93   Convert an LBA into a string representation of the MSF.
94   \warning cdio_lba_to_msf_str returns new allocated string */
95 char *
cdio_lba_to_msf_str(lba_t lba)96 cdio_lba_to_msf_str (lba_t lba)
97 {
98 
99   if (CDIO_INVALID_LBA == lba) {
100     return strdup("*INVALID");
101   } else {
102     msf_t msf;
103     msf.m = msf.s = msf.f = 0;
104     cdio_lba_to_msf (lba, &msf);
105     return cdio_msf_to_str(&msf);
106   }
107 }
108 
109 /*!
110   Convert an LSN into the corresponding LBA.
111   CDIO_INVALID_LBA is returned if there is an error.
112 */
113 lba_t
cdio_lsn_to_lba(lsn_t lsn)114 cdio_lsn_to_lba (lsn_t lsn)
115 {
116   if (CDIO_INVALID_LSN  == lsn) return CDIO_INVALID_LBA;
117   return lsn + CDIO_PREGAP_SECTORS;
118 }
119 
120 /*!
121   Convert an LBA into the corresponding MSF.
122 */
123 void
cdio_lba_to_msf(lba_t lba,msf_t * msf)124 cdio_lba_to_msf (lba_t lba, msf_t *msf)
125 {
126   cdio_assert (msf != 0);
127   cdio_lsn_to_msf(cdio_lba_to_lsn(lba), msf);
128 }
129 
130 /*!
131   Convert a MSF into the corresponding LBA.
132   CDIO_INVALID_LBA is returned if there is an error.
133 */
134 lba_t
cdio_msf_to_lba(const msf_t * msf)135 cdio_msf_to_lba (const msf_t *msf)
136 {
137   uint32_t lba = 0;
138 
139   cdio_assert (msf != 0);
140 
141   lba = cdio_from_bcd8 (msf->m);
142   lba *= CDIO_CD_SECS_PER_MIN;
143 
144   lba += cdio_from_bcd8 (msf->s);
145   lba *= CDIO_CD_FRAMES_PER_SEC;
146 
147   lba += cdio_from_bcd8 (msf->f);
148 
149   return lba;
150 }
151 
152 /*!
153   Convert a MSF into the corresponding LSN.
154   CDIO_INVALID_LSN is returned if there is an error.
155 */
156 lba_t
cdio_msf_to_lsn(const msf_t * msf)157 cdio_msf_to_lsn (const msf_t *msf)
158 {
159   return cdio_lba_to_lsn(cdio_msf_to_lba (msf));
160 }
161 
162 /*!
163   Convert an LBA into a string representation of the MSF.
164   \warning cdio_lba_to_msf_str returns new allocated string */
165 char *
cdio_msf_to_str(const msf_t * msf)166 cdio_msf_to_str (const msf_t *msf)
167 {
168   char buf[16];
169 
170   snprintf (buf, sizeof (buf), "%2.2x:%2.2x:%2.2x", msf->m, msf->s, msf->f);
171   return strdup (buf);
172 }
173 
174 /*!
175   Convert a MSF - broken out as 3 integer components into the
176   corresponding LBA.
177   CDIO_INVALID_LBA is returned if there is an error.
178 */
179 lba_t
cdio_msf3_to_lba(unsigned int minutes,unsigned int seconds,unsigned int frames)180 cdio_msf3_to_lba (unsigned int minutes, unsigned int seconds,
181                   unsigned int frames)
182 {
183   return ((minutes * CDIO_CD_SECS_PER_MIN + seconds) * CDIO_CD_FRAMES_PER_SEC
184 	  + frames);
185 }
186 
187 /*!
188   Convert a string of the form MM:SS:FF into the corresponding LBA.
189   CDIO_INVALID_LBA is returned if there is an error.
190 */
191 lba_t
cdio_mmssff_to_lba(const char * psz_mmssff)192 cdio_mmssff_to_lba (const char *psz_mmssff)
193 {
194   int psz_field;
195   lba_t ret;
196   unsigned char c;
197 
198   if (0 == strcmp (psz_mmssff, "0"))
199     return 0;
200 
201   c = *psz_mmssff++;
202   if(c >= '0' && c <= '9')
203     psz_field = (c - '0');
204   else
205     return CDIO_INVALID_LBA;
206   while(':' != (c = *psz_mmssff++)) {
207     if(c >= '0' && c <= '9')
208       psz_field = psz_field * 10 + (c - '0');
209     else
210       return CDIO_INVALID_LBA;
211   }
212 
213   ret = cdio_msf3_to_lba (psz_field, 0, 0);
214 
215   c = *psz_mmssff++;
216   if(c >= '0' && c <= '9')
217     psz_field = (c - '0');
218   else
219     return CDIO_INVALID_LBA;
220   if(':' != (c = *psz_mmssff++)) {
221     if(c >= '0' && c <= '9') {
222       psz_field = psz_field * 10 + (c - '0');
223       c = *psz_mmssff++;
224       if(c != ':')
225 	return CDIO_INVALID_LBA;
226     }
227     else
228       return CDIO_INVALID_LBA;
229   }
230 
231   if(psz_field >= CDIO_CD_SECS_PER_MIN)
232     return CDIO_INVALID_LBA;
233 
234   ret += cdio_msf3_to_lba (0, psz_field, 0);
235 
236   c = *psz_mmssff++;
237   if (isdigit(c))
238     psz_field = (c - '0');
239   else
240     return -1;
241   if('\0' != (c = *psz_mmssff++)) {
242     if (isdigit(c)) {
243       psz_field = psz_field * 10 + (c - '0');
244       c = *psz_mmssff++;
245     }
246     else
247       return CDIO_INVALID_LBA;
248   }
249 
250   if('\0' != c)
251     return CDIO_INVALID_LBA;
252 
253   if(psz_field >= CDIO_CD_FRAMES_PER_SEC)
254     return CDIO_INVALID_LBA;
255 
256   ret += psz_field;
257 
258   return ret;
259 }
260 
261 
262 /*
263  * Local variables:
264  *  c-file-style: "gnu"
265  *  tab-width: 8
266  *  indent-tabs-mode: nil
267  * End:
268  */
269