| 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: 12 авг. 2022 г. |
| 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/fmt/url.h> |
| 24 |
#include <lsp-plug.in/fmt/bookmarks.h> |
| 25 |
#include <lsp-plug.in/fmt/lnk/types.h> |
| 26 |
#include <lsp-plug.in/io/Dir.h> |
| 27 |
#include <lsp-plug.in/io/InFileStream.h> |
| 28 |
#include <lsp-plug.in/io/InSequence.h> |
| 29 |
#include <lsp-plug.in/stdlib/stdlib.h> |
| 30 |
|
| 31 |
namespace lsp |
| 32 |
{ |
| 33 |
namespace bookmarks |
| 34 |
{ |
| 35 |
static inline status_t read_exact(io::IInStream *is, void *dst, size_t count, status_t code) |
| 36 |
{ |
| 37 |
wssize_t res = is->read_fully(dst, count); |
| 38 |
if (res < 0) |
| 39 |
return status_t(-res); |
| 40 |
return (res == wssize_t(count)) ? STATUS_OK : code; |
| 41 |
} |
| 42 |
|
| 43 |
static inline status_t skip_exact(io::IInStream *is, size_t count, status_t code) |
| 44 |
{ |
| 45 |
wssize_t res = is->skip(count); |
| 46 |
if (res < 0) |
| 47 |
return status_t(-res); |
| 48 |
return (res == wssize_t(count)) ? STATUS_OK : code; |
| 49 |
} |
| 50 |
|
| 51 |
static inline size_t utf16_nlen(const lsp_utf16_t *s, size_t limit) |
| 52 |
{ |
| 53 |
for (size_t i=0; i<limit; ++i) |
| 54 |
if (s[i] == 0) |
| 55 |
return i; |
| 56 |
return limit; |
| 57 |
} |
| 58 |
|
| 59 |
static status_t read_string_data(io::IInStream *is, LSPString *s, bool unicode) |
| 60 |
{ |
| 61 |
status_t res; |
| 62 |
lnk::shlink_string_data_t sdata; |
| 63 |
|
| 64 |
// Read the lenght of the string data |
| 65 |
if ((res = read_exact(is, &sdata, sizeof(sdata), STATUS_CORRUPTED)) != STATUS_OK) |
| 66 |
return res; |
| 67 |
|
| 68 |
// Obtain the length |
| 69 |
size_t length = LE_TO_CPU(sdata.length); |
| 70 |
if (length == 0) |
| 71 |
{ |
| 72 |
s->clear(); |
| 73 |
return STATUS_OK; |
| 74 |
} |
| 75 |
|
| 76 |
// Read in unicode mode |
| 77 |
if (unicode) |
| 78 |
{ |
| 79 |
size_t ubytes = length * sizeof(lsp_utf16_t); |
| 80 |
lsp_utf16_t *us = reinterpret_cast<lsp_utf16_t *>(malloc(ubytes)); |
| 81 |
if (us == NULL) |
| 82 |
return STATUS_NO_MEM; |
| 83 |
lsp_finally { free(us); }; |
| 84 |
if ((res = read_exact(is, us, ubytes, STATUS_CORRUPTED)) != STATUS_OK) |
| 85 |
return res; |
| 86 |
return (s->set_utf16le(us, length)) ? STATUS_OK : STATUS_NO_MEM; |
| 87 |
} |
| 88 |
|
| 89 |
// Read in OEM encoding |
| 90 |
char *os = reinterpret_cast<char *>(malloc(length)); |
| 91 |
if (os == NULL) |
| 92 |
return STATUS_NO_MEM; |
| 93 |
lsp_finally { free(os); }; |
| 94 |
if ((res = read_exact(is, os, length, STATUS_CORRUPTED)) != STATUS_OK) |
| 95 |
return res; |
| 96 |
return (s->set_native(os, length)) ? STATUS_OK : STATUS_NO_MEM; |
| 97 |
} |
| 98 |
|
| 99 |
static status_t fetch_bookmark_path(LSPString *path, const io::Path *location, io::IInStream *is) |
| 100 |
{ |
| 101 |
status_t res; |
| 102 |
LSPString li_base, li_ubase, name, rel_path, work_dir, arguments, icon_location; |
| 103 |
io::Path xpath; |
| 104 |
|
| 105 |
// Read LNK header |
| 106 |
lnk::shlink_header_t hdr; |
| 107 |
if ((res = read_exact(is, &hdr, sizeof(hdr), STATUS_BAD_FORMAT)) != STATUS_OK) |
| 108 |
return res; |
| 109 |
|
| 110 |
// Validate LNK header and signature |
| 111 |
if (LE_TO_CPU(hdr.size) != sizeof(hdr)) |
| 112 |
return STATUS_BAD_FORMAT; |
| 113 |
if ((LE_TO_CPU(hdr.clsid.parts[0]) != lnk::SHLINK_FILE_HEADER_GUID.parts[0]) || |
| 114 |
(LE_TO_CPU(hdr.clsid.parts[1]) != lnk::SHLINK_FILE_HEADER_GUID.parts[1]) || |
| 115 |
(LE_TO_CPU(hdr.clsid.parts[2]) != lnk::SHLINK_FILE_HEADER_GUID.parts[2]) || |
| 116 |
(LE_TO_CPU(hdr.clsid.parts[3]) != lnk::SHLINK_FILE_HEADER_GUID.parts[3])) |
| 117 |
return STATUS_BAD_FORMAT; |
| 118 |
uint32_t flags = LE_TO_CPU(hdr.flags); |
| 119 |
|
| 120 |
// Skip the record if it doesn't reference the directory |
| 121 |
if (!(LE_TO_CPU(hdr.file_attributes) & lnk::SHLINK_FILE_ATTRIBUTE_DIRECTORY)) |
| 122 |
return STATUS_SKIP; |
| 123 |
|
| 124 |
// Skip the IDList if it is present |
| 125 |
if (flags & lnk::SHLINK_HAS_LINK_TARGET_IDLIST) |
| 126 |
{ |
| 127 |
lnk::shlink_idlist_t idlist; |
| 128 |
if ((res = read_exact(is, &idlist, sizeof(idlist), STATUS_CORRUPTED)) != STATUS_OK) |
| 129 |
return res; |
| 130 |
if ((res = skip_exact(is, LE_TO_CPU(idlist.size), STATUS_CORRUPTED)) != STATUS_OK) |
| 131 |
return res; |
| 132 |
} |
| 133 |
|
| 134 |
// Process the LINKINFO structure |
| 135 |
if (flags & lnk::SHLINK_HAS_LINK_INFO) |
| 136 |
{ |
| 137 |
lnk::shlink_link_info_t li; |
| 138 |
lnk::shlink_link_info_opt_t lio; |
| 139 |
size_t li_offset = 0; |
| 140 |
|
| 141 |
bzero(&lio, sizeof(lio)); |
| 142 |
// Mandatory header |
| 143 |
if ((res = read_exact(is, &li, sizeof(li), STATUS_CORRUPTED)) != STATUS_OK) |
| 144 |
return res; |
| 145 |
li_offset += sizeof(li); |
| 146 |
|
| 147 |
// Parse fields of mandatory header and read optional header |
| 148 |
uint32_t li_size = LE_TO_CPU(li.size); |
| 149 |
uint32_t li_hdr_size = LE_TO_CPU(li.header_size); |
| 150 |
uint32_t li_flags = LE_TO_CPU(li.flags); |
| 151 |
if (li_size < sizeof(li)) |
| 152 |
return STATUS_CORRUPTED; |
| 153 |
if (li_hdr_size < sizeof(li)) |
| 154 |
return STATUS_CORRUPTED; |
| 155 |
else if (li_hdr_size > sizeof(li)) |
| 156 |
{ |
| 157 |
// Read optional header |
| 158 |
size_t left = li_hdr_size - sizeof(li); |
| 159 |
size_t extra = lsp_min(left, sizeof(lio)); |
| 160 |
if ((res = read_exact(is, &lio, extra, STATUS_CORRUPTED)) != STATUS_OK) |
| 161 |
return res; |
| 162 |
if ((res = skip_exact(is, left - extra, STATUS_CORRUPTED)) != STATUS_OK) |
| 163 |
return res; |
| 164 |
li_offset += left; |
| 165 |
} |
| 166 |
|
| 167 |
// Read the rest data from the link info |
| 168 |
const size_t li_rest = li_size - li_offset; |
| 169 |
uint8_t *li_data = reinterpret_cast<uint8_t *>(malloc(li_rest)); |
| 170 |
if (li_data == NULL) |
| 171 |
return STATUS_NO_MEM; |
| 172 |
lsp_finally { free(li_data); }; |
| 173 |
if ((res = read_exact(is, li_data, li_rest, STATUS_CORRUPTED)) != STATUS_OK) |
| 174 |
return res; |
| 175 |
|
| 176 |
// Process the link info data suffix paths |
| 177 |
LSPString li_suffix, li_usuffix; |
| 178 |
if (li_flags & lnk::SHLINK_LINK_INFO_COMMON_NETWORK_RELATIVE_LINK_AND_PATH_SUFFIX) |
| 179 |
{ |
| 180 |
// Read the CommonPathSuffixOffset |
| 181 |
uint32_t li_suffix_off = LE_TO_CPU(li.common_path_suffix_offset); |
| 182 |
if (li_suffix_off != 0) |
| 183 |
{ |
| 184 |
if (li_suffix_off < li_offset) |
| 185 |
return STATUS_CORRUPTED; |
| 186 |
const char *li_suffix_ptr = reinterpret_cast<const char *>(&li_data[li_suffix_off - li_offset]); |
| 187 |
size_t li_suffix_len = strnlen(li_suffix_ptr, li_rest); |
| 188 |
if (!li_suffix.set_native(li_suffix_ptr, li_suffix_len)) |
| 189 |
return STATUS_NO_MEM; |
| 190 |
} |
| 191 |
|
| 192 |
// Read the CommonPathSuffixUnicode |
| 193 |
uint32_t li_usuffix_off = LE_TO_CPU(lio.local_base_path_suffix_offset_unicode); |
| 194 |
if (li_usuffix_off != 0) |
| 195 |
{ |
| 196 |
if (li_usuffix_off < li_offset) |
| 197 |
return STATUS_CORRUPTED; |
| 198 |
const lsp_utf16_t *li_usuffix_ptr = reinterpret_cast<const lsp_utf16_t *>(&li_data[li_usuffix_off - li_offset]); |
| 199 |
size_t li_usuffix_len = utf16_nlen(li_usuffix_ptr, li_rest/sizeof(lsp_utf16_t)); |
| 200 |
if (!li_suffix.set_utf16le(li_usuffix_ptr, li_usuffix_len)) |
| 201 |
return STATUS_NO_MEM; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// Process the link info data base paths |
| 206 |
if (li_flags & lnk::SHLINK_LINK_INFO_VOLUME_ID_AND_LOCAL_BASE_PATH) |
| 207 |
{ |
| 208 |
// Read the LocalBasePath |
| 209 |
uint32_t li_base_off = LE_TO_CPU(li.local_base_path_offset); |
| 210 |
if (li_base_off != 0) |
| 211 |
{ |
| 212 |
if (li_base_off < li_offset) |
| 213 |
return STATUS_CORRUPTED; |
| 214 |
const char *li_base_ptr = reinterpret_cast<const char *>(&li_data[li_base_off - li_offset]); |
| 215 |
size_t li_base_len = strnlen(li_base_ptr, li_rest); |
| 216 |
if (!li_base.set_native(li_base_ptr, li_base_len)) |
| 217 |
return STATUS_NO_MEM; |
| 218 |
if (!li_base.append(&li_suffix)) |
| 219 |
return STATUS_NO_MEM; |
| 220 |
} |
| 221 |
|
| 222 |
// Read the LocalBasePathUnicode |
| 223 |
uint32_t li_ubase_off = LE_TO_CPU(lio.local_base_path_offset_unicode); |
| 224 |
if (li_ubase_off != 0) |
| 225 |
{ |
| 226 |
if (li_ubase_off < li_offset) |
| 227 |
return STATUS_CORRUPTED; |
| 228 |
const lsp_utf16_t *li_ubase_ptr = reinterpret_cast<const lsp_utf16_t *>(&li_data[li_ubase_off - li_offset]); |
| 229 |
size_t li_ubase_len = utf16_nlen(li_ubase_ptr, li_rest/sizeof(lsp_utf16_t)); |
| 230 |
if (!li_ubase.set_utf16le(li_ubase_ptr, li_ubase_len)) |
| 231 |
return STATUS_NO_MEM; |
| 232 |
if (!li_ubase.append(&li_usuffix)) |
| 233 |
return STATUS_NO_MEM; |
| 234 |
} |
| 235 |
} |
| 236 |
} // SHLINK_HAS_LINK_INFO |
| 237 |
|
| 238 |
// Process the STRING_DATA section |
| 239 |
const bool unicode = flags & lnk:: SHLINK_IS_UNICODE; |
| 240 |
if (flags & lnk::SHLINK_HAS_NAME) |
| 241 |
{ |
| 242 |
if ((res = read_string_data(is, &name, unicode)) != STATUS_OK) |
| 243 |
return res; |
| 244 |
} |
| 245 |
if (flags & lnk::SHLINK_HAS_RELATIVE_PATH) |
| 246 |
{ |
| 247 |
if ((res = read_string_data(is, &rel_path, unicode)) != STATUS_OK) |
| 248 |
return res; |
| 249 |
} |
| 250 |
if (flags & lnk::SHLINK_HAS_WORKING_DIR) |
| 251 |
{ |
| 252 |
if ((res = read_string_data(is, &work_dir, unicode)) != STATUS_OK) |
| 253 |
return res; |
| 254 |
} |
| 255 |
if (flags & lnk::SHLINK_HAS_ARGUMENTS) |
| 256 |
{ |
| 257 |
if ((res = read_string_data(is, &arguments, unicode)) != STATUS_OK) |
| 258 |
return res; |
| 259 |
} |
| 260 |
if (flags & lnk::SHLINK_HAS_ICON_LOCATION) |
| 261 |
{ |
| 262 |
if ((res = read_string_data(is, &icon_location, unicode)) != STATUS_OK) |
| 263 |
return res; |
| 264 |
} |
| 265 |
|
| 266 |
// We don't need to parse the rest data, just analyze what we got |
| 267 |
// Check that unicode LocalBasePath is present |
| 268 |
if (li_ubase.length() > 0) |
| 269 |
{ |
| 270 |
li_ubase.swap(path); |
| 271 |
return STATUS_OK; |
| 272 |
} |
| 273 |
// Check that OEM LocalBasePath is present |
| 274 |
if (li_base.length() > 0) |
| 275 |
{ |
| 276 |
li_base.swap(path); |
| 277 |
return STATUS_OK; |
| 278 |
} |
| 279 |
// Check if some string data has been present... |
| 280 |
if (work_dir.length() > 0) |
| 281 |
{ |
| 282 |
if (!work_dir.append(&rel_path)) |
| 283 |
return STATUS_NO_MEM; |
| 284 |
if ((res = xpath.set(&work_dir)) != STATUS_OK) |
| 285 |
return res; |
| 286 |
if ((res = xpath.canonicalize()) != STATUS_OK) |
| 287 |
return res; |
| 288 |
return xpath.get(path); |
| 289 |
} |
| 290 |
if (rel_path.length() > 0) |
| 291 |
{ |
| 292 |
if ((res = location->get_parent(&xpath)) != STATUS_OK) |
| 293 |
return res; |
| 294 |
if ((res = xpath.append_child(&rel_path)) != STATUS_OK) |
| 295 |
return res; |
| 296 |
if ((res = xpath.canonicalize()) != STATUS_OK) |
| 297 |
return res; |
| 298 |
return xpath.get(path); |
| 299 |
} |
| 300 |
|
| 301 |
// Actually, we didn't find any proper fields to fetch the path to the bookmarks |
| 302 |
return STATUS_SKIP; |
| 303 |
} |
| 304 |
|
| 305 |
static status_t read_bookmark_path(LSPString *path, io::Dir *dh, LSPString *child) |
| 306 |
{ |
| 307 |
status_t res; |
| 308 |
io::InFileStream is; |
| 309 |
io::Path p; |
| 310 |
|
| 311 |
if ((res = dh->get_path(&p)) != STATUS_OK) |
| 312 |
return res; |
| 313 |
if ((res = p.append_child(child)) != STATUS_OK) |
| 314 |
return res; |
| 315 |
if ((res = is.open(&p)) != STATUS_OK) |
| 316 |
return res; |
| 317 |
|
| 318 |
res = fetch_bookmark_path(path, &p, &is); |
| 319 |
status_t res2 = is.close(); |
| 320 |
|
| 321 |
return (res != STATUS_OK) ? res : res2; |
| 322 |
} |
| 323 |
|
| 324 |
static status_t read_bookmarks_lnk(lltl::parray<bookmark_t> *dst, io::Dir *dh) |
| 325 |
{ |
| 326 |
LSPString fname; |
| 327 |
status_t res; |
| 328 |
lltl::parray<bookmark_t> list; |
| 329 |
lsp_finally { destroy_bookmarks(&list); }; |
| 330 |
|
| 331 |
// Read directory entries |
| 332 |
while ((res = dh->read(&fname)) == STATUS_OK) |
| 333 |
{ |
| 334 |
if (!fname.ends_with_ascii_nocase(".lnk")) |
| 335 |
continue; |
| 336 |
|
| 337 |
// Create bookmark |
| 338 |
bookmark_t *bm = new bookmark_t(); |
| 339 |
if (bm == NULL) |
| 340 |
return STATUS_NO_MEM; |
| 341 |
lsp_finally { |
| 342 |
if (bm != NULL) |
| 343 |
delete bm; |
| 344 |
}; |
| 345 |
|
| 346 |
// Set the value |
| 347 |
bm->origin = BM_LNK; |
| 348 |
if (!bm->name.set(&fname, 0, fname.length() - 4)) // All except the '.lnk' extension |
| 349 |
return STATUS_NO_MEM; |
| 350 |
if ((res = read_bookmark_path(&bm->path, dh, &fname)) != STATUS_OK) |
| 351 |
{ |
| 352 |
if ((res == STATUS_BAD_FORMAT) || |
| 353 |
(res == STATUS_CORRUPTED) || |
| 354 |
(res == STATUS_SKIP)) |
| 355 |
continue; |
| 356 |
return res; |
| 357 |
} |
| 358 |
|
| 359 |
// Add bookmark to list if we have successfully read the path |
| 360 |
if (!list.add(bm)) |
| 361 |
return STATUS_NO_MEM; |
| 362 |
bm = NULL; // Release reference to avoid bookmark object deletion |
| 363 |
} |
| 364 |
|
| 365 |
// Commit result and return |
| 366 |
if (res != STATUS_EOF) |
| 367 |
return res; |
| 368 |
|
| 369 |
list.swap(dst); |
| 370 |
return STATUS_OK; |
| 371 |
} |
| 372 |
|
| 373 |
status_t read_bookmarks_lnk(lltl::parray<bookmark_t> *dst, const char *path) |
| 374 |
{ |
| 375 |
io::Dir dh; |
| 376 |
status_t res = dh.open(path); |
| 377 |
if (res != STATUS_OK) |
| 378 |
return res; |
| 379 |
lsp_finally { dh.close(); }; |
| 380 |
|
| 381 |
return read_bookmarks_lnk(dst, &dh); |
| 382 |
} |
| 383 |
|
| 384 |
status_t read_bookmarks_lnk(lltl::parray<bookmark_t> *dst, const LSPString *path) |
| 385 |
{ |
| 386 |
io::Dir dh; |
| 387 |
status_t res = dh.open(path); |
| 388 |
if (res != STATUS_OK) |
| 389 |
return res; |
| 390 |
lsp_finally { dh.close(); }; |
| 391 |
|
| 392 |
return read_bookmarks_lnk(dst, &dh); |
| 393 |
} |
| 394 |
|
| 395 |
status_t read_bookmarks_lnk(lltl::parray<bookmark_t> *dst, const io::Path *path) |
| 396 |
{ |
| 397 |
io::Dir dh; |
| 398 |
status_t res = dh.open(path); |
| 399 |
if (res != STATUS_OK) |
| 400 |
return res; |
| 401 |
lsp_finally { dh.close(); }; |
| 402 |
|
| 403 |
return read_bookmarks_lnk(dst, &dh); |
| 404 |
} |
| 405 |
|
| 406 |
} /* namespace bookmarks */ |
| 407 |
} /* namespace lsp */ |
| 408 |
|
| 409 |
|
| 410 |
|