summaryrefslogtreecommitdiffstats
path: root/src/parser/zip_archive_stream.cpp
blob: 776ac14856dea0fe69f619e61dfb63e1f3f4566f (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "orcus/zip_archive_stream.hpp"
#include "orcus/zip_archive.hpp"

#include <sstream>
#include <cstring>

#ifdef _MSC_VER
#define fseeko _fseeki64
#define ftello _ftelli64
#endif

using namespace std;

namespace orcus {

zip_archive_stream::~zip_archive_stream() {}

zip_archive_stream_fd::zip_archive_stream_fd(const char* filepath) :
    m_stream(fopen(filepath, "rb"))
{
    if (!m_stream)
    {
        // Fail early at instantiation time.
        ostringstream os;
        os << "failed to open " << filepath << " for reading";
        throw zip_error(os.str());
    }
}

zip_archive_stream_fd::~zip_archive_stream_fd()
{
    if (m_stream)
        fclose(m_stream);
}

size_t zip_archive_stream_fd::size() const
{
    if (fseeko(m_stream, 0, SEEK_END))
        throw zip_error("failed to set seek position to the end of stream.");

    return ftello(m_stream);
}

size_t zip_archive_stream_fd::tell() const
{
    return ftello(m_stream);
}

void zip_archive_stream_fd::read(unsigned char* buffer, size_t length) const
{
    size_t size_read = fread(buffer, 1, length, m_stream);
    if (size_read != length)
        throw zip_error("actual size read doesn't match what was expected.");
}

void zip_archive_stream_fd::seek(size_t pos)
{
    if (fseeko(m_stream, pos, SEEK_SET))
    {
        ostringstream os;
        os << "failed to set seek position to " << pos << ".";
        throw zip_error(os.str());
    }
}


zip_archive_stream_blob::zip_archive_stream_blob(const uint8_t* blob, std::size_t size) :
    m_blob(blob), m_cur(blob), m_size(size) {}

zip_archive_stream_blob::~zip_archive_stream_blob() {}

size_t zip_archive_stream_blob::size() const
{
    return m_size;
}

size_t zip_archive_stream_blob::tell() const
{
    return std::distance(m_blob, m_cur);
}

void zip_archive_stream_blob::seek(size_t pos)
{
    if (pos > m_size)
    {
        ostringstream os;
        os << "failed to seek position to " << pos << ".";
        throw zip_error(os.str());
    }
    m_cur = m_blob + pos;
}

void zip_archive_stream_blob::read(unsigned char* buffer, size_t length) const
{
    if (!length)
        return;
    // First, make sure we have enough blob to satisfy the requested stream length.
    const size_t length_available = m_size - tell();
    if (length_available < length)
        throw zip_error("There is not enough stream left to fill requested length.");

    memcpy(buffer, m_cur, length);
}

}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */