Skip to content
Snippets Groups Projects
README.md 2.97 KiB
Newer Older
  • Learn to ignore specific revisions
  • clwi's avatar
    clwi committed
    # CWPack
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    CWPack is a lightweight and yet complete implementation of the 
    
    clwi's avatar
    clwi committed
    [MessagePack](http://msgpack.org) serialization format 
    [version 5](https://github.com/msgpack/msgpack/blob/master/spec.md).
    
    clwi's avatar
    clwi committed
    It also supports the Timestamp extension type.
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    ## Excellent Performance
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    Together with [MPack](https://github.com/ludocode/mpack), CWPack is the fastest open-source messagepack implementation. Both totally outperform
    [CMP](https://github.com/camgunz/cmp) and [msgpack-c](https://github.com/msgpack/msgpack-c)
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    ## Design
    
    
    clwi's avatar
    clwi committed
    CWPack does no memory allocations and no file handling in its basic setup. All that is done outside of CWPack. Example extensions are included.
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    CWPack is working against memory buffers. User defined handlers are called when buffers are filled up (packing) or needs refill (unpack). 
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    Containers (arrays, maps) are read/written in parts, first the item containing the size and then the contained items one by one. Exception to this is the `cw_skip_items` function which skip whole containers.
    
    clwi's avatar
    clwi committed
    
    ## Example
    
    Pack and unpack example from the MessagePack home page:
    
    
    clwi's avatar
    clwi committed
    ```c
    
    clwi's avatar
    clwi committed
    void example (void)
    {
        cw_pack_context pc;
        char buffer[20];
    
    clwi's avatar
    clwi committed
        cw_pack_context_init (&pc, buffer, 20, 0);
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
        cw_pack_map_size (&pc, 2);
        cw_pack_str (&pc, "compact", 7);
        cw_pack_boolean (&pc, true);
        cw_pack_str (&pc, "schema", 6);
        cw_pack_unsigned (&pc, 0);
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
        if (pc.return_code != CWP_RC_OK)  ERROR;
    
    clwi's avatar
    clwi committed
        int length = pc.current - pc.start;
    
    clwi's avatar
    clwi committed
        if (length != 18) ERROR;
    
    clwi's avatar
    clwi committed
    
        cw_unpack_context uc;
    
    clwi's avatar
    clwi committed
        cw_unpack_context_init (&uc, pc.start, length, 0);
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
        if (cw_unpack_next_map_size(&uc) != 2) ERROR;
        if (cw_unpack_next_str_lengh(&uc) != 7) ERROR;
    
    clwi's avatar
    clwi committed
        if (strncmp("compact", uc.item.as.str.start, 7)) ERROR;
    
    clwi's avatar
    clwi committed
        if (cw_unpack_next_bool(&uc) != true) ERROR;
        if (cw_unpack_next_str_lengh(&uc) != 6) ERROR;
    
    clwi's avatar
    clwi committed
        if (strncmp("schema", uc.item.as.str.start, 6)) ERROR;
    
    clwi's avatar
    clwi committed
        if (cw_unpack_next_signed32(&uc) != 0) ERROR;
        
        if (uc.return_code != CWP_RC_OK)  ERROR;
    
    clwi's avatar
    clwi committed
        cw_unpack_next(&uc);
        if (uc.return_code != CWP_RC_END_OF_INPUT)  ERROR;
    
    clwi's avatar
    clwi committed
    }
    
    clwi's avatar
    clwi committed
    ```
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    In the examples folder there are more examples.
    
    ## Backward compatibility
    
    
    clwi's avatar
    clwi committed
    CWPack may be run in compatibility mode. It affects only packing; EXT & TIMESTAMP is considered illegal, BIN are transformed to STR and generation of STR8 is supressed.
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    ## Error handling
    
    
    clwi's avatar
    clwi committed
    When an error is detected in a context, the context is stopped and all future calls to that context are immediatly returned without any actions. Thus it is possible to make some calls and delay error checking until all calls are done.
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    CWPack does not check for illegal values (e.g. in STR for illegal unicode characters).
    
    clwi's avatar
    clwi committed
    
    ## Build
    
    
    clwi's avatar
    clwi committed
    CWPack consists of a single src file and three header files. It is written in strict ansi C and the files are together ~ 1.4K lines. No separate build is neccesary, just include the files in your own build.
    
    clwi's avatar
    clwi committed
    
    CWPack has no dependencies to other libraries.
    
    
    clwi's avatar
    clwi committed
    ## Test
    
    clwi's avatar
    clwi committed
    
    
    clwi's avatar
    clwi committed
    Included in the test folder are a module test and a performance test and shell scripts to run them.