| 6 |
| 7 | #include <stddef.h> |
| 8 |
| 9 | #include <vector> |
| 10 |
| 11 | namespace base { |
| 12 | namespace internal { |
| 13 |
| 14 | namespace { |
| 15 |
| 16 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) || defined(OS_WIN) |
| 17 | // Parses a null-terminated input string of an environment block. The key is |
| 18 | // placed into the given string, and the total length of the line, including |
| 19 | // the terminating null, is returned. |
| 20 | size_t ParseEnvLine(const NativeEnvironmentString::value_type* input, |
| 21 | NativeEnvironmentString* key) { |
| 22 | // Skip to the equals or end of the string, this is the key. |
| 23 | size_t cur = 0; |
| 24 | while (input[cur] && input[cur] != '=') |
| 25 | cur++; |
| 26 | *key = NativeEnvironmentString(&input[0], cur); |
| 27 |
| 28 | // Now just skip to the end of the string. |
| 29 | while (input[cur]) |
| 30 | cur++; |
| 31 | return cur + 1; |
| 32 | } |
| 33 | #endif |
| 34 |
| 35 | } // namespace |
| 36 |
| 37 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 38 |
| 39 | std::unique_ptr<char* []> AlterEnvironment(const char* const* const env, |
| 40 | const EnvironmentMap& changes) { |
| 41 | std::string value_storage; // Holds concatenated null-terminated strings. |
| 42 | std::vector<size_t> result_indices; // Line indices into value_storage. |
| 43 |
| 44 | // First build up all of the unchanged environment strings. These are |
| 45 | // null-terminated of the form "key=value". |
| 46 | std::string key; |
| 47 | for (size_t i = 0; env[i]; i++) { |
| 48 | size_t line_length = ParseEnvLine(env[i], &key); |
| 49 |
| 50 | // Keep only values not specified in the change vector. |
| 51 | auto found_change = changes.find(key); |
| 52 | if (found_change == changes.end()) { |
| 53 | result_indices.push_back(value_storage.size()); |
| 54 | value_storage.append(env[i], line_length); |
| 55 | } |
| 56 | } |
| 57 |
| 58 | // Now append all modified and new values. |
| 59 | for (const auto& i : changes) { |
| 60 | if (!i.second.empty()) { |
| 61 | result_indices.push_back(value_storage.size()); |
| 62 | value_storage.append(i.first); |
| 63 | value_storage.push_back('='); |
| 64 | value_storage.append(i.second); |
| 65 | value_storage.push_back(0); |
| 66 | } |
| 67 | } |
| 68 |
| 69 | size_t pointer_count_required = |
| 70 | result_indices.size() + 1 + // Null-terminated array of pointers. |
| 71 | (value_storage.size() + sizeof(char*) - 1) / sizeof(char*); // Buffer. |
| 72 | std::unique_ptr<char*[]> result(new char*[pointer_count_required]); |
| 73 |
| 74 | // The string storage goes after the array of pointers. |
| 75 | char* storage_data = |
| 76 | reinterpret_cast<char*>(&result.get()[result_indices.size() + 1]); |
| 77 | if (!value_storage.empty()) |
| 78 | memcpy(storage_data, value_storage.data(), value_storage.size()); |
| 79 |
| 80 | // Fill array of pointers at the beginning of the result. |
| 81 | for (size_t i = 0; i < result_indices.size(); i++) |
| 82 | result[i] = &storage_data[result_indices[i]]; |
| 83 | result[result_indices.size()] = 0; // Null terminator. |
| 84 |
| 85 | return result; |
| 86 | } |
| 87 |
| 88 | #elif defined(OS_WIN) |
| 89 |
| 90 | NativeEnvironmentString AlterEnvironment(const wchar_t* env, |
| 91 | const EnvironmentMap& changes) { |
| 92 | NativeEnvironmentString result; |
| 93 |
| 94 | // First build up all of the unchanged environment strings. |
| 95 | const wchar_t* ptr = env; |
| 96 | while (*ptr) { |
| 97 | std::wstring key; |
| 98 | size_t line_length = ParseEnvLine(ptr, &key); |
| 99 |
| 100 | // Keep only values not specified in the change vector. |
| 101 | if (changes.find(key) == changes.end()) { |
| 102 | result.append(ptr, line_length); |
| 103 | } |
| 104 | ptr += line_length; |
| 105 | } |
| 106 |
| 107 | // Now append all modified and new values. |
| 108 | for (const auto& i : changes) { |
| 109 | // Windows environment blocks cannot handle keys or values with NULs. |
| 110 | CHECK_EQ(std::wstring::npos, i.first.find(L'\0')); |
| 111 | CHECK_EQ(std::wstring::npos, i.second.find(L'\0')); |
| 112 | if (!i.second.empty()) { |
| 113 | result += i.first; |
| 114 | result.push_back('='); |
| 115 | result += i.second; |
| 116 | result.push_back('\0'); |
| 117 | } |
| 118 | } |
| 119 |
| 120 | // Add the terminating NUL. |
| 121 | result.push_back('\0'); |
| 122 | return result; |
| 123 | } |
| 124 |
| 125 | #endif // OS_POSIX || OS_FUCHSIA |
| 126 |
| 127 | } // namespace internal |
| 128 | } // namespace base |
| 129 |