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
|
// $Id: io_decorators.cpp,v 1.4 2002/07/02 22:13:40 t1mpy Exp $
// id3lib: a C++ library for creating and manipulating id3v1/v2 tags
// Copyright 1999, 2000 Scott Thomas Haug
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// 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 Library General Public
// License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with this library; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// The id3lib authors encourage improvements and optimisations to be sent to
// the id3lib coordinator. Please see the README file for details on where to
// send such submissions. See the AUTHORS file for a list of people who have
// contributed to id3lib. See the ChangeLog file for a list of changes to
// id3lib. These files are distributed with id3lib at
// http://download.sourceforge.net/id3lib/
#if defined HAVE_CONFIG_H
#include <config.h>
#endif
#include "id3/io_decorators.h" //has "readers.h" "io_helpers.h" "utils.h"
#include "zlib.h"
using namespace dami;
void io::WindowedReader::setWindow(pos_type beg, size_type size)
{
ID3D_NOTICE( "WindowedReader::setWindow() [beg, size] = [" <<
this->getBeg() << ", " << size << "]" );
pos_type cur = this->getCur();
// reset the end marker so as to avoid errors
this->setEnd(_reader.getEnd());
// set the beginning marker
this->setBeg(beg);
// since the characters might be more than a byte in size, we need to
// manually get all the chars to set the window appropriately
this->setCur(beg);
ID3D_NOTICE( "WindowedReader::setWindow(): after setCur(beg), cur = "<<
this->getCur() );
this->skipChars(size);
ID3D_NOTICE( "WindowedReader::setWindow(): after skipChars, cur = " <<
this->getCur() );
this->setEnd(this->getCur());
ID3D_NOTICE( "WindowedReader::setWindow() [beg, cur, end] = [" << this->getBeg() << ", " << this->getCur() << ", " << this->getEnd() << "]" );
// reset the stream
this->setCur(cur);
}
ID3_Reader::pos_type io::WindowedReader::setBeg(pos_type beg)
{
// make sure the position we want to set to isn't past the current
// end position or the superclass's beginning position
if (beg <= this->getEnd() && beg >= _reader.getBeg())
{
_beg = beg;
}
else if (beg > this->getEnd())
{
ID3D_WARNING( "WindowedReader::setBeg() failed, [beg, _end] = " <<
beg << ", " << this->getEnd() << "]" );
}
else
{
ID3D_WARNING( "WindowedReader::setBeg() failed, [beg, _beg] = " <<
beg << ", " << this->getBeg() << "]" );
}
return _beg;
}
ID3_Reader::pos_type io::WindowedReader::setEnd(pos_type end)
{
// make sure the position we want to set to isn't beforen the current
// beginning position or the superclass's end position
if (this->getBeg() <= end && end <= _reader.getEnd())
{
_end = end;
}
else
{
ID3D_WARNING( "WindowedReader::setEnd() failed, end = " << end );
ID3D_WARNING( "WindowedReader::setEnd() failed, beg = " <<
this->getBeg() );
ID3D_WARNING( "WindowedReader::setEnd() failed, super.end = " <<
_reader.getEnd() );
}
return _end;
}
ID3_Reader::int_type io::WindowedReader::readChar()
{
int_type ch = END_OF_READER;
if (this->inWindow())
{
ch = _reader.readChar();
}
else
{
ID3D_WARNING( "io::WindowedReader::readChar: not in window, " <<
"pos = " << this->getCur() << ", window = [" <<
this->getBeg() << ", " << this->getEnd() << "]");
}
return ch;
}
ID3_Reader::int_type io::WindowedReader::peekChar()
{
int_type ch = END_OF_READER;
if (this->inWindow())
{
ch = _reader.peekChar();
}
return ch;
}
ID3_Reader::size_type io::WindowedReader::readChars(char_type buf[], size_type len)
{
pos_type cur = this->getCur();
size_type size = 0;
if (this->inWindow(cur))
{
size = _reader.readChars(buf, min<size_type>(len, _end - cur));
}
return size;
}
ID3_Reader::size_type io::CharReader::readChars(char_type buf[], size_type len)
{
size_type numChars = 0;
ID3D_NOTICE( "CharReader::readChars(): len = " << len );
for (; numChars < len; ++numChars)
{
if (this->atEnd())
{
break;
}
char_type ch = this->readChar();
if (buf != NULL)
{
buf[numChars] = ch;
}
}
ID3D_NOTICE( "CharReader::readChars(): numChars = " << len );
return numChars;
}
ID3_Reader::int_type io::LineFeedReader::readChar()
{
if (this->atEnd())
{
return END_OF_READER;
}
char_type ch = _reader.readChar();
if (ch == 0x0D && this->peekChar() == 0x0A)
{
ID3D_NOTICE( "LineFeedReader::readChar(): found CRLF at pos " <<
this->getCur() );
ch = _reader.readChar();
}
return ch;
};
ID3_Reader::int_type io::UnsyncedReader::readChar()
{
if (this->atEnd())
{
return END_OF_READER;
}
char_type ch = _reader.readChar();
if (ch == 0xFF && this->peekChar() == 0x00)
{
ID3D_NOTICE( "UnsyncedReader::readChar(): found sync at pos " <<
this->getCur() );
_reader.readChar();
}
return ch;
}
io::CompressedReader::CompressedReader(ID3_Reader& reader, size_type newSize)
: _uncompressed(new char_type[newSize])
{
size_type oldSize = reader.remainingBytes();
BString binary = readBinary(reader, oldSize);
::uncompress(_uncompressed,
reinterpret_cast<luint*>(&newSize),
reinterpret_cast<const uchar*>(binary.data()),
oldSize);
this->setBuffer(_uncompressed, newSize);
}
io::CompressedReader::~CompressedReader()
{
delete [] _uncompressed;
}
ID3_Writer::int_type io::UnsyncedWriter::writeChar(char_type ch)
{
if (_last == 0xFF && (ch == 0x00 || ch >= 0xE0))
{
_writer.writeChar('\0');
_numSyncs++;
}
_last = _writer.writeChar(ch);
return _last;
}
void io::UnsyncedWriter::flush()
{
if (_last == 0xFF)
{
_last = _writer.writeChar('\0');
_numSyncs++;
}
_writer.flush();
}
ID3_Writer::size_type
io::UnsyncedWriter::writeChars(const char_type buf[], size_type len)
{
pos_type beg = this->getCur();
ID3D_NOTICE( "UnsyncedWriter::writeChars(): len = " << len );
for (size_t i = 0; i < len; ++i)
{
if (this->atEnd())
{
break;
}
this->writeChar(buf[i]);
}
size_type numChars = this->getCur() - beg;
ID3D_NOTICE( "CharWriter::writeChars(): numChars = " << numChars );
return numChars;
}
void io::CompressedWriter::flush()
{
if (_data.size() == 0)
{
return;
}
const char_type* data = reinterpret_cast<const char_type*>(_data.data());
size_type dataSize = _data.size();
_origSize = dataSize;
// The zlib documentation specifies that the destination size needs to
// be an unsigned long at least 0.1% larger than the source buffer,
// plus 12 bytes
unsigned long newDataSize = dataSize + (dataSize / 10) + 12;
char_type* newData = new char_type[newDataSize];
if (::compress(newData, &newDataSize, data, dataSize) != Z_OK)
{
// log this
ID3D_WARNING("io::CompressedWriter: error compressing");
_writer.writeChars(data, dataSize);
}
else if (newDataSize < dataSize)
{
ID3D_NOTICE("io::CompressedWriter: compressed size = " << newDataSize << ", original size = " << dataSize );
_writer.writeChars(newData, newDataSize);
}
else
{
ID3D_NOTICE("io::CompressedWriter: no compression!compressed size = " << newDataSize << ", original size = " << dataSize );
_writer.writeChars(data, dataSize);
}
delete [] newData;
_data.erase();
}
ID3_Writer::size_type
io::CompressedWriter::writeChars(const char_type buf[], size_type len)
{
ID3D_NOTICE("io::CompressedWriter: writing chars: " << len );
_data.append(buf, len);
return len;
}
|