blob: 074170a6e2c33b164a28cceeed2555aa4d48c9f4 [file] [log] [blame]
Joe Onorato99598ee2019-02-11 15:55:13 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#define LOG_TAG "libprotoutil"
17
18#include <android/util/ProtoFileReader.h>
19#include <cutils/log.h>
20
21#include <cinttypes>
22#include <type_traits>
23
24#include <unistd.h>
25
26namespace android {
27namespace util {
28
29/**
30 * Get the amount of data remaining in the file in fd, or -1 if the file size can't be measured.
31 * It's not the whole file, but this allows us to skip any preamble that might have already
32 * been passed over.
33 */
34ssize_t get_file_size(int fd) {
35 off_t current = lseek(fd, 0, SEEK_CUR);
36 if (current < 0) {
37 return -1;
38 }
39 off_t end = lseek(fd, 0, SEEK_END);
40 if (end < 0) {
41 return -1;
42 }
43 off_t err = lseek(fd, current, SEEK_SET);
44 if (err < 0) {
45 ALOGW("get_file_size could do SEEK_END but not SEEK_SET. We might have skipped data.");
46 return -1;
47 }
48 return (ssize_t)(end-current);
49}
50
51// =========================================================================
52ProtoFileReader::ProtoFileReader(int fd)
53 :mFd(fd),
54 mStatus(NO_ERROR),
55 mSize(get_file_size(fd)),
56 mPos(0),
57 mOffset(0),
58 mChunkSize(sizeof(mBuffer)) {
59}
60
61ProtoFileReader::~ProtoFileReader() {
62}
63
64ssize_t
65ProtoFileReader::size() const
66{
67 return (ssize_t)mSize;
68}
69
70size_t
71ProtoFileReader::bytesRead() const
72{
73 return mPos;
74}
75
76uint8_t const*
77ProtoFileReader::readBuffer()
78{
79 return hasNext() ? mBuffer + mOffset : NULL;
80}
81
82size_t
83ProtoFileReader::currentToRead()
84{
85 return mMaxOffset - mOffset;
86}
87
88bool
89ProtoFileReader::hasNext()
90{
91 return ensure_data();
92}
93
94uint8_t
95ProtoFileReader::next()
96{
97 if (!ensure_data()) {
98 // Shouldn't get to here. Always call hasNext() before calling next().
99 return 0;
100 }
101 return mBuffer[mOffset++];
102}
103
104uint64_t
105ProtoFileReader::readRawVarint()
106{
107 uint64_t val = 0, shift = 0;
108 while (true) {
109 if (!hasNext()) {
110 ALOGW("readRawVarint() called without hasNext() called first.");
111 mStatus = NOT_ENOUGH_DATA;
112 return 0;
113 }
114 uint8_t byte = next();
115 val |= (INT64_C(0x7F) & byte) << shift;
116 if ((byte & 0x80) == 0) break;
117 shift += 7;
118 }
119 return val;
120}
121
122void
123ProtoFileReader::move(size_t amt)
124{
125 while (mStatus == NO_ERROR && amt > 0) {
126 if (!ensure_data()) {
127 return;
128 }
129 const size_t chunk = mMaxOffset - mOffset < amt ? amt : mMaxOffset - mOffset;
130 mOffset += chunk;
131 amt -= chunk;
132 }
133}
134
135status_t
136ProtoFileReader::getError() const {
137 return mStatus;
138}
139
140bool
141ProtoFileReader::ensure_data() {
142 if (mStatus != NO_ERROR) {
143 return false;
144 }
145 if (mOffset < mMaxOffset) {
146 return true;
147 }
148 ssize_t amt = TEMP_FAILURE_RETRY(read(mFd, mBuffer, mChunkSize));
149 if (amt == 0) {
150 return false;
151 } else if (amt < 0) {
152 mStatus = -errno;
153 return false;
154 } else {
155 mOffset = 0;
156 mMaxOffset = amt;
157 return true;
158 }
159}
160
161
162} // util
163} // android
164