Debian Codegraph @

modules/lsp-runtime-lib/src/main/resource/BuiltinLoader.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: 15 мар. 2021 г.
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/debug.h>
23 #include <lsp-plug.in/lltl/darray.h>
24 #include <lsp-plug.in/resource/BuiltinLoader.h>
25 #include <lsp-plug.in/resource/Decompressor.h>
26
27 namespace lsp
28 {
29 namespace resource
30 {
31 BuiltinLoader::BuiltinLoader()
32 {
33 pData = NULL;
34 nDataSize = 0;
35 pCatalog = NULL;
36 nCatSize = 0;
37 nBufRank = 0;
38 }
39
40 BuiltinLoader::~BuiltinLoader()
41 {
42 pData = NULL;
43 nDataSize = 0;
44 pCatalog = NULL;
45 nCatSize = 0;
46 nBufRank = 0;
47 }
48
49 status_t BuiltinLoader::init(
50 const void *data, size_t data_size,
51 const raw_resource_t *catalog, size_t catalog_size,
52 size_t log_buf_size
53 )
54 {
55 pData = reinterpret_cast<const uint8_t *>(data);
56 nDataSize = data_size;
57 pCatalog = catalog;
58 nCatSize = catalog_size;
59 nBufRank = log_buf_size;
60
61 return STATUS_OK;
62 }
63
64 status_t BuiltinLoader::find_entry(ssize_t *out, const io::Path *path)
65 {
66 status_t res;
67 ssize_t index = -1;
68 LSPString item;
69 io::Path tmp;
70
71 // Copy path data
72 if ((res = tmp.set(path)) != STATUS_OK)
73 return res;
74
75 while (true)
76 {
77 // Get first item and remove it
78 if ((res = tmp.remove_first(&item)) != STATUS_OK)
79 return res;
80
81 // Lookup for existing directory/create yet another one
82 const raw_resource_t *found = NULL;
83 for (size_t i=0; i<nCatSize; ++i)
84 {
85 const raw_resource_t *ent = &pCatalog[i];
86
87 if ((ent == NULL) || (ent->parent != index) || (ent->name == NULL))
88 continue;
89 if (item.equals_utf8(ent->name))
90 {
91 found = ent;
92 index = i;
93 break;
94 }
95 }
96
97 // Check that item exists
98 if (found == NULL)
99 return STATUS_NOT_FOUND;
100
101 // Last entry?
102 if (tmp.is_empty())
103 {
104 *out = index;
105 return STATUS_OK;
106 }
107 else if (found->type != RES_DIR)
108 return STATUS_NOT_FOUND;
109 }
110 }
111
112 io::IInStream *BuiltinLoader::read_stream(const io::Path *name)
113 {
114 ssize_t index = 0;
115
116 // Find entry
117 status_t res = find_entry(&index, name);
118 if (res != STATUS_OK)
119 {
120 set_error(res);
121 return NULL;
122 }
123
124 // Check type
125 const raw_resource_t *ent = &pCatalog[index];
126 if (ent->type != RES_FILE)
127 {
128 set_error(STATUS_IS_DIRECTORY);
129 return NULL;
130 }
131
132 // Create decompressor
133 Decompressor *d = new Decompressor();
134 if (d == NULL)
135 {
136 set_error(STATUS_NO_MEM);
137 return NULL;
138 }
139
140 // Initialize decompressor and skip the desired amount of data to access the entry
141 res = d->init(&pData[ent->segment], nDataSize - ent->segment, ent->offset + ent->length, nBufRank);
142 if (res == STATUS_OK)
143 {
144 wssize_t skipped = d->skip(ent->offset);
145 if (skipped != ent->offset)
146 res = (skipped < 0) ? status_t(-skipped) : STATUS_CORRUPTED;
147 }
148
149 if (res == STATUS_OK)
150 return d;
151
152 delete d;
153 set_error(res);
154 return NULL;
155 }
156
157 ssize_t BuiltinLoader::enumerate(const io::Path *path, resource_t **list)
158 {
159 ssize_t index = 0;
160 lltl::darray<resource_t> xlist;
161 const raw_resource_t *ent;
162
163 // Root directory?
164 if ((path->is_empty()) || (path->equals(FILE_SEPARATOR_S)))
165 index = -1;
166 else
167 {
168 // Find entry and check that it is of directory type
169 status_t res = find_entry(&index, path);
170 if (res != STATUS_OK)
171 return -set_error(res);
172
173 ent = &pCatalog[index];
174 if (ent->type != RES_DIR)
175 return -set_error(STATUS_NOT_DIRECTORY);
176 }
177
178 // Now create list of nested items
179 for (size_t i=0; i<nCatSize; ++i)
180 {
181 ent = &pCatalog[i];
182 if ((ent == NULL) || (ent->parent != index) || (ent->name == NULL))
183 continue;
184
185 resource_t *item = xlist.add();
186 if (item == NULL)
187 return -set_error(STATUS_NO_MEM);
188
189 strncpy(item->name, ent->name, RESOURCE_NAME_MAX);
190 item->name[RESOURCE_NAME_MAX - 1] = '\0';
191 item->type = ent->type;
192 }
193
194 // Return result
195 index = xlist.size();
196 *list = xlist.release();
197
198 set_error(STATUS_OK);
199 return index;
200 }
201 } /* namespace resource */
202 } /* namespace lsp */
203
204