File: tarinputstream.cpp

package info (click to toggle)
strigi 0.7.8-1.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,556 kB
  • ctags: 6,064
  • sloc: cpp: 41,963; ansic: 1,199; perl: 483; java: 367; python: 345; xml: 177; sh: 150; makefile: 27
file content (226 lines) | stat: -rw-r--r-- 6,660 bytes parent folder | download | duplicates (2)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* This file is part of Strigi Desktop Search
 *
 * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include <strigi/tarinputstream.h>
#include <strigi/strigiconfig.h>
#include <strigi/subinputstream.h>
#include <cstring>
#include <iostream>

using namespace Strigi;
using namespace std;

TarInputStream::TarInputStream(InputStream* input)
        : SubStreamProvider(input) {
}

TarInputStream::~TarInputStream() {
}
InputStream*
TarInputStream::nextEntry() {
    if (m_status) return 0;
    if (m_entrystream) {
        m_entrystream->skip(m_entrystream->size());
        m_input->skip(numPaddingBytes);
        delete m_entrystream;
        m_entrystream = 0;
    }
    parseHeader();
    if (m_status) return 0;
    if (m_entryinfo.size >= 0) {
        m_entrystream = new SubInputStream(m_input, m_entryinfo.size);
    }
    return m_entrystream;
}
const char*
TarInputStream::readHeader() {
    // read the first 512 characters
    const char *begin;
    int32_t nread = m_input->read(begin, 512, 512);
    if (nread == 0 || m_input->status() == Eof) {
        m_status = Eof;
    } else if (nread != 512) {
        m_status = Error;
    }
    return begin;
}
bool
TarInputStream::checkHeader(const char* h, int32_t hsize) {
    if (hsize < 257) {
        // header is too small to check
        return false;
    }
    // the file starts with a filename of at most 100 characters. The filename
    // is ended by a \0, after this \0 only \0 is allowed
    int p = 0;
    while (p < 100 && h[p] != '\0') ++p;

    while (p < 100) {
        if (h[p++] != '\0') {
            return false;
        }
    }
    bool justZeroes = true;
    while (p < 256 && justZeroes) {
        justZeroes = h[p++] == 0;
    }
    if (justZeroes) {
        return false;
    }

    // check for field values that should be '\0' for the header to be a
    // tar header. Two positions are also accepted if they are ' ' because they
    return !(h[107] || h[115] || h[123] || (h[135]&&h[135]!=' ')
            || (h[147] && h[147] != ' ') || h[256]);
}
void
TarInputStream::parseHeader() {
    const char *hb;

    hb = readHeader();
    if (m_status) {
        m_status = Error;
        m_error = "Premature end of file.";
        return;
    }
    // a tar file should end with at least 2 blocks of '\0' bytes
    bool justZeroes = true;
    int i, numZeroBlocks = 0;
    for (i = 0; i < 512 && justZeroes; ++i) {
        justZeroes = hb[i] == 0;
    }
    while (justZeroes) {
        numZeroBlocks++;
        hb = readHeader();
        if (numZeroBlocks == 1 && m_status == Eof) {
            m_status = Error;
            m_error = "Premature end of file.";
            return;
        } else if (m_status == Error || m_status == Eof) {
            return;
        }
        for (i = 0; i < 512 && justZeroes; ++i) {
            justZeroes = hb[i] == 0;
        }
        if (!justZeroes) {
           m_status = Error;
           m_error = "Invalid tar file.";
           return;
        }
    }

    // check for terminators ('\0') on the first couple of fields
    if (!checkHeader(hb, 257)) {
        m_error = "Invalid tar header.\n";
        m_status = Error;
        return;
    }

    size_t len = std::strlen(hb);
    if (len == 0) {
        // ready
        m_status = Eof;
        return;
    }
    if (len > 100) len = 100;
    m_entryinfo.filename.resize(0);
    if (len == 13 && strncmp(hb, "././@LongLink", 13) == 0) {
        readLongLink(hb);
        if (m_status) return;
        hb = readHeader();
        if (m_status) return;
    } else if (len > 1 && hb[0] == '.' && hb[1] == '/') {
        // skip initial './' part of filename
        if (len == 2) { // skip entry './'
            return parseHeader();
        }
        m_entryinfo.filename.append(hb, 2, len-2);
    } else {
        m_entryinfo.filename.append(hb, 0, len);
    }

    // read the file size which is in octal format
    m_entryinfo.size = readOctalField(hb, 124);
    if (m_status) return;
    m_entryinfo.mtime = readOctalField(hb, 136);
    if (m_status) return;

    numPaddingBytes = (int32_t)(512 - m_entryinfo.size%512);
    if (numPaddingBytes == 512) {
        numPaddingBytes = 0;
    }

    len = m_entryinfo.filename.length();
    if (m_entryinfo.filename[len-1] == '/') {
        m_entryinfo.filename.resize(len-1);
    }
    // read file type
    if (hb[156] == 0 || hb[156] == '0') {
        m_entryinfo.type = EntryInfo::File;
    } else if (hb[156] == '5') {
        m_entryinfo.type = EntryInfo::Dir;
    } else {
        m_entryinfo.type = EntryInfo::Unknown;
    }
//    printf("!%s %i\n", m_entryinfo.filename.c_str(), hb[156]);
}
int32_t
TarInputStream::readOctalField(const char *b, int32_t offset) {
    int32_t val;
    int r = sscanf(b+offset, "%o", &val);
    if (r != 1) {
        m_status = Error;
        m_error = "Error reading header: octal field is not a valid integer.";
        return 0;
    }
    return val;
}
void
TarInputStream::readLongLink(const char *b) {
    int32_t toread = readOctalField(b, 124);
    int32_t left = toread%512;
    if (left) {
        left = 512 - left;
    }
    const char *begin;
    if (m_status) return;
    int32_t nread = m_input->read(begin, toread, toread);
    if (nread != toread) {
            m_status = Error;
            m_error = "Error reading LongLink: ";
            if (nread == -1) {
                m_error += m_input->error();
            } else {
                m_error += " premature end of file.";
            }
            return;
    }
    m_entryinfo.filename.append(begin, nread);

    int64_t skipped = m_input->skip(left);
    if (skipped != left) {
        m_status = Error;
        m_error = "Error reading LongLink: ";
        if (m_input->status() == Error) {
            m_error += m_input->error();
        } else {
            m_error += " premature end of file.";
        }
    }
}