Debian Codegraph @

include/prism/pack.h

raw symbols SCIP
1 /**
2 * @file pack.h
3 *
4 * A pack template string parser.
5 */
6 #ifndef PRISM_PACK_H
7 #define PRISM_PACK_H
8
9 #include "prism/defines.h"
10
11 // We optionally support parsing String#pack templates. For systems that don't
12 // want or need this functionality, it can be turned off with the
13 // PRISM_EXCLUDE_PACK define.
14 #ifdef PRISM_EXCLUDE_PACK
15
16 void pm_pack_parse(void);
17
18 #else
19
20 #include <stdint.h>
21 #include <stdlib.h>
22
23 /** The version of the pack template language that we are parsing. */
24 typedef enum pm_pack_version {
25 PM_PACK_VERSION_3_2_0
26 } pm_pack_version;
27
28 /** The type of pack template we are parsing. */
29 typedef enum pm_pack_variant {
30 PM_PACK_VARIANT_PACK,
31 PM_PACK_VARIANT_UNPACK
32 } pm_pack_variant;
33
34 /** A directive within the pack template. */
35 typedef enum pm_pack_type {
36 PM_PACK_SPACE,
37 PM_PACK_COMMENT,
38 PM_PACK_INTEGER,
39 PM_PACK_UTF8,
40 PM_PACK_BER,
41 PM_PACK_FLOAT,
42 PM_PACK_STRING_SPACE_PADDED,
43 PM_PACK_STRING_NULL_PADDED,
44 PM_PACK_STRING_NULL_TERMINATED,
45 PM_PACK_STRING_MSB,
46 PM_PACK_STRING_LSB,
47 PM_PACK_STRING_HEX_HIGH,
48 PM_PACK_STRING_HEX_LOW,
49 PM_PACK_STRING_UU,
50 PM_PACK_STRING_MIME,
51 PM_PACK_STRING_BASE64,
52 PM_PACK_STRING_FIXED,
53 PM_PACK_STRING_POINTER,
54 PM_PACK_MOVE,
55 PM_PACK_BACK,
56 PM_PACK_NULL,
57 PM_PACK_END
58 } pm_pack_type;
59
60 /** The signness of a pack directive. */
61 typedef enum pm_pack_signed {
62 PM_PACK_UNSIGNED,
63 PM_PACK_SIGNED,
64 PM_PACK_SIGNED_NA
65 } pm_pack_signed;
66
67 /** The endianness of a pack directive. */
68 typedef enum pm_pack_endian {
69 PM_PACK_AGNOSTIC_ENDIAN,
70 PM_PACK_LITTLE_ENDIAN, // aka 'VAX', or 'V'
71 PM_PACK_BIG_ENDIAN, // aka 'network', or 'N'
72 PM_PACK_NATIVE_ENDIAN,
73 PM_PACK_ENDIAN_NA
74 } pm_pack_endian;
75
76 /** The size of an integer pack directive. */
77 typedef enum pm_pack_size {
78 PM_PACK_SIZE_SHORT,
79 PM_PACK_SIZE_INT,
80 PM_PACK_SIZE_LONG,
81 PM_PACK_SIZE_LONG_LONG,
82 PM_PACK_SIZE_8,
83 PM_PACK_SIZE_16,
84 PM_PACK_SIZE_32,
85 PM_PACK_SIZE_64,
86 PM_PACK_SIZE_P,
87 PM_PACK_SIZE_NA
88 } pm_pack_size;
89
90 /** The type of length of a pack directive. */
91 typedef enum pm_pack_length_type {
92 PM_PACK_LENGTH_FIXED,
93 PM_PACK_LENGTH_MAX,
94 PM_PACK_LENGTH_RELATIVE, // special case for unpack @*
95 PM_PACK_LENGTH_NA
96 } pm_pack_length_type;
97
98 /** The type of encoding for a pack template string. */
99 typedef enum pm_pack_encoding {
100 PM_PACK_ENCODING_START,
101 PM_PACK_ENCODING_ASCII_8BIT,
102 PM_PACK_ENCODING_US_ASCII,
103 PM_PACK_ENCODING_UTF_8
104 } pm_pack_encoding;
105
106 /** The result of parsing a pack template. */
107 typedef enum pm_pack_result {
108 PM_PACK_OK,
109 PM_PACK_ERROR_UNSUPPORTED_DIRECTIVE,
110 PM_PACK_ERROR_UNKNOWN_DIRECTIVE,
111 PM_PACK_ERROR_LENGTH_TOO_BIG,
112 PM_PACK_ERROR_BANG_NOT_ALLOWED,
113 PM_PACK_ERROR_DOUBLE_ENDIAN
114 } pm_pack_result;
115
116 /**
117 * Parse a single directive from a pack or unpack format string.
118 *
119 * @param variant (in) pack or unpack
120 * @param format (in, out) the start of the next directive to parse on calling,
121 * and advanced beyond the parsed directive on return, or as much of it as
122 * was consumed until an error was encountered
123 * @param format_end (in) the end of the format string
124 * @param type (out) the type of the directive
125 * @param signed_type (out) whether the value is signed
126 * @param endian (out) the endianness of the value
127 * @param size (out) the size of the value
128 * @param length_type (out) what kind of length is specified
129 * @param length (out) the length of the directive
130 * @param encoding (in, out) takes the current encoding of the string which
131 * would result from parsing the whole format string, and returns a possibly
132 * changed directive - the encoding should be `PM_PACK_ENCODING_START` when
133 * pm_pack_parse is called for the first directive in a format string
134 *
135 * @return `PM_PACK_OK` on success or `PM_PACK_ERROR_*` on error
136 * @note Consult Ruby documentation for the meaning of directives.
137 */
138 PRISM_EXPORTED_FUNCTION pm_pack_result
139 pm_pack_parse(
140 pm_pack_variant variant,
141 const char **format,
142 const char *format_end,
143 pm_pack_type *type,
144 pm_pack_signed *signed_type,
145 pm_pack_endian *endian,
146 pm_pack_size *size,
147 pm_pack_length_type *length_type,
148 uint64_t *length,
149 pm_pack_encoding *encoding
150 );
151
152 /**
153 * Prism abstracts sizes away from the native system - this converts an abstract
154 * size to a native size.
155 *
156 * @param size The abstract size to convert.
157 * @return The native size.
158 */
159 PRISM_EXPORTED_FUNCTION size_t pm_size_to_native(pm_pack_size size);
160
161 #endif
162
163 #endif