Debian Codegraph @

modules/lsp-runtime-lib/src/main/mm/MMIOReader.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: 24 апр. 2020 г.
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/types.h>
23
24 #ifdef PLATFORM_WINDOWS
25
26 #include <lsp-plug.in/common/alloc.h>
27 #include <lsp-plug.in/common/debug.h>
28 #include <lsp-plug.in/common/endian.h>
29 #include <lsp-plug.in/mm/sample.h>
30
31 #include <private/mm/MMIOReader.h>
32
33 namespace lsp
34 {
35 namespace mm
36 {
37 MMIOReader::MMIOReader()
38 {
39 hMMIO = NULL;
40 nReadPos = 0;
41 nFrames = -1;
42 bSeekable = false;
43 pFormat = NULL;
44
45 ::ZeroMemory(&ckRiff, sizeof(MMCKINFO));
46 ::ZeroMemory(&ckData, sizeof(MMCKINFO));
47 }
48
49 MMIOReader::~MMIOReader()
50 {
51 close();
52 }
53
54 status_t MMIOReader::close(status_t code)
55 {
56 if (hMMIO != NULL)
57 {
58 ::mmioClose(hMMIO, 0);
59 hMMIO = NULL;
60 }
61 if (pFormat != NULL)
62 {
63 ::free(pFormat);
64 pFormat = NULL;
65 }
66
67 nFrames = -1;
68 bSeekable = false;
69
70 ::ZeroMemory(&ckRiff, sizeof(MMCKINFO));
71 ::ZeroMemory(&ckData, sizeof(MMCKINFO));
72
73 return code;
74 }
75
76 status_t MMIOReader::open(const LSPString *path)
77 {
78 MMCKINFO ckIn;
79 WAVEFORMATEX wfe;
80 MMRESULT error;
81
82 nReadPos = 0;
83 nFrames = -1;
84 bSeekable = false;
85
86 if ((hMMIO = ::mmioOpenW(const_cast<WCHAR *>(path->get_utf16()), NULL, MMIO_ALLOCBUF | MMIO_READ)) == NULL)
87 return STATUS_PERMISSION_DENIED;
88
89 // Lookup for RIFF chunk
90 if ((error = ::mmioDescend(hMMIO, &ckRiff, NULL, 0)) != 0)
91 return close(STATUS_BAD_FORMAT);
92
93 if ((ckRiff.ckid != FOURCC_RIFF) || (ckRiff.fccType != mmioFOURCC('W', 'A', 'V', 'E')))
94 return close(STATUS_BAD_FORMAT);
95
96 // Reset position
97 if (::mmioSeek(hMMIO, ckRiff.dwDataOffset + sizeof(FOURCC), SEEK_SET) < 0)
98 return close(STATUS_CORRUPTED_FILE);
99
100 // Lookup for format chunk
101 ckIn.ckid = mmioFOURCC('f', 'm', 't', ' ');
102 if ((error = ::mmioDescend(hMMIO, &ckIn, &ckRiff, MMIO_FINDCHUNK)) != 0)
103 return close(STATUS_BAD_FORMAT);
104
105 // Expect the 'fmt' chunk to be at least as large as sizeof(PCMWAVEFORMAT)
106 if (ckIn.cksize < sizeof(PCMWAVEFORMAT))
107 return close(STATUS_CORRUPTED_FILE);
108
109 // Read the 'fmt ' chunk
110 size_t bytes = ::mmioRead(hMMIO, reinterpret_cast<HPSTR>(&wfe), sizeof(PCMWAVEFORMAT));
111 if (bytes != sizeof(PCMWAVEFORMAT))
112 return close(STATUS_BAD_FORMAT);
113
114 // Estimate number of bytes to allocate for the format descriptor
115 size_t ftag = LE_TO_CPU(wfe.wFormatTag);
116 if ((ftag != WAVE_FORMAT_PCM) && (ftag != WAVE_FORMAT_IEEE_FLOAT))
117 {
118 // Read in length of extra bytes.
119 bytes = ::mmioRead(hMMIO, reinterpret_cast<HPSTR>(&wfe.cbSize), sizeof(WORD));
120 if (bytes != sizeof(WORD))
121 return close(STATUS_CORRUPTED_FILE);
122
123 // Allocate memory
124 size_t alloc = sizeof(WAVEFORMATEX) + LE_TO_CPU(wfe.cbSize);
125 pFormat = static_cast<WAVEFORMATEX *>(::malloc(align_size(alloc, DEFAULT_ALIGN)));
126 if (pFormat == NULL)
127 return close(STATUS_NO_MEM);
128 ::memcpy(pFormat, &wfe, sizeof(WAVEFORMATEX));
129
130 // Read the rest part of format descriptor
131 if (wfe.cbSize > 0)
132 {
133 bytes += ::mmioRead(hMMIO, reinterpret_cast<HPSTR>(&pFormat[1]), wfe.cbSize);
134 if (bytes != alloc)
135 return close(STATUS_CORRUPTED_FILE);
136 }
137 }
138 else
139 {
140 // Set size and copy wave format
141 wfe.cbSize = 0;
142 pFormat = static_cast<WAVEFORMATEX *>(memdup(&wfe, sizeof(WAVEFORMATEX)));
143 if (pFormat == NULL)
144 return close(STATUS_NO_MEM);
145 bSeekable = true;
146 }
147
148 // Reset position
149 if (::mmioSeek(hMMIO, ckRiff.dwDataOffset + sizeof(FOURCC), SEEK_SET) < 0)
150 return close(STATUS_CORRUPTED_FILE);
151
152 // Lookup for 'fact' chunk, read number of samples
153 ckIn.ckid = mmioFOURCC('f', 'a', 'c', 't');
154 if ((error = ::mmioDescend(hMMIO, &ckIn, &ckRiff, MMIO_FINDCHUNK)) == 0)
155 {
156 if (LE_TO_CPU(ckIn.cksize) >= sizeof(DWORD))
157 {
158 DWORD factlen = 0;
159 size_t bytes = ::mmioRead(hMMIO, reinterpret_cast<HPSTR>(&factlen), sizeof(DWORD));
160 if (bytes != sizeof(DWORD))
161 return close(STATUS_CORRUPTED_FILE);
162 nFrames = factlen;
163 }
164 }
165
166 // Reset position
167 if (::mmioSeek(hMMIO, ckRiff.dwDataOffset + sizeof(FOURCC), SEEK_SET) < 0)
168 return close(STATUS_CORRUPTED_FILE);
169
170 ckData.ckid = mmioFOURCC('d', 'a', 't', 'a');
171 if ((error = ::mmioDescend(hMMIO, &ckData, &ckRiff, MMIO_FINDCHUNK)) != 0)
172 return close(STATUS_CORRUPTED_FILE);
173
174 // Estimate number of frames by the other way (PCM allows this)
175 if (nFrames < 0)
176 {
177 if (LE_TO_CPU(wfe.wFormatTag) == WAVE_FORMAT_PCM)
178 {
179 size_t fsize = LE_TO_CPU(wfe.wBitsPerSample) * LE_TO_CPU(wfe.nChannels);
180 if (fsize & 0x07) // not multiple of 8?
181 return close(STATUS_UNSUPPORTED_FORMAT);
182 fsize >>= 3; // divide by 8
183 nFrames = ckData.cksize / fsize;
184 if ((nFrames * fsize) != ckData.cksize)
185 return close(STATUS_CORRUPTED_FILE);
186 }
187 else if (LE_TO_CPU(wfe.wFormatTag) == WAVE_FORMAT_IEEE_FLOAT)
188 {
189 if (LE_TO_CPU(wfe.wBitsPerSample) != sizeof(f32_t)*8)
190 return close(STATUS_UNSUPPORTED_FORMAT);
191
192 const size_t fsize = sizeof(f32_t) * LE_TO_CPU(wfe.nChannels);
193 nFrames = ckData.cksize / fsize;
194 if ((nFrames * fsize) != ckData.cksize)
195 return close(STATUS_CORRUPTED_FILE);
196 }
197 }
198
199 return STATUS_OK;
200 }
201
202 wssize_t MMIOReader::seek(wsize_t offset)
203 {
204 if (!bSeekable)
205 return -STATUS_NOT_SUPPORTED;
206
207 if (offset > ckData.cksize)
208 offset = ckData.cksize;
209
210 if (::mmioSeek(hMMIO, ckData.dwDataOffset + offset, SEEK_SET) < 0)
211 return -STATUS_IO_ERROR;
212
213 return nReadPos = offset;
214 }
215
216 ssize_t MMIOReader::read(void *buf, size_t count)
217 {
218 // Compute how many bytes we can read
219 wssize_t can_read = ckData.cksize - nReadPos;
220 if (can_read <= 0)
221 return -STATUS_EOF;
222 if (count > size_t(can_read))
223 count = can_read;
224
225 // Perform read
226 LONG res = ::mmioRead(hMMIO, reinterpret_cast<HPSTR>(buf), count);
227 if (res < 0)
228 return -STATUS_IO_ERROR;
229 nReadPos += res;
230 return res;
231 }
232
233 } /* namespace mm */
234 } /* namespace lsp */
235
236 #endif /* PLATFORM_WINDOWS */