1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | // LOVE |
22 | #include "CompressedData.h" |
23 | |
24 | namespace love |
25 | { |
26 | namespace data |
27 | { |
28 | |
29 | love::Type CompressedData::type("CompressedData" , &Data::type); |
30 | |
31 | CompressedData::CompressedData(Compressor::Format format, char *cdata, size_t compressedsize, size_t rawsize, bool own) |
32 | : format(format) |
33 | , data(nullptr) |
34 | , dataSize(compressedsize) |
35 | , originalSize(rawsize) |
36 | { |
37 | if (own) |
38 | data = cdata; |
39 | else |
40 | { |
41 | try |
42 | { |
43 | data = new char[dataSize]; |
44 | } |
45 | catch (std::bad_alloc &) |
46 | { |
47 | throw love::Exception("Out of memory." ); |
48 | } |
49 | |
50 | memcpy(data, cdata, dataSize); |
51 | } |
52 | } |
53 | |
54 | CompressedData::CompressedData(const CompressedData &c) |
55 | : format(c.format) |
56 | , data(nullptr) |
57 | , dataSize(c.dataSize) |
58 | , originalSize(c.originalSize) |
59 | { |
60 | try |
61 | { |
62 | data = new char[dataSize]; |
63 | } |
64 | catch (std::bad_alloc &) |
65 | { |
66 | throw love::Exception("Out of memory." ); |
67 | } |
68 | |
69 | memcpy(data, c.data, dataSize); |
70 | } |
71 | |
72 | CompressedData::~CompressedData() |
73 | { |
74 | delete[] data; |
75 | } |
76 | |
77 | CompressedData *CompressedData::clone() const |
78 | { |
79 | return new CompressedData(*this); |
80 | } |
81 | |
82 | Compressor::Format CompressedData::getFormat() const |
83 | { |
84 | return format; |
85 | } |
86 | |
87 | size_t CompressedData::getDecompressedSize() const |
88 | { |
89 | return originalSize; |
90 | } |
91 | |
92 | void *CompressedData::getData() const |
93 | { |
94 | return data; |
95 | } |
96 | |
97 | size_t CompressedData::getSize() const |
98 | { |
99 | return dataSize; |
100 | } |
101 | |
102 | } // data |
103 | } // love |
104 | |