xref: /aosp_15_r20/external/icing/icing/util/encode-util.cc (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "icing/util/encode-util.h"
16 
17 #include <cstdint>
18 #include <string>
19 #include <string_view>
20 
21 namespace icing {
22 namespace lib {
23 
24 namespace encode_util {
25 
EncodeIntToCString(uint64_t value)26 std::string EncodeIntToCString(uint64_t value) {
27   std::string encoded_str;
28   // Encode it in base128 and add 1 to make sure that there is no 0-byte. This
29   // increases the size of the encoded_str from 8-bytes to 10-bytes at worst.
30   do {
31     encoded_str.push_back((value & 0x7F) + 1);
32     value >>= 7;
33   } while (value);
34   return encoded_str;
35 }
36 
DecodeIntFromCString(std::string_view encoded_str)37 uint64_t DecodeIntFromCString(std::string_view encoded_str) {
38   uint64_t value = 0;
39   for (int i = encoded_str.length() - 1; i >= 0; --i) {
40     value <<= 7;
41     char c = encoded_str[i] - 1;
42     value |= (c & 0x7F);
43   }
44   return value;
45 }
46 
EncodeStringToCString(const std::string & input)47 std::string EncodeStringToCString(const std::string& input) {
48   std::string encoded_str;
49   // use uint32_t to store 4 bytes, using unsigned types to keep automatically
50   // remove extra left most bits.
51   uint32_t bit_buffer = 0;
52   int bit_count = 0;
53   for (unsigned char c : input) {
54     // Add the next byte to the buffer.
55     bit_buffer = (bit_buffer << 8) | c;
56     bit_count += 8;
57     // Encode the first 7 bits of the byte buffer by adding 1 in front.
58     while (bit_count >= 7) {
59       bit_count -= 7;
60       encoded_str += ((bit_buffer >> bit_count) & 0x7F) | 0x80;
61     }
62   }
63   // Encode the remaining byte.
64   if (bit_count > 0) {
65     encoded_str += ((bit_buffer << (7 - bit_count)) & 0x7F) | 0x80;
66   }
67   return encoded_str;
68 }
69 
70 }  // namespace encode_util
71 
72 }  // namespace lib
73 }  // namespace icing
74