blob: 4017979fd4fc2ea1f08b736bd262e7feffff1f08 [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),
Joe Onorato4796ae62019-04-03 16:17:16 -070058 mMaxOffset(0),
Joe Onorato99598ee2019-02-11 15:55:13 +000059 mChunkSize(sizeof(mBuffer)) {
60}
61
62ProtoFileReader::~ProtoFileReader() {
63}
64
65ssize_t
66ProtoFileReader::size() const
67{
68 return (ssize_t)mSize;
69}
70
71size_t
72ProtoFileReader::bytesRead() const
73{
74 return mPos;
75}
76
77uint8_t const*
78ProtoFileReader::readBuffer()
79{
80 return hasNext() ? mBuffer + mOffset : NULL;
81}
82
83size_t
84ProtoFileReader::currentToRead()
85{
86 return mMaxOffset - mOffset;
87}
88
89bool
90ProtoFileReader::hasNext()
91{
92 return ensure_data();
93}
94
95uint8_t
96ProtoFileReader::next()
97{
98 if (!ensure_data()) {
99 // Shouldn't get to here. Always call hasNext() before calling next().
100 return 0;
101 }
102 return mBuffer[mOffset++];
103}
104
105uint64_t
106ProtoFileReader::readRawVarint()
107{
108 uint64_t val = 0, shift = 0;
109 while (true) {
110 if (!hasNext()) {
111 ALOGW("readRawVarint() called without hasNext() called first.");
112 mStatus = NOT_ENOUGH_DATA;
113 return 0;
114 }
115 uint8_t byte = next();
116 val |= (INT64_C(0x7F) & byte) << shift;
117 if ((byte & 0x80) == 0) break;
118 shift += 7;
119 }
120 return val;
121}
122
123void
124ProtoFileReader::move(size_t amt)
125{
126 while (mStatus == NO_ERROR && amt > 0) {
127 if (!ensure_data()) {
128 return;
129 }
130 const size_t chunk = mMaxOffset - mOffset < amt ? amt : mMaxOffset - mOffset;
131 mOffset += chunk;
132 amt -= chunk;
133 }
134}
135
136status_t
137ProtoFileReader::getError() const {
138 return mStatus;
139}
140
141bool
142ProtoFileReader::ensure_data() {
143 if (mStatus != NO_ERROR) {
144 return false;
145 }
146 if (mOffset < mMaxOffset) {
147 return true;
148 }
149 ssize_t amt = TEMP_FAILURE_RETRY(read(mFd, mBuffer, mChunkSize));
150 if (amt == 0) {
151 return false;
152 } else if (amt < 0) {
153 mStatus = -errno;
154 return false;
155 } else {
156 mOffset = 0;
157 mMaxOffset = amt;
158 return true;
159 }
160}
161
162
163} // util
164} // android
165