OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "net/base/net_util.h" | 9 #include "net/base/net_util.h" |
10 #include "net/tools/dump_cache/url_to_filename_encoder.h" | 10 #include "net/tools/dump_cache/url_to_filename_encoder.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 return 1; | 24 return 1; |
25 } | 25 } |
26 | 26 |
27 #ifdef WIN32 | 27 #ifdef WIN32 |
28 #define strtoull _strtoui64 | 28 #define strtoull _strtoui64 |
29 #endif | 29 #endif |
30 | 30 |
31 // A simple parser for long long values. Returns the parsed value if a | 31 // A simple parser for long long values. Returns the parsed value if a |
32 // valid integer is found; else returns deflt | 32 // valid integer is found; else returns deflt |
33 // UInt64 and Int64 cannot handle decimal numbers with leading 0s. | 33 // UInt64 and Int64 cannot handle decimal numbers with leading 0s. |
34 uint64 ParseLeadingHex64Value(const char *str, uint64 deflt) { | 34 uint64 ParseLeadingHex64Value(const char* str, uint64 deflt) { |
35 char *error = NULL; | 35 char* error = NULL; |
36 const uint64 value = strtoull(str, &error, 16); | 36 const uint64 value = strtoull(str, &error, 16); |
37 return (error == str) ? deflt : value; | 37 return (error == str) ? deflt : value; |
38 } | 38 } |
39 | |
40 } | 39 } |
41 | 40 |
42 namespace net { | 41 namespace net { |
43 | 42 |
44 // The escape character choice is made here -- all code and tests in this | 43 // The escape character choice is made here -- all code and tests in this |
45 // directory are based off of this constant. However, our testdata | 44 // directory are based off of this constant. However, our testdata |
46 // has tons of dependencies on this, so it cannot be changed without | 45 // has tons of dependencies on this, so it cannot be changed without |
47 // re-running those tests and fixing them. | 46 // re-running those tests and fixing them. |
48 const char UrlToFilenameEncoder::kEscapeChar = ','; | 47 const char UrlToFilenameEncoder::kEscapeChar = ','; |
49 const char UrlToFilenameEncoder::kTruncationChar = '-'; | 48 const char UrlToFilenameEncoder::kTruncationChar = '-'; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 encoded_filename->append(1, dir_separator); | 161 encoded_filename->append(1, dir_separator); |
163 encoded_filename->append(segment); | 162 encoded_filename->append(segment); |
164 } | 163 } |
165 } | 164 } |
166 | 165 |
167 // Note: this decoder is not the exact inverse of the EncodeSegment above, | 166 // Note: this decoder is not the exact inverse of the EncodeSegment above, |
168 // because it does not take into account a prefix. | 167 // because it does not take into account a prefix. |
169 bool UrlToFilenameEncoder::Decode(const string& encoded_filename, | 168 bool UrlToFilenameEncoder::Decode(const string& encoded_filename, |
170 char dir_separator, | 169 char dir_separator, |
171 string* decoded_url) { | 170 string* decoded_url) { |
172 enum State { | 171 enum State { kStart, kEscape, kFirstDigit, kTruncate, kEscapeDot }; |
173 kStart, | |
174 kEscape, | |
175 kFirstDigit, | |
176 kTruncate, | |
177 kEscapeDot | |
178 }; | |
179 State state = kStart; | 172 State state = kStart; |
180 char hex_buffer[3]; | 173 char hex_buffer[3]; |
181 hex_buffer[2] = '\0'; | 174 hex_buffer[2] = '\0'; |
182 for (size_t i = 0; i < encoded_filename.size(); ++i) { | 175 for (size_t i = 0; i < encoded_filename.size(); ++i) { |
183 char ch = encoded_filename[i]; | 176 char ch = encoded_filename[i]; |
184 switch (state) { | 177 switch (state) { |
185 case kStart: | 178 case kStart: |
186 if (ch == kEscapeChar) { | 179 if (ch == kEscapeChar) { |
187 state = kEscape; | 180 state = kEscape; |
188 } else if (ch == dir_separator) { | 181 } else if (ch == dir_separator) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 | 247 |
255 // Note: We also chop paths into medium sized 'chunks'. | 248 // Note: We also chop paths into medium sized 'chunks'. |
256 // This is due to the incompetence of the windows | 249 // This is due to the incompetence of the windows |
257 // filesystem, which still hasn't figured out how | 250 // filesystem, which still hasn't figured out how |
258 // to deal with long filenames. | 251 // to deal with long filenames. |
259 int last_slash = 0; | 252 int last_slash = 0; |
260 for (size_t index = 0; index < path.length(); index++) { | 253 for (size_t index = 0; index < path.length(); index++) { |
261 char ch = path[index]; | 254 char ch = path[index]; |
262 if (ch == 0x5C) | 255 if (ch == 0x5C) |
263 last_slash = index; | 256 last_slash = index; |
264 if ((ch == 0x2D) || // hyphen | 257 if ((ch == 0x2D) || // hyphen |
265 (ch == 0x5C) || (ch == 0x5F) || // backslash, underscore | 258 (ch == 0x5C) || |
| 259 (ch == 0x5F) || // backslash, underscore |
266 ((0x30 <= ch) && (ch <= 0x39)) || // Digits [0-9] | 260 ((0x30 <= ch) && (ch <= 0x39)) || // Digits [0-9] |
267 ((0x41 <= ch) && (ch <= 0x5A)) || // Uppercase [A-Z] | 261 ((0x41 <= ch) && (ch <= 0x5A)) || // Uppercase [A-Z] |
268 ((0x61 <= ch) && (ch <= 0x7A))) { // Lowercase [a-z] | 262 ((0x61 <= ch) && (ch <= 0x7A))) { // Lowercase [a-z] |
269 output.append(&path[index], 1); | 263 output.append(&path[index], 1); |
270 } else { | 264 } else { |
271 char encoded[3]; | 265 char encoded[3]; |
272 encoded[0] = 'x'; | 266 encoded[0] = 'x'; |
273 encoded[1] = ch / 16; | 267 encoded[1] = ch / 16; |
274 encoded[1] += (encoded[1] >= 10) ? 'A' - 10 : '0'; | 268 encoded[1] += (encoded[1] >= 10) ? 'A' - 10 : '0'; |
275 encoded[2] = ch % 16; | 269 encoded[2] = ch % 16; |
276 encoded[2] += (encoded[2] >= 10) ? 'A' - 10 : '0'; | 270 encoded[2] += (encoded[2] >= 10) ? 'A' - 10 : '0'; |
277 output.append(encoded, 3); | 271 output.append(encoded, 3); |
278 } | 272 } |
279 if (index - last_slash > kMaximumSubdirectoryLength) { | 273 if (index - last_slash > kMaximumSubdirectoryLength) { |
280 #ifdef WIN32 | 274 #ifdef WIN32 |
281 char slash = '\\'; | 275 char slash = '\\'; |
282 #else | 276 #else |
283 char slash = '/'; | 277 char slash = '/'; |
284 #endif | 278 #endif |
285 output.append(&slash, 1); | 279 output.append(&slash, 1); |
286 last_slash = index; | 280 last_slash = index; |
287 } | 281 } |
288 } | 282 } |
289 return output; | 283 return output; |
290 } | 284 } |
291 | 285 |
292 } // namespace net | 286 } // namespace net |
OLD | NEW |