lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#ifndef CHROMIUMOS_WIDE_PROFILING_DATA_READER_H_
#define CHROMIUMOS_WIDE_PROFILING_DATA_READER_H_
 
#include <stddef.h>
#include <stdint.h>
 
#include "binary_data_utils.h"
#include "compat/string.h"
 
namespace quipper {
 
class DataReader {
 public:
  DataReader() : is_cross_endian_(false) {}
  virtual ~DataReader() {}
 
  // Moves the data read pointer to |offset| bytes from the beginning of the
  // data.
  virtual void SeekSet(size_t offset) = 0;
 
  // Returns the position of the data read pointer, in bytes from the beginning
  // of the data.
  virtual size_t Tell() const = 0;
 
  virtual size_t size() const { return size_; }
 
  // Reads raw data into |dest|. Returns true if it managed to read |size|
  // bytes.
  virtual bool ReadData(const size_t size, void* dest) = 0;
 
  // Reads raw data into a string.
  virtual bool ReadDataString(const size_t size, string* dest);
 
  // Like ReadData(), but prints an error if it doesn't read all |size| bytes.
  virtual bool ReadDataValue(const size_t size, const string& value_name,
                             void* dest);
 
  // Read integers with endian swapping.
  bool ReadUint16(uint16_t* value) { return ReadIntValue(value); }
  bool ReadUint32(uint32_t* value) { return ReadIntValue(value); }
  bool ReadUint64(uint64_t* value) { return ReadIntValue(value); }
 
  // Read a string. Returns true if it managed to read |size| bytes (excluding
  // null terminator). The actual string may be shorter than the number of bytes
  // requested.
  virtual bool ReadString(const size_t size, string* str) = 0;
 
  // Reads a string from data into |dest| at the current offset. The string in
  // data is prefixed with a 32-bit size field. The size() of |*dest| after the
  // read will be the null-terminated string length of the underlying string
  // data, and not necessarily the same as the size field in the data.
  bool ReadStringWithSizeFromData(string* dest);
 
  bool is_cross_endian() const { return is_cross_endian_; }
 
  void set_is_cross_endian(bool value) { is_cross_endian_ = value; }
 
 protected:
  // Size of the data source.
  size_t size_;
 
 private:
  // Like ReadData(), but used specifically to read integers. Will swap byte
  // order if necessary.
  // For type-safety this one private and let public member functions call it.
  template <typename T>
  bool ReadIntValue(T* dest) {
    if (!ReadData(sizeof(T), dest)) return false;
    *dest = MaybeSwap(*dest, is_cross_endian_);
    return true;
  }
 
  // For cross-endian data reading.
  bool is_cross_endian_;
};
 
}  // namespace quipper
 
#endif  // CHROMIUMOS_WIDE_PROFILING_DATA_READER_H_