pytdbpack_unpack: Clean up, and correct the handling of '$'.
authorMartin Pool <mbp@samba.org>
Wed, 6 Nov 2002 01:59:57 +0000 (01:59 +0000)
committerMartin Pool <mbp@samba.org>
Wed, 6 Nov 2002 01:59:57 +0000 (01:59 +0000)
source/python/py_tdbpack.c

index d29ada6741460bb15997c522e980cd90d4943b39..87cd804ed4ef14c1b457501f8cd85be063ae1cef 100644 (file)
@@ -209,8 +209,8 @@ pytdbpack_unpack(PyObject *self,
        PyObject *val_list = NULL, *ret_tuple = NULL;
        PyObject *rest_string = NULL;
        int format_len, packed_len;
+       char last_format = '#'; /* invalid */
        int i;
-       char last_format = '#';
        
        /* get arguments */
        if (!PyArg_ParseTuple(args, "ss#", &format_str, &packed_str, &packed_len))
@@ -228,28 +228,26 @@ pytdbpack_unpack(PyObject *self,
                goto failed;
        
        /* For every object, unpack.  */
-       for (ppacked = packed_str, i = 0; i < format_len; i++) {
-               char format;
-
-               format = format_str[i];
-               if (format == '$') {
-                       if (i == 0) {
-                               PyErr_Format(PyExc_ValueError,
-                                            "%s: '$' may not be first character in format",
-                                            __FUNCTION__);
-                               goto failed;
-                       }
-                       else {
-                               format = last_format; /* repeat */
-                       }
-               }
-
-               if (!pytdbpack_unpack_item(format, &ppacked, &packed_len, val_list))
+       for (ppacked = packed_str, i = 0; i < format_len && format_str[i] != '$'; i++) {
+               last_format = format_str[i];
+               /* packed_len is reduced in place */
+               if (!pytdbpack_unpack_item(format_str[i], &ppacked, &packed_len, val_list))
                        goto failed;
-               
-               last_format = format;
        }
 
+       /* If the last character was '$', keep going until out of space */
+       if (format_str[i] == '$') {
+               if (i == 0) {
+                       PyErr_Format(PyExc_ValueError,
+                                    "%s: '$' may not be first character in format",
+                                    __FUNCTION__);
+                       return NULL;
+               } 
+               while (packed_len > 0)
+                       if (!pytdbpack_unpack_item(last_format, &ppacked, &packed_len, val_list))
+                               goto failed;
+       }
+       
        /* save leftovers for next time */
        rest_string = PyString_FromStringAndSize(ppacked, packed_len);
        if (!rest_string)