cbor 0.5.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.travis.yml +5 -0
  4. data/ChangeLog +87 -0
  5. data/README.rdoc +180 -0
  6. data/Rakefile +94 -0
  7. data/cbor.gemspec +26 -0
  8. data/doclib/cbor.rb +80 -0
  9. data/doclib/cbor/buffer.rb +193 -0
  10. data/doclib/cbor/core_ext.rb +133 -0
  11. data/doclib/cbor/error.rb +14 -0
  12. data/doclib/cbor/packer.rb +133 -0
  13. data/doclib/cbor/simple.rb +15 -0
  14. data/doclib/cbor/tagged.rb +16 -0
  15. data/doclib/cbor/unpacker.rb +138 -0
  16. data/ext/cbor/buffer.c +693 -0
  17. data/ext/cbor/buffer.h +469 -0
  18. data/ext/cbor/buffer_class.c +516 -0
  19. data/ext/cbor/buffer_class.h +41 -0
  20. data/ext/cbor/cbor.h +69 -0
  21. data/ext/cbor/compat.h +136 -0
  22. data/ext/cbor/core_ext.c +181 -0
  23. data/ext/cbor/core_ext.h +35 -0
  24. data/ext/cbor/extconf.rb +25 -0
  25. data/ext/cbor/packer.c +169 -0
  26. data/ext/cbor/packer.h +337 -0
  27. data/ext/cbor/packer_class.c +304 -0
  28. data/ext/cbor/packer_class.h +39 -0
  29. data/ext/cbor/rbinit.c +51 -0
  30. data/ext/cbor/renamer.h +56 -0
  31. data/ext/cbor/rmem.c +103 -0
  32. data/ext/cbor/rmem.h +118 -0
  33. data/ext/cbor/sysdep.h +135 -0
  34. data/ext/cbor/sysdep_endian.h +59 -0
  35. data/ext/cbor/sysdep_types.h +55 -0
  36. data/ext/cbor/unpacker.c +735 -0
  37. data/ext/cbor/unpacker.h +133 -0
  38. data/ext/cbor/unpacker_class.c +417 -0
  39. data/ext/cbor/unpacker_class.h +39 -0
  40. data/lib/cbor.rb +9 -0
  41. data/lib/cbor/version.rb +3 -0
  42. data/spec/buffer_io_spec.rb +260 -0
  43. data/spec/buffer_spec.rb +576 -0
  44. data/spec/cases.cbor +0 -0
  45. data/spec/cases.cbor_stream +0 -0
  46. data/spec/cases.json +1 -0
  47. data/spec/cases.msg +0 -0
  48. data/spec/cases_compact.msg +0 -0
  49. data/spec/cases_spec.rb +39 -0
  50. data/spec/format_spec.rb +445 -0
  51. data/spec/packer_spec.rb +127 -0
  52. data/spec/random_compat.rb +24 -0
  53. data/spec/spec_helper.rb +45 -0
  54. data/spec/unpacker_spec.rb +238 -0
  55. metadata +196 -0
data/ext/cbor/rmem.c ADDED
@@ -0,0 +1,103 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+
28
+ #include "rmem.h"
29
+
30
+ void msgpack_rmem_init(msgpack_rmem_t* pm)
31
+ {
32
+ memset(pm, 0, sizeof(msgpack_rmem_t));
33
+ pm->head.pages = malloc(MSGPACK_RMEM_PAGE_SIZE * 32);
34
+ pm->head.mask = 0xffffffff; /* all bit is 1 = available */
35
+ }
36
+
37
+ void msgpack_rmem_destroy(msgpack_rmem_t* pm)
38
+ {
39
+ msgpack_rmem_chunk_t* c = pm->array_first;
40
+ msgpack_rmem_chunk_t* cend = pm->array_last;
41
+ for(; c != cend; c++) {
42
+ free(c->pages);
43
+ }
44
+ free(pm->head.pages);
45
+ free(pm->array_first);
46
+ }
47
+
48
+ void* _msgpack_rmem_alloc2(msgpack_rmem_t* pm)
49
+ {
50
+ msgpack_rmem_chunk_t* c = pm->array_first;
51
+ msgpack_rmem_chunk_t* last = pm->array_last;
52
+ for(; c != last; c++) {
53
+ if(_msgpack_rmem_chunk_available(c)) {
54
+ void* mem = _msgpack_rmem_chunk_alloc(c);
55
+
56
+ /* move to head */
57
+ msgpack_rmem_chunk_t tmp = pm->head;
58
+ pm->head = *c;
59
+ *c = tmp;
60
+ return mem;
61
+ }
62
+ }
63
+
64
+ if(c == pm->array_end) {
65
+ size_t capacity = c - pm->array_first;
66
+ size_t length = last - pm->array_first;
67
+ capacity = (capacity == 0) ? 8 : capacity * 2;
68
+ msgpack_rmem_chunk_t* array = realloc(pm->array_first, capacity * sizeof(msgpack_rmem_chunk_t));
69
+ pm->array_first = array;
70
+ pm->array_last = array + length;
71
+ pm->array_end = array + capacity;
72
+ }
73
+
74
+ /* allocate new chunk */
75
+ c = pm->array_last++;
76
+
77
+ /* move to head */
78
+ msgpack_rmem_chunk_t tmp = pm->head;
79
+ pm->head = *c;
80
+ *c = tmp;
81
+
82
+ pm->head.mask = 0xffffffff & (~1); /* "& (~1)" means first chunk is already allocated */
83
+ pm->head.pages = malloc(MSGPACK_RMEM_PAGE_SIZE * 32);
84
+
85
+ return pm->head.pages;
86
+ }
87
+
88
+ void _msgpack_rmem_chunk_free(msgpack_rmem_t* pm, msgpack_rmem_chunk_t* c)
89
+ {
90
+ if(pm->array_first->mask == 0xffffffff) {
91
+ /* free and move to last */
92
+ pm->array_last--;
93
+ free(c->pages);
94
+ *c = *pm->array_last;
95
+ return;
96
+ }
97
+
98
+ /* move to first */
99
+ msgpack_rmem_chunk_t tmp = *pm->array_first;
100
+ *pm->array_first = *c;
101
+ *c = tmp;
102
+ }
103
+
data/ext/cbor/rmem.h ADDED
@@ -0,0 +1,118 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+ #ifndef MSGPACK_RUBY_RMEM_H__
28
+ #define MSGPACK_RUBY_RMEM_H__
29
+
30
+ #include "compat.h"
31
+ #include "sysdep.h"
32
+
33
+ #ifndef MSGPACK_RMEM_PAGE_SIZE
34
+ #define MSGPACK_RMEM_PAGE_SIZE (4*1024)
35
+ #endif
36
+
37
+ struct msgpack_rmem_t;
38
+ typedef struct msgpack_rmem_t msgpack_rmem_t;
39
+
40
+ struct msgpack_rmem_chunk_t;
41
+ typedef struct msgpack_rmem_chunk_t msgpack_rmem_chunk_t;
42
+
43
+ /*
44
+ * a chunk contains 32 pages.
45
+ * size of each buffer is MSGPACK_RMEM_PAGE_SIZE bytes.
46
+ */
47
+ struct msgpack_rmem_chunk_t {
48
+ unsigned int mask;
49
+ char* pages;
50
+ };
51
+
52
+ struct msgpack_rmem_t {
53
+ msgpack_rmem_chunk_t head;
54
+ msgpack_rmem_chunk_t* array_first;
55
+ msgpack_rmem_chunk_t* array_last;
56
+ msgpack_rmem_chunk_t* array_end;
57
+ };
58
+
59
+ /* assert MSGPACK_RMEM_PAGE_SIZE % sysconf(_SC_PAGE_SIZE) == 0 */
60
+ void msgpack_rmem_init(msgpack_rmem_t* pm);
61
+
62
+ void msgpack_rmem_destroy(msgpack_rmem_t* pm);
63
+
64
+ void* _msgpack_rmem_alloc2(msgpack_rmem_t* pm);
65
+
66
+ #define _msgpack_rmem_chunk_available(c) ((c)->mask != 0)
67
+
68
+ static inline void* _msgpack_rmem_chunk_alloc(msgpack_rmem_chunk_t* c)
69
+ {
70
+ _msgpack_bsp32(pos, c->mask);
71
+ (c)->mask &= ~(1 << pos);
72
+ return ((char*)(c)->pages) + (pos * (MSGPACK_RMEM_PAGE_SIZE));
73
+ }
74
+
75
+ static inline bool _msgpack_rmem_chunk_try_free(msgpack_rmem_chunk_t* c, void* mem)
76
+ {
77
+ ptrdiff_t pdiff = ((char*)(mem)) - ((char*)(c)->pages);
78
+ if(0 <= pdiff && pdiff < MSGPACK_RMEM_PAGE_SIZE * 32) {
79
+ size_t pos = pdiff / MSGPACK_RMEM_PAGE_SIZE;
80
+ (c)->mask |= (1 << pos);
81
+ return true;
82
+ }
83
+ return false;
84
+ }
85
+
86
+ static inline void* msgpack_rmem_alloc(msgpack_rmem_t* pm)
87
+ {
88
+ if(_msgpack_rmem_chunk_available(&pm->head)) {
89
+ return _msgpack_rmem_chunk_alloc(&pm->head);
90
+ }
91
+ return _msgpack_rmem_alloc2(pm);
92
+ }
93
+
94
+ void _msgpack_rmem_chunk_free(msgpack_rmem_t* pm, msgpack_rmem_chunk_t* c);
95
+
96
+ static inline bool msgpack_rmem_free(msgpack_rmem_t* pm, void* mem)
97
+ {
98
+ if(_msgpack_rmem_chunk_try_free(&pm->head, mem)) {
99
+ return true;
100
+ }
101
+
102
+ /* search from last */
103
+ msgpack_rmem_chunk_t* c = pm->array_last - 1;
104
+ msgpack_rmem_chunk_t* before_first = pm->array_first - 1;
105
+ for(; c != before_first; c--) {
106
+ if(_msgpack_rmem_chunk_try_free(c, mem)) {
107
+ if(c != pm->array_first && c->mask == 0xffffffff) {
108
+ _msgpack_rmem_chunk_free(pm, c);
109
+ }
110
+ return true;
111
+ }
112
+ }
113
+ return false;
114
+ }
115
+
116
+
117
+ #endif
118
+
data/ext/cbor/sysdep.h ADDED
@@ -0,0 +1,135 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+ #ifndef MSGPACK_RUBY_SYSDEP_H__
28
+ #define MSGPACK_RUBY_SYSDEP_H__
29
+
30
+ #include "renamer.h"
31
+
32
+ #include "sysdep_types.h"
33
+ #include "sysdep_endian.h"
34
+
35
+
36
+ #define UNUSED(var) ((void)var)
37
+
38
+
39
+ #ifdef __LITTLE_ENDIAN__
40
+
41
+ /* _msgpack_be16 */
42
+ #ifdef _WIN32
43
+ # if defined(ntohs)
44
+ # define _msgpack_be16(x) ntohs(x)
45
+ # elif defined(_byteswap_ushort) || (defined(_MSC_VER) && _MSC_VER >= 1400)
46
+ # define _msgpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x))
47
+ # else
48
+ # define _msgpack_be16(x) ( \
49
+ ((((uint16_t)x) << 8) ) | \
50
+ ((((uint16_t)x) >> 8) ) )
51
+ # endif
52
+ #else
53
+ # define _msgpack_be16(x) ntohs(x)
54
+ #endif
55
+
56
+ /* _msgpack_be32 */
57
+ #ifdef _WIN32
58
+ # if defined(ntohl)
59
+ # define _msgpack_be32(x) ntohl(x)
60
+ # elif defined(_byteswap_ulong) || (defined(_MSC_VER) && _MSC_VER >= 1400)
61
+ # define _msgpack_be32(x) ((uint32_t)_byteswap_ulong((unsigned long)x))
62
+ # else
63
+ # define _msgpack_be32(x) \
64
+ ( ((((uint32_t)x) << 24) ) | \
65
+ ((((uint32_t)x) << 8) & 0x00ff0000U ) | \
66
+ ((((uint32_t)x) >> 8) & 0x0000ff00U ) | \
67
+ ((((uint32_t)x) >> 24) ) )
68
+ # endif
69
+ #else
70
+ # define _msgpack_be32(x) ntohl(x)
71
+ #endif
72
+
73
+ /* _msgpack_be64 */
74
+ #if defined(_byteswap_uint64) || (defined(_MSC_VER) && _MSC_VER >= 1400)
75
+ # define _msgpack_be64(x) (_byteswap_uint64(x))
76
+ #elif defined(bswap_64)
77
+ # define _msgpack_be64(x) bswap_64(x)
78
+ #elif defined(__DARWIN_OSSwapInt64)
79
+ # define _msgpack_be64(x) __DARWIN_OSSwapInt64(x)
80
+ #else
81
+ #define _msgpack_be64(x) \
82
+ ( ((((uint64_t)x) << 56) ) | \
83
+ ((((uint64_t)x) << 40) & 0x00ff000000000000ULL ) | \
84
+ ((((uint64_t)x) << 24) & 0x0000ff0000000000ULL ) | \
85
+ ((((uint64_t)x) << 8) & 0x000000ff00000000ULL ) | \
86
+ ((((uint64_t)x) >> 8) & 0x00000000ff000000ULL ) | \
87
+ ((((uint64_t)x) >> 24) & 0x0000000000ff0000ULL ) | \
88
+ ((((uint64_t)x) >> 40) & 0x000000000000ff00ULL ) | \
89
+ ((((uint64_t)x) >> 56) ) )
90
+ #endif
91
+
92
+ #else /* big endian */
93
+ #define _msgpack_be16(x) (x)
94
+ #define _msgpack_be32(x) (x)
95
+ #define _msgpack_be64(x) (x)
96
+
97
+ #endif
98
+
99
+
100
+ /* _msgpack_be_float */
101
+ #define _msgpack_be_float(x) _msgpack_be32(x)
102
+
103
+ /* _msgpack_be_double */
104
+ #if defined(__arm__) && !(__ARM_EABI__)
105
+ /* ARM OABI */
106
+ #define _msgpack_be_double(x) \
107
+ ( (((x) & 0xFFFFFFFFUL) << 32UL) | ((x) >> 32UL) )
108
+ #else
109
+ /* the other ABI */
110
+ #define _msgpack_be_double(x) _msgpack_be64(x)
111
+ #endif
112
+
113
+ /* _msgpack_bsp32 */
114
+ #if defined(_MSC_VER)
115
+ #define _msgpack_bsp32(name, val) \
116
+ long name; \
117
+ _BitScanForward(&name, val)
118
+ #else
119
+ #define _msgpack_bsp32(name, val) \
120
+ int name = __builtin_ctz(val)
121
+ /* TODO default impl for _msgpack_bsp32 */
122
+ #endif
123
+
124
+ #if SIZEOF_BDIGITS == 2
125
+ #define NTOHBDIGIT _msgpack_be16
126
+ #elif SIZEOF_BDIGITS == 4
127
+ #define NTOHBDIGIT _msgpack_be32
128
+ #elif SIZEOF_BDIGITS == 8
129
+ #define NTOHBDIGIT _msgpack_be64
130
+ #else
131
+ #error this size of bignum digits SIZEOF_BDIGITS not implemented
132
+ #endif
133
+
134
+ #endif
135
+
@@ -0,0 +1,59 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+ #ifndef MSGPACK_RUBY_SYSDEP_ENDIAN_H__
28
+ #define MSGPACK_RUBY_SYSDEP_ENDIAN_H__
29
+
30
+ /* including arpa/inet.h requires an extra dll on win32 */
31
+ #ifndef _WIN32
32
+ #include <arpa/inet.h> /* __BYTE_ORDER */
33
+ #endif
34
+
35
+ /*
36
+ * Use following command to add consitions here:
37
+ * cpp -dM `echo "#include <arpa/inet.h>" > test.c; echo test.c` | grep ENDIAN
38
+ */
39
+ #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__) /* Mac OS X */
40
+ # if defined(_LITTLE_ENDIAN) \
41
+ || ( defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) \
42
+ && __BYTE_ORDER == __LITTLE_ENDIAN ) /* Linux */ \
43
+ || ( defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \
44
+ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ) /* Solaris */
45
+ # define __LITTLE_ENDIAN__
46
+ # elif defined(_BIG_ENDIAN) \
47
+ || (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) \
48
+ && __BYTE_ORDER == __BIG_ENDIAN) /* Linux */ \
49
+ || (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) \
50
+ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) /* Solaris */
51
+ # define __BIG_ENDIAN__
52
+ # elif defined(_WIN32) /* Win32 */
53
+ # define __LITTLE_ENDIAN__
54
+ # endif
55
+ #endif
56
+
57
+
58
+ #endif
59
+
@@ -0,0 +1,55 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+ #ifndef MSGPACK_RUBY_SYSDEP_TYPES_H__
28
+ #define MSGPACK_RUBY_SYSDEP_TYPES_H__
29
+
30
+ #include <string.h>
31
+ #include <stdlib.h>
32
+
33
+ #include <stddef.h>
34
+
35
+ #if defined(_MSC_VER) && _MSC_VER < 1600
36
+ typedef __int8 int8_t;
37
+ typedef unsigned __int8 uint8_t;
38
+ typedef __int16 int16_t;
39
+ typedef unsigned __int16 uint16_t;
40
+ typedef __int32 int32_t;
41
+ typedef unsigned __int32 uint32_t;
42
+ typedef __int64 int64_t;
43
+ typedef unsigned __int64 uint64_t;
44
+
45
+ #elif defined(_MSC_VER) // && _MSC_VER >= 1600
46
+ #include <stdint.h>
47
+
48
+ #else
49
+ #include <stdint.h>
50
+ #include <stdbool.h>
51
+ #endif
52
+
53
+
54
+ #endif
55
+