iodine 0.4.14 → 0.4.15

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of iodine might be problematic. Click here for more details.

Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -33
  3. data/README.md +7 -5
  4. data/ext/iodine/base64.c +12 -0
  5. data/ext/iodine/defer.c +211 -108
  6. data/ext/iodine/defer.h +7 -0
  7. data/ext/iodine/facil.c +5 -1
  8. data/ext/iodine/facil.h +1 -1
  9. data/ext/iodine/fio2resp.c +19 -30
  10. data/ext/iodine/fio2resp.h +2 -1
  11. data/ext/iodine/fio_cli_helper.c +2 -2
  12. data/ext/iodine/fiobj.h +11 -624
  13. data/ext/iodine/fiobj_ary.c +65 -26
  14. data/ext/iodine/fiobj_ary.h +106 -0
  15. data/ext/iodine/fiobj_hash.c +175 -115
  16. data/ext/iodine/fiobj_hash.h +128 -0
  17. data/ext/iodine/fiobj_internal.c +189 -0
  18. data/ext/iodine/{fiobj_types.h → fiobj_internal.h} +126 -136
  19. data/ext/iodine/fiobj_json.c +161 -207
  20. data/ext/iodine/fiobj_json.h +43 -0
  21. data/ext/iodine/fiobj_numbers.c +53 -35
  22. data/ext/iodine/fiobj_numbers.h +49 -0
  23. data/ext/iodine/fiobj_primitives.c +103 -70
  24. data/ext/iodine/fiobj_primitives.h +55 -0
  25. data/ext/iodine/fiobj_str.c +171 -59
  26. data/ext/iodine/fiobj_str.h +113 -0
  27. data/ext/iodine/fiobj_sym.c +46 -124
  28. data/ext/iodine/fiobj_sym.h +60 -0
  29. data/ext/iodine/fiobject.c +589 -0
  30. data/ext/iodine/fiobject.h +276 -0
  31. data/ext/iodine/pubsub.h +1 -1
  32. data/ext/iodine/resp.c +2 -2
  33. data/ext/iodine/siphash.c +11 -0
  34. data/ext/iodine/spnlock.inc +7 -5
  35. data/ext/iodine/websocket_parser.h +44 -7
  36. data/lib/iodine/version.rb +1 -1
  37. metadata +13 -8
  38. data/ext/iodine/fiobj_alloc.c +0 -81
  39. data/ext/iodine/fiobj_generic.c +0 -260
  40. data/ext/iodine/fiobj_io.c +0 -58
  41. data/ext/iodine/fiobj_misc.c +0 -213
  42. data/ext/iodine/fiobj_tests.c +0 -474
@@ -0,0 +1,276 @@
1
+ #ifndef H_FIOBJECT_H
2
+ /*
3
+ Copyright: Boaz Segev, 2017
4
+ License: MIT
5
+ */
6
+
7
+ /**
8
+ This facil.io core library provides wrappers around complex and (or) dynamic
9
+ types, abstracting some complexity and making dynamic type related tasks easier.
10
+
11
+
12
+ The library offers a rudementry protection against cyclic references using the
13
+ `FIOBJ_NESTING_PROTECTION` flag (i.e., nesting an Array within itself)...
14
+ however, this isn't fully tested and the performance price is high.
15
+ */
16
+ #define H_FIOBJECT_H
17
+
18
+ #include <stdarg.h>
19
+ #include <stdint.h>
20
+ #include <stdio.h>
21
+ #include <stdlib.h>
22
+
23
+ #ifdef __cplusplus
24
+ extern "C" {
25
+ #endif
26
+
27
+ /* *****************************************************************************
28
+ Core Types
29
+ ***************************************************************************** */
30
+
31
+ /** The dynamic facil.io object type. */
32
+ typedef struct { uintptr_t type; } fiobj_s;
33
+ typedef fiobj_s *fiobj_pt;
34
+
35
+ /** A string information type, reports anformation about a C string. */
36
+ typedef struct {
37
+ union {
38
+ uint64_t len;
39
+ uint64_t length;
40
+ };
41
+ union {
42
+ void *buffer;
43
+ uint8_t *bytes;
44
+ char *data;
45
+ char *value;
46
+ char *name;
47
+ };
48
+ } fio_cstr_s;
49
+
50
+ /**
51
+ * Sets the default state for nesting protection.
52
+ *
53
+ * NOTICE: facil.io's default makefile will disables nesting protection.
54
+ *
55
+ * This effects traversing functions, such as `fiobj_each2`, `fiobj_dup`,
56
+ * `fiobj_free` etc'.
57
+ */
58
+ #ifndef FIOBJ_NESTING_PROTECTION
59
+ #define FIOBJ_NESTING_PROTECTION 0
60
+ #endif
61
+
62
+ /* *****************************************************************************
63
+ Generic Object API
64
+ ***************************************************************************** */
65
+
66
+ /** Returns a C string naming the objects dynamic type. */
67
+ const char *fiobj_type_name(const fiobj_s *obj);
68
+
69
+ /**
70
+ * Copy by reference(!) - increases an object's (and any nested object's)
71
+ * reference count.
72
+ *
73
+ * Always returns the value passed along.
74
+ *
75
+ * Future implementations might provide `fiobj_dup2` providing a deep copy.
76
+ *
77
+ * We don't need this feature just yet, so I'm not working on it.
78
+ */
79
+ fiobj_s *fiobj_dup(fiobj_s *);
80
+
81
+ /**
82
+ * Decreases an object's reference count, releasing memory and
83
+ * resources.
84
+ *
85
+ * This function affects nested objects, meaning that when an Array or
86
+ * a Hash object is passed along, it's children (nested objects) are
87
+ * also freed.
88
+ */
89
+ void fiobj_free(fiobj_s *);
90
+
91
+ /**
92
+ * Attempts to return the object's current reference count.
93
+ *
94
+ * This is mostly for testing rather than normal library operations.
95
+ */
96
+ uintptr_t fiobj_reference_count(const fiobj_s *);
97
+
98
+ /**
99
+ * Tests if an object evaluates as TRUE.
100
+ *
101
+ * This is object type specific. For example, empty strings might evaluate as
102
+ * FALSE, even though they aren't a boolean type.
103
+ */
104
+ int fiobj_is_true(const fiobj_s *);
105
+
106
+ /**
107
+ * Returns an Object's numerical value.
108
+ *
109
+ * If a String or Symbol are passed to the function, they will be
110
+ * parsed assuming base 10 numerical data.
111
+ *
112
+ * Hashes and Arrays return their object count.
113
+ *
114
+ * IO and File objects return their underlying file descriptor.
115
+ *
116
+ * A type error results in 0.
117
+ */
118
+ int64_t fiobj_obj2num(const fiobj_s *obj);
119
+
120
+ /**
121
+ * Returns a Float's value.
122
+ *
123
+ * If a String or Symbol are passed to the function, they will be
124
+ * parsed assuming base 10 numerical data.
125
+ *
126
+ * Hashes and Arrays return their object count.
127
+ *
128
+ * IO and File objects return their underlying file descriptor.
129
+ *
130
+ * A type error results in 0.
131
+ */
132
+ double fiobj_obj2float(const fiobj_s *obj);
133
+
134
+ /**
135
+ * Returns a C String (NUL terminated) using the `fio_cstr_s` data type.
136
+ *
137
+ * The Sting in binary safe and might contain NUL bytes in the middle as well as
138
+ * a terminating NUL.
139
+ *
140
+ * If a Symbol, a Number or a Float are passed to the function, they
141
+ * will be parsed as a *temporary*, thread-safe, String.
142
+ *
143
+ * Numbers will be represented in base 10 numerical data.
144
+ *
145
+ * A type error results in NULL (i.e. object isn't a String).
146
+ */
147
+ fio_cstr_s fiobj_obj2cstr(const fiobj_s *obj);
148
+
149
+ /**
150
+ * Single layer iteration using a callback for each nested fio object.
151
+ *
152
+ * Accepts any `fiobj_s *` type but only collections (Arrays and Hashes) are
153
+ * processed. The container itself (the Array or the Hash) is **not** processed
154
+ * (unlike `fiobj_each2`).
155
+ *
156
+ * The callback task function must accept an object and an opaque user pointer.
157
+ *
158
+ * Hash objects pass along a `FIOBJ_T_COUPLET` object, containing
159
+ * references for both the key (Symbol) and the object (any object).
160
+ *
161
+ * If the callback returns -1, the loop is broken. Any other value is ignored.
162
+ *
163
+ * Returns the "stop" position, i.e., the number of items processed + the
164
+ * starting point.
165
+ */
166
+ size_t fiobj_each1(fiobj_s *, size_t start_at,
167
+ int (*task)(fiobj_s *obj, void *arg), void *arg);
168
+
169
+ /**
170
+ * Deep iteration using a callback for each fio object, including the parent.
171
+ *
172
+ * Accepts any `fiobj_s *` type.
173
+ *
174
+ * Collections (Arrays, Hashes) are deeply probed and shouldn't be edited
175
+ * during an `fiobj_each2` call (or weird things may happen).
176
+ *
177
+ * The callback task function must accept an object and an opaque user pointer.
178
+ *
179
+ * If `FIOBJ_NESTING_PROTECTION` is equal to 1 and a cyclic (or recursive)
180
+ * nesting is detected, a NULL pointer (not a NULL object) will be used instead
181
+ * of the original (cyclic) object and the original (cyclic) object will be
182
+ * available using the `fiobj_each_get_cyclic` function.
183
+ *
184
+ * Hash objects pass along a `FIOBJ_T_COUPLET` object, containing
185
+ * references for both the key (Symbol) and the object (any object).
186
+ *
187
+ * Notice that when passing collections to the function, the collection itself
188
+ * is sent to the callback followed by it's children (if any). This is true also
189
+ * for nested collections (a nested Hash will be sent first, followed by the
190
+ * nested Hash's children and then followed by the rest of it's siblings.
191
+ *
192
+ * If the callback returns -1, the loop is broken. Any other value is ignored.
193
+ */
194
+ void fiobj_each2(fiobj_s *, int (*task)(fiobj_s *obj, void *arg), void *arg);
195
+
196
+ /** Within `fiobj_each2`, this will return the current cyclic object, if any. */
197
+ fiobj_s *fiobj_each_get_cyclic(void);
198
+
199
+ /**
200
+ * Deeply compare two objects. No hashing or recursive functio n calls are
201
+ * involved.
202
+ *
203
+ * Uses a similar algorithm to `fiobj_each2`, except adjusted to two objects.
204
+ *
205
+ * Hash order will be tested when comapring Hashes.
206
+ *
207
+ * KNOWN ISSUES:
208
+ *
209
+ * * Cyclic nesting will cause this function to hang (much like `fiobj_each2`).
210
+ *
211
+ * If `FIOBJ_NESTING_PROTECTION` is set, then cyclic nesting might produce
212
+ * false positives.
213
+ *
214
+ * * Hash order will be tested as well as the Hash content, which means that
215
+ * equal Hashes might be considered unequal if their order doesn't match.
216
+ *
217
+ */
218
+ int fiobj_iseq(const fiobj_s *obj1, const fiobj_s *obj2);
219
+
220
+ /* *****************************************************************************
221
+ Helpers: not fiobj_s specific, but since they're used internally, they're here.
222
+ ***************************************************************************** */
223
+
224
+ /**
225
+ * A helper function that converts between String data to a signed int64_t.
226
+ *
227
+ * Numbers are assumed to be in base 10.
228
+ *
229
+ * The `0x##` (or `x##`) and `0b##` (or `b##`) are recognized as base 16 and
230
+ * base 2 (binary MSB first) respectively.
231
+ *
232
+ * The pointer will be updated to point to the first byte after the number.
233
+ */
234
+ int64_t fio_atol(char **pstr);
235
+
236
+ /** A helper function that convers between String data to a signed double. */
237
+ double fio_atof(char **pstr);
238
+
239
+ /**
240
+ * A helper function that convers between a signed int64_t to a string.
241
+ *
242
+ * No overflow guard is provided, make sure there's at least 66 bytes available
243
+ * (for base 2).
244
+ *
245
+ * Supports base 2, base 10 and base 16. An unsupported base will silently
246
+ * default to base 10. Prefixes aren't added (i.e., no "0x" or "0b" at the
247
+ * beginning of the string).
248
+ *
249
+ * Returns the number of bytes actually written (excluding the NUL terminator).
250
+ */
251
+ size_t fio_ltoa(char *dest, int64_t num, uint8_t base);
252
+
253
+ /**
254
+ * A helper function that convers between a double to a string.
255
+ *
256
+ * No overflow guard is provided, make sure there's at least 130 bytes available
257
+ * (for base 2).
258
+ *
259
+ * Supports base 2, base 10 and base 16. An unsupported base will silently
260
+ * default to base 10. Prefixes aren't added (i.e., no "0x" or "0b" at the
261
+ * beginning of the string).
262
+ *
263
+ * Returns the number of bytes actually written (excluding the NUL terminator).
264
+ */
265
+ size_t fio_ftoa(char *dest, double num, uint8_t base);
266
+
267
+ #ifdef DEBUG
268
+ void fiobj_test(void);
269
+ int fiobj_test_json_str(char const *json, size_t len, uint8_t print_result);
270
+ #endif
271
+
272
+ #ifdef __cplusplus
273
+ } /* closing brace for extern "C" */
274
+ #endif
275
+
276
+ #endif
data/ext/iodine/pubsub.h CHANGED
@@ -146,7 +146,7 @@ int pubsub_publish(struct pubsub_publish_args);
146
146
  *
147
147
  * This should only be called from within the `on_message` callback.
148
148
  *
149
- * It's recommended that the `on_message` callback return immediately followin
149
+ * It's recommended that the `on_message` callback return immediately following
150
150
  * this function call, as code might run concurrently.
151
151
  *
152
152
  * Uses reference counting for zero copy.
data/ext/iodine/resp.c CHANGED
@@ -751,7 +751,7 @@ void resp_test(void) {
751
751
  resp_free_object(obj);
752
752
  } else
753
753
  fprintf(stderr, "* ERR / Simple String FAILED (type %d)\n",
754
- obj ? obj->type : -1);
754
+ obj ? (int)obj->type : -1);
755
755
 
756
756
  len = sizeof(b_num) - 1;
757
757
  obj = resp_parser_feed(parser, b_num, &len);
@@ -820,7 +820,7 @@ void resp_test(void) {
820
820
  }
821
821
  resp_free_object(obj);
822
822
  } else {
823
- fprintf(stderr, "* Array FAILED (type == %d)\n", obj ? obj->type : -1);
823
+ fprintf(stderr, "* Array FAILED (type == %d)\n", obj ? (int)obj->type : -1);
824
824
  }
825
825
  {
826
826
  // uint8_t buff[128] = {0};
data/ext/iodine/siphash.c CHANGED
@@ -130,6 +130,8 @@ uint64_t siphash24(const void *data, size_t len, uint64_t iv_key[2]) {
130
130
  #if defined(DEBUG) && DEBUG == 1
131
131
 
132
132
  #include <stdio.h>
133
+ #include <time.h>
134
+
133
135
  void bscrypt_test_siphash(void) {
134
136
  uint64_t result =
135
137
  siphash24("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e",
@@ -137,6 +139,15 @@ void bscrypt_test_siphash(void) {
137
139
  fprintf(stderr, "===================================\n");
138
140
  fprintf(stderr, "SipHash simple test %s\n",
139
141
  (result == 0xa129ca6149be45e5ULL) ? "passed" : "FAILED");
142
+ clock_t start;
143
+ start = clock();
144
+ for (size_t i = 0; i < 100000; i++) {
145
+ __asm__ volatile("" ::: "memory");
146
+ result = siphash24("The quick brown fox jumps over the lazy dog ", 43,
147
+ SIPHASH_DEFAULT_KEY);
148
+ }
149
+ fprintf(stderr, "bscrypt 100K SipHash: %lf\n",
150
+ (double)(clock() - start) / CLOCKS_PER_SEC);
140
151
  fprintf(stderr, "===================================\n");
141
152
  }
142
153
 
@@ -38,20 +38,22 @@ spinlock / sync for tasks
38
38
 
39
39
  /** locks use a single byte */
40
40
  typedef volatile unsigned char spn_lock_i;
41
+
41
42
  /** The initail value of an unlocked spinlock. */
42
43
  #define SPN_LOCK_INIT 0
43
44
 
44
- /* Select the correct compiler builtin method. */
45
- #if defined(__has_builtin)
46
-
47
- #if __has_builtin(__atomic_exchange_n)
45
+ /* C11 Atomics are defined? */
46
+ #if defined(__ATOMIC_RELAXED)
48
47
  #define SPN_LOCK_BUILTIN(...) __atomic_exchange_n(__VA_ARGS__, __ATOMIC_ACQ_REL)
49
48
  /** An atomic addition operation */
50
49
  #define spn_add(...) __atomic_add_fetch(__VA_ARGS__, __ATOMIC_ACQ_REL)
51
50
  /** An atomic subtraction operation */
52
51
  #define spn_sub(...) __atomic_sub_fetch(__VA_ARGS__, __ATOMIC_ACQ_REL)
53
52
 
54
- #elif __has_builtin(__sync_fetch_and_or)
53
+ /* Select the correct compiler builtin method. */
54
+ #elif defined(__has_builtin)
55
+
56
+ #if __has_builtin(__sync_fetch_and_or)
55
57
  #define SPN_LOCK_BUILTIN(...) __sync_fetch_and_or(__VA_ARGS__)
56
58
  /** An atomic addition operation */
57
59
  #define spn_add(...) __sync_add_and_fetch(__VA_ARGS__)
@@ -142,12 +142,47 @@ Message masking
142
142
  ***************************************************************************** */
143
143
  /** used internally to mask and unmask client messages. */
144
144
  void websocket_xmask(void *msg, uint64_t len, uint32_t mask) {
145
- const uint64_t xmask = (((uint64_t)mask) << 32) | mask;
146
- while (len >= 8) {
147
- *((uint64_t *)msg) ^= xmask;
148
- len -= 8;
149
- msg = (void *)((uintptr_t)msg + 8);
145
+ if (len > 7) {
146
+ /* XOR any unaligned memory (4 byte alignment) */
147
+ const uintptr_t offset = 4 - ((uintptr_t)msg & 3);
148
+ switch (offset) {
149
+ case 3:
150
+ ((uint8_t *)msg)[2] ^= ((uint8_t *)(&mask))[2];
151
+ /* fallthrough */
152
+ case 2:
153
+ ((uint8_t *)msg)[1] ^= ((uint8_t *)(&mask))[1];
154
+ /* fallthrough */
155
+ case 1:
156
+ ((uint8_t *)msg)[0] ^= ((uint8_t *)(&mask))[0];
157
+ /* rotate mask and move pointer to first 4 byte alignment */
158
+ mask = (mask << (offset << 3)) | (mask >> ((4 - offset) << 3));
159
+ msg = (void *)((uintptr_t)msg + offset);
160
+ len -= offset;
161
+ }
162
+ #if UINTPTR_MAX <= 0xFFFFFFFF
163
+ /* handle 4 byte XOR alignment in 32 bit mnachine*/
164
+ while (len >= 4) {
165
+ *((uint32_t *)msg) ^= mask;
166
+ len -= 4;
167
+ msg = (void *)((uintptr_t)msg + 4);
168
+ }
169
+ #else
170
+ /* handle first 4 byte XOR alignment and move on to 64 bits */
171
+ if ((uintptr_t)msg & 7) {
172
+ *((uint32_t *)msg) ^= mask;
173
+ len -= 4;
174
+ msg = (void *)((uintptr_t)msg + 4);
175
+ }
176
+ /* intrinsic / XOR by 8 byte block, memory aligned */
177
+ const uint64_t xmask = (((uint64_t)mask) << 32) | mask;
178
+ while (len >= 8) {
179
+ *((uint64_t *)msg) ^= xmask;
180
+ len -= 8;
181
+ msg = (void *)((uintptr_t)msg + 8);
182
+ }
183
+ #endif
150
184
  }
185
+ /* XOR any leftover bytes (might be non aligned) */
151
186
  switch (len) {
152
187
  case 7:
153
188
  ((uint8_t *)msg)[6] ^= ((uint8_t *)(&mask))[2];
@@ -335,7 +370,8 @@ static uint64_t websocket_server_wrap(void *target, void *msg, uint64_t len,
335
370
  * * first: set to 1 if `msg` points the begining of the message.
336
371
  * * last: set to 1 if `msg + len` ends the message.
337
372
  *
338
- * Returns the number of bytes written. Always `websocket_wrapped_len(len) + 4`
373
+ * Returns the number of bytes written. Always `websocket_wrapped_len(len) +
374
+ * 4`
339
375
  */
340
376
  static uint64_t websocket_client_wrap(void *target, void *msg, uint64_t len,
341
377
  unsigned char opcode, unsigned char first,
@@ -390,7 +426,8 @@ static uint64_t websocket_client_wrap(void *target, void *msg, uint64_t len,
390
426
 
391
427
  /* *****************************************************************************
392
428
  Message unwrapping
393
- ***************************************************************************** */
429
+ *****************************************************************************
430
+ */
394
431
 
395
432
  /**
396
433
  * Returns all known information regarding the upcoming message.
@@ -1,3 +1,3 @@
1
1
  module Iodine
2
- VERSION = '0.4.14'.freeze
2
+ VERSION = '0.4.15'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iodine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.14
4
+ version: 0.4.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boaz Segev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-27 00:00:00.000000000 Z
11
+ date: 2017-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -177,19 +177,24 @@ files:
177
177
  - ext/iodine/fio_hash_table.h
178
178
  - ext/iodine/fio_list.h
179
179
  - ext/iodine/fiobj.h
180
- - ext/iodine/fiobj_alloc.c
181
180
  - ext/iodine/fiobj_ary.c
182
- - ext/iodine/fiobj_generic.c
181
+ - ext/iodine/fiobj_ary.h
183
182
  - ext/iodine/fiobj_hash.c
184
- - ext/iodine/fiobj_io.c
183
+ - ext/iodine/fiobj_hash.h
184
+ - ext/iodine/fiobj_internal.c
185
+ - ext/iodine/fiobj_internal.h
185
186
  - ext/iodine/fiobj_json.c
186
- - ext/iodine/fiobj_misc.c
187
+ - ext/iodine/fiobj_json.h
187
188
  - ext/iodine/fiobj_numbers.c
189
+ - ext/iodine/fiobj_numbers.h
188
190
  - ext/iodine/fiobj_primitives.c
191
+ - ext/iodine/fiobj_primitives.h
189
192
  - ext/iodine/fiobj_str.c
193
+ - ext/iodine/fiobj_str.h
190
194
  - ext/iodine/fiobj_sym.c
191
- - ext/iodine/fiobj_tests.c
192
- - ext/iodine/fiobj_types.h
195
+ - ext/iodine/fiobj_sym.h
196
+ - ext/iodine/fiobject.c
197
+ - ext/iodine/fiobject.h
193
198
  - ext/iodine/hex.c
194
199
  - ext/iodine/hex.h
195
200
  - ext/iodine/http.c