view_cache_helper.cc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/url_request/view_cache_helper.h"
  5. #include <algorithm>
  6. #include <utility>
  7. #include "base/strings/escape.h"
  8. #include "base/strings/stringprintf.h"
  9. namespace net {
  10. // static
  11. void ViewCacheHelper::HexDump(const char *buf, size_t buf_len,
  12. std::string* result) {
  13. const size_t kMaxRows = 16;
  14. int offset = 0;
  15. const unsigned char *p;
  16. while (buf_len) {
  17. base::StringAppendF(result, "%08x: ", offset);
  18. offset += kMaxRows;
  19. p = (const unsigned char *) buf;
  20. size_t i;
  21. size_t row_max = std::min(kMaxRows, buf_len);
  22. // print hex codes:
  23. for (i = 0; i < row_max; ++i)
  24. base::StringAppendF(result, "%02x ", *p++);
  25. for (i = row_max; i < kMaxRows; ++i)
  26. result->append(" ");
  27. result->append(" ");
  28. // print ASCII glyphs if possible:
  29. p = (const unsigned char *) buf;
  30. for (i = 0; i < row_max; ++i, ++p) {
  31. if (*p < 0x7F && *p > 0x1F) {
  32. base::AppendEscapedCharForHTML(*p, result);
  33. } else {
  34. result->push_back('.');
  35. }
  36. }
  37. result->push_back('\n');
  38. buf += row_max;
  39. buf_len -= row_max;
  40. }
  41. }
  42. } // namespace net.