This parses EXIF and JFIF files. this is my first construct, and while making it I noticed I was missing a couple things:
- a PascalString that includes the size bytes in the length - this is fairly common in protocols. That is what the "- 2" below is for.
- Embed() on a Switch() doesn't work very well - because it discards the non-struct substructs.
- FastReader() below improves speed by about 10X.
class FastReader(Construct):
def _parse(self, stream, context):
return stream.read()
def _build(self, obj, stream, context):
stream.write(obj)
SegBody = Struct(None,
UBInt16('size'),
Field('data', lambda ctx: ctx['size'] - 2),
)
Seg = Struct('seg',
Literal('\xff'),
Byte('kind'),
Switch('body', lambda c: c['kind'],
{
SOS: FastReader('data'),
},
default = Embed(SegBody),
)
)
JPEG = Struct('jpeg',
Literal('\xff\xd8'),
GreedyRange(Seg),
)