Debian Codegraph @

include/dpp/etf.h

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 * Copyright 2021 Craig Edwards and D++ contributors
7 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * Parts of this file inspired by, or outright copied from erlpack:
22 * https://github.com/discord/erlpack/
23 *
24 * Acknowledgements:
25 *
26 * sysdep.h:
27 * Based on work by FURUHASHI Sadayuki in msgpack-python
28 * (https://github.com/msgpack/msgpack-python)
29 *
30 * Copyright (C) 2008-2010 FURUHASHI Sadayuki
31 * Licensed under the Apache License, Version 2.0 (the "License").
32 *
33 ************************************************************************************/
34
35 #pragma once
36 #include <dpp/export.h>
37 #include <dpp/snowflake.h>
38 #include <dpp/json_fwd.h>
39
40 namespace dpp {
41
42 /**
43 * @brief Current ETF format version in use
44 */
45 const uint8_t FORMAT_VERSION = 131;
46
47 /**
48 * @brief Represents a token which identifies the type of value which follows it
49 * in the ETF binary structure.
50 */
51 enum etf_token_type : uint8_t {
52 /**
53 * @brief 68 [Distribution header]
54 */
55 ett_distribution = 'D',
56
57 /**
58 * @brief 70 [Float64:IEEE float]
59 */
60 ett_new_float = 'F',
61
62 /**
63 * @brief 77 [UInt32:Len, UInt8:Bits, Len:Data]
64 */
65 ett_bit_binary = 'M',
66
67 /**
68 * @brief 80 [UInt4:UncompressedSize, N:ZlibCompressedData]
69 */
70 ett_compressed = 'P',
71
72 /**
73 * @brief 97 [UInt8:Int]
74 */
75 ett_smallint = 'a',
76
77 /**
78 * @brief 98 [Int32:Int]
79 */
80 ett_integer = 'b',
81
82 /**
83 * @brief 99 [31:Float String] Float in string format (formatted "%.20e", sscanf "%lf").
84 *
85 * @note Superseded by ett_new_float.
86 */
87 ett_float = 'c',
88
89 /**
90 * @brief 100 [UInt16:Len, Len:AtomName] max Len is 255
91 */
92 ett_atom = 'd',
93
94 /**
95 * @brief 101 [atom:Node, UInt32:ID, UInt8:Creation]
96 */
97 ett_reference = 'e',
98
99 /**
100 * @brief 102 [atom:Node, UInt32:ID, UInt8:Creation]
101 */
102 ett_port = 'f',
103
104 /**
105 * @brief 103 [atom:Node, UInt32:ID, UInt32:Serial, UInt8:Creation]
106 */
107 ett_pid = 'g',
108
109 /**
110 * @brief 104 [UInt8:Arity, N:Elements]
111 */
112 ett_small_tuple = 'h',
113
114 /**
115 * @brief 105 [UInt32:Arity, N:Elements]
116 */
117 ett_large_tuple = 'i',
118
119 /**
120 * @brief 106 empty list
121 */
122 ett_nil = 'j',
123
124 /**
125 * @brief 107 [UInt16:Len, Len:Characters]
126 */
127 ett_string = 'k',
128
129 /**
130 * @brief 108 [UInt32:Len, Elements, Tail]
131 */
132 ett_list = 'l',
133
134 /**
135 * @brief 109 [UInt32:Len, Len:Data]
136 */
137 ett_binary = 'm',
138
139 /**
140 * @brief 110 [UInt8:n, UInt8:Sign, n:nums]
141 */
142 ett_bigint_small = 'n',
143
144 /**
145 * @brief 111 [UInt32:n, UInt8:Sign, n:nums]
146 */
147 ett_bigint_large = 'o',
148
149 /**
150 * @brief 112 [UInt32:Size, UInt8:Arity, 16*Uint6-MD5:Uniq, UInt32:Index, UInt32:NumFree, atom:Module, int:OldIndex, int:OldUniq, pid:Pid, NunFree*ext:FreeVars]
151 */
152 ett_new_function = 'p',
153
154 /**
155 * @brief 113 [atom:Module, atom:Function, smallint:Arity]
156 */
157 ett_export = 'q',
158
159 /**
160 * @brief 114 [UInt16:Len, atom:Node, UInt8:Creation, Len*UInt32:ID]
161 */
162 ett_new_reference = 'r',
163
164 /**
165 * @brief 115 [UInt8:Len, Len:AtomName]
166 */
167 ett_atom_small = 's',
168
169 /**
170 * @brief 116 [UInt32:Airty, N:Pairs]
171 */
172 ett_map = 't',
173
174 /**
175 * @brief 117 [UInt4:NumFree, pid:Pid, atom:Module, int:Index, int:Uniq, NumFree*ext:FreeVars]
176 */
177 ett_function = 'u',
178
179 /**
180 * @brief 118 [UInt16:Len, Len:AtomName] max Len is 255 characters (up to 4 bytes per)
181 */
182 ett_atom_utf8 = 'v',
183
184 /**
185 * @brief 119 [UInt8:Len, Len:AtomName]
186 */
187 ett_atom_utf8_small = 'w'
188 };
189
190 /**
191 * @brief Represents a buffer of bytes being encoded into ETF
192 */
193 struct DPP_EXPORT etf_buffer {
194 /**
195 * @brief Raw buffer
196 */
197 std::vector<char> buf;
198
199 /**
200 * @brief Current used length of buffer
201 * (this is different from buf.size() as it is pre-allocated
202 * using resize and may not all be in use)
203 */
204 size_t length;
205
206 /**
207 * @brief Construct a new etf buffer object
208 *
209 * @param initial initial buffer size to allocate
210 */
211 etf_buffer(size_t initial);
212
213 /**
214 * @brief Destroy the etf buffer object
215 */
216 ~etf_buffer();
217 };
218
219 /**
220 * @brief The etf_parser class can serialise and deserialise ETF (Erlang Term Format)
221 * into and out of an nlohmann::json object, so that layers above the websocket don't
222 * have to be any different for handling ETF.
223 */
224 class DPP_EXPORT etf_parser {
225 /**
226 * @brief Current size of binary data
227 */
228 size_t size;
229
230 /**
231 * @brief Current offset into binary data
232 */
233 size_t offset;
234
235 /**
236 * @brief Pointer to binary ETF data to be decoded
237 */
238 uint8_t* data;
239
240 /**
241 * @brief Parse a single value, and if that value contains other
242 * values (e.g. an array or map) then call itself recursively.
243 *
244 * @return nlohmann::json JSON value from the ETF
245 */
246 nlohmann::json inner_parse();
247
248 /**
249 * @brief Read 8 bits of data from the buffer
250 *
251 * @return uint8_t data retrieved
252 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
253 */
254 uint8_t read_8_bits();
255
256 /**
257 * @brief Read 16 bits of data from the buffer
258 *
259 * @return uint16_t data retrieved
260 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
261 */
262 uint16_t read_16_bits();
263
264 /**
265 * @brief Read 32 bits of data from the buffer
266 *
267 * @return uint32_t data retrieved
268 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
269 */
270 uint32_t read_32_bits();
271
272 /**
273 * @brief Read 64 bits of data from the buffer
274 *
275 * @return uint64_t data retrieved
276 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
277 */
278 uint64_t read_64_bits();
279
280 /**
281 * @brief Read string data from the buffer
282 *
283 * @param length Length of string to retrieve
284 * @return const char* data retrieved
285 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
286 */
287 const char* read_string(uint32_t length);
288
289 /**
290 * @brief Process an 'atom' value.
291 * An atom is a "label" or constant value within the data,
292 * such as a key name, nullptr, or false.
293 *
294 * @return nlohmann::json value converted to JSON
295 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
296 */
297 nlohmann::json process_atom(const char* atom, uint16_t length);
298
299 /**
300 * @brief Decode an 'atom' value.
301 *
302 * @return nlohmann::json value converted to JSON
303 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
304 */
305 nlohmann::json decode_atom();
306
307 /**
308 * @brief Decode a small 'atom' value.
309 *
310 * @return nlohmann::json value converted to JSON
311 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
312 */
313 nlohmann::json decode_small_atom();
314
315 /**
316 * @brief Decode a small integer value (0-255).
317 *
318 * @return nlohmann::json value converted to JSON
319 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
320 */
321 nlohmann::json decode_small_integer();
322
323 /**
324 * @brief Decode an integer value (-MAXINT -> MAXINT-1).
325 *
326 * @return nlohmann::json value converted to JSON
327 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
328 */
329 nlohmann::json decode_integer();
330
331 /**
332 * @brief Decode an array of values.
333 *
334 * @return nlohmann::json values converted to JSON
335 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
336 */
337 nlohmann::json decode_array(uint32_t length);
338
339 /**
340 * @brief Decode a list of values.
341 *
342 * @return nlohmann::json values converted to JSON
343 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
344 */
345 nlohmann::json decode_list();
346
347 /**
348 * @brief Decode a 'tuple' value.
349 *
350 * @return nlohmann::json value converted to JSON
351 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
352 */
353 nlohmann::json decode_tuple(uint32_t length);
354
355 /**
356 * @brief Decode a nil 'atom' value.
357 *
358 * @return nlohmann::json value converted to JSON
359 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
360 */
361 nlohmann::json decode_nil();
362
363 /**
364 * @brief Decode a map (object) value.
365 * Will recurse to evaluate each member variable.
366 *
367 * @return nlohmann::json values converted to JSON
368 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
369 */
370 nlohmann::json decode_map();
371
372 /**
373 * @brief Decode a floating point numeric value.
374 * (depreciated in erlang but still expected to be supported)
375 *
376 * @return nlohmann::json value converted to JSON
377 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
378 */
379 nlohmann::json decode_float();
380
381 /**
382 * @brief Decode a floating type numeric value.
383 *
384 * @return nlohmann::json value converted to JSON
385 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
386 */
387 nlohmann::json decode_new_float();
388
389 /**
390 * @brief Decode a 'bigint' value.
391 *
392 * @return nlohmann::json value converted to JSON
393 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
394 */
395 nlohmann::json decode_bigint(uint32_t digits);
396
397 /**
398 * @brief Decode a small 'bigint' value.
399 *
400 * @return nlohmann::json value converted to JSON
401 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
402 */
403 nlohmann::json decode_bigint_small();
404
405 /**
406 * @brief Decode a large 'bigint' value.
407 *
408 * @return nlohmann::json value converted to JSON
409 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
410 */
411 nlohmann::json decode_bigint_large();
412
413 /**
414 * @brief Decode a binary value.
415 *
416 * @return nlohmann::json value converted to JSON
417 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
418 */
419 nlohmann::json decode_binary();
420
421 /**
422 * @brief Decode a string value.
423 *
424 * @return nlohmann::json value converted to JSON
425 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
426 */
427 nlohmann::json decode_string();
428
429 /**
430 * @brief Decode a string list value.
431 *
432 * @return nlohmann::json value converted to JSON
433 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
434 */
435 nlohmann::json decode_string_as_list();
436
437 /**
438 * @brief Decode a 'small tuple' value.
439 *
440 * @return nlohmann::json value converted to JSON
441 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
442 */
443 nlohmann::json decode_tuple_small();
444
445 /**
446 * @brief Decode a 'large tuple' value.
447 *
448 * @return nlohmann::json value converted to JSON
449 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
450 */
451 nlohmann::json decode_tuple_large();
452
453 /**
454 * @brief Decode a compressed value.
455 * This is a zlib-compressed binary blob which contains another
456 * ETF object.
457 *
458 * @return nlohmann::json value converted to JSON
459 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
460 */
461 nlohmann::json decode_compressed();
462
463 /**
464 * @brief Decode a 'reference' value.
465 * Erlang expects this to be supported, in practice Discord doesn't send these right now.
466 *
467 * @return nlohmann::json value converted to JSON
468 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
469 */
470 nlohmann::json decode_reference();
471
472 /**
473 * @brief Decode a 'new reference' value.
474 * Erlang expects this to be supported, in practice Discord doesn't send these right now.
475 *
476 * @return nlohmann::json value converted to JSON
477 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
478 */
479 nlohmann::json decode_new_reference();
480
481 /**
482 * @brief Decode a 'port' value.
483 * Erlang expects this to be supported, in practice Discord doesn't send these right now.
484 *
485 * @return nlohmann::json value converted to JSON
486 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
487 */
488 nlohmann::json decode_port();
489
490 /**
491 * @brief Decode a 'PID' value.
492 * Erlang expects this to be supported, in practice Discord doesn't send these right now.
493 *
494 * @return nlohmann::json value converted to JSON
495 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
496 */
497 nlohmann::json decode_pid();
498
499 /**
500 * @brief Decode an 'export' value.
501 * Erlang expects this to be supported, in practice Discord doesn't send these right now.
502 *
503 * @return nlohmann::json value converted to JSON
504 * @throw dpp::exception Data stream isn't long enough to fetch requested bits
505 */
506 nlohmann::json decode_export();
507
508 /**
509 * @brief Write to output buffer for creation of ETF from JSON
510 *
511 * @param pk buffer struct
512 * @param bytes byte buffer to write
513 * @param l number of bytes to write
514 * @throw std::exception Buffer cannot be extended
515 */
516 void buffer_write(etf_buffer *pk, const char *bytes, size_t l);
517
518 /**
519 * @brief Append version number to ETF buffer
520 *
521 * @param b buffer to append to
522 * @throw std::exception Buffer cannot be extended
523 */
524 void append_version(etf_buffer *b);
525
526 /**
527 * @brief Append nil value to ETF buffer
528 *
529 * @param b buffer to append to
530 * @throw std::exception Buffer cannot be extended
531 */
532 void append_nil(etf_buffer *b);
533
534 /**
535 * @brief Append false value to ETF buffer
536 *
537 * @param b buffer to append to
538 * @throw std::exception Buffer cannot be extended
539 */
540 void append_false(etf_buffer *b);
541
542 /**
543 * @brief Append true value to ETF buffer
544 *
545 * @param b buffer to append to
546 * @throw std::exception Buffer cannot be extended
547 */
548 void append_true(etf_buffer *b);
549
550 /**
551 * @brief Append small integer value to ETF buffer
552 *
553 * @param b buffer to append to
554 * @param d double to append
555 * @throw std::exception Buffer cannot be extended
556 */
557 void append_small_integer(etf_buffer *b, unsigned char d);
558
559 /**
560 * @brief Append integer value to ETF buffer
561 *
562 * @param b buffer to append to
563 * @param d integer to append
564 * @throw std::exception Buffer cannot be extended
565 */
566 void append_integer(etf_buffer *b, int32_t d);
567
568 /**
569 * @brief Append 64 bit integer value to ETF buffer
570 *
571 * @param b buffer to append to
572 * @param d integer to append
573 * @throw std::exception Buffer cannot be extended
574 */
575 void append_unsigned_long_long(etf_buffer *b, unsigned long long d);
576
577 /**
578 * @brief Append 64 bit integer value to ETF buffer
579 *
580 * @param b buffer to append to
581 * @param d integer to append
582 * @throw std::exception Buffer cannot be extended
583 */
584 void append_long_long(etf_buffer *b, long long d);
585
586 /**
587 * @brief Append double value to ETF buffer
588 *
589 * @param b buffer to append to
590 * @param f double to append
591 * @throw std::exception Buffer cannot be extended
592 */
593 void append_double(etf_buffer *b, double f);
594
595 /**
596 * @brief Append atom value to ETF buffer
597 *
598 * @param b buffer to append to
599 * @param bytes pointer to string to append
600 * @param size size of string to append
601 * @throw std::exception Buffer cannot be extended
602 */
603 void append_atom(etf_buffer *b, const char *bytes, size_t size);
604
605 /**
606 * @brief Append utf8 atom value to ETF buffer
607 *
608 * @param b buffer to append to
609 * @param bytes pointer to string to append
610 * @param size size of string to append
611 * @throw std::exception Buffer cannot be extended
612 */
613 void append_atom_utf8(etf_buffer *b, const char *bytes, size_t size);
614
615 /**
616 * @brief Append binary value to ETF buffer
617 *
618 * @param b buffer to append to
619 * @param bytes pointer to string to append
620 * @param size size of string to append
621 * @throw std::exception Buffer cannot be extended
622 */
623 void append_binary(etf_buffer *b, const char *bytes, size_t size);
624
625 /**
626 * @brief Append string value to ETF buffer
627 *
628 * @param b buffer to append to
629 * @param bytes pointer to string to append
630 * @param size size of string to append
631 * @throw std::exception Buffer cannot be extended
632 */
633 void append_string(etf_buffer *b, const char *bytes, size_t size);
634
635 /**
636 * @brief Append tuple value to ETF buffer
637 *
638 * @param b buffer to append to
639 * @param size size of value to append
640 * @throw std::exception Buffer cannot be extended
641 */
642 void append_tuple_header(etf_buffer *b, size_t size);
643
644 /**
645 * @brief Append list terminator to ETF buffer
646 *
647 * @param b buffer to append to
648 * @throw std::exception Buffer cannot be extended
649 */
650 void append_nil_ext(etf_buffer *b);
651
652 /**
653 * @brief Append a list header value to ETF buffer
654 *
655 * @param b buffer to append to
656 * @param size size of values to append
657 * @throw std::exception Buffer cannot be extended
658 */
659 void append_list_header(etf_buffer *b, size_t size);
660
661 /**
662 * @brief Append a map header value to ETF buffer
663 *
664 * @param b buffer to append to
665 * @param size size of values to append
666 * @throw std::exception Buffer cannot be extended
667 */
668 void append_map_header(etf_buffer *b, size_t size);
669
670 /**
671 * @brief Build ETF buffer
672 *
673 * @param j JSON object to build from
674 * @param b Buffer to append to
675 * @throw std::exception Buffer cannot be extended
676 */
677 void inner_build(const nlohmann::json* j, etf_buffer* b);
678
679 public:
680 /**
681 * @brief Construct a new etf parser object
682 */
683 etf_parser();
684
685 /**
686 * @brief Destroy the etf parser object
687 */
688 ~etf_parser();
689
690 /**
691 * @brief Convert ETF binary content to nlohmann::json
692 *
693 * @param in Raw binary ETF data (generally from a websocket)
694 * @return nlohmann::json JSON data for use in the library
695 * @throw dpp::exception Malformed or otherwise invalid ETF content
696 */
697 nlohmann::json parse(const std::string& in);
698
699 /**
700 * @brief Create ETF binary data from nlohmann::json
701 *
702 * @param j JSON value to encode to ETF
703 * @return std::string raw ETF data. Note that this can
704 * and probably will contain null values, use std::string::data()
705 * and std::string::size() to manipulate or send it.
706 * @throw std::exception Not enough memory, or invalid data types/values
707 */
708 std::string build(const nlohmann::json& j);
709 };
710
711 }