File: compression_adapter.h

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (140 lines) | stat: -rw-r--r-- 5,567 bytes parent folder | download
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
/*
	This file is part of Warzone 2100.
	Copyright (C) 2025  Warzone 2100 Project

	Warzone 2100 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.

	Warzone 2100 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 Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#pragma once

#include "net_result.h"

#include <stddef.h>
#include <stdint.h>
#include <vector>

/// <summary>
/// Generic facade for integration of various compression algorithms into WZ's
/// networking code.
///
/// Provides very basic facilities to initialize compression/decompression streams
/// and perform compression/decompression tasks.
/// </summary>
class ICompressionAdapter
{
public:

	virtual ~ICompressionAdapter() = default;

	/// <summary>
	/// The first thing that should be called on an `ICompressionAdapter` instance
	/// before doing anything else with it.
	///
	/// Initializes states of internal buffers, streams, etc.
	/// </summary>
	/// <returns>
	/// In case of failure, returns an error code describing the error.
	/// </returns>
	virtual net::result<void> initialize() = 0;

	/// <summary>
	/// Executes the compression routine against `src` buffer of a given size.
	/// The result (compressed buffer) can be later accessed via `compressionOutBuffer()` function.
	///
	/// This function should be used to incrementally compress incoming data.
	///
	/// Note, that it won't necessarily flush all the data to the output buffer,
	/// so one would then need to call `flushCompressionStream()` to flush all
	/// internal streams to the output buffer.
	/// </summary>
	/// <param name="src">Source buffer containing uncompressed data</param>
	/// <param name="size">Size of the source buffer in bytes</param>
	/// <returns>
	/// In case of failure, returns an error code describing the error.
	/// </returns>
	virtual net::result<void> compress(const void* src, size_t size) = 0;
	/// <summary>
	/// Flushes the internal compression stream to the output buffer.
	/// The resulting data can be accessed via `compressionOutBuffer()`
	/// accessor function.
	/// </summary>
	/// <returns>
	/// In case of failure, returns an error code describing the error.
	/// </returns>
	virtual net::result<void> flushCompressionStream() = 0;
	/// <summary>
	/// Accessor function (non-const) for compression output buffer (this will be the
	/// implicit destination to `compress()` and `flushCompressionStream()` functions.
	/// </summary>
	virtual std::vector<uint8_t>& compressionOutBuffer() = 0;
	/// <summary>
	/// Accessor function (const) for compression output buffer (this will be the
	/// implicit destination to `compress()` and `flushCompressionStream()` functions.
	/// </summary>
	virtual const std::vector<uint8_t>& compressionOutBuffer() const = 0;

	/// <summary>
	/// Decompress the data from the compressed buffer into `dst` output buffer
	/// of a given size.
	///
	/// The caller is responsible for ensuring that `dst` is long enough to
	/// hold the decompression result of `size` length in bytes.
	///
	/// One would also need to call `resetDecompressionStreamInput()` function
	/// first to specify the input buffer (containing compressed data) before
	/// calling this function.
	///
	/// </summary>
	/// <param name="dst">Destination buffer, where the decompressed data will go</param>
	/// <param name="size">Size of the destination buffer</param>
	/// <returns>
	/// In case of failure, returns an error code describing the error.
	/// </returns>
	virtual net::result<void> decompress(void* dst, size_t size) = 0;
	/// <summary>
	/// Accessor function (non-const) for decompression input buffer (this will be
	/// the implicit source for `decompress()` function).
	/// </summary>
	virtual std::vector<uint8_t>& decompressionInBuffer() = 0;
	/// <summary>
	/// Accessor function (const) for decompression input buffer (this will be
	/// the implicit source for `decompress()` function).
	/// </summary>
	virtual const std::vector<uint8_t>& decompressionInBuffer() const = 0;
	/// <summary>
	/// Remaining free space (in bytes) at the decompression output buffer.
	/// </summary>
	virtual size_t availableSpaceToDecompress() const = 0;
	/// <summary>
	/// Returns `true` if the internal decompression stream doesn't have any more
	/// available bytes in the input buffer (all input was consumed by a prior
	/// `decompress()` function call).
	/// </summary>
	virtual bool decompressionStreamConsumedAllInput() const = 0;
	/// <summary>
	/// Returns `true` if the decompression algorithm needs to process more input (i.e.
	/// there's more data available to process).
	/// </summary>
	virtual bool decompressionNeedInput() const = 0;
	/// <summary>
	/// Update the decompression algorithm on whether it needs more input or not.
	/// </summary>
	virtual void setDecompressionNeedInput(bool needInput) = 0;
	/// <summary>
	/// Updates the available input bytes count at the decompression input stream.
	/// </summary>
	/// <param name="size">New size for the decompression input stream</param>
	virtual void resetDecompressionStreamInputSize(size_t size) = 0;
};