Missing data when reading file using ctypes.Structure subclass

Malte Forkel <[email protected]> Fri, 28 Dec 2012 17:29:06 +0100
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
Hello,

I' trying to read binary data files using subclasses of
ctyptes.Structure. While the first test case worked fine, the second one
fails, possibly due to the bit fields in the structure: While some
struct fields receive correct data, others seem to receive none. I'm new
to ctypes, so this is probably a beginner's mistake...

The input file contains data of this type
    struct tIndexTs
    {
      uint64_t offset:40;
      int reserved:7;
      int independent:1;
      uint16_t number:16;
    };

I'm trying to read it with something like
    ....
    class IndexStruct(ctypes.Structure):
        _fields_ = [
                    ('offset', c_uint64, 40),
                    ('reserved', c_int16, 7),
                    ('independent', c_int16, 1),
                    ('number', c_uint16, 16),
                   ]
    ...
    is = IndexStruct()
    with open(indexFilename, 'rb') as file:
        while file.readinto(is) == sizeof(is):
            print is.offset, is.reserved, is.independent, is.number

The value of 'sizeof(is)' is 8, which seems ok. The first 4 chunks of 8
bytes each in the input file are
    00 00 00 00 00 80 01 00
    9c 4f 01 00 00 00 01 00
    50 98 01 00 00 00 01 00
    00 d6 01 00 00 00 01 00
But the values read are
    0 0 0 0
    85916 0 0 0
    104528 0 0 0
    120320 0 0 0
The values of 'offset' are fine. But 'independent' should be 1 for the
first chunk and 'number' should be 1 in all chunks. Instead, they are 0.
The input data to those fields is not all zeros.

What am I doing wrong?

Environment:
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2

Thanks, Malte


------------------------------------------------------------------------------
Master HTML5, CSS3, ASP.NET, MVC, AJAX, Knockout.js, Web API and
much more. Get web development skills now with LearnDevNow -
350+ hours of step-by-step video tutorials by Microsoft MVPs and experts.
SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122812