Skip to content

Commit

Permalink
Release 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
clwi committed Jan 27, 2017
1 parent 6429a45 commit 3e5002d
Show file tree
Hide file tree
Showing 23 changed files with 1,716 additions and 759 deletions.
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#CWPack

CWPack is a minimalistic and yet fast and complete implementation of the
CWPack is a lightweight and yet complete implementation of the
[MessagePack](http://msgpack.org) serialization format
[version 5](https://github.com/msgpack/msgpack/blob/master/spec.md).

## Fast, Faster, Fastest

CWPack is the fastest open-source messagepack implementation. It is faster then
[MPack](https://github.com/ludocode/mpack)
and both totally outperform
[CMP](https://github.com/camgunz/cmp)

## Design

CWPack does no memory allocations and no file handling. All that is done
Expand All @@ -27,54 +34,60 @@ void example (void)
char buffer[20];
cw_pack_context_init (&pc, buffer, 20, 0, 0);

if (cw_pack_map_size (&pc, 2)) ERROR;
if (cw_pack_str (&pc, "compact", 7)) ERROR;
if (cw_pack_boolean (&pc, true)) ERROR;
if (cw_pack_str (&pc, "schema", 6)) ERROR;
if (cw_pack_unsigned (&pc, 0)) ERROR;
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);

int length = pc.current - pc.start;
if (length > 18) ERROR
if (length > 18) ERROR;

cw_unpack_context uc;
cw_unpack_context_init (&uc, pc.start, length, 0, 0);

if (cw_unpack_next(&uc)) ERROR;
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_MAP || uc.item.as.map.size != 2) ERROR;

if (cw_unpack_next(&uc)) ERROR;
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_STR || uc.item.as.str.length != 7)) ERROR;
if (strncmp("compact", uc.item.as.str.start, 7)) ERROR;

if (cw_unpack_next(&uc)) ERROR;
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_BOOLEAN || uc.item.as.boolean != true) ERROR;

if (cw_unpack_next(&uc)) ERROR;
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_STR || uc.item.as.str.length != 6)) ERROR;
if (strncmp("schema", uc.item.as.str.start, 6)) ERROR;

if (cw_unpack_next(&uc)) ERROR;
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER || uc.item.as.u64 != 0) ERROR;

if (cw_unpack_next(&uc) != CWP_RC_END_OF_INPUT) ERROR;
cw_unpack_next(&uc);
if (uc.return_code != CWP_RC_END_OF_INPUT) ERROR;
}
```
In the examples folder there are more examples.
## Backward compatibility
CWPack may be run in compatibility mode. It affects only packing; EXTs are considered illegal, BINs is transformed to STRs and generation of STR8 is supressed.
## Error handling
CWPack does not check for illegal values (e.g. in STRs for illegal unicode characters).
When an error is detected in a context, that context is stopped and all calls are returned
with the stopped error code.
When an error is detected in a context, that context is stopped and all future calls to that context are immediatly returned without any actions.
## Build
CWPack consists of a single src file with corresponding header file. It is written
in C and the files are together ~ 1K lines. No build is neccesary, just include the
CWPack consists of a single src file and two header files. It is written
in strict ansi C and the files are together ~ 1.1K lines. No separate build is neccesary, just include the
files in your own build.
CWPack has no dependencies to other libraries.
## Module test
## Test
Included in the test folder in the repository is a simple module test and a shell script to run it.
Included in the test folder are a module test and a performance test and shell scripts to run them.
14 changes: 14 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#CWPack - Example

The example contains a program that takes a json file and then converts it to a messagePack file, then converts the latter back to json.

In the script runExample.sh the 2 json files are also diffed.

The files `item.(h,c)` contains a memory tree representation of json data and the conversion routines:

- Item Tree To Json File
- Item Tree To MessagePack File
- Json File To Item Tree
- MessagePack File To Item Tree

The conversion routines are just examples and not of production quality.
64 changes: 0 additions & 64 deletions example/contexts.h

This file was deleted.

14 changes: 8 additions & 6 deletions example/item.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <stdio.h>

#include "item.h"
#include "contexts.h"
#include "basic_contexts.h"



Expand Down Expand Up @@ -435,9 +435,10 @@ static void item32packContext(cw_pack_context* pc, item_root* item)

void item32cwpackFile (FILE* file, item_root* item)
{
cw_pack_context* pc = new_file_pack_context(10, file);
item32packContext (pc, item);
flush_file_pack_context(pc);
stream_pack_context spc;
init_stream_pack_context(&spc, 10, file);
item32packContext (&spc.pc, item);
flush_stream_pack_context(&spc);
}


Expand Down Expand Up @@ -519,8 +520,9 @@ static item_root* packContext2item3 (cw_unpack_context* uc)

item_root* cwpackFile2item3 (FILE* file)
{
cw_unpack_context* uc = new_file_unpack_context(10, file);
return packContext2item3(uc);
stream_unpack_context suc;
init_stream_unpack_context(&suc, 0, file);
return packContext2item3(&suc.uc);
}


Expand Down
9 changes: 1 addition & 8 deletions example/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
#define item_h


#ifdef __cplusplus
extern "C" {
#endif



/************** ITEMS **********************/

Expand Down Expand Up @@ -85,8 +82,4 @@ extern "C" {



#ifdef __cplusplus
}
#endif

#endif /* item_h */
2 changes: 1 addition & 1 deletion example/json2cwpack2json.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <stdio.h>

#include "item.h"
#include "contexts.h"
#include "basic_contexts.h"


int main(int argc, const char * argv[])
Expand Down
4 changes: 2 additions & 2 deletions example/runExample.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
clang -ansi -I ../src/ -o json2cwpack2json *.c ../src/*.c
clang -ansi -I ../src/ -I ../goodies/basic-contexts/ *.c ../src/*.c ../goodies/basic-contexts/*.c -o json2cwpack2json
./json2cwpack2json test1.json
diff -a test1.json test1.json.msgpack.json
rm -f *.o test1.json.msgpack.json json2cwpack2json
rm -f *.o json2cwpack2json
10 changes: 10 additions & 0 deletions goodies/basic-contexts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#CWPack - Goodies - Basic Contexts


Basic contexts contains 3 contexts:

- **Dynamic Memory Pack Context** is used when you want to pack to a malloc´d memory buffer. At buffer overflow the context handler tries to reallocate the buffer to a larger size.

- **File Pack Context** is used when you pack to a file. At buffer overflow the context handler writes the buffer out and then reuses it. If an item is larger than the buffer, the handler tries to reallocate the buffer so the item would fit.

- **File Unpack Context** is used when you unpack from a file. As with File Pack Context, the handler asserts that an items will always fit in the buffer.
Loading

0 comments on commit 3e5002d

Please sign in to comment.