MCPcopy Create free account
hub / github.com/avast/retdec / readNullTerminatedAscii

Function readNullTerminatedAscii

src/utils/string.cpp:513–551  ·  view source on GitHub ↗

* @brief Read up to @a maxBytes bytes as ASCII string. * * @param[in] bytes Bytes to read from. * @param[in] bytesLen Length of @a bytes * @param[in] offset Offset in bytes. * @param[in] maxBytes Maximum of bytes to read. Zero indicates as much as possible. * @param[in] failOnExceed If string isn't null terminated until @a maxBytes, an empty string is returned * * @return Converted string in ASCII

Source from the content-addressed store, hash-verified

511* @return Converted string in ASCII.
512*/
513std::string readNullTerminatedAscii(const std::uint8_t *bytes, std::size_t bytesLen, std::size_t offset,
514 std::size_t maxBytes, bool failOnExceed)
515{
516 std::string result;
517 if (!bytes)
518 {
519 return {};
520 }
521
522 if (maxBytes == 0)
523 {
524 maxBytes = bytesLen;
525 }
526 else if (offset + maxBytes > bytesLen)
527 {
528 maxBytes = bytesLen;
529 }
530 else
531 {
532 maxBytes += offset;
533 }
534
535 std::size_t i;
536 for (i = offset; i < maxBytes; i++)
537 {
538 if (bytes[i] == '\0')
539 {
540 break;
541 }
542 result.push_back(bytes[i]);
543 }
544
545 if (i == maxBytes && failOnExceed)
546 {
547 return {};
548 }
549
550 return replaceNonprintableChars(result);
551}
552
553/**
554* @brief Trims the given string.

Calls 1

replaceNonprintableCharsFunction · 0.85

Tested by

no test coverage detected