luasandbox  1.4.0
Generic Lua sandbox for dynamic data analysis
protobuf.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /** Generic protobuf utility functions @file */
8 
9 #ifndef luasandbox_util_protobuf_h_
10 #define luasandbox_util_protobuf_h_
11 
12 #include <stddef.h>
13 #include <stdbool.h>
14 
15 #include "output_buffer.h"
16 #include "util.h"
17 
18 #define LSB_MAX_VARINT_BYTES 10
19 
20 typedef enum {
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /**
34  * Extract the tag and wiretype from a protobuf key
35  *
36  * @param p Key
37  * @param tag Tag Id
38  * @param wiretype Wiretype Id
39  *
40  * @return LSB_EXPORT const char*
41  */
43 const char* lsb_pb_read_key(const char *p, int *tag, int *wiretype);
44 
45 /**
46  * Writes a field key (tag id/wire type) to the output buffer.
47  *
48  * @param ob Pointer to the output data buffer.
49  * @param tag Field identifier.
50  * @param wiretype Field wire type.
51  *
52  * @return lsb_err_value NULL on success error message on failure
53  */
55 lsb_pb_write_key(lsb_output_buffer *ob, unsigned char tag,
56  unsigned char wiretype);
57 
58 /**
59  * Reads the varint into the provided variable
60  *
61  * @param p Start of buffer
62  * @param e End of buffer
63  * @param vi Varint value
64  *
65  * @return const char* Position in the buffer after the varint
66  */
68 const char* lsb_pb_read_varint(const char *p, const char *e, long long *vi);
69 
70 
71 /**
72  * Outputs the varint to an existing buffer
73  *
74  * @param buf Pointer to buffer with at least LSB_MAX_VARINT_BYTES available,
75  * @param i Number to be encoded.
76  *
77  * @return int Number of bytes written
78  */
79 LSB_UTIL_EXPORT int lsb_pb_output_varint(char *buf, unsigned long long i);
80 
81 /**
82  * Writes a varint encoded number to the output buffer.
83  *
84  * @param ob Pointer to the output data buffer.
85  * @param i Number to be encoded.
86  *
87  * @return lsb_err_value NULL on success error message on failure
88  */
90 lsb_pb_write_varint(lsb_output_buffer *ob, unsigned long long i);
91 
92 /**
93  * Writes a bool to the output buffer.
94  *
95  * @param ob Pointer to the output data buffer.
96  * @param i Number to be encoded.
97  *
98  * @return lsb_err_value NULL on success error message on failure
99  */
101 
102 /**
103  * Writes a double to the output buffer.
104  *
105  * @param ob Pointer to the output data buffer.
106  * @param i Double to be encoded.
107  *
108  * @return lsb_err_value NULL on success error message on failure
109  */
112 
113 /**
114  * Writes a string to the output buffer.
115  *
116  * @param ob Pointer to the output data buffer.
117  * @param tag Field identifier.
118  * @param s String to output.
119  * @param len Length of s.
120  *
121  * @return lsb_err_value NULL on success error message on failure
122  */
124 lsb_pb_write_string(lsb_output_buffer *ob, char tag, const char *s, size_t len);
125 
126 /**
127  * Updates the field length in the output buffer once the size is known, this
128  * allows for single pass encoding.
129  *
130  * @param ob Pointer to the output data buffer.
131  * @param len_pos Position in the output buffer where the length should be
132  * written.
133  *
134  * @return lsb_err_value NULL on success error message on failure
135  */
137 lsb_pb_update_field_length(lsb_output_buffer *ob, size_t len_pos);
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif
const char * lsb_err_value
Definition: error.h:15
LSB_UTIL_EXPORT lsb_err_value lsb_pb_write_double(lsb_output_buffer *ob, double i)
Writes a double to the output buffer.
LSB_UTIL_EXPORT int lsb_pb_output_varint(char *buf, unsigned long long i)
Outputs the varint to an existing buffer.
lsb_pb_wire_types
Definition: protobuf.h:20
Data stream output buffer.
Shared types and structures.
#define LSB_UTIL_EXPORT
Definition: util.h:28
LSB_UTIL_EXPORT lsb_err_value lsb_pb_update_field_length(lsb_output_buffer *ob, size_t len_pos)
Updates the field length in the output buffer once the size is known, this allows for single pass enc...
LSB_UTIL_EXPORT const char * lsb_pb_read_key(const char *p, int *tag, int *wiretype)
Extract the tag and wiretype from a protobuf key.
LSB_UTIL_EXPORT lsb_err_value lsb_pb_write_string(lsb_output_buffer *ob, char tag, const char *s, size_t len)
Writes a string to the output buffer.
LSB_UTIL_EXPORT lsb_err_value lsb_pb_write_key(lsb_output_buffer *ob, unsigned char tag, unsigned char wiretype)
Writes a field key (tag id/wire type) to the output buffer.
LSB_UTIL_EXPORT lsb_err_value lsb_pb_write_varint(lsb_output_buffer *ob, unsigned long long i)
Writes a varint encoded number to the output buffer.
LSB_UTIL_EXPORT lsb_err_value lsb_pb_write_bool(lsb_output_buffer *ob, int i)
Writes a bool to the output buffer.
LSB_UTIL_EXPORT const char * lsb_pb_read_varint(const char *p, const char *e, long long *vi)
Reads the varint into the provided variable.