MCPcopy Create free account
hub / github.com/RtlZeroMemory/Rezi / parseResourceState

Function parseResourceState

packages/core/src/__tests__/drawlistDecode.ts:132–202  ·  view source on GitHub ↗
(bytes: Uint8Array)

Source from the content-addressed store, hash-verified

130}
131
132function parseResourceState(bytes: Uint8Array): ResourceState {
133 const headers = parseCommandHeaders(bytes);
134 const strings = new Map<number, Uint8Array>();
135 const blobs = new Map<number, Uint8Array>();
136
137 for (const cmd of headers) {
138 switch (cmd.opcode) {
139 case OP_DEF_STRING: {
140 if (cmd.size < 16) {
141 throw new Error(
142 `DEF_STRING too small at offset ${String(cmd.offset)} size=${String(cmd.size)}`,
143 );
144 }
145 const id = u32(bytes, cmd.offset + 8);
146 const byteLen = u32(bytes, cmd.offset + 12);
147 const dataStart = cmd.offset + 16;
148 const dataEnd = dataStart + byteLen;
149 if (dataEnd > cmd.offset + cmd.size) {
150 throw new Error(
151 `DEF_STRING payload overflow at offset ${String(cmd.offset)} (len=${String(byteLen)}, size=${String(cmd.size)})`,
152 );
153 }
154 strings.set(id, cloneBytes(bytes.subarray(dataStart, dataEnd)));
155 break;
156 }
157 case OP_FREE_STRING: {
158 if (cmd.size !== 12) {
159 throw new Error(
160 `FREE_STRING wrong size at offset ${String(cmd.offset)} size=${String(cmd.size)}`,
161 );
162 }
163 strings.delete(u32(bytes, cmd.offset + 8));
164 break;
165 }
166 case OP_DEF_BLOB: {
167 if (cmd.size < 16) {
168 throw new Error(
169 `DEF_BLOB too small at offset ${String(cmd.offset)} size=${String(cmd.size)}`,
170 );
171 }
172 const id = u32(bytes, cmd.offset + 8);
173 const byteLen = u32(bytes, cmd.offset + 12);
174 const dataStart = cmd.offset + 16;
175 const dataEnd = dataStart + byteLen;
176 if (dataEnd > cmd.offset + cmd.size) {
177 throw new Error(
178 `DEF_BLOB payload overflow at offset ${String(cmd.offset)} (len=${String(byteLen)}, size=${String(cmd.size)})`,
179 );
180 }
181 blobs.set(id, cloneBytes(bytes.subarray(dataStart, dataEnd)));
182 break;
183 }
184 case OP_FREE_BLOB: {
185 if (cmd.size !== 12) {
186 throw new Error(
187 `FREE_BLOB wrong size at offset ${String(cmd.offset)} size=${String(cmd.size)}`,
188 );
189 }

Callers 2

parseBlobByIdFunction · 0.85

Calls 5

parseCommandHeadersFunction · 0.85
cloneBytesFunction · 0.85
u32Function · 0.70
setMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected