コード例 #1
0
ファイル: decoder.py プロジェクト: zwcdp/fatty
    def decode_boot_sector(self, stream: bitstring.BitStream):
        self.bpb = BiosParameterBlock()

        jmp, oem = stream.readlist(['bytes:3', 'bytes:8'])
        self.bpb.bytes_per_sector = stream.read('uintle:16')
        self.bpb.sectors_per_cluster = stream.read('uintle:8')
        self.bpb.reserved_sectors = stream.read('uintle:16')

        self.bpb.fats_count = stream.read('uintle:8')
        roots = stream.read('uintle:16')
        sectors = stream.read('uintle:16')
        self.bpb.media_descriptor = stream.read('bytes:1')
        sec_per_fat = stream.read('uintle:16')
        self.bpb.sec_per_track = stream.read('uintle:16')
        self.bpb.heads = stream.read('uintle:16')
        self.bpb.hidden_sectors = stream.read('uintle:32')

        self.bpb.sectors = stream.read('uintle:32')
        self.bpb.sec_per_fat = stream.read('uintle:32')

        active_fat, _, fat_is_mirrored, _ = stream.readlist([4, 2, 1, 9])
        fs_version = stream.read('uintle:16')
        self.bpb.root_cluster = stream.read('uintle:32')
        fs_info = stream.read('uintle:16')
        backup_boot_sector = stream.read('uintle:16')
        _, drive_num, _, boot_sig, volume_id, volume_label, fs_type = stream.readlist([
            'bytes:12', 'uintle:8', 'bytes:1', 'uintle:8', 'uintle:32', 'bytes:11', 'bytes:8'
        ])
        remaining_stuff = stream.read('bytes:' + str(self.bpb.bytes_per_sector - stream.bytepos))

        self.skip_sectors(stream, n=self.bpb.reserved_sectors - 1)
コード例 #2
0
ファイル: stegano.py プロジェクト: Aveheuzed/secrets_suite
def unformat_message(text:BitStream)->bytes :
    """Raises ValueError if the message has been tampered with."""
    try :
        sha, len = text.readlist("bytes:64, ue")
        message = text.read(f"bytes:{len}")
    except ReadError as e :
        raise ValueError("No valid message found — read error.") from e
    if hashlib.sha512(message).digest() != sha :
        raise ValueError("No valid message found — SHA mismatch.")
    return message
コード例 #3
0
def load_from_file(model_file, bits_per_block, p_scale_number, seeds_number):
    """Retrieve the p_scales and the seeds from file

    Parameters
    ---------

    model_file: str
    bits_per_block: int
    p_scale_number: int
        How many p scales do we read
    seeds_number: int
        How many seeds do we read
    """
    with open(model_file, mode='rb') as f:
        stream = BitStream(f)

    p_scale_vars = list()
    for index in range(p_scale_number):
        p_scale_vars += stream.readlist('float: {}'.format(P_SCALE_LENGTH))
    seeds = list()
    for index in range(seeds_number):
        seeds += stream.readlist('uint: {}'.format(bits_per_block))

    return p_scale_vars, seeds
コード例 #4
0
def DecodeGSM(gsm):
    '''
    Decode the encoded message from the GSM 03.38 character set.

    Args:
        gsm: The GSM 03.38 encoded text as byte array.

    Returns:
        The decoded text as UTF-8 encoded byte array.
    '''
    # convert to bit stream
    bits = BitStream(gsm)

    # get the indices, characters, and merge together
    res = ''.join(__gsm[idx]
                  for idx in bits.readlist('{}'.format(len(bits) // 7) +
                                           '*uint:7')).encode('utf-8')

    # strip last character in case we have a perfect wrapping
    if len(bits) % 7 == 0 and chr(res[-1]):
        res = res[0:len(res) - 1]

    return res
コード例 #5
0
def DecodeSixbit(six):
    '''
    Decode the encoded message from the AIS Sixbit ASCII character set.

    Args:
        six: The AIS Sixbit encoded text as byte array.

    Returns:
        The decoded text as UTF-8 encoded byte array.
    '''
    # convert to bit stream
    bits = BitStream(six)

    # get the indices, characters, and merge together
    res = ''.join(__sixbit[idx]
                  for idx in bits.readlist('{}'.format(len(bits) // 6) +
                                           '*uint:6')).encode('utf-8')

    # strip last character in case we have a perfect wrapping
    if len(bits) % 6 == 0 and chr(res[-1]):
        res = res[0:len(res) - 1]

    return res
コード例 #6
0
class CIQA(common_classes.LossyCompression):
    def encode_image(self):
        ##### Read input image and separate into coding blocks.
        self._separate_blocks()
        ##### Quantize image.
        self._compute_coefficients()
        ##### Instantiate and write bitstring
        self._write_bitstring()
        return

    def decode_binary(self):
        ##### Obtain bitstring
        self._get_bitstring()
        ##### Read header and decode bitstring.
        self._read_coefficientes()

    def _compute_coefficients(self):
        ##### Get minimum and maximum values by patch
        min_values = np.expand_dims(np.amin(self.patches, axis=2), axis=2)
        max_values = np.expand_dims(np.amax(self.patches, axis=2), axis=2)

        ##### Compute deltas
        deltas = (max_values - min_values) / self.M

        ##### Quantize patches
        normalized_patches = self.patches - min_values
        quantized_patches = np.clip(np.floor(normalized_patches / deltas), 0,
                                    self.M - 1)

        ##### Stack information to be sent to the decoder and reshape them.
        info_to_be_sent = np.concatenate(
            (min_values, max_values, quantized_patches), axis=2)
        info_to_be_sent = np.reshape(info_to_be_sent, (int(
            np.prod(info_to_be_sent.shape[:2])), info_to_be_sent.shape[-1]))
        self.info_to_be_sent = info_to_be_sent.astype(np.uint8)

        return

    def _write_bitstring(self):
        ##### Write information about patch size and quantization levels.
        # The first is the difference between width and height:
        # NOTE: The maximum patch dimension is 32, while the maximum quantization level is 16.
        self.bitstring = BitStream(
            f'int:7={self.patches.shape[1] - self.patches.shape[0]}, '
            f'uint:5={self.N - 1}, uint:4={self.M - 1}')

        ##### Write patch coefficients into the bitstring.
        bits_for_quantized_values = int(np.ceil(math.log2(self.M)))
        for patch_information in self.info_to_be_sent:
            ##### Write minimum and maximum values.
            self.bitstring.append(
                f'uint:8={patch_information[0]}, uint:8={patch_information[1]}'
            )
            ##### Write coefficients.
            list(
                map(
                    lambda quantized: self.bitstring.append(
                        f'uint:{bits_for_quantized_values}={quantized}'),
                    patch_information[2:]))

        return

    def _read_coefficientes(self):
        ##### Read header
        patches_diff, self.N, self.M = self.bitstring.readlist(
            f'int:7, uint:5, uint:4')
        self.N += 1
        self.M += 1
        ##### Obtain image dimensions
        bits_for_quantized_values = int(np.ceil(math.log2(self.M)))
        bits_per_patch = 2 * 8 + np.ceil(math.log2(self.M)) * self.N**2
        patches_amount = len(
            self.bitstring.bin.__str__()[16:]) / bits_per_patch
        patches_shape = self._compute_dimensions(patches_diff, patches_amount)
        image_shape = patches_shape * self.N
        ##### Decode patches
        self.quantized_image = np.zeros(image_shape)
        for v in range(patches_shape[0]):
            for h in range(patches_shape[1]):
                ##### Get index mapping within a patch
                min_value, max_value = self.bitstring.readlist(
                    f'uint:8, uint:8')
                index_mapping = np.linspace(min_value, max_value, num=self.M)
                ##### Read index
                indexes = np.reshape(list(\
                    map(lambda pixel_idx: self.bitstring.read(f'uint:{bits_for_quantized_values}'), range(self.N**2))), \
                        (self.N, self.N))
                ##### Decode and store patch.
                reconstructed_patch = index_mapping[indexes]
                self.quantized_image[v * self.N:(v + 1) * self.N,
                                     h * self.N:(h + 1) *
                                     self.N] = reconstructed_patch
        ##### Cast patch to uint8.
        self.quantized_image = self.quantized_image.astype(np.uint8)
        return
コード例 #7
0
    messageBuffer = BitStream()

    i = 0
    pumpSession = MedtronicSession()

    for packet in cap:
        # Make sure the data is coming from one of the USB endpoints we care about.
        # Skip anything else
        usbEndpoint = int( packet.usb.endpoint_number, 16 )

        if( usbEndpoint != INCOMING_ENDPOINT and
            usbEndpoint != OUTGOING_ENDPOINT ):
            continue

        usbBuffer = BitStream('0x%s' % ( packet.data.usb_capdata.replace( ':', '' ) ) )
        usbHeader = usbBuffer.readlist( 'bytes:3, uint:8' )

        # Validate the header
        if( usbEndpoint == OUTGOING_ENDPOINT and usbHeader[0].encode( 'hex' ) != '000000' ):
            print 'Unexpected USB Header. Expected "0x000000", got "0x%s".' % ( usbHeader[0].encode( 'hex' ) )
            raise Exception
        if( usbEndpoint == INCOMING_ENDPOINT and usbHeader[0] != 'ABC' ):
            print 'Unexpected USB Header. Expected "0x414243", got "0x%s".' % ( usbHeader[0].encode( 'hex' ) )
            raise Exception

        messageBuffer.append( usbBuffer.read( usbHeader[1] * 8 ) )

        # Clear the messageBuffer if we have a full message
        # TODO - we need to be able to figure out if all 60 bytes are conusumed, but it's the end of the message
        if( usbHeader[1] < USB_PACKET_SIZE ):
            print >> sys.stderr, 'Message %s' % ( 'OUT' if usbEndpoint == OUTGOING_ENDPOINT else 'IN' )
コード例 #8
0
    i = 0
    pumpSession = MedtronicSession(args.encyption_key)

    for packet in cap:
        # Make sure the data is coming from one of the USB endpoints we care about.
        # Skip anything else
        usbEndpoint = int(packet.usb.endpoint_number, 16)

        if (usbEndpoint != INCOMING_ENDPOINT
                and usbEndpoint != OUTGOING_ENDPOINT):
            continue

        usbBuffer = BitStream('0x%s' %
                              (packet.data.usb_capdata.replace(':', '')))
        usbHeader = usbBuffer.readlist('bytes:3, uint:8')

        # Validate the header
        if (usbEndpoint == OUTGOING_ENDPOINT
                and usbHeader[0].encode('hex') != '000000'):
            print 'Unexpected USB Header. Expected "0x000000", got "0x%s".' % (
                usbHeader[0].encode('hex'))
            raise Exception
        if (usbEndpoint == INCOMING_ENDPOINT and usbHeader[0] != 'ABC'):
            print 'Unexpected USB Header. Expected "0x414243", got "0x%s".' % (
                usbHeader[0].encode('hex'))
            raise Exception

        messageBuffer.append(usbBuffer.read(usbHeader[1] * 8))

        # Clear the messageBuffer if we have a full message
コード例 #9
0
UDP_IP = "127.0.0.1"
UDP_PORT = 40868

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))

while True:
	try:
		data, addr = sock.recvfrom(1024)
		data = data.split(b'\x5e')[1]
		data = data.split(b'\xd5')[0]

		tlm = BitStream(data)
		print(tlm.bin)
		fps, err, Ax, Ay, Az, Gx, Gy, Gz, roll, pitch, yaw, alt, temp, volts, resv = tlm.readlist('bits:6, bits:6, int:6, int:6, int:9, int:10, int:10, int:12, int:9, int:9, int:9, uint:12, int:8, uint:8, bits:8')

		Ax /= 10
		Ay /= 10
		Az /= 10
		volts /= 10

		print("FPS = {}".format(fps))
		print("err = {}".format(err))
		print(" Ax = {}".format(Ax))
		print(" Ay = {}".format(Ay))
		print(" Az = {}".format(Az))
		print(" Gx = {}".format(Gx))
		print(" Gy = {}".format(Gy))
		print(" Gz = {}".format(Gz))
		print("rol = {}".format(roll))
コード例 #10
0
    def parse_sps_data(self):
        s = BitStream('0x' + self.data.encode('hex'))
        self.forbidden_bit = s.read('uint:1')
        self.nal_ref_idc = s.read('uint:2')
        self.nal_unit_type = s.read('uint:5')
        self.profile_idc = s.read('uint:8')
        self.constraint_flags = s.readlist('4*uint:1')
        self.reserved_bits = s.read('uint:4')
        self.level_idc = s.read('uint:8')
        self.seq_parameter_set_id = s.read('ue')
        if self.profile_idc in self.chroma_profiles:
            self.chroma_format_idc = s.read('ue')
            if self.chroma_format_idc == 3:
                self.separate_color_plane_flag = s.read('uint:1')
            self.bit_depth_luma_minus8 = s.read('ue')
            self.bit_depth_chroma_minus8 = s.read('ue')
            self.qpprime_y_zero_transform_bypass_flag = s.read('uint:1')
            self.seq_scaling_matrix_present_flag = s.read('uint:1')
            if self.seq_scaling_matrix_present_flag:
                seq_scaling_list_present = s.read('uint:1')
                scaling_list_count = 12 if self.chroma_format_idc == 3 else 8
                for i in xrange(scaling_list_count):
                    if seq_scaling_list_present:
                        last_scale = 8
                        next_scale = 8
                        list_size = 16 if i < 6 else 64
                        for j in xrange(list_size):
                            delta_scale = s.read('ue')

        self.log2_max_frame_num_minus4 = s.read('ue')
        self.log2_max_frame_num = self.log2_max_frame_num_minus4 + 4
        frame_num_width = self.log2_max_frame_num
        self.pic_order_cnt_type = s.read('ue')
        if self.pic_order_cnt_type == 0:
            self.log2_max_pic_order_cnt_lsb_minus4 = s.read('ue')
        elif self.pic_order_cnt_type == 1:
            self.delta_pic_order_always_zero_flag = s.read('uint:1')
            self.offset_for_non_ref_pic = s.read('se')
            self.offset_for_top_to_bottom_field = s.read('se')
            self.num_ref_frames_in_pic_order_cnt_cycle = s.read('ue')
            self.offsets_for_ref_frame = s.readlist('4*')

        self.max_num_ref_frames = s.read('ue')
        self.gaps_in_frame_num_value_allowed_flag = s.read('uint:1')
        self.pic_width_in_mbs_minus1 = s.read('ue')
        self.pic_height_in_map_units_minus1 = s.read('ue')
        self.frame_mbs_only_flag = s.read('uint:1')
        if self.frame_mbs_only_flag == 0:
            self.mb_adaptive_frame_field_flag = s.read('uint:1')
        self.direct_8x8_inference_flag = s.read('uint:1')
        self.frame_cropping_flag = s.read('uint:1')
        self.frame_crop_offsets = s.readlist('4*ue')
        self.vui_parameters_present_flag = s.read('uint:1')
        self.width = 16 * (self.pic_width_in_mbs_minus1 + 1)
        self.height = 16 * (2 - self.frame_mbs_only_flag) * (
            self.pic_height_in_map_units_minus1 + 1)

        if self.separate_color_plane_flag or self.chroma_format_idc == 0:
            self.frame_crop_offsets[CROP_BOTTOM] = self.frame_crop_offsets[
                CROP_BOTTOM] * (2 - self.frame_mbs_only_flag)
            self.frame_crop_offsets[CROP_TOP] = self.frame_crop_offsets[
                CROP_TOP] * (2 - self.frame_mbs_only_flag)
        elif not self.separate_color_plane_flag and self.chroma_format_idc > 0:
            if self.chroma_format_idc == 1 or self.chroma_format_idc == 2:
                self.frame_crop_offsets[
                    CROP_LEFT] = self.frame_crop_offsets[CROP_LEFT] * 2
                self.frame_crop_offsets[
                    CROP_RIGHT] = self.frame_crop_offsets[CROP_RIGHT] * 2

            if self.chroma_format_idc == 1:
                self.frame_crop_offsets[
                    CROP_TOP] = self.frame_crop_offsets[CROP_TOP] * 2
                self.frame_crop_offsets[
                    CROP_BOTTOM] = self.frame_crop_offsets[CROP_BOTTOM] * 2

        self.width = self.width - (self.frame_crop_offsets[CROP_LEFT] +
                                   self.frame_crop_offsets[CROP_RIGHT])
        self.height = self.height - (self.frame_crop_offsets[CROP_TOP] +
                                     self.frame_crop_offsets[CROP_BOTTOM])