pbwire 0.1.17 → 0.1.18

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.
@@ -0,0 +1,255 @@
1
+ /* Well-known-type helpers over the dynamic API: google.protobuf.Any packing
2
+ * plus Timestamp/Duration millisecond conversion. Field numbers only (Any 1/2,
3
+ * seconds/nanos 1/2), so any descriptor with the same shape works; the core
4
+ * never special-cases WKTs anywhere else. */
5
+
6
+ #include "internal.h"
7
+
8
+ #include <stdint.h>
9
+
10
+ pbw_status
11
+ pbw_any_pack(const pbw_descriptor *any_desc, const char *type_url,
12
+ const void *payload, size_t len, pbw_message **out)
13
+ {
14
+ pbw_message *msg;
15
+ pbw_value *vurl;
16
+ pbw_value *vpay;
17
+ pbw_status status;
18
+
19
+ if (out == NULL) {
20
+ return PBW_ERR_INVALID;
21
+ }
22
+ *out = NULL;
23
+ if (any_desc == NULL || type_url == NULL || (payload == NULL && len > 0)) {
24
+ return PBW_ERR_INVALID;
25
+ }
26
+ msg = pbw_message_new(any_desc);
27
+ vurl = pbw_value_new_string(type_url);
28
+ vpay = pbw_value_new_bytes(payload, len);
29
+ if (msg == NULL || vurl == NULL || vpay == NULL) {
30
+ pbw_message_free(msg);
31
+ pbw_value_free(vurl);
32
+ pbw_value_free(vpay);
33
+ return PBW_ERR_NOMEM;
34
+ }
35
+ /* set() owns the value on success only; free what it rejects. */
36
+ status = pbw_message_set(msg, 1, vurl);
37
+ if (status != PBW_OK) {
38
+ pbw_value_free(vurl);
39
+ pbw_value_free(vpay);
40
+ pbw_message_free(msg);
41
+ return status;
42
+ }
43
+ status = pbw_message_set(msg, 2, vpay);
44
+ if (status != PBW_OK) {
45
+ pbw_value_free(vpay);
46
+ pbw_message_free(msg);
47
+ return status;
48
+ }
49
+ *out = msg;
50
+ return PBW_OK;
51
+ }
52
+
53
+ const char *
54
+ pbw_any_type_url(const pbw_message *any_msg)
55
+ {
56
+ pbw_value *v;
57
+
58
+ if (any_msg == NULL) {
59
+ return NULL;
60
+ }
61
+ v = pbw_message_get(any_msg, 1);
62
+ return v == NULL ? NULL : pbw_value_get_string(v);
63
+ }
64
+
65
+ const void *
66
+ pbw_any_payload(const pbw_message *any_msg, size_t *len)
67
+ {
68
+ pbw_value *v;
69
+
70
+ if (len != NULL) {
71
+ *len = 0;
72
+ }
73
+ if (any_msg == NULL) {
74
+ return NULL;
75
+ }
76
+ v = pbw_message_get(any_msg, 2);
77
+ return v == NULL ? NULL : pbw_value_get_bytes(v, len);
78
+ }
79
+
80
+ /* seconds (field 1) + nanos (field 2) to milliseconds, shared by Timestamp
81
+ * and Duration: same shape, same range rule. */
82
+ static pbw_status
83
+ wkt_to_millis(const pbw_message *msg, int64_t *out, int is_timestamp)
84
+ {
85
+ pbw_value *vsec;
86
+ pbw_value *vnano;
87
+ int64_t sec;
88
+ int32_t nanos = 0;
89
+
90
+ if (msg == NULL || out == NULL) {
91
+ return PBW_ERR_INVALID;
92
+ }
93
+ vsec = pbw_message_get(msg, 1);
94
+ if (vsec == NULL || vsec->kind != PBW_VALUE_UINT) {
95
+ /* A hand-built tree can hold any kind here; the getters return 0
96
+ * for the wrong kind, which would silently become the epoch. */
97
+ return PBW_ERR_INVALID;
98
+ }
99
+ sec = (int64_t)pbw_value_get_uint(vsec);
100
+ vnano = pbw_message_get(msg, 2);
101
+ if (vnano != NULL) {
102
+ /* All numerics share PBW_VALUE_UINT (raw bit pattern). */
103
+ if (vnano->kind != PBW_VALUE_UINT) {
104
+ return PBW_ERR_INVALID;
105
+ }
106
+ nanos = pbw_value_get_int32(vnano);
107
+ }
108
+ if (nanos > 999999999 || nanos < -999999999) {
109
+ return PBW_ERR_INVALID;
110
+ }
111
+ if (is_timestamp && nanos < 0) {
112
+ /* Negative nanos are only legal on a Duration. */
113
+ return PBW_ERR_INVALID;
114
+ }
115
+ /* sec*1000 + nanos/1000000 must stay inside int64, and the sub-second
116
+ * part only moves the result towards zero or away from it depending on
117
+ * its sign: bound each direction by the actual fraction, so a legal
118
+ * boundary value (sec 9223372036854775, nanos 807000000 == INT64_MAX)
119
+ * round-trips instead of reporting a limit. */
120
+ {
121
+ int64_t frac = nanos / 1000000;
122
+ int64_t hi;
123
+ int64_t lo;
124
+
125
+ /* Both bounds are always needed: a positive fraction can push the
126
+ * product *up* and a negative one *down*, but a far negative sec
127
+ * overflows on the low side regardless of the fraction's sign. The
128
+ * bound uses the actual remainder per direction, so a legal boundary
129
+ * value (sec 9223372036854775, nanos 807000000 == INT64_MAX) still
130
+ * converts. */
131
+ hi = (INT64_MAX - (frac > 0 ? frac : 0)) / 1000;
132
+ lo = (INT64_MIN - (frac < 0 ? frac : 0)) / 1000;
133
+ if (sec > hi || sec < lo) {
134
+ return PBW_ERR_LIMIT;
135
+ }
136
+ *out = sec * 1000 + frac;
137
+ }
138
+ return PBW_OK;
139
+ }
140
+
141
+ pbw_status
142
+ pbw_timestamp_to_millis(const pbw_message *ts, int64_t *out)
143
+ {
144
+ return wkt_to_millis(ts, out, 1);
145
+ }
146
+
147
+ pbw_status
148
+ pbw_duration_to_millis(const pbw_message *dur, int64_t *out)
149
+ {
150
+ return wkt_to_millis(dur, out, 0);
151
+ }
152
+
153
+ /* Pack direction for the two time shapes: field 1 holds whole seconds,
154
+ * field 2 the sub-second remainder. Duration keeps the same sign in both
155
+ * fields; Timestamp requires nanos in [0, 1e9), so a negative millisecond
156
+ * remainder borrows a second. That borrow can step one second below the
157
+ * representable range at the very bottom of int64, which is refused rather
158
+ * than written back as a pair that cannot convert. */
159
+ static pbw_status
160
+ wkt_from_millis(const pbw_descriptor *desc, int64_t ms, int floor_nanos,
161
+ pbw_message **out)
162
+ {
163
+ pbw_message *msg;
164
+ pbw_value *vsec;
165
+ pbw_value *vnano;
166
+ pbw_status status;
167
+ int64_t secs = ms / 1000;
168
+ int64_t nanos = (ms % 1000) * 1000000;
169
+
170
+ if (floor_nanos && nanos < 0) {
171
+ secs -= 1;
172
+ nanos += 1000000000;
173
+ }
174
+ if (out == NULL) {
175
+ return PBW_ERR_INVALID;
176
+ }
177
+ *out = NULL;
178
+ if (desc == NULL) {
179
+ return PBW_ERR_INVALID;
180
+ }
181
+ /* The pair must convert back: secs * 1000 has to stay inside int64, and
182
+ * borrowing a second for a negative remainder can push a value at the
183
+ * very bottom of the int64 range out of it (Timestamp cannot represent
184
+ * INT64_MIN ms, Duration can: its nanos keep the sign). */
185
+ if (secs > INT64_MAX / 1000 || secs < INT64_MIN / 1000) {
186
+ return PBW_ERR_LIMIT;
187
+ }
188
+ msg = pbw_message_new(desc);
189
+ vsec = pbw_value_new_int(secs);
190
+ vnano = pbw_value_new_int(nanos);
191
+ if (msg == NULL || vsec == NULL || vnano == NULL) {
192
+ pbw_message_free(msg);
193
+ pbw_value_free(vsec);
194
+ pbw_value_free(vnano);
195
+ return PBW_ERR_NOMEM;
196
+ }
197
+ status = pbw_message_set(msg, 1, vsec);
198
+ if (status != PBW_OK) {
199
+ pbw_value_free(vsec);
200
+ pbw_value_free(vnano);
201
+ pbw_message_free(msg);
202
+ return status;
203
+ }
204
+ status = pbw_message_set(msg, 2, vnano);
205
+ if (status != PBW_OK) {
206
+ pbw_value_free(vnano);
207
+ pbw_message_free(msg);
208
+ return status;
209
+ }
210
+ *out = msg;
211
+ return PBW_OK;
212
+ }
213
+
214
+ pbw_status
215
+ pbw_timestamp_from_millis(const pbw_descriptor *ts_desc, int64_t ms,
216
+ pbw_message **out)
217
+ {
218
+ return wkt_from_millis(ts_desc, ms, 1, out);
219
+ }
220
+
221
+ pbw_status
222
+ pbw_duration_from_millis(const pbw_descriptor *dur_desc, int64_t ms,
223
+ pbw_message **out)
224
+ {
225
+ return wkt_from_millis(dur_desc, ms, 0, out);
226
+ }
227
+
228
+ size_t
229
+ pbw_fieldmask_path_count(const pbw_message *mask)
230
+ {
231
+ pbw_value *v;
232
+
233
+ if (mask == NULL) {
234
+ return 0;
235
+ }
236
+ v = pbw_message_get(mask, 1);
237
+ return v == NULL ? 0 : pbw_value_count(v);
238
+ }
239
+
240
+ const char *
241
+ pbw_fieldmask_path_at(const pbw_message *mask, size_t index)
242
+ {
243
+ pbw_value *v;
244
+ pbw_value *item;
245
+
246
+ if (mask == NULL) {
247
+ return NULL;
248
+ }
249
+ v = pbw_message_get(mask, 1);
250
+ if (v == NULL) {
251
+ return NULL;
252
+ }
253
+ item = pbw_value_at(v, index);
254
+ return item == NULL ? NULL : pbw_value_get_string(item);
255
+ }
@@ -1,15 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Builds the C extension for rubygems: compiles ext/pbwire_ext/pbwire_ext.c
4
- # together with the C core (../../../src/*.c), so the gem is self-contained.
3
+ # Builds the C extension for rubygems from the files shipped inside the gem:
4
+ # ext/pbwire_ext/pbwire_ext.c plus the vendored core (core_*.c symlinks into
5
+ # ../../../../src, packaged as real files) and the two headers the core
6
+ # includes. No repo-root paths: `gem install` unpacks elsewhere.
5
7
  require "mkmf"
6
8
 
7
- root = File.expand_path("../../../..", __dir__)
8
- core = %w[wire arena schema message encode decode ctx descriptor_set text json stream]
9
- srcs = [File.join(__dir__, "pbwire_ext.c")] + core.map { |c| File.join(root, "src", "#{c}.c") }
9
+ core = %w[arena ctx decode descriptor_set encode json message schema stream text wire wkt]
10
+ srcs = [File.join(__dir__, "pbwire_ext.c")] + core.map { |c| File.join(__dir__, "core_#{c}.c") }
10
11
 
11
- $INCFLAGS += " -I#{File.join(root, "include")}"
12
+ $INCFLAGS += " -I#{__dir__}"
12
13
  $srcs = srcs
13
- $objs = srcs.map { |s| s.sub(/\.c$/, ".o") }
14
+ $objs = srcs.map { |s| File.join(__dir__, "#{File.basename(s, ".c")}.o") }
14
15
 
15
16
  create_makefile("pbwire_ext")