--- src\Modules\structmodule.c 2004-09-27 19:27:51 +0000 2.62 +++ src\Modules\structmodule.c 2004-10-02 22:23:08 +0000 2.63 @@ -200,6 +200,33 @@ return PyFloat_FromDouble(x); } +/* bigint (gG) packing and unpacking. */ + +static int +p_bigint(char *p, PyObject *v, const int num, const int little_endian, const int sign) +{ + int res; + v = get_pylong(v); + if (v == NULL) + return -1; + res = _PyLong_AsByteArray((PyLongObject *)v, + (unsigned char *)p, + num, + little_endian, /* little_endian */ + sign /* signed */); + Py_DECREF(v); + return res; +} + +static PyObject * +u_bigint(const char *p, const int num, const int little_endian, const int sign) +{ + return _PyLong_FromByteArray((const unsigned char *)p, + num, + little_endian, /* little-endian */ + sign /* signed */); +} + @@ -707,6 +734,8 @@ {'B', 1, 0, bu_uint, bp_int}, {'c', 1, 0, nu_char, np_char}, {'s', 1, 0, NULL}, + {'g', 1, 0, NULL}, + {'G', 1, 0, NULL}, {'p', 1, 0, NULL}, {'h', 2, 0, bu_int, bp_int}, {'H', 2, 0, bu_uint, bp_uint}, @@ -873,6 +902,8 @@ {'B', 1, 0, lu_uint, lp_int}, {'c', 1, 0, nu_char, np_char}, {'s', 1, 0, NULL}, + {'g', 1, 0, NULL}, + {'G', 1, 0, NULL}, {'p', 1, 0, NULL}, {'h', 2, 0, lu_int, lp_int}, {'H', 2, 0, lu_uint, lp_uint}, @@ -1113,6 +1144,21 @@ res += num; break; } + else if ((c == 'g') || (c == 'G')) { + /* num is long size, not repeat count */ + int n; + if (!(PyLong_Check(v) || PyInt_Check(v))) { + PyErr_SetString(StructError, + "argument for 'g' must be an integer"); + goto fail; + } + n = p_bigint(res, v, num, (f==lilendian_table), (c=='g')); + if (n == -1) + goto fail; + + res += num; + break; + } else if (c == 'p') { /* num is string size + 1, to fit in the count byte */ @@ -1223,6 +1269,14 @@ str += num; num = 0; } + else if ((c == 'g') || (c == 'G')) { + /* num is integer size, not repeat count */ + v = u_bigint(str, num, (f==lilendian_table), (c=='g')); + if (v == NULL) + goto fail; + str += num; + num = 0; + } else if (c == 'p') { /* num is string buffer size, not repeat count */