Skip to content

Latest commit

 

History

History
119 lines (95 loc) · 3.81 KB

archiver_grp.c

File metadata and controls

119 lines (95 loc) · 3.81 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* GRP support routines for PhysicsFS.
*
* This driver handles BUILD engine archives ("groupfiles"). This format
* (but not this driver) was put together by Ken Silverman.
*
* The format is simple enough. In Ken's words:
*
* What's the .GRP file format?
*
* The ".grp" file format is just a collection of a lot of files stored
* into 1 big one. I tried to make the format as simple as possible: The
* first 12 bytes contains my name, "KenSilverman". The next 4 bytes is
* the number of files that were compacted into the group file. Then for
* each file, there is a 16 byte structure, where the first 12 bytes are
* the filename, and the last 4 bytes are the file's size. The rest of
* the group file is just the raw data packed one after the other in the
* same order as the list of files.
*
* (That info is from http://www.advsys.net/ken/build.htm ...)
*
Mar 11, 2007
Mar 11, 2007
22
* Please see the file LICENSE.txt in the source's root directory.
23
24
25
26
*
* This file written by Ryan C. Gordon.
*/
May 10, 2002
May 10, 2002
27
28
#if (defined PHYSFS_SUPPORTS_GRP)
29
30
31
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Aug 30, 2010
Aug 30, 2010
32
static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
Aug 21, 2010
Aug 21, 2010
33
{
Aug 30, 2010
Aug 30, 2010
34
return (io->read(io, buf, len) == len);
Aug 21, 2010
Aug 21, 2010
35
36
} /* readAll */
Sep 6, 2010
Sep 6, 2010
37
static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
Jul 23, 2002
Jul 23, 2002
39
PHYSFS_uint32 location = 16; /* sizeof sig. */
Sep 6, 2010
Sep 6, 2010
40
41
42
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
char *ptr = NULL;
Jul 23, 2002
Jul 23, 2002
43
Sep 6, 2010
Sep 6, 2010
44
45
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
BAIL_IF_MACRO(entries == NULL, ERR_OUT_OF_MEMORY, NULL);
Jul 23, 2002
Jul 23, 2002
46
Jul 23, 2002
Jul 23, 2002
47
48
location += (16 * fileCount);
Sep 6, 2010
Sep 6, 2010
49
for (entry = entries; fileCount > 0; fileCount--, entry++)
Jul 23, 2002
Jul 23, 2002
50
{
Sep 6, 2010
Sep 6, 2010
51
52
GOTO_IF_MACRO(!readAll(io, &entry->name, 12), NULL, grpLoad_failed);
GOTO_IF_MACRO(!readAll(io, &entry->size, 4), NULL, grpLoad_failed);
Jul 23, 2002
Jul 23, 2002
53
54
55
56
57
58
entry->name[12] = '\0'; /* name isn't null-terminated in file. */
if ((ptr = strchr(entry->name, ' ')) != NULL)
*ptr = '\0'; /* trim extra spaces. */
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
Jul 23, 2002
Jul 23, 2002
59
location += entry->size;
Jul 23, 2002
Jul 23, 2002
60
61
} /* for */
Sep 6, 2010
Sep 6, 2010
62
63
64
65
66
67
return entries;
grpLoad_failed:
allocator.Free(entries);
return NULL;
} /* grpLoadEntries */
Jul 23, 2002
Jul 23, 2002
68
69
Aug 30, 2010
Aug 30, 2010
70
static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
Jul 23, 2002
Jul 23, 2002
71
{
Aug 30, 2010
Aug 30, 2010
72
PHYSFS_uint8 buf[12];
Sep 6, 2010
Sep 6, 2010
73
74
PHYSFS_uint32 entryCount = 0;
UNPKentry *entries = NULL;
Aug 30, 2010
Aug 30, 2010
75
76
assert(io != NULL); /* shouldn't ever happen. */
Aug 30, 2010
Aug 30, 2010
78
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
Aug 30, 2010
Aug 30, 2010
80
81
BAIL_IF_MACRO(!readAll(io, buf, sizeof (buf)), NULL, NULL);
if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
Sep 6, 2010
Sep 6, 2010
82
BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);
Mar 25, 2002
Mar 25, 2002
83
Sep 6, 2010
Sep 6, 2010
84
85
BAIL_IF_MACRO(!readAll(io, &entryCount, sizeof (entryCount)), NULL, NULL);
entryCount = PHYSFS_swapULE32(entryCount);
Mar 25, 2002
Mar 25, 2002
86
Sep 6, 2010
Sep 6, 2010
87
88
89
entries = grpLoadEntries(io, entryCount);
BAIL_IF_MACRO(entries == NULL, NULL, NULL);
return UNPK_openArchive(io, entries, entryCount);
90
91
92
} /* GRP_openArchive */
Sep 29, 2004
Sep 29, 2004
93
94
95
96
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
{
"GRP",
GRP_ARCHIVE_DESCRIPTION,
Jan 1, 2006
Jan 1, 2006
97
"Ryan C. Gordon <icculus@icculus.org>",
Sep 29, 2004
Sep 29, 2004
98
99
100
101
102
103
104
105
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
{
&__PHYSFS_ArchiveInfo_GRP,
GRP_openArchive, /* openArchive() method */
Sep 6, 2010
Sep 6, 2010
106
107
108
109
110
111
112
113
UNPK_enumerateFiles, /* enumerateFiles() method */
UNPK_openRead, /* openRead() method */
UNPK_openWrite, /* openWrite() method */
UNPK_openAppend, /* openAppend() method */
UNPK_remove, /* remove() method */
UNPK_mkdir, /* mkdir() method */
UNPK_dirClose, /* dirClose() method */
UNPK_stat /* stat() method */
Sep 29, 2004
Sep 29, 2004
114
115
};
May 10, 2002
May 10, 2002
116
117
#endif /* defined PHYSFS_SUPPORTS_GRP */
118
/* end of grp.c ... */