cstruct-buffer
Version:
Binary serialization framework for C-style structs
71 lines (70 loc) • 2.5 kB
TypeScript
type CStructType = new () => CStruct;
type CType = 'u8' | 'u16' | 'u32' | 'u64' | 'i8' | 'i16' | 'i32' | 'i64' | 'f32' | 'f64' | 'str' | CStructType;
type Align = 0 | 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256;
type Endian = 'little' | 'big';
interface FieldMetadata {
name: string;
ctype: CType;
align: Align;
elementSize?: number;
arrLength: number;
isBigInt?: boolean;
getter?: any[];
setter?: Function;
isLength?: boolean;
includeSelf?: boolean;
}
interface CalculatedField {
name: string;
align: Align;
offset: number;
size: number;
metadata: FieldMetadata;
}
type Options = {
arrLength?: number;
align?: Align;
isLength?: boolean;
includeSelf?: boolean;
};
declare abstract class CStruct {
[CStruct.fieldHierarchy]: FieldMetadata[][];
[CStruct.metadataFields]: FieldMetadata[];
private static fieldHierarchy;
private static metadataFields;
static readonly PlatformEndian: Endian;
pack: Align;
align: Align;
effectiveAlignment: Align;
size: number;
Fields: CalculatedField[];
private static VALID_ALIGN;
private closed;
static Layout(attr?: {
pack?: Align;
align?: Align;
}): ClassDecorator;
private static field;
private static calculateLayout;
constructor();
private getOrInitFields;
serialize(endian?: Endian): Uint8Array;
private writeFixedSizeField;
private calculateDynaSize;
static deserialize<T extends CStruct>(this: new () => T, buffer: Uint8Array, endian?: Endian): T;
protected readFromBuffer(view: DataView, baseOffset: number, endian: Endian): void;
private handleVariableLengthField;
static U8(options?: Options): PropertyDecorator;
static U16(options?: Options): PropertyDecorator;
static U32(options?: Options): PropertyDecorator;
static U64(options?: Options): PropertyDecorator;
static I8(options?: Options): PropertyDecorator;
static I16(options?: Options): PropertyDecorator;
static I32(options?: Options): PropertyDecorator;
static I64(options?: Options): PropertyDecorator;
static F32(options?: Omit<Options, 'isLength' | 'includeSelf'>): PropertyDecorator;
static F64(options?: Omit<Options, 'isLength' | 'includeSelf'>): PropertyDecorator;
static Str(maxBytes?: number, align?: Align): PropertyDecorator;
static Struct(constructor: CStructType, options?: Omit<Options, 'isLength' | 'includeSelf'>): PropertyDecorator;
}
export { CStruct, };