Skip to content

Commit

Permalink
fix padding for blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
russellmcc committed Oct 14, 2024
1 parent e904ae6 commit c874ffa
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/osc-utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,15 @@ const parseOscArg = (
case "b": {
const view = toView(buffer);
const { value: length, rest: data } = splitInteger(view);
// We added padding to make sure the blob's length in the buffer is a multiple of 4
const padding = (4 - (length % 4)) % 4;

return {
value: {
type: "blob",
value: new DataView(data.buffer, data.byteOffset, length),
},
rest: sliceDataView(data, length),
rest: sliceDataView(data, length + padding),
};
}
case "T":
Expand Down Expand Up @@ -245,9 +248,15 @@ const toOscArgument = (arg: OscArgWithType): ArrayBuffer => {
}
case "blob": {
const view = toView(arg.value);
const ret = new DataView(new ArrayBuffer(4 + arg.value.byteLength));

// Add padding to make the blob's length a multiple of 4
const padding = (4 - (arg.value.byteLength % 4)) % 4;

const ret = new DataView(
new ArrayBuffer(4 + arg.value.byteLength + padding)
);
ret.setUint32(0, arg.value.byteLength, false);
new Uint8Array(ret.buffer, ret.byteOffset + 4, ret.byteLength - 4).set(
new Uint8Array(ret.buffer, ret.byteOffset + 4).set(
new Uint8Array(view.buffer, view.byteOffset, view.byteLength)
);
return ret.buffer;
Expand Down

0 comments on commit c874ffa

Please sign in to comment.