function arrayBufferToString(arrayBuffer) { const decoder = new TextDecoder('latin1'); return decoder.decode(arrayBuffer); }
function extractBinary(arrayBuffer, prefixRegex, suffixRegex = null) { const str = arrayBufferToString(arrayBuffer);
const prefixMatch = prefixRegex.exec(str); if (!prefixMatch) { return { text: str }; }
const dataStartIndex = prefixMatch.index + prefixMatch[0].length; const strFirstHalf = str.substring(0, dataStartIndex); let retVal = null;
const suffixMatch = suffixRegex ? suffixRegex.exec(str) : null; if (suffixMatch) { const strSecondHalf = str.substr(suffixMatch.index); retVal = { text: strFirstHalf + strSecondHalf, binaryBuffer: arrayBuffer.slice(dataStartIndex, suffixMatch.index), }; } else { retVal = { text: strFirstHalf, binaryBuffer: arrayBuffer.slice(dataStartIndex), }; }
return retVal; }
export default { arrayBufferToString, extractBinary, };
|