summaryrefslogtreecommitdiffstats
path: root/c/src/libnetworking/rtems_webserver/rom.c
blob: 6bb8a8da9962eda9c936c3bf510e2fe612a5c9fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * rom.c -- Support for ROMed page retrieval.
 *
 * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved.
 *
 * See the file "license.txt" for usage and redistribution license requirements
 */

/******************************** Description *********************************/

/*
 *	This module provides web page retrieval from compiled web pages. Use the
 *	webcomp program to compile web pages and link into the GoAhead WebServer.
 *	This module uses a hashed symbol table for fast page lookup.
 *
 *	Usage: webcomp -f webPageFileList -p Prefix >webrom.c
 */

/********************************* Includes ***********************************/

#include	<stdlib.h>

#include	"wsIntrn.h"

/******************************** Local Data **********************************/

#if WEBS_PAGE_ROM

sym_fd_t	romTab;						/* Symbol table for web pages */

/*********************************** Code *************************************/
/*
 *	Open the ROM module
 */

int websRomOpen()
{
	websRomPageIndexType	*wip;
	int						nchars;
	char_t					name[SYM_MAX];

	romTab = symOpen(WEBS_SYM_INIT);

	for (wip = websRomPageIndex; wip->path; wip++) {
		gstrncpy(name, wip->path, SYM_MAX);
		nchars = gstrlen(name) - 1;
		if (nchars > 0 &&
			(name[nchars] == '/' || name[nchars] == '\\')) {
			name[nchars] = '\0';
		}
		symEnter(romTab, name, valueInteger((int) wip), 0);
	}
	return 0;
}

/******************************************************************************/
/*
 *	Close the ROM module
 */

void websRomClose()
{
	symClose(romTab);
}

/******************************************************************************/
/*
 *	Open a web page
 */

int websRomPageOpen(webs_t wp, char_t *path, int mode, int perm)
{
	websRomPageIndexType	*wip;
	sym_t					*sp;

	a_assert(websValid(wp));
	a_assert(path && *path);

	if ((sp = symLookup(romTab, path)) == NULL) {
		return -1;
	}
	wip = (websRomPageIndexType*) sp->content.value.integer;
	wip->pos = 0;
	return (wp->docfd = wip - websRomPageIndex);
}

/******************************************************************************/
/*
 *	Close a web page
 */

void websRomPageClose(int fd)
{
}

/******************************************************************************/
/*
 *	Stat a web page
 */

int websRomPageStat(char_t *path, websStatType *sbuf)
{
	websRomPageIndexType	*wip;
	sym_t					*sp;

	a_assert(path && *path);

	if ((sp = symLookup(romTab, path)) == NULL) {
		return -1;
	}
	wip = (websRomPageIndexType*) sp->content.value.integer;

	memset(sbuf, 0, sizeof(websStatType));
	sbuf->size = wip->size;
	if (wip->page == NULL) {
		sbuf->isDir = 1;
	}
	return 0;
}

/******************************************************************************/
/*
 *	Read a web page
 */

int websRomPageReadData(webs_t wp, char *buf, int nBytes)
{
	websRomPageIndexType	*wip;
	int						len;

	a_assert(websValid(wp));
	a_assert(buf);
	a_assert(wp->docfd >= 0);

	wip = &websRomPageIndex[wp->docfd];

	len = min(wip->size - wip->pos, nBytes);
	memcpy(buf, &wip->page[wip->pos], len);
	wip->pos += len;
	return len;
}

/******************************************************************************/
/*
 *	Position a web page
 */

long websRomPageSeek(webs_t wp, long offset, int origin)
{
	websRomPageIndexType	*wip;
	long pos;

	a_assert(websValid(wp));
	a_assert(origin == SEEK_SET || origin == SEEK_CUR || origin == SEEK_END);
	a_assert(wp->docfd >= 0);

	wip = &websRomPageIndex[wp->docfd];

	if (origin != SEEK_SET && origin != SEEK_CUR && origin != SEEK_END) {
		errno = EINVAL;
		return -1;
	}

	if (wp->docfd < 0) {
		errno = EBADF;
		return -1;
	}

	pos = offset;
	switch (origin) {
	case SEEK_CUR:
		pos = wip->pos + offset;
		break;
	case SEEK_END:
		pos = wip->size + offset;
		break;
	default:
		break;
	}

	if (pos < 0) {
		errno = EBADF;
		return -1;
	}

	return (wip->pos = pos);
}

#endif /* WEBS_PAGE_ROM */

/******************************************************************************/