1// Aseprite Document IO Library
2// Copyright (c) 2018 Igara Studio S.A.
3// Copyright (c) 2017 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#include "dio/decoder.h"
9
10#include "dio/file_interface.h"
11#include "doc/document.h"
12
13namespace dio {
14
15Decoder::Decoder()
16 : m_delegate(nullptr)
17 , m_f(nullptr)
18{
19}
20
21Decoder::~Decoder()
22{
23}
24
25void Decoder::initialize(DecodeDelegate* delegate, FileInterface* f)
26{
27 m_delegate = delegate;
28 m_f = f;
29}
30
31uint8_t Decoder::read8()
32{
33 return m_f->read8();
34}
35
36uint16_t Decoder::read16()
37{
38 int b1 = m_f->read8();
39 int b2 = m_f->read8();
40
41 if (m_f->ok()) {
42 return ((b2 << 8) | b1); // Little endian
43 }
44 else
45 return 0;
46}
47
48uint32_t Decoder::read32()
49{
50 int b1 = m_f->read8();
51 int b2 = m_f->read8();
52 int b3 = m_f->read8();
53 int b4 = m_f->read8();
54
55 if (m_f->ok()) {
56 // Little endian
57 return ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
58 }
59 else
60 return 0;
61}
62
63size_t Decoder::readBytes(uint8_t* buf, size_t n)
64{
65 return m_f->readBytes(buf, n);
66}
67
68} // namespace dio
69