xref: /aosp_15_r20/external/icing/icing/legacy/core/icing-string-util.cc (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2019 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 #include "icing/legacy/core/icing-string-util.h"
15 
16 #include <algorithm>
17 #include <cstdarg>
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstdio>
21 #include <string>
22 
23 #include "icing/legacy/portable/icing-zlib.h"
24 #include "icing/util/logging.h"
25 
26 namespace icing {
27 namespace lib {
28 
UpdateCrc32(uint32_t crc,const char * str,int len)29 uint32_t IcingStringUtil::UpdateCrc32(uint32_t crc, const char *str, int len) {
30   if (len > 0) {
31     crc = ~crc32(~crc, reinterpret_cast<const Bytef *>(str), len);
32   }
33   return crc;
34 }
35 
UpdateAtPositionCrc32(uint32_t crc,int orig_len,int offset,const char * xored_str,int len)36 uint32_t IcingStringUtil::UpdateAtPositionCrc32(uint32_t crc, int orig_len,
37                                                 int offset,
38                                                 const char *xored_str,
39                                                 int len) {
40   // For appending, use UpdateCrc32.
41   if (offset + len > orig_len) {
42     ICING_LOG(FATAL) << "offset: " << offset << " length: " << len
43                      << "original_length: " << orig_len;
44   }
45 
46   // We have CRC(A|U|B) and we want CRC(A|V|B) where U is the slice
47   // that updated to V.
48   //
49   // Then if xored_str = X = U ^ V:
50   //
51   // CRC(A|V|B) = CRC(0_lenA|X|0_lenB ^ A|U|B)
52   // = CRC(0_lenA|X|0_lenB) ^ CRC(A|U|B)
53   // = CRC_COMBINE(CRC(0_lenA), CRC_COMBINE(CRC(X), CRC(0_lenB)) ^ CRC(A|U|B)
54   //
55   // But CRC(0s) = 0, and CRC_COMBINE(0, X) = X, so this becomes
56   // = CRC_COMBINE(CRC(X), CRC(0_lenB)) ^ CRC(A|U|B)
57   uint32_t update_crc = UpdateCrc32(0, xored_str, len);
58   update_crc = crc32_combine(update_crc, 0, orig_len - (offset + len));
59   return crc ^ update_crc;
60 }
61 
SStringAppendV(std::string * strp,int bufsize,const char * fmt,va_list arglist)62 void IcingStringUtil::SStringAppendV(std::string *strp, int bufsize,
63                                      const char *fmt, va_list arglist) {
64   int capacity = bufsize;
65   if (capacity <= 0) {
66     va_list backup;
67     va_copy(backup, arglist);
68     capacity = vsnprintf(nullptr, 0, fmt, backup);
69     va_end(arglist);
70   }
71 
72   size_t start = strp->size();
73   strp->resize(strp->size() + capacity + 1);
74 
75   int written = vsnprintf(&(*strp)[start], capacity + 1, fmt, arglist);
76   va_end(arglist);
77   strp->resize(start + std::min(capacity, written));
78 }
79 
SStringAppendF(std::string * strp,int bufsize,const char * fmt,...)80 void IcingStringUtil::SStringAppendF(std::string *strp, int bufsize,
81                                      const char *fmt, ...) {
82   va_list arglist;
83   va_start(arglist, fmt);
84   SStringAppendV(strp, bufsize, fmt, arglist);
85 }
86 
StringPrintf(const char * fmt,...)87 std::string IcingStringUtil::StringPrintf(const char *fmt, ...) {
88   std::string s;
89   va_list arglist;
90   va_start(arglist, fmt);
91   SStringAppendV(&s, 0, fmt, arglist);
92   return s;
93 }
94 
95 }  // namespace lib
96 }  // namespace icing
97