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.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +74 -0
- data/Rakefile +18 -0
- data/ext/trilogy-ruby/cast.c +272 -0
- data/ext/trilogy-ruby/cext.c +933 -0
- data/ext/trilogy-ruby/extconf.rb +16 -0
- data/ext/trilogy-ruby/inc/trilogy/blocking.h +163 -0
- data/ext/trilogy-ruby/inc/trilogy/buffer.h +64 -0
- data/ext/trilogy-ruby/inc/trilogy/builder.h +161 -0
- data/ext/trilogy-ruby/inc/trilogy/charset.h +277 -0
- data/ext/trilogy-ruby/inc/trilogy/client.h +546 -0
- data/ext/trilogy-ruby/inc/trilogy/error.h +43 -0
- data/ext/trilogy-ruby/inc/trilogy/packet_parser.h +34 -0
- data/ext/trilogy-ruby/inc/trilogy/protocol.h +756 -0
- data/ext/trilogy-ruby/inc/trilogy/reader.h +212 -0
- data/ext/trilogy-ruby/inc/trilogy/socket.h +111 -0
- data/ext/trilogy-ruby/inc/trilogy/vendor/curl_hostcheck.h +29 -0
- data/ext/trilogy-ruby/inc/trilogy/vendor/openssl_hostname_validation.h +51 -0
- data/ext/trilogy-ruby/inc/trilogy.h +8 -0
- data/ext/trilogy-ruby/src/blocking.c +241 -0
- data/ext/trilogy-ruby/src/buffer.c +60 -0
- data/ext/trilogy-ruby/src/builder.c +198 -0
- data/ext/trilogy-ruby/src/charset.c +212 -0
- data/ext/trilogy-ruby/src/client.c +728 -0
- data/ext/trilogy-ruby/src/error.c +17 -0
- data/ext/trilogy-ruby/src/packet_parser.c +140 -0
- data/ext/trilogy-ruby/src/protocol.c +676 -0
- data/ext/trilogy-ruby/src/reader.c +244 -0
- data/ext/trilogy-ruby/src/socket.c +623 -0
- data/ext/trilogy-ruby/src/vendor/curl_hostcheck.c +206 -0
- data/ext/trilogy-ruby/src/vendor/openssl_hostname_validation.c +175 -0
- data/ext/trilogy-ruby/trilogy-ruby.h +36 -0
- data/lib/trilogy/version.rb +3 -0
- data/lib/trilogy.rb +61 -0
- data/trilogy.gemspec +27 -0
- metadata +106 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
#include <stddef.h>
|
2
|
+
|
3
|
+
#include "trilogy/error.h"
|
4
|
+
|
5
|
+
const char *trilogy_error(int error)
|
6
|
+
{
|
7
|
+
switch (error) {
|
8
|
+
#define XX(name, code) \
|
9
|
+
case code: \
|
10
|
+
return #name;
|
11
|
+
TRILOGY_ERROR_CODES(XX)
|
12
|
+
#undef XX
|
13
|
+
|
14
|
+
default:
|
15
|
+
return NULL;
|
16
|
+
}
|
17
|
+
}
|
@@ -0,0 +1,140 @@
|
|
1
|
+
#include "trilogy/packet_parser.h"
|
2
|
+
#include "trilogy/error.h"
|
3
|
+
|
4
|
+
enum {
|
5
|
+
S_LEN_0 = 0,
|
6
|
+
S_LEN_1 = 1,
|
7
|
+
S_LEN_2 = 2,
|
8
|
+
S_SEQ = 3,
|
9
|
+
S_PAYLOAD = 4,
|
10
|
+
};
|
11
|
+
|
12
|
+
void trilogy_packet_parser_init(trilogy_packet_parser_t *parser, const trilogy_packet_parser_callbacks_t *callbacks)
|
13
|
+
{
|
14
|
+
parser->user_data = NULL;
|
15
|
+
parser->callbacks = callbacks;
|
16
|
+
parser->state = S_LEN_0;
|
17
|
+
parser->fragment = 0;
|
18
|
+
parser->deferred_end_callback = 0;
|
19
|
+
parser->sequence_number = 0;
|
20
|
+
}
|
21
|
+
|
22
|
+
size_t trilogy_packet_parser_execute(trilogy_packet_parser_t *parser, const uint8_t *buff, size_t len, int *error)
|
23
|
+
{
|
24
|
+
size_t i = 0;
|
25
|
+
|
26
|
+
if (parser->deferred_end_callback) {
|
27
|
+
parser->deferred_end_callback = 0;
|
28
|
+
|
29
|
+
int rc = parser->callbacks->on_packet_end(parser->user_data);
|
30
|
+
|
31
|
+
if (rc) {
|
32
|
+
*error = rc;
|
33
|
+
return 0;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
while (i < len) {
|
38
|
+
uint8_t cur_byte = buff[i];
|
39
|
+
|
40
|
+
switch (parser->state) {
|
41
|
+
case S_LEN_0: {
|
42
|
+
parser->bytes_remaining = cur_byte;
|
43
|
+
parser->state = S_LEN_1;
|
44
|
+
|
45
|
+
i++;
|
46
|
+
break;
|
47
|
+
}
|
48
|
+
case S_LEN_1: {
|
49
|
+
parser->bytes_remaining |= cur_byte << 8;
|
50
|
+
parser->state = S_LEN_2;
|
51
|
+
|
52
|
+
i++;
|
53
|
+
break;
|
54
|
+
}
|
55
|
+
case S_LEN_2: {
|
56
|
+
parser->bytes_remaining |= cur_byte << 16;
|
57
|
+
|
58
|
+
int was_fragment = parser->fragment;
|
59
|
+
|
60
|
+
parser->fragment = (parser->bytes_remaining == TRILOGY_MAX_PACKET_LEN);
|
61
|
+
|
62
|
+
parser->state = S_SEQ;
|
63
|
+
i++;
|
64
|
+
|
65
|
+
if (!was_fragment) {
|
66
|
+
int rc = parser->callbacks->on_packet_begin(parser->user_data);
|
67
|
+
|
68
|
+
if (rc) {
|
69
|
+
*error = rc;
|
70
|
+
return i;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
break;
|
75
|
+
}
|
76
|
+
case S_SEQ: {
|
77
|
+
if (cur_byte != parser->sequence_number) {
|
78
|
+
*error = TRILOGY_INVALID_SEQUENCE_ID;
|
79
|
+
return i;
|
80
|
+
}
|
81
|
+
|
82
|
+
parser->sequence_number++;
|
83
|
+
parser->state = S_PAYLOAD;
|
84
|
+
|
85
|
+
i++;
|
86
|
+
|
87
|
+
if (parser->bytes_remaining == 0) {
|
88
|
+
goto end_of_payload;
|
89
|
+
}
|
90
|
+
|
91
|
+
break;
|
92
|
+
}
|
93
|
+
case S_PAYLOAD: {
|
94
|
+
const uint8_t *ptr = buff + i;
|
95
|
+
size_t chunk_length = len - i;
|
96
|
+
|
97
|
+
if (chunk_length > parser->bytes_remaining) {
|
98
|
+
chunk_length = parser->bytes_remaining;
|
99
|
+
}
|
100
|
+
|
101
|
+
i += chunk_length;
|
102
|
+
parser->bytes_remaining -= chunk_length;
|
103
|
+
|
104
|
+
int rc = parser->callbacks->on_packet_data(parser->user_data, ptr, chunk_length);
|
105
|
+
|
106
|
+
if (rc) {
|
107
|
+
if (parser->bytes_remaining == 0) {
|
108
|
+
parser->deferred_end_callback = 1;
|
109
|
+
}
|
110
|
+
|
111
|
+
*error = rc;
|
112
|
+
return i;
|
113
|
+
}
|
114
|
+
|
115
|
+
if (parser->bytes_remaining == 0) {
|
116
|
+
goto end_of_payload;
|
117
|
+
}
|
118
|
+
|
119
|
+
break;
|
120
|
+
}
|
121
|
+
end_of_payload : {
|
122
|
+
parser->state = S_LEN_0;
|
123
|
+
|
124
|
+
if (!parser->fragment) {
|
125
|
+
int rc = parser->callbacks->on_packet_end(parser->user_data);
|
126
|
+
|
127
|
+
if (rc) {
|
128
|
+
*error = rc;
|
129
|
+
return i;
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
break;
|
134
|
+
}
|
135
|
+
}
|
136
|
+
}
|
137
|
+
|
138
|
+
*error = 0;
|
139
|
+
return i;
|
140
|
+
}
|