ndtypes 0.2.0dev4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +50 -0
  3. data/Gemfile +2 -0
  4. data/History.md +0 -0
  5. data/README.md +19 -0
  6. data/Rakefile +125 -0
  7. data/ext/ruby_ndtypes/extconf.rb +55 -0
  8. data/ext/ruby_ndtypes/gc_guard.c +36 -0
  9. data/ext/ruby_ndtypes/gc_guard.h +12 -0
  10. data/ext/ruby_ndtypes/ndtypes/AUTHORS.txt +5 -0
  11. data/ext/ruby_ndtypes/ndtypes/INSTALL.txt +101 -0
  12. data/ext/ruby_ndtypes/ndtypes/LICENSE.txt +29 -0
  13. data/ext/ruby_ndtypes/ndtypes/MANIFEST.in +3 -0
  14. data/ext/ruby_ndtypes/ndtypes/Makefile.in +87 -0
  15. data/ext/ruby_ndtypes/ndtypes/README.rst +47 -0
  16. data/ext/ruby_ndtypes/ndtypes/config.guess +1530 -0
  17. data/ext/ruby_ndtypes/ndtypes/config.h.in +67 -0
  18. data/ext/ruby_ndtypes/ndtypes/config.sub +1782 -0
  19. data/ext/ruby_ndtypes/ndtypes/configure +5260 -0
  20. data/ext/ruby_ndtypes/ndtypes/configure.ac +161 -0
  21. data/ext/ruby_ndtypes/ndtypes/doc/Makefile +14 -0
  22. data/ext/ruby_ndtypes/ndtypes/doc/_static/copybutton.js +66 -0
  23. data/ext/ruby_ndtypes/ndtypes/doc/conf.py +26 -0
  24. data/ext/ruby_ndtypes/ndtypes/doc/grammar/grammar.rst +27 -0
  25. data/ext/ruby_ndtypes/ndtypes/doc/index.rst +56 -0
  26. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/context.rst +131 -0
  27. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/encodings.rst +68 -0
  28. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/fields-values.rst +175 -0
  29. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/functions.rst +72 -0
  30. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/index.rst +43 -0
  31. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/init.rst +48 -0
  32. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/io.rst +100 -0
  33. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/memory.rst +124 -0
  34. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/predicates.rst +110 -0
  35. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/typedef.rst +31 -0
  36. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/types.rst +594 -0
  37. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/util.rst +166 -0
  38. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/buffer-protocol.rst +27 -0
  39. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/index.rst +21 -0
  40. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/pattern-matching.rst +330 -0
  41. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/quickstart.rst +144 -0
  42. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/types.rst +544 -0
  43. data/ext/ruby_ndtypes/ndtypes/doc/releases/index.rst +35 -0
  44. data/ext/ruby_ndtypes/ndtypes/install-sh +527 -0
  45. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.in +271 -0
  46. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.vc +269 -0
  47. data/ext/ruby_ndtypes/ndtypes/libndtypes/alloc.c +230 -0
  48. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.c +268 -0
  49. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.h +109 -0
  50. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.in +73 -0
  51. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.vc +70 -0
  52. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/README.txt +16 -0
  53. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.c +2179 -0
  54. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.h +134 -0
  55. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.y +428 -0
  56. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.c +2543 -0
  57. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.h +735 -0
  58. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.l +176 -0
  59. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/export.c +543 -0
  60. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/import.c +110 -0
  61. data/ext/ruby_ndtypes/ndtypes/libndtypes/context.c +228 -0
  62. data/ext/ruby_ndtypes/ndtypes/libndtypes/copy.c +634 -0
  63. data/ext/ruby_ndtypes/ndtypes/libndtypes/encodings.c +116 -0
  64. data/ext/ruby_ndtypes/ndtypes/libndtypes/equal.c +288 -0
  65. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.c +3067 -0
  66. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.h +180 -0
  67. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.y +417 -0
  68. data/ext/ruby_ndtypes/ndtypes/libndtypes/io.c +1658 -0
  69. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.c +2773 -0
  70. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.h +734 -0
  71. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.l +222 -0
  72. data/ext/ruby_ndtypes/ndtypes/libndtypes/match.c +1132 -0
  73. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.c +2323 -0
  74. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.h.in +893 -0
  75. data/ext/ruby_ndtypes/ndtypes/libndtypes/overflow.h +161 -0
  76. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.c +473 -0
  77. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.h +92 -0
  78. data/ext/ruby_ndtypes/ndtypes/libndtypes/parser.c +246 -0
  79. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.c +269 -0
  80. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.h +197 -0
  81. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.in +48 -0
  82. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.vc +46 -0
  83. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/deserialize.c +1007 -0
  84. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/serialize.c +442 -0
  85. data/ext/ruby_ndtypes/ndtypes/libndtypes/slice.h +42 -0
  86. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.c +238 -0
  87. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.h +50 -0
  88. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.c +371 -0
  89. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.h +100 -0
  90. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.in +55 -0
  91. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.vc +45 -0
  92. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.c +82 -0
  93. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.h +49 -0
  94. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/runtest.c +1657 -0
  95. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test.h +85 -0
  96. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_array.c +115 -0
  97. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_buffer.c +137 -0
  98. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_indent.c +201 -0
  99. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_match.c +2397 -0
  100. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_numba.c +57 -0
  101. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse.c +349 -0
  102. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_error.c +27839 -0
  103. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_roundtrip.c +350 -0
  104. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_record.c +231 -0
  105. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typecheck.c +375 -0
  106. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typedef.c +65 -0
  107. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/valgrind.supp +30 -0
  108. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/bench.c +79 -0
  109. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/indent.c +94 -0
  110. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/print_ast.c +96 -0
  111. data/ext/ruby_ndtypes/ndtypes/libndtypes/util.c +474 -0
  112. data/ext/ruby_ndtypes/ndtypes/libndtypes/values.c +228 -0
  113. data/ext/ruby_ndtypes/ndtypes/python/bench.py +49 -0
  114. data/ext/ruby_ndtypes/ndtypes/python/ndt_randtype.py +409 -0
  115. data/ext/ruby_ndtypes/ndtypes/python/ndt_support.py +14 -0
  116. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/__init__.py +70 -0
  117. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/_ndtypes.c +1332 -0
  118. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/docstrings.h +319 -0
  119. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/pyndtypes.h +154 -0
  120. data/ext/ruby_ndtypes/ndtypes/python/test_ndtypes.py +1977 -0
  121. data/ext/ruby_ndtypes/ndtypes/setup.py +288 -0
  122. data/ext/ruby_ndtypes/ndtypes/vcbuild/INSTALL.txt +41 -0
  123. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest32.bat +15 -0
  124. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest64.bat +13 -0
  125. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild32.bat +38 -0
  126. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild64.bat +38 -0
  127. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcclean.bat +13 -0
  128. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcdistclean.bat +14 -0
  129. data/ext/ruby_ndtypes/ruby_ndtypes.c +1003 -0
  130. data/ext/ruby_ndtypes/ruby_ndtypes.h +37 -0
  131. data/ext/ruby_ndtypes/ruby_ndtypes_internal.h +28 -0
  132. data/lib/ndtypes.rb +45 -0
  133. data/lib/ndtypes/errors.rb +2 -0
  134. data/lib/ndtypes/version.rb +6 -0
  135. data/ndtypes.gemspec +47 -0
  136. data/spec/gc_table_spec.rb +10 -0
  137. data/spec/ndtypes_spec.rb +289 -0
  138. data/spec/spec_helper.rb +241 -0
  139. metadata +242 -0
@@ -0,0 +1,197 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #ifndef SEQ_H
35
+ #define SEQ_H
36
+
37
+
38
+ #include "ndtypes.h"
39
+
40
+
41
+ /*****************************************************************************/
42
+ /* Strongly typed sequences */
43
+ /*****************************************************************************/
44
+
45
+ /* Create a new sequence with one initial element. Steal the element. */
46
+ #define NDT_SEQ_NEW(elem) \
47
+ elem##_seq_t * \
48
+ elem##_seq_new(elem##_t *elt, ndt_context_t *ctx) \
49
+ { \
50
+ elem##_seq_t *seq; \
51
+ elem##_t *ptr; \
52
+ \
53
+ seq = ndt_alloc(1, sizeof *seq); \
54
+ if (seq == NULL) { \
55
+ elem##_del(elt); \
56
+ return ndt_memory_error(ctx); \
57
+ } \
58
+ \
59
+ ptr = ndt_alloc(2, sizeof *ptr); \
60
+ if (ptr == NULL) { \
61
+ ndt_free(seq); \
62
+ elem##_del(elt); \
63
+ return ndt_memory_error(ctx); \
64
+ } \
65
+ \
66
+ ptr[0] = *elt; \
67
+ seq->len = 1; \
68
+ seq->reserved = 2; \
69
+ seq->ptr = ptr; \
70
+ \
71
+ ndt_free(elt); \
72
+ return seq; \
73
+ }
74
+
75
+ /* Delete a sequence */
76
+ #define NDT_SEQ_DEL(elem) \
77
+ void \
78
+ elem##_seq_del(elem##_seq_t *seq) \
79
+ { \
80
+ if (seq != NULL) { \
81
+ elem##_array_del(seq->ptr, seq->len); \
82
+ ndt_free(seq); \
83
+ } \
84
+ }
85
+
86
+ /* Grow a sequence */
87
+ #define NDT_SEQ_GROW(elem) \
88
+ static int \
89
+ elem##_seq_grow(elem##_seq_t *seq, ndt_context_t *ctx) \
90
+ { \
91
+ elem##_t *ptr; \
92
+ \
93
+ ptr = ndt_realloc(seq->ptr, seq->reserved, 2 * (sizeof *ptr)); \
94
+ if (ptr == NULL) { \
95
+ ndt_err_format(ctx, NDT_MemoryError, "out of memory"); \
96
+ return -1; \
97
+ } \
98
+ \
99
+ seq->ptr = ptr; \
100
+ seq->reserved = 2 * seq->reserved; \
101
+ \
102
+ return 0; \
103
+ }
104
+
105
+ /* Append an element to a sequence */
106
+ #define NDT_SEQ_APPEND(elem) \
107
+ elem##_seq_t * \
108
+ elem##_seq_append(elem##_seq_t *seq, elem##_t *elt, ndt_context_t *ctx) \
109
+ { \
110
+ assert(seq->len <= seq->reserved); \
111
+ \
112
+ if (seq->len == seq->reserved) { \
113
+ if (elem##_seq_grow(seq, ctx) < 0) { \
114
+ elem##_seq_del(seq); \
115
+ elem##_del(elt); \
116
+ return NULL; \
117
+ } \
118
+ } \
119
+ \
120
+ seq->ptr[seq->len] = *elt; \
121
+ seq->len++; \
122
+ \
123
+ ndt_free(elt); \
124
+ return seq; \
125
+ }
126
+
127
+ #define NDT_SEQ_FINALIZE(elem) \
128
+ elem##_seq_t * \
129
+ elem##_seq_finalize(elem##_seq_t *seq) \
130
+ { \
131
+ elem##_t *ptr; \
132
+ \
133
+ if (seq == NULL) { \
134
+ return NULL; \
135
+ } \
136
+ \
137
+ assert(seq->len <= seq->reserved); \
138
+ \
139
+ ptr = ndt_realloc(seq->ptr, seq->len, sizeof *ptr); \
140
+ if (ptr == NULL) { \
141
+ return seq; /* seq is still valid */ \
142
+ } \
143
+ \
144
+ seq->ptr = ptr; \
145
+ seq->reserved = seq->len; \
146
+ \
147
+ return seq; \
148
+ }
149
+
150
+ typedef struct {
151
+ int64_t len;
152
+ int64_t reserved;
153
+ ndt_field_t *ptr;
154
+ } ndt_field_seq_t;
155
+
156
+ typedef struct {
157
+ int64_t len;
158
+ int64_t reserved;
159
+ ndt_value_t *ptr;
160
+ } ndt_value_seq_t;
161
+
162
+ typedef struct {
163
+ int64_t len;
164
+ int64_t reserved;
165
+ char **ptr;
166
+ } ndt_string_seq_t;
167
+
168
+ typedef struct {
169
+ int64_t len;
170
+ int64_t reserved;
171
+ ndt_t **ptr;
172
+ } ndt_type_seq_t;
173
+
174
+
175
+ ndt_field_seq_t *ndt_field_seq_new(ndt_field_t *, ndt_context_t *ctx);
176
+ void ndt_field_seq_del(ndt_field_seq_t *);
177
+ ndt_field_seq_t *ndt_field_seq_append(ndt_field_seq_t *, ndt_field_t *, ndt_context_t *ctx);
178
+ ndt_field_seq_t *ndt_field_seq_finalize(ndt_field_seq_t *);
179
+
180
+ ndt_value_seq_t *ndt_value_seq_new(ndt_value_t *, ndt_context_t *ctx);
181
+ void ndt_value_seq_del(ndt_value_seq_t *);
182
+ ndt_value_seq_t *ndt_value_seq_append(ndt_value_seq_t *, ndt_value_t *, ndt_context_t *ctx);
183
+ ndt_value_seq_t *ndt_value_seq_finalize(ndt_value_seq_t *);
184
+
185
+ ndt_string_seq_t *ndt_string_seq_new(char *, ndt_context_t *ctx);
186
+ void ndt_string_seq_del(ndt_string_seq_t *);
187
+ ndt_string_seq_t *ndt_string_seq_append(ndt_string_seq_t *, char *, ndt_context_t *ctx);
188
+ ndt_string_seq_t *ndt_string_seq_finalize(ndt_string_seq_t *);
189
+
190
+ ndt_type_seq_t *ndt_type_seq_empty(ndt_context_t *ctx);
191
+ ndt_type_seq_t *ndt_type_seq_new(ndt_t *, ndt_context_t *ctx);
192
+ void ndt_type_seq_del(ndt_type_seq_t *);
193
+ ndt_type_seq_t *ndt_type_seq_append(ndt_type_seq_t *, ndt_t *, ndt_context_t *ctx);
194
+ ndt_type_seq_t *ndt_type_seq_finalize(ndt_type_seq_t *);
195
+
196
+
197
+ #endif /* SEQ_H */
@@ -0,0 +1,48 @@
1
+
2
+ CC = @CC@
3
+ LD = @LD@
4
+
5
+ CONFIGURE_CFLAGS = @CONFIGURE_CFLAGS@
6
+ NDT_CFLAGS = $(strip $(CONFIGURE_CFLAGS) $(CFLAGS))
7
+ NDT_CFLAGS_SHARED = $(NDT_CFLAGS) -fPIC
8
+
9
+ CONFIGURE_COV_CFLAGS = @CONFIGURE_COV_CFLAGS@
10
+ ifeq ($(MAKECMDGOALS), coverage)
11
+ NDT_CFLAGS = $(strip $(CONFIGURE_COV_CFLAGS) $(CFLAGS))
12
+ endif
13
+
14
+
15
+ OBJS = serialize.o deserialize.o
16
+ SHARED_OBJS = .objs/serialize.o .objs/deserialize.o
17
+
18
+
19
+ default: $(OBJS) $(SHARED_OBJS)
20
+
21
+
22
+ serialize.o:\
23
+ Makefile serialize.c ../ndtypes.h
24
+ $(CC) $(NDT_CFLAGS) -c serialize.c
25
+
26
+ .objs/serialize.o:\
27
+ Makefile serialize.c ../ndtypes.h
28
+ $(CC) $(NDT_CFLAGS_SHARED) -c serialize.c -o .objs/serialize.o
29
+
30
+ deserialize.o:\
31
+ Makefile deserialize.c ../ndtypes.h
32
+ $(CC) $(NDT_CFLAGS) -c deserialize.c
33
+
34
+ .objs/deserialize.o:\
35
+ Makefile deserialize.c ../ndtypes.h
36
+ $(CC) $(NDT_CFLAGS_SHARED) -c deserialize.c -o .objs/deserialize.o
37
+
38
+
39
+ FORCE:
40
+
41
+ clean: FORCE
42
+ rm -f *.o *.so *.gch *.gcda *.gcno *.gcov *.dyn *.dpi *.lock
43
+ cd .objs && rm -f *.o *.so *.gch *.gcda *.gcno *.gcov *.dyn *.dpi *.lock
44
+
45
+ distclean: clean
46
+ rm -f Makefile
47
+
48
+
@@ -0,0 +1,46 @@
1
+
2
+ SRCDIR = ..
3
+
4
+ OPT = /MT /Ox /GS /EHsc
5
+ OPT_SHARED = /DNDT_EXPORT /MD /Ox /GS /EHsc /Fo.objs^\
6
+
7
+ COMMON_CFLAGS = -I$(SRCDIR) /nologo /W4 /wd4200 /wd4201 /wd4204 /D_CRT_SECURE_NO_WARNINGS
8
+
9
+ CFLAGS = $(COMMON_CFLAGS) $(OPT)
10
+ CFLAGS_SHARED = $(COMMON_CFLAGS) $(OPT_SHARED)
11
+
12
+
13
+ OBJS = serialize.obj deserialize.obj
14
+ SHARED_OBJS = .objs\serialize.obj .objs\deserialize.obj
15
+
16
+
17
+ default: $(OBJS) $(SHARED_OBJS)
18
+
19
+
20
+ serialize.obj:\
21
+ Makefile serialize.c ..\ndtypes.h
22
+ $(CC) $(CFLAGS) -c serialize.c
23
+
24
+ .objs\serialize.obj:\
25
+ Makefile serialize.c ..\ndtypes.h
26
+ $(CC) $(CFLAGS_SHARED) -c serialize.c
27
+
28
+ deserialize.obj:\
29
+ Makefile deserialize.c ..\ndtypes.h
30
+ $(CC) $(CFLAGS) -c deserialize.c
31
+
32
+ .objs\deserialize.obj:\
33
+ Makefile deserialize.c ..\ndtypes.h
34
+ $(CC) $(CFLAGS_SHARED) -c deserialize.c
35
+
36
+
37
+ FORCE:
38
+
39
+ clean: FORCE
40
+ del /q /f *.exe *.obj *.lib *.dll *.exp *.manifest 2>NUL
41
+ cd .objs && del /q /f *.obj 2>NUL
42
+
43
+ distclean: clean
44
+ del Makefile 2>NUL
45
+
46
+
@@ -0,0 +1,1007 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #include <stdlib.h>
35
+ #include <stdint.h>
36
+ #include <inttypes.h>
37
+ #include <string.h>
38
+ #include <assert.h>
39
+ #include "ndtypes.h"
40
+ #include "overflow.h"
41
+
42
+
43
+ static ndt_t *read_type(ndt_meta_t *m, const char * const ptr, int64_t offset,
44
+ const int64_t len, ndt_context_t *ctx);
45
+
46
+
47
+
48
+ typedef double float64_t;
49
+ typedef bool bool_t;
50
+
51
+ typedef enum ndt ndt_tag;
52
+ typedef enum ndt_access ndt_access;
53
+ typedef enum ndt_variadic ndt_variadic;
54
+ typedef enum ndt_encoding ndt_encoding;
55
+ typedef enum ndt_value ndt_value;
56
+ typedef enum ndt_contig ndt_contig;
57
+
58
+ typedef struct {
59
+ enum ndt tag;
60
+ enum ndt_access access;
61
+ uint32_t flags;
62
+ int ndim;
63
+ int64_t datasize;
64
+ uint16_t align;
65
+ } common_t;
66
+
67
+
68
+ /*****************************************************************************/
69
+ /* Read values from the bytes buffer */
70
+ /*****************************************************************************/
71
+
72
+ static inline int64_t
73
+ next_offset(const int64_t offset, const size_t size, const int64_t len,
74
+ ndt_context_t *ctx)
75
+ {
76
+ bool overflow = 0;
77
+ int64_t next;
78
+
79
+ next = ADDi64(offset, (int64_t)size, &overflow);
80
+ if (overflow || next > len) {
81
+ ndt_err_format(ctx, NDT_ValueError,
82
+ "buffer overflow in type deserialization");
83
+ return -1;
84
+ }
85
+
86
+ return next;
87
+ }
88
+
89
+ static inline size_t
90
+ array_size(const int64_t nmemb, const size_t size, ndt_context_t *ctx)
91
+ {
92
+ bool overflow = 0;
93
+ size_t array_size;
94
+
95
+ array_size = MULi64_size(nmemb, (int64_t)size, &overflow);
96
+ if (nmemb < 0 || overflow) {
97
+ ndt_err_format(ctx, NDT_ValueError,
98
+ "corrupted data or buffer overflow in type deserialization");
99
+ return SIZE_MAX;
100
+ }
101
+
102
+ return array_size;
103
+ }
104
+
105
+ static inline int64_t
106
+ string_size(const char * const ptr, const int64_t offset, const int64_t len,
107
+ ndt_context_t *ctx)
108
+ {
109
+ const int64_t n = len-offset;
110
+ const char *end = memchr(ptr+offset, '\0', (size_t)n);
111
+
112
+ if (end == NULL) {
113
+ ndt_err_format(ctx, NDT_ValueError,
114
+ "buffer overflow in type deserialization");
115
+ return -1;
116
+ }
117
+
118
+ return end-(ptr+offset)+1;
119
+ }
120
+
121
+ #define READ(type) \
122
+ static inline int64_t \
123
+ read_##type(type##_t * const value, const char * const ptr, \
124
+ const int64_t offset, const int64_t len, \
125
+ ndt_context_t *ctx ) \
126
+ { \
127
+ const size_t size = sizeof(type##_t); \
128
+ const int64_t next = next_offset(offset, size, len, ctx); \
129
+ if (next < 0) { \
130
+ return -1; \
131
+ } \
132
+ \
133
+ memcpy(value, ptr+offset, size); \
134
+ return next; \
135
+ }
136
+
137
+ #define READ_POS(type) \
138
+ static inline int64_t \
139
+ read_pos_##type(type##_t * const value, const char * const ptr, \
140
+ const int64_t offset, const int64_t len, \
141
+ ndt_context_t *ctx) \
142
+ { \
143
+ const size_t size = sizeof(type##_t); \
144
+ const int64_t next = next_offset(offset, size, len, ctx); \
145
+ if (next < 0) { \
146
+ return -1; \
147
+ } \
148
+ \
149
+ memcpy(value, ptr+offset, size); \
150
+ if (*value < 0) { \
151
+ ndt_err_format(ctx, NDT_ValueError, \
152
+ "unexpected negative value in deserialization (corrupt data?)"); \
153
+ return -1; \
154
+ } \
155
+ return next; \
156
+ }
157
+
158
+ #define READ_CAST(dtype, stype) \
159
+ static inline int64_t \
160
+ dtype##_from_##stype(dtype * const value, const char * const ptr, \
161
+ const int64_t offset, const int64_t len, \
162
+ ndt_context_t *ctx) \
163
+ { \
164
+ stype##_t x; \
165
+ const size_t size = sizeof x; \
166
+ const int64_t next = next_offset(offset, size, len, ctx); \
167
+ if (next < 0) { \
168
+ return -1; \
169
+ } \
170
+ \
171
+ memcpy(&x, ptr+offset, size); \
172
+ *value = (dtype)x; \
173
+ return next; \
174
+ }
175
+
176
+ #define READ_ARRAY(type) \
177
+ static inline int64_t \
178
+ read_##type##_array(type##_t * const v, const int64_t nmemb, \
179
+ const char * const ptr, const int64_t offset, \
180
+ const int64_t len, ndt_context_t *ctx) \
181
+ { \
182
+ const size_t size = array_size(nmemb, sizeof(type##_t), ctx); \
183
+ if (size == SIZE_MAX) { \
184
+ return -1; \
185
+ } \
186
+ \
187
+ const int64_t next = next_offset(offset, size, len, ctx); \
188
+ if (next < 0) { \
189
+ return -1; \
190
+ } \
191
+ \
192
+ memcpy(v, ptr+offset, size); \
193
+ return next; \
194
+ }
195
+
196
+ READ_CAST(ndt_tag, uint8)
197
+ READ_CAST(ndt_access, uint8)
198
+ READ_CAST(ndt_variadic, uint8)
199
+ READ_CAST(ndt_encoding, uint8)
200
+ READ_CAST(ndt_value, uint8)
201
+ READ_CAST(bool, uint8)
202
+ READ_CAST(ndt_contig, uint8)
203
+
204
+ READ_POS(int64)
205
+ READ_POS(int32)
206
+
207
+ READ(uint16)
208
+ READ(uint32)
209
+ READ(int64)
210
+ READ(float64)
211
+ READ_ARRAY(uint16)
212
+ READ_ARRAY(int32)
213
+ READ_ARRAY(int64)
214
+ READ_ARRAY(ndt_slice)
215
+
216
+ static inline int64_t
217
+ next_metaoffset(int64_t *offset, const char * const ptr,
218
+ int64_t metaoffset, const int64_t len,
219
+ ndt_context_t *ctx)
220
+ {
221
+ metaoffset = read_pos_int64(offset, ptr, metaoffset, len, ctx);
222
+ if (metaoffset < 0 || *offset >= len) {
223
+ ndt_err_format(ctx, NDT_ValueError,
224
+ "corrupt data or buffer overflow in type deserialization");
225
+ return -1;
226
+ }
227
+
228
+ return metaoffset;
229
+ }
230
+
231
+
232
+ /*****************************************************************************/
233
+ /* Deserialize types */
234
+ /*****************************************************************************/
235
+
236
+ static void
237
+ copy_common(ndt_t *t, const common_t *f)
238
+ {
239
+ t->tag = f->tag;
240
+ t->access = f->access;
241
+ t->flags = f->flags;
242
+ t->ndim = f->ndim;
243
+ t->datasize = f->datasize;
244
+ t->align = f->align;
245
+ }
246
+
247
+ static ndt_t *
248
+ new_copy_common(const common_t *f, ndt_context_t *ctx)
249
+ {
250
+ ndt_t *t = ndt_new(f->tag, ctx);
251
+ if (t == NULL) {
252
+ return NULL;
253
+ }
254
+
255
+ copy_common(t, f);
256
+ return t;
257
+ }
258
+
259
+ static int64_t
260
+ read_string(char **dest, const char * const ptr, const int64_t offset,
261
+ const int64_t len, ndt_context_t *ctx)
262
+ {
263
+ const int64_t size = string_size(ptr, offset, len, ctx);
264
+ if (size < 0) {
265
+ return -1;
266
+ }
267
+
268
+ *dest = ndt_strdup(ptr+offset, ctx);
269
+ if (*dest == NULL) {
270
+ return -1;
271
+ }
272
+
273
+ return offset + size;
274
+ }
275
+
276
+ static int64_t
277
+ read_string_array(char *s[], int64_t shape, const char * const ptr,
278
+ int64_t offset, const int64_t len,
279
+ ndt_context_t *ctx)
280
+ {
281
+ for (int64_t i = 0; i < shape; i++) {
282
+ offset = read_string(&s[i], ptr, offset, len, ctx);
283
+ if (offset < 0) {
284
+ for (int64_t k = 0; k < i; k++) {
285
+ ndt_free(s[k]);
286
+ s[k] = NULL;
287
+ }
288
+ return -1;
289
+ }
290
+ }
291
+
292
+ return offset;
293
+ }
294
+
295
+ static inline int64_t
296
+ read_ndt_value_array(ndt_value_t *v, const int64_t nmemb,
297
+ const char * const ptr,
298
+ int64_t offset, const int64_t len,
299
+ ndt_context_t *ctx)
300
+ {
301
+ for (int64_t i = 0; i < nmemb; i++) {
302
+ offset = ndt_value_from_uint8(&v[i].tag, ptr, offset, len, ctx);
303
+ if (offset < 0) return -1;
304
+
305
+ switch (v[i].tag) {
306
+ case ValNA:
307
+ break;
308
+ case ValBool:
309
+ offset = bool_from_uint8(&v[i].ValBool, ptr, offset, len, ctx);
310
+ if (offset < 0) return -1;
311
+ break;
312
+ case ValInt64:
313
+ offset = read_int64(&v[i].ValInt64, ptr, offset, len, ctx);
314
+ if (offset < 0) return -1;
315
+ break;
316
+ case ValFloat64:
317
+ offset = read_float64(&v[i].ValFloat64, ptr, offset, len, ctx);
318
+ if (offset < 0) return -1;
319
+ break;
320
+ case ValString:
321
+ offset = read_string(&v[i].ValString, ptr, offset, len, ctx);
322
+ if (offset < 0) return -1;
323
+ break;
324
+ }
325
+ }
326
+
327
+ return offset;
328
+ }
329
+
330
+ static int64_t
331
+ read_common_fields(common_t *fields, const char * const ptr,
332
+ int64_t offset, const int64_t len,
333
+ ndt_context_t *ctx)
334
+ {
335
+ offset = ndt_tag_from_uint8(&fields->tag, ptr, offset, len, ctx);
336
+ if (offset < 0) return -1;
337
+
338
+ offset = ndt_access_from_uint8(&fields->access, ptr, offset, len, ctx);
339
+ if (offset < 0) return -1;
340
+
341
+ offset = read_uint32(&fields->flags, ptr, offset, len, ctx);
342
+ if (offset < 0) return -1;
343
+
344
+ offset = read_pos_int32(&fields->ndim, ptr, offset, len, ctx);
345
+ if (offset < 0) return -1;
346
+
347
+ offset = read_pos_int64(&fields->datasize, ptr, offset, len, ctx);
348
+ if (offset < 0) return -1;
349
+
350
+ return read_uint16(&fields->align, ptr, offset, len, ctx);
351
+ }
352
+
353
+ static ndt_t *
354
+ read_module(ndt_meta_t *m, const common_t *fields, const char * const ptr,
355
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
356
+ {
357
+ char *name;
358
+ ndt_t *type;
359
+ ndt_t *t;
360
+
361
+ offset = read_string(&name, ptr, offset, len, ctx);
362
+ if (offset < 0) {
363
+ return NULL;
364
+ }
365
+
366
+ type = read_type(m, ptr, offset, len, ctx);
367
+ if (type == NULL) {
368
+ ndt_free(name);
369
+ return NULL;
370
+ }
371
+
372
+ t = new_copy_common(fields, ctx);
373
+ if (t == NULL) {
374
+ ndt_del(type);
375
+ ndt_free(name);
376
+ return NULL;
377
+ }
378
+ t->Module.name = name;
379
+ t->Module.type = type;
380
+
381
+ return t;
382
+ }
383
+
384
+ static ndt_t *
385
+ read_function(ndt_meta_t *m, common_t *fields, const char * const ptr,
386
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
387
+ {
388
+ int64_t metaoffset;
389
+ int64_t nin;
390
+ int64_t nout;
391
+ int64_t nargs;
392
+ ndt_t *t;
393
+
394
+ offset = read_pos_int64(&nin, ptr, offset, len, ctx);
395
+ if (offset < 0) return NULL;
396
+
397
+ offset = read_pos_int64(&nout, ptr, offset, len, ctx);
398
+ if (offset < 0) return NULL;
399
+
400
+ offset = read_pos_int64(&nargs, ptr, offset, len, ctx);
401
+ if (offset < 0) return NULL;
402
+
403
+
404
+ t = ndt_function_new(nargs, ctx);
405
+ if (t == NULL) {
406
+ return NULL;
407
+ }
408
+ copy_common(t, fields);
409
+
410
+ metaoffset = offset;
411
+ for (int64_t i = 0; i < nargs; i++) {
412
+ metaoffset = next_metaoffset(&offset, ptr, metaoffset, len, ctx);
413
+ if (metaoffset < 0) return NULL;
414
+
415
+ t->Function.types[i] = read_type(m, ptr, offset, len, ctx);
416
+ if (t->Function.types[i] == NULL) {
417
+ ndt_del(t);
418
+ return NULL;
419
+ }
420
+ }
421
+
422
+ t->Function.nin = nin;
423
+ t->Function.nout = nout;
424
+
425
+ return t;
426
+ }
427
+
428
+ static ndt_t *
429
+ read_fixed_dim(ndt_meta_t *m, const common_t *fields, const char * const ptr,
430
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
431
+ {
432
+ ndt_contig tag;
433
+ int64_t shape;
434
+ int64_t step;
435
+ int64_t itemsize;
436
+ ndt_t *type;
437
+ ndt_t *t;
438
+
439
+ offset = ndt_contig_from_uint8(&tag, ptr, offset, len, ctx);
440
+ if (offset < 0) return NULL;
441
+
442
+ offset = read_pos_int64(&shape, ptr, offset, len, ctx);
443
+ if (offset < 0) return NULL;
444
+
445
+ offset = read_int64(&step, ptr, offset, len, ctx);
446
+ if (offset < 0) return NULL;
447
+
448
+ offset = read_pos_int64(&itemsize, ptr, offset, len, ctx);
449
+ if (offset < 0) return NULL;
450
+
451
+ type = read_type(m, ptr, offset, len, ctx);
452
+ if (type == NULL) {
453
+ return NULL;
454
+ }
455
+
456
+ t = new_copy_common(fields, ctx);
457
+ if (t == NULL) {
458
+ ndt_del(type);
459
+ return NULL;
460
+ }
461
+ t->FixedDim.tag = tag;
462
+ t->FixedDim.shape = shape;
463
+ t->FixedDim.type = type;
464
+ t->Concrete.FixedDim.step = step;
465
+ t->Concrete.FixedDim.itemsize = itemsize;
466
+
467
+ return t;
468
+ }
469
+
470
+ static ndt_t *
471
+ read_symbolic_dim(ndt_meta_t *m, const common_t *fields, const char * const ptr,
472
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
473
+ {
474
+ ndt_contig tag;
475
+ char *name;
476
+ ndt_t *type;
477
+ ndt_t *t;
478
+
479
+ offset = ndt_contig_from_uint8(&tag, ptr, offset, len, ctx);
480
+ if (offset < 0) {
481
+ return NULL;
482
+ }
483
+
484
+ offset = read_string(&name, ptr, offset, len, ctx);
485
+ if (offset < 0) {
486
+ return NULL;
487
+ }
488
+
489
+ type = read_type(m, ptr, offset, len, ctx);
490
+ if (type == NULL) {
491
+ ndt_free(name);
492
+ return NULL;
493
+ }
494
+
495
+ t = new_copy_common(fields, ctx);
496
+ if (t == NULL) {
497
+ ndt_del(type);
498
+ ndt_free(name);
499
+ return NULL;
500
+ }
501
+ t->SymbolicDim.tag = tag;
502
+ t->SymbolicDim.name = name;
503
+ t->SymbolicDim.type = type;
504
+
505
+ return t;
506
+ }
507
+
508
+ static ndt_t *
509
+ read_ellipsis_dim(ndt_meta_t *m, const common_t *fields, const char * const ptr,
510
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
511
+ {
512
+ ndt_contig tag;
513
+ char *name;
514
+ ndt_t *type;
515
+ ndt_t *t;
516
+
517
+ offset = ndt_contig_from_uint8(&tag, ptr, offset, len, ctx);
518
+ if (offset < 0) {
519
+ return NULL;
520
+ }
521
+
522
+ offset = read_string(&name, ptr, offset, len, ctx);
523
+ if (offset < 0) {
524
+ return NULL;
525
+ }
526
+ if (name[0] == '\0') {
527
+ ndt_free(name);
528
+ name = NULL;
529
+ }
530
+
531
+ type = read_type(m, ptr, offset, len, ctx);
532
+ if (type == NULL) {
533
+ ndt_free(name);
534
+ return NULL;
535
+ }
536
+
537
+ t = new_copy_common(fields, ctx);
538
+ if (t == NULL) {
539
+ ndt_del(type);
540
+ ndt_free(name);
541
+ return NULL;
542
+ }
543
+ t->EllipsisDim.tag = tag;
544
+ t->EllipsisDim.name = name;
545
+ t->EllipsisDim.type = type;
546
+
547
+ return t;
548
+ }
549
+
550
+ static ndt_t *
551
+ read_var_dim(ndt_meta_t *m, const common_t *fields, const char * const ptr,
552
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
553
+ {
554
+ int64_t itemsize;
555
+ int32_t noffsets;
556
+ int32_t *offsets;
557
+ int32_t nslices = 0;
558
+ ndt_slice_t *slices = NULL;
559
+ ndt_t *type;
560
+ ndt_t *t;
561
+
562
+ offset = read_pos_int64(&itemsize, ptr, offset, len, ctx);
563
+ if (offset < 0) return NULL;
564
+
565
+ offset = read_pos_int32(&noffsets, ptr, offset, len, ctx);
566
+ if (offset < 0) return NULL;
567
+
568
+ offset = read_pos_int32(&nslices, ptr, offset, len, ctx);
569
+ if (offset < 0) return NULL;
570
+
571
+ offsets = ndt_alloc(noffsets, sizeof *offsets);
572
+ if (offsets == NULL) {
573
+ return ndt_memory_error(ctx);
574
+ }
575
+
576
+ offset = read_int32_array(offsets, noffsets, ptr, offset, len, ctx);
577
+ if (offset < 0) {
578
+ ndt_free(offsets);
579
+ return NULL;
580
+ }
581
+
582
+ slices = ndt_alloc(nslices, sizeof *slices);
583
+ if (slices == NULL) {
584
+ ndt_free(offsets);
585
+ return ndt_memory_error(ctx);
586
+ }
587
+
588
+ offset = read_ndt_slice_array(slices, nslices, ptr, offset, len, ctx);
589
+ if (offset < 0) {
590
+ ndt_free(offsets);
591
+ ndt_free(slices);
592
+ return NULL;
593
+ }
594
+
595
+ if (nslices == 0) {
596
+ ndt_free(slices);
597
+ slices = NULL;
598
+ }
599
+
600
+ type = read_type(m, ptr, offset, len, ctx);
601
+ if (type == NULL) {
602
+ ndt_free(offsets);
603
+ ndt_free(slices);
604
+ return NULL;
605
+ }
606
+
607
+ t = new_copy_common(fields, ctx);
608
+ if (t == NULL) {
609
+ ndt_free(offsets);
610
+ ndt_free(slices);
611
+ ndt_del(type);
612
+ return NULL;
613
+ }
614
+ t->VarDim.type = type;
615
+ t->Concrete.VarDim.flag = ExternalOffsets;
616
+ t->Concrete.VarDim.itemsize = itemsize;
617
+ t->Concrete.VarDim.noffsets = noffsets;
618
+ t->Concrete.VarDim.offsets = offsets;
619
+ t->Concrete.VarDim.nslices = nslices;
620
+ t->Concrete.VarDim.slices = slices;
621
+
622
+ m->noffsets[m->ndims] = noffsets;
623
+ m->offsets[m->ndims] = offsets;
624
+ m->ndims++;
625
+
626
+ return t;
627
+ }
628
+
629
+ static ndt_t *
630
+ read_tuple(ndt_meta_t *m, const common_t *fields, const char * const ptr,
631
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
632
+ {
633
+ int64_t metaoffset;
634
+ enum ndt_variadic flag;
635
+ int64_t shape;
636
+ ndt_t *t;
637
+
638
+ offset = ndt_variadic_from_uint8(&flag, ptr, offset, len, ctx);
639
+ if (offset < 0) return NULL;
640
+
641
+ offset = read_pos_int64(&shape, ptr, offset, len, ctx);
642
+ if (offset < 0) return NULL;
643
+
644
+ t = ndt_tuple_new(flag, shape, ctx);
645
+ if (t == NULL) {
646
+ return NULL;
647
+ }
648
+ copy_common(t, fields);
649
+
650
+ offset = read_int64_array(t->Concrete.Tuple.offset, shape, ptr, offset, len, ctx);
651
+ if (offset < 0) goto error;
652
+
653
+ offset = read_uint16_array(t->Concrete.Tuple.align, shape, ptr, offset, len, ctx);
654
+ if (offset < 0) goto error;
655
+
656
+ offset = read_uint16_array(t->Concrete.Tuple.pad, shape, ptr, offset, len, ctx);
657
+ if (offset < 0) goto error;
658
+
659
+ metaoffset = offset;
660
+ for (int64_t i = 0; i < shape; i++) {
661
+ metaoffset = next_metaoffset(&offset, ptr, metaoffset, len, ctx);
662
+ if (metaoffset < 0) goto error;
663
+
664
+ t->Tuple.types[i] = read_type(m, ptr, offset, len, ctx);
665
+ if (t->Tuple.types[i] == NULL) {
666
+ goto error;
667
+ }
668
+ }
669
+
670
+ return t;
671
+
672
+ error:
673
+ ndt_del(t);
674
+ return NULL;
675
+ }
676
+
677
+ static ndt_t *
678
+ read_record(ndt_meta_t *m, const common_t *fields, const char * const ptr,
679
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
680
+ {
681
+ int64_t metaoffset;
682
+ enum ndt_variadic flag;
683
+ int64_t shape;
684
+ ndt_t *t;
685
+
686
+ offset = ndt_variadic_from_uint8(&flag, ptr, offset, len, ctx);
687
+ if (offset < 0) return NULL;
688
+
689
+ offset = read_pos_int64(&shape, ptr, offset, len, ctx);
690
+ if (offset < 0) return NULL;
691
+
692
+ t = ndt_record_new(flag, shape, ctx);
693
+ if (t == NULL) {
694
+ return NULL;
695
+ }
696
+ copy_common(t, fields);
697
+
698
+ offset = read_int64_array(t->Concrete.Record.offset, shape, ptr, offset, len, ctx);
699
+ if (offset < 0) goto error;
700
+
701
+ offset = read_uint16_array(t->Concrete.Record.align, shape, ptr, offset, len, ctx);
702
+ if (offset < 0) goto error;
703
+
704
+ offset = read_uint16_array(t->Concrete.Tuple.pad, shape, ptr, offset, len, ctx);
705
+ if (offset < 0) goto error;
706
+
707
+ offset = read_string_array(t->Record.names, shape, ptr, offset, len, ctx);
708
+ if (offset < 0) goto error;
709
+
710
+ metaoffset = offset;
711
+ for (int64_t i = 0; i < shape; i++) {
712
+ metaoffset = next_metaoffset(&offset, ptr, metaoffset, len, ctx);
713
+ if (metaoffset < 0) goto error;
714
+
715
+ t->Record.types[i] = read_type(m, ptr, offset, len, ctx);
716
+ if (t->Record.types[i] == NULL) {
717
+ goto error;
718
+ }
719
+ }
720
+
721
+ return t;
722
+
723
+ error:
724
+ ndt_del(t);
725
+ return NULL;
726
+ }
727
+
728
+ static ndt_t *
729
+ read_ref(ndt_meta_t *m, const common_t *fields, const char * const ptr,
730
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
731
+ {
732
+ ndt_t *type;
733
+ ndt_t *t;
734
+
735
+ type = read_type(m, ptr, offset, len, ctx);
736
+ if (type == NULL) {
737
+ return NULL;
738
+ }
739
+
740
+ t = new_copy_common(fields, ctx);
741
+ if (t == NULL) {
742
+ ndt_del(type);
743
+ return NULL;
744
+ }
745
+ t->Ref.type = type;
746
+
747
+ return t;
748
+ }
749
+
750
+ static ndt_t *
751
+ read_constr(ndt_meta_t *m, const common_t *fields, const char * const ptr,
752
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
753
+ {
754
+ char *name;
755
+ ndt_t *type;
756
+ ndt_t *t;
757
+
758
+ offset = read_string(&name, ptr, offset, len, ctx);
759
+ if (offset < 0) return NULL;
760
+
761
+ type = read_type(m, ptr, offset, len, ctx);
762
+ if (type == NULL) {
763
+ ndt_free(name);
764
+ return NULL;
765
+ }
766
+
767
+ t = new_copy_common(fields, ctx);
768
+ if (t == NULL) {
769
+ ndt_del(type);
770
+ ndt_free(name);
771
+ return NULL;
772
+ }
773
+ t->Constr.name = name;
774
+ t->Constr.type = type;
775
+
776
+ return t;
777
+ }
778
+
779
+ static ndt_t *
780
+ read_nominal(ndt_meta_t *m, const common_t *fields, const char * const ptr,
781
+ int64_t offset, const int64_t len, ndt_context_t *ctx)
782
+ {
783
+ char *name;
784
+ ndt_t *type;
785
+ const ndt_typedef_t *d;
786
+ ndt_t *t;
787
+
788
+ offset = read_string(&name, ptr, offset, len, ctx);
789
+ if (offset < 0) return NULL;
790
+
791
+ type = read_type(m, ptr, offset, len, ctx);
792
+ if (type == NULL) {
793
+ ndt_free(name);
794
+ return NULL;
795
+ }
796
+
797
+ d = ndt_typedef_find(name, ctx);
798
+ if (d == NULL) {
799
+ ndt_free(name);
800
+ ndt_del(type);
801
+ return NULL;
802
+ }
803
+
804
+ t = new_copy_common(fields, ctx);
805
+ if (t == NULL) {
806
+ ndt_free(name);
807
+ ndt_del(type);
808
+ return NULL;
809
+ }
810
+ t->Nominal.name = name;
811
+ t->Nominal.type = type;
812
+ t->Nominal.meth = &d->meth;
813
+
814
+ return t;
815
+ }
816
+
817
+ static ndt_t *
818
+ read_categorical(const common_t *fields, const char * const ptr, int64_t offset,
819
+ const int64_t len, ndt_context_t *ctx)
820
+ {
821
+ int64_t ntypes;
822
+ ndt_value_t *types;
823
+ ndt_t *t;
824
+
825
+ offset = read_pos_int64(&ntypes, ptr, offset, len, ctx);
826
+ if (offset < 0) return NULL;
827
+
828
+ types = ndt_calloc(ntypes, sizeof *types);
829
+ if (types == NULL) {
830
+ return ndt_memory_error(ctx);
831
+ }
832
+
833
+ offset = read_ndt_value_array(types, ntypes, ptr, offset, len, ctx);
834
+ if (offset < 0) {
835
+ ndt_value_array_del(types, ntypes);
836
+ return NULL;
837
+ }
838
+
839
+ t = new_copy_common(fields, ctx);
840
+ if (t == NULL) {
841
+ ndt_value_array_del(types, ntypes);
842
+ return NULL;
843
+ }
844
+ t->Categorical.ntypes = ntypes;
845
+ t->Categorical.types = types;
846
+
847
+ return t;
848
+ }
849
+
850
+ static ndt_t *
851
+ read_fixed_string(const common_t *fields, const char * const ptr, int64_t offset,
852
+ const int64_t len, ndt_context_t *ctx)
853
+ {
854
+ int64_t size;
855
+ enum ndt_encoding encoding;
856
+ ndt_t *t;
857
+
858
+ offset = read_pos_int64(&size, ptr, offset, len, ctx);
859
+ if (offset < 0) return NULL;
860
+
861
+ offset = ndt_encoding_from_uint8(&encoding, ptr, offset, len, ctx);
862
+ if (offset < 0) return NULL;
863
+
864
+ t = new_copy_common(fields, ctx);
865
+ if (t == NULL) {
866
+ return NULL;
867
+ }
868
+ t->FixedString.size = size;
869
+ t->FixedString.encoding = encoding;
870
+
871
+ return t;
872
+ }
873
+
874
+ static ndt_t *
875
+ read_fixed_bytes(const common_t *fields, const char * const ptr, int64_t offset,
876
+ const int64_t len, ndt_context_t *ctx)
877
+ {
878
+ int64_t size;
879
+ uint16_t align;
880
+ ndt_t *t;
881
+
882
+ offset = read_pos_int64(&size, ptr, offset, len, ctx);
883
+ if (offset < 0) return NULL;
884
+
885
+ offset = read_uint16(&align, ptr, offset, len, ctx);
886
+ if (offset < 0) return NULL;
887
+
888
+ t = new_copy_common(fields, ctx);
889
+ if (t == NULL) {
890
+ return NULL;
891
+ }
892
+ t->FixedBytes.size = size;
893
+ t->FixedBytes.align = align;
894
+
895
+ return t;
896
+ }
897
+
898
+ static ndt_t *
899
+ read_bytes(const common_t *fields, const char * const ptr, int64_t offset,
900
+ const int64_t len, ndt_context_t *ctx)
901
+ {
902
+ uint16_t align;
903
+ ndt_t *t;
904
+
905
+ offset = read_uint16(&align, ptr, offset, len, ctx);
906
+ if (offset < 0) return NULL;
907
+
908
+ t = new_copy_common(fields, ctx);
909
+ if (t == NULL) {
910
+ return NULL;
911
+ }
912
+ t->Bytes.target_align = align;
913
+
914
+ return t;
915
+ }
916
+
917
+ static ndt_t *
918
+ read_char(const common_t *fields, const char * const ptr, int64_t offset,
919
+ const int64_t len, ndt_context_t *ctx)
920
+ {
921
+ enum ndt_encoding encoding;
922
+ ndt_t *t;
923
+
924
+ offset = ndt_encoding_from_uint8(&encoding, ptr, offset, len, ctx);
925
+ if (offset < 0) return NULL;
926
+
927
+ t = new_copy_common(fields, ctx);
928
+ if (t == NULL) {
929
+ return NULL;
930
+ }
931
+ t->Char.encoding = encoding;
932
+
933
+ return t;
934
+ }
935
+
936
+ static ndt_t *
937
+ read_typevar(const common_t *fields, const char * const ptr, int64_t offset,
938
+ const int64_t len, ndt_context_t *ctx)
939
+ {
940
+ char *name;
941
+ ndt_t *t;
942
+
943
+ offset = read_string(&name, ptr, offset, len, ctx);
944
+ if (offset < 0) return NULL;
945
+
946
+ t = new_copy_common(fields, ctx);
947
+ if (t == NULL) {
948
+ ndt_free(name);
949
+ return NULL;
950
+ }
951
+ t->Typevar.name = name;
952
+
953
+ return t;
954
+ }
955
+
956
+ static ndt_t *
957
+ read_type(ndt_meta_t *m, const char * const ptr, int64_t offset, const int64_t len,
958
+ ndt_context_t *ctx)
959
+ {
960
+ common_t fields;
961
+
962
+ offset = read_common_fields(&fields, ptr, offset, len, ctx);
963
+ if (offset < 0) return NULL;
964
+
965
+ switch (fields.tag) {
966
+ case Module: return read_module(m, &fields, ptr, offset, len, ctx);
967
+ case Function: return read_function(m, &fields, ptr, offset, len, ctx);
968
+ case FixedDim: return read_fixed_dim(m, &fields, ptr, offset, len, ctx);
969
+ case SymbolicDim: return read_symbolic_dim(m, &fields, ptr, offset, len, ctx);
970
+ case EllipsisDim: return read_ellipsis_dim(m, &fields, ptr, offset, len, ctx);
971
+ case VarDim: return read_var_dim(m, &fields, ptr, offset, len, ctx);
972
+ case Tuple: return read_tuple(m, &fields, ptr, offset, len, ctx);
973
+ case Record: return read_record(m, &fields, ptr, offset, len, ctx);
974
+ case Ref: return read_ref(m, &fields, ptr, offset, len, ctx);
975
+ case Constr: return read_constr(m, &fields, ptr, offset, len, ctx);
976
+ case Nominal: return read_nominal(m, &fields, ptr, offset, len, ctx);
977
+ case Categorical: return read_categorical(&fields, ptr, offset, len, ctx);
978
+ case FixedString: return read_fixed_string(&fields, ptr, offset, len, ctx);
979
+ case FixedBytes: return read_fixed_bytes(&fields, ptr, offset, len, ctx);
980
+ case Bytes: return read_bytes(&fields, ptr, offset, len, ctx);
981
+ case Char: return read_char(&fields, ptr, offset, len, ctx);
982
+ case Typevar: return read_typevar(&fields, ptr, offset, len, ctx);
983
+
984
+ case Bool:
985
+ case Int8: case Int16: case Int32: case Int64:
986
+ case Uint8: case Uint16: case Uint32: case Uint64:
987
+ case Float16: case Float32: case Float64:
988
+ case Complex32: case Complex64: case Complex128:
989
+ case String:
990
+
991
+ case AnyKind:
992
+ case ScalarKind: case SignedKind: case UnsignedKind: case FloatKind:
993
+ case ComplexKind: case FixedStringKind: case FixedBytesKind:
994
+ return new_copy_common(&fields, ctx);
995
+ }
996
+
997
+ ndt_err_format(ctx, NDT_RuntimeError,
998
+ "found invalid type tag in deserialization (corrupted data?)");
999
+ return NULL;
1000
+ }
1001
+
1002
+ ndt_t *
1003
+ ndt_deserialize(ndt_meta_t *m, const char * const ptr, int64_t len,
1004
+ ndt_context_t *ctx)
1005
+ {
1006
+ return read_type(m, ptr, 0, len, ctx);
1007
+ }