long_string_dictionary.cc
1 // Copyright 2017 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #ifdef HAVE_CONFIG_H 30 #include <config.h> // Must come first 31 #endif 32 33 #include "common/long_string_dictionary.h" 34 35 #include <assert.h> 36 #include <string.h> 37 38 #include <algorithm> 39 #include <string> 40 41 #include "common/simple_string_dictionary.h" 42 43 #define arraysize(f) (sizeof(f) / sizeof(*f)) 44 45 namespace { 46 // Suffixes for segment keys. 47 const char* const kSuffixes[] = {"__1", "__2", "__3", "__4", "__5", "__6", 48 "__7", "__8", "__9", "__10"}; 49 #if !defined(NDEBUG) 50 // The maximum suffix string length. 51 const size_t kMaxSuffixLength = 4; 52 #endif 53 } // namespace 54 55 namespace google_breakpad { 56 57 using std::string; 58 59 void LongStringDictionary::SetKeyValue(const char* key, const char* value) { 60 assert(key); 61 if (!key) 62 return; 63 64 RemoveKey(key); 65 66 if (!value) { 67 return; 68 } 69 70 // Key must not be an empty string. 71 assert(key[0] != '\0'); 72 if (key[0] == '\0') 73 return; 74 75 // If the value is not valid for segmentation, forwards the key and the value 76 // to SetKeyValue of SimpleStringDictionary and returns. 77 size_t value_length = strlen(value); 78 if (value_length <= (value_size - 1)) { 79 SimpleStringDictionary::SetKeyValue(key, value); 80 return; 81 } 82 83 size_t key_length = strlen(key); 84 assert(key_length + kMaxSuffixLength <= (key_size - 1)); 85 86 char segment_key[key_size]; 87 char segment_value[value_size]; 88 89 strcpy(segment_key, key); 90 91 const char* remain_value = value; 92 size_t remain_value_length = strlen(value); 93 94 for (unsigned long i = 0; i < arraysize(kSuffixes); i++) { 95 if (remain_value_length == 0) { 96 return; 97 } 98 99 strcpy(segment_key + key_length, kSuffixes[i]); 100 101 size_t segment_value_length = 102 std::min(remain_value_length, value_size - 1); 103 104 strncpy(segment_value, remain_value, segment_value_length); 105 segment_value[segment_value_length] = '\0'; 106 107 remain_value += segment_value_length; 108 remain_value_length -= segment_value_length; 109 110 SimpleStringDictionary::SetKeyValue(segment_key, segment_value); 111 } 112 } 113 114 bool LongStringDictionary::RemoveKey(const char* key) { 115 assert(key); 116 if (!key) 117 return false; 118 119 if (SimpleStringDictionary::RemoveKey(key)) { 120 return true; 121 } 122 123 size_t key_length = strlen(key); 124 assert(key_length + kMaxSuffixLength <= (key_size - 1)); 125 126 char segment_key[key_size]; 127 strcpy(segment_key, key); 128 129 unsigned long i = 0; 130 for (; i < arraysize(kSuffixes); i++) { 131 strcpy(segment_key + key_length, kSuffixes[i]); 132 if (!SimpleStringDictionary::RemoveKey(segment_key)) { 133 break; 134 } 135 } 136 return i != 0; 137 } 138 139 const string LongStringDictionary::GetValueForKey(const char* key) const { 140 assert(key); 141 if (!key) 142 return ""; 143 144 // Key must not be an empty string. 145 assert(key[0] != '\0'); 146 if (key[0] == '\0') 147 return ""; 148 149 const char* value = SimpleStringDictionary::GetValueForKey(key); 150 if (value) 151 return string(value); 152 153 size_t key_length = strlen(key); 154 assert(key_length + kMaxSuffixLength <= (key_size - 1)); 155 156 bool found_segment = false; 157 char segment_key[key_size]; 158 string return_value; 159 160 strcpy(segment_key, key); 161 for (unsigned long i = 0; i < arraysize(kSuffixes); i++) { 162 strcpy(segment_key + key_length, kSuffixes[i]); 163 164 const char* segment_value = 165 SimpleStringDictionary::GetValueForKey(segment_key); 166 167 if (segment_value != NULL) { 168 found_segment = true; 169 return_value.append(segment_value); 170 } else { 171 break; 172 } 173 } 174 175 if (found_segment) { 176 return return_value; 177 } 178 return ""; 179 } 180 181 } // namespace google_breakpad