1 /*
2     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 #include "k3bdeviceglobals.h"
6 #include "k3bdevice.h"
7 #include "k3bdevice_i18n.h"
8 #include "k3bdiskinfo.h"
9 
10 #include <QDebug>
11 #include <QStringList>
12 
13 
deviceTypeString(int t)14 QString K3b::Device::deviceTypeString( int t )
15 {
16     QStringList s;
17     if( t & K3b::Device::DEVICE_CD_R )
18         s += i18n("CD-R");
19     if( t & K3b::Device::DEVICE_CD_RW )
20         s += i18n("CD-RW");
21     if( t & K3b::Device::DEVICE_CD_ROM )
22         s += i18n("CD-ROM");
23     if( t & K3b::Device::DEVICE_DVD_ROM )
24         s += i18n("DVD-ROM");
25     if( t & K3b::Device::DEVICE_DVD_RAM )
26         s += i18n("DVD-RAM");
27     if( t & K3b::Device::DEVICE_DVD_R )
28         s += i18n("DVD-R");
29     if( t & K3b::Device::DEVICE_DVD_RW )
30         s += i18n("DVD-RW");
31     if( t & K3b::Device::DEVICE_DVD_R_DL )
32         s += i18n("DVD-R DL");
33     if( t & DEVICE_HD_DVD_ROM )
34         s += i18n("HD DVD-ROM");
35     if( t & DEVICE_HD_DVD_R )
36         s += i18n("HD DVD-R");
37     if( t & DEVICE_HD_DVD_RAM )
38         s += i18n("HD DVD-RAM");
39     if( t & DEVICE_BD_ROM )
40         s += i18n("BD-ROM");
41     if( t & DEVICE_BD_R )
42         s += i18n("BD-R");
43     if( t & DEVICE_BD_RE )
44         s += i18n("BD-RE");
45     if( t & K3b::Device::DEVICE_DVD_PLUS_R )
46         s += i18n("DVD+R");
47     if( t & K3b::Device::DEVICE_DVD_PLUS_RW )
48         s += i18n("DVD+RW");
49     if( t & K3b::Device::DEVICE_DVD_PLUS_R_DL )
50         s += i18n("DVD+R DL");
51 
52     if( s.isEmpty() )
53         return i18n("Error");
54     else
55         return s.join( ", " );
56 }
57 
58 
writingModeString(int m)59 QString K3b::Device::writingModeString( int m )
60 {
61     QStringList s;
62     if( m & K3b::Device::WRITINGMODE_SAO )
63         s += i18n("SAO");
64     if( m & K3b::Device::WRITINGMODE_TAO )
65         s += i18n("TAO");
66     if( m & K3b::Device::WRITINGMODE_RAW )
67         s += i18n("RAW");
68     if( m & K3b::Device::WRITINGMODE_SAO_R96P )
69         s += i18n("SAO/R96P");
70     if( m & K3b::Device::WRITINGMODE_SAO_R96R )
71         s += i18n("SAO/R96R");
72     if( m & K3b::Device::WRITINGMODE_RAW_R16 )
73         s += i18n("RAW/R16");
74     if( m & K3b::Device::WRITINGMODE_RAW_R96P )
75         s += i18n("RAW/R96P");
76     if( m & K3b::Device::WRITINGMODE_RAW_R96R )
77         s += i18n("RAW/R96R");
78     if( m & K3b::Device::WRITINGMODE_INCR_SEQ )
79         s += i18n("Incremental Sequential");
80     if( m & K3b::Device::WRITINGMODE_RES_OVWR )
81         s += i18n("Restricted Overwrite");
82     if( m & K3b::Device::WRITINGMODE_LAYER_JUMP )
83         s += i18n("Layer Jump");
84 
85     if( m & K3b::Device::WRITINGMODE_RRM )
86         s += i18n("Random Recording");
87     if( m & K3b::Device::WRITINGMODE_SRM )
88         s += i18n("Sequential Recording");
89     if( m & K3b::Device::WRITINGMODE_SRM_POW )
90         s += i18n("Sequential Recording + POW");
91 
92     if( s.isEmpty() )
93         return i18nc("no writing mode", "None");
94     else
95         return s.join( ", " );
96 }
97 
98 
mediaTypeString(int m,bool simple)99 QString K3b::Device::mediaTypeString( int m, bool simple )
100 {
101     if( m == K3b::Device::MEDIA_UNKNOWN )
102         return i18nc("unknown medium type", "Unknown");
103 
104     QStringList s;
105     if( m & MEDIA_NONE )
106         s += i18n("No media");
107     if( m & MEDIA_DVD_ROM )
108         s += i18n("DVD-ROM");
109     if( m & MEDIA_DVD_R ||
110         (simple && (m & MEDIA_DVD_R_SEQ)) )
111         s += i18n("DVD-R");
112     if( m & MEDIA_DVD_R_SEQ && !simple )
113         s += i18n("DVD-R Sequential");
114     if( m & MEDIA_DVD_R_DL ||
115         (simple && (m & (MEDIA_DVD_R_DL_SEQ|MEDIA_DVD_R_DL_JUMP))) )
116         s += i18n("DVD-R Dual Layer");
117     if( m & MEDIA_DVD_R_DL_SEQ && !simple )
118         s += i18n("DVD-R Dual Layer Sequential");
119     if( m & MEDIA_DVD_R_DL_JUMP && !simple )
120         s += i18n("DVD-R Dual Layer Jump");
121     if( m & MEDIA_DVD_RAM )
122         s += i18n("DVD-RAM");
123     if( m & MEDIA_DVD_RW ||
124         (simple && (m & (MEDIA_DVD_RW_OVWR|MEDIA_DVD_RW_SEQ))) )
125         s += i18n("DVD-RW");
126     if( m & MEDIA_DVD_RW_OVWR && !simple )
127         s += i18n("DVD-RW Restricted Overwrite");
128     if( m & MEDIA_DVD_RW_SEQ && !simple )
129         s += i18n("DVD-RW Sequential");
130     if( m & MEDIA_DVD_PLUS_RW )
131         s += i18n("DVD+RW");
132     if( m & MEDIA_DVD_PLUS_R )
133         s += i18n("DVD+R");
134     if( m & MEDIA_DVD_PLUS_RW_DL )
135         s += i18n("DVD+RW Dual Layer");
136     if( m & MEDIA_DVD_PLUS_R_DL )
137         s += i18n("DVD+R Dual Layer");
138     if( m & MEDIA_CD_ROM )
139         s += i18n("CD-ROM");
140     if( m & MEDIA_CD_R )
141         s += i18n("CD-R");
142     if( m & MEDIA_CD_RW )
143         s += i18n("CD-RW");
144     if( m & MEDIA_HD_DVD_ROM )
145         s += i18n("HD DVD-ROM");
146     if( m & MEDIA_HD_DVD_R )
147         s += i18n("HD DVD-R");
148     if( m & MEDIA_HD_DVD_RAM )
149         s += i18n("HD DVD-RAM");
150     if( m & MEDIA_BD_ROM )
151         s += i18n("BD-ROM");
152     if( m & MEDIA_BD_R ||
153         (simple && (m & (MEDIA_BD_R_SRM|MEDIA_BD_R_RRM))) )
154         s += i18n("BD-R");
155     if( m & MEDIA_BD_R_SRM && !simple )
156         s += i18n("BD-R Sequential (SRM)");
157     if( m & MEDIA_BD_R_SRM_POW && !simple )
158         s += i18n("BD-R Sequential Pseudo Overwrite (SRM+POW)");
159     if( m & MEDIA_BD_R_RRM && !simple )
160         s += i18n("BD-R Random (RRM)");
161     if( m & MEDIA_BD_RE )
162         s += i18n("BD-RE");
163 
164     if( s.isEmpty() )
165         return i18n("Error");
166     else
167         return s.join( ", " );
168 }
169 
170 
mediaStateString(int state)171 QString K3b::Device::mediaStateString( int state )
172 {
173     if( state == K3b::Device::STATE_UNKNOWN )
174         return i18nc("unknown medium state", "Unknown");
175 
176     QStringList s;
177     if( state & STATE_NO_MEDIA )
178         s += i18n("no medium");
179     if( state & STATE_COMPLETE )
180         s += i18n("complete medium");
181     if( state & STATE_INCOMPLETE )
182         s += i18n("incomplete medium");
183     if( state & STATE_EMPTY )
184         s += i18n("empty medium");
185 
186     if( s.isEmpty() )
187         return i18n("Error");
188     else
189         return s.join( ", " );
190 }
191 
192 
debugBitfield(unsigned char * data,long len)193 void K3b::Device::debugBitfield( unsigned char* data, long len )
194 {
195     for( int i = 0; i < len; ++i ) {
196         QString index, bitString;
197         index = QString::asprintf( "%4i", i );
198         for( int bp = 7; bp >= 0; --bp )
199             bitString[7-bp] = ( data[i] & (1<<bp) ? '1' : '0' );
200         qDebug() << index << " - " << bitString << " - " << (int)data[i];
201     }
202 }
203 
204 
from2Byte(const unsigned char * d)205 quint16 K3b::Device::from2Byte(const unsigned char* d)
206 {
207     if (d == NULL) {
208         qWarning() << "Invalid Byte!";
209         return 0;
210     }
211     return ((d[0] << 8 & 0xFF00) |
212             (d[1]      & 0xFF));
213 }
214 
215 
from4Byte(const unsigned char * d)216 quint32 K3b::Device::from4Byte(const unsigned char* d)
217 {
218     if (d == NULL) {
219         qWarning() << "Invalid Byte!";
220         return 0;
221     }
222     return ((d[0] << 24 & 0xFF000000) |
223             (d[1] << 16 & 0xFF0000)   |
224             (d[2] << 8  & 0xFF00)     |
225             (d[3]       & 0xFF));
226 }
227 
228 
fromBcd(const char & i)229 char K3b::Device::fromBcd( const char& i )
230 {
231     return (i & 0x0f) + 10 * ( (i >> 4) & 0x0f );
232 }
233 
234 
toBcd(const char & i)235 char K3b::Device::toBcd( const char& i )
236 {
237     return ( i % 10 ) | ( ( (( i / 10 ) % 10) << 4 ) & 0xf0 );
238 }
239 
240 
isValidBcd(const char & i)241 bool K3b::Device::isValidBcd( const char& i )
242 {
243     return ( i & 0x0f ) <= 0x09 && ( i & 0xf0 ) <= 0x90;
244 }
245 
246 
determineMaxReadingBufferSize(K3b::Device::Device * dev,const K3b::Msf & firstSector)247 int K3b::Device::determineMaxReadingBufferSize( K3b::Device::Device* dev, const K3b::Msf& firstSector )
248 {
249     //
250     // As long as we do not know how to determine the max read buffer properly we simply determine it
251     // by trying. :)
252     //
253 
254     int bufferSizeSectors = 128;
255     unsigned char buffer[2048*128];
256     while( !dev->read10( buffer, 2048*bufferSizeSectors, firstSector.lba(), bufferSizeSectors ) ) {
257         qDebug() << "(K3b::DataTrackReader) determine max read sectors: "
258                  << bufferSizeSectors << " too high." << endl;
259         bufferSizeSectors--;
260     }
261     qDebug() << "(K3b::DataTrackReader) determine max read sectors: "
262              << bufferSizeSectors << " is max." << endl;
263 
264     return bufferSizeSectors;
265 }
266 
267 
operator <<(QDebug & dbg,K3b::Device::MediaType type)268 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::MediaType type )
269 {
270     return dbg << mediaTypeString( type );
271 }
272 
273 
operator <<(QDebug & dbg,K3b::Device::MediaTypes types)274 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::MediaTypes types )
275 {
276     return dbg << mediaTypeString( types );
277 }
278 
279 
operator <<(QDebug & dbg,K3b::Device::WritingMode mode)280 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::WritingMode mode )
281 {
282     return dbg << writingModeString( mode );
283 }
284 
285 
operator <<(QDebug & dbg,K3b::Device::WritingModes modes)286 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::WritingModes modes )
287 {
288     return dbg << writingModeString( modes );
289 }
290 
291 
operator <<(QDebug & dbg,K3b::Device::MediaState state)292 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::MediaState state )
293 {
294     return dbg << mediaStateString( state );
295 }
296 
297 
operator <<(QDebug & dbg,K3b::Device::MediaStates states)298 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::MediaStates states )
299 {
300     return dbg << mediaStateString( states );
301 }
302