trilogy 2.0.0

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

Potentially problematic release.


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

Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +74 -0
  4. data/Rakefile +18 -0
  5. data/ext/trilogy-ruby/cast.c +272 -0
  6. data/ext/trilogy-ruby/cext.c +933 -0
  7. data/ext/trilogy-ruby/extconf.rb +16 -0
  8. data/ext/trilogy-ruby/inc/trilogy/blocking.h +163 -0
  9. data/ext/trilogy-ruby/inc/trilogy/buffer.h +64 -0
  10. data/ext/trilogy-ruby/inc/trilogy/builder.h +161 -0
  11. data/ext/trilogy-ruby/inc/trilogy/charset.h +277 -0
  12. data/ext/trilogy-ruby/inc/trilogy/client.h +546 -0
  13. data/ext/trilogy-ruby/inc/trilogy/error.h +43 -0
  14. data/ext/trilogy-ruby/inc/trilogy/packet_parser.h +34 -0
  15. data/ext/trilogy-ruby/inc/trilogy/protocol.h +756 -0
  16. data/ext/trilogy-ruby/inc/trilogy/reader.h +212 -0
  17. data/ext/trilogy-ruby/inc/trilogy/socket.h +111 -0
  18. data/ext/trilogy-ruby/inc/trilogy/vendor/curl_hostcheck.h +29 -0
  19. data/ext/trilogy-ruby/inc/trilogy/vendor/openssl_hostname_validation.h +51 -0
  20. data/ext/trilogy-ruby/inc/trilogy.h +8 -0
  21. data/ext/trilogy-ruby/src/blocking.c +241 -0
  22. data/ext/trilogy-ruby/src/buffer.c +60 -0
  23. data/ext/trilogy-ruby/src/builder.c +198 -0
  24. data/ext/trilogy-ruby/src/charset.c +212 -0
  25. data/ext/trilogy-ruby/src/client.c +728 -0
  26. data/ext/trilogy-ruby/src/error.c +17 -0
  27. data/ext/trilogy-ruby/src/packet_parser.c +140 -0
  28. data/ext/trilogy-ruby/src/protocol.c +676 -0
  29. data/ext/trilogy-ruby/src/reader.c +244 -0
  30. data/ext/trilogy-ruby/src/socket.c +623 -0
  31. data/ext/trilogy-ruby/src/vendor/curl_hostcheck.c +206 -0
  32. data/ext/trilogy-ruby/src/vendor/openssl_hostname_validation.c +175 -0
  33. data/ext/trilogy-ruby/trilogy-ruby.h +36 -0
  34. data/lib/trilogy/version.rb +3 -0
  35. data/lib/trilogy.rb +61 -0
  36. data/trilogy.gemspec +27 -0
  37. metadata +106 -0
@@ -0,0 +1,244 @@
1
+ #include "trilogy/reader.h"
2
+ #include "trilogy/error.h"
3
+
4
+ #include <string.h>
5
+
6
+ #define CHECK(bytes) \
7
+ if ((bytes) > (reader->len - reader->pos)) { \
8
+ return TRILOGY_TRUNCATED_PACKET; \
9
+ }
10
+
11
+ void trilogy_reader_init(trilogy_reader_t *reader, const uint8_t *buff, size_t len)
12
+ {
13
+ reader->buff = buff;
14
+ reader->len = len;
15
+ reader->pos = 0;
16
+ }
17
+
18
+ static uint8_t next_uint8(trilogy_reader_t *reader) { return reader->buff[reader->pos++]; }
19
+
20
+ int trilogy_reader_get_uint8(trilogy_reader_t *reader, uint8_t *out)
21
+ {
22
+ CHECK(1);
23
+
24
+ uint8_t a = next_uint8(reader);
25
+
26
+ if (out) {
27
+ *out = a;
28
+ }
29
+
30
+ return TRILOGY_OK;
31
+ }
32
+
33
+ int trilogy_reader_get_uint16(trilogy_reader_t *reader, uint16_t *out)
34
+ {
35
+ CHECK(2);
36
+
37
+ uint16_t a = next_uint8(reader);
38
+ uint16_t b = next_uint8(reader);
39
+
40
+ if (out) {
41
+ *out = (uint16_t)(a | (b << 8));
42
+ }
43
+
44
+ return TRILOGY_OK;
45
+ }
46
+
47
+ int trilogy_reader_get_uint24(trilogy_reader_t *reader, uint32_t *out)
48
+ {
49
+ CHECK(3);
50
+
51
+ uint32_t a = next_uint8(reader);
52
+ uint32_t b = next_uint8(reader);
53
+ uint32_t c = next_uint8(reader);
54
+
55
+ if (out) {
56
+ *out = a | (b << 8) | (c << 16);
57
+ }
58
+
59
+ return TRILOGY_OK;
60
+ }
61
+
62
+ int trilogy_reader_get_uint32(trilogy_reader_t *reader, uint32_t *out)
63
+ {
64
+ CHECK(4);
65
+
66
+ uint32_t a = next_uint8(reader);
67
+ uint32_t b = next_uint8(reader);
68
+ uint32_t c = next_uint8(reader);
69
+ uint32_t d = next_uint8(reader);
70
+
71
+ if (out) {
72
+ *out = a | (b << 8) | (c << 16) | (d << 24);
73
+ }
74
+
75
+ return TRILOGY_OK;
76
+ }
77
+
78
+ int trilogy_reader_get_uint64(trilogy_reader_t *reader, uint64_t *out)
79
+ {
80
+ CHECK(8);
81
+
82
+ uint64_t a = next_uint8(reader);
83
+ uint64_t b = next_uint8(reader);
84
+ uint64_t c = next_uint8(reader);
85
+ uint64_t d = next_uint8(reader);
86
+ uint64_t e = next_uint8(reader);
87
+ uint64_t f = next_uint8(reader);
88
+ uint64_t g = next_uint8(reader);
89
+ uint64_t h = next_uint8(reader);
90
+
91
+ if (out) {
92
+ *out = a | (b << 8) | (c << 16) | (d << 24) | (e << 32) | (f << 40) | (g << 48) | (h << 56);
93
+ }
94
+
95
+ return TRILOGY_OK;
96
+ }
97
+
98
+ int trilogy_reader_get_lenenc(trilogy_reader_t *reader, uint64_t *out)
99
+ {
100
+ CHECK(1);
101
+
102
+ uint8_t leader = next_uint8(reader);
103
+
104
+ if (leader < 0xfb) {
105
+ if (out) {
106
+ *out = leader;
107
+ }
108
+
109
+ return TRILOGY_OK;
110
+ }
111
+
112
+ switch (leader) {
113
+ case 0xfb:
114
+ return TRILOGY_NULL_VALUE;
115
+
116
+ case 0xfc: {
117
+ uint16_t u16 = 0;
118
+ int rc = trilogy_reader_get_uint16(reader, &u16);
119
+
120
+ if (out) {
121
+ *out = u16;
122
+ }
123
+
124
+ return rc;
125
+ }
126
+
127
+ case 0xfd: {
128
+ uint32_t u24 = 0;
129
+ int rc = trilogy_reader_get_uint24(reader, &u24);
130
+
131
+ if (out) {
132
+ *out = u24;
133
+ }
134
+
135
+ return rc;
136
+ }
137
+
138
+ case 0xfe:
139
+ return trilogy_reader_get_uint64(reader, out);
140
+
141
+ default:
142
+ return TRILOGY_PROTOCOL_VIOLATION;
143
+ }
144
+ }
145
+
146
+ int trilogy_reader_get_buffer(trilogy_reader_t *reader, size_t len, const void **out)
147
+ {
148
+ CHECK(len);
149
+
150
+ if (out) {
151
+ *out = (const void *)(reader->buff + reader->pos);
152
+ }
153
+
154
+ reader->pos += len;
155
+
156
+ return TRILOGY_OK;
157
+ }
158
+
159
+ int trilogy_reader_copy_buffer(trilogy_reader_t *reader, size_t len, void *out)
160
+ {
161
+ CHECK(len);
162
+
163
+ if (out) {
164
+ memcpy(out, reader->buff + reader->pos, len);
165
+ }
166
+
167
+ reader->pos += len;
168
+
169
+ return TRILOGY_OK;
170
+ }
171
+
172
+ int trilogy_reader_get_lenenc_buffer(trilogy_reader_t *reader, size_t *out_len, const void **out)
173
+ {
174
+ uint64_t len;
175
+
176
+ int rc = trilogy_reader_get_lenenc(reader, &len);
177
+
178
+ if (rc) {
179
+ return rc;
180
+ }
181
+
182
+ // check len is not larger than the amount of bytes left before downcasting
183
+ // to size_t (which may be smaller than uint64_t on some architectures)
184
+ if (len > (uint64_t)(reader->len - reader->pos)) {
185
+ return TRILOGY_TRUNCATED_PACKET;
186
+ }
187
+
188
+ if (out_len) {
189
+ *out_len = (size_t)len;
190
+ }
191
+
192
+ return trilogy_reader_get_buffer(reader, (size_t)len, out);
193
+ }
194
+
195
+ int trilogy_reader_get_string(trilogy_reader_t *reader, const char **out, size_t *out_len)
196
+ {
197
+ const uint8_t *pos = reader->buff + reader->pos;
198
+
199
+ const uint8_t *end = memchr(pos, 0, reader->len - reader->pos);
200
+
201
+ if (!end) {
202
+ return TRILOGY_TRUNCATED_PACKET;
203
+ }
204
+
205
+ if (out) {
206
+ *out = (const char *)pos;
207
+ }
208
+
209
+ size_t len = (size_t)(end - pos);
210
+
211
+ if (out_len) {
212
+ *out_len = len;
213
+ }
214
+
215
+ reader->pos += len + 1;
216
+
217
+ return TRILOGY_OK;
218
+ }
219
+
220
+ int trilogy_reader_get_eof_buffer(trilogy_reader_t *reader, size_t *out_len, const void **out)
221
+ {
222
+ if (out_len) {
223
+ *out_len = reader->len - reader->pos;
224
+ }
225
+
226
+ if (out) {
227
+ *out = reader->buff + reader->pos;
228
+ }
229
+
230
+ reader->pos = reader->len;
231
+
232
+ return TRILOGY_OK;
233
+ }
234
+
235
+ bool trilogy_reader_eof(trilogy_reader_t *reader) { return !(reader->pos < reader->len); }
236
+
237
+ int trilogy_reader_finish(trilogy_reader_t *reader)
238
+ {
239
+ if (reader->pos < reader->len) {
240
+ return TRILOGY_EXTRA_DATA_IN_PACKET;
241
+ } else {
242
+ return TRILOGY_OK;
243
+ }
244
+ }