| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/android/string_message_codec.h" | 5 #include "content/browser/android/string_message_codec.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 | 10 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 memcpy(destination, &buffer[0], buffer.size()); | 107 memcpy(destination, &buffer[0], buffer.size()); |
| 108 return result; | 108 return result; |
| 109 } | 109 } |
| 110 | 110 |
| 111 bool DecodeStringMessage(const base::string16& encoded_data, | 111 bool DecodeStringMessage(const base::string16& encoded_data, |
| 112 base::string16* result) { | 112 base::string16* result) { |
| 113 size_t num_bytes = encoded_data.size() * 2; | 113 size_t num_bytes = encoded_data.size() * 2; |
| 114 | 114 |
| 115 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&encoded_data[0]); | 115 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&encoded_data[0]); |
| 116 const uint8_t* end = ptr + num_bytes; | 116 const uint8_t* end = ptr + num_bytes; |
| 117 uint8_t tag; |
| 117 | 118 |
| 118 uint8_t tag; | 119 // Discard any leading version and padding tags. |
| 119 if (!ReadUint8(&ptr, end, &tag) || tag != kVersionTag) | 120 // There may be more than one version, due to Blink and V8 having separate |
| 120 return false; | 121 // version tags. |
| 121 | |
| 122 uint32_t version; | |
| 123 if (!ReadUint32(&ptr, end, &version)) | |
| 124 return false; | |
| 125 | |
| 126 do { | 122 do { |
| 127 if (!ReadUint8(&ptr, end, &tag)) | 123 if (!ReadUint8(&ptr, end, &tag)) |
| 128 return false; | 124 return false; |
| 129 } while (tag == kPaddingTag); | 125 uint32_t version; |
| 126 if (tag == kVersionTag && !ReadUint32(&ptr, end, &version)) |
| 127 return false; |
| 128 } while (tag == kVersionTag || tag == kPaddingTag); |
| 130 | 129 |
| 131 switch (tag) { | 130 switch (tag) { |
| 132 case kOneByteStringTag: { | 131 case kOneByteStringTag: { |
| 133 uint32_t num_bytes; | 132 uint32_t num_bytes; |
| 134 if (!ReadUint32(&ptr, end, &num_bytes)) | 133 if (!ReadUint32(&ptr, end, &num_bytes)) |
| 135 return false; | 134 return false; |
| 136 result->assign(reinterpret_cast<const char*>(ptr), | 135 result->assign(reinterpret_cast<const char*>(ptr), |
| 137 reinterpret_cast<const char*>(ptr) + num_bytes); | 136 reinterpret_cast<const char*>(ptr) + num_bytes); |
| 138 return true; | 137 return true; |
| 139 } | 138 } |
| 140 case kTwoByteStringTag: { | 139 case kTwoByteStringTag: { |
| 141 uint32_t num_bytes; | 140 uint32_t num_bytes; |
| 142 if (!ReadUint32(&ptr, end, &num_bytes)) | 141 if (!ReadUint32(&ptr, end, &num_bytes)) |
| 143 return false; | 142 return false; |
| 144 result->assign(reinterpret_cast<const base::char16*>(ptr), num_bytes / 2); | 143 result->assign(reinterpret_cast<const base::char16*>(ptr), num_bytes / 2); |
| 145 return true; | 144 return true; |
| 146 } | 145 } |
| 147 } | 146 } |
| 148 | 147 |
| 149 DLOG(WARNING) << "Unexpected tag: " << tag; | 148 DLOG(WARNING) << "Unexpected tag: " << tag; |
| 150 return false; | 149 return false; |
| 151 } | 150 } |
| 152 | 151 |
| 153 } // namespace content | 152 } // namespace content |
| OLD | NEW |