aboutsummaryrefslogtreecommitdiffstats
path: root/taglib/toolkit/tfile.cpp
blob: 055b0d6d30f48353c4e6c0878a7d16e0feac75c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/***************************************************************************
    copyright            : (C) 2002 - 2008 by Scott Wheeler
    email                : wheeler@kde.org
 ***************************************************************************/

/***************************************************************************
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License version   *
 *   2.1 as published by the Free Software Foundation.                     *
 *                                                                         *
 *   This library 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     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
 *   02110-1301  USA                                                       *
 *                                                                         *
 *   Alternatively, this file is available under the Mozilla Public        *
 *   License Version 1.1.  You may obtain a copy of the License at         *
 *   http://www.mozilla.org/MPL/                                           *
 ***************************************************************************/

#include "tfile.h"
#include "tfilestream.h"
#include "tstring.h"
#include "tdebug.h"
#include "tpropertymap.h"

#ifdef _WIN32
# include <windows.h>
# include <io.h>
#else
# include <stdio.h>
# include <unistd.h>
#endif

#ifndef R_OK
# define R_OK 4
#endif
#ifndef W_OK
# define W_OK 2
#endif

#include "asffile.h"
#include "mpegfile.h"
#include "vorbisfile.h"
#include "flacfile.h"
#include "oggflacfile.h"
#include "mpcfile.h"
#include "mp4file.h"
#include "wavpackfile.h"
#include "speexfile.h"
#include "opusfile.h"
#include "trueaudiofile.h"
#include "aifffile.h"
#include "wavfile.h"
#include "apefile.h"
#include "modfile.h"
#include "s3mfile.h"
#include "itfile.h"
#include "xmfile.h"
#include "mp4file.h"
#include "dsffile.h"
#include "dsdifffile.h"

using namespace TagLib;

class File::FilePrivate
{
public:
  FilePrivate(IOStream *stream, bool owner) :
    stream(stream),
    streamOwner(owner),
    valid(true) {}

  ~FilePrivate()
  {
    if(streamOwner)
      delete stream;
  }

  IOStream *stream;
  bool streamOwner;
  bool valid;
};

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

File::File(FileName fileName) :
  d(new FilePrivate(new FileStream(fileName), true))
{
}

File::File(IOStream *stream) :
  d(new FilePrivate(stream, false))
{
}

File::~File()
{
  delete d;
}

FileName File::name() const
{
  return d->stream->name();
}

PropertyMap File::properties() const
{
  // ugly workaround until this method is virtual
  if(dynamic_cast<const APE::File* >(this))
    return dynamic_cast<const APE::File* >(this)->properties();
  if(dynamic_cast<const FLAC::File* >(this))
    return dynamic_cast<const FLAC::File* >(this)->properties();
  if(dynamic_cast<const IT::File* >(this))
    return dynamic_cast<const IT::File* >(this)->properties();
  if(dynamic_cast<const Mod::File* >(this))
    return dynamic_cast<const Mod::File* >(this)->properties();
  if(dynamic_cast<const MPC::File* >(this))
    return dynamic_cast<const MPC::File* >(this)->properties();
  if(dynamic_cast<const MPEG::File* >(this))
    return dynamic_cast<const MPEG::File* >(this)->properties();
  if(dynamic_cast<const Ogg::FLAC::File* >(this))
    return dynamic_cast<const Ogg::FLAC::File* >(this)->properties();
  if(dynamic_cast<const Ogg::Speex::File* >(this))
    return dynamic_cast<const Ogg::Speex::File* >(this)->properties();
  if(dynamic_cast<const Ogg::Opus::File* >(this))
    return dynamic_cast<const Ogg::Opus::File* >(this)->properties();
  if(dynamic_cast<const Ogg::Vorbis::File* >(this))
    return dynamic_cast<const Ogg::Vorbis::File* >(this)->properties();
  if(dynamic_cast<const RIFF::AIFF::File* >(this))
    return dynamic_cast<const RIFF::AIFF::File* >(this)->properties();
  if(dynamic_cast<const RIFF::WAV::File* >(this))
    return dynamic_cast<const RIFF::WAV::File* >(this)->properties();
  if(dynamic_cast<const S3M::File* >(this))
    return dynamic_cast<const S3M::File* >(this)->properties();
  if(dynamic_cast<const TrueAudio::File* >(this))
    return dynamic_cast<const TrueAudio::File* >(this)->properties();
  if(dynamic_cast<const WavPack::File* >(this))
    return dynamic_cast<const WavPack::File* >(this)->properties();
  if(dynamic_cast<const XM::File* >(this))
    return dynamic_cast<const XM::File* >(this)->properties();
  if(dynamic_cast<const MP4::File* >(this))
    return dynamic_cast<const MP4::File* >(this)->properties();
  if(dynamic_cast<const ASF::File* >(this))
    return dynamic_cast<const ASF::File* >(this)->properties();
  if(dynamic_cast<const DSF::File* >(this))
    return dynamic_cast<const DSF::File* >(this)->properties();
  if(dynamic_cast<const DSDIFF::File* >(this))
    return dynamic_cast<const DSDIFF::File* >(this)->properties();
  return tag()->properties();
}

void File::removeUnsupportedProperties(const StringList &properties)
{
  // here we only consider those formats that could possibly contain
  // unsupported properties
  if(dynamic_cast<APE::File* >(this))
    dynamic_cast<APE::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<FLAC::File* >(this))
    dynamic_cast<FLAC::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<MPC::File* >(this))
    dynamic_cast<MPC::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<MPEG::File* >(this))
    dynamic_cast<MPEG::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<Ogg::Vorbis::File* >(this))
    dynamic_cast<Ogg::Vorbis::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<RIFF::AIFF::File* >(this))
    dynamic_cast<RIFF::AIFF::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<RIFF::WAV::File* >(this))
    dynamic_cast<RIFF::WAV::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<TrueAudio::File* >(this))
    dynamic_cast<TrueAudio::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<WavPack::File* >(this))
    dynamic_cast<WavPack::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<MP4::File* >(this))
    dynamic_cast<MP4::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<ASF::File* >(this))
    dynamic_cast<ASF::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<DSF::File* >(this))
    dynamic_cast<DSF::File* >(this)->removeUnsupportedProperties(properties);
  else if(dynamic_cast<DSDIFF::File* >(this))
    dynamic_cast<DSDIFF::File* >(this)->removeUnsupportedProperties(properties);
  else
    tag()->removeUnsupportedProperties(properties);
}

PropertyMap File::setProperties(const PropertyMap &properties)
{
  if(dynamic_cast<APE::File* >(this))
    return dynamic_cast<APE::File* >(this)->setProperties(properties);
  else if(dynamic_cast<FLAC::File* >(this))
    return dynamic_cast<FLAC::File* >(this)->setProperties(properties);
  else if(dynamic_cast<IT::File* >(this))
    return dynamic_cast<IT::File* >(this)->setProperties(properties);
  else if(dynamic_cast<Mod::File* >(this))
    return dynamic_cast<Mod::File* >(this)->setProperties(properties);
  else if(dynamic_cast<MPC::File* >(this))
    return dynamic_cast<MPC::File* >(this)->setProperties(properties);
  else if(dynamic_cast<MPEG::File* >(this))
    return dynamic_cast<MPEG::File* >(this)->setProperties(properties);
  else if(dynamic_cast<Ogg::FLAC::File* >(this))
    return dynamic_cast<Ogg::FLAC::File* >(this)->setProperties(properties);
  else if(dynamic_cast<Ogg::Speex::File* >(this))
    return dynamic_cast<Ogg::Speex::File* >(this)->setProperties(properties);
  else if(dynamic_cast<Ogg::Opus::File* >(this))
    return dynamic_cast<Ogg::Opus::File* >(this)->setProperties(properties);
  else if(dynamic_cast<Ogg::Vorbis::File* >(this))
    return dynamic_cast<Ogg::Vorbis::File* >(this)->setProperties(properties);
  else if(dynamic_cast<RIFF::AIFF::File* >(this))
    return dynamic_cast<RIFF::AIFF::File* >(this)->setProperties(properties);
  else if(dynamic_cast<RIFF::WAV::File* >(this))
    return dynamic_cast<RIFF::WAV::File* >(this)->setProperties(properties);
  else if(dynamic_cast<S3M::File* >(this))
    return dynamic_cast<S3M::File* >(this)->setProperties(properties);
  else if(dynamic_cast<TrueAudio::File* >(this))
    return dynamic_cast<TrueAudio::File* >(this)->setProperties(properties);
  else if(dynamic_cast<WavPack::File* >(this))
    return dynamic_cast<WavPack::File* >(this)->setProperties(properties);
  else if(dynamic_cast<XM::File* >(this))
    return dynamic_cast<XM::File* >(this)->setProperties(properties);
  else if(dynamic_cast<MP4::File* >(this))
    return dynamic_cast<MP4::File* >(this)->setProperties(properties);
  else if(dynamic_cast<ASF::File* >(this))
    return dynamic_cast<ASF::File* >(this)->setProperties(properties);
  else if(dynamic_cast<DSF::File* >(this))
    return dynamic_cast<DSF::File* >(this)->setProperties(properties);
  else if(dynamic_cast<DSDIFF::File* >(this))
    return dynamic_cast<DSDIFF::File* >(this)->setProperties(properties);
  else
    return tag()->setProperties(properties);
}

ByteVector File::readBlock(unsigned long length)
{
  return d->stream->readBlock(length);
}

void File::writeBlock(const ByteVector &data)
{
  d->stream->writeBlock(data);
}

long File::find(const ByteVector &pattern, long fromOffset, const ByteVector &before)
{
  if(!d->stream || pattern.size() > bufferSize())
      return -1;

  // The position in the file that the current buffer starts at.

  long bufferOffset = fromOffset;
  ByteVector buffer;

  // These variables are used to keep track of a partial match that happens at
  // the end of a buffer.

  int previousPartialMatch = -1;
  int beforePreviousPartialMatch = -1;

  // Save the location of the current read pointer.  We will restore the
  // position using seek() before all returns.

  long originalPosition = tell();

  // Start the search at the offset.

  seek(fromOffset);

  // This loop is the crux of the find method.  There are three cases that we
  // want to account for:
  //
  // (1) The previously searched buffer contained a partial match of the search
  // pattern and we want to see if the next one starts with the remainder of
  // that pattern.
  //
  // (2) The search pattern is wholly contained within the current buffer.
  //
  // (3) The current buffer ends with a partial match of the pattern.  We will
  // note this for use in the next iteration, where we will check for the rest
  // of the pattern.
  //
  // All three of these are done in two steps.  First we check for the pattern
  // and do things appropriately if a match (or partial match) is found.  We
  // then check for "before".  The order is important because it gives priority
  // to "real" matches.

  for(buffer = readBlock(bufferSize()); buffer.size() > 0; buffer = readBlock(bufferSize())) {

    // (1) previous partial match

    if(previousPartialMatch >= 0 && int(bufferSize()) > previousPartialMatch) {
      const int patternOffset = (bufferSize() - previousPartialMatch);
      if(buffer.containsAt(pattern, 0, patternOffset)) {
        seek(originalPosition);
        return bufferOffset - bufferSize() + previousPartialMatch;
      }
    }

    if(!before.isEmpty() && beforePreviousPartialMatch >= 0 && int(bufferSize()) > beforePreviousPartialMatch) {
      const int beforeOffset = (bufferSize() - beforePreviousPartialMatch);
      if(buffer.containsAt(before, 0, beforeOffset)) {
        seek(originalPosition);
        return -1;
      }
    }

    // (2) pattern contained in current buffer

    long location = buffer.find(pattern);
    if(location >= 0) {
      seek(originalPosition);
      return bufferOffset + location;
    }

    if(!before.isEmpty() && buffer.find(before) >= 0) {
      seek(originalPosition);
      return -1;
    }

    // (3) partial match

    previousPartialMatch = buffer.endsWithPartialMatch(pattern);

    if(!before.isEmpty())
      beforePreviousPartialMatch = buffer.endsWithPartialMatch(before);

    bufferOffset += bufferSize();
  }

  // Since we hit the end of the file, reset the status before continuing.

  clear();

  seek(originalPosition);

  return -1;
}


long File::rfind(const ByteVector &pattern, long fromOffset, const ByteVector &before)
{
  if(!d->stream || pattern.size() > bufferSize())
      return -1;

  // The position in the file that the current buffer starts at.

  ByteVector buffer;

  // These variables are used to keep track of a partial match that happens at
  // the end of a buffer.

  /*
  int previousPartialMatch = -1;
  int beforePreviousPartialMatch = -1;
  */

  // Save the location of the current read pointer.  We will restore the
  // position using seek() before all returns.

  long originalPosition = tell();

  // Start the search at the offset.

  if(fromOffset == 0)
    fromOffset = length();

  long bufferLength = bufferSize();
  long bufferOffset = fromOffset + pattern.size();

  // See the notes in find() for an explanation of this algorithm.

  while(true) {

    if(bufferOffset > bufferLength) {
      bufferOffset -= bufferLength;
    }
    else {
      bufferLength = bufferOffset;
      bufferOffset = 0;
    }
    seek(bufferOffset);

    buffer = readBlock(bufferLength);
    if(buffer.isEmpty())
      break;

    // TODO: (1) previous partial match

    // (2) pattern contained in current buffer

    const long location = buffer.rfind(pattern);
    if(location >= 0) {
      seek(originalPosition);
      return bufferOffset + location;
    }

    if(!before.isEmpty() && buffer.find(before) >= 0) {
      seek(originalPosition);
      return -1;
    }

    // TODO: (3) partial match
  }

  // Since we hit the end of the file, reset the status before continuing.

  clear();

  seek(originalPosition);

  return -1;
}

void File::insert(const ByteVector &data, unsigned long start, unsigned long replace)
{
  d->stream->insert(data, start, replace);
}

void File::removeBlock(unsigned long start, unsigned long length)
{
  d->stream->removeBlock(start, length);
}

bool File::readOnly() const
{
  return d->stream->readOnly();
}

bool File::isOpen() const
{
  return d->stream->isOpen();
}

bool File::isValid() const
{
  return isOpen() && d->valid;
}

void File::seek(long offset, Position p)
{
  d->stream->seek(offset, IOStream::Position(p));
}

void File::truncate(long length)
{
  d->stream->truncate(length);
}

void File::clear()
{
  d->stream->clear();
}

long File::tell() const
{
  return d->stream->tell();
}

long File::length()
{
  return d->stream->length();
}

bool File::isReadable(const char *file)
{

#if defined(_MSC_VER) && (_MSC_VER >= 1400)  // VC++2005 or later

  return _access_s(file, R_OK) == 0;

#else

  return access(file, R_OK) == 0;

#endif

}

bool File::isWritable(const char *file)
{

#if defined(_MSC_VER) && (_MSC_VER >= 1400)  // VC++2005 or later

  return _access_s(file, W_OK) == 0;

#else

  return access(file, W_OK) == 0;

#endif

}

////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

unsigned int File::bufferSize()
{
  return 1024;
}

void File::setValid(bool valid)
{
  d->valid = valid;
}