Re: Get ASCII value for character when higher than 127
ssetz@wxs.nl wrote:
For a while, I thought everything worked fine, but now I'm running
into a really weird problem. In some cases, the XOR on the '\0'
character is not done correctly, with the result that I get a
different (not readable) character back in C# in stead of a '\0'. This
happens for some ending characters, not all.
[...]
string GetPwdFilePath()
{
const char* timeStringFormat = "%Y-%m-%d_%H-%M-%S";
const size_t timeStringLength = 20;
char timeString[timeStringLength] = { 0 };
time_t t = time(0);
tm *curTime = localtime(&t);
strftime(timeString, timeStringLength,
timeStringFormat, curTime);
string path("C:\\PwdSync\\pwds\\");
path.append(timeString);
path.append(".pwd");
free(timeString);
^^^^
This is serious error. You're freeing memory block, which is
allocated on stack. Remove this statement.
return path;
}
string GetLogFilePath()
{
const char* timeStringFormat = "%Y%m%d";
const size_t timeStringLength = 20;
char timeString[timeStringLength] = { 0 };
time_t t = time(0);
tm *curTime = localtime(&t);
strftime(timeString, timeStringLength,
timeStringFormat, curTime);
string path("C:\\PwdSync\\log\\pwdlog_");
path.append(timeString);
path.append(".txt");
free(timeString);
^^^^
The same error again. Remove it.
return path;
}
[...]
string ObfuscateUnicodeString(LPCWSTR pwszIn, USHORT nLen)
{
ostringstream oss;
oss << right;
int k = GetNextXORVal(0);
for(size_t i = 0; i < nLen; ++i)
{
int ch = pwszIn[i]^k;
k = GetNextXORVal(k) ;
oss << setw(7) << setfill('0') << ch;
}
string result = oss.str();
oss.clear();
^^^^^
This call is meaningless. Just return `oss.str()'.
return result;
}
[...]
void WriteLogMessage(
const string& path,
const string& logmsg,
const string username
)
{
const char* timeStringFormat = "%H:%M:%S";
const size_t timeStringLength = 20;
char timeString[timeStringLength] = { 0 };
time_t t = time(0);
tm *curTime = localtime(&t);
strftime(timeString, timeStringLength,
timeStringFormat, curTime);
ofstream outPwd(path.c_str(), ios::app);
if (!outPwd)
{
ofstream outPwd(path.c_str(), ios::out );
}
outPwd << timeString << " - " << logmsg << username << "\n";
outPwd.close();
free(timeString);
^^^^
Again, freeing wrong memory block.
}
I think that because of wrong `free' calls the process'
memory became corrupted. That's why you see weird problems.
Alex