strawberry-audio-player-win.../3rdparty/taglib/tagutils.cpp

109 lines
3.3 KiB
C++
Raw Normal View History

2018-05-10 15:29:28 +02:00
/***************************************************************************
copyright : (C) 2015 by Tsuda Kageyu
email : tsuda.kageyu@gmail.com
***************************************************************************/
/***************************************************************************
* 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 "id3v1tag.h"
#include "id3v2header.h"
#include "apetag.h"
#include "tagutils.h"
using namespace Strawberry_TagLib::TagLib;
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
long long Utils::findID3v1(File *file) {
2020-06-13 19:02:42 +02:00
if (!file->isValid())
2018-05-10 15:29:28 +02:00
return -1;
file->seek(-128, File::End);
2020-06-26 23:30:30 +02:00
const long long p = file->tell();
2018-05-10 15:29:28 +02:00
2020-06-13 19:02:42 +02:00
if (file->readBlock(3) == ID3v1::Tag::fileIdentifier())
2018-05-10 15:29:28 +02:00
return p;
return -1;
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
long long Utils::findID3v2(File *file) {
2020-06-13 19:02:42 +02:00
if (!file->isValid())
2018-05-10 15:29:28 +02:00
return -1;
file->seek(0);
2020-06-13 19:02:42 +02:00
if (file->readBlock(3) == ID3v2::Header::fileIdentifier())
2018-05-10 15:29:28 +02:00
return 0;
return -1;
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
long long Utils::findAPE(File *file, long long id3v1Location) {
2020-06-13 19:02:42 +02:00
if (!file->isValid())
2018-05-10 15:29:28 +02:00
return -1;
2020-06-13 19:02:42 +02:00
if (id3v1Location >= 0)
2018-05-10 15:29:28 +02:00
file->seek(id3v1Location - 32, File::Beginning);
else
file->seek(-32, File::End);
2020-06-26 23:30:30 +02:00
const long long p = file->tell();
2018-05-10 15:29:28 +02:00
2020-06-13 19:02:42 +02:00
if (file->readBlock(8) == APE::Tag::fileIdentifier())
2018-05-10 15:29:28 +02:00
return p;
return -1;
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
ByteVector Strawberry_TagLib::TagLib::Utils::readHeader(IOStream *stream, size_t length, bool skipID3v2, long long *headerOffset) {
2020-06-13 19:02:42 +02:00
if (!stream || !stream->isOpen())
2018-05-10 15:29:28 +02:00
return ByteVector();
2020-06-26 23:30:30 +02:00
const long long originalPosition = stream->tell();
long long bufferOffset = 0;
2018-05-10 15:29:28 +02:00
2020-06-13 19:02:42 +02:00
if (skipID3v2) {
2018-05-10 15:29:28 +02:00
stream->seek(0);
const ByteVector data = stream->readBlock(ID3v2::Header::size());
2020-06-13 19:02:42 +02:00
if (data.startsWith(ID3v2::Header::fileIdentifier()))
2018-05-10 15:29:28 +02:00
bufferOffset = ID3v2::Header(data).completeTagSize();
}
stream->seek(bufferOffset);
const ByteVector header = stream->readBlock(length);
stream->seek(originalPosition);
2020-06-13 19:02:42 +02:00
if (headerOffset)
2018-05-10 15:29:28 +02:00
*headerOffset = bufferOffset;
return header;
2018-05-10 15:29:28 +02:00
}