MCPcopy Create free account
hub / github.com/Tripwire/tripwire-open-source / InterpretEscapedString

Method InterpretEscapedString

src/twparser/parserhelper.cpp:418–521  ·  view source on GitHub ↗

converts escaped character sequences in strEscapedString into their escape characters in strInterpretedString ( e.g. turns "Hello World!\n" (literal backslash) into "Hello World! " all C++ escape sequences are recognized: (1) octal numbers \012 (1, 2, or 3 octal digits), (2) hex numbers \x2A ( 'x' followed by one or two hex digits ), (3) unicode characters \uABCD ( 'u' followed by exac

Source from the content-addressed store, hash-verified

416// (4) chars: \t, \v, \b, \r, \f, \a, \\, \?, \', and \"
417// (5) all other escaped chars are treated as if not escaped
418void cParserUtil::InterpretEscapedString(const std::string& strEscapedString, TSTRING& strInterpretedString)
419{
420 cDebug d("cParserHelper::InterpretEscapedString");
421
422 ASSERT((void*)&strEscapedString != (void*)&strInterpretedString); // don't let us read and write to same string
423
424 // The source string may contain literal multibyte characters, escaped MB characters, and escaped Unicode characters.
425 // On Unix TCHAR == char always, therefore literal and esacped mb chars can be stored in strInterpretedString
426 // directly, and everythign will be hunky dory (escased Unicode will cause an error).
427 // But on Windows we need to build an intermediate narrow string because it needs to be passed through
428 // cStringUtil::StrToTstr() to convert literal and escaped mb chars to TCHARs (which are wchar_t in this case).
429 // Escaped Unicode chars are included in this intermediate string specially encoded so that StrToTstr()
430 // leaves it alone. After we have the wide string, we convert these encoded Unicode chars into true 16 bit
431 // Unicode chars.
432
433
434 // for unix we cheat a bit and do the initial interpretation
435 // directly to the final string.
436 typedef TCHAR INTERCHAR;
437 TSTRING& strIntermediateString = strInterpretedString;
438
439 const char* pchCur = strEscapedString.c_str();
440
441 for (strIntermediateString.erase(); *pchCur != 0; pchCur++) // only single byte in this part of the policy file
442 {
443 if (*pchCur != '\\') // just regular char
444 {
445 strIntermediateString += *pchCur;
446 }
447 else // deal with escaped character sequence
448 {
449 // make sure the '\' isn't the end of the string
450 // ( if it is, the "for" will ++ pchCur, see it's 0, then break )
451 if (*(pchCur + 1))
452 {
453 int nCharsRead;
454
455 pchCur++; // go to char past '\'
456
457 if (*pchCur == 'x' &&
458 std::isxdigit<TCHAR>(
459 *(pchCur + 1),
460 std::locale())) // deal with \xXXXX where 'x' is the character 'x', and 'X' is a hex number
461 {
462 pchCur++; // go to char past 'x'
463 char cEscapedChar = static_cast<char>(util_ConvertHex(pchCur, &nCharsRead));
464 pchCur += (nCharsRead == 0) ? 0 : nCharsRead - 1; //increment pointer to last char we read
465
466 if (cEscapedChar == 0) // null characters are not allowed
467 {
468 throw eParserBadHex(cStringUtil::StrToTstr(pchCur - ((nCharsRead == 0) ? 0 : nCharsRead - 1)));
469 }
470 else
471 {
472 strIntermediateString += static_cast<INTERCHAR>(cEscapedChar);
473 }
474 }
475 else if (*pchCur == 'u') // unicode escape

Callers

nothing calls this directly

Calls 7

util_ConvertHexFunction · 0.85
util_ConvertUnicodeFunction · 0.85
util_IsOctalFunction · 0.85
util_ConvertOctalFunction · 0.85
c_strMethod · 0.80
TraceDebugMethod · 0.60

Tested by

no test coverage detected