MCPcopy Create free account
hub / github.com/MusicPlayerDaemon/MPD / mod_loadfile

Function mod_loadfile

src/decoder/plugins/ModCommon.cxx:10–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8static constexpr offset_type MOD_FILE_LIMIT = 100 * 1024 * 1024;
9
10AllocatedArray<std::byte>
11mod_loadfile(const Domain *domain, DecoderClient *client, InputStream &is)
12{
13 //known/unknown size, preallocate array, lets read in chunks
14
15 const bool is_stream = !is.KnownSize();
16 size_t buffer_size;
17
18 if (is_stream)
19 buffer_size = MOD_PREALLOC_BLOCK;
20 else {
21 const auto size = is.GetSize();
22
23 if (size == 0) {
24 LogWarning(*domain, "file is empty");
25 return nullptr;
26 }
27
28 if (size > MOD_FILE_LIMIT) {
29 LogWarning(*domain, "file too large");
30 return nullptr;
31 }
32
33 buffer_size = size;
34 }
35
36 auto buffer = AllocatedArray<std::byte>(buffer_size);
37
38 std::byte *p = buffer.data();
39 std::byte *const end = p + buffer.size();
40
41 while (true) {
42 size_t ret = decoder_read(client, is, {p, end});
43 if (ret == 0) {
44 if (is.LockIsEOF())
45 /* end of file */
46 break;
47
48 /* I/O error - skip this song */
49 return buffer;
50 }
51
52 p += ret;
53 if (p == end) {
54 if (!is_stream)
55 break;
56
57 LogWarning(*domain, "stream too large");
58 return buffer;
59 }
60 }
61
62 buffer.SetSize(p - buffer.data());
63 return buffer;
64}
65

Callers 3

LoadModPlugFileFunction · 0.85
mod_decodeFunction · 0.85
openmpt_scan_streamFunction · 0.85

Calls 4

decoder_readFunction · 0.85
LockIsEOFMethod · 0.80
SetSizeMethod · 0.80
GetSizeMethod · 0.45

Tested by

no test coverage detected