#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/process/environment_internal.h"
#include <stddef.h>
#include <vector>
#include "build/build_config.h"
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include <string.h>
#endif
#if BUILDFLAG(IS_WIN)
#include "base/check_op.h"
#endif
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) { … }
#endif
}
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
base::HeapArray<char*> AlterEnvironment(const char* const* const env,
const EnvironmentMap& changes) { … }
#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
}
}