Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 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/ChunkWriter.h>
25
26 namespace lsp
27 {
28 namespace lspc
29 {
30 ChunkWriter::ChunkWriter(Resource *fd, uint32_t magic):
31 ChunkAccessor(fd, magic),
32 sStream(this)
33 {
34 nChunksOut = 0;
35 nPosition = 0;
36 if (last_error() != STATUS_OK)
37 return;
38 if (pFile != NULL)
39 set_error(pFile->allocate(&nUID));
40 }
41
42 ChunkWriter::~ChunkWriter()
43 {
44 }
45
46 status_t ChunkWriter::do_flush(size_t flags)
47 {
48 if (pFile == NULL)
49 return set_error(STATUS_CLOSED);
50
51 if ((nBufPos > 0) || ((flags & F_FORCE) && (nChunksOut <= 0)) || (flags & F_LAST))
52 {
53 chunk_header_t hdr;
54 hdr.magic = nMagic;
55 hdr.size = uint32_t(nBufPos);
56 hdr.flags = (flags & F_LAST) ? LSPC_CHUNK_FLAG_LAST : 0;
57 hdr.uid = nUID;
58
59 // Convert CPU -> BE
60 hdr.magic = CPU_TO_BE(hdr.magic);
61 hdr.size = CPU_TO_BE(hdr.size);
62 hdr.flags = CPU_TO_BE(hdr.flags);
63 hdr.uid = CPU_TO_BE(hdr.uid);
64
65 // Write buffer header and data to file
66 status_t res = pFile->write(&hdr, sizeof(chunk_header_t));
67 if (res == STATUS_OK)
68 pFile->write(pBuffer, nBufPos);
69 if (set_error(res) != STATUS_OK)
70 return res;
71
72 // Flush the buffer
73 nBufPos = 0;
74 nChunksOut ++;
75 }
76
77 return STATUS_OK;
78 }
79
80 status_t ChunkWriter::write(const void *buf, size_t count)
81 {
82 if (pFile == NULL)
83 return set_error(STATUS_CLOSED);
84
85 chunk_header_t hdr;
86 const uint8_t *src = static_cast<const uint8_t *>(buf);
87
88 while (count > 0)
89 {
90 size_t can_write = nBufSize - nBufPos;
91
92 if ((nBufPos > 0) || (count < can_write))
93 {
94 if (can_write > count)
95 can_write = count;
96
97 // Copy data to buffer
98 ::memcpy(&pBuffer[nBufPos], src, can_write);
99 nBufPos += can_write;
100 count -= can_write;
101 src += can_write;
102
103 // Check buffer size
104 if (nBufPos >= nBufSize)
105 {
106 hdr.magic = nMagic;
107 hdr.size = uint32_t(nBufSize);
108 hdr.flags = 0;
109 hdr.uid = nUID;
110
111 // Convert CPU -> BE
112 hdr.magic = CPU_TO_BE(hdr.magic);
113 hdr.size = CPU_TO_BE(hdr.size);
114 hdr.flags = CPU_TO_BE(hdr.flags);
115 hdr.uid = CPU_TO_BE(hdr.uid);
116
117 // Write buffer header and data to file
118 status_t res = pFile->write(&hdr, sizeof(chunk_header_t));
119 if (res == STATUS_OK)
120 res = pFile->write(pBuffer, nBufSize);
121 if (set_error(res) != STATUS_OK)
122 return res;
123
124 // Update position and counter
125 nBufPos = 0;
126 nPosition += nBufSize;
127 nChunksOut ++;
128 }
129 }
130 else // Write directly avoiding buffer
131 {
132 hdr.magic = nMagic;
133 hdr.size = uint32_t(can_write);
134 hdr.flags = 0;
135 hdr.uid = nUID;
136
137 // Convert CPU -> BE
138 hdr.magic = CPU_TO_BE(hdr.magic);
139 hdr.size = CPU_TO_BE(hdr.size);
140 hdr.flags = CPU_TO_BE(hdr.flags);
141 hdr.uid = CPU_TO_BE(hdr.uid);
142
143 // Write buffer header and data to file
144 status_t res = pFile->write(&hdr, sizeof(chunk_header_t));
145 if (res == STATUS_OK)
146 pFile->write(src, can_write);
147 if (set_error(res) != STATUS_OK)
148 return res;
149
150 // Update position and counter
151 count -= can_write;
152 src += can_write;
153 nPosition += can_write;
154 nChunksOut ++;
155 }
156 }
157
158 return set_error(STATUS_OK);
159 }
160
161 status_t ChunkWriter::write_header(const void *buf)
162 {
163 if (pFile == NULL)
164 return set_error(STATUS_CLOSED);
165
166 const header_t *phdr = static_cast<const header_t *>(buf);
167 if (phdr->size < sizeof(header_t))
168 return set_error(STATUS_BAD_ARGUMENTS);
169
170 // Write encoded header
171 header_t shdr;
172 shdr.size = CPU_TO_BE(phdr->size);
173 shdr.version = CPU_TO_BE(phdr->version);
174 status_t res = write(&shdr, sizeof(shdr));
175 if (res != STATUS_OK)
176 return res;
177
178 // Write header data
179 return write(&phdr[1], phdr->size - sizeof(header_t));
180 }
181
182 status_t ChunkWriter::flush()
183 {
184 return do_flush(0);
185 }
186
187 status_t ChunkWriter::close()
188 {
189 status_t result = do_flush(F_FORCE | F_LAST);
190 status_t result2 = ChunkAccessor::close();
191 return set_error((result == STATUS_OK) ? result2 : result);
192 }
193
194 } /* namespace lspc */
195 } /* namespace lsp */