libfacade 1.1
A library for manipulating PNG images with payloads.
Loading...
Searching...
No Matches
exception.hpp
Go to the documentation of this file.
1#ifndef __FACADE_EXCEPTION_HPP
2#define __FACADE_EXCEPTION_HPP
3
7
8#include <cstdint>
9#include <exception>
10#include <sstream>
11#include <string>
12
13namespace facade
14{
15namespace exception
16{
19 class Exception : public std::exception
20 {
21 public:
23 std::string error;
24
25 Exception() : std::exception() {}
26 Exception(const std::string &error) : error(error), std::exception() {}
27
30 const char *what() const noexcept {
31 return this->error.c_str();
32 }
33 };
34
40 {
41 public:
42 InvalidChunkTag() : Exception("Invalid chunk tag: the string provided was not a valid chunk tag. "
43 "Chunk tags can only be 4 characters long.") {}
44 };
45
47 class OutOfBounds : public Exception
48 {
49 public:
51 std::size_t given;
53 std::size_t boundary;
54
55 OutOfBounds(std::size_t given, std::size_t boundary) : given(given), boundary(boundary), Exception() {
56 std::stringstream stream;
57
58 stream << "Out of bounds: the given index is "
59 << given
60 << ", but the boundary is "
61 << boundary;
62
63 this->error = stream.str();
64 }
65 };
66
68 class NullPointer : public Exception
69 {
70 public:
71 NullPointer() : Exception("Null pointer: encountered a null pointer where it wasn't expected.") {}
72 };
73
76 {
77 public:
78 BadPNGSignature() : Exception("Bad PNG signature: the signature header of the PNG file was not valid.") {}
79 };
80
82 class BadCRC : public Exception
83 {
84 public:
86 std::uint32_t given;
88 std::uint32_t expected;
89
90 BadCRC(std::uint32_t given, std::uint32_t expected) : given(given), expected(expected), Exception() {
91 std::stringstream stream;
92
93 stream << "Bad CRC: the given CRC was "
94 << std::hex << std::showbase << std::uppercase
95 << given
96 << ", but the expected CRC was "
97 << expected;
98
99 this->error = stream.str();
100 }
101 };
102
105 {
106 public:
108 std::string filename;
109
111 std::stringstream stream;
112
113 stream << "Open file failure: failed to open file with name \""
114 << filename
115 << "\"";
116
117 this->error = stream.str();
118 }
119 };
120
123 {
124 public:
126 std::size_t given;
128 std::size_t minimum;
129
130 InsufficientSize(std::size_t given, std::size_t minimum) : given(given), minimum(minimum), Exception() {
131 std::stringstream stream;
132
133 stream << "Insufficient size: given a size of "
134 << given
135 << ", but needed at least "
136 << minimum;
137
138 this->error = stream.str();
139 }
140 };
141
144 {
145 public:
146 NoHeaderChunk() : Exception("No header chunk: the header chunk for the PNG image was not found.") {}
147 };
148
150 class ZLibError : public Exception
151 {
152 public:
155 int code;
156
158 std::stringstream stream;
159
160 stream << "ZLib error: ZLib encountered an error while compressing/decompressiong, error code "
161 << code;
162
163 this->error = stream.str();
164 }
165 };
166
169 {
170 public:
171 NoImageDataChunks() : Exception("No image data chunks: there were no image data (IDAT) chunks found in the PNG image.") {}
172 };
173
175 class NoImageData : public Exception
176 {
177 public:
178 NoImageData() : Exception("No image data: either the image data was not decompressed from the IDAT chunks "
179 "or the canvas was not initialized.") {}
180 };
181
184 {
185 public:
186 PixelMismatch() : Exception("Pixel mismatch: the given pixel type does not match with either the header type or the "
187 "raw pixel data.") {}
188 };
189
191 class NoPixels : public Exception
192 {
193 public:
194 NoPixels() : Exception("No pixels: there are no pixels in the given scanline.") {}
195 };
196
199 {
200 public:
202 std::uint8_t color_type;
203
205 std::stringstream stream;
206
207 stream << "Invalid color type: the encountered color type value (" << static_cast<int>(color_type) << ") is invalid.";
208
209 this->error = stream.str();
210 }
211 };
212
217 {
218 public:
220 std::uint8_t bit_depth;
221
223 std::stringstream stream;
224
225 stream << "Invalid bit depth: the encountered bit depth (" << bit_depth << ") is invalid. ";
226 stream << "Valid values can be 1, 2, 4, 8 and 16.";
227
228 this->error = stream.str();
229 }
230 };
231
234 {
235 public:
236 ScanlineMismatch() : Exception("Scanline mismatch: the given scanline does not match the target scanline.") {}
237 };
238
241 {
242 public:
244 std::uint8_t filter_type;
245
247 std::stringstream stream;
248
249 stream << "Invalid filter type: the given filter type " << static_cast<int>(filter_type) << " was not a valid value.";
250
251 this->error = stream.str();
252 }
253 };
254
257 {
258 public:
259 AlreadyFiltered() : Exception("Already filtered: the given scanline has already had a filter applied to it.") {}
260 };
261
264 {
265 public:
267 std::size_t given;
268
270 std::size_t max_;
271
272 IntegerOverflow(std::size_t given, std::size_t max) : given(given), max_(max), Exception() {
273 std::stringstream stream;
274
275 stream << "Integer overflow: the given number was " << given
276 << ", but the maximum value is " << max;
277
278 this->error = stream.str();
279 }
280 };
281
283 class NoData : public Exception
284 {
285 public:
286 NoData() : Exception("No data: no data was provided.") {}
287 };
288
291 {
292 public:
294 std::size_t pixel_type;
295
297 std::stringstream stream;
298
299 stream << "Invalid pixel type: the given pixel type, " << pixel_type << ", is invalid.";
300
301 this->error = stream.str();
302 }
303 };
304
306 class NoKeyword : public Exception
307 {
308 public:
309 NoKeyword() : Exception("No keyword: there is no keyword present in the text chunk.") {}
310 };
311
316 {
317 public:
318 KeywordTooLong() : Exception("Keyword too long: the keyword in a text chunk can only be 79 characters long.") {}
319 };
320
322 class TextNotFound : public Exception
323 {
324 public:
325 TextNotFound() : Exception("Text not found: the given text was not found in the PNG image.") {}
326 };
327
331 {
332 public:
334 char c;
335
337 std::stringstream stream;
338
339 stream << "Invalid Base64 character: the given character, '" << c << "', is not a valid Base64 character.";
340
341 this->error = stream.str();
342 }
343 };
344
347 {
348 public:
350 std::string string;
351
352 InvalidBase64String(const std::string &string) : string(string), Exception() {
353 std::stringstream stream;
354
355 stream << "Invalid Base64 string: the given string is not a valid Base64 string: " << string;
356
357 this->error = stream.str();
358 }
359 };
360
363 {
364 public:
366 std::size_t pixel_type;
367
369 std::stringstream stream;
370
371 stream << "Unsupported pixel type: the given pixel-type value, " << pixel_type << ", is unsupported for the given operation.";
372
373 this->error = stream.str();
374 }
375 };
376
379 {
380 public:
382 std::size_t given;
384 std::size_t needed;
385
386 ImageTooSmall(std::size_t given, std::size_t needed) : given(given), needed(needed), Exception() {
387 std::stringstream stream;
388
389 stream << "Image too small: the image wasn't large enough to support the operation. The image can hold "
390 << given << " bytes of data, but needed at least " << needed;
391
392 this->error = stream.str();
393 }
394 };
395
397 class NoStegoData : public Exception
398 {
399 public:
400 NoStegoData() : Exception("No stego data: there is no steganographic data within the image.") {}
401 };
402
405 {
406 public:
408 std::string tag;
409
410 ChunkNotFound(std::string tag) : tag(tag), Exception() {
411 std::stringstream stream;
412
413 stream << "Chunk not found: chunks with the tag \"" << tag << "\" could not be found in the image.";
414
415 this->error = stream.str();
416 }
417 };
418
421 {
422 public:
424 std::size_t offset;
425
427 std::stringstream stream;
428
429 stream << "Invalid bit offset: the given bit offset, " << offset << ", was not divisble by 4.";
430
431 this->error = stream.str();
432 }
433 };
434
437 {
438 public:
439 NoTrailingData() : Exception("No trailing data: the image has no trailing data.") {}
440 };
441
444 {
445 public:
446 InvalidIconHeader() : Exception("Invalid icon header: the data does not represent a valid icon file.") {}
447 };
448
450 class NoIconData : public Exception
451 {
452 public:
453 NoIconData() : Exception("No icon data: there is no image data present in the icon object.") {}
454 };
455
457 class NoPNGIcon : public Exception
458 {
459 public:
460 NoPNGIcon() : Exception("No PNG icon: the given icon file does not have a PNG section present.") {}
461 };
462}}
463#endif
An exception thrown when the PNG image has already been filtered.
Definition: exception.hpp:257
AlreadyFiltered()
Definition: exception.hpp:259
An exception thrown when the CRC didn't match expectations.
Definition: exception.hpp:83
std::uint32_t expected
The expected CRC.
Definition: exception.hpp:88
BadCRC(std::uint32_t given, std::uint32_t expected)
Definition: exception.hpp:90
std::uint32_t given
The bad CRC.
Definition: exception.hpp:86
An exception thrown when the signature header of a given binary stream is not a PNG header.
Definition: exception.hpp:76
BadPNGSignature()
Definition: exception.hpp:78
An exception thrown when the chunk is not found in the PNG data.
Definition: exception.hpp:405
ChunkNotFound(std::string tag)
Definition: exception.hpp:410
std::string tag
The chunk tag which could not be found.
Definition: exception.hpp:408
The base exception that all exceptions in this library are built upon.
Definition: exception.hpp:20
const char * what() const noexcept
Get a C-string representation of the error. Adds compatibility with std::exception.
Definition: exception.hpp:30
Exception()
Definition: exception.hpp:25
Exception(const std::string &error)
Definition: exception.hpp:26
std::string error
The resulting error string provided by the exception.
Definition: exception.hpp:23
An exception thrown when the possible space provided by the image data is too small for the given ope...
Definition: exception.hpp:379
std::size_t needed
The amount of space needed by the operation.
Definition: exception.hpp:384
std::size_t given
The amount of data the given image can hold, in bytes.
Definition: exception.hpp:382
ImageTooSmall(std::size_t given, std::size_t needed)
Definition: exception.hpp:386
An exception thrown when there was not enough data to complete the operation.
Definition: exception.hpp:123
std::size_t given
The offending size.
Definition: exception.hpp:126
InsufficientSize(std::size_t given, std::size_t minimum)
Definition: exception.hpp:130
std::size_t minimum
The minimum size needed to complete the operation.
Definition: exception.hpp:128
An exception thrown when a given integer overflows.
Definition: exception.hpp:264
std::size_t given
The given value which caused the overflow.
Definition: exception.hpp:267
std::size_t max_
The maximum value that can be given.
Definition: exception.hpp:270
IntegerOverflow(std::size_t given, std::size_t max)
Definition: exception.hpp:272
An exception thrown when encountering an invalid Base64 character.
Definition: exception.hpp:331
char c
The offending character which was encountered.
Definition: exception.hpp:334
InvalidBase64Character(char c)
Definition: exception.hpp:336
An exception thrown when encountering an invalid base64 string.
Definition: exception.hpp:347
InvalidBase64String(const std::string &string)
Definition: exception.hpp:352
std::string string
The offending string.
Definition: exception.hpp:350
An exception thrown when the given bit depth isn't a valid value.
Definition: exception.hpp:217
std::uint8_t bit_depth
The offending bit depth value.
Definition: exception.hpp:220
InvalidBitDepth(std::uint8_t bit_depth)
Definition: exception.hpp:222
An exception thrown when the given bit offset is not a multiple of 4.
Definition: exception.hpp:421
InvalidBitOffset(std::size_t offset)
Definition: exception.hpp:426
std::size_t offset
The offending offset.
Definition: exception.hpp:424
An exception thrown when a provided chunk tag is not valid.
Definition: exception.hpp:40
InvalidChunkTag()
Definition: exception.hpp:42
An exception thrown when the color type does not match the facade::png::ColorType enum.
Definition: exception.hpp:199
std::uint8_t color_type
The offending color type value.
Definition: exception.hpp:202
InvalidColorType(std::uint8_t color_type)
Definition: exception.hpp:204
An exception thrown when the given filter type does not match the facade::png::FilterType enum.
Definition: exception.hpp:241
InvalidFilterType(std::uint8_t filter_type)
Definition: exception.hpp:246
std::uint8_t filter_type
The offending filter type value.
Definition: exception.hpp:244
An exception thrown when encountering an invalid icon file.
Definition: exception.hpp:444
InvalidIconHeader()
Definition: exception.hpp:446
An exception thrown when the pixel type does not match the facade::png::PixelEnum enum.
Definition: exception.hpp:291
std::size_t pixel_type
The offending pixel type value.
Definition: exception.hpp:294
InvalidPixelType(std::size_t pixel_type)
Definition: exception.hpp:296
An exception thrown when the given keyword is too long for the tEXt or zTXt chunk.
Definition: exception.hpp:316
KeywordTooLong()
Definition: exception.hpp:318
An exception thrown when no data is provided.
Definition: exception.hpp:284
NoData()
Definition: exception.hpp:286
An exception thrown when no header chunk is present in a PNG file.
Definition: exception.hpp:144
NoHeaderChunk()
Definition: exception.hpp:146
An exception thrown when encountering no image data within an icon object.
Definition: exception.hpp:451
NoIconData()
Definition: exception.hpp:453
An exception thrown when no image data chunks are present in the PNG file.
Definition: exception.hpp:169
NoImageDataChunks()
Definition: exception.hpp:171
An exception thrown when image data is not currently loaded in the PNG object.
Definition: exception.hpp:176
NoImageData()
Definition: exception.hpp:178
An exception thrown when no keyword is present in the given tEXt or zTXt chunk of a PNG image.
Definition: exception.hpp:307
NoKeyword()
Definition: exception.hpp:309
An exception thrown when no PNG image is present in icon data.
Definition: exception.hpp:458
NoPNGIcon()
Definition: exception.hpp:460
An exception thrown when no pixels appear in a given scanline.
Definition: exception.hpp:192
NoPixels()
Definition: exception.hpp:194
An exception thrown when no steganographic data is present.
Definition: exception.hpp:398
NoStegoData()
Definition: exception.hpp:400
An exception thrown when no trailing data is present in the PNG image.
Definition: exception.hpp:437
NoTrailingData()
Definition: exception.hpp:439
An exception thrown when encountering a null pointer.
Definition: exception.hpp:69
NullPointer()
Definition: exception.hpp:71
An exception thrown when a file couldn't be opened, either for reading or writing.
Definition: exception.hpp:105
std::string filename
The file which could not be opened.
Definition: exception.hpp:108
OpenFileFailure(const std::string &filename)
Definition: exception.hpp:110
An exception thrown when the operation goes out of bounds.
Definition: exception.hpp:48
std::size_t boundary
The boundary the offense broke.
Definition: exception.hpp:53
std::size_t given
The offending boundary.
Definition: exception.hpp:51
OutOfBounds(std::size_t given, std::size_t boundary)
Definition: exception.hpp:55
An exception thrown when pixel types do not match up.
Definition: exception.hpp:184
PixelMismatch()
Definition: exception.hpp:186
An exception thrown when the scanline pixel types do not match up.
Definition: exception.hpp:234
ScanlineMismatch()
Definition: exception.hpp:236
An exception returned when the given tEXt or zTXt section parameters (typically the keyword) is not f...
Definition: exception.hpp:323
TextNotFound()
Definition: exception.hpp:325
An exception thrown when the given pixel enum type is not supported.
Definition: exception.hpp:363
UnsupportedPixelType(std::size_t pixel_type)
Definition: exception.hpp:368
std::size_t pixel_type
The offending pixel type.
Definition: exception.hpp:366
An exception thrown when encountering a ZLib error.
Definition: exception.hpp:151
int code
The code returned by zlib.
Definition: exception.hpp:155
ZLibError(int code)
Definition: exception.hpp:157
Definition: exception.hpp:14