• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium OS 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.
4 
5 #include "data_reader.h"
6 
7 #include <algorithm>
8 
9 #include "base/logging.h"
10 
11 #include "binary_data_utils.h"
12 
13 namespace quipper {
14 
ReadDataString(const size_t size,string * dest)15 bool DataReader::ReadDataString(const size_t size, string* dest) {
16   if (size == 0) {
17     dest->clear();
18     return true;
19   }
20   const size_t orig_size = dest->size();
21   dest->resize(std::max(size, orig_size));
22   bool ret = ReadData(size, &(*dest)[0]);
23   dest->resize(ret ? size : orig_size);
24   return ret;
25 }
26 
ReadDataValue(const size_t size,const string & value_name,void * dest)27 bool DataReader::ReadDataValue(const size_t size, const string& value_name,
28                                void* dest) {
29   if (ReadData(size, dest)) return true;
30   LOG(ERROR) << "Unable to read " << value_name << ". Requested " << size
31              << " bytes, " << size_ - Tell() << " bytes remaining.";
32   return false;
33 }
34 
ReadStringWithSizeFromData(string * dest)35 bool DataReader::ReadStringWithSizeFromData(string* dest) {
36   uint32_t len = 0;
37   if (!ReadUint32(&len)) {
38     LOG(ERROR) << "Could not read string length from data.";
39     return false;
40   }
41 
42   if (!ReadString(len, dest)) {
43     LOG(ERROR) << "Failed to read string from data. len: " << len;
44     return false;
45   }
46   return true;
47 }
48 
49 }  // namespace quipper
50