pass LDFLAGS to compiler at link time
[tennix.git] / archive.hh
blob0ec2509b5a52a87913c496b2a1457ffc96934156
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 strcpy(header.header, TENNIX_ARCHIVE_HEADER);
84 header.items = 0;
85 building = 1;
86 fp = NULL;
89 TennixArchive(const char* filename, const char* fallback=NULL);
91 ~TennixArchive() {
92 if (fp != NULL) {
93 fclose(fp);
95 free(items);
98 int setItemFilename(const char* filename);
100 const char* getItemFilename() {
101 return items[current_item].filename;
104 char* getItemBytes();
106 size_t getItemSize() {
107 return items[current_item].length;
110 int endOfFile() {
111 return current_item >= header.items;
114 void next() {
115 current_item++;
118 /* only for building/utility mode: */
119 void appendItem(char* filename, char* data, uint32_t length);
120 void buildFile(char* filename);
122 friend std::ostream& operator<<(std::ostream& out, TennixArchive& archive);
125 /* Used for dumping the contents of a TennixArchive */
126 std::ostream&
127 operator<<(std::ostream& out, TennixArchive& archive);
129 #endif