| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 383fa01 commit 6870bb7
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -185,11 +185,10 @@ namespace internals { | |||
| 185 | 185 | // |type| is the major type as specified in RFC 7049 Section 2.1. | |
| 186 | 186 | // |value| is the payload (e.g. for MajorType::UNSIGNED) or is the size | |
| 187 | 187 | // (e.g. for BYTE_STRING). | |
| 188 | - // If successful, returns the number of bytes read. Otherwise returns -1. | ||
| 189 | - // TODO(johannes): change return type to size_t and use 0 for error. | ||
| 190 | - int8_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) { | ||
| 188 | + // If successful, returns the number of bytes read. Otherwise returns 0. | ||
| 189 | + size_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) { | ||
| 191 | 190 | if (bytes.empty()) | |
| 192 | - return -1; | ||
| 191 | + return 0; | ||
| 193 | 192 | uint8_t initial_byte = bytes[0]; | |
| 194 | 193 | *type = MajorType((initial_byte & kMajorTypeMask) >> kMajorTypeBitShift); | |
| 195 | 194 | ||
@@ -203,32 +202,32 @@ int8_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) { | |||
| 203 | 202 | if (additional_information == kAdditionalInformation1Byte) { | |
| 204 | 203 | // Values 24-255 are encoded with one initial byte, followed by the value. | |
| 205 | 204 | if (bytes.size() < 2) | |
| 206 | - return -1; | ||
| 205 | + return 0; | ||
| 207 | 206 | *value = ReadBytesMostSignificantByteFirst<uint8_t>(bytes.subspan(1)); | |
| 208 | 207 | return 2; | |
| 209 | 208 | } | |
| 210 | 209 | if (additional_information == kAdditionalInformation2Bytes) { | |
| 211 | 210 | // Values 256-65535: 1 initial byte + 2 bytes payload. | |
| 212 | 211 | if (bytes.size() < 1 + sizeof(uint16_t)) | |
| 213 | - return -1; | ||
| 212 | + return 0; | ||
| 214 | 213 | *value = ReadBytesMostSignificantByteFirst<uint16_t>(bytes.subspan(1)); | |
| 215 | 214 | return 3; | |
| 216 | 215 | } | |
| 217 | 216 | if (additional_information == kAdditionalInformation4Bytes) { | |
| 218 | 217 | // 32 bit uint: 1 initial byte + 4 bytes payload. | |
| 219 | 218 | if (bytes.size() < 1 + sizeof(uint32_t)) | |
| 220 | - return -1; | ||
| 219 | + return 0; | ||
| 221 | 220 | *value = ReadBytesMostSignificantByteFirst<uint32_t>(bytes.subspan(1)); | |
| 222 | 221 | return 5; | |
| 223 | 222 | } | |
| 224 | 223 | if (additional_information == kAdditionalInformation8Bytes) { | |
| 225 | 224 | // 64 bit uint: 1 initial byte + 8 bytes payload. | |
| 226 | 225 | if (bytes.size() < 1 + sizeof(uint64_t)) | |
| 227 | - return -1; | ||
| 226 | + return 0; | ||
| 228 | 227 | *value = ReadBytesMostSignificantByteFirst<uint64_t>(bytes.subspan(1)); | |
| 229 | 228 | return 9; | |
| 230 | 229 | } | |
| 231 | - return -1; | ||
| 230 | + return 0; | ||
| 232 | 231 | } | |
| 233 | 232 | ||
| 234 | 233 | // Writes the start of a token with |type|. The |value| may indicate the size, | |
@@ -770,10 +769,10 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) { | |||
| 770 | 769 | SetToken(CBORTokenTag::NULL_VALUE, 1); | |
| 771 | 770 | return; | |
| 772 | 771 | case kExpectedConversionToBase64Tag: { // BINARY | |
| 773 | - const int8_t bytes_read = internals::ReadTokenStart( | ||
| 772 | + const size_t bytes_read = internals::ReadTokenStart( | ||
| 774 | 773 | bytes_.subspan(status_.pos + 1), &token_start_type_, | |
| 775 | 774 | &token_start_internal_value_); | |
| 776 | - if (bytes_read < 0 || token_start_type_ != MajorType::BYTE_STRING || | ||
| 775 | + if (!bytes_read || token_start_type_ != MajorType::BYTE_STRING || | ||
| 777 | 776 | token_start_internal_value_ > kMaxValidLength) { | |
| 778 | 777 | SetError(Error::CBOR_INVALID_BINARY); | |
| 779 | 778 | return; | |
@@ -823,22 +822,21 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) { | |||
| 823 | 822 | return; | |
| 824 | 823 | } | |
| 825 | 824 | default: { | |
| 826 | - const int8_t token_start_length = internals::ReadTokenStart( | ||
| 825 | + const size_t bytes_read = internals::ReadTokenStart( | ||
| 827 | 826 | bytes_.subspan(status_.pos), &token_start_type_, | |
| 828 | 827 | &token_start_internal_value_); | |
| 829 | - const bool success = token_start_length >= 0; | ||
| 830 | 828 | switch (token_start_type_) { | |
| 831 | 829 | case MajorType::UNSIGNED: // INT32. | |
| 832 | 830 | // INT32 is a signed int32 (int32 makes sense for the | |
| 833 | 831 | // inspector_protocol, it's not a CBOR limitation), so we check | |
| 834 | 832 | // against the signed max, so that the allowable values are | |
| 835 | 833 | // 0, 1, 2, ... 2^31 - 1. | |
| 836 | - if (!success || std::numeric_limits<int32_t>::max() < | ||
| 837 | - token_start_internal_value_) { | ||
| 834 | + if (!bytes_read || std::numeric_limits<int32_t>::max() < | ||
| 835 | + token_start_internal_value_) { | ||
| 838 | 836 | SetError(Error::CBOR_INVALID_INT32); | |
| 839 | 837 | return; | |
| 840 | 838 | } | |
| 841 | - SetToken(CBORTokenTag::INT32, token_start_length); | ||
| 839 | + SetToken(CBORTokenTag::INT32, bytes_read); | ||
| 842 | 840 | return; | |
| 843 | 841 | case MajorType::NEGATIVE: { // INT32. | |
| 844 | 842 | // INT32 is a signed int32 (int32 makes sense for the | |
@@ -851,21 +849,20 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) { | |||
| 851 | 849 | // We check the the payload in token_start_internal_value_ against | |
| 852 | 850 | // that range (2^31-1 is also known as | |
| 853 | 851 | // std::numeric_limits<int32_t>::max()). | |
| 854 | - if (!success || token_start_internal_value_ > | ||
| 855 | - std::numeric_limits<int32_t>::max()) { | ||
| 852 | + if (!bytes_read || token_start_internal_value_ > | ||
| 853 | + std::numeric_limits<int32_t>::max()) { | ||
| 856 | 854 | SetError(Error::CBOR_INVALID_INT32); | |
| 857 | 855 | return; | |
| 858 | 856 | } | |
| 859 | - SetToken(CBORTokenTag::INT32, token_start_length); | ||
| 857 | + SetToken(CBORTokenTag::INT32, bytes_read); | ||
| 860 | 858 | return; | |
| 861 | 859 | } | |
| 862 | 860 | case MajorType::STRING: { // STRING8. | |
| 863 | - if (!success || token_start_internal_value_ > kMaxValidLength) { | ||
| 861 | + if (!bytes_read || token_start_internal_value_ > kMaxValidLength) { | ||
| 864 | 862 | SetError(Error::CBOR_INVALID_STRING8); | |
| 865 | 863 | return; | |
| 866 | 864 | } | |
| 867 | - uint64_t token_byte_length = | ||
| 868 | - token_start_internal_value_ + token_start_length; | ||
| 865 | + uint64_t token_byte_length = token_start_internal_value_ + bytes_read; | ||
| 869 | 866 | if (token_byte_length > remaining_bytes) { | |
| 870 | 867 | SetError(Error::CBOR_INVALID_STRING8); | |
| 871 | 868 | return; | |
@@ -877,13 +874,12 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) { | |||
| 877 | 874 | case MajorType::BYTE_STRING: { // STRING16. | |
| 878 | 875 | // Length must be divisible by 2 since UTF16 is 2 bytes per | |
| 879 | 876 | // character, hence the &1 check. | |
| 880 | - if (!success || token_start_internal_value_ > kMaxValidLength || | ||
| 877 | + if (!bytes_read || token_start_internal_value_ > kMaxValidLength || | ||
| 881 | 878 | token_start_internal_value_ & 1) { | |
| 882 | 879 | SetError(Error::CBOR_INVALID_STRING16); | |
| 883 | 880 | return; | |
| 884 | 881 | } | |
| 885 | - uint64_t token_byte_length = | ||
| 886 | - token_start_internal_value_ + token_start_length; | ||
| 882 | + uint64_t token_byte_length = token_start_internal_value_ + bytes_read; | ||
| 887 | 883 | if (token_byte_length > remaining_bytes) { | |
| 888 | 884 | SetError(Error::CBOR_INVALID_STRING16); | |
| 889 | 885 | return; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -427,7 +427,7 @@ Status AppendString8EntryToCBORMap(span<uint8_t> string8_key, | |||
| 427 | 427 | std::string* cbor); | |
| 428 | 428 | ||
| 429 | 429 | namespace internals { // Exposed only for writing tests. | |
| 430 | - int8_t ReadTokenStart(span<uint8_t> bytes, | ||
| 430 | + size_t ReadTokenStart(span<uint8_t> bytes, | ||
| 431 | 431 | cbor::MajorType* type, | |
| 432 | 432 | uint64_t* value); | |
| 433 | 433 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -192,11 +192,10 @@ namespace internals { | |||
| 192 | 192 | // |type| is the major type as specified in RFC 7049 Section 2.1. | |
| 193 | 193 | // |value| is the payload (e.g. for MajorType::UNSIGNED) or is the size | |
| 194 | 194 | // (e.g. for BYTE_STRING). | |
| 195 | - // If successful, returns the number of bytes read. Otherwise returns -1. | ||
| 196 | - // TODO(johannes): change return type to size_t and use 0 for error. | ||
| 197 | - int8_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) { | ||
| 195 | + // If successful, returns the number of bytes read. Otherwise returns 0. | ||
| 196 | + size_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) { | ||
| 198 | 197 | if (bytes.empty()) | |
| 199 | - return -1; | ||
| 198 | + return 0; | ||
| 200 | 199 | uint8_t initial_byte = bytes[0]; | |
| 201 | 200 | *type = MajorType((initial_byte & kMajorTypeMask) >> kMajorTypeBitShift); | |
| 202 | 201 | ||
@@ -210,32 +209,32 @@ int8_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) { | |||
| 210 | 209 | if (additional_information == kAdditionalInformation1Byte) { | |
| 211 | 210 | // Values 24-255 are encoded with one initial byte, followed by the value. | |
| 212 | 211 | if (bytes.size() < 2) | |
| 213 | - return -1; | ||
| 212 | + return 0; | ||
| 214 | 213 | *value = ReadBytesMostSignificantByteFirst<uint8_t>(bytes.subspan(1)); | |
| 215 | 214 | return 2; | |
| 216 | 215 | } | |
| 217 | 216 | if (additional_information == kAdditionalInformation2Bytes) { | |
| 218 | 217 | // Values 256-65535: 1 initial byte + 2 bytes payload. | |
| 219 | 218 | if (bytes.size() < 1 + sizeof(uint16_t)) | |
| 220 | - return -1; | ||
| 219 | + return 0; | ||
| 221 | 220 | *value = ReadBytesMostSignificantByteFirst<uint16_t>(bytes.subspan(1)); | |
| 222 | 221 | return 3; | |
| 223 | 222 | } | |
| 224 | 223 | if (additional_information == kAdditionalInformation4Bytes) { | |
| 225 | 224 | // 32 bit uint: 1 initial byte + 4 bytes payload. | |
| 226 | 225 | if (bytes.size() < 1 + sizeof(uint32_t)) | |
| 227 | - return -1; | ||
| 226 | + return 0; | ||
| 228 | 227 | *value = ReadBytesMostSignificantByteFirst<uint32_t>(bytes.subspan(1)); | |
| 229 | 228 | return 5; | |
| 230 | 229 | } | |
| 231 | 230 | if (additional_information == kAdditionalInformation8Bytes) { | |
| 232 | 231 | // 64 bit uint: 1 initial byte + 8 bytes payload. | |
| 233 | 232 | if (bytes.size() < 1 + sizeof(uint64_t)) | |
| 234 | - return -1; | ||
| 233 | + return 0; | ||
| 235 | 234 | *value = ReadBytesMostSignificantByteFirst<uint64_t>(bytes.subspan(1)); | |
| 236 | 235 | return 9; | |
| 237 | 236 | } | |
| 238 | - return -1; | ||
| 237 | + return 0; | ||
| 239 | 238 | } | |
| 240 | 239 | ||
| 241 | 240 | // Writes the start of a token with |type|. The |value| may indicate the size, | |
@@ -777,10 +776,10 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) { | |||
| 777 | 776 | SetToken(CBORTokenTag::NULL_VALUE, 1); | |
| 778 | 777 | return; | |
| 779 | 778 | case kExpectedConversionToBase64Tag: { // BINARY | |
| 780 | - const int8_t bytes_read = internals::ReadTokenStart( | ||
| 779 | + const size_t bytes_read = internals::ReadTokenStart( | ||
| 781 | 780 | bytes_.subspan(status_.pos + 1), &token_start_type_, | |
| 782 | 781 | &token_start_internal_value_); | |
| 783 | - if (bytes_read < 0 || token_start_type_ != MajorType::BYTE_STRING || | ||
| 782 | + if (!bytes_read || token_start_type_ != MajorType::BYTE_STRING || | ||
| 784 | 783 | token_start_internal_value_ > kMaxValidLength) { | |
| 785 | 784 | SetError(Error::CBOR_INVALID_BINARY); | |
| 786 | 785 | return; | |
@@ -830,47 +829,47 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) { | |||
| 830 | 829 | return; | |
| 831 | 830 | } | |
| 832 | 831 | default: { | |
| 833 | - const int8_t token_start_length = internals::ReadTokenStart( | ||
| 832 | + const size_t bytes_read = internals::ReadTokenStart( | ||
| 834 | 833 | bytes_.subspan(status_.pos), &token_start_type_, | |
| 835 | 834 | &token_start_internal_value_); | |
| 836 | - const bool success = token_start_length >= 0; | ||
| 837 | 835 | switch (token_start_type_) { | |
| 838 | 836 | case MajorType::UNSIGNED: // INT32. | |
| 839 | 837 | // INT32 is a signed int32 (int32 makes sense for the | |
| 840 | 838 | // inspector_protocol, it's not a CBOR limitation), so we check | |
| 841 | 839 | // against the signed max, so that the allowable values are | |
| 842 | 840 | // 0, 1, 2, ... 2^31 - 1. | |
| 843 | - if (!success || std::numeric_limits<int32_t>::max() < | ||
| 844 | - token_start_internal_value_) { | ||
| 841 | + if (!bytes_read || std::numeric_limits<int32_t>::max() < | ||
| 842 | + token_start_internal_value_) { | ||
| 845 | 843 | SetError(Error::CBOR_INVALID_INT32); | |
| 846 | 844 | return; | |
| 847 | 845 | } | |
| 848 | - SetToken(CBORTokenTag::INT32, token_start_length); | ||
| 846 | + SetToken(CBORTokenTag::INT32, bytes_read); | ||
| 849 | 847 | return; | |
| 850 | 848 | case MajorType::NEGATIVE: { // INT32. | |
| 851 | 849 | // INT32 is a signed int32 (int32 makes sense for the | |
| 852 | 850 | // inspector_protocol, it's not a CBOR limitation); in CBOR, the | |
| 853 | 851 | // negative values for INT32 are represented as NEGATIVE, that is, -1 | |
| 854 | 852 | // INT32 is represented as 1 << 5 | 0 (major type 1, additional info | |
| 855 | - // value 0). The minimal allowed INT32 value in our protocol is | ||
| 856 | - // std::numeric_limits<int32_t>::min(). We check for it by directly | ||
| 857 | - // checking the payload against the maximal allowed signed (!) int32 | ||
| 858 | - // value. | ||
| 859 | - if (!success || token_start_internal_value_ > | ||
| 860 | - std::numeric_limits<int32_t>::max()) { | ||
| 853 | + // value 0). | ||
| 854 | + // The represented allowed values range is -1 to -2^31. | ||
| 855 | + // They are mapped into the encoded range of 0 to 2^31-1. | ||
| 856 | + // We check the the payload in token_start_internal_value_ against | ||
| 857 | + // that range (2^31-1 is also known as | ||
| 858 | + // std::numeric_limits<int32_t>::max()). | ||
| 859 | + if (!bytes_read || token_start_internal_value_ > | ||
| 860 | + std::numeric_limits<int32_t>::max()) { | ||
| 861 | 861 | SetError(Error::CBOR_INVALID_INT32); | |
| 862 | 862 | return; | |
| 863 | 863 | } | |
| 864 | - SetToken(CBORTokenTag::INT32, token_start_length); | ||
| 864 | + SetToken(CBORTokenTag::INT32, bytes_read); | ||
| 865 | 865 | return; | |
| 866 | 866 | } | |
| 867 | 867 | case MajorType::STRING: { // STRING8. | |
| 868 | - if (!success || token_start_internal_value_ > kMaxValidLength) { | ||
| 868 | + if (!bytes_read || token_start_internal_value_ > kMaxValidLength) { | ||
| 869 | 869 | SetError(Error::CBOR_INVALID_STRING8); | |
| 870 | 870 | return; | |
| 871 | 871 | } | |
| 872 | - uint64_t token_byte_length = | ||
| 873 | - token_start_internal_value_ + token_start_length; | ||
| 872 | + uint64_t token_byte_length = token_start_internal_value_ + bytes_read; | ||
| 874 | 873 | if (token_byte_length > remaining_bytes) { | |
| 875 | 874 | SetError(Error::CBOR_INVALID_STRING8); | |
| 876 | 875 | return; | |
@@ -882,13 +881,12 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) { | |||
| 882 | 881 | case MajorType::BYTE_STRING: { // STRING16. | |
| 883 | 882 | // Length must be divisible by 2 since UTF16 is 2 bytes per | |
| 884 | 883 | // character, hence the &1 check. | |
| 885 | - if (!success || token_start_internal_value_ > kMaxValidLength || | ||
| 884 | + if (!bytes_read || token_start_internal_value_ > kMaxValidLength || | ||
| 886 | 885 | token_start_internal_value_ & 1) { | |
| 887 | 886 | SetError(Error::CBOR_INVALID_STRING16); | |
| 888 | 887 | return; | |
| 889 | 888 | } | |
| 890 | - uint64_t token_byte_length = | ||
| 891 | - token_start_internal_value_ + token_start_length; | ||
| 889 | + uint64_t token_byte_length = token_start_internal_value_ + bytes_read; | ||
| 892 | 890 | if (token_byte_length > remaining_bytes) { | |
| 893 | 891 | SetError(Error::CBOR_INVALID_STRING16); | |
| 894 | 892 | return; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -435,7 +435,7 @@ Status AppendString8EntryToCBORMap(span<uint8_t> string8_key, | |||
| 435 | 435 | std::string* cbor); | |
| 436 | 436 | ||
| 437 | 437 | namespace internals { // Exposed only for writing tests. | |
| 438 | - int8_t ReadTokenStart(span<uint8_t> bytes, | ||
| 438 | + size_t ReadTokenStart(span<uint8_t> bytes, | ||
| 439 | 439 | cbor::MajorType* type, | |
| 440 | 440 | uint64_t* value); | |
| 441 | 441 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments