Port archiving tool to C++
[tennix.git] / archive.hh
blobc0ba7a6327d8652a07875ee258d5d2aa7a11318d
1 #ifndef __ARCHIVE_HH
2 #define __ARCHIVE_HH
4 /**
6 * Tennix Archive File Format
7 * Copyright (C) 2009-2010 Thomas Perl <thp@thpinfo.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301, USA.
24 **/
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #define TENNIX_ARCHIVE_HEADER "thpinfo.com/2009/tennix/afmt "
32 #define TENNIX_ARCHIVE_HEADER_LEN 30
34 #define TENNIX_ARCHIVE_VERSIONMAJOR 1
35 #define TENNIX_ARCHIVE_VERSIONMINOR 0
37 #define TENNIX_ARCHIVE_ITEM_MAXNAME 86
40 /* architecture-independent (in-file) structs */
42 struct _TennixArchiveItem {
43 char filename[TENNIX_ARCHIVE_ITEM_MAXNAME];
44 uint32_t offset; /* network byte order */
45 uint32_t length; /* network byte order */
46 uint8_t key;
49 typedef struct _TennixArchiveItem TennixArchiveItem;
51 struct _TennixArchiveHeader {
52 char header[TENNIX_ARCHIVE_HEADER_LEN];
53 uint8_t versionmajor; /* major file version */
54 uint8_t versionminor; /* minor file version */
55 uint8_t key;
56 uint8_t items; /* maximum 255 files per archive */
59 typedef struct _TennixArchiveHeader TennixArchiveHeader;
62 class TennixArchive {
63 private:
64 FILE* fp;
65 TennixArchiveHeader header;
66 TennixArchiveItem* items;
67 char** blobs;
68 size_t offset;
69 int current_item;
70 int building;
72 static void xormem(char* mem, uint32_t length, char key);
74 public:
75 TennixArchive() {
76 strcpy(header.header, TENNIX_ARCHIVE_HEADER);
77 header.items = 0;
78 building = 1;
79 fp = NULL;
82 TennixArchive(const char* filename);
84 ~TennixArchive() {
85 if (fp != NULL) {
86 fclose(fp);
88 free(items);
91 int setItemFilename(const char* filename);
93 const char* getItemFilename() {
94 return items[current_item].filename;
97 char* getItemBytes();
99 size_t getItemSize() {
100 return items[current_item].length;
103 int endOfFile() {
104 return current_item >= header.items;
107 void next() {
108 current_item++;
111 /* only for building/utility mode: */
112 void appendItem(char* filename, char* data, uint32_t length);
113 void buildFile(char* filename);
114 void dump();
117 #endif