symbian-qemu-0.9.1-12/python-2.6.1/Modules/_struct.c
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 /* struct module -- pack values into and (out of) strings */
       
     2 
       
     3 /* New version supporting byte order, alignment and size options,
       
     4    character strings, and unsigned numbers */
       
     5 
       
     6 #define PY_SSIZE_T_CLEAN
       
     7 
       
     8 #include "Python.h"
       
     9 #include "structseq.h"
       
    10 #include "structmember.h"
       
    11 #include <ctype.h>
       
    12 
       
    13 static PyTypeObject PyStructType;
       
    14 
       
    15 /* compatibility macros */
       
    16 #if (PY_VERSION_HEX < 0x02050000)
       
    17 typedef int Py_ssize_t;
       
    18 #endif
       
    19 
       
    20 /* If PY_STRUCT_OVERFLOW_MASKING is defined, the struct module will wrap all input
       
    21    numbers for explicit endians such that they fit in the given type, much
       
    22    like explicit casting in C. A warning will be raised if the number did
       
    23    not originally fit within the range of the requested type. If it is
       
    24    not defined, then all range errors and overflow will be struct.error
       
    25    exceptions. */
       
    26 
       
    27 #define PY_STRUCT_OVERFLOW_MASKING 1
       
    28 
       
    29 #ifdef PY_STRUCT_OVERFLOW_MASKING
       
    30 static PyObject *pylong_ulong_mask = NULL;
       
    31 static PyObject *pyint_zero = NULL;
       
    32 #endif
       
    33 
       
    34 /* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float
       
    35    arguments for integer formats with a warning for backwards
       
    36    compatibility. */
       
    37 
       
    38 #define PY_STRUCT_FLOAT_COERCE 1
       
    39 
       
    40 #ifdef PY_STRUCT_FLOAT_COERCE
       
    41 #define FLOAT_COERCE "integer argument expected, got float"
       
    42 #endif
       
    43 
       
    44 
       
    45 /* The translation function for each format character is table driven */
       
    46 typedef struct _formatdef {
       
    47 	char format;
       
    48 	Py_ssize_t size;
       
    49 	Py_ssize_t alignment;
       
    50 	PyObject* (*unpack)(const char *,
       
    51 			    const struct _formatdef *);
       
    52 	int (*pack)(char *, PyObject *,
       
    53 		    const struct _formatdef *);
       
    54 } formatdef;
       
    55 
       
    56 typedef struct _formatcode {
       
    57 	const struct _formatdef *fmtdef;
       
    58 	Py_ssize_t offset;
       
    59 	Py_ssize_t size;
       
    60 } formatcode;
       
    61 
       
    62 /* Struct object interface */
       
    63 
       
    64 typedef struct {
       
    65 	PyObject_HEAD
       
    66 	Py_ssize_t s_size;
       
    67 	Py_ssize_t s_len;
       
    68 	formatcode *s_codes;
       
    69 	PyObject *s_format;
       
    70 	PyObject *weakreflist; /* List of weak references */
       
    71 } PyStructObject;
       
    72 
       
    73 
       
    74 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
       
    75 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
       
    76 
       
    77 
       
    78 /* Exception */
       
    79 
       
    80 static PyObject *StructError;
       
    81 
       
    82 
       
    83 /* Define various structs to figure out the alignments of types */
       
    84 
       
    85 
       
    86 typedef struct { char c; short x; } st_short;
       
    87 typedef struct { char c; int x; } st_int;
       
    88 typedef struct { char c; long x; } st_long;
       
    89 typedef struct { char c; float x; } st_float;
       
    90 typedef struct { char c; double x; } st_double;
       
    91 typedef struct { char c; void *x; } st_void_p;
       
    92 
       
    93 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
       
    94 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
       
    95 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
       
    96 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
       
    97 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
       
    98 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
       
    99 
       
   100 /* We can't support q and Q in native mode unless the compiler does;
       
   101    in std mode, they're 8 bytes on all platforms. */
       
   102 #ifdef HAVE_LONG_LONG
       
   103 typedef struct { char c; PY_LONG_LONG x; } s_long_long;
       
   104 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
       
   105 #endif
       
   106 
       
   107 #ifdef HAVE_C99_BOOL
       
   108 #define BOOL_TYPE _Bool
       
   109 typedef struct { char c; _Bool x; } s_bool;
       
   110 #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
       
   111 #else
       
   112 #define BOOL_TYPE char
       
   113 #define BOOL_ALIGN 0
       
   114 #endif
       
   115 
       
   116 #define STRINGIFY(x)    #x
       
   117 
       
   118 #ifdef __powerc
       
   119 #pragma options align=reset
       
   120 #endif
       
   121 
       
   122 /* Helper to get a PyLongObject by hook or by crook.  Caller should decref. */
       
   123 
       
   124 static PyObject *
       
   125 get_pylong(PyObject *v)
       
   126 {
       
   127 	PyNumberMethods *m;
       
   128 
       
   129 	assert(v != NULL);
       
   130 	if (PyInt_Check(v))
       
   131 		return PyLong_FromLong(PyInt_AS_LONG(v));
       
   132 	if (PyLong_Check(v)) {
       
   133 		Py_INCREF(v);
       
   134 		return v;
       
   135 	}
       
   136 	m = Py_TYPE(v)->tp_as_number;
       
   137 	if (m != NULL && m->nb_long != NULL) {
       
   138 		v = m->nb_long(v);
       
   139 		if (v == NULL)
       
   140 			return NULL;
       
   141 		if (PyLong_Check(v))
       
   142 			return v;
       
   143 		Py_DECREF(v);
       
   144 	}
       
   145 	PyErr_SetString(StructError,
       
   146 			"cannot convert argument to long");
       
   147 	return NULL;
       
   148 }
       
   149 
       
   150 /* Helper routine to get a Python integer and raise the appropriate error
       
   151    if it isn't one */
       
   152 
       
   153 static int
       
   154 get_long(PyObject *v, long *p)
       
   155 {
       
   156 	long x = PyInt_AsLong(v);
       
   157 	if (x == -1 && PyErr_Occurred()) {
       
   158 #ifdef PY_STRUCT_FLOAT_COERCE
       
   159 		if (PyFloat_Check(v)) {
       
   160 			PyObject *o;
       
   161 			int res;
       
   162 			PyErr_Clear();
       
   163 			if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
       
   164 				return -1;
       
   165 			o = PyNumber_Int(v);
       
   166 			if (o == NULL)
       
   167 				return -1;
       
   168 			res = get_long(o, p);
       
   169 			Py_DECREF(o);
       
   170 			return res;
       
   171 		}
       
   172 #endif
       
   173 		if (PyErr_ExceptionMatches(PyExc_TypeError))
       
   174 			PyErr_SetString(StructError,
       
   175 					"required argument is not an integer");
       
   176 		return -1;
       
   177 	}
       
   178 	*p = x;
       
   179 	return 0;
       
   180 }
       
   181 
       
   182 
       
   183 /* Same, but handling unsigned long */
       
   184 
       
   185 static int
       
   186 get_ulong(PyObject *v, unsigned long *p)
       
   187 {
       
   188 	if (PyLong_Check(v)) {
       
   189 		unsigned long x = PyLong_AsUnsignedLong(v);
       
   190 		if (x == (unsigned long)(-1) && PyErr_Occurred())
       
   191 			return -1;
       
   192 		*p = x;
       
   193 		return 0;
       
   194 	}
       
   195 	if (get_long(v, (long *)p) < 0)
       
   196 		return -1;
       
   197 	if (((long)*p) < 0) {
       
   198 		PyErr_SetString(StructError,
       
   199 				"unsigned argument is < 0");
       
   200 		return -1;
       
   201 	}
       
   202 	return 0;
       
   203 }
       
   204 
       
   205 #ifdef HAVE_LONG_LONG
       
   206 
       
   207 /* Same, but handling native long long. */
       
   208 
       
   209 static int
       
   210 get_longlong(PyObject *v, PY_LONG_LONG *p)
       
   211 {
       
   212 	PY_LONG_LONG x;
       
   213 
       
   214 	v = get_pylong(v);
       
   215 	if (v == NULL)
       
   216 		return -1;
       
   217 	assert(PyLong_Check(v));
       
   218 	x = PyLong_AsLongLong(v);
       
   219 	Py_DECREF(v);
       
   220 	if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
       
   221 		return -1;
       
   222 	*p = x;
       
   223 	return 0;
       
   224 }
       
   225 
       
   226 /* Same, but handling native unsigned long long. */
       
   227 
       
   228 static int
       
   229 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
       
   230 {
       
   231 	unsigned PY_LONG_LONG x;
       
   232 
       
   233 	v = get_pylong(v);
       
   234 	if (v == NULL)
       
   235 		return -1;
       
   236 	assert(PyLong_Check(v));
       
   237 	x = PyLong_AsUnsignedLongLong(v);
       
   238 	Py_DECREF(v);
       
   239 	if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
       
   240 		return -1;
       
   241 	*p = x;
       
   242 	return 0;
       
   243 }
       
   244 
       
   245 #endif
       
   246 
       
   247 #ifdef PY_STRUCT_OVERFLOW_MASKING
       
   248 
       
   249 /* Helper routine to get a Python integer and raise the appropriate error
       
   250    if it isn't one */
       
   251 
       
   252 #define INT_OVERFLOW "struct integer overflow masking is deprecated"
       
   253 
       
   254 static int
       
   255 get_wrapped_long(PyObject *v, long *p)
       
   256 {
       
   257 	if (get_long(v, p) < 0) {
       
   258 		if (PyLong_Check(v) &&
       
   259 		    PyErr_ExceptionMatches(PyExc_OverflowError)) {
       
   260 			PyObject *wrapped;
       
   261 			long x;
       
   262 			PyErr_Clear();
       
   263 #ifdef PY_STRUCT_FLOAT_COERCE
       
   264 			if (PyFloat_Check(v)) {
       
   265 				PyObject *o;
       
   266 				int res;
       
   267 				PyErr_Clear();
       
   268 				if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
       
   269 					return -1;
       
   270 				o = PyNumber_Int(v);
       
   271 				if (o == NULL)
       
   272 					return -1;
       
   273 				res = get_wrapped_long(o, p);
       
   274 				Py_DECREF(o);
       
   275 				return res;
       
   276 			}
       
   277 #endif
       
   278 			if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0)
       
   279 				return -1;
       
   280 			wrapped = PyNumber_And(v, pylong_ulong_mask);
       
   281 			if (wrapped == NULL)
       
   282 				return -1;
       
   283 			x = (long)PyLong_AsUnsignedLong(wrapped);
       
   284 			Py_DECREF(wrapped);
       
   285 			if (x == -1 && PyErr_Occurred())
       
   286 				return -1;
       
   287 			*p = x;
       
   288 		} else {
       
   289 			return -1;
       
   290 		}
       
   291 	}
       
   292 	return 0;
       
   293 }
       
   294 
       
   295 static int
       
   296 get_wrapped_ulong(PyObject *v, unsigned long *p)
       
   297 {
       
   298 	long x = (long)PyLong_AsUnsignedLong(v);
       
   299 	if (x == -1 && PyErr_Occurred()) {
       
   300 		PyObject *wrapped;
       
   301 		PyErr_Clear();
       
   302 #ifdef PY_STRUCT_FLOAT_COERCE
       
   303 		if (PyFloat_Check(v)) {
       
   304 			PyObject *o;
       
   305 			int res;
       
   306 			PyErr_Clear();
       
   307 			if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
       
   308 				return -1;
       
   309 			o = PyNumber_Int(v);
       
   310 			if (o == NULL)
       
   311 				return -1;
       
   312 			res = get_wrapped_ulong(o, p);
       
   313 			Py_DECREF(o);
       
   314 			return res;
       
   315 		}
       
   316 #endif
       
   317 		wrapped = PyNumber_And(v, pylong_ulong_mask);
       
   318 		if (wrapped == NULL)
       
   319 			return -1;
       
   320 		if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) {
       
   321 			Py_DECREF(wrapped);
       
   322 			return -1;
       
   323 		}
       
   324 		x = (long)PyLong_AsUnsignedLong(wrapped);
       
   325 		Py_DECREF(wrapped);
       
   326 		if (x == -1 && PyErr_Occurred())
       
   327 			return -1;
       
   328 	}
       
   329 	*p = (unsigned long)x;
       
   330 	return 0;
       
   331 }
       
   332 
       
   333 #define RANGE_ERROR(x, f, flag, mask) \
       
   334 	do { \
       
   335 		if (_range_error(f, flag) < 0) \
       
   336 			return -1; \
       
   337 		else \
       
   338 			(x) &= (mask); \
       
   339 	} while (0)
       
   340 
       
   341 #else
       
   342 
       
   343 #define get_wrapped_long get_long
       
   344 #define get_wrapped_ulong get_ulong
       
   345 #define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
       
   346 
       
   347 #endif
       
   348 
       
   349 /* Floating point helpers */
       
   350 
       
   351 static PyObject *
       
   352 unpack_float(const char *p,  /* start of 4-byte string */
       
   353              int le)	     /* true for little-endian, false for big-endian */
       
   354 {
       
   355 	double x;
       
   356 
       
   357 	x = _PyFloat_Unpack4((unsigned char *)p, le);
       
   358 	if (x == -1.0 && PyErr_Occurred())
       
   359 		return NULL;
       
   360 	return PyFloat_FromDouble(x);
       
   361 }
       
   362 
       
   363 static PyObject *
       
   364 unpack_double(const char *p,  /* start of 8-byte string */
       
   365               int le)         /* true for little-endian, false for big-endian */
       
   366 {
       
   367 	double x;
       
   368 
       
   369 	x = _PyFloat_Unpack8((unsigned char *)p, le);
       
   370 	if (x == -1.0 && PyErr_Occurred())
       
   371 		return NULL;
       
   372 	return PyFloat_FromDouble(x);
       
   373 }
       
   374 
       
   375 /* Helper to format the range error exceptions */
       
   376 static int
       
   377 _range_error(const formatdef *f, int is_unsigned)
       
   378 {
       
   379 	/* ulargest is the largest unsigned value with f->size bytes.
       
   380 	 * Note that the simpler:
       
   381 	 *     ((size_t)1 << (f->size * 8)) - 1
       
   382 	 * doesn't work when f->size == sizeof(size_t) because C doesn't
       
   383 	 * define what happens when a left shift count is >= the number of
       
   384 	 * bits in the integer being shifted; e.g., on some boxes it doesn't
       
   385 	 * shift at all when they're equal.
       
   386 	 */
       
   387 	const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
       
   388 	assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
       
   389 	if (is_unsigned)
       
   390 		PyErr_Format(StructError,
       
   391 			"'%c' format requires 0 <= number <= %zu",
       
   392 			f->format,
       
   393 			ulargest);
       
   394 	else {
       
   395 		const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
       
   396 		PyErr_Format(StructError,
       
   397 			"'%c' format requires %zd <= number <= %zd",
       
   398 			f->format,
       
   399 			~ largest,
       
   400 			largest);
       
   401 	}
       
   402 #ifdef PY_STRUCT_OVERFLOW_MASKING
       
   403 	{
       
   404 		PyObject *ptype, *pvalue, *ptraceback;
       
   405 		PyObject *msg;
       
   406 		int rval;
       
   407 		PyErr_Fetch(&ptype, &pvalue, &ptraceback);
       
   408 		assert(pvalue != NULL);
       
   409 		msg = PyObject_Str(pvalue);
       
   410 		Py_XDECREF(ptype);
       
   411 		Py_XDECREF(pvalue);
       
   412 		Py_XDECREF(ptraceback);
       
   413 		if (msg == NULL)
       
   414 			return -1;
       
   415 		rval = PyErr_WarnEx(PyExc_DeprecationWarning,
       
   416 				    PyString_AS_STRING(msg), 2);
       
   417 		Py_DECREF(msg);
       
   418 		if (rval == 0)
       
   419 			return 0;
       
   420 	}
       
   421 #endif
       
   422 	return -1;
       
   423 }
       
   424 
       
   425 
       
   426 
       
   427 /* A large number of small routines follow, with names of the form
       
   428 
       
   429    [bln][up]_TYPE
       
   430 
       
   431    [bln] distiguishes among big-endian, little-endian and native.
       
   432    [pu] distiguishes between pack (to struct) and unpack (from struct).
       
   433    TYPE is one of char, byte, ubyte, etc.
       
   434 */
       
   435 
       
   436 /* Native mode routines. ****************************************************/
       
   437 /* NOTE:
       
   438    In all n[up]_<type> routines handling types larger than 1 byte, there is
       
   439    *no* guarantee that the p pointer is properly aligned for each type,
       
   440    therefore memcpy is called.  An intermediate variable is used to
       
   441    compensate for big-endian architectures.
       
   442    Normally both the intermediate variable and the memcpy call will be
       
   443    skipped by C optimisation in little-endian architectures (gcc >= 2.91
       
   444    does this). */
       
   445 
       
   446 static PyObject *
       
   447 nu_char(const char *p, const formatdef *f)
       
   448 {
       
   449 	return PyString_FromStringAndSize(p, 1);
       
   450 }
       
   451 
       
   452 static PyObject *
       
   453 nu_byte(const char *p, const formatdef *f)
       
   454 {
       
   455 	return PyInt_FromLong((long) *(signed char *)p);
       
   456 }
       
   457 
       
   458 static PyObject *
       
   459 nu_ubyte(const char *p, const formatdef *f)
       
   460 {
       
   461 	return PyInt_FromLong((long) *(unsigned char *)p);
       
   462 }
       
   463 
       
   464 static PyObject *
       
   465 nu_short(const char *p, const formatdef *f)
       
   466 {
       
   467 	short x;
       
   468 	memcpy((char *)&x, p, sizeof x);
       
   469 	return PyInt_FromLong((long)x);
       
   470 }
       
   471 
       
   472 static PyObject *
       
   473 nu_ushort(const char *p, const formatdef *f)
       
   474 {
       
   475 	unsigned short x;
       
   476 	memcpy((char *)&x, p, sizeof x);
       
   477 	return PyInt_FromLong((long)x);
       
   478 }
       
   479 
       
   480 static PyObject *
       
   481 nu_int(const char *p, const formatdef *f)
       
   482 {
       
   483 	int x;
       
   484 	memcpy((char *)&x, p, sizeof x);
       
   485 	return PyInt_FromLong((long)x);
       
   486 }
       
   487 
       
   488 static PyObject *
       
   489 nu_uint(const char *p, const formatdef *f)
       
   490 {
       
   491 	unsigned int x;
       
   492 	memcpy((char *)&x, p, sizeof x);
       
   493 #if (SIZEOF_LONG > SIZEOF_INT)
       
   494 	return PyInt_FromLong((long)x);
       
   495 #else
       
   496 	if (x <= ((unsigned int)LONG_MAX))
       
   497 		return PyInt_FromLong((long)x);
       
   498 	return PyLong_FromUnsignedLong((unsigned long)x);
       
   499 #endif
       
   500 }
       
   501 
       
   502 static PyObject *
       
   503 nu_long(const char *p, const formatdef *f)
       
   504 {
       
   505 	long x;
       
   506 	memcpy((char *)&x, p, sizeof x);
       
   507 	return PyInt_FromLong(x);
       
   508 }
       
   509 
       
   510 static PyObject *
       
   511 nu_ulong(const char *p, const formatdef *f)
       
   512 {
       
   513 	unsigned long x;
       
   514 	memcpy((char *)&x, p, sizeof x);
       
   515 	if (x <= LONG_MAX)
       
   516 		return PyInt_FromLong((long)x);
       
   517 	return PyLong_FromUnsignedLong(x);
       
   518 }
       
   519 
       
   520 /* Native mode doesn't support q or Q unless the platform C supports
       
   521    long long (or, on Windows, __int64). */
       
   522 
       
   523 #ifdef HAVE_LONG_LONG
       
   524 
       
   525 static PyObject *
       
   526 nu_longlong(const char *p, const formatdef *f)
       
   527 {
       
   528 	PY_LONG_LONG x;
       
   529 	memcpy((char *)&x, p, sizeof x);
       
   530 	if (x >= LONG_MIN && x <= LONG_MAX)
       
   531 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
       
   532 	return PyLong_FromLongLong(x);
       
   533 }
       
   534 
       
   535 static PyObject *
       
   536 nu_ulonglong(const char *p, const formatdef *f)
       
   537 {
       
   538 	unsigned PY_LONG_LONG x;
       
   539 	memcpy((char *)&x, p, sizeof x);
       
   540 	if (x <= LONG_MAX)
       
   541 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
       
   542 	return PyLong_FromUnsignedLongLong(x);
       
   543 }
       
   544 
       
   545 #endif
       
   546 
       
   547 static PyObject *
       
   548 nu_bool(const char *p, const formatdef *f)
       
   549 {
       
   550 	BOOL_TYPE x;
       
   551 	memcpy((char *)&x, p, sizeof x);
       
   552 	return PyBool_FromLong(x != 0);
       
   553 }
       
   554 
       
   555 
       
   556 static PyObject *
       
   557 nu_float(const char *p, const formatdef *f)
       
   558 {
       
   559 	float x;
       
   560 	memcpy((char *)&x, p, sizeof x);
       
   561 	return PyFloat_FromDouble((double)x);
       
   562 }
       
   563 
       
   564 static PyObject *
       
   565 nu_double(const char *p, const formatdef *f)
       
   566 {
       
   567 	double x;
       
   568 	memcpy((char *)&x, p, sizeof x);
       
   569 	return PyFloat_FromDouble(x);
       
   570 }
       
   571 
       
   572 static PyObject *
       
   573 nu_void_p(const char *p, const formatdef *f)
       
   574 {
       
   575 	void *x;
       
   576 	memcpy((char *)&x, p, sizeof x);
       
   577 	return PyLong_FromVoidPtr(x);
       
   578 }
       
   579 
       
   580 static int
       
   581 np_byte(char *p, PyObject *v, const formatdef *f)
       
   582 {
       
   583 	long x;
       
   584 	if (get_long(v, &x) < 0)
       
   585 		return -1;
       
   586 	if (x < -128 || x > 127){
       
   587 		PyErr_SetString(StructError,
       
   588 				"byte format requires -128 <= number <= 127");
       
   589 		return -1;
       
   590 	}
       
   591 	*p = (char)x;
       
   592 	return 0;
       
   593 }
       
   594 
       
   595 static int
       
   596 np_ubyte(char *p, PyObject *v, const formatdef *f)
       
   597 {
       
   598 	long x;
       
   599 	if (get_long(v, &x) < 0)
       
   600 		return -1;
       
   601 	if (x < 0 || x > 255){
       
   602 		PyErr_SetString(StructError,
       
   603 				"ubyte format requires 0 <= number <= 255");
       
   604 		return -1;
       
   605 	}
       
   606 	*p = (char)x;
       
   607 	return 0;
       
   608 }
       
   609 
       
   610 static int
       
   611 np_char(char *p, PyObject *v, const formatdef *f)
       
   612 {
       
   613 	if (!PyString_Check(v) || PyString_Size(v) != 1) {
       
   614 		PyErr_SetString(StructError,
       
   615 				"char format require string of length 1");
       
   616 		return -1;
       
   617 	}
       
   618 	*p = *PyString_AsString(v);
       
   619 	return 0;
       
   620 }
       
   621 
       
   622 static int
       
   623 np_short(char *p, PyObject *v, const formatdef *f)
       
   624 {
       
   625 	long x;
       
   626 	short y;
       
   627 	if (get_long(v, &x) < 0)
       
   628 		return -1;
       
   629 	if (x < SHRT_MIN || x > SHRT_MAX){
       
   630 		PyErr_SetString(StructError,
       
   631 				"short format requires " STRINGIFY(SHRT_MIN)
       
   632 				" <= number <= " STRINGIFY(SHRT_MAX));
       
   633 		return -1;
       
   634 	}
       
   635 	y = (short)x;
       
   636 	memcpy(p, (char *)&y, sizeof y);
       
   637 	return 0;
       
   638 }
       
   639 
       
   640 static int
       
   641 np_ushort(char *p, PyObject *v, const formatdef *f)
       
   642 {
       
   643 	long x;
       
   644 	unsigned short y;
       
   645 	if (get_long(v, &x) < 0)
       
   646 		return -1;
       
   647 	if (x < 0 || x > USHRT_MAX){
       
   648 		PyErr_SetString(StructError,
       
   649 				"short format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
       
   650 		return -1;
       
   651 	}
       
   652 	y = (unsigned short)x;
       
   653 	memcpy(p, (char *)&y, sizeof y);
       
   654 	return 0;
       
   655 }
       
   656 
       
   657 static int
       
   658 np_int(char *p, PyObject *v, const formatdef *f)
       
   659 {
       
   660 	long x;
       
   661 	int y;
       
   662 	if (get_long(v, &x) < 0)
       
   663 		return -1;
       
   664 #if (SIZEOF_LONG > SIZEOF_INT)
       
   665 	if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
       
   666 		return _range_error(f, 0);
       
   667 #endif
       
   668 	y = (int)x;
       
   669 	memcpy(p, (char *)&y, sizeof y);
       
   670 	return 0;
       
   671 }
       
   672 
       
   673 static int
       
   674 np_uint(char *p, PyObject *v, const formatdef *f)
       
   675 {
       
   676 	unsigned long x;
       
   677 	unsigned int y;
       
   678 	if (get_ulong(v, &x) < 0)
       
   679 		return _range_error(f, 1);
       
   680 	y = (unsigned int)x;
       
   681 #if (SIZEOF_LONG > SIZEOF_INT)
       
   682 	if (x > ((unsigned long)UINT_MAX))
       
   683 		return _range_error(f, 1);
       
   684 #endif
       
   685 	memcpy(p, (char *)&y, sizeof y);
       
   686 	return 0;
       
   687 }
       
   688 
       
   689 static int
       
   690 np_long(char *p, PyObject *v, const formatdef *f)
       
   691 {
       
   692 	long x;
       
   693 	if (get_long(v, &x) < 0)
       
   694 		return -1;
       
   695 	memcpy(p, (char *)&x, sizeof x);
       
   696 	return 0;
       
   697 }
       
   698 
       
   699 static int
       
   700 np_ulong(char *p, PyObject *v, const formatdef *f)
       
   701 {
       
   702 	unsigned long x;
       
   703 	if (get_ulong(v, &x) < 0)
       
   704 		return _range_error(f, 1);
       
   705 	memcpy(p, (char *)&x, sizeof x);
       
   706 	return 0;
       
   707 }
       
   708 
       
   709 #ifdef HAVE_LONG_LONG
       
   710 
       
   711 static int
       
   712 np_longlong(char *p, PyObject *v, const formatdef *f)
       
   713 {
       
   714 	PY_LONG_LONG x;
       
   715 	if (get_longlong(v, &x) < 0)
       
   716 		return -1;
       
   717 	memcpy(p, (char *)&x, sizeof x);
       
   718 	return 0;
       
   719 }
       
   720 
       
   721 static int
       
   722 np_ulonglong(char *p, PyObject *v, const formatdef *f)
       
   723 {
       
   724 	unsigned PY_LONG_LONG x;
       
   725 	if (get_ulonglong(v, &x) < 0)
       
   726 		return -1;
       
   727 	memcpy(p, (char *)&x, sizeof x);
       
   728 	return 0;
       
   729 }
       
   730 #endif
       
   731 
       
   732 
       
   733 static int
       
   734 np_bool(char *p, PyObject *v, const formatdef *f)
       
   735 {
       
   736 	BOOL_TYPE y; 
       
   737 	y = PyObject_IsTrue(v);
       
   738 	memcpy(p, (char *)&y, sizeof y);
       
   739 	return 0;
       
   740 }
       
   741 
       
   742 static int
       
   743 np_float(char *p, PyObject *v, const formatdef *f)
       
   744 {
       
   745 	float x = (float)PyFloat_AsDouble(v);
       
   746 	if (x == -1 && PyErr_Occurred()) {
       
   747 		PyErr_SetString(StructError,
       
   748 				"required argument is not a float");
       
   749 		return -1;
       
   750 	}
       
   751 	memcpy(p, (char *)&x, sizeof x);
       
   752 	return 0;
       
   753 }
       
   754 
       
   755 static int
       
   756 np_double(char *p, PyObject *v, const formatdef *f)
       
   757 {
       
   758 	double x = PyFloat_AsDouble(v);
       
   759 	if (x == -1 && PyErr_Occurred()) {
       
   760 		PyErr_SetString(StructError,
       
   761 				"required argument is not a float");
       
   762 		return -1;
       
   763 	}
       
   764 	memcpy(p, (char *)&x, sizeof(double));
       
   765 	return 0;
       
   766 }
       
   767 
       
   768 static int
       
   769 np_void_p(char *p, PyObject *v, const formatdef *f)
       
   770 {
       
   771 	void *x;
       
   772 
       
   773 	v = get_pylong(v);
       
   774 	if (v == NULL)
       
   775 		return -1;
       
   776 	assert(PyLong_Check(v));
       
   777 	x = PyLong_AsVoidPtr(v);
       
   778 	Py_DECREF(v);
       
   779 	if (x == NULL && PyErr_Occurred())
       
   780 		return -1;
       
   781 	memcpy(p, (char *)&x, sizeof x);
       
   782 	return 0;
       
   783 }
       
   784 
       
   785 static formatdef native_table[] = {
       
   786 	{'x',	sizeof(char),	0,		NULL},
       
   787 	{'b',	sizeof(char),	0,		nu_byte,	np_byte},
       
   788 	{'B',	sizeof(char),	0,		nu_ubyte,	np_ubyte},
       
   789 	{'c',	sizeof(char),	0,		nu_char,	np_char},
       
   790 	{'s',	sizeof(char),	0,		NULL},
       
   791 	{'p',	sizeof(char),	0,		NULL},
       
   792 	{'h',	sizeof(short),	SHORT_ALIGN,	nu_short,	np_short},
       
   793 	{'H',	sizeof(short),	SHORT_ALIGN,	nu_ushort,	np_ushort},
       
   794 	{'i',	sizeof(int),	INT_ALIGN,	nu_int,		np_int},
       
   795 	{'I',	sizeof(int),	INT_ALIGN,	nu_uint,	np_uint},
       
   796 	{'l',	sizeof(long),	LONG_ALIGN,	nu_long,	np_long},
       
   797 	{'L',	sizeof(long),	LONG_ALIGN,	nu_ulong,	np_ulong},
       
   798 #ifdef HAVE_LONG_LONG
       
   799 	{'q',	sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
       
   800 	{'Q',	sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
       
   801 #endif
       
   802 	{'?',	sizeof(BOOL_TYPE),	BOOL_ALIGN,	nu_bool,	np_bool},
       
   803 	{'f',	sizeof(float),	FLOAT_ALIGN,	nu_float,	np_float},
       
   804 	{'d',	sizeof(double),	DOUBLE_ALIGN,	nu_double,	np_double},
       
   805 	{'P',	sizeof(void *),	VOID_P_ALIGN,	nu_void_p,	np_void_p},
       
   806 	{0}
       
   807 };
       
   808 
       
   809 /* Big-endian routines. *****************************************************/
       
   810 
       
   811 static PyObject *
       
   812 bu_int(const char *p, const formatdef *f)
       
   813 {
       
   814 	long x = 0;
       
   815 	Py_ssize_t i = f->size;
       
   816 	const unsigned char *bytes = (const unsigned char *)p;
       
   817 	do {
       
   818 		x = (x<<8) | *bytes++;
       
   819 	} while (--i > 0);
       
   820 	/* Extend the sign bit. */
       
   821 	if (SIZEOF_LONG > f->size)
       
   822 		x |= -(x & (1L << ((8 * f->size) - 1)));
       
   823 	return PyInt_FromLong(x);
       
   824 }
       
   825 
       
   826 static PyObject *
       
   827 bu_uint(const char *p, const formatdef *f)
       
   828 {
       
   829 	unsigned long x = 0;
       
   830 	Py_ssize_t i = f->size;
       
   831 	const unsigned char *bytes = (const unsigned char *)p;
       
   832 	do {
       
   833 		x = (x<<8) | *bytes++;
       
   834 	} while (--i > 0);
       
   835 	if (x <= LONG_MAX)
       
   836 		return PyInt_FromLong((long)x);
       
   837 	return PyLong_FromUnsignedLong(x);
       
   838 }
       
   839 
       
   840 static PyObject *
       
   841 bu_longlong(const char *p, const formatdef *f)
       
   842 {
       
   843 #ifdef HAVE_LONG_LONG
       
   844 	PY_LONG_LONG x = 0;
       
   845 	Py_ssize_t i = f->size;
       
   846 	const unsigned char *bytes = (const unsigned char *)p;
       
   847 	do {
       
   848 		x = (x<<8) | *bytes++;
       
   849 	} while (--i > 0);
       
   850 	/* Extend the sign bit. */
       
   851 	if (SIZEOF_LONG_LONG > f->size)
       
   852 		x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
       
   853 	if (x >= LONG_MIN && x <= LONG_MAX)
       
   854 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
       
   855 	return PyLong_FromLongLong(x);
       
   856 #else
       
   857 	return _PyLong_FromByteArray((const unsigned char *)p,
       
   858 				      8,
       
   859 				      0, /* little-endian */
       
   860 				      1  /* signed */);
       
   861 #endif
       
   862 }
       
   863 
       
   864 static PyObject *
       
   865 bu_ulonglong(const char *p, const formatdef *f)
       
   866 {
       
   867 #ifdef HAVE_LONG_LONG
       
   868 	unsigned PY_LONG_LONG x = 0;
       
   869 	Py_ssize_t i = f->size;
       
   870 	const unsigned char *bytes = (const unsigned char *)p;
       
   871 	do {
       
   872 		x = (x<<8) | *bytes++;
       
   873 	} while (--i > 0);
       
   874 	if (x <= LONG_MAX)
       
   875 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
       
   876 	return PyLong_FromUnsignedLongLong(x);
       
   877 #else
       
   878 	return _PyLong_FromByteArray((const unsigned char *)p,
       
   879 				      8,
       
   880 				      0, /* little-endian */
       
   881 				      0  /* signed */);
       
   882 #endif
       
   883 }
       
   884 
       
   885 static PyObject *
       
   886 bu_float(const char *p, const formatdef *f)
       
   887 {
       
   888 	return unpack_float(p, 0);
       
   889 }
       
   890 
       
   891 static PyObject *
       
   892 bu_double(const char *p, const formatdef *f)
       
   893 {
       
   894 	return unpack_double(p, 0);
       
   895 }
       
   896 
       
   897 static PyObject *
       
   898 bu_bool(const char *p, const formatdef *f)
       
   899 {
       
   900 	char x;
       
   901 	memcpy((char *)&x, p, sizeof x);
       
   902 	return PyBool_FromLong(x != 0);
       
   903 }
       
   904 
       
   905 static int
       
   906 bp_int(char *p, PyObject *v, const formatdef *f)
       
   907 {
       
   908 	long x;
       
   909 	Py_ssize_t i;
       
   910 	if (get_wrapped_long(v, &x) < 0)
       
   911 		return -1;
       
   912 	i = f->size;
       
   913 	if (i != SIZEOF_LONG) {
       
   914 		if ((i == 2) && (x < -32768 || x > 32767))
       
   915 			RANGE_ERROR(x, f, 0, 0xffffL);
       
   916 #if (SIZEOF_LONG != 4)
       
   917 		else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
       
   918 			RANGE_ERROR(x, f, 0, 0xffffffffL);
       
   919 #endif
       
   920 #ifdef PY_STRUCT_OVERFLOW_MASKING
       
   921 		else if ((i == 1) && (x < -128 || x > 127))
       
   922 			RANGE_ERROR(x, f, 0, 0xffL);
       
   923 #endif
       
   924 	}
       
   925 	do {
       
   926 		p[--i] = (char)x;
       
   927 		x >>= 8;
       
   928 	} while (i > 0);
       
   929 	return 0;
       
   930 }
       
   931 
       
   932 static int
       
   933 bp_uint(char *p, PyObject *v, const formatdef *f)
       
   934 {
       
   935 	unsigned long x;
       
   936 	Py_ssize_t i;
       
   937 	if (get_wrapped_ulong(v, &x) < 0)
       
   938 		return -1;
       
   939 	i = f->size;
       
   940 	if (i != SIZEOF_LONG) {
       
   941 		unsigned long maxint = 1;
       
   942 		maxint <<= (unsigned long)(i * 8);
       
   943 		if (x >= maxint)
       
   944 			RANGE_ERROR(x, f, 1, maxint - 1);
       
   945 	}
       
   946 	do {
       
   947 		p[--i] = (char)x;
       
   948 		x >>= 8;
       
   949 	} while (i > 0);
       
   950 	return 0;
       
   951 }
       
   952 
       
   953 static int
       
   954 bp_longlong(char *p, PyObject *v, const formatdef *f)
       
   955 {
       
   956 	int res;
       
   957 	v = get_pylong(v);
       
   958 	if (v == NULL)
       
   959 		return -1;
       
   960 	res = _PyLong_AsByteArray((PyLongObject *)v,
       
   961 			   	  (unsigned char *)p,
       
   962 				  8,
       
   963 				  0, /* little_endian */
       
   964 				  1  /* signed */);
       
   965 	Py_DECREF(v);
       
   966 	return res;
       
   967 }
       
   968 
       
   969 static int
       
   970 bp_ulonglong(char *p, PyObject *v, const formatdef *f)
       
   971 {
       
   972 	int res;
       
   973 	v = get_pylong(v);
       
   974 	if (v == NULL)
       
   975 		return -1;
       
   976 	res = _PyLong_AsByteArray((PyLongObject *)v,
       
   977 			   	  (unsigned char *)p,
       
   978 				  8,
       
   979 				  0, /* little_endian */
       
   980 				  0  /* signed */);
       
   981 	Py_DECREF(v);
       
   982 	return res;
       
   983 }
       
   984 
       
   985 static int
       
   986 bp_float(char *p, PyObject *v, const formatdef *f)
       
   987 {
       
   988 	double x = PyFloat_AsDouble(v);
       
   989 	if (x == -1 && PyErr_Occurred()) {
       
   990 		PyErr_SetString(StructError,
       
   991 				"required argument is not a float");
       
   992 		return -1;
       
   993 	}
       
   994 	return _PyFloat_Pack4(x, (unsigned char *)p, 0);
       
   995 }
       
   996 
       
   997 static int
       
   998 bp_double(char *p, PyObject *v, const formatdef *f)
       
   999 {
       
  1000 	double x = PyFloat_AsDouble(v);
       
  1001 	if (x == -1 && PyErr_Occurred()) {
       
  1002 		PyErr_SetString(StructError,
       
  1003 				"required argument is not a float");
       
  1004 		return -1;
       
  1005 	}
       
  1006 	return _PyFloat_Pack8(x, (unsigned char *)p, 0);
       
  1007 }
       
  1008 
       
  1009 static int
       
  1010 bp_bool(char *p, PyObject *v, const formatdef *f)
       
  1011 {
       
  1012 	char y; 
       
  1013 	y = PyObject_IsTrue(v);
       
  1014 	memcpy(p, (char *)&y, sizeof y);
       
  1015 	return 0;
       
  1016 }
       
  1017 
       
  1018 static formatdef bigendian_table[] = {
       
  1019 	{'x',	1,		0,		NULL},
       
  1020 #ifdef PY_STRUCT_OVERFLOW_MASKING
       
  1021 	/* Native packers do range checking without overflow masking. */
       
  1022 	{'b',	1,		0,		nu_byte,	bp_int},
       
  1023 	{'B',	1,		0,		nu_ubyte,	bp_uint},
       
  1024 #else
       
  1025 	{'b',	1,		0,		nu_byte,	np_byte},
       
  1026 	{'B',	1,		0,		nu_ubyte,	np_ubyte},
       
  1027 #endif
       
  1028 	{'c',	1,		0,		nu_char,	np_char},
       
  1029 	{'s',	1,		0,		NULL},
       
  1030 	{'p',	1,		0,		NULL},
       
  1031 	{'h',	2,		0,		bu_int,		bp_int},
       
  1032 	{'H',	2,		0,		bu_uint,	bp_uint},
       
  1033 	{'i',	4,		0,		bu_int,		bp_int},
       
  1034 	{'I',	4,		0,		bu_uint,	bp_uint},
       
  1035 	{'l',	4,		0,		bu_int,		bp_int},
       
  1036 	{'L',	4,		0,		bu_uint,	bp_uint},
       
  1037 	{'q',	8,		0,		bu_longlong,	bp_longlong},
       
  1038 	{'Q',	8,		0,		bu_ulonglong,	bp_ulonglong},
       
  1039 	{'?',	1,		0,		bu_bool,	bp_bool},
       
  1040 	{'f',	4,		0,		bu_float,	bp_float},
       
  1041 	{'d',	8,		0,		bu_double,	bp_double},
       
  1042 	{0}
       
  1043 };
       
  1044 
       
  1045 /* Little-endian routines. *****************************************************/
       
  1046 
       
  1047 static PyObject *
       
  1048 lu_int(const char *p, const formatdef *f)
       
  1049 {
       
  1050 	long x = 0;
       
  1051 	Py_ssize_t i = f->size;
       
  1052 	const unsigned char *bytes = (const unsigned char *)p;
       
  1053 	do {
       
  1054 		x = (x<<8) | bytes[--i];
       
  1055 	} while (i > 0);
       
  1056 	/* Extend the sign bit. */
       
  1057 	if (SIZEOF_LONG > f->size)
       
  1058 		x |= -(x & (1L << ((8 * f->size) - 1)));
       
  1059 	return PyInt_FromLong(x);
       
  1060 }
       
  1061 
       
  1062 static PyObject *
       
  1063 lu_uint(const char *p, const formatdef *f)
       
  1064 {
       
  1065 	unsigned long x = 0;
       
  1066 	Py_ssize_t i = f->size;
       
  1067 	const unsigned char *bytes = (const unsigned char *)p;
       
  1068 	do {
       
  1069 		x = (x<<8) | bytes[--i];
       
  1070 	} while (i > 0);
       
  1071 	if (x <= LONG_MAX)
       
  1072 		return PyInt_FromLong((long)x);
       
  1073 	return PyLong_FromUnsignedLong((long)x);
       
  1074 }
       
  1075 
       
  1076 static PyObject *
       
  1077 lu_longlong(const char *p, const formatdef *f)
       
  1078 {
       
  1079 #ifdef HAVE_LONG_LONG
       
  1080 	PY_LONG_LONG x = 0;
       
  1081 	Py_ssize_t i = f->size;
       
  1082 	const unsigned char *bytes = (const unsigned char *)p;
       
  1083 	do {
       
  1084 		x = (x<<8) | bytes[--i];
       
  1085 	} while (i > 0);
       
  1086 	/* Extend the sign bit. */
       
  1087 	if (SIZEOF_LONG_LONG > f->size)
       
  1088 		x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
       
  1089 	if (x >= LONG_MIN && x <= LONG_MAX)
       
  1090 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
       
  1091 	return PyLong_FromLongLong(x);
       
  1092 #else
       
  1093 	return _PyLong_FromByteArray((const unsigned char *)p,
       
  1094 				      8,
       
  1095 				      1, /* little-endian */
       
  1096 				      1  /* signed */);
       
  1097 #endif
       
  1098 }
       
  1099 
       
  1100 static PyObject *
       
  1101 lu_ulonglong(const char *p, const formatdef *f)
       
  1102 {
       
  1103 #ifdef HAVE_LONG_LONG
       
  1104 	unsigned PY_LONG_LONG x = 0;
       
  1105 	Py_ssize_t i = f->size;
       
  1106 	const unsigned char *bytes = (const unsigned char *)p;
       
  1107 	do {
       
  1108 		x = (x<<8) | bytes[--i];
       
  1109 	} while (i > 0);
       
  1110 	if (x <= LONG_MAX)
       
  1111 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
       
  1112 	return PyLong_FromUnsignedLongLong(x);
       
  1113 #else
       
  1114 	return _PyLong_FromByteArray((const unsigned char *)p,
       
  1115 				      8,
       
  1116 				      1, /* little-endian */
       
  1117 				      0  /* signed */);
       
  1118 #endif
       
  1119 }
       
  1120 
       
  1121 static PyObject *
       
  1122 lu_float(const char *p, const formatdef *f)
       
  1123 {
       
  1124 	return unpack_float(p, 1);
       
  1125 }
       
  1126 
       
  1127 static PyObject *
       
  1128 lu_double(const char *p, const formatdef *f)
       
  1129 {
       
  1130 	return unpack_double(p, 1);
       
  1131 }
       
  1132 
       
  1133 static int
       
  1134 lp_int(char *p, PyObject *v, const formatdef *f)
       
  1135 {
       
  1136 	long x;
       
  1137 	Py_ssize_t i;
       
  1138 	if (get_wrapped_long(v, &x) < 0)
       
  1139 		return -1;
       
  1140 	i = f->size;
       
  1141 	if (i != SIZEOF_LONG) {
       
  1142 		if ((i == 2) && (x < -32768 || x > 32767))
       
  1143 			RANGE_ERROR(x, f, 0, 0xffffL);
       
  1144 #if (SIZEOF_LONG != 4)
       
  1145 		else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
       
  1146 			RANGE_ERROR(x, f, 0, 0xffffffffL);
       
  1147 #endif
       
  1148 #ifdef PY_STRUCT_OVERFLOW_MASKING
       
  1149 		else if ((i == 1) && (x < -128 || x > 127))
       
  1150 			RANGE_ERROR(x, f, 0, 0xffL);
       
  1151 #endif
       
  1152 	}
       
  1153 	do {
       
  1154 		*p++ = (char)x;
       
  1155 		x >>= 8;
       
  1156 	} while (--i > 0);
       
  1157 	return 0;
       
  1158 }
       
  1159 
       
  1160 static int
       
  1161 lp_uint(char *p, PyObject *v, const formatdef *f)
       
  1162 {
       
  1163 	unsigned long x;
       
  1164 	Py_ssize_t i;
       
  1165 	if (get_wrapped_ulong(v, &x) < 0)
       
  1166 		return -1;
       
  1167 	i = f->size;
       
  1168 	if (i != SIZEOF_LONG) {
       
  1169 		unsigned long maxint = 1;
       
  1170 		maxint <<= (unsigned long)(i * 8);
       
  1171 		if (x >= maxint)
       
  1172 			RANGE_ERROR(x, f, 1, maxint - 1);
       
  1173 	}
       
  1174 	do {
       
  1175 		*p++ = (char)x;
       
  1176 		x >>= 8;
       
  1177 	} while (--i > 0);
       
  1178 	return 0;
       
  1179 }
       
  1180 
       
  1181 static int
       
  1182 lp_longlong(char *p, PyObject *v, const formatdef *f)
       
  1183 {
       
  1184 	int res;
       
  1185 	v = get_pylong(v);
       
  1186 	if (v == NULL)
       
  1187 		return -1;
       
  1188 	res = _PyLong_AsByteArray((PyLongObject*)v,
       
  1189 			   	  (unsigned char *)p,
       
  1190 				  8,
       
  1191 				  1, /* little_endian */
       
  1192 				  1  /* signed */);
       
  1193 	Py_DECREF(v);
       
  1194 	return res;
       
  1195 }
       
  1196 
       
  1197 static int
       
  1198 lp_ulonglong(char *p, PyObject *v, const formatdef *f)
       
  1199 {
       
  1200 	int res;
       
  1201 	v = get_pylong(v);
       
  1202 	if (v == NULL)
       
  1203 		return -1;
       
  1204 	res = _PyLong_AsByteArray((PyLongObject*)v,
       
  1205 			   	  (unsigned char *)p,
       
  1206 				  8,
       
  1207 				  1, /* little_endian */
       
  1208 				  0  /* signed */);
       
  1209 	Py_DECREF(v);
       
  1210 	return res;
       
  1211 }
       
  1212 
       
  1213 static int
       
  1214 lp_float(char *p, PyObject *v, const formatdef *f)
       
  1215 {
       
  1216 	double x = PyFloat_AsDouble(v);
       
  1217 	if (x == -1 && PyErr_Occurred()) {
       
  1218 		PyErr_SetString(StructError,
       
  1219 				"required argument is not a float");
       
  1220 		return -1;
       
  1221 	}
       
  1222 	return _PyFloat_Pack4(x, (unsigned char *)p, 1);
       
  1223 }
       
  1224 
       
  1225 static int
       
  1226 lp_double(char *p, PyObject *v, const formatdef *f)
       
  1227 {
       
  1228 	double x = PyFloat_AsDouble(v);
       
  1229 	if (x == -1 && PyErr_Occurred()) {
       
  1230 		PyErr_SetString(StructError,
       
  1231 				"required argument is not a float");
       
  1232 		return -1;
       
  1233 	}
       
  1234 	return _PyFloat_Pack8(x, (unsigned char *)p, 1);
       
  1235 }
       
  1236 
       
  1237 static formatdef lilendian_table[] = {
       
  1238 	{'x',	1,		0,		NULL},
       
  1239 #ifdef PY_STRUCT_OVERFLOW_MASKING
       
  1240 	/* Native packers do range checking without overflow masking. */
       
  1241 	{'b',	1,		0,		nu_byte,	lp_int},
       
  1242 	{'B',	1,		0,		nu_ubyte,	lp_uint},
       
  1243 #else
       
  1244 	{'b',	1,		0,		nu_byte,	np_byte},
       
  1245 	{'B',	1,		0,		nu_ubyte,	np_ubyte},
       
  1246 #endif
       
  1247 	{'c',	1,		0,		nu_char,	np_char},
       
  1248 	{'s',	1,		0,		NULL},
       
  1249 	{'p',	1,		0,		NULL},
       
  1250 	{'h',	2,		0,		lu_int,		lp_int},
       
  1251 	{'H',	2,		0,		lu_uint,	lp_uint},
       
  1252 	{'i',	4,		0,		lu_int,		lp_int},
       
  1253 	{'I',	4,		0,		lu_uint,	lp_uint},
       
  1254 	{'l',	4,		0,		lu_int,		lp_int},
       
  1255 	{'L',	4,		0,		lu_uint,	lp_uint},
       
  1256 	{'q',	8,		0,		lu_longlong,	lp_longlong},
       
  1257 	{'Q',	8,		0,		lu_ulonglong,	lp_ulonglong},
       
  1258 	{'?',	1,		0,		bu_bool,	bp_bool}, /* Std rep not endian dep,
       
  1259 		but potentially different from native rep -- reuse bx_bool funcs. */
       
  1260 	{'f',	4,		0,		lu_float,	lp_float},
       
  1261 	{'d',	8,		0,		lu_double,	lp_double},
       
  1262 	{0}
       
  1263 };
       
  1264 
       
  1265 
       
  1266 static const formatdef *
       
  1267 whichtable(char **pfmt)
       
  1268 {
       
  1269 	const char *fmt = (*pfmt)++; /* May be backed out of later */
       
  1270 	switch (*fmt) {
       
  1271 	case '<':
       
  1272 		return lilendian_table;
       
  1273 	case '>':
       
  1274 	case '!': /* Network byte order is big-endian */
       
  1275 		return bigendian_table;
       
  1276 	case '=': { /* Host byte order -- different from native in aligment! */
       
  1277 		int n = 1;
       
  1278 		char *p = (char *) &n;
       
  1279 		if (*p == 1)
       
  1280 			return lilendian_table;
       
  1281 		else
       
  1282 			return bigendian_table;
       
  1283 	}
       
  1284 	default:
       
  1285 		--*pfmt; /* Back out of pointer increment */
       
  1286 		/* Fall through */
       
  1287 	case '@':
       
  1288 		return native_table;
       
  1289 	}
       
  1290 }
       
  1291 
       
  1292 
       
  1293 /* Get the table entry for a format code */
       
  1294 
       
  1295 static const formatdef *
       
  1296 getentry(int c, const formatdef *f)
       
  1297 {
       
  1298 	for (; f->format != '\0'; f++) {
       
  1299 		if (f->format == c) {
       
  1300 			return f;
       
  1301 		}
       
  1302 	}
       
  1303 	PyErr_SetString(StructError, "bad char in struct format");
       
  1304 	return NULL;
       
  1305 }
       
  1306 
       
  1307 
       
  1308 /* Align a size according to a format code */
       
  1309 
       
  1310 static int
       
  1311 align(Py_ssize_t size, char c, const formatdef *e)
       
  1312 {
       
  1313 	if (e->format == c) {
       
  1314 		if (e->alignment) {
       
  1315 			size = ((size + e->alignment - 1)
       
  1316 				/ e->alignment)
       
  1317 				* e->alignment;
       
  1318 		}
       
  1319 	}
       
  1320 	return size;
       
  1321 }
       
  1322 
       
  1323 
       
  1324 /* calculate the size of a format string */
       
  1325 
       
  1326 static int
       
  1327 prepare_s(PyStructObject *self)
       
  1328 {
       
  1329 	const formatdef *f;
       
  1330 	const formatdef *e;
       
  1331 	formatcode *codes;
       
  1332 
       
  1333 	const char *s;
       
  1334 	const char *fmt;
       
  1335 	char c;
       
  1336 	Py_ssize_t size, len, num, itemsize, x;
       
  1337 
       
  1338 	fmt = PyString_AS_STRING(self->s_format);
       
  1339 
       
  1340 	f = whichtable((char **)&fmt);
       
  1341 
       
  1342 	s = fmt;
       
  1343 	size = 0;
       
  1344 	len = 0;
       
  1345 	while ((c = *s++) != '\0') {
       
  1346 		if (isspace(Py_CHARMASK(c)))
       
  1347 			continue;
       
  1348 		if ('0' <= c && c <= '9') {
       
  1349 			num = c - '0';
       
  1350 			while ('0' <= (c = *s++) && c <= '9') {
       
  1351 				x = num*10 + (c - '0');
       
  1352 				if (x/10 != num) {
       
  1353 					PyErr_SetString(
       
  1354 						StructError,
       
  1355 						"overflow in item count");
       
  1356 					return -1;
       
  1357 				}
       
  1358 				num = x;
       
  1359 			}
       
  1360 			if (c == '\0')
       
  1361 				break;
       
  1362 		}
       
  1363 		else
       
  1364 			num = 1;
       
  1365 
       
  1366 		e = getentry(c, f);
       
  1367 		if (e == NULL)
       
  1368 			return -1;
       
  1369 
       
  1370 		switch (c) {
       
  1371 			case 's': /* fall through */
       
  1372 			case 'p': len++; break;
       
  1373 			case 'x': break;
       
  1374 			default: len += num; break;
       
  1375 		}
       
  1376 
       
  1377 		itemsize = e->size;
       
  1378 		size = align(size, c, e);
       
  1379 		x = num * itemsize;
       
  1380 		size += x;
       
  1381 		if (x/itemsize != num || size < 0) {
       
  1382 			PyErr_SetString(StructError,
       
  1383 					"total struct size too long");
       
  1384 			return -1;
       
  1385 		}
       
  1386 	}
       
  1387 
       
  1388 	/* check for overflow */
       
  1389 	if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
       
  1390 		PyErr_NoMemory();
       
  1391 		return -1;
       
  1392 	}
       
  1393 
       
  1394 	self->s_size = size;
       
  1395 	self->s_len = len;
       
  1396 	codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
       
  1397 	if (codes == NULL) {
       
  1398 		PyErr_NoMemory();
       
  1399 		return -1;
       
  1400 	}
       
  1401 	self->s_codes = codes;
       
  1402 
       
  1403 	s = fmt;
       
  1404 	size = 0;
       
  1405 	while ((c = *s++) != '\0') {
       
  1406 		if (isspace(Py_CHARMASK(c)))
       
  1407 			continue;
       
  1408 		if ('0' <= c && c <= '9') {
       
  1409 			num = c - '0';
       
  1410 			while ('0' <= (c = *s++) && c <= '9')
       
  1411 				num = num*10 + (c - '0');
       
  1412 			if (c == '\0')
       
  1413 				break;
       
  1414 		}
       
  1415 		else
       
  1416 			num = 1;
       
  1417 
       
  1418 		e = getentry(c, f);
       
  1419 
       
  1420 		size = align(size, c, e);
       
  1421 		if (c == 's' || c == 'p') {
       
  1422 			codes->offset = size;
       
  1423 			codes->size = num;
       
  1424 			codes->fmtdef = e;
       
  1425 			codes++;
       
  1426 			size += num;
       
  1427 		} else if (c == 'x') {
       
  1428 			size += num;
       
  1429 		} else {
       
  1430 			while (--num >= 0) {
       
  1431 				codes->offset = size;
       
  1432 				codes->size = e->size;
       
  1433 				codes->fmtdef = e;
       
  1434 				codes++;
       
  1435 				size += e->size;
       
  1436 			}
       
  1437 		}
       
  1438 	}
       
  1439 	codes->fmtdef = NULL;
       
  1440 	codes->offset = size;
       
  1441 	codes->size = 0;
       
  1442 
       
  1443 	return 0;
       
  1444 }
       
  1445 
       
  1446 static PyObject *
       
  1447 s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
       
  1448 {
       
  1449 	PyObject *self;
       
  1450 
       
  1451 	assert(type != NULL && type->tp_alloc != NULL);
       
  1452 
       
  1453 	self = type->tp_alloc(type, 0);
       
  1454 	if (self != NULL) {
       
  1455 		PyStructObject *s = (PyStructObject*)self;
       
  1456 		Py_INCREF(Py_None);
       
  1457 		s->s_format = Py_None;
       
  1458 		s->s_codes = NULL;
       
  1459 		s->s_size = -1;
       
  1460 		s->s_len = -1;
       
  1461 	}
       
  1462 	return self;
       
  1463 }
       
  1464 
       
  1465 static int
       
  1466 s_init(PyObject *self, PyObject *args, PyObject *kwds)
       
  1467 {
       
  1468 	PyStructObject *soself = (PyStructObject *)self;
       
  1469 	PyObject *o_format = NULL;
       
  1470 	int ret = 0;
       
  1471 	static char *kwlist[] = {"format", 0};
       
  1472 
       
  1473 	assert(PyStruct_Check(self));
       
  1474 
       
  1475 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
       
  1476 					 &o_format))
       
  1477 		return -1;
       
  1478 
       
  1479 	Py_INCREF(o_format);
       
  1480 	Py_CLEAR(soself->s_format);
       
  1481 	soself->s_format = o_format;
       
  1482 
       
  1483 	ret = prepare_s(soself);
       
  1484 	return ret;
       
  1485 }
       
  1486 
       
  1487 static void
       
  1488 s_dealloc(PyStructObject *s)
       
  1489 {
       
  1490 	if (s->weakreflist != NULL)
       
  1491 		PyObject_ClearWeakRefs((PyObject *)s);
       
  1492 	if (s->s_codes != NULL) {
       
  1493 		PyMem_FREE(s->s_codes);
       
  1494 	}
       
  1495 	Py_XDECREF(s->s_format);
       
  1496 	Py_TYPE(s)->tp_free((PyObject *)s);
       
  1497 }
       
  1498 
       
  1499 static PyObject *
       
  1500 s_unpack_internal(PyStructObject *soself, char *startfrom) {
       
  1501 	formatcode *code;
       
  1502 	Py_ssize_t i = 0;
       
  1503 	PyObject *result = PyTuple_New(soself->s_len);
       
  1504 	if (result == NULL)
       
  1505 		return NULL;
       
  1506 
       
  1507 	for (code = soself->s_codes; code->fmtdef != NULL; code++) {
       
  1508 		PyObject *v;
       
  1509 		const formatdef *e = code->fmtdef;
       
  1510 		const char *res = startfrom + code->offset;
       
  1511 		if (e->format == 's') {
       
  1512 			v = PyString_FromStringAndSize(res, code->size);
       
  1513 		} else if (e->format == 'p') {
       
  1514 			Py_ssize_t n = *(unsigned char*)res;
       
  1515 			if (n >= code->size)
       
  1516 				n = code->size - 1;
       
  1517 			v = PyString_FromStringAndSize(res + 1, n);
       
  1518 		} else {
       
  1519 			v = e->unpack(res, e);
       
  1520 		}
       
  1521 		if (v == NULL)
       
  1522 			goto fail;
       
  1523 		PyTuple_SET_ITEM(result, i++, v);
       
  1524 	}
       
  1525 
       
  1526 	return result;
       
  1527 fail:
       
  1528 	Py_DECREF(result);
       
  1529 	return NULL;
       
  1530 }
       
  1531 
       
  1532 
       
  1533 PyDoc_STRVAR(s_unpack__doc__,
       
  1534 "S.unpack(str) -> (v1, v2, ...)\n\
       
  1535 \n\
       
  1536 Return tuple containing values unpacked according to this Struct's format.\n\
       
  1537 Requires len(str) == self.size. See struct.__doc__ for more on format\n\
       
  1538 strings.");
       
  1539 
       
  1540 static PyObject *
       
  1541 s_unpack(PyObject *self, PyObject *inputstr)
       
  1542 {
       
  1543 	char *start;
       
  1544 	Py_ssize_t len;
       
  1545 	PyObject *args=NULL, *result;
       
  1546 	PyStructObject *soself = (PyStructObject *)self;
       
  1547 	assert(PyStruct_Check(self));
       
  1548 	assert(soself->s_codes != NULL);
       
  1549 	if (inputstr == NULL)
       
  1550 		goto fail;
       
  1551 	if (PyString_Check(inputstr) &&
       
  1552 		PyString_GET_SIZE(inputstr) == soself->s_size) {
       
  1553 			return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
       
  1554 	}
       
  1555 	args = PyTuple_Pack(1, inputstr);
       
  1556 	if (args == NULL)
       
  1557 		return NULL;
       
  1558 	if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len))
       
  1559 		goto fail;
       
  1560 	if (soself->s_size != len)
       
  1561 		goto fail;
       
  1562 	result = s_unpack_internal(soself, start);
       
  1563 	Py_DECREF(args);
       
  1564 	return result;
       
  1565 
       
  1566 fail:
       
  1567 	Py_XDECREF(args);
       
  1568 	PyErr_Format(StructError,
       
  1569 		"unpack requires a string argument of length %zd",
       
  1570 		soself->s_size);
       
  1571 	return NULL;
       
  1572 }
       
  1573 
       
  1574 PyDoc_STRVAR(s_unpack_from__doc__,
       
  1575 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
       
  1576 \n\
       
  1577 Return tuple containing values unpacked according to this Struct's format.\n\
       
  1578 Unlike unpack, unpack_from can unpack values from any object supporting\n\
       
  1579 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
       
  1580 See struct.__doc__ for more on format strings.");
       
  1581 
       
  1582 static PyObject *
       
  1583 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
       
  1584 {
       
  1585 	static char *kwlist[] = {"buffer", "offset", 0};
       
  1586 #if (PY_VERSION_HEX < 0x02050000)
       
  1587 	static char *fmt = "z#|i:unpack_from";
       
  1588 #else
       
  1589 	static char *fmt = "z#|n:unpack_from";
       
  1590 #endif
       
  1591 	Py_ssize_t buffer_len = 0, offset = 0;
       
  1592 	char *buffer = NULL;
       
  1593 	PyStructObject *soself = (PyStructObject *)self;
       
  1594 	assert(PyStruct_Check(self));
       
  1595 	assert(soself->s_codes != NULL);
       
  1596 
       
  1597 	if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
       
  1598 					 &buffer, &buffer_len, &offset))
       
  1599 		return NULL;
       
  1600 
       
  1601 	if (buffer == NULL) {
       
  1602 		PyErr_Format(StructError,
       
  1603 			"unpack_from requires a buffer argument");
       
  1604 		return NULL;
       
  1605 	}
       
  1606 
       
  1607 	if (offset < 0)
       
  1608 		offset += buffer_len;
       
  1609 
       
  1610 	if (offset < 0 || (buffer_len - offset) < soself->s_size) {
       
  1611 		PyErr_Format(StructError,
       
  1612 			"unpack_from requires a buffer of at least %zd bytes",
       
  1613 			soself->s_size);
       
  1614 		return NULL;
       
  1615 	}
       
  1616 	return s_unpack_internal(soself, buffer + offset);
       
  1617 }
       
  1618 
       
  1619 
       
  1620 /*
       
  1621  * Guts of the pack function.
       
  1622  *
       
  1623  * Takes a struct object, a tuple of arguments, and offset in that tuple of
       
  1624  * argument for where to start processing the arguments for packing, and a
       
  1625  * character buffer for writing the packed string.  The caller must insure
       
  1626  * that the buffer may contain the required length for packing the arguments.
       
  1627  * 0 is returned on success, 1 is returned if there is an error.
       
  1628  *
       
  1629  */
       
  1630 static int
       
  1631 s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
       
  1632 {
       
  1633 	formatcode *code;
       
  1634 	/* XXX(nnorwitz): why does i need to be a local?  can we use
       
  1635 	   the offset parameter or do we need the wider width? */
       
  1636 	Py_ssize_t i;
       
  1637 
       
  1638 	memset(buf, '\0', soself->s_size);
       
  1639 	i = offset;
       
  1640 	for (code = soself->s_codes; code->fmtdef != NULL; code++) {
       
  1641 		Py_ssize_t n;
       
  1642 		PyObject *v = PyTuple_GET_ITEM(args, i++);
       
  1643 		const formatdef *e = code->fmtdef;
       
  1644 		char *res = buf + code->offset;
       
  1645 		if (e->format == 's') {
       
  1646 			if (!PyString_Check(v)) {
       
  1647 				PyErr_SetString(StructError,
       
  1648 						"argument for 's' must be a string");
       
  1649 				return -1;
       
  1650 			}
       
  1651 			n = PyString_GET_SIZE(v);
       
  1652 			if (n > code->size)
       
  1653 				n = code->size;
       
  1654 			if (n > 0)
       
  1655 				memcpy(res, PyString_AS_STRING(v), n);
       
  1656 		} else if (e->format == 'p') {
       
  1657 			if (!PyString_Check(v)) {
       
  1658 				PyErr_SetString(StructError,
       
  1659 						"argument for 'p' must be a string");
       
  1660 				return -1;
       
  1661 			}
       
  1662 			n = PyString_GET_SIZE(v);
       
  1663 			if (n > (code->size - 1))
       
  1664 				n = code->size - 1;
       
  1665 			if (n > 0)
       
  1666 				memcpy(res + 1, PyString_AS_STRING(v), n);
       
  1667 			if (n > 255)
       
  1668 				n = 255;
       
  1669 			*res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
       
  1670 		} else {
       
  1671 			if (e->pack(res, v, e) < 0) {
       
  1672 				if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
       
  1673 					PyErr_SetString(StructError,
       
  1674 							"long too large to convert to int");
       
  1675 				return -1;
       
  1676 			}
       
  1677 		}
       
  1678 	}
       
  1679 
       
  1680 	/* Success */
       
  1681 	return 0;
       
  1682 }
       
  1683 
       
  1684 
       
  1685 PyDoc_STRVAR(s_pack__doc__,
       
  1686 "S.pack(v1, v2, ...) -> string\n\
       
  1687 \n\
       
  1688 Return a string containing values v1, v2, ... packed according to this\n\
       
  1689 Struct's format. See struct.__doc__ for more on format strings.");
       
  1690 
       
  1691 static PyObject *
       
  1692 s_pack(PyObject *self, PyObject *args)
       
  1693 {
       
  1694 	PyStructObject *soself;
       
  1695 	PyObject *result;
       
  1696 
       
  1697 	/* Validate arguments. */
       
  1698 	soself = (PyStructObject *)self;
       
  1699 	assert(PyStruct_Check(self));
       
  1700 	assert(soself->s_codes != NULL);
       
  1701 	if (PyTuple_GET_SIZE(args) != soself->s_len)
       
  1702 	{
       
  1703 		PyErr_Format(StructError,
       
  1704 			"pack requires exactly %zd arguments", soself->s_len);
       
  1705 		return NULL;
       
  1706 	}
       
  1707 
       
  1708 	/* Allocate a new string */
       
  1709 	result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
       
  1710 	if (result == NULL)
       
  1711 		return NULL;
       
  1712 
       
  1713 	/* Call the guts */
       
  1714 	if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {
       
  1715 		Py_DECREF(result);
       
  1716 		return NULL;
       
  1717 	}
       
  1718 
       
  1719 	return result;
       
  1720 }
       
  1721 
       
  1722 PyDoc_STRVAR(s_pack_into__doc__,
       
  1723 "S.pack_into(buffer, offset, v1, v2, ...)\n\
       
  1724 \n\
       
  1725 Pack the values v1, v2, ... according to this Struct's format, write \n\
       
  1726 the packed bytes into the writable buffer buf starting at offset.  Note\n\
       
  1727 that the offset is not an optional argument.  See struct.__doc__ for \n\
       
  1728 more on format strings.");
       
  1729 
       
  1730 static PyObject *
       
  1731 s_pack_into(PyObject *self, PyObject *args)
       
  1732 {
       
  1733 	PyStructObject *soself;
       
  1734 	char *buffer;
       
  1735 	Py_ssize_t buffer_len, offset;
       
  1736 
       
  1737 	/* Validate arguments.  +1 is for the first arg as buffer. */
       
  1738 	soself = (PyStructObject *)self;
       
  1739 	assert(PyStruct_Check(self));
       
  1740 	assert(soself->s_codes != NULL);
       
  1741 	if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
       
  1742 	{
       
  1743 		PyErr_Format(StructError,
       
  1744 			     "pack_into requires exactly %zd arguments",
       
  1745 			     (soself->s_len + 2));
       
  1746 		return NULL;
       
  1747 	}
       
  1748 
       
  1749 	/* Extract a writable memory buffer from the first argument */
       
  1750 	if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
       
  1751 								(void**)&buffer, &buffer_len) == -1 ) {
       
  1752 		return NULL;
       
  1753 	}
       
  1754 	assert( buffer_len >= 0 );
       
  1755 
       
  1756 	/* Extract the offset from the first argument */
       
  1757 	offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1));
       
  1758 	if (offset == -1 && PyErr_Occurred())
       
  1759 		return NULL;
       
  1760 
       
  1761 	/* Support negative offsets. */
       
  1762 	if (offset < 0)
       
  1763 		offset += buffer_len;
       
  1764 
       
  1765 	/* Check boundaries */
       
  1766 	if (offset < 0 || (buffer_len - offset) < soself->s_size) {
       
  1767 		PyErr_Format(StructError,
       
  1768 			     "pack_into requires a buffer of at least %zd bytes",
       
  1769 			     soself->s_size);
       
  1770 		return NULL;
       
  1771 	}
       
  1772 
       
  1773 	/* Call the guts */
       
  1774 	if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
       
  1775 		return NULL;
       
  1776 	}
       
  1777 
       
  1778 	Py_RETURN_NONE;
       
  1779 }
       
  1780 
       
  1781 static PyObject *
       
  1782 s_get_format(PyStructObject *self, void *unused)
       
  1783 {
       
  1784 	Py_INCREF(self->s_format);
       
  1785 	return self->s_format;
       
  1786 }
       
  1787 
       
  1788 static PyObject *
       
  1789 s_get_size(PyStructObject *self, void *unused)
       
  1790 {
       
  1791     return PyInt_FromSsize_t(self->s_size);
       
  1792 }
       
  1793 
       
  1794 /* List of functions */
       
  1795 
       
  1796 static struct PyMethodDef s_methods[] = {
       
  1797 	{"pack",	s_pack,		METH_VARARGS, s_pack__doc__},
       
  1798 	{"pack_into",	s_pack_into,	METH_VARARGS, s_pack_into__doc__},
       
  1799 	{"unpack",	s_unpack,       METH_O, s_unpack__doc__},
       
  1800 	{"unpack_from",	(PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
       
  1801 			s_unpack_from__doc__},
       
  1802 	{NULL,	 NULL}		/* sentinel */
       
  1803 };
       
  1804 
       
  1805 PyDoc_STRVAR(s__doc__, "Compiled struct object");
       
  1806 
       
  1807 #define OFF(x) offsetof(PyStructObject, x)
       
  1808 
       
  1809 static PyGetSetDef s_getsetlist[] = {
       
  1810 	{"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
       
  1811 	{"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
       
  1812 	{NULL} /* sentinel */
       
  1813 };
       
  1814 
       
  1815 static
       
  1816 PyTypeObject PyStructType = {
       
  1817 	PyVarObject_HEAD_INIT(NULL, 0)
       
  1818 	"Struct",
       
  1819 	sizeof(PyStructObject),
       
  1820 	0,
       
  1821 	(destructor)s_dealloc,	/* tp_dealloc */
       
  1822 	0,					/* tp_print */
       
  1823 	0,					/* tp_getattr */
       
  1824 	0,					/* tp_setattr */
       
  1825 	0,					/* tp_compare */
       
  1826 	0,					/* tp_repr */
       
  1827 	0,					/* tp_as_number */
       
  1828 	0,					/* tp_as_sequence */
       
  1829 	0,					/* tp_as_mapping */
       
  1830 	0,					/* tp_hash */
       
  1831 	0,					/* tp_call */
       
  1832 	0,					/* tp_str */
       
  1833 	PyObject_GenericGetAttr,	/* tp_getattro */
       
  1834 	PyObject_GenericSetAttr,	/* tp_setattro */
       
  1835 	0,					/* tp_as_buffer */
       
  1836 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */
       
  1837 	s__doc__,			/* tp_doc */
       
  1838 	0,					/* tp_traverse */
       
  1839 	0,					/* tp_clear */
       
  1840 	0,					/* tp_richcompare */
       
  1841 	offsetof(PyStructObject, weakreflist),	/* tp_weaklistoffset */
       
  1842 	0,					/* tp_iter */
       
  1843 	0,					/* tp_iternext */
       
  1844 	s_methods,			/* tp_methods */
       
  1845 	NULL,				/* tp_members */
       
  1846 	s_getsetlist,		/* tp_getset */
       
  1847 	0,					/* tp_base */
       
  1848 	0,					/* tp_dict */
       
  1849 	0,					/* tp_descr_get */
       
  1850 	0,					/* tp_descr_set */
       
  1851 	0,					/* tp_dictoffset */
       
  1852 	s_init,				/* tp_init */
       
  1853 	PyType_GenericAlloc,/* tp_alloc */
       
  1854 	s_new,				/* tp_new */
       
  1855 	PyObject_Del,		/* tp_free */
       
  1856 };
       
  1857 
       
  1858 
       
  1859 /* ---- Standalone functions  ---- */
       
  1860 
       
  1861 #define MAXCACHE 100
       
  1862 static PyObject *cache = NULL;
       
  1863 
       
  1864 static PyObject *
       
  1865 cache_struct(PyObject *fmt)
       
  1866 {
       
  1867 	PyObject * s_object;
       
  1868 
       
  1869 	if (cache == NULL) {
       
  1870 		cache = PyDict_New();
       
  1871 		if (cache == NULL)
       
  1872 			return NULL;
       
  1873 	}
       
  1874 
       
  1875 	s_object = PyDict_GetItem(cache, fmt);
       
  1876 	if (s_object != NULL) {
       
  1877 		Py_INCREF(s_object);
       
  1878 		return s_object;
       
  1879 	}
       
  1880 
       
  1881 	s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
       
  1882 	if (s_object != NULL) {
       
  1883 		if (PyDict_Size(cache) >= MAXCACHE)
       
  1884 			PyDict_Clear(cache);
       
  1885 		/* Attempt to cache the result */
       
  1886 		if (PyDict_SetItem(cache, fmt, s_object) == -1)
       
  1887 			PyErr_Clear();
       
  1888 	}
       
  1889 	return s_object;
       
  1890 }
       
  1891 
       
  1892 PyDoc_STRVAR(clearcache_doc,
       
  1893 "Clear the internal cache.");
       
  1894 
       
  1895 static PyObject *
       
  1896 clearcache(PyObject *self)
       
  1897 {
       
  1898 	Py_CLEAR(cache);
       
  1899 	Py_RETURN_NONE;
       
  1900 }
       
  1901 
       
  1902 PyDoc_STRVAR(calcsize_doc,
       
  1903 "Return size of C struct described by format string fmt.");
       
  1904 
       
  1905 static PyObject *
       
  1906 calcsize(PyObject *self, PyObject *fmt)
       
  1907 {
       
  1908 	Py_ssize_t n;
       
  1909 	PyObject *s_object = cache_struct(fmt);
       
  1910 	if (s_object == NULL)
       
  1911 		return NULL;
       
  1912 	n = ((PyStructObject *)s_object)->s_size;
       
  1913 	Py_DECREF(s_object);
       
  1914     	return PyInt_FromSsize_t(n);
       
  1915 }
       
  1916 
       
  1917 PyDoc_STRVAR(pack_doc,
       
  1918 "Return string containing values v1, v2, ... packed according to fmt.");
       
  1919 
       
  1920 static PyObject *
       
  1921 pack(PyObject *self, PyObject *args)
       
  1922 {
       
  1923 	PyObject *s_object, *fmt, *newargs, *result;
       
  1924 	Py_ssize_t n = PyTuple_GET_SIZE(args);
       
  1925 
       
  1926 	if (n == 0) {
       
  1927 		PyErr_SetString(PyExc_TypeError, "missing format argument");
       
  1928 		return NULL;
       
  1929 	}
       
  1930 	fmt = PyTuple_GET_ITEM(args, 0);
       
  1931 	newargs = PyTuple_GetSlice(args, 1, n);
       
  1932 	if (newargs == NULL)
       
  1933 		return NULL;
       
  1934 
       
  1935 	s_object = cache_struct(fmt);
       
  1936 	if (s_object == NULL) {
       
  1937 		Py_DECREF(newargs);
       
  1938 		return NULL;
       
  1939 	}
       
  1940     	result = s_pack(s_object, newargs);
       
  1941 	Py_DECREF(newargs);
       
  1942 	Py_DECREF(s_object);
       
  1943 	return result;
       
  1944 }
       
  1945 
       
  1946 PyDoc_STRVAR(pack_into_doc,
       
  1947 "Pack the values v1, v2, ... according to fmt.\n\
       
  1948 Write the packed bytes into the writable buffer buf starting at offset.");
       
  1949 
       
  1950 static PyObject *
       
  1951 pack_into(PyObject *self, PyObject *args)
       
  1952 {
       
  1953 	PyObject *s_object, *fmt, *newargs, *result;
       
  1954 	Py_ssize_t n = PyTuple_GET_SIZE(args);
       
  1955 
       
  1956 	if (n == 0) {
       
  1957 		PyErr_SetString(PyExc_TypeError, "missing format argument");
       
  1958 		return NULL;
       
  1959 	}
       
  1960 	fmt = PyTuple_GET_ITEM(args, 0);
       
  1961 	newargs = PyTuple_GetSlice(args, 1, n);
       
  1962 	if (newargs == NULL)
       
  1963 		return NULL;
       
  1964 
       
  1965 	s_object = cache_struct(fmt);
       
  1966 	if (s_object == NULL) {
       
  1967 		Py_DECREF(newargs);
       
  1968 		return NULL;
       
  1969 	}
       
  1970     	result = s_pack_into(s_object, newargs);
       
  1971 	Py_DECREF(newargs);
       
  1972 	Py_DECREF(s_object);
       
  1973 	return result;
       
  1974 }
       
  1975 
       
  1976 PyDoc_STRVAR(unpack_doc,
       
  1977 "Unpack the string containing packed C structure data, according to fmt.\n\
       
  1978 Requires len(string) == calcsize(fmt).");
       
  1979 
       
  1980 static PyObject *
       
  1981 unpack(PyObject *self, PyObject *args)
       
  1982 {
       
  1983 	PyObject *s_object, *fmt, *inputstr, *result;
       
  1984 
       
  1985 	if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
       
  1986 		return NULL;
       
  1987 
       
  1988 	s_object = cache_struct(fmt);
       
  1989 	if (s_object == NULL)
       
  1990 		return NULL;
       
  1991     	result = s_unpack(s_object, inputstr);
       
  1992 	Py_DECREF(s_object);
       
  1993 	return result;
       
  1994 }
       
  1995 
       
  1996 PyDoc_STRVAR(unpack_from_doc,
       
  1997 "Unpack the buffer, containing packed C structure data, according to\n\
       
  1998 fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
       
  1999 
       
  2000 static PyObject *
       
  2001 unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
       
  2002 {
       
  2003 	PyObject *s_object, *fmt, *newargs, *result;
       
  2004 	Py_ssize_t n = PyTuple_GET_SIZE(args);
       
  2005 
       
  2006 	if (n == 0) {
       
  2007 		PyErr_SetString(PyExc_TypeError, "missing format argument");
       
  2008 		return NULL;
       
  2009 	}
       
  2010 	fmt = PyTuple_GET_ITEM(args, 0);
       
  2011 	newargs = PyTuple_GetSlice(args, 1, n);
       
  2012 	if (newargs == NULL)
       
  2013 		return NULL;
       
  2014 
       
  2015 	s_object = cache_struct(fmt);
       
  2016 	if (s_object == NULL) {
       
  2017 		Py_DECREF(newargs);
       
  2018 		return NULL;
       
  2019 	}
       
  2020     	result = s_unpack_from(s_object, newargs, kwds);
       
  2021 	Py_DECREF(newargs);
       
  2022 	Py_DECREF(s_object);
       
  2023 	return result;
       
  2024 }
       
  2025 
       
  2026 static struct PyMethodDef module_functions[] = {
       
  2027 	{"_clearcache",	(PyCFunction)clearcache,	METH_NOARGS, 	clearcache_doc},
       
  2028 	{"calcsize",	calcsize,	METH_O, 	calcsize_doc},
       
  2029 	{"pack",	pack,		METH_VARARGS, 	pack_doc},
       
  2030 	{"pack_into",	pack_into,	METH_VARARGS, 	pack_into_doc},
       
  2031 	{"unpack",	unpack,       	METH_VARARGS, 	unpack_doc},
       
  2032 	{"unpack_from",	(PyCFunction)unpack_from, 	
       
  2033 			METH_VARARGS|METH_KEYWORDS, 	unpack_from_doc},
       
  2034 	{NULL,	 NULL}		/* sentinel */
       
  2035 };
       
  2036 
       
  2037 
       
  2038 /* Module initialization */
       
  2039 
       
  2040 PyDoc_STRVAR(module_doc,
       
  2041 "Functions to convert between Python values and C structs.\n\
       
  2042 Python strings are used to hold the data representing the C struct\n\
       
  2043 and also as format strings to describe the layout of data in the C struct.\n\
       
  2044 \n\
       
  2045 The optional first format char indicates byte order, size and alignment:\n\
       
  2046  @: native order, size & alignment (default)\n\
       
  2047  =: native order, std. size & alignment\n\
       
  2048  <: little-endian, std. size & alignment\n\
       
  2049  >: big-endian, std. size & alignment\n\
       
  2050  !: same as >\n\
       
  2051 \n\
       
  2052 The remaining chars indicate types of args and must match exactly;\n\
       
  2053 these can be preceded by a decimal repeat count:\n\
       
  2054   x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
       
  2055   h:short; H:unsigned short; i:int; I:unsigned int;\n\
       
  2056   l:long; L:unsigned long; f:float; d:double.\n\
       
  2057 Special cases (preceding decimal count indicates length):\n\
       
  2058   s:string (array of char); p: pascal string (with count byte).\n\
       
  2059 Special case (only available in native format):\n\
       
  2060   P:an integer type that is wide enough to hold a pointer.\n\
       
  2061 Special case (not in native mode unless 'long long' in platform C):\n\
       
  2062   q:long long; Q:unsigned long long\n\
       
  2063 Whitespace between formats is ignored.\n\
       
  2064 \n\
       
  2065 The variable struct.error is an exception raised on errors.\n");
       
  2066 
       
  2067 PyMODINIT_FUNC
       
  2068 init_struct(void)
       
  2069 {
       
  2070 	PyObject *ver, *m;
       
  2071 
       
  2072 	ver = PyString_FromString("0.2");
       
  2073 	if (ver == NULL)
       
  2074 		return;
       
  2075 
       
  2076 	m = Py_InitModule3("_struct", module_functions, module_doc);
       
  2077 	if (m == NULL)
       
  2078 		return;
       
  2079 
       
  2080 	Py_TYPE(&PyStructType) = &PyType_Type;
       
  2081 	if (PyType_Ready(&PyStructType) < 0)
       
  2082 		return;
       
  2083 
       
  2084 #ifdef PY_STRUCT_OVERFLOW_MASKING
       
  2085 	if (pyint_zero == NULL) {
       
  2086 		pyint_zero = PyInt_FromLong(0);
       
  2087 		if (pyint_zero == NULL)
       
  2088 			return;
       
  2089 	}
       
  2090 	if (pylong_ulong_mask == NULL) {
       
  2091 #if (SIZEOF_LONG == 4)
       
  2092 		pylong_ulong_mask = PyLong_FromString("FFFFFFFF", NULL, 16);
       
  2093 #else
       
  2094 		pylong_ulong_mask = PyLong_FromString("FFFFFFFFFFFFFFFF", NULL, 16);
       
  2095 #endif
       
  2096 		if (pylong_ulong_mask == NULL)
       
  2097 			return;
       
  2098 	}
       
  2099 
       
  2100 #else
       
  2101 	/* This speed trick can't be used until overflow masking goes away, because
       
  2102 	   native endian always raises exceptions instead of overflow masking. */
       
  2103 
       
  2104 	/* Check endian and swap in faster functions */
       
  2105 	{
       
  2106 		int one = 1;
       
  2107 		formatdef *native = native_table;
       
  2108 		formatdef *other, *ptr;
       
  2109 		if ((int)*(unsigned char*)&one)
       
  2110 			other = lilendian_table;
       
  2111 		else
       
  2112 			other = bigendian_table;
       
  2113 		/* Scan through the native table, find a matching
       
  2114 		   entry in the endian table and swap in the
       
  2115 		   native implementations whenever possible
       
  2116 		   (64-bit platforms may not have "standard" sizes) */
       
  2117 		while (native->format != '\0' && other->format != '\0') {
       
  2118 			ptr = other;
       
  2119 			while (ptr->format != '\0') {
       
  2120 				if (ptr->format == native->format) {
       
  2121 					/* Match faster when formats are
       
  2122 					   listed in the same order */
       
  2123 					if (ptr == other)
       
  2124 						other++;
       
  2125 					/* Only use the trick if the
       
  2126 					   size matches */
       
  2127 					if (ptr->size != native->size)
       
  2128 						break;
       
  2129 					/* Skip float and double, could be
       
  2130 					   "unknown" float format */
       
  2131 					if (ptr->format == 'd' || ptr->format == 'f')
       
  2132 						break;
       
  2133 					ptr->pack = native->pack;
       
  2134 					ptr->unpack = native->unpack;
       
  2135 					break;
       
  2136 				}
       
  2137 				ptr++;
       
  2138 			}
       
  2139 			native++;
       
  2140 		}
       
  2141 	}
       
  2142 #endif
       
  2143 
       
  2144 	/* Add some symbolic constants to the module */
       
  2145 	if (StructError == NULL) {
       
  2146 		StructError = PyErr_NewException("struct.error", NULL, NULL);
       
  2147 		if (StructError == NULL)
       
  2148 			return;
       
  2149 	}
       
  2150 
       
  2151 	Py_INCREF(StructError);
       
  2152 	PyModule_AddObject(m, "error", StructError);
       
  2153 
       
  2154 	Py_INCREF((PyObject*)&PyStructType);
       
  2155 	PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
       
  2156 
       
  2157 	PyModule_AddObject(m, "__version__", ver);
       
  2158 
       
  2159 	PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);
       
  2160 #ifdef PY_STRUCT_OVERFLOW_MASKING
       
  2161 	PyModule_AddIntConstant(m, "_PY_STRUCT_OVERFLOW_MASKING", 1);
       
  2162 #endif
       
  2163 #ifdef PY_STRUCT_FLOAT_COERCE
       
  2164 	PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
       
  2165 #endif
       
  2166 
       
  2167 }