| 1 | /* This file is part of the KDE libraries |
| 2 | SPDX-FileCopyrightText: 2011 Mario Bensi <mbensi@ipsquad.net> |
| 3 | |
| 4 | Based on kbzip2filter: |
| 5 | SPDX-FileCopyrightText: 2000, 2009 David Faure <faure@kde.org> |
| 6 | |
| 7 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 | */ |
| 9 | |
| 10 | #include "knonefilter.h" |
| 11 | |
| 12 | #include <QFile> |
| 13 | |
| 14 | class Q_DECL_HIDDEN KNoneFilter::Private |
| 15 | { |
| 16 | public: |
| 17 | Private() |
| 18 | : mode(0) |
| 19 | , avail_out(0) |
| 20 | , avail_in(0) |
| 21 | , next_in(nullptr) |
| 22 | , next_out(nullptr) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | int mode; |
| 27 | int avail_out; |
| 28 | int avail_in; |
| 29 | const char *next_in; |
| 30 | char *next_out; |
| 31 | }; |
| 32 | |
| 33 | KNoneFilter::KNoneFilter() |
| 34 | : d(new Private) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | KNoneFilter::~KNoneFilter() |
| 39 | { |
| 40 | delete d; |
| 41 | } |
| 42 | |
| 43 | bool KNoneFilter::init(int mode) |
| 44 | { |
| 45 | d->mode = mode; |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | int KNoneFilter::mode() const |
| 50 | { |
| 51 | return d->mode; |
| 52 | } |
| 53 | |
| 54 | bool KNoneFilter::terminate() |
| 55 | { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | void KNoneFilter::reset() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | bool KNoneFilter::() |
| 64 | { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool KNoneFilter::(const QByteArray & /*fileName*/) |
| 69 | { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | void KNoneFilter::setOutBuffer(char *data, uint maxlen) |
| 74 | { |
| 75 | d->avail_out = maxlen; |
| 76 | d->next_out = data; |
| 77 | } |
| 78 | |
| 79 | void KNoneFilter::setInBuffer(const char *data, uint size) |
| 80 | { |
| 81 | d->next_in = data; |
| 82 | d->avail_in = size; |
| 83 | } |
| 84 | |
| 85 | int KNoneFilter::inBufferAvailable() const |
| 86 | { |
| 87 | return d->avail_in; |
| 88 | } |
| 89 | |
| 90 | int KNoneFilter::outBufferAvailable() const |
| 91 | { |
| 92 | return d->avail_out; |
| 93 | } |
| 94 | |
| 95 | KNoneFilter::Result KNoneFilter::uncompress() |
| 96 | { |
| 97 | #ifndef NDEBUG |
| 98 | if (d->mode != QIODevice::ReadOnly) { |
| 99 | return KFilterBase::Error; |
| 100 | } |
| 101 | #endif |
| 102 | return copyData(); |
| 103 | } |
| 104 | |
| 105 | KNoneFilter::Result KNoneFilter::compress(bool finish) |
| 106 | { |
| 107 | Q_ASSERT(d->mode == QIODevice::WriteOnly); |
| 108 | Q_UNUSED(finish); |
| 109 | |
| 110 | return copyData(); |
| 111 | } |
| 112 | |
| 113 | KNoneFilter::Result KNoneFilter::copyData() |
| 114 | { |
| 115 | Q_ASSERT(d->avail_out > 0); |
| 116 | if (d->avail_in > 0) { |
| 117 | const int n = qMin(a: d->avail_in, b: d->avail_out); |
| 118 | memcpy(dest: d->next_out, src: d->next_in, n: n); |
| 119 | d->avail_out -= n; |
| 120 | d->next_in += n; |
| 121 | d->next_out += n; |
| 122 | d->avail_in -= n; |
| 123 | return KFilterBase::Ok; |
| 124 | } else { |
| 125 | return KFilterBase::End; |
| 126 | } |
| 127 | } |
| 128 | |