1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "net/url_request/view_cache_helper.h"
11
12 #include <algorithm>
13 #include <utility>
14
15 #include "base/strings/escape.h"
16 #include "base/strings/stringprintf.h"
17
18 namespace net {
19
20 // static
HexDump(const char * buf,size_t buf_len,std::string * result)21 void ViewCacheHelper::HexDump(const char *buf, size_t buf_len,
22 std::string* result) {
23 const size_t kMaxRows = 16;
24 int offset = 0;
25
26 const unsigned char *p;
27 while (buf_len) {
28 base::StringAppendF(result, "%08x: ", offset);
29 offset += kMaxRows;
30
31 p = (const unsigned char *) buf;
32
33 size_t i;
34 size_t row_max = std::min(kMaxRows, buf_len);
35
36 // print hex codes:
37 for (i = 0; i < row_max; ++i)
38 base::StringAppendF(result, "%02x ", *p++);
39 for (i = row_max; i < kMaxRows; ++i)
40 result->append(" ");
41 result->append(" ");
42
43 // print ASCII glyphs if possible:
44 p = (const unsigned char *) buf;
45 for (i = 0; i < row_max; ++i, ++p) {
46 if (*p < 0x7F && *p > 0x1F) {
47 base::AppendEscapedCharForHTML(*p, result);
48 } else {
49 result->push_back('.');
50 }
51 }
52
53 result->push_back('\n');
54
55 buf += row_max;
56 buf_len -= row_max;
57 }
58 }
59
60 } // namespace net.
61