# -*- coding: utf-8 -*- # # pyhwp : hwp file format parser in python # Copyright (C) 2010-2023 mete0r # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # from __future__ import absolute_import from __future__ import print_function from __future__ import unicode_literals from itertools import islice import json import struct from . import dataio from . import filestructure from .dataio import dumpbytes from .dataio import Eof from .dataio import UINT32 from .tagids import HWPTAG_BEGIN from .tagids import tagnames from .utils import JsonObjects def tagname(tagid): return tagnames.get(tagid, 'HWPTAG%d' % (tagid - HWPTAG_BEGIN)) def Record(tagid, level, payload, size=None, seqno=None): if size is None: size = len(payload) d = dict(tagid=tagid, tagname=tagname(tagid), level=level, size=size, payload=payload) if seqno is not None: d['seqno'] = seqno return d def decode_record_header(f): try: # TagID, Level, Size rechdr = UINT32.read(f) tagid = rechdr & 0x3ff level = (rechdr >> 10) & 0x3ff size = (rechdr >> 20) & 0xfff if size == 0xfff: size = UINT32.read(f) return (tagid, level, size) except Eof: return None def encode_record_header(rec): size = len(rec['payload']) level = rec['level'] tagid = rec['tagid'] if size < 0xfff: hdr = (size << 20) | (level << 10) | tagid return struct.pack('