File: pack.c

package info (click to toggle)
php3 1%3A3.0-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 6,512 kB
  • ctags: 6,320
  • sloc: ansic: 58,611; sh: 2,279; yacc: 1,090; php: 1,031; makefile: 911; cpp: 529; perl: 411; awk: 90; sql: 11
file content (283 lines) | stat: -rw-r--r-- 7,879 bytes parent folder | download
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
   +----------------------------------------------------------------------+
   | PHP HTML Embedded Scripting Language Version 3.0                     |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997,1998 PHP Development Team (See Credits file)      |
   +----------------------------------------------------------------------+
   | This program is free software; you can redistribute it and/or modify |
   | it under the terms of one of the following licenses:                 |
   |                                                                      |
   |  A) the GNU General Public License as published by the Free Software |
   |     Foundation; either version 2 of the License, or (at your option) |
   |     any later version.                                               |
   |                                                                      |
   |  B) the PHP License as published by the PHP Development Team and     |
   |     included in the distribution in the file: LICENSE                |
   |                                                                      |
   | This program is distributed in the hope that it will be useful,      |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of both licenses referred to here.   |
   | If you did not, or have any questions about PHP licensing, please    |
   | contact core@php.net.                                                |
   +----------------------------------------------------------------------+
   | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
   +----------------------------------------------------------------------+
 */
/* $Id $ */
#ifdef THREAD_SAFE
#include "tls.h"
#endif
#include "php.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#if MSVC5
#include <windows.h>
#include <winsock.h>
#define O_RDONLY _O_RDONLY
#include "win32/param.h"
#else
#include <sys/param.h>
#endif
#include "head.h"
#include "internal_functions.h"
#include "safe_mode.h"
#include "php3_list.h"
#include "php3_string.h"
#include "pack.h"
#if HAVE_PWD_H
#if MSVC5
#include "win32/pwd.h"
#else
#include <pwd.h>
#endif
#endif
#include "snprintf.h"
#include "fsock.h"
#if HAVE_NETINET_IN_H
#include "netinet/in.h"
#endif

function_entry pack_functions[] = {
	{"pack",		php3_pack,	NULL},
	{"unpack",		php3_unpack,		NULL},
	{NULL, NULL, NULL}
};

php3_module_entry pack_module_entry = {
	"PHP_pack", pack_functions, php3_minit_pack, NULL, NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES
};

/* pack() idea stolen from Perl. Implemented formats are a,a,c,C,s,S,i,I,l,L,n,N,f,d,x,X,@.
 * 'h' is also implemented, but I'm not sure if it works the same way as Perl
 */
void php3_pack(INTERNAL_FUNCTION_PARAMETERS) {
	pval **args;
	int argc, currarg = 1, size = 240;
	const char *format;
	char thisformat = '\0';
	char *out, *outstart;
	int repeat = 0;
	signed char c;
	unsigned char C;
	signed short s;
	unsigned short S;
	signed int i;
	unsigned int I;
	signed long l;
	unsigned long L;
	float f;
	double d;

	argc = ARG_COUNT(ht);

	if (argc < 1) {
		WRONG_PARAM_COUNT;
	}
	args = emalloc(argc * sizeof(pval *));

	if (getParametersArray(ht, argc, args) == FAILURE) {
		efree(args);
		WRONG_PARAM_COUNT;
	}
	convert_to_string(args[0]);
	out = outstart = emalloc(size);

	/* I got tired of seeing warnings under msvc so
	is some places where this macro was used, I replaced
	it with actual code */
#define CASEFOR(vartype,var) \
	case vartype:\
		convert_to_long(args[currarg]);\
		var = args[currarg]->value.lval;\
		memcpy(out, &var, sizeof(var));\
		out += sizeof(var);\
		currarg++;\
		break;

	format = args[0]->value.str.val;
	while (*format || repeat) {
		char repeatbuf[32];
		int inpos;

		/* make sure we didn't go past the end */
		if (currarg >= argc && *format != 'x' && *format != 'X' && *format != '@') {
			php3_error(E_WARNING,"pack: too few arguments");
			break;
		}
		if (repeat == 0) {
			thisformat = *(format++);
			/* grab any repeater args */
			if (*format == '*') {
				repeat = 32000;
			}
			else {
				for (repeat = 0; *format && *format >= '0' && *format <= '9'; format++, repeat++)
					repeatbuf[repeat] = *format;
				repeatbuf[repeat] = '\0';
				repeat = atoi(repeatbuf);
				if (repeat == 0)
					repeat = 1;
			}
		}
		
		/* handle the format char */
		switch (thisformat) {
			case 'A':
			case 'a':
				convert_to_string(args[currarg]);
				for (inpos = 0; inpos < repeat; inpos++)
					if (inpos >= args[currarg]->value.str.len)
						*(out++) = (thisformat == 'A') ? ' ' : '\0';
					else
						*(out++) = args[currarg]->value.str.val[inpos];
				repeat = 0;
				currarg++;
				break;
			case 'h':
				convert_to_string(args[currarg]);
				for (inpos = 0; inpos < args[currarg]->value.str.len; inpos++) {
					sprintf(out, "%02x", (int)args[currarg]->value.str.val[inpos]);
					out += 2;
				}
				currarg++;
				break;
			case 'c':
				convert_to_long(args[currarg]);
				c = (signed char)args[currarg]->value.lval;
				memcpy(out, &c, sizeof(c));
				out += sizeof(c);
				currarg++;
				break;
			case 'C':
				convert_to_long(args[currarg]);
				C = (unsigned char)args[currarg]->value.lval;
				memcpy(out, &C, sizeof(C));
				out += sizeof(C);
				currarg++;
				break;
			case 's':
				convert_to_long(args[currarg]);
				s = (signed short)args[currarg]->value.lval;
				memcpy(out, &s, sizeof(s));
				out += sizeof(s);
				currarg++;
				break;
			case 'S':
				convert_to_long(args[currarg]);
				S = (unsigned short)args[currarg]->value.lval;
				memcpy(out, &S, sizeof(S));
				out += sizeof(S);
				currarg++;
				break;
			CASEFOR('i', i);
			CASEFOR('I', I);
			CASEFOR('l', l);
			CASEFOR('L', L);
			case 'n':
				convert_to_long(args[currarg]);
				S = htons((unsigned short)args[currarg]->value.lval);
				memcpy(out, &S, sizeof(S));
				out += sizeof(S);
				currarg++;
				break;
			case 'N':
				convert_to_long(args[currarg]);
				L = htonl(args[currarg]->value.lval);
				memcpy(out, &L, sizeof(L));
				out += sizeof(L);
				currarg++;
				break;
			case 'f':
				convert_to_long(args[currarg]);
				f = (float)args[currarg]->value.lval;
				memcpy(out, &f, sizeof(f));
				out += sizeof(f);
				currarg++;
				break;
			CASEFOR('d', d);
			case 'x':
				*(out++) = '\0';
				break;
			case 'X':
				out--;
				break;
			case '@':
				/* repeat == the length to which we should pad */
				while (out - outstart < repeat)
					*(out++) = '\0';
				repeat = 0;
				break;
			default:
				php3_error(E_WARNING,"pack: unknown format type '%c'", *format);
				break;
		}
		if (repeat)
			repeat--;
	}
#undef CASEFOR
	
	efree(args);
	RETVAL_STRINGL(outstart, out - outstart, 1);
	efree(outstart);

	return;
}


/* unpack() is based on Perl's unpack(), but is modified quite a bit from there.
 * Rather than depending on error-prone ordered lists or syntactically unpleasant
 * pass-by-reference, we return an object with named paramters 
 * (like *_fetch_object()). Syntax is "f[rpt]name/...", where "f" is the
 * formatter char (like pack()), "[rpt]" is the optional repeater argument,
 * and "name" is the name of the variable to use.
 * Example: "c2chars/nints" will return an object with fields chars1, chars2, and ints.
 * Numeric pack types will return numbers, a and A will return strings,
 * f and d will return
 */
void php3_unpack(INTERNAL_FUNCTION_PARAMETERS) {
	return;
}


int php3_minit_pack(INIT_FUNC_ARGS)
{
	TLS_VARS;
	
	return SUCCESS;
}



/*
 * Local variables:
 * tab-width: 4
 * End:
 */