Esempio n. 1
0
### along with the Super Duper Script Editor.
### If not, see <http://www.gnu.org/licenses/>.
################################################################################

from bitstring import ConstBitStream
from enum import Enum

import os

from .ext import guess_ext
from .invalidarchiveexception import *

EXT_MODE = Enum("force", "suggest", "auto")
DEFAULT_EXT = ".dat"

NULL_BYTE = ConstBitStream(hex="0x00")
P3D_MAGIC = ConstBitStream(hex="0xF0306090")


##################################################
###
##################################################
def parse_pak_toc(data):

    # If we don't have enough to even get a file count, we're obviously no good.
    if data.len < 32:
        raise InvalidArchiveException

    data.bytepos = 0

    num_files = data.read("uintle:32")
Esempio n. 2
0
def sps_extract_width_height(byte_array):
    s = ConstBitStream("0x" + binascii.hexlify(byte_array))

    # read forbidden_zero_bit, nal_ref_idc and nal_unit_type
    forbidden_zero_bit = s.read('uint:1')
    nal_ref_idc = s.read('uint:2')
    nal_unit_type = s.read('uint:5')

    if nal_unit_type != 15 and nal_unit_type != 7:
        Exception(
            "Error in SPS_extract_width_height: Expected nalu type 7 or 15 (SPS), but got "
            + str(nal_unit_type))
        return
    # end if

    profile_idc = s.read('uint:8')

    # read constraint set (5 bit)
    constrained_set = s.read('bits:5')

    # read reserved zero 3 bit
    s.read('uint:3')  # ignore

    level_idc = s.read('uint:8')

    seq_param_set_id = s.read('ue')

    if profile_idc == 100 or profile_idc == 110 or profile_idc == 122 or profile_idc == 244 or profile_idc == 44 or profile_idc == 83 or profile_idc == 86 or profile_idc == 118:
        chroma_format_idc = s.read('ue')
        separate_colour_plane_flag = 0
        if chroma_format_idc == 3:
            separate_colour_plane_flag = s.read('uint:1')
        # end if
        bit_depth_luma_minus8 = s.read('ue')
        bit_depth_chroma_minus8 = s.read('ue')
        qpprime_y_zero_transform_bypass_flag2 = s.read('uint:1')
        seq_scaling_matrix_present_flag = s.read('uint:1')
    # end if

    log2_max_frame_num_minus4 = s.read('ue')
    pic_order_cnt_type = s.read('ue')
    log2_max_pic_order_cnt_lsb_minus4 = s.read('ue')
    max_num_ref_frames = s.read('ue')
    gaps_in_frame_num_value_allowed_flag = s.read('uint:1')

    pic_width_in_mbs_minus1 = s.read('ue')
    pic_height_in_map_units_minus1 = s.read('ue')

    frame_mbs_only_flag = s.read('uint:1')
    direct_8x8_inference_flag = s.read('uint:1')

    frame_cropping_flag = s.read('uint:1')

    # initialize frame_crop_*_offset
    frame_crop_left_offset = frame_crop_right_offset = frame_crop_top_offset = frame_crop_bottom_offset = 0

    # get offsets
    if frame_cropping_flag == 1:
        frame_crop_left_offset = s.read('ue')
        frame_crop_right_offset = s.read('ue')
        frame_crop_top_offset = s.read('ue')
        frame_crop_bottom_offset = s.read('ue')

    Width = ((pic_width_in_mbs_minus1 + 1) *
             16) - (frame_crop_right_offset * 2) - (frame_crop_left_offset * 2)
    Height = ((2 - frame_mbs_only_flag) *
              (pic_height_in_map_units_minus1 + 1) *
              16) - frame_crop_bottom_offset * 2 - frame_crop_top_offset * 2

    FramesPerSecond = 0

    return {
        'ProfileIDC': profile_idc,
        'LevelIDC': level_idc,
        'SPS-ID': seq_param_set_id,
        'Width': Width,
        'Height': Height,
        'FPS': FramesPerSecond
    }


# end function
Esempio n. 3
0
 def test_unsupported_action(self):
     alp_action_bytes = [0x25, 0x40, 0x00, 0x00]
     with self.assertRaises(ParseError):
         cmd = Parser().parse(ConstBitStream(bytes=alp_action_bytes),
                              len(alp_action_bytes))
Esempio n. 4
0
from script_pack import ScriptPack
from sprite import SpriteId, SPRITE_TYPE
from text_format import TEXT_FORMATS, TEXT_ORIENT, TEXT_ALIGN
from voice import VoiceId

_LOGGER_NAME = common.LOGGER_NAME + "." + __name__
_LOGGER = logging.getLogger(_LOGGER_NAME)

NONSTOP_DIR = os.path.join("all", "bin")

NONSTOP_LINE_TYPE = Enum("normal", "chatter", "unknown1", "unknown3",
                         "unknown4", "hanron1", "hanron2", "hanron3",
                         "hanron4", "hanron5", "hanron6", "hanron7", "hanron8",
                         "hanron_mc")
LINE_TYPE_MAP = {
    ConstBitStream(hex="0x00000000"): NONSTOP_LINE_TYPE.normal,
    ConstBitStream(hex="0x01000100"): NONSTOP_LINE_TYPE.chatter,
    ConstBitStream(hex="0x01000000"): NONSTOP_LINE_TYPE.unknown1,
    ConstBitStream(hex="0x03000000"): NONSTOP_LINE_TYPE.unknown3,
    ConstBitStream(hex="0x04000000"): NONSTOP_LINE_TYPE.unknown4,
    ConstBitStream(hex="0x00000100"): NONSTOP_LINE_TYPE.hanron1,
    ConstBitStream(hex="0x00000200"): NONSTOP_LINE_TYPE.hanron2,
    ConstBitStream(hex="0x00000300"): NONSTOP_LINE_TYPE.hanron3,
    ConstBitStream(hex="0x00000400"): NONSTOP_LINE_TYPE.hanron4,
    ConstBitStream(hex="0x00000500"): NONSTOP_LINE_TYPE.hanron5,
    ConstBitStream(hex="0x00000600"): NONSTOP_LINE_TYPE.hanron6,
    ConstBitStream(hex="0x00000700"): NONSTOP_LINE_TYPE.hanron7,
    ConstBitStream(hex="0x00000800"): NONSTOP_LINE_TYPE.hanron8,
    ConstBitStream(hex="0x02000000"): NONSTOP_LINE_TYPE.hanron_mc,
}
Esempio n. 5
0
    def load(self, filename):
        filename = filename.lower()

        if not filename in DIR_MAP:
            _LOGGER.error("Invalid nonstop file: %s" % filename)
            return

        self.filename = filename

        script_dir = DIR_MAP[filename]
        self.script_pack = ScriptPack(script_dir,
                                      common.editor_config.data01_dir)

        file_order = []

        # --- NONSTOP FORMAT ---
        # XX XX -- ???
        # XX XX -- Number of lines (not necessarily files)
        #
        # [68 bytes per line]
        # XX XX       -- File number
        # XX XX XX XX
        #  * 0x00000000 = normal line
        #  * 0x01000100 = chatter
        #  * 0x01000000 = ??? (Only appears in SDR2)
        #  * 0x02000000 = ??? (Only appears in SDR2)
        #  * 0x03000000 = ??? (Only appears in SDR2)
        #  * 0x04000000 = ??? (Only appears in SDR2)
        # XX XX       -- Ammo ID that reacts to this line.
        # XX XX       -- Converted line ID that reacts to this line.
        # XX XX       -- ???
        # XX XX       -- 1 = has a weak point, 0 = has no weak point
        # XX XX       -- The amount of time before the next line is shown (in sixtieths of seconds (frames?)).
        # XX XX       -- Unknown (Possibly line orientation? Only 0 in the first game, but sometimes 2 in the second.)
        # XX XX       -- Effect used when transitioning text in.
        # XX XX       -- Effect used when transitioning text out.
        #  * 0: fade
        #  * 1: spin in/out
        #  * 2: zoom out
        #  * 3: slide in/out
        # XX XX       -- The amount of the the line stays on-screen (in sixtieths of seconds (frames?)).
        # XX XX       -- Initial X position (text centered around this pos).
        # XX XX       -- Initial Y position (text centered around this pos).
        # XX XX       -- Text velocity.
        # XX XX       -- Angle of motion.
        # XX XX       -- Initial text zoom (in percent).
        # XX XX       -- Change in zoom over time (in percent).
        #  * 90% means it gradually shrinks.
        #  * 100% means it stays the same size the whole time.
        #  * 110% means it gradually grows.
        # XX XX       -- 0 = no shake, 1 = shake
        # XX XX       -- Rotate the text clockwise to this angle.
        # XX XX       -- Text spins clockwise at this rate.
        # XX XX       -- Speaker (00 00 if chatter)
        # XX XX       -- Sprite ID (00 00 if chatter)
        # XX XX XX XX -- ???
        # XX XX       -- Voice index (FF FF if chatter)
        # XX XX       -- ???
        # XX XX       -- Chapter
        # XX XX XX XX -- ??? (padding?)
        nonstop = ConstBitStream(filename=os.path.join(
            common.editor_config.data01_dir, NONSTOP_DIR, self.filename))

        self.magic = nonstop.read(16)
        num_lines = nonstop.read('uintle:16')

        # Four byte header plus 68 bytes per line.
        if nonstop.len < (4 + (num_lines * 68)) * 8:
            raise Exception("Invalid nonstop file.")

        prev_non_chatter = -1
        self.lines = []

        for i in range(num_lines):
            line = NonstopLine()

            line.file_num = nonstop.read('uintle:16')

            line.line_type = nonstop.read(32)
            if line.line_type in LINE_TYPE_MAP:
                line.line_type = LINE_TYPE_MAP[line.line_type]

            line.ammo_id = nonstop.read('intle:16')
            line.converted_id = nonstop.read('intle:16')
            line.unknown1 = nonstop.read(16)
            line.weak_point = nonstop.read('uintle:16')

            line.delay = nonstop.read('intle:16')
            line.orientation = nonstop.read('intle:16')
            line.in_effect = nonstop.read('intle:16')
            line.out_effect = nonstop.read('intle:16')
            line.time_visible = nonstop.read('intle:16')
            line.x_start = nonstop.read('intle:16')
            line.y_start = nonstop.read('intle:16')
            line.velocity = nonstop.read('intle:16')
            line.angle = nonstop.read('intle:16')
            line.zoom_start = nonstop.read('intle:16')
            line.zoom_change = nonstop.read('intle:16')
            line.shake = nonstop.read('intle:16')
            line.rot_angle = nonstop.read('intle:16')
            line.spin_vel = nonstop.read('intle:16')

            line.speaker = nonstop.read('intle:16')

            # Since we mess with speaker a little bit later, we want to keep the ID for the sprite.
            line.char_id = line.speaker
            line.sprite_id = nonstop.read('intle:16')

            line.unknown3 = nonstop.read(32)
            line.voice_id = nonstop.read('intle:16')
            line.unknown4 = nonstop.read(16)
            line.chapter = nonstop.read('intle:16')
            line.unknown5 = nonstop.read(32)
            line.unknown6 = nonstop.read(64)

            format = copy.deepcopy(TEXT_FORMATS[common.SCENE_MODES.debate])
            format.orient = TEXT_ORIENT.hor if line.orientation == 0 else TEXT_ORIENT.ver
            format.align = TEXT_ALIGN.center

            if format.orient == TEXT_ORIENT.ver:
                format.y = format.h
                format.x = format.w / 3.5

            self.script_pack[line.file_num].scene_info.format = format

            if line.line_type == NONSTOP_LINE_TYPE.normal:
                prev_non_chatter = line.file_num

                # Fixing some weirdness.
                # if filename in ["nonstop_06_003.dat", "nonstop_06_005.dat", "nonstop_06_006.dat", "nonstop_06_007.dat"] and line.speaker == 16:
                # line.speaker = 15
                # if filename[:10] == "nonstop_06" and int(filename[11:14]) >= 10 and line.speaker == 10:
                # line.speaker = 18
                # if filename in ["nonstop_02_003.dat", "nonstop_02_005.dat", "nonstop_04_005.dat", "nonstop_04_006.dat"] and line.speaker == 10:
                # line.speaker = 18

                self.script_pack[
                    line.file_num].scene_info.speaker = line.speaker

                sprite = SpriteId(SPRITE_TYPE.stand, line.char_id,
                                  line.sprite_id)
                self.script_pack[line.file_num].scene_info.sprite = sprite

                voice = VoiceId(line.speaker, line.chapter, line.voice_id)
                self.script_pack[line.file_num].scene_info.voice = voice

                self.script_pack[
                    line.
                    file_num].scene_info.special = common.SCENE_SPECIAL.debate

            elif "hanron" in str(line.line_type):

                self.script_pack[
                    line.file_num].scene_info.speaker = line.speaker

                sprite = SpriteId(SPRITE_TYPE.stand, line.char_id,
                                  line.sprite_id)
                self.script_pack[line.file_num].scene_info.sprite = sprite

                voice = VoiceId(line.speaker, line.chapter, line.voice_id)
                self.script_pack[line.file_num].scene_info.voice = voice

                self.script_pack[
                    line.
                    file_num].scene_info.special = common.SCENE_SPECIAL.hanron

            elif line.line_type == NONSTOP_LINE_TYPE.chatter:
                self.script_pack[line.file_num].scene_info.speaker = -1
                self.script_pack[
                    line.
                    file_num].scene_info.special = common.SCENE_SPECIAL.chatter
                self.script_pack[
                    line.file_num].scene_info.extra_val = prev_non_chatter

            else:
                _LOGGER.error("Invalid line type: %s" % line.line_type)

            file_order.append(line.file_num)
            self.lines.append(line)

        for index in xrange(len(self.script_pack)):
            if not index in file_order:
                file_order.append(index)

        self.script_pack.script_files = [
            self.script_pack[i] for i in file_order
        ]
Esempio n. 6
0
import json
from bitarray import bitarray
from bitstring import ConstBitStream
import sys


in_filename = sys.argv[1]
out_filename = sys.argv[2]

with open('decode.txt', 'r') as f:
    deccode_list = json.load(f)


bits = ConstBitStream(filename=in_filename)

text = ''
list_de = deccode_list
for bit in bits:
    list_de = list_de[bit]
    if isinstance(list_de, str):
        text += list_de
        list_de = deccode_list

with open(out_filename, 'w') as f:
    f.write(text)
Esempio n. 7
0
def decode_section4(stream, descriptors, n_subsets=1, compressed=False):
    """
    Decode Section 4, the data section, of a BUFR message into a :class:`.Section4` object.

    :param ReadableStream stream: BUFR message, starting at section 4
    :param descriptors: List of descriptors specifying message structure
    :param int n_subsets: Number of data subsets, from section 3
    :param bool compressed: Whether message data is compressed or not, from section 3
    :raises NotImplementedError: if the message contains operator descriptors
    :raises NotImplementedError: if the message contains sequence descriptors
    """

    REPLICATION_DESCRIPTORS = set(
        [fxy2int("031000"),
         fxy2int("031001"),
         fxy2int("031002")])
    REPETITION_DESCRIPTORS = set([fxy2int("031011"), fxy2int("031012")])

    from bitstring import ConstBitStream, Bits
    length = stream.readint(3)
    pad = stream.readint(1)
    data = stream.readbytes(length - 4)
    bits = ConstBitStream(bytes=data)

    def decode(bits, descriptors, operators, descriptor_overlay):
        """
        :param bits: Bit stream to decode from
        :param descriptors: Descriptor iterator
        :param dict operators: Operators in effect, indexed by opcode
        :param dict descriptor_overlay: Overlay descriptors affected by CHANGE_REFERENCE_VALUES operator
        """
        values = []
        for descriptor in descriptors:
            descriptor = descriptor_overlay.get(descriptor.code, descriptor)
            if isinstance(descriptor, ElementDescriptor):
                op_crf = operators.get(OpCode.CHANGE_REFERENCE_VALUES, None)
                if op_crf is not None:
                    ref_value = Bits._readuint(bits, op_crf.bits(), bits.pos)
                    bits.pos += op_crf.bits()
                    top_bit_mask = (1 << op_crf.bits() - 1)
                    if ref_value & top_bit_mask:
                        ref_value = -(ref_value & ~top_bit_mask)
                    overlay_descriptor = ElementDescriptor(
                        descriptor.code, descriptor.length, descriptor.scale,
                        ref_value, descriptor.significance, descriptor.unit)
                    descriptor_overlay[descriptor.code] = overlay_descriptor
                    continue

                op_aaf = operators.get(OpCode.ADD_ASSOCIATED_FIELD, None)
                if op_aaf is not None and descriptor.code != fxy2int("031021"):
                    # Don't apply to ASSOCIATED FIELD SIGNIFICANCE
                    associated_value = Bits._readuint(bits, op_aaf.bits(),
                                                      bits.pos)
                    bits.pos += op_aaf.bits()
                    # Use dummy descriptor 999999 for associated field, like Geo::BUFR and libbufr
                    dummy_descriptor = ElementDescriptor(
                        fxy2int("999999"), op_aaf.bits(), 0, 0,
                        "ASSOCIATED FIELD", "NUMERIC")
                    values.append(
                        BufrValue(associated_value, associated_value,
                                  dummy_descriptor))

                read_length = _calculate_read_length(descriptor, operators)
                if descriptor.unit == 'CCITTIA5':
                    raw_value = Bits._readhex(bits, read_length, bits.pos)
                else:
                    raw_value = Bits._readuint(bits, read_length, bits.pos)
                bits.pos += read_length
                values.append(
                    _decode_raw_value(raw_value, descriptor, operators))
            elif isinstance(descriptor, ReplicationDescriptor):
                aggregation = []
                if descriptor.count:
                    bval = None
                    count = descriptor.count
                else:
                    bval = decode(bits, itertools.islice(descriptors, 1), {},
                                  {})[0]
                    count = bval.value
                n_fields = descriptor.fields
                field_descriptors = list(
                    itertools.islice(descriptors, n_fields))
                if bval is None or bval.descriptor.code in REPLICATION_DESCRIPTORS:
                    # Regular replication, X elements repeated Y or <element value> times in the file
                    for _ in range(count):
                        aggregation.append(
                            decode(bits, iter(field_descriptors), operators,
                                   descriptor_overlay))
                elif bval.descriptor.code in REPETITION_DESCRIPTORS:
                    # Repeated replication, X elements present once in the file, output <element value> times
                    repeated_values = decode(bits, iter(field_descriptors),
                                             operators, descriptor_overlay)
                    for _ in range(count):
                        aggregation.append(repeated_values)
                else:
                    raise ValueError(
                        "Unexpected delayed replication element %s" % bval)
                values.append(aggregation)
            elif isinstance(descriptor, OperatorDescriptor):
                op = descriptor.operator
                if op.immediate:
                    if op.opcode == OpCode.SIGNIFY_CHARACTER:
                        raw_value = Bits._readhex(bits, op.bits(), bits.pos)
                        bits.pos += op.bits()
                        char_descriptor = ElementDescriptor(
                            fxy2int(op.code), op.bits(), 0, 0,
                            "CHARACTER INFORMATION", "CCITTIA5")
                        value = _decode_raw_value(raw_value, char_descriptor,
                                                  {})
                        values.append(value)
                    elif op.opcode == OpCode.SIGNIFY_LOCAL_DESCRIPTOR:
                        base_descriptor = itertools.islice(descriptors, 1)[0]
                        mod_descriptor = ElementDescriptor(
                            base_descriptor.code, op.bits(),
                            base_descriptor.scale, base_descriptor.ref,
                            base_descriptor.significance, base_descriptor.unit)
                        values.add(decode(bits, descriptors, {}, {})[0].value)

                    else:
                        raise NotImplementedError(
                            "Unknown immediate operator: %s" % str(descriptor))
                else:
                    if op.neutral():
                        del operators[op.opcode]
                    else:
                        op.check_conflict(operators)
                        operators[op.opcode] = op
            elif isinstance(descriptor, SequenceDescriptor):
                seq = decode(bits, iter(descriptor.descriptors), operators,
                             descriptor_overlay)
                values.extend(seq)
            else:
                raise NotImplementedError("Unknown descriptor type: %s" %
                                          descriptor)
        return values

    def decode_compressed(bits, descriptors, n_subsets, operators,
                          descriptor_overlay):
        """
        :param bits: Bit stream to decode from
        :param descriptors: Descriptor iterator
        :param n_subsets: Number of subsets to decode
        :param dict operators: Operators in effect, indexed by opcode
        :param dict descriptor_overlay: Overlay descriptors affected by CHANGE_REFERENCE_VALUES operator
        """
        subsets = [[] for x in range(n_subsets)]
        for descriptor in descriptors:
            descriptor = descriptor_overlay.get(descriptor.code, descriptor)

            if isinstance(descriptor, ElementDescriptor):
                op_crf = operators.get(OpCode.CHANGE_REFERENCE_VALUES, None)
                if op_crf is not None:
                    dummy_descriptors = iter([
                        ElementDescriptor(fxy2int("999999"), op_crf.bits(), 0,
                                          0, "ASSOCIATED FIELD", "NUMERIC")
                    ])
                    _subsets = decode_compressed(bits, dummy_descriptors,
                                                 n_subsets, {}, {})
                    raw_vals = [subset[0].raw_value for subset in _subsets]

                    if len(set(raw_vals)) != 1:
                        raise ValueError(
                            "Encountered different reference values for different subsets: %s",
                            raw_vals)

                    ref_value = raw_vals[0]
                    top_bit_mask = (1 << op_crf.bits() - 1)
                    if ref_value & top_bit_mask:
                        ref_value = -(ref_value & ~top_bit_mask)

                    overlay_descriptor = ElementDescriptor(
                        descriptor.code, descriptor.length, descriptor.scale,
                        ref_value, descriptor.significance, descriptor.unit)
                    descriptor_overlay[descriptor.code] = overlay_descriptor
                    continue

                op_aaf = operators.get(OpCode.ADD_ASSOCIATED_FIELD, None)
                if op_aaf is not None and descriptor.code != fxy2int("031021"):
                    # Don't apply to ASSOCIATED FIELD SIGNIFICANCE
                    # Use dummy descriptor 999999 for associated field, like Geo::BUFR and libbufr
                    dummy_descriptors = iter([
                        ElementDescriptor(fxy2int("999999"), op_aaf.bits(), 0,
                                          0, "ASSOCIATED FIELD", "NUMERIC")
                    ])
                    vals = decode_compressed(bits, dummy_descriptors,
                                             n_subsets, {}, {})
                    for i, ss in enumerate(vals):
                        subsets[i].extend(ss)

                read_length = _calculate_read_length(descriptor, operators)
                if descriptor.unit == 'CCITTIA5':
                    ref_value = Bits._readhex(bits, read_length, bits.pos)
                else:
                    ref_value = Bits._readuint(bits, read_length, bits.pos)
                bits.pos += read_length

                n_bits = Bits._readuint(bits, 6, bits.pos)
                bits.pos += 6

                for i in range(n_subsets):
                    if descriptor.unit == 'CCITTIA5':
                        n_chars = n_bits
                        if n_chars:
                            raw_value = Bits._readhex(bits, n_chars * 8,
                                                      bits.pos)
                            bits.pos += n_chars * 8
                            value = _decode_raw_value(raw_value, descriptor,
                                                      operators)
                        else:
                            value = _decode_raw_value(ref_value, descriptor,
                                                      operators)
                    else:
                        if n_bits:
                            increment = Bits._readuint(bits, n_bits, bits.pos)
                            bits.pos += n_bits
                            if increment ^ ((1 << n_bits) -
                                            1) == 0:  # Missing value, all-ones
                                value = _decode_raw_value(
                                    (1 << descriptor.length) - 1, descriptor,
                                    operators)
                            else:
                                value = _decode_raw_value(
                                    ref_value + increment, descriptor,
                                    operators)
                        else:
                            value = _decode_raw_value(ref_value, descriptor,
                                                      operators)
                    subsets[i].append(value)
            elif isinstance(descriptor, ReplicationDescriptor):
                aggregations = [[] for x in range(n_subsets)]
                if descriptor.count:
                    bval = None
                    count = descriptor.count
                else:
                    bval = decode_compressed(bits,
                                             itertools.islice(descriptors, 1),
                                             n_subsets, {}, {})[0][0]
                    count = bval.value
                n_fields = descriptor.fields
                field_descriptors = list(
                    itertools.islice(descriptors, n_fields))

                if bval is None or bval.descriptor.code in REPLICATION_DESCRIPTORS:
                    # Regular replication, X elements repeated Y or <element value> times in the file
                    for _ in range(count):
                        replication = decode_compressed(
                            bits, iter(field_descriptors), n_subsets,
                            operators, descriptor_overlay)
                        for subset_idx in range(n_subsets):
                            aggregations[subset_idx].append(
                                replication[subset_idx])
                elif bval.descriptor.code in REPETITION_DESCRIPTORS:
                    # Repeated replication, X elements present once in the file, output <element value> times
                    replication = decode_compressed(bits,
                                                    iter(field_descriptors),
                                                    n_subsets, operators,
                                                    descriptor_overlay)
                    for _ in range(count):
                        for subset_idx in range(n_subsets):
                            aggregations[subset_idx].append(
                                replication[subset_idx])
                else:
                    raise ValueError(
                        "Unexpected delayed replication element %s" % bval)

                for subset_idx in range(n_subsets):
                    subsets[subset_idx].append(aggregations[subset_idx])
            elif isinstance(descriptor, OperatorDescriptor):
                op = descriptor.operator
                if op.opcode in (1, 2, 3, 4, 7):
                    if op.neutral():
                        del operators[op.opcode]
                    else:
                        op.check_conflict(operators)
                        operators[op.opcode] = op
                else:
                    raise NotImplementedError(
                        "Can only decode operators 201-204 and 207 for compressed BUFR data at the moment, please file an issue on GitHub, found operator: 2%02d"
                        % op.opcode)
            elif isinstance(descriptor, SequenceDescriptor):
                comp = decode_compressed(bits, iter(descriptor.descriptors),
                                         n_subsets, operators,
                                         descriptor_overlay)
                for i, subset in enumerate(comp):
                    subsets[i].extend(subset)
            else:
                raise NotImplementedError("Unknown descriptor type: %s" %
                                          descriptor)
        return subsets

    if compressed:
        subsets = [
            BufrSubset(x) for x in decode_compressed(bits, iter(descriptors),
                                                     n_subsets, {}, {})
        ]
    else:
        subsets = [
            BufrSubset(decode(bits, iter(descriptors), {}, {}))
            for _ in range(n_subsets)
        ]
    return Section4(length, subsets)
 def load_bin(self, filename):
   data = ConstBitStream(filename = filename)
   self.from_bin(data)
Esempio n. 9
0
    x = np.delete(x, deletes)
    y = np.delete(y, deletes)
    r = np.delete(r, deletes)

    return (x, y, r, a, b)


def plot(x, y, r):
    for i in range(len(x)):
        circle1 = plt.Circle((x[i], y[i]), r[i] / 1000, color='r')
        plt.gcf().gca().add_artist(circle1)


# read file
b = ConstBitStream(filename='manytracks.raw')

fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')

for j in range(10):
    x = np.zeros([8])
    y = np.zeros([8])
    r = np.zeros([8])
    for i in range(8):
        x[i] = (b.read(3)).uint
        y[i] = (b.read(3)).uint
        r[i] = (b.read(10)).uint
        print(x[i], y[i], r[i])

        if x[i] % 2 != 0:
Esempio n. 10
0
from d7a.dll.parser import Parser as DllFrameParser, FrameType
from d7a.serial_modem_interface.parser import Parser as SerialParser
from d7a.system_files.system_file_ids import SystemFileIds
from d7a.system_files.system_files import SystemFiles

parser_types = ["fg", "bg", "alp", "serial", "systemfile"]
argparser = argparse.ArgumentParser()
argparser.add_argument("-t", "--type", choices=parser_types, required=True)
argparser.add_argument("-f", "--file-id", help="the ID of the system file to parse", type=int)
argparser.add_argument('data', help="The data to be parsed, input as an hexstring")
args = argparser.parse_args()

hexstring = args.data.strip().replace(' ', '')
data = bytearray(hexstring.decode("hex"))
if args.type == "alp":
  print AlpParser().parse(ConstBitStream(data), len(data))
  exit(0)
if args.type == "serial":
  parser = SerialParser()
if args.type == "fg":
  parser = DllFrameParser(frame_type=FrameType.FOREGROUND)
if args.type == "bg":
  parser = DllFrameParser(frame_type=FrameType.BACKGROUND)
if args.type == "systemfile":
  file = SystemFileIds(args.file_id)
  file_type = SystemFiles().files[file]
  print(file_type.parse(ConstBitStream(data)))
  exit(0)

msgtype, cmds, info = parser.parse(data)
for cmd in cmds:
Esempio n. 11
0
#TODO: Implementar as estatísticas de comparação

import serial
import cv2
import numpy as np
from bitstring import ConstBitStream
import VLC
'''
ser = serial.Serial(
        port='/dev/ttyUSB0',
        baudrate = 100e3,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
)

data = ser.read()

'''
received_bits = ConstBitStream(filename='img_bits_encoded.bin').bin
n = 10 # chunk length
received_chunks = [received_bits[i:i+n] for i in range(0, len(received_bits), n)]

decoded = VLC.decArray8b10b(received_chunks)
img = cv2.imdecode(np.frombuffer(decoded, np.uint8), -1)
cv2.imwrite('macaquito.tiff', img)
Esempio n. 12
0
 def test_parse_three_bytes(self):
     offset_bytes = [0x20, 0x40, 0x41]
     offset = Offset.parse(ConstBitStream(bytes=offset_bytes))
     self.assertEqual(offset.offset.value, 65)
Esempio n. 13
0
 def test_parse(self):
     offset_bytes = [0x20, 0x01]
     offset = Offset.parse(ConstBitStream(bytes=offset_bytes))
     self.assertEqual(offset.id, 32)
     self.assertEqual(offset.offset.value, 1)
Esempio n. 14
0
def convert_bup(filename, out_dir):
    data = ConstBitStream(filename=filename)

    out_template = os.path.join(
        out_dir,
        os.path.splitext(os.path.basename(filename))[0])

    magic = data.read("bytes:4")
    size = data.read("uintle:32")
    ew = data.read("uintle:16")
    eh = data.read("uintle:16")
    width = data.read("uintle:16")
    height = data.read("uintle:16")

    tbl1 = data.read("uintle:32")
    base_chunks = data.read("uintle:32")
    exp_chunks = data.read("uintle:32")

    print width, height

    # Dunno what this is for.
    for i in range(tbl1):
        data.read(32)

    base = QImage(width, height, QImage.Format_ARGB32)
    # base.fill(0)
    base.fill(TRANSPARENT_COLOR)

    for i in range(base_chunks):
        offset = data.read("uintle:32")
        chunk, x, y, masked = process_chunk(data, offset)
        base = blit(chunk, base, x, y, masked)

    # base.save("%s.png" % (out_template))
    # return

    for i in range(exp_chunks):
        name = data.read("bytes:16").strip("\0").decode("CP932")
        face_off = data.read("uintle:32")
        unk1 = data.read("uintle:32")
        unk2 = data.read("uintle:32")
        unk3 = data.read("uintle:32")
        mouth1_off = data.read("uintle:32")
        mouth2_off = data.read("uintle:32")
        mouth3_off = data.read("uintle:32")

        if not face_off:
            base.save("%s_%s.png" % (out_template, name))
            # base.save("%s.png" % (out_template))
            # return
            continue

        face, x, y, masked = process_chunk(data, face_off)
        exp_base = blit(face, base, x, y, masked)

        # exp_base.save("test/%s.png" % name)
        # exp_base.save("%s_%s.png" % (out_template, name))
        # face.save("%s_%sf.png" % (out_template, name))
        # break

        for j, mouth_off in enumerate([mouth1_off, mouth2_off, mouth3_off]):
            if not mouth_off:
                continue

            mouth, x, y, masked = process_chunk(data, mouth_off)

            if not mouth:
                continue

            exp = blit(mouth, exp_base, x, y, masked)

            exp.save("%s_%s_%d.png" % (out_template, name, j))
### 
### The Super Duper Script Editor 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 General Public License for more details.
### 
### You should have received a copy of the GNU General Public License
### along with the Super Duper Script Editor.
### If not, see <http://www.gnu.org/licenses/>.
################################################################################

from bitstring import ConstBitStream
import os

EXTENSION_MAP = {
  ConstBitStream(bytes = "MIG.00.1PSP"):  ".gim",
  ConstBitStream(bytes = "LLFS"):         ".sfl",
  ConstBitStream(bytes = "RIFF"):         ".at3",
  ConstBitStream(bytes = "OMG.00.1PSP"):  ".gmo",
  ConstBitStream(hex = "0x89504E47"):     ".png",
  ConstBitStream(bytes = "BM"):           ".bmp",
  ConstBitStream(bytes = "VAGp"):         ".vag",
  ConstBitStream(bytes = "tFpS"):         ".font",
  ConstBitStream(hex = "0x41465332"):     ".awb",
#  ConstBitStream(hex = "0x7000"):         ".scp.wrd",
  ConstBitStream(hex = "0xF0306090020000000C000000"): ".p3d",
}

##################################################
### 
##################################################
Esempio n. 16
0
    def handle(self):
        """
        handle requests
        """
        while True:
            data0 = ""
            # set the socket time-out
            # if nothing is received within this time just send data to the
            # stellarium server
            self.request.settimeout(0.01)
            try:
                data0 = self.request.recv(160)
                logger.debug("Input")
                logger.debug(data0)
                data = ConstBitStream(bytes=data0, length=160)
                logger.debug(data)
                data.read("intle:16")
                mtype = data.read("intle:16")
                logger.debug("mtype: %s ", mtype)
                if mtype == command.STELLARIUM:
                    # stellarium telescope client
                    ra, dec = self._unpack_stellarium(data)
                    self.server.controller.goto(ra, dec)

                elif mtype == command.LOCATION:
                    # set observer lon/lat/alt given as three floats
                    lon, lat, alt = self._unpack_data(data, ["floatle:32"] * 3)
                    try:
                        self.server.controller.set_observer(lon, lat, alt)
                    except Exception:
                        logger.error("could not set location")
                    break

                elif mtype == command.START_CAL:
                    # start calibration
                    try:
                        self.server.controller.start_calibration()
                    except Exception:
                        logger.error("cannot start calibration")
                    break

                elif mtype == command.STOP_CAL:
                    # stop calibration
                    try:
                        self.server.controller.stop_calibration()
                    except Exception:
                        logger.error("cannot stop calibration")
                    break

                elif mtype == command.MAKE_STEP:
                    # make steps (azimuthal/altitudal steps given as two small integers)
                    azimuth_steps, altitude_steps = self._unpack_data(
                        data, ["intle:16"] * 2
                    )
                    try:
                        self.server.controller.make_step(azimuth_steps, altitude_steps)
                    except Exception:
                        logger.error("cannot make steps")
                    break

                elif mtype == command.START_MOT:
                    # start or stop motor
                    motor_id, action, direction = self._unpack_data(
                        data, ["intle:16"] * 3
                    )
                    self.server.controller.start_stop_motor(motor_id, action, direction)
                    break

                elif mtype == command.SET_ANGLE:
                    # set the angle of the motors to given object_id (small integer)
                    # this shall be defined in the controller class
                    object_id = self._unpack_data(data, "intle:16")
                    try:
                        self.server.controller.set_object(object_id)
                    except Exception:
                        logger.error("cannot set controller to given object")
                    break

                elif mtype == command.TOGGLE_TRACK:
                    # toggle tracking (earth rotation compensation)
                    try:
                        self.server.controller.toggle_tracking()
                    except Exception:
                        logger.error("cannot toggle tracking")
                    break

                elif mtype == command.APPLY_OBJECT:
                    # apply the angle of the motors to given object_id (small integer)
                    # this shall be defined in the controller class
                    try:
                        self.server.controller.apply_object()
                    except Exception:
                        logger.error("cannot apply controller to given object")
                    break

                elif mtype == command.RAS_SHUTDOWN:
                    # Shutdown Rasberry
                    try:
                        subprocess.run("halt", shell=True)
                    except Exception:
                        logger.error("cannot shutdown rasberry")
                    break

                elif mtype == command.RAS_RESTART:
                    # Reboot Rasberry
                    try:
                        subprocess.run("reboot", shell=True)
                    except Exception:
                        logger.error("cannot restart rasberry")
                    break

                elif mtype == command.TEL_RESTART:
                    # Reboot Rasberry
                    try:
                        subprocess.run(
                            "systemctl restart telescoped.service", shell=True
                        )
                    except Exception:
                        logger.error("cannot restart telescope-server")
                    break

                elif mtype == command.STATUS:
                    # get the status of the controller by status_code (small integer)
                    status_code = self._unpack_data(data, "intle:16")
                    try:
                        response = self.server.controller.get_status(status_code)
                        logger.debug("response: %s ", response)
                        self.request.sendall(response.encode())
                        sleep(0.01)
                    except Exception as exc:
                        logger.error(f"{exc} cannot get status of controller")
                    break

            except Exception:
                # no data received
                # send current position
                ra, dec = self.server.controller.current_pos()
                sdata = self._pack_stellarium(ra, dec)
                try:
                    self.request.send(sdata.bytes)
                except Exception:
                    pass
                sleep(0.5)
Esempio n. 17
0
def find_offset(of, in_data):
    bitstream = ConstBitStream(in_data)
    offset = bitstream.find(of, bytealigned=False)
    return offset
Esempio n. 18
0
from PyQt4.QtGui import QImage, QColor

from bitstring import ConstBitStream
import os.path
import re

from clt import CLT_STYLES

# BASE_DIR      = os.path.dirname(os.path.abspath(__file__))

# GFX_DIR         = common.editor_config.gfx_dir
# FONT_FOLDER   = os.path.join(GFX_DIR, "font")
FONT1_TABLE   = "Font01.font"
FONT2_TABLE   = "Font02.font"

SPFT_MAGIC     = ConstBitStream(hex='0x7446705304000000')

RE_DEFAULT_CLT = re.compile(ur"<CLT>", re.UNICODE | re.S)
RE_CLT         = re.compile(ur"\<CLT (?P<CLT_INDEX>\d+)\>", re.UNICODE | re.S)
RE_DIG         = re.compile(ur"<DIG.*?>", re.UNICODE | re.S)
RE_RUB         = re.compile(ur"<RUB.*?>.*?<RUB>", re.UNICODE | re.S)

FONT_DATA = { 1: {}, 2: {} }

def parse_font(font_num, spft_filename):
  
  FONT_DATA[font_num] = {}
  
  # spft_filename = ""
  
  # if font_num == 1:
Esempio n. 19
0
    def __init__(self, HexMessage):
        self.fullStream = ConstBitStream('0x' + HexMessage)
        #Service Centre Address
        #######################

        #  Read SCA Length [1]
        self.SCALen = self.fullStream.read('int:8') - 1

        #  Read Type [1]
        self.SCATyp = self.fullStream.read('hex:8')

        #  Read Address [SCALen x 8]
        temp = self.fullStream.read('hex:%i' % (self.SCALen * 8))

        #  Format to readable phone number
        result = ""
        for x in range(1, len(temp), 2):
            result = result + temp[x] + temp[x - 1]
        self.SCANum = "+" + result[:result.find('f')]

        #PDU Type [1]
        self.PDUTyp = self.fullStream.read('bin:8')
        self.Multipart = self.PDUTyp[1:2]

        #Origination Address
        ####################

        #  Read Length [1]
        self.OALen = self.fullStream.read('int:8') - 5

        #  Read Type [1]
        self.OATyp = self.fullStream.read('hex:8')

        #  Read Address [OALen x8]
        temp = self.fullStream.read('hex:%i' % (self.OALen * 8))

        #  Format to readable phone number
        result = ""
        for x in range(1, len(temp), 2):
            result = result + temp[x] + temp[x - 1]
        self.OANum = "+" + result[:result.find('f')]

        #PDU PID [1]
        self.PID = self.fullStream.read('hex:8')

        #PDU DCS [1]
        self.DCS = self.fullStream.read('hex:8')

        #Timestamp
        ##########
        temp = self.fullStream.read('hex:56')
        #  Format Timestamp
        result = ""
        for x in range(1, len(temp), 2):
            result = result + temp[x] + temp[x - 1]
        self.SCTS = "20" + result
        #User Data
        ##########

        #User Data Length
        self.UDL = self.fullStream.read('uint:8')
        #User Data Header if Multipart
        if (self.Multipart == '1'):
            self.UDHL = self.fullStream.read('int:8')
            self.UDH = self.fullStream.read('hex:40')
        else:
            self.UDHL = 0
            self.UDH = ''
        #Calculate number of bits to read, make sure to read full 8 bytes.
        if ((self.UDL % 8) != 0):
            bitsToRead = (self.UDL * 7) + (8 - (self.UDL * 7) % 8)
        else:
            bitsToRead = self.UDL * 7
        if (self.UDHL != 0):
            bitsToRead = bitsToRead - (8 * (self.UDHL + 1))
        print("PDU bitsToRead = %i" % bitsToRead)
        temp = self.fullStream.read('hex:%i' % bitsToRead)
        #Read User Data
        print("PDU Bytes = %s" % temp)
        #Format User Data and reverse order.
        result = ''
        for x in range(0, len(temp), 2):
            result = temp[x] + temp[x + 1] + result
        print(result)
        print("PDU Reversed Bytes = %s" % result)
        self.UD = ConstBitStream('0x' + result)
        #Make a Bits format string in UDbin
        print("PDU len(result) = %i" % len(result))
        self.UDbinL = len(result) * 4
        self.UDbin = self.UD.read('bin:%i' % (self.UDbinL))
        print("PDU UDBin = %s" % self.UDbin)
Esempio n. 20
0
def parse_font(font_num, spft_filename):
  
  FONT_DATA[font_num] = {}
  
  # spft_filename = ""
  
  # if font_num == 1:
    # spft_filename = os.path.join(FONT_FOLDER, FONT1_TABLE)
  # elif font_num == 2:
    # spft_filename = os.path.join(FONT_FOLDER, FONT2_TABLE)
  if not font_num in [1, 2]:
    print "Invalid font number. Valid values: 1, 2"
    return None
  
  # Header: 
  # 74467053 -- Magic
  # 04000000 -- Magic
  # XXXXXXXX -- Number of entries in font table
  # XXXXXXXX -- Position of first entry in font table
  # 
  # XXXXXXXX -- Number of chunks in the mappings table
  # XXXXXXXX -- Start position of mappings table (little-endian, as always)
  #             ***0x20000000 in both fonts I've seen
  # XXXXXXXX -- ????
  # XXXXXXXX -- ????
  # 
  # Character Mappings: from start pos (0x20) to (start pos + (# chunks * 2))
  #   * To avoid overcomplicating this, I'm just referring to the start pos as
  #     0x20 since I've only ever seen that value used.
  #   * Two-byte chunks (XXXX)
  #   * The position of each chunk, minus 0x20, divided by two (because they're
  #     two-byte chunks), equals the UTF-16 representation of a character.
  #     (i.e. pos 0x00A8: (0x00A8 - 0x20) / 2 = 0x0044 -> "A")
  #   * The value of each chunk is the index of that character in the font table,
  #     little-endian.
  #     (i.e. if the character "A" is the 35th entry, zero-indexed = 0x2200)
  #   * A chunk value of 0xFFFF means that character is not present in the font.
  spft = ConstBitStream(filename = spft_filename)
  
  magic = spft.read(64)
  
  if magic != SPFT_MAGIC:
    print "Didn't find SPFT magic."
    exit()
  
  num_entries = spft.read('uintle:32')
  table_start = spft.read('uintle:32')
  
  if num_entries == 0:
    print "No entries in SPFT table."
    return None
  
  if table_start * 8 > spft.len:
    print "Invalid SPFT table position."
    return None
  
  #print "Characters in font:", num_entries
  
  spft.pos = table_start * 8
  
  # Table:
  # * Entry:
  #   XXXX -- Character
  #   XXXX -- X Pos
  #   XXXX -- Y Pos
  #   XXXX -- Width
  #   XXXX -- Height
  #   0000 -- Padding
  #   0000 -- Padding
  #   FA08 -- Something to do with rendering offset. FA -> -6 -> renders six pixels down
  
  #print "    XXXX YYYY WWW HHH"
  
  for i in range(0, num_entries):
    char    = spft.read(16)
    char    = char.bytes.decode('utf-16le')    
    xpos    = spft.read('uintle:16')
    ypos    = spft.read('uintle:16')
    width   = spft.read('uintle:16')
    height  = spft.read('uintle:16')
    dummy   = spft.read('uintle:16')
    dummy   = spft.read('uintle:16')
    yshift  = spft.read('intle:8')
    dummy   = spft.read('uintle:8')
    
    info = {'x': xpos, 'y': ypos, 'w': width, 'h': height}
    FONT_DATA[font_num][char] = info
    def create_archives(self):

        try:
            self.width = self.parent.width()
            self.height = self.parent.height()
            self.x = self.parent.x()
            self.y = self.parent.y()
        except:
            self.width = 1920
            self.height = 1080
            self.x = 0
            self.y = 0

        self.file_count = 0

        self.progress = QProgressDialog("Reading...", QtCore.QString(), 0,
                                        72000, self.parent)
        self.progress.setWindowModality(Qt.Qt.WindowModal)
        self.progress.setValue(0)
        self.progress.setAutoClose(False)
        self.progress.setMinimumDuration(0)

        # with open(common.editor_config.eboot_orig, "rb") as f:
        with open(
                os.path.join(common.editor_config.iso_dir, "PSP_GAME",
                             "SYSDIR", "EBOOT.BIN"), "rb") as f:
            eboot = BitStream(bytes=f.read())

        eboot, eboot_offset = eboot_patch.apply_eboot_patches(eboot)

        USRDIR = os.path.join(common.editor_config.iso_dir, "PSP_GAME",
                              "USRDIR")

        # So we can loop. :)
        ARCHIVE_INFO = [
            {
                "toc": UMDIMAGES.umdimage,
                "dir": common.editor_config.umdimage_dir,
                "dat": os.path.join(USRDIR, "umdimage.dat"),
                "name": "umdimage.dat",
                "pack": common.editor_config.pack_umdimage,
                "eof": False,
            },
            {
                "toc": UMDIMAGES.umdimage2,
                "dir": common.editor_config.umdimage2_dir,
                "dat": os.path.join(USRDIR, "umdimage2.dat"),
                "name": "umdimage2.dat",
                "pack": common.editor_config.pack_umdimage2,
                "eof": False,
            },
            {
                "toc": None,
                "dir": common.editor_config.voice_dir,
                "dat": os.path.join(USRDIR, "voice.pak"),
                "name": "voice.pak",
                "pack": common.editor_config.pack_voice,
                "eof": True,
            },
            {
                "toc": None,
                "dir": common.editor_config.bgm_dir,
                "dat": os.path.join(USRDIR, "bgm.pak"),
                "name": "bgm.pak",
                "pack": common.editor_config.pack_bgm,
                "eof": True,
            },
        ]

        for archive in ARCHIVE_INFO:

            if not archive["pack"]:
                continue

            self.progress.setWindowTitle("Building " + archive["name"])

            toc_info = {}
            file_list = None

            if archive["toc"]:
                file_list = []

                toc = get_toc(eboot, archive["toc"])

                for entry in toc:
                    filename = entry["filename"]
                    pos_pos = entry["file_pos_pos"]
                    len_pos = entry["file_len_pos"]

                    toc_info[filename] = [pos_pos, len_pos]
                    file_list.append(filename)

            # Causes memory issues if I use the original order, for whatever reason.
            file_list = None

            with io.FileIO(archive["dat"], "w") as handler:
                table_of_contents = self.pack_dir(archive["dir"],
                                                  handler,
                                                  file_list=file_list,
                                                  eof=archive["eof"])

            # We're playing fast and loose with the file count anyway, so why not?
            self.file_count += 1
            self.progress.setValue(self.file_count)
            self.progress.setLabelText("Saving " + archive["name"] + "...")

            if archive["toc"]:
                for entry in table_of_contents:
                    if not entry in toc_info:
                        _LOGGER.warning(
                            "%s missing from %s table of contents." %
                            (entry, archive["name"]))
                        continue

                    file_pos = table_of_contents[entry]["pos"]
                    file_size = table_of_contents[entry]["size"]

                    eboot.overwrite(BitStream(uintle=file_pos, length=32),
                                    toc_info[entry][0] * 8)
                    eboot.overwrite(BitStream(uintle=file_size, length=32),
                                    toc_info[entry][1] * 8)

            del table_of_contents

        self.progress.setLabelText("Saving EBOOT.BIN...")
        self.progress.setValue(self.progress.maximum())

        # Text replacement
        to_replace = eboot_text.get_eboot_text()
        for replacement in to_replace:

            orig = bytearray(replacement.orig, encoding=replacement.enc)

            # If they left something blank, write the original text back.
            if len(replacement.text) == 0:
                data = orig
            else:
                data = bytearray(replacement.text, encoding=replacement.enc)

            pos = replacement.pos.int + eboot_offset

            padding = len(orig) - len(data)
            if padding > 0:
                # Null bytes to fill the rest of the space the original took.
                data.extend(bytearray(padding))

            data = ConstBitStream(bytes=data)
            eboot.overwrite(data, pos * 8)

        eboot_out = os.path.join(common.editor_config.iso_dir, "PSP_GAME",
                                 "SYSDIR", "EBOOT.BIN")

        with open(eboot_out, "wb") as f:
            eboot.tofile(f)

        self.progress.close()
### or (at your option) any later version.
###
### The Super Duper Script Editor 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 General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with the Super Duper Script Editor.
### If not, see <http://www.gnu.org/licenses/>.
################################################################################

from bitstring import ConstBitStream
import os

AWB_MAGIC = ConstBitStream(hex="0x41465332")  # AFS2


##################################################
###
##################################################
def get_awb_files(data, *args, **kwargs):

    magic = data.read(32)
    if not magic == AWB_MAGIC:
        print "Invalid AWB file."
        return

    unknown1 = data.read(32)
    num_entries = data.read("uintle:32")
    alignment = data.read("uintle:32")
Esempio n. 23
0
    def to_data(self):
        data = \
          ConstBitStream(uintle = self.file_num, length = 16) + \
          [key for key, value in LINE_TYPE_MAP.iteritems() if value == self.line_type][0] + \
          ConstBitStream(uintle = self.ammo_id, length = 16) + \
          ConstBitStream(uintle = self.converted_id, length = 16) + \
          self.unknown1 + \
          ConstBitStream(uintle = self.weak_point, length = 16) + \
          ConstBitStream(intle = self.delay, length = 16) + \
          ConstBitStream(intle = self.orientation, length = 16) + \
          ConstBitStream(intle = self.in_effect, length = 16) + \
          ConstBitStream(intle = self.out_effect, length = 16) + \
          ConstBitStream(intle = self.time_visible, length = 16) + \
          ConstBitStream(intle = self.x_start, length = 16) + \
          ConstBitStream(intle = self.y_start, length = 16) + \
          ConstBitStream(intle = self.velocity, length = 16) + \
          ConstBitStream(intle = self.angle, length = 16) + \
          ConstBitStream(intle = self.zoom_start, length = 16) + \
          ConstBitStream(intle = self.zoom_change, length = 16) + \
          ConstBitStream(intle = self.shake, length = 16) + \
          ConstBitStream(intle = self.rot_angle, length = 16) + \
          ConstBitStream(intle = self.spin_vel, length = 16) + \
          ConstBitStream(uintle = self.char_id, length = 16) + \
          ConstBitStream(uintle = self.sprite_id, length = 16) + \
          self.unknown3 + \
          ConstBitStream(uintle = self.voice_id, length = 16) + \
          self.unknown4 + \
          ConstBitStream(uintle = self.chapter, length = 16) + \
          self.unknown5 + \
          self.unknown6

        return data
Esempio n. 24
0

#Main Program###

if not os.path.exists(args.infile):
    print(
        'Error! Given input file name not found! Please check path given in CMD or set in script code!'
    )
    sys.exit()

F = args.infile

print('Opened file ' + F)

print('Analyzing...')
s = ConstBitStream(filename=F)
print(s[32:96])
if s[32:96] != '0x6674797058415643':
    print(
        'No XAVC type tag detected. Please user original XAVC MP4 file. Exiting.'
    )
    sys.exit()

### Get filesize ###
filesize = os.path.getsize(F)

#check for mdat atom tag
sampl_check = s.find('0x6D646174000000', bytealigned=True)
if len(sampl_check) != 0:
    #s.bytepos+=13
    #sampl_string = s.read(4*8)
Esempio n. 25
0
    def run(self):

        self.stop_flg = False
        sch = 0

        try:

            stream = ConstBitStream(filename=self.file)

            while True and not self.stop_flg:

                I = None
                Q = None

                # ----- читаем I
                packet = stream.read(16)

                if self.endian == 'Big Endian':
                    I = packet.unpack('intbe:16')[0]
                elif self.endian == 'Little Endian':
                    I = packet.unpack('intle:16')[0]

                # ----- читаем Q

                packet = stream.read(16)

                if self.endian == 'Big Endian':
                    Q = packet.unpack('intbe:16')[0]
                elif self.endian == 'Little Endian':
                    Q = packet.unpack('intle:16')[0]

                # ----- вычисляем

                val = math.atan2(Q, I)

                val = val * 180 / np.pi

                val = (val + 360) % 360

                val = round(val)

                struct = IQData()

                struct.I = I
                struct.Q = Q
                struct.DEG = val

                if 0:
                    self.plot_signal.emit(struct)
                else:
                    for i in range(random.randint(1, 5)):
                        self.plot_signal.emit(struct)
                        time.sleep(self.tout / 1000)

                if self.tout:
                    time.sleep(self.tout / 1000)

                sch = sch + 1

        except:
            raise
            pass
        finally:
            self.end_signal.emit(sch)
### You should have received a copy of the GNU General Public License
### along with the Super Duper Script Editor.
### If not, see <http://www.gnu.org/licenses/>.
################################################################################

from bitstring import ConstBitStream, BitArray
import os
import time

from ctypes import byref, cast, cdll, create_string_buffer, c_long, c_char, POINTER
cpk_decompress = cdll.LoadLibrary('extract/cpk_uncompress.dll')

############################################################
### CONSTANTS
############################################################
CRILAYLA_MAGIC = ConstBitStream(hex = "0x4352494C41594C41") # "CRILAYLA"

VLE_LEVELS = 4
VLE_LENS   = [2, 3, 5, 8]

############################################################
### VARIABLES
############################################################
__offset    = 0
__bit_pool  = 0
__bits_left = 0
__data      = bytes()

############################################################
### FUNCTIONS
############################################################
Esempio n. 27
0
        bitStream.pos = start
        readLength = (end - start) - 8
        hexSlice = bitStream.read(readLength).hex
        out = bytearray.fromhex(hexSlice).decode()
        return out

    def getinputURL(self):
        return self.inputURL


man = manifestRequest()
man.inputURL = argv[1]
print(man.getinputURL())

#get the position of the player
bs = ConstBitStream(downloadfile(URL))
#bs = ConstBitStream(filename='test.txt')
ss = b'html5player.setVideoHLS'
os = 0
playerPosition = getPostion(bs, ss, os)

#starting from the player, get the position of the first tic.
ss = '0x27'
os = playerPosition
startTic = getPostion(bs, ss, os)

#starting from the first tic, get position of second tic.
ss = '0x27'
os = startTic + 8
endTic = getPostion(bs, ss, os)
def decompress_cpk_py(data, extract_size):
  global __offset
  global __bit_pool
  global __bits_left
  global __data
  
  data.pos = 0
  magic = data.read(64)
  
  if not magic == CRILAYLA_MAGIC or magic.uintle == 0:
    print "Didn't find 0 or CRILAYLA signature for compressed data."
    return data
  
  uncompressed_size          = data.read("uintle:32")
  uncompressed_header_offset = data.read("uintle:32") + 0x10
  
  if uncompressed_header_offset + 0x100 != data.len / 8:
    print "Size mismatch."
    return data
  
  __data = data.bytes
  
  #uncompressed_header = data[uncompressed_header_offset * 8 : (uncompressed_header_offset + 0x100) * 8]
  uncompressed_header = __data[uncompressed_header_offset : uncompressed_header_offset + 0x100]
  out_data = bytearray(uncompressed_size + 0x100)
  out_data[:0x100] = uncompressed_header
  
  __offset     = len(__data) - 0x100 - 1
  __bit_pool   = 0
  __bits_left  = 0
  output_end   = 0x100 + uncompressed_size - 1
  bytes_output = 0
  
  while bytes_output < uncompressed_size:
  
    if __get_next_bits(1):
      backref_offset = output_end - bytes_output + __get_next_bits(13) + 3
      backref_length = 3
      
      # Says "decode variable length coding for length"
      # in the original C, but I dunno what that means.
      vle_level  = 0
      
      for i in xrange(VLE_LEVELS):
        this_level = __get_next_bits(VLE_LENS[vle_level])
        backref_length += this_level
        
        if this_level != ((1 << VLE_LENS[vle_level]) - 1):
          break
        
        vle_level += 1
      
      if vle_level == VLE_LEVELS:
        this_level == 255
        while this_level == 255:
          this_level = __get_next_bits(8)
          backref_length += this_level
      
      for i in xrange(backref_length):
        out_data[output_end - bytes_output] = out_data[backref_offset]
        
        backref_offset -= 1
        bytes_output   += 1
    
    else:
      # Verbatim byte.
      out_data[output_end - bytes_output] = __get_next_bits(8)
      bytes_output += 1
  
  # while bytes_output < uncompressed_size
  
  return ConstBitStream(bytes = out_data)
Esempio n. 29
0
def zfs_search(search_file_path, pool_name, target_pool_name="kdiwlcik", buffer_size=buffer_size_default, print_starts_ends_only=False, starts=None, ends=None, tmp_dir="/tmp", loop_device=sp.check_output([losetup, "-f"]).strip(), ):
    #if search_file_path != None:
    #    search_bytes = open(search_file_path, "r")
    #else:
    #    search_bytes = sys.stdin
    #    logger.info("no input file specified, reading from stdin")
    
    #raise RuntimeError("as long issue https://github.com/zfsonlinux/zfs/issues/2830 isn't fixed in the zpool command it doesn't make sense to use the script because copying files takes > 1 month regarding the number of combination, data volume and decreasing performance of a driver becoming fuller and fuller - on the other hand if it is fixed or a workaround found the script will be very efficient")
    
    # check privileges first (this is polite)
    if os.getuid() != 0:
        raise RuntimeError("Privileges are necessary to invoke zpool import tests, exiting")
    # validate loop_device option (assume that losetup -f always returns a 
    # valid and usable result)
    if re.match("/dev/loop[0-9]+", loop_device) is None:
        raise ValueError("loop_device '%s' is not a valid loop device specification" % (loop_device,))
        # @TODO: more checks for loop_device adequacy are necessary
    logger.info("using loop device %s" % (loop_device,))

    if print_starts_ends_only and (starts != None or ends != None):
        raise ValueError("starts or ends specified together with print-starts-ends-only which doesn't make sense (you don't want simply the list you specified on command line to be printed)")

    if search_file_path is None:
        raise ValueError("search_file_path mustn't be None")
    if not os.path.exists(search_file_path):
        raise ValueError("search_file_path '%s' doesn't exist" % (search_file_path,))
    #if not os.path.isfile(search_file_path): # return false for device files
    #    raise ValueError("search_file_path '%s' isn't a file, but has to be" % (search_file_path,))
    search_bytes = open(search_file_path, "r")

    # Can initialise from files, bytes, etc.
    if starts is None or ends is None:
        s = ConstBitStream(search_bytes) # if argument name is omitted, auto is used
    if starts is None:
        bitstring_result = list(s.findall(zfs_start_bytes, bytealigned=True))
        bytealigned_bitstring_result = [x/8 for x in bitstring_result]
        starts = sorted(bytealigned_bitstring_result) # ConstBitStream.find returns the bit position (rather than byte position)
        logger.info("found starts '%s'" % (str(starts)))
    else:
        starts = [int(x) for x in starts]
        logger.info("using starts\n%s\nspecified on command line" % (str(starts)))
    if ends is None:
        bitstring_result = list(s.findall(zfs_end_bytes, bytealigned=True))
        bytealigned_bitstring_result = [x/8 for x in bitstring_result]
        ends = sorted(bytealigned_bitstring_result, reverse=True) # reverse causes testing from largest possible to smallest possible (this eventually causes I/O overhead, but possibility to find a pool in the largest set is much higher)
        logger.info("found ends '%s'" % (str(ends)))
    else:
        ends = [int(x) for x in ends]
        logger.info("using ends\n%s\nspecified on command line" % (str(ends)))

    if print_starts_ends_only:
        logger.info("found start points\n%s\n and end points\n%s\nExiting" % (str(starts), str(ends)))
        return
    logger.info("discarding start positions which don't have a corresponding start position after %s bytes" % (label_size, ))
    valid_starts = []
    for start in starts:
        for start0 in starts:
	    if start0 == start + label_size:
	        valid_starts.append(start)
	        # do nothing with start0 because it might be valid for some strange reason, but if it is, it is more valid than start because there're always 2 ZFS pool labels
    logger.info("The remaining valid starts are '%s'" % (str(valid_starts)))
    starts = valid_starts
    
    logger.info("discarding end positions which don't have a corresponding end position after %s bytes" % (label_size, ))
    valid_ends = []
    for end in ends:
        for end0 in ends:
	    if end0 == end+ label_size:
	        valid_ends.append(end)
    logger.info("The remaining valid ends are '%s'" % (str(valid_ends)))
    ends = valid_ends

    logger.info("trying all %s combinations of %s start and %s end points from largest to smallest eventual result" % (str(len(starts)*len(ends)), str(len(starts)), str(len(ends)), ))
    for start in starts:
        for end in ends:
            if end < start:
                logger.info("skipping occurance of end at '%s' before start at '%s'" % (str(end),str(start)))
                continue
            end = end + zfs_end_tail_bytes_count
            
            sp.check_call([losetup, "-o", str(start), "--sizelimit", str(end-start), loop_device, search_file_path])
            logger.info("mounting subset of search file %s from byte %s to byte %s under %s" % (search_file_path, str(start), str(end), loop_device))
            
            # test zpool import
            zpool_import_process = sp.Popen([zpool, "import", "-D", ], stdout=sp.PIPE, stderr=sp.PIPE) # there's no difference in returncode between no results and successful listing of possible import -> parse output
            zpool_import_process.wait()
            zpool_import_output_tuple = zpool_import_process.communicate()
            zpool_import_output = zpool_import_output_tuple[1] # no pool available message is written onto stderr; subprocess.Popen.communicate can be invoked only once, second invokation causes error because performing I/O on closed file
            zpool_import_output_stdout = zpool_import_output_tuple[0]
            logger.debug("The output of the ''zpool import'' test command is:\n%s\n\n\n%s\n\n" % (zpool_import_output, zpool_import_output_stdout, ))
            if zpool_import_process.returncode == 0 and (zpool_import_output is None or (zpool_import_output.strip() != "no pools available to import" and not "state: UNAVAIL" in zpool_import_output)) and (zpool_import_output_stdout is None or (not "state: UNAVAIL" in zpool_import_output and not "state: UNAVAIL" in zpool_import_output_stdout)): # zpool import returncodes are useless (finding a pool with status UNAVAIL returns 0) 
                logger.info("interval %s to %s possibly contains a valid zpool. The output of the ''zpool import'' test command is:\n%s\n\n\n%s\n\nSkipping further search" % (str(start), str(end), zpool_import_output, zpool_import_output_stdout))
                logger.info("the loop device '%s' still provides the pool. Move the data from it to a reliable pool as soon as possible and detach the loop device yourself." % (loop_device,))
                return
            sp.check_call([losetup, "-d", loop_device]) # only detach loop device if import wasn't successful because otherwise the newly rescued pooled is displayed as faulted because the underlying loop device is no longer available

    return
Esempio n. 30
0
    def load(self, filename):
        filename = filename.lower()

        if not filename in MTB_DIR:
            _LOGGER.error("Invalid MTB file: %s" % filename)
            return

        self.filename = filename

        script_dir = MTB_DIR[filename]
        self.script_pack = ScriptPack(script_dir,
                                      common.editor_config.umdimage_dir)

        # --- MTB FORMAT ---
        # 12 bytes     -- ???
        # XX XX XX XX  -- Table offset
        # 24 bytes     -- ???
        #
        # XX XX        -- MTB Index
        # XX XX        -- Char ID for sprites
        # XX XX        -- Char ID for voices (chapter for voices is 0x63)
        # XX XX        -- Initial sprite ID (?)

        mtb = ConstBitStream(filename=os.path.join(
            common.editor_config.umdimage_dir, self.filename))

        mtb.read(12 * 8)
        table_offset = mtb.read("uintle:32")
        mtb.read(24 * 8)

        mtb_index = mtb.read("uintle:16")
        sprite_char = mtb.read("uintle:16")
        voice_char = mtb.read("uintle:16")
        sprite_id = mtb.read("uintle:16")

        sprite = SpriteId(SPRITE_TYPE.stand, sprite_char, sprite_id)

        # --- TABLE FORMAT ---
        # XX XX XX XX -- Number of files
        #
        # [for each line]
        # XX XX XX XX -- Offset (from table start) of voice info.
        #
        # -- Voice Info --
        # XX XX -- File ID
        # XX XX -- Voice ID (with char ID above and the chapter ID 0x63, we know which voice file to use)

        mtb.bytepos = table_offset
        num_files = mtb.read("uintle:32")

        for i in range(num_files):
            voice_offset = mtb.read("uintle:32")

            # Store our position in the table so we can jump back to it.
            table_pos = mtb.bytepos
            mtb.bytepos = table_offset + voice_offset

            file_id = mtb.read("uintle:16")
            voice_id = mtb.read("uintle:16")

            # Chapter is 0x63, which is where the non-Trial voice samples are stored,
            # but I don't see the information actually recorded in the MTB files,
            # so I'm magic-numbering it here.
            voice = VoiceId(voice_char, 0x63, voice_id)

            self.script_pack[file_id].scene_info.sprite = sprite
            self.script_pack[file_id].scene_info.voice = voice

            # Restore it to our old position.
            mtb.bytepos = table_pos