#include "base/process/environment_internal.h"
#include <stddef.h>
#include "build/build_config.h"
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include <string.h>
#endif
#include <vector>
namespace base {
namespace internal {
namespace {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_WIN)
size_t ParseEnvLine(const NativeEnvironmentString::value_type* input,
NativeEnvironmentString* key) {
size_t cur = 0;
while (input[cur] && input[cur] != '=')
cur++;
*key = NativeEnvironmentString(&input[0], cur);
while (input[cur])
cur++;
return cur + 1;
}
#endif
}
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
std::unique_ptr<char* []> AlterEnvironment(const char* const* const env,
const EnvironmentMap& changes) {
std::string value_storage;
std::vector<size_t> result_indices;
std::string key;
for (size_t i = 0; env[i]; i++) {
size_t line_length = ParseEnvLine(env[i], &key);
auto found_change = changes.find(key);
if (found_change == changes.end()) {
result_indices.push_back(value_storage.size());
value_storage.append(env[i], line_length);
}
}
for (const auto& i : changes) {
if (!i.second.empty()) {
result_indices.push_back(value_storage.size());
value_storage.append(i.first);
value_storage.push_back('=');
value_storage.append(i.second);
value_storage.push_back(0);
}
}
size_t pointer_count_required =
result_indices.size() + 1 +
(value_storage.size() + sizeof(char*) - 1) / sizeof(char*);
std::unique_ptr<char*[]> result(new char*[pointer_count_required]);
char* storage_data =
reinterpret_cast<char*>(&result.get()[result_indices.size() + 1]);
if (!value_storage.empty())
memcpy(storage_data, value_storage.data(), value_storage.size());
for (size_t i = 0; i < result_indices.size(); i++)
result[i] = &storage_data[result_indices[i]];
result[result_indices.size()] = 0;
return result;
}
#elif BUILDFLAG(IS_WIN)
NativeEnvironmentString AlterEnvironment(const wchar_t* env,
const EnvironmentMap& changes) {
NativeEnvironmentString result;
const wchar_t* ptr = env;
while (*ptr) {
std::wstring key;
size_t line_length = ParseEnvLine(ptr, &key);
if (changes.find(key) == changes.end()) {
result.append(ptr, line_length);
}
ptr += line_length;
}
for (const auto& i : changes) {
CHECK_EQ(std::wstring::npos, i.first.find(L'\0'));
CHECK_EQ(std::wstring::npos, i.second.find(L'\0'));
if (!i.second.empty()) {
result += i.first;
result.push_back('=');
result += i.second;
result.push_back('\0');
}
}
result.push_back('\0');
return result;
}
#endif
}
}