Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

JPEG parser #5

@akvadrako

Description

@akvadrako

This parses EXIF and JFIF files. this is my first construct, and while making it I noticed I was missing a couple things:

  1. a PascalString that includes the size bytes in the length - this is fairly common in protocols. That is what the "- 2" below is for.
  2. Embed() on a Switch() doesn't work very well - because it discards the non-struct substructs.
  3. 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),
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions