Skip to content

Commit

Permalink
Merge upstream 'v1.3.1' into amd-staging
Browse files Browse the repository at this point in the history
Change-Id: Ifed83e406bf40de6c9b20cbd4b36a1805928e70b
  • Loading branch information
Flakebi committed Aug 30, 2021
2 parents 7387247 + 0d36a9c commit aafa640
Show file tree
Hide file tree
Showing 38 changed files with 3,142 additions and 506 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ cwpackModuleTest
json2cwpack2json

# Data files
*.msgpack
*.msgpack.json
58 changes: 29 additions & 29 deletions README.md
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# CWPack

CWPack is a lightweight and yet complete implementation of the
[MessagePack](http://msgpack.org) serialization format
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).
It also supports the Timestamp extension type.

## Excellent Performance

Expand All @@ -11,15 +12,11 @@ Together with [MPack](https://github.com/ludocode/mpack), CWPack is the fastest

## Design

CWPack does no memory allocations and no file handling. All that is done
outside of CWPack.
CWPack does no memory allocations and no file handling in its basic setup. All that is done outside of CWPack. Example extensions are included.

CWPack is working against memory buffers. User defined handlers are called when buffers are
filled up (packing) or needs refill (unpack).
CWPack is working against memory buffers. User defined handlers are called when buffers are filled up (packing) or needs refill (unpack).

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.
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.

## Example

Expand All @@ -38,29 +35,22 @@ void example (void)
cw_pack_str (&pc, "schema", 6);
cw_pack_unsigned (&pc, 0);

if (pc.return_code != CWP_RC_OK) ERROR;
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);

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

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

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

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

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

if (uc.return_code != CWP_RC_OK) ERROR;
cw_unpack_next(&uc);
if (uc.return_code != CWP_RC_END_OF_INPUT) ERROR;
}
Expand All @@ -70,22 +60,32 @@ In the examples folder there are more examples.
## Backward compatibility
CWPack may be run in compatibility mode. It affects only packing; EXT is considered illegal, BIN are transformed to STR and generation of STR8 is supressed.
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.
## Error handling
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.
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.
CWPack does not check for illegal values (e.g. in STR for illegal unicode characters).
## Build
CWPack consists of a single src file and two header files. It is written
in strict ansi C and the files are together ~ 1.2K lines. No separate build is neccesary, just include the
files in your own build.
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.
CWPack has no dependencies to other libraries.
## Test
Included in the test folder are a module test and a performance test and shell scripts to run them.
# Objective-C
CWPack also contains an Objective-C interface. The MessagePack home page example would look as:
```C
CWPackContext *pc = [CWPackContext newWithContext:my_cw_pack_context];
[pc packObject:@{@"compact":@YES, @"schema":@0}];
CWUnpackContext *uc = [CWUnpackContext newWithContext:my_cw_unpack_context];
NSDictionary *dict = [uc unpackNextObject];
```
4 changes: 2 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#CWPack - Example
# CWPack / Example

The example contains a program that takes a json file and 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.*` contains a memory tree representation of json data and the conversion routines:
The files `item.*` contains a memory tree representation of json data and the conversion routines:

- Item Tree To Json File
- Item Tree To MessagePack File
Expand Down
76 changes: 38 additions & 38 deletions example/item.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* CWPack/example - item.c */
/*
The MIT License (MIT)
Copyright (c) 2017 Claes Wihlborg
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Expand Down Expand Up @@ -44,7 +44,7 @@ void freeItem3 (item_root* root)
{
freeItem3(ic->items[i]);
}

default:
free(root);
}
Expand All @@ -62,7 +62,7 @@ static void item32jsonFile (FILE* file, item_root* item)
char c;
unsigned u, ti;
static unsigned tabs = 0;

#define NEW_LINE {fprintf (file, "\n"); for (ti=0; ti<tabs; ti++) fprintf (file, "\t");}

switch (item->item_type)
Expand All @@ -83,7 +83,7 @@ static void item32jsonFile (FILE* file, item_root* item)
NEW_LINE
fprintf (file, "}");
break;

case ITEM_ARRAY:
jc = (item_container*)item;
fprintf (file, "[");
Expand All @@ -98,23 +98,23 @@ static void item32jsonFile (FILE* file, item_root* item)
NEW_LINE
fprintf(file, "]");
break;

case ITEM_NIL:
fprintf (file, "null");
break;

case ITEM_TRUE:
fprintf (file, "true");
break;

case ITEM_FALSE:
fprintf (file, "false");
break;

case ITEM_INTEGER:
fprintf (file, "%lld", ((item_integer*)item)->value);
break;

case ITEM_REAL:
sprintf (tmp, "%-25.15g", ((item_real*)item)->value);
for (i=0;i<30;i++)
Expand All @@ -129,7 +129,7 @@ static void item32jsonFile (FILE* file, item_root* item)
}
fprintf (file, "%s", tmp);
break;

case ITEM_STRING:
fprintf (file, "\"");
cp = ((item_string*)item)->string;
Expand Down Expand Up @@ -161,15 +161,15 @@ static void item32jsonFile (FILE* file, item_root* item)
case 0x0a: fprintf (file, "\\n"); break; /* LF */
case 0x0c: fprintf (file, "\\f"); break; /* FF */
case 0x0d: fprintf (file, "\\r"); break; /* CR */

default:
fprintf (file, "%c", c);
break;
}
}
fprintf (file, "\"");
break;

default: break;
}
}
Expand Down Expand Up @@ -299,7 +299,7 @@ static item_root* jsonString2item3 (const char** ptr)
{
scanSpace;
item_root* result = NULL;

char c = *(*ptr)++;
switch (c) {
case '{':
Expand All @@ -309,20 +309,20 @@ static item_root* jsonString2item3 (const char** ptr)
else
result = (item_root*)pullMapPair (ptr, 0);
break;

case '[':
scanSpace;
if (**ptr == ']')
result = (item_root*)allocate_container (ITEM_ARRAY, 0);
else
result = (item_root*)pullArray (ptr, 0);
break;

case '"': result = (item_root*)pullString (ptr, 0);break;
case 'n': result = allocate_item(item_root,ITEM_NIL,0); *ptr+=3;break;
case 't': result = allocate_item(item_root,ITEM_TRUE,0); *ptr+=3;break;
case 'f': result = allocate_item(item_root,ITEM_FALSE,0); *ptr+=4;break;

case '-':
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
{
Expand Down Expand Up @@ -355,11 +355,11 @@ static item_root* jsonString2item3 (const char** ptr)
}
}
break;

default:
break;
}

return result;
}

Expand All @@ -369,7 +369,7 @@ item_root* jsonFile2item3 (FILE* file)
fseek (file, 0, SEEK_END);
long length = ftell(file);
char* buffer = malloc (length+1);

fseek (file, 0l, SEEK_SET);
fread (buffer, 1, length, file);
buffer[length] = 0;
Expand All @@ -387,7 +387,7 @@ static void item32packContext(cw_pack_context* pc, item_root* item)
int i;
item_container* ic;
char* cp;

switch (item->item_type)
{
case ITEM_MAP:
Expand All @@ -396,39 +396,39 @@ static void item32packContext(cw_pack_context* pc, item_root* item)
for( i=0; i< ic->count; i++)
item32packContext (pc, ic->items[i]);
break;

case ITEM_ARRAY:
ic = (item_container*)item;
cw_pack_array_size(pc, ic->count);
for( i=0; i< ic->count; i++)
item32packContext (pc, ic->items[i]);
break;

case ITEM_NIL:
cw_pack_nil (pc);
break;

case ITEM_TRUE:
cw_pack_boolean(pc, true);
break;

case ITEM_FALSE:
cw_pack_boolean(pc, false);
break;

case ITEM_INTEGER:
cw_pack_signed(pc, ((item_integer*)item)->value);
break;

case ITEM_REAL:
cw_pack_double(pc, ((item_real*)item)->value);
break;

case ITEM_STRING:
cp = ((item_string*)item)->string;
cw_pack_str(pc, cp, (unsigned)strlen(cp));
break;

default: break;
}
}
Expand Down Expand Up @@ -457,7 +457,7 @@ static item_root* packContext2item3 (cw_unpack_context* uc)
case CWP_ITEM_NIL:
result = allocate_item(item_root,ITEM_NIL,0);
break;

case CWP_ITEM_BOOLEAN:
if (uc->item.as.boolean)
{
Expand All @@ -474,23 +474,23 @@ static item_root* packContext2item3 (cw_unpack_context* uc)
result = (item_root*)allocate_item(item_integer,ITEM_INTEGER,0);
((item_integer*)result)->value = uc->item.as.i64;
break;

case CWP_ITEM_FLOAT:
result = (item_root*)allocate_item(item_real,ITEM_REAL,0);
((item_real*)result)->value = uc->item.as.real;
break;

case CWP_ITEM_DOUBLE:
result = (item_root*)allocate_item(item_real,ITEM_REAL,0);
((item_real*)result)->value = uc->item.as.long_real;
break;

case CWP_ITEM_STR:
result = (item_root*)allocate_item(item_string,ITEM_STRING,uc->item.as.str.length + 1);
strncpy(((item_string*)result)->string, (const char*)uc->item.as.str.start, uc->item.as.str.length);
((item_string*)result)->string[uc->item.as.str.length] = 0;
break;

case CWP_ITEM_MAP:
dim = 2 * uc->item.as.map.size;
ic = allocate_container(ITEM_MAP, dim);
Expand All @@ -500,7 +500,7 @@ static item_root* packContext2item3 (cw_unpack_context* uc)
}
result = (item_root*)ic;
break;

case CWP_ITEM_ARRAY:
dim = uc->item.as.array.size;
ic = allocate_container(ITEM_ARRAY, dim);
Expand All @@ -510,7 +510,7 @@ static item_root* packContext2item3 (cw_unpack_context* uc)
}
result = (item_root*)ic;
break;

default:
result = NULL;
break;
Expand Down
Loading

0 comments on commit aafa640

Please sign in to comment.