Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/lspc/ChunkReader.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 14 янв. 2018 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/common/endian.h>
23 #include <lsp-plug.in/stdlib/string.h>
24 #include <lsp-plug.in/fmt/lspc/lspc.h>
25 #include <lsp-plug.in/fmt/lspc/ChunkReader.h>
26
27 namespace lsp
28 {
29 namespace lspc
30 {
31 ChunkReader::ChunkReader(Resource *fd, uint32_t magic, uint32_t uid):
32 ChunkAccessor(fd, magic),
33 sStream(this)
34 {
35 nUnread = 0;
36 nBufTail = 0;
37 nFileOff = 0;
38 nPosition = 0;
39 nUID = uid;
40 bLast = false;
41 }
42
43 ChunkReader::~ChunkReader()
44 {
45 }
46
47 ssize_t ChunkReader::read(void *buf, size_t count)
48 {
49 if (pFile == NULL)
50 return -set_error(STATUS_CLOSED);
51
52 chunk_header_t hdr;
53
54 uint8_t *dst = static_cast<uint8_t *>(buf);
55 ssize_t total = 0;
56
57 while (count > 0)
58 {
59 size_t to_read = nBufTail - nBufPos;
60
61 if (to_read > 0) // There is data in the buffer?
62 {
63 if (to_read > count)
64 to_read = count;
65
66 // Copy memory from buffer
67 memcpy(dst, &pBuffer[nBufPos], to_read);
68
69 // Update pointer
70 dst += to_read;
71 nBufPos += to_read;
72 nPosition += to_read;
73 count -= to_read;
74 total += to_read;
75 }
76 else if (nUnread > 0)
77 {
78 if (nUnread <= count)
79 {
80 // Read data
81 ssize_t n = pFile->read(nFileOff, dst, nUnread);
82 if (n <= 0)
83 return total;
84
85 // Update pointer
86 dst += n;
87 count -= n;
88 total += n;
89 nUnread -= n;
90 nFileOff += n;
91 nPosition += n;
92 }
93 else // Fill buffer
94 {
95 to_read = (nUnread < nBufSize) ? nUnread : nBufSize;
96
97 // Read data
98 ssize_t n = pFile->read(nFileOff, pBuffer, to_read);
99 if (n <= 0)
100 return total;
101
102 // Update pointer
103 nBufPos = 0;
104 nBufTail = n;
105 nFileOff += n;
106 nUnread -= n;
107 }
108 }
109 else // Seek for the next valid chunk
110 {
111 // There is no chunk after current
112 if (bLast)
113 {
114 set_error(STATUS_EOF);
115 return total;
116 }
117
118 // Read chunk header
119 ssize_t n = pFile->read(nFileOff, &hdr, sizeof(chunk_header_t));
120 if (n < ssize_t(sizeof(chunk_header_t)))
121 {
122 set_error(STATUS_EOF);
123 return total;
124 }
125 nFileOff += sizeof(chunk_header_t);
126
127 hdr.magic = BE_TO_CPU(hdr.magic);
128 hdr.flags = BE_TO_CPU(hdr.flags);
129 hdr.size = BE_TO_CPU(hdr.size);
130 hdr.uid = BE_TO_CPU(hdr.uid);
131
132 // Validate chunk header
133 if ((hdr.magic == nMagic) && (hdr.uid == nUID)) // We've found our chunk, remember unread bytes count
134 {
135 bLast = hdr.flags & LSPC_CHUNK_FLAG_LAST;
136 nUnread = hdr.size;
137 }
138 else // Skip this chunk
139 nFileOff += hdr.size;
140 }
141 }
142
143 return total;
144 }
145
146 ssize_t ChunkReader::read_header(void *hdr, size_t size)
147 {
148 if (size < sizeof(header_t))
149 return -set_error(STATUS_BAD_ARGUMENTS);
150
151 // Read header data first
152 header_t shdr;
153 ssize_t count = read(&shdr, sizeof(header_t));
154 if (count < 0)
155 return count;
156 else if (count < ssize_t(sizeof(header_t)))
157 return -set_error(STATUS_EOF); // Unexpected end of file
158
159 // Now read header
160 chunk_raw_header_t *dhdr = reinterpret_cast<chunk_raw_header_t *>(hdr);
161 size_t hdr_size = BE_TO_CPU(shdr.size);
162 if (hdr_size < sizeof(header_t)) // header size should be at least of sizeof(lspc_header_t)
163 return -set_error(STATUS_CORRUPTED_FILE);
164 dhdr->common.size = uint32_t(hdr_size);
165 dhdr->common.version = BE_TO_CPU(shdr.version);
166 hdr_size -= sizeof(header_t);
167 size -= sizeof(header_t);
168
169 // Read header contents
170 ssize_t to_read = lsp_min(size, hdr_size);
171 count = read(&dhdr->data, to_read);
172 if (count < 0)
173 return count;
174 else if (count < to_read)
175 return -set_error(STATUS_EOF); // Unexpected end of file
176
177 // Analyze size of header
178 if (size < hdr_size) // Requested size less than actual header size?
179 {
180 // We need to skip extra bytes that do not fit into header
181 to_read = hdr_size - size;
182 count = skip(to_read);
183 if (count < 0)
184 return count;
185 else if (count < to_read)
186 return -set_error(STATUS_EOF); // Unexpected end of file
187
188 // Patch the header size to be at most of size bytes
189 dhdr->common.size = uint32_t(size + sizeof(header_t));
190 }
191 else if (size > hdr_size)
192 bzero(&dhdr->data[count], size - hdr_size);
193
194 return dhdr->common.size;
195 }
196
197 ssize_t ChunkReader::skip(size_t count)
198 {
199 if (pFile == NULL)
200 return -set_error(STATUS_CLOSED);
201
202 chunk_header_t hdr;
203
204 ssize_t total = 0;
205
206 while (count > 0)
207 {
208 size_t to_read = nBufTail - nBufPos;
209
210 if (to_read > 0) // There is data in the buffer?
211 {
212 if (to_read > count)
213 to_read = count;
214
215 // Update pointer
216 nBufPos += to_read;
217 count -= to_read;
218 total += to_read;
219 }
220 else if (nUnread > 0)
221 {
222 if (nUnread <= count)
223 {
224 // Update counters
225 count -= nUnread;
226 total += nUnread;
227 nFileOff += nUnread;
228 nUnread = 0;
229 }
230 else // Fill buffer
231 {
232 nUnread -= count;
233 nFileOff += count;
234 total += count;
235 count = 0;
236 }
237 }
238 else // Seek for the next valid chunk
239 {
240 // There is no chunk after current
241 if (bLast)
242 {
243 set_error(STATUS_EOF);
244 return total;
245 }
246
247 // Read chunk header
248 ssize_t n = pFile->read(nFileOff, &hdr, sizeof(chunk_header_t));
249 if (n < ssize_t(sizeof(chunk_header_t)))
250 {
251 set_error(STATUS_EOF);
252 return 0;
253 }
254 nFileOff += sizeof(chunk_header_t);
255
256 hdr.magic = BE_TO_CPU(hdr.magic);
257 hdr.flags = BE_TO_CPU(hdr.flags);
258 hdr.size = BE_TO_CPU(hdr.size);
259 hdr.uid = BE_TO_CPU(hdr.uid);
260
261 // Validate chunk header
262 if ((hdr.magic == nMagic) && (hdr.uid == nUID)) // We've found our chunk, remember unread bytes count
263 {
264 bLast = hdr.flags & LSPC_CHUNK_FLAG_LAST;
265 nUnread = hdr.size;
266 }
267 else // Skip this chunk
268 nFileOff += hdr.size;
269 }
270 }
271
272 return total;
273 }
274 }
275
276 } /* namespace lsp */