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.
5 #ifndef NET_QUIC_QUIC_DATA_READER_H_
6 #define NET_QUIC_QUIC_DATA_READER_H_
10 #include "base/basictypes.h"
11 #include "base/strings/string_piece.h"
12 #include "net/base/int128.h"
13 #include "net/base/net_export.h"
17 // Used for reading QUIC data. Though there isn't really anything terribly
18 // QUIC-specific here, it's a helper class that's useful when doing QUIC
21 // To use, simply construct a QuicDataReader using the underlying buffer that
22 // you'd like to read fields from, then call one of the Read*() methods to
23 // actually do some reading.
25 // This class keeps an internal iterator to keep track of what's already been
26 // read and each successive Read*() call automatically increments said iterator
27 // on success. On failure, internal state of the QuicDataReader should not be
28 // trusted and it is up to the caller to throw away the failed instance and
29 // handle the error as appropriate. None of the Read*() methods should ever be
30 // called after failure, as they will also fail immediately.
31 class NET_EXPORT_PRIVATE QuicDataReader
{
33 // Caller must provide an underlying buffer to work on.
34 QuicDataReader(const char* data
, const size_t len
);
39 // Reads a 16-bit unsigned integer into the given output parameter.
40 // Forwards the internal iterator on success.
41 // Returns true on success, false otherwise.
42 bool ReadUInt16(uint16
* result
);
44 // Reads a 32-bit unsigned integer into the given output parameter.
45 // Forwards the internal iterator on success.
46 // Returns true on success, false otherwise.
47 bool ReadUInt32(uint32
* result
);
49 // Reads a 64-bit unsigned integer into the given output parameter.
50 // Forwards the internal iterator on success.
51 // Returns true on success, false otherwise.
52 bool ReadUInt64(uint64
* result
);
54 // Reads a 16-bit unsigned float into the given output parameter.
55 // Forwards the internal iterator on success.
56 // Returns true on success, false otherwise.
57 bool ReadUFloat16(uint64
* result
);
59 // Reads a string prefixed with 16-bit length into the given output parameter.
61 // NOTE: Does not copy but rather references strings in the underlying buffer.
62 // This should be kept in mind when handling memory management!
64 // Forwards the internal iterator on success.
65 // Returns true on success, false otherwise.
66 bool ReadStringPiece16(base::StringPiece
* result
);
68 // Reads a given number of bytes into the given buffer. The buffer
69 // must be of adequate size.
70 // Forwards the internal iterator on success.
71 // Returns true on success, false otherwise.
72 bool ReadStringPiece(base::StringPiece
* result
, size_t len
);
74 // Returns the remaining payload as a StringPiece.
76 // NOTE: Does not copy but rather references strings in the underlying buffer.
77 // This should be kept in mind when handling memory management!
79 // Forwards the internal iterator.
80 base::StringPiece
ReadRemainingPayload();
82 // Returns the remaining payload as a StringPiece.
84 // NOTE: Does not copy but rather references strings in the underlying buffer.
85 // This should be kept in mind when handling memory management!
87 // DOES NOT forward the internal iterator.
88 base::StringPiece
PeekRemainingPayload();
90 // Reads a given number of bytes into the given buffer. The buffer
91 // must be of adequate size.
92 // Forwards the internal iterator on success.
93 // Returns true on success, false otherwise.
94 bool ReadBytes(void* result
, size_t size
);
96 // Returns true if the entirety of the underlying buffer has been read via
98 bool IsDoneReading() const;
100 // Returns the number of bytes remaining to be read.
101 size_t BytesRemaining() const;
104 // Returns true if the underlying buffer has enough room to read the given
106 bool CanRead(size_t bytes
) const;
108 // To be called when a read fails for any reason.
111 // The data buffer that we're reading from.
114 // The length of the data buffer that we're reading from.
117 // The location of the next read from our data buffer.
120 DISALLOW_COPY_AND_ASSIGN(QuicDataReader
);
125 #endif // NET_QUIC_QUIC_DATA_READER_H_