}
-#if 0
-else if (ch == 'B') {
- long size;
- char *sval;
-
- if (!PyNumber_Check(val_obj)) {
- pytdbpack_bad_type(ch, "Number", val_obj);
- return NULL;
- }
+static PyObject *pytdbpack_bad_type(char ch,
+ const char *expected,
+ PyObject *val_obj)
+{
+ PyObject *r = PyObject_Repr(val_obj);
+ if (!r)
+ return NULL;
+ PyErr_Format(PyExc_TypeError,
+ "tdbpack: format '%c' requires %s, not %s",
+ ch, expected, PyString_AS_STRING(r));
+ Py_DECREF(r);
+ return val_obj;
+}
- if (!(val_obj = PyNumber_Long(val_obj)))
- return NULL;
- size = PyLong_AsLong(val_obj);
- pack_le_uint32(size, &packed);
+/*
+ XXX: glib and Samba have quicker macro for doing the endianness conversions,
+ but I don't know of one in plain libc, and it's probably not a big deal. I
+ realize this is kind of dumb because we'll almost always be on x86, but
+ being safe is important.
+*/
+static void pack_le_uint32(unsigned long val_long, unsigned char *pbuf)
+{
+ pbuf[0] = val_long & 0xff;
+ pbuf[1] = (val_long >> 8) & 0xff;
+ pbuf[2] = (val_long >> 16) & 0xff;
+ pbuf[3] = (val_long >> 24) & 0xff;
+}
- /* Release the new reference created by the cast */
- Py_DECREF(val_obj);
- val_obj = PySequence_GetItem(val_seq, val_i++);
- if (!val_obj)
- return NULL;
-
- sval = PyString_AsString(val_obj);
- if (!sval)
- return NULL;
-
- pack_bytes(size, sval, &packed); /* do not include nul */
- }
- else {
-
- }
-
- return Py_None;
+static void pack_bytes(long len, const char *from,
+ unsigned char **pbuf)
+{
+ memcpy(*pbuf, from, len);
+ (*pbuf) += len;
}
-#endif
+
+
static PyObject *
pytdbpack_unpack(PyObject *self,
}
-
-#if 0
-/*
- Internal routine that calculates how many bytes will be required to
- encode the values in the format.
-
- Also checks that the value list is the right size for the format list.
-
- Returns number of bytes (may be 0), or -1 if there's something wrong, in
- which case a Python exception has been raised.
-
- Arguments:
-
- val_seq: a Fast Sequence (list or tuple), being all the values
-*/
-static int
-pytdbpack_calc_reqd_len(char *format_str,
- PyObject *val_seq)
-{
- int len = 0;
- char *p;
- int val_i;
- int val_len;
-
- val_len = PySequence_Length(val_seq);
- if (val_len == -1)
- return -1;
-
- for (p = format_str, val_i = 0; *p; p++, val_i++) {
- char ch = *p;
-
- if (val_i >= val_len) {
- PyErr_Format(PyExc_IndexError,
- "%s: value list is too short for format string",
- __FUNCTION__);
- return -1;
- }
-
- /* borrow a reference to the item */
- if (ch == 'd' || ch == 'p')
- len += 4;
- else if (ch == 'w')
- len += 2;
- else if (ch == 'f' || ch == 'P') {
- /* nul-terminated 8-bit string */
- int item_len;
- PyObject *str_obj;
-
- str_obj = PySequence_GetItem(val_seq, val_i);
- if (!str_obj)
- return -1;
-
- if (!PyString_Check(str_obj) || ((item_len = PyString_Size(str_obj)) == -1)) {
- pytdbpack_bad_type(ch, "String", str_obj);
- return -1;
- }
-
- len += 1 + item_len;
- }
- else if (ch == 'B') {
- /* length-preceded byte buffer: n bytes, plus a preceding
- * word */
- PyObject *len_obj;
- long len_val;
-
- len_obj = PySequence_GetItem(val_seq, val_i);
- val_i++; /* skip over buffer */
-
- if (!PyNumber_Check(len_obj)) {
- pytdbpack_bad_type(ch, "Number", len_obj);
- return -1;
- }
-
- len_val = PyInt_AsLong(len_obj);
- if (len_val < 0) {
- PyErr_Format(PyExc_ValueError,
- "%s: format 'B' requires positive integer", __FUNCTION__);
- return -1;
- }
-
- len += 4 + len_val;
- }
- else {
- PyErr_Format(PyExc_ValueError,
- "%s: format character '%c' is not supported",
- __FUNCTION__, ch);
-
- return -1;
- }
- }
-
- return len;
-}
-#endif
-
-
-static PyObject *pytdbpack_bad_type(char ch,
- const char *expected,
- PyObject *val_obj)
-{
- PyObject *r = PyObject_Repr(val_obj);
- if (!r)
- return NULL;
- PyErr_Format(PyExc_TypeError,
- "tdbpack: format '%c' requires %s, not %s",
- ch, expected, PyString_AS_STRING(r));
- Py_DECREF(r);
- return val_obj;
-}
-
-
-/*
- XXX: glib and Samba have quicker macro for doing the endianness conversions,
- but I don't know of one in plain libc, and it's probably not a big deal. I
- realize this is kind of dumb because we'll almost always be on x86, but
- being safe is important.
-*/
-static void pack_le_uint32(unsigned long val_long, unsigned char *pbuf)
-{
- pbuf[0] = val_long & 0xff;
- pbuf[1] = (val_long >> 8) & 0xff;
- pbuf[2] = (val_long >> 16) & 0xff;
- pbuf[3] = (val_long >> 24) & 0xff;
-}
-
-
-static void pack_bytes(long len, const char *from,
- unsigned char **pbuf)
-{
- memcpy(*pbuf, from, len);
- (*pbuf) += len;
-}
-
-
static void
unpack_err_too_short(void)
{