File: archive.hh

package info (click to toggle)
tennix 1.1-3.2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 6,804 kB
  • sloc: ansic: 2,906; cpp: 2,340; python: 275; objc: 245; makefile: 127
file content (129 lines) | stat: -rw-r--r-- 3,417 bytes parent folder | download | duplicates (4)
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
#ifndef __ARCHIVE_HH
#define __ARCHIVE_HH

/**
 *
 * Tennix Archive File Format
 * Copyright (C) 2009-2010 Thomas Perl <thp@thpinfo.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 **/

#include <iostream>

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#define TENNIX_ARCHIVE_HEADER "thpinfo.com/2009/tennix/afmt "
#define TENNIX_ARCHIVE_HEADER_LEN 30

#define TENNIX_ARCHIVE_VERSIONMAJOR 1
#define TENNIX_ARCHIVE_VERSIONMINOR 0

#define TENNIX_ARCHIVE_ITEM_MAXNAME 86


/* architecture-independent (in-file) structs */

struct _TennixArchiveItem {
    char filename[TENNIX_ARCHIVE_ITEM_MAXNAME];
    uint32_t offset; /* network byte order */
    uint32_t length; /* network byte order */
    uint8_t key;
};

typedef struct _TennixArchiveItem TennixArchiveItem;

std::ostream&
operator<<(std::ostream& out, TennixArchiveItem& item);

struct _TennixArchiveHeader {
    char header[TENNIX_ARCHIVE_HEADER_LEN];
    uint8_t versionmajor; /* major file version */
    uint8_t versionminor; /* minor file version */
    uint8_t key;
    uint8_t items; /* maximum 255 files per archive */
};

typedef struct _TennixArchiveHeader TennixArchiveHeader;

std::ostream&
operator<<(std::ostream& out, TennixArchiveHeader& header);

class TennixArchive {
    private:
        FILE* fp;
        TennixArchiveHeader header;
        TennixArchiveItem* items;
        char** blobs;
        size_t offset;
        int current_item;
        int building;

        static void xormem(char* mem, uint32_t length, char key);

    public:
        TennixArchive() {
            strcpy(header.header, TENNIX_ARCHIVE_HEADER);
            header.items = 0;
            building = 1;
            fp = NULL;
        }

        TennixArchive(const char* filename, const char* fallback=NULL);

        ~TennixArchive() {
            if (fp != NULL) {
                fclose(fp);
            }
            free(items);
        }

        int setItemFilename(const char* filename);

        const char* getItemFilename() {
            return items[current_item].filename;
        }

        char* getItemBytes();

        size_t getItemSize() {
            return items[current_item].length;
        }

        int endOfFile() {
            return current_item >= header.items;
        }

        void next() {
            current_item++;
        }

        /* only for building/utility mode: */
        void appendItem(char* filename, char* data, uint32_t length);
        void buildFile(char* filename);

        friend std::ostream& operator<<(std::ostream& out, TennixArchive& archive);
};

/* Used for dumping the contents of a TennixArchive */
std::ostream&
operator<<(std::ostream& out, TennixArchive& archive);

#endif