summaryrefslogtreecommitdiffstats
path: root/kio/kio/kar.cpp
blob: 07072d0c658c7611e9567013260d58bc6194bfbd (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
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

/* This file is part of the KDE libraries
   Copyright (C) 2002 Laurence Anderson <l.d.anderson@warwick.ac.uk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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 <tqfile.h>
#include <tqdir.h>
#include <time.h>
#include <kdebug.h>
#include <tqptrlist.h>
#include <kmimetype.h>
#include <tqregexp.h>

#include "kfilterdev.h"
#include "kar.h"
//#include "klimitediodevice.h"

////////////////////////////////////////////////////////////////////////
/////////////////////////// KAr ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////

class KAr::KArPrivate
{
public:
    KArPrivate() {}
};

KAr::KAr( const TQString& filename )
    : KArchive( 0L )
{
    //kdDebug(7042) << "KAr(filename) reached." << endl;
    m_filename = filename;
    d = new KArPrivate;
    setDevice( TQT_TQIODEVICE(new TQFile( filename )) );
}

KAr::KAr( TQIODevice * dev )
    : KArchive( dev )
{
    //kdDebug(7042) << "KAr::KAr( TQIODevice * dev) reached." << endl;
    d = new KArPrivate;
}

KAr::~KAr()
{
    // mjarrett: Closes to prevent ~KArchive from aborting w/o device
    //kdDebug(7042) << "~KAr reached." << endl;
    if( isOpened() )
        close();
    if ( !m_filename.isEmpty() )
        delete device(); // we created it ourselves
    delete d;
}

bool KAr::openArchive( int mode )
{
    // Open archive

    //kdDebug(7042) << "openarchive reached." << endl;

    if ( mode == IO_WriteOnly )
        return true;
    if ( mode != IO_ReadOnly && mode != IO_ReadWrite )
    {
        kdWarning(7042) << "Unsupported mode " << mode << endl;
        return false;
    }

    TQIODevice* dev = device();
    if ( !dev )
        return false;

    char magic[8];
    dev->readBlock (magic, 8);
    if (tqstrncmp(magic, "!<arch>", 7) != 0) {
        kdWarning(7042) << "Invalid main magic" << endl;
        return false;
    }

    char *ar_longnames = 0;
    while (! dev->atEnd()) {
        TQCString ar_header;
        ar_header.resize(61);
        TQCString name;
        int date, uid, gid, mode, size;

        dev->at( dev->at() + (2 - (dev->at() % 2)) % 2 ); // Ar headers are padded to byte boundary

        if ( dev->readBlock (ar_header.data(), 60) != 60 ) { // Read ar header
            kdWarning(7042) << "Couldn't read header" << endl;
            delete[] ar_longnames;
            //return false;
            return true; // Probably EOF / trailing junk
        }

        if (ar_header.right(2) != "`\n") { // Check header magic
            kdWarning(7042) << "Invalid magic" << endl;
            delete[] ar_longnames;
            return false;
        }

        name = ar_header.mid( 0, 16 ); // Process header
        date = ar_header.mid( 16, 12 ).toInt();
        uid = ar_header.mid( 28, 6 ).toInt();
        gid = ar_header.mid( 34, 6 ).toInt();
        mode = ar_header.mid( 40, 8 ).toInt();
        size = ar_header.mid( 48, 10 ).toInt();

        bool skip_entry = false; // Deal with special entries
        if (name.mid(0, 1) == "/") {
            if (name.mid(1, 1) == "/") { // Longfilename table entry
                delete[] ar_longnames;
                ar_longnames = new char[size + 1];
                ar_longnames[size] = '\0';
                dev->readBlock (ar_longnames, size);
                skip_entry = true;
                kdDebug(7042) << "Read in longnames entry" << endl;
            } else if (name.mid(1, 1) == " ") { // Symbol table entry
                kdDebug(7042) << "Skipped symbol entry" << endl;
                dev->at( dev->at() + size );
                skip_entry = true;
            } else { // Longfilename
                kdDebug(7042) << "Longfilename #" << name.mid(1, 15).toInt() << endl;
                if (! ar_longnames) {
                    kdWarning(7042) << "Invalid longfilename reference" << endl;
                    return false;
                }
                name = &ar_longnames[name.mid(1, 15).toInt()];
                name = name.left(name.find("/"));
            }
        }
        if (skip_entry) continue;

        name = name.stripWhiteSpace(); // Process filename
        name.replace( "/", "" );
        kdDebug(7042) << "Filename: " << name << " Size: " << size << endl;

        KArchiveEntry* entry;
        entry = new KArchiveFile(this, name, mode, date, /*uid*/ 0, /*gid*/ 0, 0, dev->at(), size);
        rootDir()->addEntry(entry); // Ar files don't support directorys, so everything in root

        dev->at( dev->at() + size ); // Skip contents
    }
    delete[] ar_longnames;

    return true;
}

bool KAr::closeArchive()
{
    // Close the archive
    return true;
}

void KAr::virtual_hook( int id, void* data )
{ KArchive::virtual_hook( id, data ); }