8bb0ba797ba737eaa4fb875807a91b39d2263949
[platform/core/uifw/rive-tizen.git] / submodule / src / core / binary_reader.cpp
1 #include "core/binary_reader.hpp"
2 #include "core/reader.h"
3
4 using namespace rive;
5
6 BinaryReader::BinaryReader(uint8_t* bytes, size_t length) :
7     m_Position(bytes),
8     m_End(bytes + length),
9     m_Overflowed(false),
10     m_Length(length)
11 {
12 }
13
14 size_t BinaryReader::lengthInBytes() const { return m_Length; }
15
16 bool BinaryReader::didOverflow() const { return m_Overflowed; }
17
18 void BinaryReader::overflow()
19 {
20         m_Overflowed = true;
21         m_Position = m_End;
22 }
23
24 uint64_t BinaryReader::readVarUint()
25 {
26         uint64_t value;
27         auto readBytes = decode_uint_leb(m_Position, m_End, &value);
28         if (readBytes == 0)
29         {
30                 overflow();
31                 return 0;
32         }
33         m_Position += readBytes;
34         return value;
35 }
36
37 std::string BinaryReader::readString()
38 {
39         uint64_t length = readVarUint();
40         if (didOverflow())
41         {
42                 return std::string();
43         }
44
45         char rawValue[length];
46         auto readBytes = decode_string(length, m_Position, m_End, &rawValue[0]);
47         if (readBytes != length)
48         {
49                 overflow();
50                 return std::string();
51         }
52         m_Position += readBytes;
53         return std::string(rawValue);
54 }
55
56 double BinaryReader::readFloat64()
57 {
58         double value;
59         auto readBytes = decode_double(m_Position, m_End, &value);
60         if (readBytes == 0)
61         {
62                 overflow();
63                 return 0.0;
64         }
65         m_Position += readBytes;
66         return value;
67 }
68
69 float BinaryReader::readFloat32()
70 {
71         float value;
72         auto readBytes = decode_float(m_Position, m_End, &value);
73         if (readBytes == 0)
74         {
75                 overflow();
76                 return 0.0f;
77         }
78         m_Position += readBytes;
79         return value;
80 }
81
82 uint8_t BinaryReader::readByte()
83 {
84         if (m_End - m_Position < 1)
85         {
86                 overflow();
87                 return 0;
88         }
89         return *m_Position++;
90 }
91
92 uint32_t BinaryReader::readUint32() 
93 {
94         uint32_t value;
95         auto readBytes = decode_uint_32(m_Position, m_End, &value);
96         if (readBytes == 0)
97         {
98                 overflow();
99                 return 0;
100         }
101         m_Position += readBytes;
102         return value;
103 }