fs/io: move to io/
This commit is contained in:
59
src/lib/zlib/AutoGunzipReader.cxx
Normal file
59
src/lib/zlib/AutoGunzipReader.cxx
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2003-2021 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include "AutoGunzipReader.hxx"
|
||||
#include "GunzipReader.hxx"
|
||||
|
||||
AutoGunzipReader::AutoGunzipReader(Reader &_next) noexcept
|
||||
:peek(_next) {}
|
||||
|
||||
AutoGunzipReader::~AutoGunzipReader() noexcept = default;
|
||||
|
||||
[[gnu::pure]]
|
||||
static bool
|
||||
IsGzip(const uint8_t data[4]) noexcept
|
||||
{
|
||||
return data[0] == 0x1f && data[1] == 0x8b && data[2] == 0x08 &&
|
||||
(data[3] & 0xe0) == 0;
|
||||
}
|
||||
|
||||
inline void
|
||||
AutoGunzipReader::Detect()
|
||||
{
|
||||
const auto *data = (const uint8_t *)peek.Peek(4);
|
||||
if (data == nullptr) {
|
||||
next = &peek;
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsGzip(data))
|
||||
next = (gunzip = std::make_unique<GunzipReader>(peek)).get();
|
||||
else
|
||||
next = &peek;
|
||||
}
|
||||
|
||||
size_t
|
||||
AutoGunzipReader::Read(void *data, size_t size)
|
||||
{
|
||||
if (next == nullptr)
|
||||
Detect();
|
||||
|
||||
assert(next != nullptr);
|
||||
return next->Read(data, size);
|
||||
}
|
49
src/lib/zlib/AutoGunzipReader.hxx
Normal file
49
src/lib/zlib/AutoGunzipReader.hxx
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2003-2021 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#ifndef MPD_AUTO_GUNZIP_READER_HXX
|
||||
#define MPD_AUTO_GUNZIP_READER_HXX
|
||||
|
||||
#include "io/PeekReader.hxx"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class GunzipReader;
|
||||
|
||||
/**
|
||||
* A filter that detects gzip compression and optionally inserts a
|
||||
* #GunzipReader.
|
||||
*/
|
||||
class AutoGunzipReader final : public Reader {
|
||||
Reader *next = nullptr;
|
||||
PeekReader peek;
|
||||
std::unique_ptr<GunzipReader> gunzip;
|
||||
|
||||
public:
|
||||
explicit AutoGunzipReader(Reader &_next) noexcept;
|
||||
~AutoGunzipReader() noexcept;
|
||||
|
||||
/* virtual methods from class Reader */
|
||||
size_t Read(void *data, size_t size) override;
|
||||
|
||||
private:
|
||||
void Detect();
|
||||
};
|
||||
|
||||
#endif
|
96
src/lib/zlib/GunzipReader.cxx
Normal file
96
src/lib/zlib/GunzipReader.cxx
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "GunzipReader.hxx"
|
||||
#include "lib/zlib/Error.hxx"
|
||||
|
||||
GunzipReader::GunzipReader(Reader &_next)
|
||||
:next(_next)
|
||||
{
|
||||
z.next_in = nullptr;
|
||||
z.avail_in = 0;
|
||||
z.zalloc = Z_NULL;
|
||||
z.zfree = Z_NULL;
|
||||
z.opaque = Z_NULL;
|
||||
|
||||
int result = inflateInit2(&z, 16 + MAX_WBITS);
|
||||
if (result != Z_OK)
|
||||
throw ZlibError(result);
|
||||
}
|
||||
|
||||
inline bool
|
||||
GunzipReader::FillBuffer()
|
||||
{
|
||||
auto w = buffer.Write();
|
||||
assert(!w.empty());
|
||||
|
||||
size_t nbytes = next.Read(w.data, w.size);
|
||||
if (nbytes == 0)
|
||||
return false;
|
||||
|
||||
buffer.Append(nbytes);
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t
|
||||
GunzipReader::Read(void *data, size_t size)
|
||||
{
|
||||
if (eof)
|
||||
return 0;
|
||||
|
||||
z.next_out = (Bytef *)data;
|
||||
z.avail_out = size;
|
||||
|
||||
while (true) {
|
||||
int flush = Z_NO_FLUSH;
|
||||
|
||||
auto r = buffer.Read();
|
||||
if (r.empty()) {
|
||||
if (FillBuffer())
|
||||
r = buffer.Read();
|
||||
else
|
||||
flush = Z_FINISH;
|
||||
}
|
||||
|
||||
z.next_in = r.data;
|
||||
z.avail_in = r.size;
|
||||
|
||||
int result = inflate(&z, flush);
|
||||
if (result == Z_STREAM_END) {
|
||||
eof = true;
|
||||
return size - z.avail_out;
|
||||
} else if (result != Z_OK)
|
||||
throw ZlibError(result);
|
||||
|
||||
buffer.Consume(r.size - z.avail_in);
|
||||
|
||||
if (z.avail_out < size)
|
||||
return size - z.avail_out;
|
||||
}
|
||||
}
|
69
src/lib/zlib/GunzipReader.hxx
Normal file
69
src/lib/zlib/GunzipReader.hxx
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef GUNZIP_READER_HXX
|
||||
#define GUNZIP_READER_HXX
|
||||
|
||||
#include "io/Reader.hxx"
|
||||
#include "util/StaticFifoBuffer.hxx"
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
/**
|
||||
* A filter that decompresses data using zlib.
|
||||
*/
|
||||
class GunzipReader final : public Reader {
|
||||
Reader &next;
|
||||
|
||||
bool eof = false;
|
||||
|
||||
z_stream z;
|
||||
|
||||
StaticFifoBuffer<Bytef, 65536> buffer;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Construct the filter.
|
||||
*
|
||||
* Throws on error.
|
||||
*/
|
||||
explicit GunzipReader(Reader &_next);
|
||||
|
||||
~GunzipReader() noexcept {
|
||||
inflateEnd(&z);
|
||||
}
|
||||
|
||||
/* virtual methods from class Reader */
|
||||
size_t Read(void *data, size_t size) override;
|
||||
|
||||
private:
|
||||
bool FillBuffer();
|
||||
};
|
||||
|
||||
#endif
|
101
src/lib/zlib/GzipOutputStream.cxx
Normal file
101
src/lib/zlib/GzipOutputStream.cxx
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (C) 2014-2018 Max Kellermann <max.kellermann@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "GzipOutputStream.hxx"
|
||||
#include "lib/zlib/Error.hxx"
|
||||
|
||||
GzipOutputStream::GzipOutputStream(OutputStream &_next)
|
||||
:next(_next)
|
||||
{
|
||||
z.next_in = nullptr;
|
||||
z.avail_in = 0;
|
||||
z.zalloc = Z_NULL;
|
||||
z.zfree = Z_NULL;
|
||||
z.opaque = Z_NULL;
|
||||
|
||||
constexpr int windowBits = 15;
|
||||
constexpr int gzip_encoding = 16;
|
||||
|
||||
int result = deflateInit2(&z, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
|
||||
windowBits | gzip_encoding,
|
||||
8, Z_DEFAULT_STRATEGY);
|
||||
if (result != Z_OK)
|
||||
throw ZlibError(result);
|
||||
}
|
||||
|
||||
GzipOutputStream::~GzipOutputStream()
|
||||
{
|
||||
deflateEnd(&z);
|
||||
}
|
||||
|
||||
void
|
||||
GzipOutputStream::Flush()
|
||||
{
|
||||
/* no more input */
|
||||
z.next_in = nullptr;
|
||||
z.avail_in = 0;
|
||||
|
||||
while (true) {
|
||||
Bytef output[16384];
|
||||
z.next_out = output;
|
||||
z.avail_out = sizeof(output);
|
||||
|
||||
int result = deflate(&z, Z_FINISH);
|
||||
if (z.next_out > output)
|
||||
next.Write(output, z.next_out - output);
|
||||
|
||||
if (result == Z_STREAM_END)
|
||||
break;
|
||||
else if (result != Z_OK)
|
||||
throw ZlibError(result);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
GzipOutputStream::Write(const void *_data, size_t size)
|
||||
{
|
||||
/* zlib's API requires non-const input pointer */
|
||||
void *data = const_cast<void *>(_data);
|
||||
|
||||
z.next_in = reinterpret_cast<Bytef *>(data);
|
||||
z.avail_in = size;
|
||||
|
||||
while (z.avail_in > 0) {
|
||||
Bytef output[16384];
|
||||
z.next_out = output;
|
||||
z.avail_out = sizeof(output);
|
||||
|
||||
int result = deflate(&z, Z_NO_FLUSH);
|
||||
if (result != Z_OK)
|
||||
throw ZlibError(result);
|
||||
|
||||
if (z.next_out > output)
|
||||
next.Write(output, z.next_out - output);
|
||||
}
|
||||
}
|
65
src/lib/zlib/GzipOutputStream.hxx
Normal file
65
src/lib/zlib/GzipOutputStream.hxx
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2014-2018 Max Kellermann <max.kellermann@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef GZIP_OUTPUT_STREAM_HXX
|
||||
#define GZIP_OUTPUT_STREAM_HXX
|
||||
|
||||
#include "io/OutputStream.hxx"
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
/**
|
||||
* A filter that compresses data written to it using zlib, forwarding
|
||||
* compressed data in the "gzip" format.
|
||||
*
|
||||
* Don't forget to call Flush() before destructing this object.
|
||||
*/
|
||||
class GzipOutputStream final : public OutputStream {
|
||||
OutputStream &next;
|
||||
|
||||
z_stream z;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Construct the filter.
|
||||
*/
|
||||
explicit GzipOutputStream(OutputStream &_next);
|
||||
~GzipOutputStream();
|
||||
|
||||
/**
|
||||
* Finish the file and write all data remaining in zlib's
|
||||
* output buffer.
|
||||
*/
|
||||
void Flush();
|
||||
|
||||
/* virtual methods from class OutputStream */
|
||||
void Write(const void *data, size_t size) override;
|
||||
};
|
||||
|
||||
#endif
|
@@ -7,6 +7,9 @@ endif
|
||||
zlib = static_library(
|
||||
'zlib',
|
||||
'Error.cxx',
|
||||
'GunzipReader.cxx',
|
||||
'GzipOutputStream.cxx',
|
||||
'AutoGunzipReader.cxx',
|
||||
include_directories: inc,
|
||||
dependencies: [
|
||||
zlib_dep,
|
||||
@@ -17,5 +20,6 @@ zlib_dep = declare_dependency(
|
||||
link_with: zlib,
|
||||
dependencies: [
|
||||
zlib_dep,
|
||||
io_dep,
|
||||
],
|
||||
)
|
||||
|
Reference in New Issue
Block a user