1*834a2baaSAndroid Build Coastguard Worker /* 2*834a2baaSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*834a2baaSAndroid Build Coastguard Worker * 4*834a2baaSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*834a2baaSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*834a2baaSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*834a2baaSAndroid Build Coastguard Worker * 8*834a2baaSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*834a2baaSAndroid Build Coastguard Worker * 10*834a2baaSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*834a2baaSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*834a2baaSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*834a2baaSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*834a2baaSAndroid Build Coastguard Worker * limitations under the License. 15*834a2baaSAndroid Build Coastguard Worker */ 16*834a2baaSAndroid Build Coastguard Worker 17*834a2baaSAndroid Build Coastguard Worker #include "FileUtils.h" 18*834a2baaSAndroid Build Coastguard Worker 19*834a2baaSAndroid Build Coastguard Worker #include <sys/stat.h> 20*834a2baaSAndroid Build Coastguard Worker #include <cstdio> 21*834a2baaSAndroid Build Coastguard Worker #include <string> 22*834a2baaSAndroid Build Coastguard Worker #include <vector> 23*834a2baaSAndroid Build Coastguard Worker 24*834a2baaSAndroid Build Coastguard Worker #include <cutils/log.h> 25*834a2baaSAndroid Build Coastguard Worker readWholeFile(const std::string & filePath)26*834a2baaSAndroid Build Coastguard Workerstd::vector<uint8_t> readWholeFile(const std::string& filePath) { 27*834a2baaSAndroid Build Coastguard Worker FILE* fp = fopen(filePath.c_str(), "r"); 28*834a2baaSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(fp == nullptr); 29*834a2baaSAndroid Build Coastguard Worker struct stat st; 30*834a2baaSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(fstat(fileno(fp), &st) != 0); 31*834a2baaSAndroid Build Coastguard Worker 32*834a2baaSAndroid Build Coastguard Worker std::vector<uint8_t> result(st.st_size); 33*834a2baaSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(fread(result.data(), 1, st.st_size, fp) != static_cast<size_t>(st.st_size)); 34*834a2baaSAndroid Build Coastguard Worker fclose(fp); 35*834a2baaSAndroid Build Coastguard Worker return result; 36*834a2baaSAndroid Build Coastguard Worker } 37