diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CTarReader.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CTarReader.cpp | 516 |
1 files changed, 258 insertions, 258 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CTarReader.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CTarReader.cpp index bbc9a8c..01eacd4 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/CTarReader.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/CTarReader.cpp | |||
@@ -1,258 +1,258 @@ | |||
1 | // Copyright (C) 2009-2012 Gaz Davidson | 1 | // Copyright (C) 2009-2012 Gaz Davidson |
2 | // This file is part of the "Irrlicht Engine". | 2 | // This file is part of the "Irrlicht Engine". |
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | 3 | // For conditions of distribution and use, see copyright notice in irrlicht.h |
4 | 4 | ||
5 | #include "CTarReader.h" | 5 | #include "CTarReader.h" |
6 | 6 | ||
7 | #ifdef __IRR_COMPILE_WITH_TAR_ARCHIVE_LOADER_ | 7 | #ifdef __IRR_COMPILE_WITH_TAR_ARCHIVE_LOADER_ |
8 | 8 | ||
9 | #include "CFileList.h" | 9 | #include "CFileList.h" |
10 | #include "CLimitReadFile.h" | 10 | #include "CLimitReadFile.h" |
11 | #include "os.h" | 11 | #include "os.h" |
12 | #include "coreutil.h" | 12 | #include "coreutil.h" |
13 | #if !defined(_IRR_WINDOWS_CE_PLATFORM_) | 13 | #if !defined(_IRR_WINDOWS_CE_PLATFORM_) |
14 | #include "errno.h" | 14 | #include "errno.h" |
15 | #endif | 15 | #endif |
16 | 16 | ||
17 | namespace irr | 17 | namespace irr |
18 | { | 18 | { |
19 | namespace io | 19 | namespace io |
20 | { | 20 | { |
21 | 21 | ||
22 | //! Constructor | 22 | //! Constructor |
23 | CArchiveLoaderTAR::CArchiveLoaderTAR(io::IFileSystem* fs) | 23 | CArchiveLoaderTAR::CArchiveLoaderTAR(io::IFileSystem* fs) |
24 | : FileSystem(fs) | 24 | : FileSystem(fs) |
25 | { | 25 | { |
26 | #ifdef _DEBUG | 26 | #ifdef _DEBUG |
27 | setDebugName("CArchiveLoaderTAR"); | 27 | setDebugName("CArchiveLoaderTAR"); |
28 | #endif | 28 | #endif |
29 | } | 29 | } |
30 | 30 | ||
31 | 31 | ||
32 | //! returns true if the file maybe is able to be loaded by this class | 32 | //! returns true if the file maybe is able to be loaded by this class |
33 | bool CArchiveLoaderTAR::isALoadableFileFormat(const io::path& filename) const | 33 | bool CArchiveLoaderTAR::isALoadableFileFormat(const io::path& filename) const |
34 | { | 34 | { |
35 | return core::hasFileExtension(filename, "tar"); | 35 | return core::hasFileExtension(filename, "tar"); |
36 | } | 36 | } |
37 | 37 | ||
38 | //! Check to see if the loader can create archives of this type. | 38 | //! Check to see if the loader can create archives of this type. |
39 | bool CArchiveLoaderTAR::isALoadableFileFormat(E_FILE_ARCHIVE_TYPE fileType) const | 39 | bool CArchiveLoaderTAR::isALoadableFileFormat(E_FILE_ARCHIVE_TYPE fileType) const |
40 | { | 40 | { |
41 | return fileType == EFAT_TAR; | 41 | return fileType == EFAT_TAR; |
42 | } | 42 | } |
43 | 43 | ||
44 | //! Creates an archive from the filename | 44 | //! Creates an archive from the filename |
45 | /** \param file File handle to check. | 45 | /** \param file File handle to check. |
46 | \return Pointer to newly created archive, or 0 upon error. */ | 46 | \return Pointer to newly created archive, or 0 upon error. */ |
47 | IFileArchive* CArchiveLoaderTAR::createArchive(const io::path& filename, bool ignoreCase, bool ignorePaths) const | 47 | IFileArchive* CArchiveLoaderTAR::createArchive(const io::path& filename, bool ignoreCase, bool ignorePaths) const |
48 | { | 48 | { |
49 | IFileArchive *archive = 0; | 49 | IFileArchive *archive = 0; |
50 | io::IReadFile* file = FileSystem->createAndOpenFile(filename); | 50 | io::IReadFile* file = FileSystem->createAndOpenFile(filename); |
51 | 51 | ||
52 | if (file) | 52 | if (file) |
53 | { | 53 | { |
54 | archive = createArchive(file, ignoreCase, ignorePaths); | 54 | archive = createArchive(file, ignoreCase, ignorePaths); |
55 | file->drop(); | 55 | file->drop(); |
56 | } | 56 | } |
57 | 57 | ||
58 | return archive; | 58 | return archive; |
59 | } | 59 | } |
60 | 60 | ||
61 | 61 | ||
62 | //! creates/loads an archive from the file. | 62 | //! creates/loads an archive from the file. |
63 | //! \return Pointer to the created archive. Returns 0 if loading failed. | 63 | //! \return Pointer to the created archive. Returns 0 if loading failed. |
64 | IFileArchive* CArchiveLoaderTAR::createArchive(io::IReadFile* file, bool ignoreCase, bool ignorePaths) const | 64 | IFileArchive* CArchiveLoaderTAR::createArchive(io::IReadFile* file, bool ignoreCase, bool ignorePaths) const |
65 | { | 65 | { |
66 | IFileArchive *archive = 0; | 66 | IFileArchive *archive = 0; |
67 | if (file) | 67 | if (file) |
68 | { | 68 | { |
69 | file->seek(0); | 69 | file->seek(0); |
70 | archive = new CTarReader(file, ignoreCase, ignorePaths); | 70 | archive = new CTarReader(file, ignoreCase, ignorePaths); |
71 | } | 71 | } |
72 | return archive; | 72 | return archive; |
73 | } | 73 | } |
74 | 74 | ||
75 | //! Check if the file might be loaded by this class | 75 | //! Check if the file might be loaded by this class |
76 | /** Check might look into the file. | 76 | /** Check might look into the file. |
77 | \param file File handle to check. | 77 | \param file File handle to check. |
78 | \return True if file seems to be loadable. */ | 78 | \return True if file seems to be loadable. */ |
79 | bool CArchiveLoaderTAR::isALoadableFileFormat(io::IReadFile* file) const | 79 | bool CArchiveLoaderTAR::isALoadableFileFormat(io::IReadFile* file) const |
80 | { | 80 | { |
81 | // TAR files consist of blocks of 512 bytes | 81 | // TAR files consist of blocks of 512 bytes |
82 | // if it isn't a multiple of 512 then it's not a TAR file. | 82 | // if it isn't a multiple of 512 then it's not a TAR file. |
83 | if (file->getSize() % 512) | 83 | if (file->getSize() % 512) |
84 | return false; | 84 | return false; |
85 | 85 | ||
86 | file->seek(0); | 86 | file->seek(0); |
87 | 87 | ||
88 | // read header of first file | 88 | // read header of first file |
89 | STarHeader fHead; | 89 | STarHeader fHead; |
90 | file->read(&fHead, sizeof(STarHeader)); | 90 | file->read(&fHead, sizeof(STarHeader)); |
91 | 91 | ||
92 | u32 checksum = 0; | 92 | u32 checksum = 0; |
93 | sscanf(fHead.Checksum, "%o", &checksum); | 93 | sscanf(fHead.Checksum, "%o", &checksum); |
94 | 94 | ||
95 | // verify checksum | 95 | // verify checksum |
96 | 96 | ||
97 | // some old TAR writers assume that chars are signed, others assume unsigned | 97 | // some old TAR writers assume that chars are signed, others assume unsigned |
98 | // USTAR archives have a longer header, old TAR archives end after linkname | 98 | // USTAR archives have a longer header, old TAR archives end after linkname |
99 | 99 | ||
100 | u32 checksum1=0; | 100 | u32 checksum1=0; |
101 | s32 checksum2=0; | 101 | s32 checksum2=0; |
102 | 102 | ||
103 | // remember to blank the checksum field! | 103 | // remember to blank the checksum field! |
104 | memset(fHead.Checksum, ' ', 8); | 104 | memset(fHead.Checksum, ' ', 8); |
105 | 105 | ||
106 | // old header | 106 | // old header |
107 | for (u8* p = (u8*)(&fHead); p < (u8*)(&fHead.Magic[0]); ++p) | 107 | for (u8* p = (u8*)(&fHead); p < (u8*)(&fHead.Magic[0]); ++p) |
108 | { | 108 | { |
109 | checksum1 += *p; | 109 | checksum1 += *p; |
110 | checksum2 += c8(*p); | 110 | checksum2 += c8(*p); |
111 | } | 111 | } |
112 | 112 | ||
113 | if (!strncmp(fHead.Magic, "ustar", 5)) | 113 | if (!strncmp(fHead.Magic, "ustar", 5)) |
114 | { | 114 | { |
115 | for (u8* p = (u8*)(&fHead.Magic[0]); p < (u8*)(&fHead) + sizeof(fHead); ++p) | 115 | for (u8* p = (u8*)(&fHead.Magic[0]); p < (u8*)(&fHead) + sizeof(fHead); ++p) |
116 | { | 116 | { |
117 | checksum1 += *p; | 117 | checksum1 += *p; |
118 | checksum2 += c8(*p); | 118 | checksum2 += c8(*p); |
119 | } | 119 | } |
120 | } | 120 | } |
121 | return checksum1 == checksum || checksum2 == (s32)checksum; | 121 | return checksum1 == checksum || checksum2 == (s32)checksum; |
122 | } | 122 | } |
123 | 123 | ||
124 | /* | 124 | /* |
125 | TAR Archive | 125 | TAR Archive |
126 | */ | 126 | */ |
127 | CTarReader::CTarReader(IReadFile* file, bool ignoreCase, bool ignorePaths) | 127 | CTarReader::CTarReader(IReadFile* file, bool ignoreCase, bool ignorePaths) |
128 | : CFileList((file ? file->getFileName() : io::path("")), ignoreCase, ignorePaths), File(file) | 128 | : CFileList((file ? file->getFileName() : io::path("")), ignoreCase, ignorePaths), File(file) |
129 | { | 129 | { |
130 | #ifdef _DEBUG | 130 | #ifdef _DEBUG |
131 | setDebugName("CTarReader"); | 131 | setDebugName("CTarReader"); |
132 | #endif | 132 | #endif |
133 | 133 | ||
134 | if (File) | 134 | if (File) |
135 | { | 135 | { |
136 | File->grab(); | 136 | File->grab(); |
137 | 137 | ||
138 | // fill the file list | 138 | // fill the file list |
139 | populateFileList(); | 139 | populateFileList(); |
140 | 140 | ||
141 | sort(); | 141 | sort(); |
142 | } | 142 | } |
143 | } | 143 | } |
144 | 144 | ||
145 | 145 | ||
146 | CTarReader::~CTarReader() | 146 | CTarReader::~CTarReader() |
147 | { | 147 | { |
148 | if (File) | 148 | if (File) |
149 | File->drop(); | 149 | File->drop(); |
150 | } | 150 | } |
151 | 151 | ||
152 | const IFileList* CTarReader::getFileList() const | 152 | const IFileList* CTarReader::getFileList() const |
153 | { | 153 | { |
154 | return this; | 154 | return this; |
155 | } | 155 | } |
156 | 156 | ||
157 | 157 | ||
158 | u32 CTarReader::populateFileList() | 158 | u32 CTarReader::populateFileList() |
159 | { | 159 | { |
160 | STarHeader fHead; | 160 | STarHeader fHead; |
161 | Files.clear(); | 161 | Files.clear(); |
162 | 162 | ||
163 | u32 pos = 0; | 163 | u32 pos = 0; |
164 | while ( s32(pos + sizeof(STarHeader)) < File->getSize()) | 164 | while ( s32(pos + sizeof(STarHeader)) < File->getSize()) |
165 | { | 165 | { |
166 | // seek to next file header | 166 | // seek to next file header |
167 | File->seek(pos); | 167 | File->seek(pos); |
168 | 168 | ||
169 | // read the header | 169 | // read the header |
170 | File->read(&fHead, sizeof(fHead)); | 170 | File->read(&fHead, sizeof(fHead)); |
171 | 171 | ||
172 | // only add standard files for now | 172 | // only add standard files for now |
173 | if (fHead.Link == ETLI_REGULAR_FILE || ETLI_REGULAR_FILE_OLD) | 173 | if (fHead.Link == ETLI_REGULAR_FILE || ETLI_REGULAR_FILE_OLD) |
174 | { | 174 | { |
175 | io::path fullPath = ""; | 175 | io::path fullPath = ""; |
176 | fullPath.reserve(255); | 176 | fullPath.reserve(255); |
177 | 177 | ||
178 | // USTAR archives have a filename prefix | 178 | // USTAR archives have a filename prefix |
179 | // may not be null terminated, copy carefully! | 179 | // may not be null terminated, copy carefully! |
180 | if (!strncmp(fHead.Magic, "ustar", 5)) | 180 | if (!strncmp(fHead.Magic, "ustar", 5)) |
181 | { | 181 | { |
182 | c8* np = fHead.FileNamePrefix; | 182 | c8* np = fHead.FileNamePrefix; |
183 | while(*np && (np - fHead.FileNamePrefix) < 155) | 183 | while(*np && (np - fHead.FileNamePrefix) < 155) |
184 | fullPath.append(*np); | 184 | fullPath.append(*np); |
185 | np++; | 185 | np++; |
186 | } | 186 | } |
187 | 187 | ||
188 | // append the file name | 188 | // append the file name |
189 | c8* np = fHead.FileName; | 189 | c8* np = fHead.FileName; |
190 | while(*np && (np - fHead.FileName) < 100) | 190 | while(*np && (np - fHead.FileName) < 100) |
191 | { | 191 | { |
192 | fullPath.append(*np); | 192 | fullPath.append(*np); |
193 | np++; | 193 | np++; |
194 | } | 194 | } |
195 | 195 | ||
196 | // get size | 196 | // get size |
197 | core::stringc sSize = ""; | 197 | core::stringc sSize = ""; |
198 | sSize.reserve(12); | 198 | sSize.reserve(12); |
199 | np = fHead.Size; | 199 | np = fHead.Size; |
200 | while(*np && (np - fHead.Size) < 12) | 200 | while(*np && (np - fHead.Size) < 12) |
201 | { | 201 | { |
202 | sSize.append(*np); | 202 | sSize.append(*np); |
203 | np++; | 203 | np++; |
204 | } | 204 | } |
205 | 205 | ||
206 | u32 size = strtoul(sSize.c_str(), NULL, 8); | 206 | u32 size = strtoul(sSize.c_str(), NULL, 8); |
207 | #if !defined(_IRR_WINDOWS_CE_PLATFORM_) | 207 | #if !defined(_IRR_WINDOWS_CE_PLATFORM_) |
208 | if (errno == ERANGE) | 208 | if (errno == ERANGE) |
209 | os::Printer::log("File too large", fullPath, ELL_WARNING); | 209 | os::Printer::log("File too large", fullPath, ELL_WARNING); |
210 | #endif | 210 | #endif |
211 | 211 | ||
212 | // save start position | 212 | // save start position |
213 | u32 offset = pos + 512; | 213 | u32 offset = pos + 512; |
214 | 214 | ||
215 | // move to next file header block | 215 | // move to next file header block |
216 | pos = offset + (size / 512) * 512 + ((size % 512) ? 512 : 0); | 216 | pos = offset + (size / 512) * 512 + ((size % 512) ? 512 : 0); |
217 | 217 | ||
218 | // add file to list | 218 | // add file to list |
219 | addItem(fullPath, offset, size, false ); | 219 | addItem(fullPath, offset, size, false ); |
220 | } | 220 | } |
221 | else | 221 | else |
222 | { | 222 | { |
223 | // todo: ETLI_DIRECTORY, ETLI_LINK_TO_ARCHIVED_FILE | 223 | // todo: ETLI_DIRECTORY, ETLI_LINK_TO_ARCHIVED_FILE |
224 | 224 | ||
225 | // move to next block | 225 | // move to next block |
226 | pos += 512; | 226 | pos += 512; |
227 | } | 227 | } |
228 | 228 | ||
229 | } | 229 | } |
230 | 230 | ||
231 | return Files.size(); | 231 | return Files.size(); |
232 | } | 232 | } |
233 | 233 | ||
234 | //! opens a file by file name | 234 | //! opens a file by file name |
235 | IReadFile* CTarReader::createAndOpenFile(const io::path& filename) | 235 | IReadFile* CTarReader::createAndOpenFile(const io::path& filename) |
236 | { | 236 | { |
237 | const s32 index = findFile(filename, false); | 237 | const s32 index = findFile(filename, false); |
238 | 238 | ||
239 | if (index != -1) | 239 | if (index != -1) |
240 | return createAndOpenFile(index); | 240 | return createAndOpenFile(index); |
241 | 241 | ||
242 | return 0; | 242 | return 0; |
243 | } | 243 | } |
244 | 244 | ||
245 | //! opens a file by index | 245 | //! opens a file by index |
246 | IReadFile* CTarReader::createAndOpenFile(u32 index) | 246 | IReadFile* CTarReader::createAndOpenFile(u32 index) |
247 | { | 247 | { |
248 | if (index >= Files.size() ) | 248 | if (index >= Files.size() ) |
249 | return 0; | 249 | return 0; |
250 | 250 | ||
251 | const SFileListEntry &entry = Files[index]; | 251 | const SFileListEntry &entry = Files[index]; |
252 | return createLimitReadFile( entry.FullName, File, entry.Offset, entry.Size ); | 252 | return createLimitReadFile( entry.FullName, File, entry.Offset, entry.Size ); |
253 | } | 253 | } |
254 | 254 | ||
255 | } // end namespace io | 255 | } // end namespace io |
256 | } // end namespace irr | 256 | } // end namespace irr |
257 | 257 | ||
258 | #endif // __IRR_COMPILE_WITH_TAR_ARCHIVE_LOADER_ | 258 | #endif // __IRR_COMPILE_WITH_TAR_ARCHIVE_LOADER_ |