Debian Codegraph @

Source/Core/Common/SettingsHandler.cpp

raw symbols SCIP
1 // Copyright 2012 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 // Thanks to Treeki for writing the original class - 29/01/2012
5
6 #include "Common/SettingsHandler.h"
7
8 #include <algorithm>
9 #include <cstddef>
10 #include <ctime>
11 #include <string>
12
13 #include <fmt/chrono.h>
14
15 #include "Common/CommonTypes.h"
16
17 namespace Common
18 {
19 namespace
20 {
21 // Key used to encrypt/decrypt setting.txt contents
22 constexpr u32 INITIAL_SEED = 0x73B5DBFA;
23 } // namespace
24
25 SettingsWriter::SettingsWriter() : m_buffer{}, m_position{0}, m_key{INITIAL_SEED}
26 {
27 }
28
29 const SettingsBuffer& SettingsWriter::GetBytes() const
30 {
31 return m_buffer;
32 }
33
34 std::string SettingsReader::GetValue(std::string_view key) const
35 {
36 constexpr char delim[] = "\n";
37 std::string toFind = std::string(delim).append(key).append("=");
38 size_t found = m_decoded.find(toFind);
39
40 if (found != std::string_view::npos)
41 {
42 size_t delimFound = m_decoded.find(delim, found + toFind.length());
43 if (delimFound == std::string_view::npos)
44 delimFound = m_decoded.length() - 1;
45 return m_decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
46 }
47 else
48 {
49 toFind = std::string(key).append("=");
50 found = m_decoded.find(toFind);
51 if (found == 0)
52 {
53 size_t delimFound = m_decoded.find(delim, found + toFind.length());
54 if (delimFound == std::string_view::npos)
55 delimFound = m_decoded.length() - 1;
56 return m_decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
57 }
58 }
59
60 return "";
61 }
62
63 SettingsReader::SettingsReader(const SettingsBuffer& buffer) : m_decoded{""}
64 {
65 u32 key = INITIAL_SEED;
66 for (u32 position = 0; position < buffer.size(); ++position)
67 {
68 m_decoded.push_back((u8)(buffer[position] ^ key));
69 key = (key >> 31) | (key << 1);
70 }
71
72 // The decoded data normally uses CRLF line endings, but occasionally
73 // (see the comment in WriteLine), lines can be separated by CRLFLF.
74 // To handle this, we remove every CR and treat LF as the line ending.
75 // (We ignore empty lines.)
76 std::erase(m_decoded, '\x0d');
77 }
78
79 void SettingsWriter::AddSetting(std::string_view key, std::string_view value)
80 {
81 WriteLine(fmt::format("{}={}\r\n", key, value));
82 }
83
84 void SettingsWriter::WriteLine(std::string_view str)
85 {
86 const u32 old_position = m_position;
87 const u32 old_key = m_key;
88
89 // Encode and write the line
90 for (char c : str)
91 WriteByte(c);
92
93 // If the encoded data contains a null byte, Nintendo's decoder will stop at that null byte
94 // instead of decoding all the data. To avoid this: If the data we just wrote contains
95 // a null byte, add an LF right before the line to prod the values into being different,
96 // just like Nintendo does. Due to the chosen key, LF itself never encodes into a null byte.
97 const auto begin = m_buffer.cbegin() + old_position;
98 const auto end = m_buffer.cbegin() + m_position;
99 if (std::find(begin, end, 0) != end)
100 {
101 m_key = old_key;
102 m_position = old_position;
103 WriteByte('\n');
104 WriteLine(str);
105 }
106 }
107
108 void SettingsWriter::WriteByte(u8 b)
109 {
110 if (m_position >= m_buffer.size())
111 return;
112
113 m_buffer[m_position] = b ^ m_key;
114 m_position++;
115 m_key = (m_key >> 31) | (m_key << 1);
116 }
117
118 std::string SettingsWriter::GenerateSerialNumber()
119 {
120 const std::time_t t = std::time(nullptr);
121
122 // Must be 9 characters at most; otherwise the serial number will be rejected by SDK libraries,
123 // as there is a check to ensure the string length is strictly lower than 10.
124 return fmt::format("{:09}", t % 1000000000);
125 }
126 } // namespace Common