| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "input.h" |
| 16 |
| 17 | BSSL_NAMESPACE_BEGIN |
| 18 | namespace der { |
| 19 |
| 20 | std::string Input::AsString() const { return std::string(AsStringView()); } |
| 21 |
| 22 | bool operator==(Input lhs, Input rhs) { return Span(lhs) == Span(rhs); } |
| 23 |
| 24 | bool operator!=(Input lhs, Input rhs) { return !(lhs == rhs); } |
| 25 |
| 26 | ByteReader::ByteReader(Input in) : data_(in) {} |
| 27 |
| 28 | bool ByteReader::ReadByte(uint8_t *byte_p) { |
| 29 | if (!HasMore()) { |
| 30 | return false; |
| 31 | } |
| 32 | *byte_p = data_[0]; |
| 33 | Advance(1); |
| 34 | return true; |
| 35 | } |
| 36 |
| 37 | bool ByteReader::ReadBytes(size_t len, Input *out) { |
| 38 | if (len > data_.size()) { |
| 39 | return false; |
| 40 | } |
| 41 | *out = Input(data_.first(len)); |
| 42 | Advance(len); |
| 43 | return true; |
| 44 | } |
| 45 |
| 46 | // Returns whether there is any more data to be read. |
| 47 | bool ByteReader::HasMore() { return !data_.empty(); } |
| 48 |
| 49 | void ByteReader::Advance(size_t len) { |
| 50 | BSSL_CHECK(len <= data_.size()); |
| 51 | data_ = data_.subspan(len); |
| 52 | } |
| 53 |
| 54 | } // namespace der |
| 55 | BSSL_NAMESPACE_END |
| 56 |