| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_SPDY_SPDY_FRAME_READER_H_ | |
| 6 #define NET_SPDY_SPDY_FRAME_READER_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include "net/base/net_export.h" | |
| 12 #include "net/spdy/platform/api/spdy_string_piece.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 // Used for reading SPDY frames. Though there isn't really anything terribly | |
| 17 // SPDY-specific here, it's a helper class that's useful when doing SPDY | |
| 18 // framing. | |
| 19 // | |
| 20 // To use, simply construct a SpdyFramerReader using the underlying buffer that | |
| 21 // you'd like to read fields from, then call one of the Read*() methods to | |
| 22 // actually do some reading. | |
| 23 // | |
| 24 // This class keeps an internal iterator to keep track of what's already been | |
| 25 // read and each successive Read*() call automatically increments said iterator | |
| 26 // on success. On failure, internal state of the SpdyFrameReader should not be | |
| 27 // trusted and it is up to the caller to throw away the failed instance and | |
| 28 // handle the error as appropriate. None of the Read*() methods should ever be | |
| 29 // called after failure, as they will also fail immediately. | |
| 30 class NET_EXPORT_PRIVATE SpdyFrameReader { | |
| 31 public: | |
| 32 // Caller must provide an underlying buffer to work on. | |
| 33 SpdyFrameReader(const char* data, const size_t len); | |
| 34 | |
| 35 // Empty destructor. | |
| 36 ~SpdyFrameReader() {} | |
| 37 | |
| 38 // Reads an 8-bit unsigned integer into the given output parameter. | |
| 39 // Forwards the internal iterator on success. | |
| 40 // Returns true on success, false otherwise. | |
| 41 bool ReadUInt8(uint8_t* result); | |
| 42 | |
| 43 // Reads a 16-bit unsigned integer into the given output parameter. | |
| 44 // Forwards the internal iterator on success. | |
| 45 // Returns true on success, false otherwise. | |
| 46 bool ReadUInt16(uint16_t* result); | |
| 47 | |
| 48 // Reads a 32-bit unsigned integer into the given output parameter. | |
| 49 // Forwards the internal iterator on success. | |
| 50 // Returns true on success, false otherwise. | |
| 51 bool ReadUInt32(uint32_t* result); | |
| 52 | |
| 53 // Reads a 64-bit unsigned integer into the given output parameter. | |
| 54 // Forwards the internal iterator on success. | |
| 55 // Returns true on success, false otherwise. | |
| 56 bool ReadUInt64(uint64_t* result); | |
| 57 | |
| 58 // Reads a 31-bit unsigned integer into the given output parameter. This is | |
| 59 // equivalent to ReadUInt32() above except that the highest-order bit is | |
| 60 // discarded. | |
| 61 // Forwards the internal iterator (by 4B) on success. | |
| 62 // Returns true on success, false otherwise. | |
| 63 bool ReadUInt31(uint32_t* result); | |
| 64 | |
| 65 // Reads a 24-bit unsigned integer into the given output parameter. | |
| 66 // Forwards the internal iterator (by 3B) on success. | |
| 67 // Returns true on success, false otherwise. | |
| 68 bool ReadUInt24(uint32_t* result); | |
| 69 | |
| 70 // Reads a string prefixed with 16-bit length into the given output parameter. | |
| 71 // | |
| 72 // NOTE: Does not copy but rather references strings in the underlying buffer. | |
| 73 // This should be kept in mind when handling memory management! | |
| 74 // | |
| 75 // Forwards the internal iterator on success. | |
| 76 // Returns true on success, false otherwise. | |
| 77 bool ReadStringPiece16(SpdyStringPiece* result); | |
| 78 | |
| 79 // Reads a string prefixed with 32-bit length into the given output parameter. | |
| 80 // | |
| 81 // NOTE: Does not copy but rather references strings in the underlying buffer. | |
| 82 // This should be kept in mind when handling memory management! | |
| 83 // | |
| 84 // Forwards the internal iterator on success. | |
| 85 // Returns true on success, false otherwise. | |
| 86 bool ReadStringPiece32(SpdyStringPiece* result); | |
| 87 | |
| 88 // Reads a given number of bytes into the given buffer. The buffer | |
| 89 // must be of adequate size. | |
| 90 // Forwards the internal iterator on success. | |
| 91 // Returns true on success, false otherwise. | |
| 92 bool ReadBytes(void* result, size_t size); | |
| 93 | |
| 94 // Seeks a given number of bytes into the buffer from the current offset. | |
| 95 // Equivelant to an empty read. | |
| 96 // Forwards the internal iterator. | |
| 97 // Returns true on success, false otherwise. | |
| 98 bool Seek(size_t size); | |
| 99 | |
| 100 // Rewinds this reader to the beginning of the frame. | |
| 101 void Rewind() { ofs_ = 0; } | |
| 102 | |
| 103 // Returns true if the entirety of the underlying buffer has been read via | |
| 104 // Read*() calls. | |
| 105 bool IsDoneReading() const; | |
| 106 | |
| 107 // Returns the number of bytes that have been consumed by the reader so far. | |
| 108 size_t GetBytesConsumed() const { return ofs_; } | |
| 109 | |
| 110 private: | |
| 111 // Returns true if the underlying buffer has enough room to read the given | |
| 112 // amount of bytes. | |
| 113 bool CanRead(size_t bytes) const; | |
| 114 | |
| 115 // To be called when a read fails for any reason. | |
| 116 void OnFailure(); | |
| 117 | |
| 118 // The data buffer that we're reading from. | |
| 119 const char* data_; | |
| 120 | |
| 121 // The length of the data buffer that we're reading from. | |
| 122 const size_t len_; | |
| 123 | |
| 124 // The location of the next read from our data buffer. | |
| 125 size_t ofs_; | |
| 126 }; | |
| 127 | |
| 128 } // namespace net | |
| 129 | |
| 130 #endif // NET_SPDY_SPDY_FRAME_READER_H_ | |
| OLD | NEW |