Refactor PickleIterator.

Remove undefined behaviors by using offsets instead of pointers
and by checking that offset never exceeds the size of the data.

Update Native Messaging test to use Native Messaging format
instead of Pickle format which accidentally coincided before.

BUG=none

Review URL: https://codereview.chromium.org/290173008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274367 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/pickle.h b/base/pickle.h
index dd34f54..70b8eef 100644
--- a/base/pickle.h
+++ b/base/pickle.h
@@ -20,13 +20,14 @@
 // while the PickleIterator object is in use.
 class BASE_EXPORT PickleIterator {
  public:
-  PickleIterator() : read_ptr_(NULL), read_end_ptr_(NULL) {}
+  PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {}
   explicit PickleIterator(const Pickle& pickle);
 
   // Methods for reading the payload of the Pickle. To read from the start of
   // the Pickle, create a PickleIterator from a Pickle. If successful, these
   // methods return true. Otherwise, false is returned to indicate that the
-  // result could not be extracted.
+  // result could not be extracted. It is not possible to read from iterator
+  // after that.
   bool ReadBool(bool* result) WARN_UNUSED_RESULT;
   bool ReadInt(int* result) WARN_UNUSED_RESULT;
   bool ReadLong(long* result) WARN_UNUSED_RESULT;
@@ -61,11 +62,15 @@
 
   // Read Type from Pickle.
   template <typename Type>
-  inline bool ReadBuiltinType(Type* result);
+  bool ReadBuiltinType(Type* result);
+
+  // Advance read_index_ but do not allow it to exceed end_index_.
+  // Keeps read_index_ aligned.
+  void Advance(size_t size);
 
   // Get read pointer for Type and advance read pointer.
   template<typename Type>
-  inline const char* GetReadPointerAndAdvance();
+  const char* GetReadPointerAndAdvance();
 
   // Get read pointer for |num_bytes| and advance read pointer. This method
   // checks num_bytes for negativity and wrapping.
@@ -73,12 +78,12 @@
 
   // Get read pointer for (num_elements * size_element) bytes and advance read
   // pointer. This method checks for int overflow, negativity and wrapping.
-  inline const char* GetReadPointerAndAdvance(int num_elements,
-                                              size_t size_element);
+  const char* GetReadPointerAndAdvance(int num_elements,
+                                       size_t size_element);
 
-  // Pointers to the Pickle data.
-  const char* read_ptr_;
-  const char* read_end_ptr_;
+  const char* payload_;  // Start of our pickle's payload.
+  size_t read_index_;  // Offset of the next readable byte in payload.
+  size_t end_index_;  // Payload size.
 
   FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance);
 };
@@ -277,7 +282,9 @@
   }
 
   // The payload is the pickle data immediately following the header.
-  size_t payload_size() const { return header_->payload_size; }
+  size_t payload_size() const {
+    return header_ ? header_->payload_size : 0;
+  }
 
   const char* payload() const {
     return reinterpret_cast<const char*>(header_) + header_size_;