create wire/ subdir for all wire-format code PiperOrigin-RevId: 485898080
diff --git a/upb/conformance_upb.c b/upb/conformance_upb.c index eca978c..4101540 100644 --- a/upb/conformance_upb.c +++ b/upb/conformance_upb.c
@@ -39,12 +39,12 @@ #include "conformance/conformance.upbdefs.h" #include "google/protobuf/test_messages_proto2.upbdefs.h" #include "google/protobuf/test_messages_proto3.upbdefs.h" -#include "upb/decode.h" -#include "upb/encode.h" #include "upb/json/decode.h" #include "upb/json/encode.h" #include "upb/reflection/message.h" #include "upb/text_encode.h" +#include "upb/wire/decode.h" +#include "upb/wire/encode.h" // Must be last. #include "upb/port_def.inc"
diff --git a/upb/decode.h b/upb/decode.h index 526f569..981c2fa 100644 --- a/upb/decode.h +++ b/upb/decode.h
@@ -25,72 +25,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* - * upb_decode: parsing into a upb_Message using a upb_MiniTable. - */ +// This header is deprecated, use upb/wire/decode.h instead #ifndef UPB_DECODE_H_ #define UPB_DECODE_H_ -#include "upb/extension_registry.h" -#include "upb/mem/arena.h" -#include "upb/msg.h" - -// Must be last. -#include "upb/port_def.inc" - -#ifdef __cplusplus -extern "C" { -#endif - -enum { - /* If set, strings will alias the input buffer instead of copying into the - * arena. */ - kUpb_DecodeOption_AliasString = 1, - - /* If set, the parse will return failure if any message is missing any - * required fields when the message data ends. The parse will still continue, - * and the failure will only be reported at the end. - * - * IMPORTANT CAVEATS: - * - * 1. This can throw a false positive failure if an incomplete message is seen - * on the wire but is later completed when the sub-message occurs again. - * For this reason, a second pass is required to verify a failure, to be - * truly robust. - * - * 2. This can return a false success if you are decoding into a message that - * already has some sub-message fields present. If the sub-message does - * not occur in the binary payload, we will never visit it and discover the - * incomplete sub-message. For this reason, this check is only useful for - * implemting ParseFromString() semantics. For MergeFromString(), a - * post-parse validation step will always be necessary. */ - kUpb_DecodeOption_CheckRequired = 2, -}; - -#define UPB_DECODE_MAXDEPTH(depth) ((depth) << 16) - -typedef enum { - kUpb_DecodeStatus_Ok = 0, - kUpb_DecodeStatus_Malformed = 1, // Wire format was corrupt - kUpb_DecodeStatus_OutOfMemory = 2, // Arena alloc failed - kUpb_DecodeStatus_BadUtf8 = 3, // String field had bad UTF-8 - kUpb_DecodeStatus_MaxDepthExceeded = 4, // Exceeded UPB_DECODE_MAXDEPTH - - // kUpb_DecodeOption_CheckRequired failed (see above), but the parse otherwise - // succeeded. - kUpb_DecodeStatus_MissingRequired = 5, -} upb_DecodeStatus; - -upb_DecodeStatus upb_Decode(const char* buf, size_t size, upb_Message* msg, - const upb_MiniTable* l, - const upb_ExtensionRegistry* extreg, int options, - upb_Arena* arena); - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#include "upb/port_undef.inc" +#include "upb/wire/decode.h" #endif /* UPB_DECODE_H_ */
diff --git a/upb/decode_fast.h b/upb/decode_fast.h index 7803367..04a66fb 100644 --- a/upb/decode_fast.h +++ b/upb/decode_fast.h
@@ -25,143 +25,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -// These are the specialized field parser functions for the fast parser. -// Generated tables will refer to these by name. -// -// The function names are encoded with names like: -// -// // 123 4 -// upb_pss_1bt(); // Parse singular string, 1 byte tag. -// -// In position 1: -// - 'p' for parse, most function use this -// - 'c' for copy, for when we are copying strings instead of aliasing -// -// In position 2 (cardinality): -// - 's' for singular, with or without hasbit -// - 'o' for oneof -// - 'r' for non-packed repeated -// - 'p' for packed repeated -// -// In position 3 (type): -// - 'b1' for bool -// - 'v4' for 4-byte varint -// - 'v8' for 8-byte varint -// - 'z4' for zig-zag-encoded 4-byte varint -// - 'z8' for zig-zag-encoded 8-byte varint -// - 'f4' for 4-byte fixed -// - 'f8' for 8-byte fixed -// - 'm' for sub-message -// - 's' for string (validate UTF-8) -// - 'b' for bytes -// -// In position 4 (tag length): -// - '1' for one-byte tags (field numbers 1-15) -// - '2' for two-byte tags (field numbers 16-2048) +// This header is deprecated, use upb/wire/decode_fast.h instead #ifndef UPB_DECODE_FAST_H_ #define UPB_DECODE_FAST_H_ -#include "upb/msg.h" - -// Must be last. -#include "upb/port_def.inc" - -#ifdef __cplusplus -extern "C" { -#endif - -struct upb_Decoder; - -// The fallback, generic parsing function that can handle any field type. -// This just uses the regular (non-fast) parser to parse a single field. -const char* _upb_FastDecoder_DecodeGeneric(struct upb_Decoder* d, - const char* ptr, upb_Message* msg, - intptr_t table, uint64_t hasbits, - uint64_t data); - -#define UPB_PARSE_PARAMS \ - struct upb_Decoder *d, const char *ptr, upb_Message *msg, intptr_t table, \ - uint64_t hasbits, uint64_t data - -/* primitive fields ***********************************************************/ - -#define F(card, type, valbytes, tagbytes) \ - const char* upb_p##card##type##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS); - -#define TYPES(card, tagbytes) \ - F(card, b, 1, tagbytes) \ - F(card, v, 4, tagbytes) \ - F(card, v, 8, tagbytes) \ - F(card, z, 4, tagbytes) \ - F(card, z, 8, tagbytes) \ - F(card, f, 4, tagbytes) \ - F(card, f, 8, tagbytes) - -#define TAGBYTES(card) \ - TYPES(card, 1) \ - TYPES(card, 2) - -TAGBYTES(s) -TAGBYTES(o) -TAGBYTES(r) -TAGBYTES(p) - -#undef F -#undef TYPES -#undef TAGBYTES - -/* string fields **************************************************************/ - -#define F(card, tagbytes, type) \ - const char* upb_p##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS); \ - const char* upb_c##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS); - -#define UTF8(card, tagbytes) \ - F(card, tagbytes, s) \ - F(card, tagbytes, b) - -#define TAGBYTES(card) \ - UTF8(card, 1) \ - UTF8(card, 2) - -TAGBYTES(s) -TAGBYTES(o) -TAGBYTES(r) - -#undef F -#undef TAGBYTES - -/* sub-message fields *********************************************************/ - -#define F(card, tagbytes, size_ceil, ceil_arg) \ - const char* upb_p##card##m_##tagbytes##bt_max##size_ceil##b(UPB_PARSE_PARAMS); - -#define SIZES(card, tagbytes) \ - F(card, tagbytes, 64, 64) \ - F(card, tagbytes, 128, 128) \ - F(card, tagbytes, 192, 192) \ - F(card, tagbytes, 256, 256) \ - F(card, tagbytes, max, -1) - -#define TAGBYTES(card) \ - SIZES(card, 1) \ - SIZES(card, 2) - -TAGBYTES(s) -TAGBYTES(o) -TAGBYTES(r) - -#undef TAGBYTES -#undef SIZES -#undef F - -#undef UPB_PARSE_PARAMS - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#include "upb/port_undef.inc" +#include "upb/wire/decode_fast.h" #endif /* UPB_DECODE_FAST_H_ */
diff --git a/upb/decode_internal.h b/upb/decode_internal.h deleted file mode 100644 index fe3cbc7..0000000 --- a/upb/decode_internal.h +++ /dev/null
@@ -1,36 +0,0 @@ -/* - * Copyright (c) 2009-2021, Google LLC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * 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. - * * Neither the name of Google LLC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * 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 Google LLC 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. - */ - -#ifndef UPB_DECODE_INT_H_ -#define UPB_DECODE_INT_H_ - -// TODO(b/232091617): Delete this entire header which currently exists only for -// temporary backwards compatibility. - -#include "upb/internal/decode.h" - -#endif /* UPB_DECODE_INT_H_ */
diff --git a/upb/encode.h b/upb/encode.h index 977baa4..14ce190 100644 --- a/upb/encode.h +++ b/upb/encode.h
@@ -25,57 +25,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* - * upb_Encode: parsing from a upb_Message using a upb_MiniTable. - */ +// This header is deprecated, use upb/wire/encode.h instead #ifndef UPB_ENCODE_H_ #define UPB_ENCODE_H_ -#include "upb/msg.h" - -// Must be last. -#include "upb/port_def.inc" - -#ifdef __cplusplus -extern "C" { -#endif - -enum { - /* If set, the results of serializing will be deterministic across all - * instances of this binary. There are no guarantees across different - * binary builds. - * - * If your proto contains maps, the encoder will need to malloc()/free() - * memory during encode. */ - kUpb_EncodeOption_Deterministic = 1, - - /* When set, unknown fields are not printed. */ - kUpb_EncodeOption_SkipUnknown = 2, - - /* When set, the encode will fail if any required fields are missing. */ - kUpb_EncodeOption_CheckRequired = 4, -}; - -#define UPB_ENCODE_MAXDEPTH(depth) ((depth) << 16) - -typedef enum { - kUpb_EncodeStatus_Ok = 0, - kUpb_EncodeStatus_OutOfMemory = 1, // Arena alloc failed - kUpb_EncodeStatus_MaxDepthExceeded = 2, // Exceeded UPB_ENCODE_MAXDEPTH - - // kUpb_EncodeOption_CheckRequired failed but the parse otherwise succeeded. - kUpb_EncodeStatus_MissingRequired = 3, -} upb_EncodeStatus; - -upb_EncodeStatus upb_Encode(const void* msg, const upb_MiniTable* l, - int options, upb_Arena* arena, char** buf, - size_t* size); - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#include "upb/port_undef.inc" +#include "upb/wire/encode.h" #endif /* UPB_ENCODE_H_ */
diff --git a/upb/json/decode.c b/upb/json/decode.c index f632678..65fc684 100644 --- a/upb/json/decode.c +++ b/upb/json/decode.c
@@ -35,11 +35,11 @@ #include <stdlib.h> #include <string.h> -#include "upb/encode.h" #include "upb/internal/atoi.h" #include "upb/internal/unicode.h" #include "upb/map.h" #include "upb/reflection/message.h" +#include "upb/wire/encode.h" // Must be last. #include "upb/port_def.inc"
diff --git a/upb/json/encode.c b/upb/json/encode.c index c799b8e..4d0035d 100644 --- a/upb/json/encode.c +++ b/upb/json/encode.c
@@ -35,11 +35,11 @@ #include <stdio.h> #include <string.h> -#include "upb/decode.h" -#include "upb/internal/encode.h" #include "upb/internal/vsnprintf_compat.h" #include "upb/map.h" #include "upb/reflection/message.h" +#include "upb/wire/decode.h" +#include "upb/wire/encode_internal.h" // Must be last. #include "upb/port_def.inc"
diff --git a/upb/mini_table/accessors.c b/upb/mini_table/accessors.c index 02f7121..414f8a5 100644 --- a/upb/mini_table/accessors.c +++ b/upb/mini_table/accessors.c
@@ -27,9 +27,9 @@ #include "upb/mini_table/accessors.h" -#include "upb/decode.h" -#include "upb/encode.h" #include "upb/internal/array.h" +#include "upb/wire/decode.h" +#include "upb/wire/encode.h" // Must be last. #include "upb/msg.h"
diff --git a/upb/mini_table/accessors_test.cc b/upb/mini_table/accessors_test.cc index 96bb4a1..7b1f3aa 100644 --- a/upb/mini_table/accessors_test.cc +++ b/upb/mini_table/accessors_test.cc
@@ -37,11 +37,11 @@ #include "google/protobuf/test_messages_proto2.upb.h" #include "google/protobuf/test_messages_proto3.upb.h" #include "upb/array.h" -#include "upb/decode.h" #include "upb/mini_table/decode.h" #include "upb/mini_table/encode.h" #include "upb/test.upb.h" #include "upb/upb.h" +#include "upb/wire/decode.h" namespace {
diff --git a/upb/mini_table/encode_test.cc b/upb/mini_table/encode_test.cc index d76dc4a..d607aba 100644 --- a/upb/mini_table/encode_test.cc +++ b/upb/mini_table/encode_test.cc
@@ -29,12 +29,12 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" #include "absl/container/flat_hash_set.h" -#include "upb/decode.h" #include "upb/mini_table.hpp" #include "upb/mini_table/common_internal.h" #include "upb/mini_table/decode.h" #include "upb/upb.h" #include "upb/upb.hpp" +#include "upb/wire/decode.h" // begin:google_only // #include "testing/fuzzing/fuzztest.h"
diff --git a/upb/text_encode.c b/upb/text_encode.c index 45acc34..6c20206 100644 --- a/upb/text_encode.c +++ b/upb/text_encode.c
@@ -34,11 +34,11 @@ #include <stdio.h> #include <string.h> -#include "upb/internal/encode.h" #include "upb/internal/vsnprintf_compat.h" #include "upb/map.h" #include "upb/map_sorter.h" #include "upb/reflection/message.h" +#include "upb/wire/encode_internal.h" // Must be last. #include "upb/port_def.inc"
diff --git a/upb/upb.c b/upb/upb.c index 0c088b0..61649cb 100644 --- a/upb/upb.c +++ b/upb/upb.c
@@ -29,7 +29,7 @@ #include <stdio.h> #include <stdlib.h> -#include "upb/internal/encode.h" +#include "upb/wire/encode_internal.h" // Must be last. #include "upb/port_def.inc"
diff --git a/upb/upb_internal.h b/upb/upb_internal.h deleted file mode 100644 index ba8ab7e..0000000 --- a/upb/upb_internal.h +++ /dev/null
@@ -1,36 +0,0 @@ -/* - * Copyright (c) 2009-2021, Google LLC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * 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. - * * Neither the name of Google LLC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * 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 Google LLC 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. - */ - -#ifndef UPB_INT_H_ -#define UPB_INT_H_ - -// TODO(b/232091617): Delete this entire header which currently exists only for -// temporary backwards compatibility. - -#include "upb/internal/encode.h" - -#endif /* UPB_INT_H_ */
diff --git a/upb/decode.c b/upb/wire/decode.c similarity index 99% rename from upb/decode.c rename to upb/wire/decode.c index f67579b..2d2f705 100644 --- a/upb/decode.c +++ b/upb/wire/decode.c
@@ -25,14 +25,14 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "upb/decode.h" +#include "upb/wire/decode.h" #include <string.h> #include "upb/internal/array.h" -#include "upb/internal/decode.h" #include "upb/msg_internal.h" #include "upb/upb.h" +#include "upb/wire/decode_internal.h" // Must be last. #include "upb/port_def.inc"
diff --git a/upb/wire/decode.h b/upb/wire/decode.h new file mode 100644 index 0000000..d489fda --- /dev/null +++ b/upb/wire/decode.h
@@ -0,0 +1,94 @@ +/* + * Copyright (c) 2009-2021, Google LLC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Google LLC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 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 Google LLC 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. + */ + +// upb_decode: parsing into a upb_Message using a upb_MiniTable. + +#ifndef UPB_WIRE_DECODE_H_ +#define UPB_WIRE_DECODE_H_ + +#include "upb/mem/arena.h" +#include "upb/extension_registry.h" +#include "upb/msg.h" + +// Must be last. +#include "upb/port_def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + /* If set, strings will alias the input buffer instead of copying into the + * arena. */ + kUpb_DecodeOption_AliasString = 1, + + /* If set, the parse will return failure if any message is missing any + * required fields when the message data ends. The parse will still continue, + * and the failure will only be reported at the end. + * + * IMPORTANT CAVEATS: + * + * 1. This can throw a false positive failure if an incomplete message is seen + * on the wire but is later completed when the sub-message occurs again. + * For this reason, a second pass is required to verify a failure, to be + * truly robust. + * + * 2. This can return a false success if you are decoding into a message that + * already has some sub-message fields present. If the sub-message does + * not occur in the binary payload, we will never visit it and discover the + * incomplete sub-message. For this reason, this check is only useful for + * implemting ParseFromString() semantics. For MergeFromString(), a + * post-parse validation step will always be necessary. */ + kUpb_DecodeOption_CheckRequired = 2, +}; + +#define UPB_DECODE_MAXDEPTH(depth) ((depth) << 16) + +typedef enum { + kUpb_DecodeStatus_Ok = 0, + kUpb_DecodeStatus_Malformed = 1, // Wire format was corrupt + kUpb_DecodeStatus_OutOfMemory = 2, // Arena alloc failed + kUpb_DecodeStatus_BadUtf8 = 3, // String field had bad UTF-8 + kUpb_DecodeStatus_MaxDepthExceeded = 4, // Exceeded UPB_DECODE_MAXDEPTH + + // kUpb_DecodeOption_CheckRequired failed (see above), but the parse otherwise + // succeeded. + kUpb_DecodeStatus_MissingRequired = 5, +} upb_DecodeStatus; + +upb_DecodeStatus upb_Decode(const char* buf, size_t size, upb_Message* msg, + const upb_MiniTable* l, + const upb_ExtensionRegistry* extreg, int options, + upb_Arena* arena); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port_undef.inc" + +#endif /* UPB_WIRE_DECODE_H_ */
diff --git a/upb/decode_fast.c b/upb/wire/decode_fast.c similarity index 99% rename from upb/decode_fast.c rename to upb/wire/decode_fast.c index 79ff6db..ff37c3b 100644 --- a/upb/decode_fast.c +++ b/upb/wire/decode_fast.c
@@ -35,10 +35,10 @@ // field type (eg. oneof boolean field with a 1 byte tag) and then dispatch // to the specialized function as quickly as possible. -#include "upb/decode_fast.h" +#include "upb/wire/decode_fast.h" #include "upb/internal/array.h" -#include "upb/internal/decode.h" +#include "upb/wire/decode_internal.h" // Must be last. #include "upb/port_def.inc"
diff --git a/upb/wire/decode_fast.h b/upb/wire/decode_fast.h new file mode 100644 index 0000000..34d9453 --- /dev/null +++ b/upb/wire/decode_fast.h
@@ -0,0 +1,167 @@ +/* + * Copyright (c) 2009-2021, Google LLC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Google LLC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 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 Google LLC 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. + */ + +// These are the specialized field parser functions for the fast parser. +// Generated tables will refer to these by name. +// +// The function names are encoded with names like: +// +// // 123 4 +// upb_pss_1bt(); // Parse singular string, 1 byte tag. +// +// In position 1: +// - 'p' for parse, most function use this +// - 'c' for copy, for when we are copying strings instead of aliasing +// +// In position 2 (cardinality): +// - 's' for singular, with or without hasbit +// - 'o' for oneof +// - 'r' for non-packed repeated +// - 'p' for packed repeated +// +// In position 3 (type): +// - 'b1' for bool +// - 'v4' for 4-byte varint +// - 'v8' for 8-byte varint +// - 'z4' for zig-zag-encoded 4-byte varint +// - 'z8' for zig-zag-encoded 8-byte varint +// - 'f4' for 4-byte fixed +// - 'f8' for 8-byte fixed +// - 'm' for sub-message +// - 's' for string (validate UTF-8) +// - 'b' for bytes +// +// In position 4 (tag length): +// - '1' for one-byte tags (field numbers 1-15) +// - '2' for two-byte tags (field numbers 16-2048) + +#ifndef UPB_WIRE_DECODE_FAST_H_ +#define UPB_WIRE_DECODE_FAST_H_ + +#include "upb/msg.h" + +// Must be last. +#include "upb/port_def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +struct upb_Decoder; + +// The fallback, generic parsing function that can handle any field type. +// This just uses the regular (non-fast) parser to parse a single field. +const char* _upb_FastDecoder_DecodeGeneric(struct upb_Decoder* d, + const char* ptr, upb_Message* msg, + intptr_t table, uint64_t hasbits, + uint64_t data); + +#define UPB_PARSE_PARAMS \ + struct upb_Decoder *d, const char *ptr, upb_Message *msg, intptr_t table, \ + uint64_t hasbits, uint64_t data + +/* primitive fields ***********************************************************/ + +#define F(card, type, valbytes, tagbytes) \ + const char* upb_p##card##type##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS); + +#define TYPES(card, tagbytes) \ + F(card, b, 1, tagbytes) \ + F(card, v, 4, tagbytes) \ + F(card, v, 8, tagbytes) \ + F(card, z, 4, tagbytes) \ + F(card, z, 8, tagbytes) \ + F(card, f, 4, tagbytes) \ + F(card, f, 8, tagbytes) + +#define TAGBYTES(card) \ + TYPES(card, 1) \ + TYPES(card, 2) + +TAGBYTES(s) +TAGBYTES(o) +TAGBYTES(r) +TAGBYTES(p) + +#undef F +#undef TYPES +#undef TAGBYTES + +/* string fields **************************************************************/ + +#define F(card, tagbytes, type) \ + const char* upb_p##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS); \ + const char* upb_c##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS); + +#define UTF8(card, tagbytes) \ + F(card, tagbytes, s) \ + F(card, tagbytes, b) + +#define TAGBYTES(card) \ + UTF8(card, 1) \ + UTF8(card, 2) + +TAGBYTES(s) +TAGBYTES(o) +TAGBYTES(r) + +#undef F +#undef TAGBYTES + +/* sub-message fields *********************************************************/ + +#define F(card, tagbytes, size_ceil, ceil_arg) \ + const char* upb_p##card##m_##tagbytes##bt_max##size_ceil##b(UPB_PARSE_PARAMS); + +#define SIZES(card, tagbytes) \ + F(card, tagbytes, 64, 64) \ + F(card, tagbytes, 128, 128) \ + F(card, tagbytes, 192, 192) \ + F(card, tagbytes, 256, 256) \ + F(card, tagbytes, max, -1) + +#define TAGBYTES(card) \ + SIZES(card, 1) \ + SIZES(card, 2) + +TAGBYTES(s) +TAGBYTES(o) +TAGBYTES(r) + +#undef TAGBYTES +#undef SIZES +#undef F + +#undef UPB_PARSE_PARAMS + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port_undef.inc" + +#endif /* UPB_WIRE_DECODE_FAST_H_ */
diff --git a/upb/internal/decode.h b/upb/wire/decode_internal.h similarity index 97% rename from upb/internal/decode.h rename to upb/wire/decode_internal.h index ec6b375..59bf244 100644 --- a/upb/internal/decode.h +++ b/upb/wire/decode_internal.h
@@ -30,12 +30,12 @@ * decode.c and decode_fast.c. */ -#ifndef UPB_INTERNAL_DECODE_H_ -#define UPB_INTERNAL_DECODE_H_ +#ifndef UPB_WIRE_DECODE_INTERNAL_H_ +#define UPB_WIRE_DECODE_INTERNAL_H_ -#include "upb/decode.h" #include "upb/mem/arena_internal.h" #include "upb/msg_internal.h" +#include "upb/wire/decode.h" #include "third_party/utf8_range/utf8_range.h" // Must be last. @@ -208,4 +208,4 @@ #include "upb/port_undef.inc" -#endif /* UPB_INTERNAL_DECODE_H_ */ +#endif /* UPB_WIRE_DECODE_INTERNAL_H_ */
diff --git a/upb/encode.c b/upb/wire/encode.c similarity index 99% rename from upb/encode.c rename to upb/wire/encode.c index b18418c..1b8deea 100644 --- a/upb/encode.c +++ b/upb/wire/encode.c
@@ -25,9 +25,9 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* We encode backwards, to avoid pre-computing lengths (one-pass encode). */ +// We encode backwards, to avoid pre-computing lengths (one-pass encode). -#include "upb/encode.h" +#include "upb/wire/encode.h" #include <string.h>
diff --git a/upb/wire/encode.h b/upb/wire/encode.h new file mode 100644 index 0000000..8245d08 --- /dev/null +++ b/upb/wire/encode.h
@@ -0,0 +1,81 @@ +/* + * Copyright (c) 2009-2021, Google LLC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Google LLC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 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 Google LLC 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. + */ + +/* + * upb_Encode: parsing from a upb_Message using a upb_MiniTable. + */ + +#ifndef UPB_WIRE_ENCODE_H_ +#define UPB_WIRE_ENCODE_H_ + +#include "upb/msg.h" + +// Must be last. +#include "upb/port_def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + /* If set, the results of serializing will be deterministic across all + * instances of this binary. There are no guarantees across different + * binary builds. + * + * If your proto contains maps, the encoder will need to malloc()/free() + * memory during encode. */ + kUpb_EncodeOption_Deterministic = 1, + + /* When set, unknown fields are not printed. */ + kUpb_EncodeOption_SkipUnknown = 2, + + /* When set, the encode will fail if any required fields are missing. */ + kUpb_EncodeOption_CheckRequired = 4, +}; + +#define UPB_ENCODE_MAXDEPTH(depth) ((depth) << 16) + +typedef enum { + kUpb_EncodeStatus_Ok = 0, + kUpb_EncodeStatus_OutOfMemory = 1, // Arena alloc failed + kUpb_EncodeStatus_MaxDepthExceeded = 2, // Exceeded UPB_ENCODE_MAXDEPTH + + // kUpb_EncodeOption_CheckRequired failed but the parse otherwise succeeded. + kUpb_EncodeStatus_MissingRequired = 3, +} upb_EncodeStatus; + +upb_EncodeStatus upb_Encode(const void* msg, const upb_MiniTable* l, + int options, upb_Arena* arena, char** buf, + size_t* size); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port_undef.inc" + +#endif /* UPB_WIRE_ENCODE_H_ */
diff --git a/upb/internal/encode.h b/upb/wire/encode_internal.h similarity index 94% rename from upb/internal/encode.h rename to upb/wire/encode_internal.h index c74d08f..24ae976 100644 --- a/upb/internal/encode.h +++ b/upb/wire/encode_internal.h
@@ -25,8 +25,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef UPB_INTERNAL_ENCODE_H_ -#define UPB_INTERNAL_ENCODE_H_ +#ifndef UPB_WIRE_ENCODE_INTERNAL_H_ +#define UPB_WIRE_ENCODE_INTERNAL_H_ // Must be last. #include "upb/port_def.inc" @@ -51,4 +51,4 @@ #include "upb/port_undef.inc" -#endif /* UPB_INTERNAL_ENCODE_H_ */ +#endif /* UPB_WIRE_ENCODE_INTERNAL_H_ */