slow_blink 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/ext/slow_blink/ext_compact_encoder/compact_encoder.c +258 -0
  3. data/ext/slow_blink/ext_compact_encoder/compact_encoder.h +92 -0
  4. data/ext/slow_blink/ext_compact_encoder/ext_compact_encoder.c +555 -0
  5. data/ext/slow_blink/ext_compact_encoder/extconf.rb +4 -0
  6. data/ext/slow_blink/ext_schema_parser/lexer.c +59 -59
  7. data/ext/slow_blink/ext_schema_parser/lexer.h +2 -2
  8. data/ext/slow_blink/ext_schema_parser/parser.c +380 -367
  9. data/ext/slow_blink/ext_schema_parser/parser.h +1 -1
  10. data/lib/slow_blink/annotatable.rb +1 -1
  11. data/lib/slow_blink/annotation.rb +5 -5
  12. data/lib/slow_blink/binary.rb +26 -0
  13. data/lib/slow_blink/boolean.rb +26 -0
  14. data/lib/slow_blink/compact_encoder.rb +45 -71
  15. data/lib/slow_blink/date.rb +25 -0
  16. data/lib/slow_blink/decimal.rb +26 -0
  17. data/lib/slow_blink/definition.rb +19 -10
  18. data/lib/slow_blink/enumeration.rb +14 -36
  19. data/lib/slow_blink/error.rb +19 -0
  20. data/lib/slow_blink/field.rb +3 -16
  21. data/lib/slow_blink/fixed.rb +26 -0
  22. data/lib/slow_blink/floating_point.rb +27 -0
  23. data/lib/slow_blink/group.rb +30 -43
  24. data/lib/slow_blink/incremental_annotation.rb +75 -21
  25. data/lib/slow_blink/integer.rb +65 -0
  26. data/lib/slow_blink/message/binary.rb +87 -0
  27. data/lib/slow_blink/message/boolean.rb +68 -0
  28. data/lib/slow_blink/message/date.rb +33 -0
  29. data/lib/slow_blink/message/decimal.rb +25 -0
  30. data/lib/slow_blink/message/enumeration.rb +69 -0
  31. data/lib/slow_blink/message/field.rb +73 -0
  32. data/lib/slow_blink/message/fixed.rb +84 -0
  33. data/lib/slow_blink/message/floating_point.rb +68 -0
  34. data/lib/slow_blink/message/group.rb +260 -0
  35. data/lib/slow_blink/message/integer.rb +217 -0
  36. data/lib/slow_blink/message/model.rb +202 -0
  37. data/lib/slow_blink/message/sequence.rb +85 -0
  38. data/lib/slow_blink/message/string.rb +95 -0
  39. data/lib/slow_blink/message/time.rb +78 -0
  40. data/lib/slow_blink/message/time_of_day.rb +132 -0
  41. data/lib/slow_blink/name_with_id.rb +2 -1
  42. data/lib/slow_blink/namespace.rb +140 -0
  43. data/lib/slow_blink/object.rb +29 -0
  44. data/lib/slow_blink/ref.rb +93 -0
  45. data/lib/slow_blink/schema.rb +94 -68
  46. data/lib/slow_blink/{component_reference.rb → schema_buffer.rb} +11 -22
  47. data/lib/slow_blink/sequence.rb +58 -0
  48. data/lib/slow_blink/string.rb +40 -0
  49. data/lib/slow_blink/sym.rb +4 -0
  50. data/lib/slow_blink/time.rb +30 -0
  51. data/lib/slow_blink/time_of_day.rb +30 -0
  52. data/lib/slow_blink/type.rb +7 -402
  53. data/lib/slow_blink/version.rb +1 -1
  54. data/lib/slow_blink.rb +1 -0
  55. data/rakefile +14 -3
  56. data/test/{integration/capture_stderr.rb → capture_stderr.rb} +0 -0
  57. data/test/tc_compact_encoder.rb +341 -0
  58. data/test/tc_field.rb +53 -0
  59. data/test/tc_group.rb +122 -0
  60. data/test/tc_incr_annote.rb +27 -0
  61. data/test/{integration/tc_inputs.rb → tc_inputs.rb} +7 -11
  62. data/test/tc_model.rb +65 -0
  63. data/test/tc_model_encode.rb +63 -0
  64. data/test/tc_namespace.rb +8 -0
  65. data/test/tc_types.rb +198 -0
  66. metadata +58 -11
  67. data/ext/slow_blink/ext_schema_parser/parser.l +0 -139
  68. data/ext/slow_blink/ext_schema_parser/parser.y +0 -932
  69. data/lib/slow_blink/message.rb +0 -43
@@ -0,0 +1,555 @@
1
+ /* Copyright (c) 2016 Cameron Harper
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ * this software and associated documentation files (the "Software"), to deal in
5
+ * the Software without restriction, including without limitation the rights to
6
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ * the Software, and to permit persons to whom the Software is furnished to do so,
8
+ * subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in all
11
+ * copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
+ *
20
+ *
21
+ * */
22
+
23
+ /* includes ***********************************************************/
24
+
25
+ #include <ruby.h>
26
+ #include <stdint.h>
27
+ #include <stdbool.h>
28
+ #include <assert.h>
29
+
30
+ #include "compact_encoder.h"
31
+
32
+ /* defines ************************************************************/
33
+
34
+
35
+ #define MIN8 -128L
36
+ #define MAX8 127L
37
+ #define MIN16 -32768L
38
+ #define MAX16 32767L
39
+ #define MIN32 -2147483648L
40
+ #define MAX32 2147483647L
41
+ #define MIN64 -9223372036854775808L
42
+ #define MAX64 9223372036854775807L
43
+ #define MAXU8 0xffUL
44
+ #define MAXU16 0xffffUL
45
+ #define MAXU32 0xffffffffUL
46
+ #define MAXU64 0xffffffffffffffffUL
47
+
48
+ /* static function prototypes *****************************************/
49
+
50
+ static VALUE putNull(VALUE self);
51
+
52
+ static VALUE putPresent(VALUE self, VALUE val);
53
+ static VALUE getPresent(VALUE self);
54
+
55
+ static VALUE putU8(VALUE self, VALUE val);
56
+ static VALUE putU16(VALUE self, VALUE val);
57
+ static VALUE putU32(VALUE self, VALUE val);
58
+ static VALUE putU64(VALUE self, VALUE val);
59
+ static VALUE putI8(VALUE self, VALUE val);
60
+ static VALUE putI16(VALUE self, VALUE val);
61
+ static VALUE putI32(VALUE self, VALUE val);
62
+ static VALUE putI64(VALUE self, VALUE val);
63
+ static VALUE putF64(VALUE self, VALUE val);
64
+ static VALUE putBool(VALUE self, VALUE val);
65
+
66
+ static VALUE putBinary(VALUE self, VALUE val);
67
+ static VALUE putString(VALUE self, VALUE val);
68
+ static VALUE putFixed(VALUE self, VALUE val);
69
+ static VALUE putFixedOptional(VALUE self, VALUE val);
70
+
71
+ static VALUE getU8(VALUE self);
72
+ static VALUE getU16(VALUE self);
73
+ static VALUE getU32(VALUE self);
74
+ static VALUE getU64(VALUE self);
75
+ static VALUE getI8(VALUE self);
76
+ static VALUE getI16(VALUE self);
77
+ static VALUE getI32(VALUE self);
78
+ static VALUE getI64(VALUE self);
79
+ static VALUE getF64(VALUE self);
80
+ static VALUE getBool(VALUE self);
81
+
82
+ static VALUE getBinary(VALUE self);
83
+ static VALUE getString(VALUE self);
84
+ static VALUE getFixed(VALUE self, VALUE size);
85
+ static VALUE getFixedOptional(VALUE self, VALUE size);
86
+
87
+ static VALUE putInt(VALUE self, VALUE val, int64_t min, int64_t max, bool isSigned);
88
+ static VALUE getInt(VALUE input, int64_t min, int64_t max, bool isSigned);
89
+
90
+ /* static variables ***************************************************/
91
+
92
+ static VALUE cError;
93
+
94
+ /* functions **********************************************************/
95
+
96
+ void Init_ext_compact_encoder(void)
97
+ {
98
+ VALUE cSlowBlink;
99
+
100
+ cSlowBlink = rb_define_module("SlowBlink");
101
+ cError = rb_const_get(cSlowBlink, rb_intern("Error"));
102
+
103
+ rb_define_method(rb_cString, "putNull", putNull, 0);
104
+ rb_define_method(rb_cString, "putPresent", putPresent, 0);
105
+ rb_define_method(rb_cString, "getPresent", getPresent, 0);
106
+
107
+ rb_define_method(rb_cString, "putU8", putU8, 1);
108
+ rb_define_method(rb_cString, "putU16", putU16, 1);
109
+ rb_define_method(rb_cString, "putU32", putU32, 1);
110
+ rb_define_method(rb_cString, "putU64", putU64, 1);
111
+ rb_define_method(rb_cString, "putI8", putI8, 1);
112
+ rb_define_method(rb_cString, "putI16", putI16, 1);
113
+ rb_define_method(rb_cString, "putI32", putI32, 1);
114
+ rb_define_method(rb_cString, "putI64", putI64, 1);
115
+
116
+ rb_define_method(rb_cString, "putF64", putF64, 1);
117
+
118
+ rb_define_method(rb_cString, "putBool", putBool, 1);
119
+
120
+ rb_define_method(rb_cString, "putString", putString, 1);
121
+ rb_define_method(rb_cString, "putBinary", putBinary, 1);
122
+ rb_define_method(rb_cString, "putFixed", putFixed, 1);
123
+ rb_define_method(rb_cString, "putFixedOptional", putFixedOptional, 1);
124
+
125
+ rb_define_method(rb_cString, "getU8!", getU8, 0);
126
+ rb_define_method(rb_cString, "getU16!", getU16, 0);
127
+ rb_define_method(rb_cString, "getU32!", getU32, 0);
128
+ rb_define_method(rb_cString, "getU64!", getU64, 0);
129
+ rb_define_method(rb_cString, "getI8!", getI8, 0);
130
+ rb_define_method(rb_cString, "getI16!", getI16, 0);
131
+ rb_define_method(rb_cString, "getI32!", getI32, 0);
132
+ rb_define_method(rb_cString, "getI64!", getI64, 0);
133
+
134
+ rb_define_method(rb_cString, "getF64!", getF64, 0);
135
+
136
+ rb_define_method(rb_cString, "getBool!", getBool, 0);
137
+
138
+ rb_define_method(rb_cString, "getString!", getString, 0);
139
+ rb_define_method(rb_cString, "getBinary!", getBinary, 0);
140
+ rb_define_method(rb_cString, "getFixed!", getFixed, 1);
141
+ rb_define_method(rb_cString, "getFixedOptional!", getFixedOptional, 1);
142
+ }
143
+
144
+ /* static functions ***************************************************/
145
+
146
+ static VALUE putU8(VALUE self, VALUE val)
147
+ {
148
+ return putInt(self, val, 0, MAXU8, false);
149
+ }
150
+ static VALUE putU16(VALUE self, VALUE val)
151
+ {
152
+ return putInt(self, val, 0, MAXU16, false);
153
+ }
154
+ static VALUE putU32(VALUE self, VALUE val)
155
+ {
156
+ return putInt(self, val, 0, MAXU32, false);
157
+ }
158
+ static VALUE putU64(VALUE self, VALUE val)
159
+ {
160
+ return putInt(self, val, 0, MAXU64, false);
161
+ }
162
+ static VALUE putI8(VALUE self, VALUE val)
163
+ {
164
+ return putInt(self, val, MIN8, MAX8, true);
165
+ }
166
+ static VALUE putI16(VALUE self, VALUE val)
167
+ {
168
+ return putInt(self, val, MIN16, MAX16, true);
169
+ }
170
+ static VALUE putI32(VALUE self, VALUE val)
171
+ {
172
+ return putInt(self, val, MIN32, MAX32, true);
173
+ }
174
+ static VALUE putI64(VALUE self, VALUE val)
175
+ {
176
+ return putInt(self, val, MIN64, MAX64, true);
177
+ }
178
+ static VALUE putF64(VALUE self, VALUE val)
179
+ {
180
+ VALUE retval;
181
+ uint8_t out[10U];
182
+
183
+ if(val == Qnil){
184
+
185
+ retval = putNull(self);
186
+ }
187
+ else{
188
+
189
+ double value = NUM2DBL(val);
190
+ retval = rb_str_buf_cat(self, (const char *)out, BLINK_putVLC(*((uint64_t *)(&value)), false, out, sizeof(out)));
191
+ }
192
+
193
+ return retval;
194
+ }
195
+
196
+ static VALUE putBool(VALUE self, VALUE val)
197
+ {
198
+ VALUE retval;
199
+
200
+ if(val == Qnil){
201
+
202
+ retval = putNull(self);
203
+ }
204
+ else{
205
+
206
+ retval = putU8(self, (val == Qfalse) ? INT2FIX(0) : INT2FIX(1));
207
+ }
208
+
209
+ return retval;
210
+ }
211
+
212
+ static VALUE putNull(VALUE self)
213
+ {
214
+ uint8_t str[] = {0xc0};
215
+ return rb_str_buf_cat(self, (const char *)str, sizeof(str));
216
+ }
217
+
218
+ static VALUE putPresent(VALUE self, VALUE val)
219
+ {
220
+ VALUE retval;
221
+
222
+ if(val == Qfalse){
223
+
224
+ retval = putNull(self);
225
+ }
226
+ else{
227
+
228
+ uint8_t str[] = {0x1};
229
+ retval = rb_str_buf_cat(self, (const char *)str, sizeof(str));
230
+ }
231
+
232
+ return retval;
233
+ }
234
+
235
+ static VALUE putBinary(VALUE self, VALUE val)
236
+ {
237
+ VALUE retval;
238
+
239
+ if(val == Qnil){
240
+
241
+ retval = putNull(self);
242
+ }
243
+ else{
244
+
245
+ retval = putU32(self, UINT2NUM(RSTRING_LEN(val)));
246
+ retval = rb_str_concat(retval, val);
247
+
248
+ }
249
+
250
+ return retval;
251
+ }
252
+
253
+ static VALUE putString(VALUE self, VALUE val)
254
+ {
255
+ //todo: enforce utf8 encoding
256
+ return putBinary(self, val);
257
+ }
258
+
259
+ static VALUE putFixedOptional(VALUE self, VALUE val)
260
+ {
261
+ VALUE retval;
262
+
263
+ if(val == Qnil){
264
+
265
+ retval = putNull(self);
266
+ }
267
+ else{
268
+
269
+ uint8_t str[] = {0x01};
270
+ retval = rb_str_new((const char *)str, sizeof(str));
271
+ rb_str_concat(retval, val);
272
+ }
273
+
274
+ return retval;
275
+ }
276
+
277
+ static VALUE putFixed(VALUE self, VALUE val)
278
+ {
279
+ return rb_str_dup(val);
280
+ }
281
+
282
+ static VALUE putInt(VALUE self, VALUE val, int64_t min, int64_t max, bool isSigned)
283
+ {
284
+ uint8_t out[10U];
285
+ VALUE retval;
286
+
287
+ if(val == Qnil){
288
+
289
+ retval = putNull(self);
290
+ }
291
+ else{
292
+
293
+ if(isSigned){
294
+
295
+ int64_t value = NUM2LL(val);
296
+
297
+ if((value < min) || (value > max)){
298
+
299
+ rb_raise(cError, "Input exceeds allowable range of type");
300
+ }
301
+
302
+ retval = rb_str_buf_cat(self, (const char *)out, BLINK_putVLC((uint64_t)value, true, out, sizeof(out)));
303
+ }
304
+ else{
305
+
306
+ uint64_t value = NUM2ULL(val);
307
+
308
+ if(value > (uint64_t)max){
309
+
310
+ rb_raise(cError, "Input exceeds allowable range of type");
311
+ }
312
+
313
+ retval = rb_str_buf_cat(self, (const char *)out, BLINK_putVLC(value, false, out, sizeof(out)));
314
+ }
315
+ }
316
+
317
+ return retval;
318
+ }
319
+
320
+ static VALUE getU8(VALUE self)
321
+ {
322
+ return getInt(self, 0, MAXU8, false);
323
+ }
324
+
325
+ static VALUE getU16(VALUE self)
326
+ {
327
+ return getInt(self, 0, MAXU16, false);
328
+ }
329
+
330
+ static VALUE getU32(VALUE self)
331
+ {
332
+ return getInt(self, 0, MAXU32, false);
333
+ }
334
+
335
+ static VALUE getU64(VALUE self)
336
+ {
337
+ return getInt(self, 0, MAXU64, false);
338
+ }
339
+
340
+ static VALUE getI8(VALUE self)
341
+ {
342
+ return getInt(self, MIN8, MAX8, true);
343
+ }
344
+
345
+ static VALUE getI16(VALUE self)
346
+ {
347
+ return getInt(self, MIN16, MAX16, true);
348
+ }
349
+
350
+ static VALUE getI32(VALUE self)
351
+ {
352
+ return getInt(self, MIN32, MAX32, true);
353
+ }
354
+
355
+ static VALUE getI64(VALUE self)
356
+ {
357
+ return getInt(self, MIN64, MAX64, true);
358
+ }
359
+
360
+ static VALUE getF64(VALUE self)
361
+ {
362
+ bool isNull;
363
+ double out;
364
+ uint32_t ret;
365
+ VALUE retval = Qnil;
366
+
367
+ ret = BLINK_getVLC((const uint8_t *)RSTRING_PTR(self), RSTRING_LEN(self), false, (uint64_t *)&out, &isNull);
368
+
369
+ if(ret > 0){
370
+
371
+ rb_str_drop_bytes(self, ret);
372
+ if(!isNull){
373
+
374
+ retval = rb_float_new(out);
375
+ }
376
+ }
377
+ else{
378
+
379
+ rb_raise(cError, "S1: Group encoding ends prematurely");
380
+ }
381
+
382
+ return retval;
383
+ }
384
+
385
+ static VALUE getPresent(VALUE self)
386
+ {
387
+ VALUE retval = getInt(self, 0, 1, false);
388
+
389
+ if(retval != Qnil){
390
+
391
+ retval = Qtrue;
392
+ }
393
+
394
+ return retval;
395
+ }
396
+
397
+ static VALUE getBool(VALUE self)
398
+ {
399
+ VALUE retval;
400
+ VALUE value = getInt(self, 0, MAXU8, false);
401
+
402
+ if(value == UINT2NUM(0)){
403
+
404
+ retval = Qfalse;
405
+ }
406
+ else if(value == UINT2NUM(1)){
407
+
408
+ retval = Qtrue;
409
+ }
410
+ else{
411
+
412
+ rb_raise(cError, "W11: Decoded value is not 0x00 or 0x01");
413
+ }
414
+
415
+ return retval;
416
+ }
417
+
418
+ static VALUE getBinary(VALUE self)
419
+ {
420
+ VALUE retval = Qnil;
421
+ VALUE size = getInt(self, 0, MAXU32, false);
422
+
423
+ if(size != Qnil){
424
+
425
+ if(NUM2UINT(size) > RSTRING_LEN(self)){
426
+ rb_raise(cError, "S1: Group encoding ends prematurely");
427
+ }
428
+
429
+ retval = rb_str_substr(self, 0, NUM2UINT(size));
430
+ rb_str_drop_bytes(self, NUM2UINT(size));
431
+ }
432
+
433
+ return retval;
434
+ }
435
+
436
+ static VALUE getString(VALUE self)
437
+ {
438
+ VALUE retval = getBinary(self);
439
+
440
+ if(retval != Qnil){
441
+
442
+ //todo: test UTF8 encoding here
443
+ }
444
+
445
+ return retval;
446
+ }
447
+
448
+ static VALUE getFixed(VALUE self, VALUE size)
449
+ {
450
+ VALUE retval;
451
+
452
+ if(NUM2UINT(size) > RSTRING_LEN(self)){
453
+
454
+ rb_raise(cError, "S1: Group encoding ends prematurely");
455
+ }
456
+
457
+ retval = rb_str_substr(self, 0, NUM2UINT(size));
458
+ rb_str_drop_bytes(self, NUM2UINT(size));
459
+
460
+ return retval;
461
+ }
462
+
463
+ static VALUE getFixedOptional(VALUE self, VALUE size)
464
+ {
465
+ VALUE retval = Qnil;
466
+ uint32_t ret;
467
+ bool isNull;
468
+ uint64_t present;
469
+
470
+ ret = BLINK_getVLC((const uint8_t *)RSTRING_PTR(self), RSTRING_LEN(self), false, (uint64_t *)&present, &isNull);
471
+
472
+ if(ret > 0){
473
+
474
+ rb_str_drop_bytes(self, ret);
475
+
476
+ if(isNull){
477
+
478
+ retval = Qnil;
479
+ }
480
+ else if(present == 0x01){
481
+
482
+ retval = rb_str_substr(self, 0, NUM2UINT(size));
483
+
484
+ if(RSTRING_LEN(retval) != NUM2UINT(size)){
485
+
486
+ rb_raise(cError, "S1: Group encoding ends prematurely");
487
+ }
488
+
489
+ rb_str_drop_bytes(self, NUM2UINT(size));
490
+ }
491
+ else{
492
+
493
+ rb_raise(cError, "W9: Presence flag is not 0xC0 or 0x01");
494
+ }
495
+ }
496
+ else{
497
+
498
+ rb_raise(cError, "S1: Group encoding ends prematurely");
499
+ }
500
+
501
+ return retval;
502
+ }
503
+
504
+ static VALUE getInt(VALUE input, int64_t min, int64_t max, bool isSigned)
505
+ {
506
+ bool isNull;
507
+ uint64_t out;
508
+ VALUE retval = Qnil;
509
+ uint32_t ret;
510
+
511
+ ret = BLINK_getVLC((const uint8_t *)RSTRING_PTR(input), RSTRING_LEN(input), isSigned, &out, &isNull);
512
+
513
+ if(ret > 0){
514
+
515
+ rb_str_drop_bytes(input, ret);
516
+
517
+ if(!isNull){
518
+
519
+ if(isSigned){
520
+
521
+ if(((int64_t)out < min) || ((int64_t)out > max)){
522
+
523
+ rb_raise(cError, "W3: Decoded value overflows range");
524
+ }
525
+
526
+ if(BLINK_getSizeSigned((int64_t)out) != ret){
527
+
528
+ rb_raise(cError, "W4: VLC entity contains more bytes than needed to express full width of type");
529
+ }
530
+
531
+ retval = LL2NUM((int64_t)out);
532
+ }
533
+ else{
534
+
535
+ if(out > (uint64_t)max){
536
+
537
+ rb_raise(cError, "W3: Decoded value overflows range");
538
+ }
539
+
540
+ if(BLINK_getSizeUnsigned(out) != ret){
541
+
542
+ rb_raise(cError, "W4: VLC entity contains more bytes than needed to express full width of type");
543
+ }
544
+
545
+ retval = ULL2NUM(out);
546
+ }
547
+ }
548
+ }
549
+ else{
550
+
551
+ rb_raise(cError, "S1: Group encoding ends prematurely");
552
+ }
553
+
554
+ return retval;
555
+ }
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+ $CFLAGS += " -std=c99"
3
+ create_makefile('slow_blink/ext_compact_encoder')
4
+