forked from SWI-Prolog/contrib-protobufs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protobufs.c
481 lines (428 loc) · 12.8 KB
/
protobufs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* Part of SWI-Prolog
Author: Jeffrey Rosenwald, extended by Peter Ludemann
E-mail: [email protected], [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2010-2015, Jeffrey Rosenwald; 2021-2024, SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/* Further information on the foreign language interface functions
used in this module:
https://swi-prolog.discourse.group/t/foreign-language-interface-integers-exceptions/4101
*/
/* TODO: Improve how failure/errors are done.
See https://github.com/SWI-Prolog/contrib-protobufs/issues/8
*/
#define O_DEBUG 1
#include <config.h>
#include <SWI-Prolog.h>
#ifdef _MSC_VER
#define inline __inline
typedef int int32_t;
#endif
/* Copy "size" bytes from "from" to "to", putting them in the byte
order for protobufs. Note that protobufs use little-endian
ordering; classic "network byte order" is big-endian. */
static inline void
cp_net_order(char *to, const char *from, size_t size) /* must be a power of 2 */
{
register int i = 0, j = 0;
#ifdef WORDS_BIGENDIAN
j = size - 1;
#endif
for (i = 0; i < size; i++)
{
to[i] = from[i ^ j];
}
}
/* For documentation of this function, see protobufs.pl */
static foreign_t
int64_zigzag(term_t Integer, term_t ZigZag)
{
if (PL_is_variable(Integer))
{
if (PL_is_variable(ZigZag))
{
return PL_instantiation_error(Integer); /* Integer,ZigZag uninstantiated */
} else
{
uint64_t v_ZigZag;
if (!PL_get_uint64_ex(ZigZag, &v_ZigZag))
return FALSE;
int64_t v_Integer = (v_ZigZag >> 1) ^ (-1 * (v_ZigZag & 1));
return PL_unify_int64(Integer, v_Integer);
}
} else
{
int64_t v_Integer;
if (!PL_get_int64_ex(Integer, &v_Integer))
return FALSE;
uint64_t v_ZigZag = ((uint64_t)v_Integer << 1) ^ (v_Integer >> 63);
return PL_unify_uint64(ZigZag, v_ZigZag);
}
}
/* For documentation of this function, see protobufs.pl */
static foreign_t
uint32_codes(term_t Number, term_t Codes)
{
union
{
uint32_t asUnsignedNumber;
char asCodes[sizeof(uint32_t)]; /* Treated as unsigned char */
}
val, val1;
/* TODO: add compile-time verification that size (int32_t) == size (uint32_t) */
char *data; /* Treated as unsigned char */
size_t len;
if (PL_is_variable(Number))
{
if (PL_is_variable(Codes))
{
return PL_instantiation_error(Number); /* Codes also is uninstantiated */
}
if (PL_get_list_nchars(Codes, &len, &data, CVT_LIST)
&& len == sizeof val.asCodes)
{
cp_net_order(val.asCodes, data, sizeof(val.asCodes));
return PL_unify_uint64(Number, val.asUnsignedNumber);
} else
{
return PL_type_error("list", Codes);
}
} else
{
if (!PL_cvt_i_uint(Number, &val.asUnsignedNumber))
return FALSE;
cp_net_order(val1.asCodes, val.asCodes, sizeof val1.asCodes);
return PL_unify_list_ncodes(Codes, sizeof(val1.asCodes), val1.asCodes);
}
}
/* For documentation of this function, see protobufs.pl */
static foreign_t
int32_codes(term_t Number, term_t Codes)
{
union
{
int32_t asSignedNumber;
char asCodes[sizeof(int32_t)]; /* Treated as unsigned char */
}
val, val1;
/* TODO: add compile-time verification that size (int32_t) == size (int32_t) */
char *data; /* Treated as unsigned char */
size_t len;
if (PL_is_variable(Number))
{
if (PL_is_variable(Codes))
{
return PL_instantiation_error(Number); /* Codes also is uninstantiated */
}
if (PL_get_list_nchars(Codes, &len, &data, CVT_LIST)
&& len == sizeof val.asCodes)
{
cp_net_order(val.asCodes, data, sizeof(val.asCodes));
return PL_unify_integer(Number, val.asSignedNumber);
} else
{
return PL_type_error("list", Codes);
}
} else
{
if (!PL_cvt_i_int(Number, &val.asSignedNumber))
return FALSE;
cp_net_order(val1.asCodes, val.asCodes, sizeof val1.asCodes);
return PL_unify_list_ncodes(Codes, sizeof(val1.asCodes), val1.asCodes);
}
}
/* For documentation of this function, see protobufs.pl */
static foreign_t
uint64_codes(term_t Number, term_t Codes)
{
union
{
uint64_t asUnsignedNumber;
char asCodes[sizeof(uint64_t)]; /* Should be unsigned char */
}
val, val1;
char *data; /* Treated as unsigned char */
size_t len;
if (PL_is_variable(Number))
{
if (PL_is_variable(Codes))
{
return PL_instantiation_error(Number); /* Codes also is uninstantiated */
}
if (PL_get_list_nchars(Codes, &len, &data, CVT_LIST)
&& len == sizeof val.asCodes)
{
cp_net_order(val.asCodes, data, sizeof(val.asCodes));
return PL_unify_uint64(Number, val.asUnsignedNumber);
} else
{
return PL_type_error("list", Codes);
}
} else
{
uint64_t v_Number;
if (!PL_get_uint64_ex(Number, &v_Number))
return FALSE;
val.asUnsignedNumber = v_Number;
cp_net_order(val1.asCodes, val.asCodes, sizeof val1.asCodes);
return PL_unify_list_ncodes(Codes, sizeof(val1.asCodes), val1.asCodes);
}
}
/* For documentation of this function, see protobufs.pl */
static foreign_t
int64_codes(term_t Number, term_t Codes)
{
union
{
int64_t asSignedNumber;
char asCodes[sizeof(int64_t)]; /* Should be unsigned char */
}
val, val1;
char *data; /* Treated as unsigned char */
size_t len;
if (PL_is_variable(Number))
{
if (PL_is_variable(Codes))
{
return PL_instantiation_error(Number); /* Codes also is uninstantiated */
}
if (PL_get_list_nchars(Codes, &len, &data, CVT_LIST)
&& len == sizeof val.asCodes)
{
cp_net_order(val.asCodes, data, sizeof(val.asCodes));
return PL_unify_int64(Number, val.asSignedNumber);
} else
{
return PL_type_error("list", Codes);
}
} else
{
int64_t v_Number;
if (!PL_get_int64_ex(Number, &v_Number))
return FALSE;
val.asSignedNumber = v_Number;
cp_net_order(val1.asCodes, val.asCodes, sizeof val1.asCodes);
return PL_unify_list_ncodes(Codes, sizeof(val1.asCodes), val1.asCodes);
}
}
/* For documentation of this function, see protobufs.pl */
static
foreign_t float32_codes(term_t Number, term_t Codes)
{
union
{
float asNumber;
char asCodes[sizeof(float)];
} val, val1;
char *data;
size_t len;
double tmp;
if(PL_get_float(Number, &tmp))
{ val.asNumber = (float)tmp;
cp_net_order(val1.asCodes, val.asCodes, sizeof(val1.asCodes));
return PL_unify_list_ncodes(Codes, sizeof(val1.asCodes), val1.asCodes);
}
if(PL_get_list_nchars(Codes, &len, &data, CVT_LIST)
&& len == sizeof(val.asCodes))
{ cp_net_order(val.asCodes, data, sizeof(val.asCodes));
tmp = val.asNumber;
return PL_unify_float(Number, tmp);
}
/* TODO: Change logic to be similar to uint32_codes, for better error messages. */
return PL_instantiation_error(Number);
}
/* For documentation of this function, see protobufs.pl */
static
foreign_t float64_codes(term_t Number, term_t Codes)
{
union
{
double asNumber;
char asCodes[sizeof(double)];
} val, val1;
char *data;
size_t len;
if(PL_get_float(Number, &val.asNumber))
{ cp_net_order(val1.asCodes, val.asCodes, sizeof(val1.asCodes));
return PL_unify_list_ncodes(Codes, sizeof(val1.asCodes), val1.asCodes);
}
if(PL_get_list_nchars(Codes, &len, &data, CVT_LIST)
&& len == sizeof(val.asCodes))
{ cp_net_order(val.asCodes, data, sizeof(val.asCodes));
return PL_unify_float(Number, val.asNumber);
}
/* TODO: Change logic to be similar to uint32_codes, for better error messages. */
return PL_instantiation_error(Number);
}
/* For documentation of this function, see protobufs.pl */
static foreign_t
uint32_int32(term_t Uint32, term_t Int32)
{
/* TODO: might generate better code if we do
asUnsigned = (uint32_t)asSigned
asSigned = (int32_t)asUnsigned
*/
union
{
uint32_t asUnsigned;
int32_t asSigned;
} val;
if (PL_is_variable(Uint32))
{
if (PL_is_variable(Int32))
{
return PL_instantiation_error(Uint32); /* Int32 also is uninstantiated */
} else
{
if (!PL_cvt_i_int(Int32, &val.asSigned))
return FALSE;
return PL_unify_uint64(Uint32, val.asUnsigned);
}
} else
{
/* Can't use PL_cvt_i_uint because it disallows the 64-bit version
of -1. Instead, get the value as uint64, then range-check it
and mask. */
uint64_t uint64;
if (!PL_get_uint64_ex(Uint32, &uint64))
return FALSE;
if (uint64 > 0xffffffff && ((uint64 & 0xffffffff00000000) != 0xffffffff00000000))
{
return PL_domain_error("32_bit_integer", Uint32);
}
val.asUnsigned = 0xffffffff & uint64; /* TODO: mask not needed? */
return PL_unify_int64(Int32, val.asSigned);
}
}
/* For documentation of this function, see protobufs.pl */
static foreign_t
uint64_int64(term_t Uint64, term_t Int64)
{
/* TODO: might generate better code if we do
asUnsigned = (uint64_t)asSigned
asSigned = (int64_t)asUnsigned
*/
union
{
uint64_t asUnsigned;
int64_t asSigned;
} val;
if (PL_is_variable(Uint64))
{
if (PL_is_variable(Int64))
{
return PL_instantiation_error(Uint64); /* Int64 also is uninstantiated */
} else
{
if (!PL_get_int64_ex(Int64, &val.asSigned))
return FALSE;
return PL_unify_uint64(Uint64, val.asUnsigned);
}
} else
{
if (!PL_get_uint64_ex(Uint64, &val.asUnsigned))
return FALSE;
return PL_unify_int64(Int64, val.asSigned);
}
}
/* For documentation of this function, see protobufs.pl */
static foreign_t
int64_float64(term_t Int64, term_t Float64)
{
/* TODO: might generate better code if we do
asInt = (int64_t)asFloat
asFloat = (double)asSigned
*/
union
{
int64_t asInt;
double asFloat;
} val;
if (PL_is_variable(Int64))
{
if (PL_is_variable(Float64))
{
return PL_instantiation_error(Int64); /* Float64 also is uninstantiated */
} else
{
if (!PL_get_float_ex(Float64, &val.asFloat))
return FALSE;
return PL_unify_int64(Int64, val.asInt);
}
} else
{
if (!PL_get_int64_ex(Int64, &val.asInt))
return FALSE;
return PL_unify_float(Float64, val.asFloat);
}
}
/* For documentation of this function, see protobufs.pl */
static foreign_t
int32_float32(term_t Int32, term_t Float32)
{
/* TODO: might generate better code if we do
int32_t asInt = (int32_t)asFloat
float asFloat = (float)asInt
*/
union
{
int32_t asInt;
float asFloat;
} val;
if (PL_is_variable(Int32))
{
if (PL_is_variable(Float32))
{
return PL_instantiation_error(Int32); /* Float32 also is uninstantiated */
} else
{
double float64;
if (!PL_get_float_ex(Float32, &float64))
return FALSE;
val.asFloat = (float)float64;
return PL_unify_int64(Int32, val.asInt);
}
} else
{
if (!PL_get_integer_ex(Int32, &val.asInt))
return FALSE;
return PL_unify_float(Float32, val.asFloat);
}
}
install_t
install_protobufs()
{
PL_register_foreign("uint32_codes", 2, uint32_codes, 0);
PL_register_foreign("int32_codes", 2, int32_codes, 0);
PL_register_foreign("float32_codes", 2, float32_codes, 0);
PL_register_foreign("uint64_codes", 2, uint64_codes, 0);
PL_register_foreign("int64_codes", 2, int64_codes, 0);
PL_register_foreign("float64_codes", 2, float64_codes, 0);
PL_register_foreign("int64_zigzag", 2, int64_zigzag, 0);
PL_register_foreign("uint32_int32", 2, uint32_int32, 0);
PL_register_foreign("uint64_int64", 2, uint64_int64, 0);
PL_register_foreign("int64_float64", 2, int64_float64, 0);
PL_register_foreign("int32_float32", 2, int32_float32, 0);
}