Version 1.2.0 with new build/configure system
[tennix.git] / src / archive.h
blobe2b5ab4bca33797e5a7a5ea39fe4272a30401bea
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 <iostream>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #define TENNIX_ARCHIVE_HEADER "thpinfo.com/2009/tennix/afmt "
34 #define TENNIX_ARCHIVE_HEADER_LEN 30
36 #define TENNIX_ARCHIVE_VERSIONMAJOR 1
37 #define TENNIX_ARCHIVE_VERSIONMINOR 0
39 #define TENNIX_ARCHIVE_ITEM_MAXNAME 86
42 /* architecture-independent (in-file) structs */
44 struct _TennixArchiveItem {
45 char filename[TENNIX_ARCHIVE_ITEM_MAXNAME];
46 uint32_t offset; /* network byte order */
47 uint32_t length; /* network byte order */
48 uint8_t key;
51 typedef struct _TennixArchiveItem TennixArchiveItem;
53 std::ostream&
54 operator<<(std::ostream& out, TennixArchiveItem& item);
56 struct _TennixArchiveHeader {
57 char header[TENNIX_ARCHIVE_HEADER_LEN];
58 uint8_t versionmajor; /* major file version */
59 uint8_t versionminor; /* minor file version */
60 uint8_t key;
61 uint8_t items; /* maximum 255 files per archive */
64 typedef struct _TennixArchiveHeader TennixArchiveHeader;
66 std::ostream&
67 operator<<(std::ostream& out, TennixArchiveHeader& header);
69 class TennixArchive {
70 private:
71 FILE* fp;
72 TennixArchiveHeader header;
73 TennixArchiveItem* items;
74 char** blobs;
75 size_t offset;
76 int current_item;
77 int building;
79 static void xormem(char* mem, uint32_t length, char key);
81 public:
82 TennixArchive();
83 TennixArchive(const char* filename, const char* fallback=NULL);
85 ~TennixArchive() {
86 if (fp != NULL) {
87 fclose(fp);
89 free(items);
92 int setItemFilename(const char* filename);
94 const char* getItemFilename() {
95 return items[current_item].filename;
98 char* getItemBytes();
100 size_t getItemSize() {
101 return items[current_item].length;
104 int endOfFile() {
105 return current_item >= header.items;
108 void next() {
109 current_item++;
112 /* only for building/utility mode: */
113 void appendItem(char* filename, char* data, uint32_t length);
114 void buildFile(char* filename);
116 friend std::ostream& operator<<(std::ostream& out, TennixArchive& archive);
119 /* Used for dumping the contents of a TennixArchive */
120 std::ostream&
121 operator<<(std::ostream& out, TennixArchive& archive);
123 #endif