• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_tokenizer/encode_args.h"
16 
17 #include <algorithm>
18 #include <cstring>
19 
20 #include "pw_preprocessor/compiler.h"
21 #include "pw_varint/varint.h"
22 
23 namespace pw {
24 namespace tokenizer {
25 namespace {
26 
27 // Declare the types as an enum for convenience.
28 enum class ArgType : uint8_t {
29   kInt = PW_TOKENIZER_ARG_TYPE_INT,
30   kInt64 = PW_TOKENIZER_ARG_TYPE_INT64,
31   kDouble = PW_TOKENIZER_ARG_TYPE_DOUBLE,
32   kString = PW_TOKENIZER_ARG_TYPE_STRING,
33 };
34 
EncodeInt(int value,const std::span<std::byte> & output)35 size_t EncodeInt(int value, const std::span<std::byte>& output) {
36   return varint::Encode(value, std::as_writable_bytes(output));
37 }
38 
EncodeInt64(int64_t value,const std::span<std::byte> & output)39 size_t EncodeInt64(int64_t value, const std::span<std::byte>& output) {
40   return varint::Encode(value, std::as_writable_bytes(output));
41 }
42 
EncodeFloat(float value,const std::span<std::byte> & output)43 size_t EncodeFloat(float value, const std::span<std::byte>& output) {
44   if (output.size() < sizeof(value)) {
45     return 0;
46   }
47   std::memcpy(output.data(), &value, sizeof(value));
48   return sizeof(value);
49 }
50 
EncodeString(const char * string,const std::span<std::byte> & output)51 size_t EncodeString(const char* string, const std::span<std::byte>& output) {
52   // The top bit of the status byte indicates if the string was truncated.
53   static constexpr size_t kMaxStringLength = 0x7Fu;
54 
55   if (output.empty()) {  // At least one byte is needed for the status/size.
56     return 0;
57   }
58 
59   if (string == nullptr) {
60     string = "NULL";
61   }
62 
63   // Subtract 1 to save room for the status byte.
64   const size_t max_bytes =
65       std::min(static_cast<size_t>(output.size()), kMaxStringLength) - 1;
66 
67   // Scan the string to find out how many bytes to copy.
68   size_t bytes_to_copy = 0;
69   std::byte overflow_bit = std::byte(0);
70 
71   while (string[bytes_to_copy] != '\0') {
72     if (bytes_to_copy == max_bytes) {
73       overflow_bit = std::byte('\x80');
74       break;
75     }
76     bytes_to_copy += 1;
77   }
78 
79   output[0] = static_cast<std::byte>(bytes_to_copy) | overflow_bit;
80   std::memcpy(output.data() + 1, string, bytes_to_copy);
81 
82   return bytes_to_copy + 1;  // include the status byte in the total
83 }
84 
85 }  // namespace
86 
EncodeArgs(pw_tokenizer_ArgTypes types,va_list args,std::span<std::byte> output)87 size_t EncodeArgs(pw_tokenizer_ArgTypes types,
88                   va_list args,
89                   std::span<std::byte> output) {
90   size_t arg_count = types & PW_TOKENIZER_TYPE_COUNT_MASK;
91   types >>= PW_TOKENIZER_TYPE_COUNT_SIZE_BITS;
92 
93   size_t encoded_bytes = 0;
94   while (arg_count != 0u) {
95     // How many bytes were encoded; 0 indicates that there wasn't enough space.
96     size_t argument_bytes = 0;
97 
98     switch (static_cast<ArgType>(types & 0b11u)) {
99       case ArgType::kInt:
100         argument_bytes = EncodeInt(va_arg(args, int), output);
101         break;
102       case ArgType::kInt64:
103         argument_bytes = EncodeInt64(va_arg(args, int64_t), output);
104         break;
105       case ArgType::kDouble:
106         argument_bytes =
107             EncodeFloat(static_cast<float>(va_arg(args, double)), output);
108         break;
109       case ArgType::kString:
110         argument_bytes = EncodeString(va_arg(args, const char*), output);
111         break;
112     }
113 
114     // If zero bytes were encoded, the encoding buffer is full.
115     if (argument_bytes == 0u) {
116       break;
117     }
118 
119     output = output.subspan(argument_bytes);
120     encoded_bytes += argument_bytes;
121 
122     arg_count -= 1;
123     types >>= 2;  // each argument type is encoded in two bits
124   }
125 
126   return encoded_bytes;
127 }
128 
129 }  // namespace tokenizer
130 }  // namespace pw
131