cobs

    COBS (Consistent Overhead Byte Stuffing) codec in pure MoonBit

    cobs
    encoding
    serial
    framing
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    28 days ago
    Downloads
    27

    No dependencies

    #MuVirus/cobs

    纯 MoonBit 实现的 COBS(Consistent Overhead Byte Stuffing,一致性开销字节填充)协议库。

    • 纯 MoonBit、零第三方依赖:只使用语言内置类型,wasm / js / native 后端均可编译。
    • 完整 API:一次性编解码 + 流式增量解码器,适合串口、TCP 等字节流链路。

    #什么是 COBS

    COBS 是一种字节填充(成帧)算法:编码后的帧内部保证不含 0x00,以单个 0x00 作为帧分隔符。接收方只需按 0x00 切分字节流即可定位帧边界,因此对 噪声或丢字节具有天然的重同步能力。相比 SLIP / PPP 转义,它的开销是一致的: 最坏情况每 254 字节只增加 1 字节(加上末尾分隔符),即 encode 输出长度不超过 max_encoded_len(n) = n + n/254 + 2。

    #用法

    在你的 moon.pkg 中添加依赖:

    import { "MuVirus/cobs" @cobs, }

    一次性编解码:

    let payload = b"\x01\x02\x00\x03hello\x00world"
    let frame = @cobs.encode(payload)
    // frame = 03 01 02 07 03 68 65 6c 6c 6f 06 77 6f 72 6c 64 00
    // (内部无 0x00,末尾是分隔符)

    match @cobs.decode(frame) {
    Ok(data) => assert_eq(data, payload) // 还原成功
    Err(TruncatedFrame) => ... // 帧在分隔符之前结束
    Err(TrailingData) => ... // 分隔符之后还有多余字节
    }

    流式解码(逐字节喂入,凑满一帧返回载荷):

    let dec = @cobs.Decoder::new()
    for byte in wire {
    match dec.push(byte) {
    Some(data) => handle(data) // 0x00 分隔符到达,一帧完成
    None => ()
    }
    }

    一次喂入整个缓冲区、取出其中所有完整帧:

    ///|
    let frames : Array[Bytes] = @cobs.Decoder::new().feed(buffer)

    #语义细节

    • encode 输出总是以 0x00 结尾;编码空载荷得到 [0x01, 0x00]。
    • 块最大长度为 254 字节;写满 254 字节的块以 0xFF 关闭(按长度关闭, 不吸收零字节),随后若载荷继续,会开启新块。
    • decode 是严格模式:0x00 分隔符必须是输入的最后一个字节, 任何截断(TruncatedFrame)或分隔符后的多余数据(TrailingData)都会报错。
    • Decoder 是宽容模式:0x00 永远作为当前帧的边界,因此损坏的码字会在 下一个分隔符处自动重新同步(中途到达的分隔符会产出部分帧而不是报错)。 需要严格校验时请使用 decode。
    • Decoder::finish 用于流结束检查:所有字节都已作为帧交付时返回 Ok(()), 否则返回 Err(TruncatedFrame)。

    #API 一览

    函数说明
    max_encoded_len(n) -> Intn 字节载荷的编码长度上界(含分隔符)
    encode(Bytes) -> Bytes编码为完整帧(含末尾 0x00 分隔符)
    decode(Bytes) -> Result[Bytes, DecodeError]严格解码一帧
    Decoder::new() / push / feed / finish流式增量解码

    #开发

    moon test # 运行测试 moon fmt # 格式化 moon info # 更新 .mbti 接口 moon run cmd/main # 运行演示

    DecodeError

    pub(all) enum DecodeError {
    TruncatedFrame
    TrailingData
    } derive(Eq,
    Debug
    )

    Errors reported by decode and Decoder::finish.
    impl Show for DecodeError

    DecodeError::equal

    fn DecodeError::equal(DecodeError, DecodeError) -> Bool

    DecodeError::not_equal

    fn DecodeError::not_equal(x : DecodeError, y : DecodeError) -> Bool

    DecodeError::output

    fn DecodeError::output(self : DecodeError, logger : &Logger) -> Unit

    DecodeError::to_string

    fn DecodeError::to_string(self : DecodeError) -> String

    Decoder

    pub(all) struct Decoder {
    // private fields
    }

    A streaming COBS decoder for wire bytes that arrive one at a time. Feed every byte to push; when a 0x00 delimiter arrives, the payload accumulated so far is returned.

    The delimiter is always treated as the end of the current frame, so a corrupted stream resynchronizes at the next delimiter: a delimiter that arrives in the middle of a code word yields a partial frame instead of an error. Use decode when you need strict validation of a whole frame.

    Decoder::feed

    fn Decoder::feed(self : Decoder, data : Bytes) -> Array[Bytes]

    Feeds a whole buffer and collects every frame it completes, in order.

    Decoder::finish

    fn Decoder::finish(self : Decoder) -> Result[Unit, DecodeError]

    Checks that the fed byte stream ended on a frame boundary: returns Ok(()) when every byte fed so far has been delivered as a frame, and Err(TruncatedFrame) when a frame was still in progress.

    Decoder::new

    fn Decoder::new() -> Decoder

    Creates a decoder that expects the first byte of a frame.

    Decoder::push

    fn Decoder::push(self : Decoder, byte : Byte) -> Bytes?

    Feeds a single wire byte into the decoder. Returns Some(payload) when the byte was the 0x00 delimiter that completed a frame, None otherwise.

    decode

    fn decode(frame : Bytes) -> Result[Bytes, DecodeError]

    Strictly decodes one complete COBS frame: the 0x00 delimiter must be the final byte of frame. Use Decoder to pull frames out of a larger byte stream instead. Returns the original payload on success.

    encode

    fn encode(payload : Bytes) -> Bytes

    Encodes payload into a complete COBS frame: a byte sequence guaranteed to contain no interior 0x00, followed by a single 0x00 delimiter that marks the end of the frame on a wire. The result is at most max_encoded_len(payload.length()) bytes long; encoding an empty payload yields [0x01, 0x00].

    max_encoded_len

    fn max_encoded_len(n : Int) -> Int

    Upper bound of the encoded length for a payload of n bytes, including the trailing 0x00 delimiter produced by encode: the payload bytes themselves, at most one code byte per 254 payload bytes, a final code byte for the last block, and the delimiter.

    Source Files