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 #include "net/url_request/view_cache_helper.h"
6
7 #include <algorithm>
8 #include <utility>
9
10 #include "base/strings/escape.h"
11 #include "base/strings/stringprintf.h"
12
13 namespace net {
14
15 // static
HexDump(const char * buf,size_t buf_len,std::string * result)16 void ViewCacheHelper::HexDump(const char *buf, size_t buf_len,
17 std::string* result) {
18 const size_t kMaxRows = 16;
19 int offset = 0;
20
21 const unsigned char *p;
22 while (buf_len) {
23 base::StringAppendF(result, "%08x: ", offset);
24 offset += kMaxRows;
25
26 p = (const unsigned char *) buf;
27
28 size_t i;
29 size_t row_max = std::min(kMaxRows, buf_len);
30
31 // print hex codes:
32 for (i = 0; i < row_max; ++i)
33 base::StringAppendF(result, "%02x ", *p++);
34 for (i = row_max; i < kMaxRows; ++i)
35 result->append(" ");
36 result->append(" ");
37
38 // print ASCII glyphs if possible:
39 p = (const unsigned char *) buf;
40 for (i = 0; i < row_max; ++i, ++p) {
41 if (*p < 0x7F && *p > 0x1F) {
42 base::AppendEscapedCharForHTML(*p, result);
43 } else {
44 result->push_back('.');
45 }
46 }
47
48 result->push_back('\n');
49
50 buf += row_max;
51 buf_len -= row_max;
52 }
53 }
54
55 } // namespace net.
56