@eventmsg/core
Version:
EventMsgV3 TypeScript library - Core protocol implementation with transport abstraction
74 lines • 2.25 kB
TypeScript
//#region src/internal/byte-stuffing.d.ts
/**
* EventMsgV3 Byte Stuffing Implementation
*
* Implements the exact byte stuffing algorithm from the C implementation
* to ensure protocol compatibility.
*/
/**
* Control characters used in EventMsgV3 protocol
*/
declare const CONTROL_CHARS: {
readonly SOH: 0x01;
readonly STX: 0x02;
readonly EOT: 0x04;
readonly US: 0x1f;
readonly ESC: 0x1b;
};
/**
* Stuff (encode) bytes by escaping control characters
*
* Algorithm:
* - If byte is a control character: Insert ESC, then XOR byte with 0x20
* - Otherwise: Insert byte as-is
*
* @param data Input data to stuff
* @returns Stuffed data
*/
declare function stuff(data: Uint8Array): Uint8Array;
/**
* Unstuff (decode) bytes by handling escaped characters
*
* Algorithm:
* - If byte is ESC: Mark next byte as escaped, continue
* - If previous byte was ESC: XOR current byte with 0x20, add to output
* - Otherwise: Add byte as-is to output
*
* @param data Stuffed data to unstuff
* @returns Unstuffed data
* @throws {Error} If data contains invalid escape sequences
*/
declare function unstuff(data: Uint8Array): Uint8Array;
/**
* Calculate the maximum possible size after stuffing
* In worst case, every byte could be a control character, doubling the size
*
* @param originalSize Original data size
* @returns Maximum size after stuffing
*/
declare function getMaxStuffedSize(originalSize: number): number;
/**
* Calculate the minimum possible size after unstuffing
* In best case, no bytes are stuffed
*
* @param stuffedSize Stuffed data size
* @returns Minimum size after unstuffing
*/
declare function getMinUnstuffedSize(stuffedSize: number): number;
/**
* Test if data contains any control characters that would need stuffing
*
* @param data Data to test
* @returns True if data contains control characters
*/
declare function needsStuffing(data: Uint8Array): boolean;
/**
* Validate that stuffed data has proper escape sequences
*
* @param data Stuffed data to validate
* @returns True if data has valid stuffing
*/
declare function isValidStuffing(data: Uint8Array): boolean;
//#endregion
export { CONTROL_CHARS, getMaxStuffedSize, getMinUnstuffedSize, isValidStuffing, needsStuffing, stuff, unstuff };
//# sourceMappingURL=byte-stuffing.d.ts.map