TODO PES packet extension fields, .e.g. ESCR
550563
function writePESPackets() {
  var continuityCounters = {};
  var pesWriter = x => {
    if (x.type === 'PESPacket') {
      // var bytePos = 0;
      var counter = continuityCounters[x.pid];
      if (typeof counter === 'undefined') counter = 0;
      counter = counter % 16;
      var packets = [];
      var data = (x.payloads.length === 1) ? x.payloads[0] :
        Buffer.concat(x.payloads);
      var packetLength = data.length;
      if (packetLength > 0xffff) {
        packetLength = 0;
        if (!x.streamID.startsWith('video_stream')) {
          console.log(`Warning: PES packet for pid ${x.pid} is not video and has length greater than 0xffff.`);
        }
      }
      var hpp = Buffer.allocUnsafe(100);
      hpp.writeUIntBE(1, 0, 3);
      hpp.writeUInt8(util.streamNameID[x.streamID], 3);
      hpp.writeUInt16BE(packetLength, 4);
      hpp.writeUInt16BE(
        0x8000 |
        (x.scramblingControl & 0x03) << 12 |
        ((x.priority === true) ? 0x0800 : 0x0000) |
        ((x.dataAlignmentIndicator === true) ? 0x0400 : 0x0000) |
        ((x.copyright === true) ? 0x0200 : 0x0000) |
        ((x.originalOrCopy === true) ? 0x0100 : 0x0000) |
        (x.ptsDtsIndicator & 0x03) << 6 |
        ((x.escrFlag === true) ? 0x0020 : 0x0000) |
        ((x.esRateFlag === true) ? 0x0010 : 0x0000) |
        ((x.dsmTrickModeFlag === true) ? 0x0008 : 0x0000) |
        ((x.additionalCopyInfoFlag === true) ? 0x0004 : 0x0000) |
        ((x.crcFlag === true) ? 0x0002 : 0x0000) |
        ((x.extensionFlag === true) ? 0x0001 : 0x0000), 6);
      var pesHeaderLength =
        (x.ptsDtsIndicator === 2 ? 5 : 0) +
        (x.ptsDtsIndicator === 3 ? 10 : 0);
      hpp.writeUInt8(pesHeaderLength, 8);
      hpp = hpp.slice(0, 9 + pesHeaderLength);
      switch (x.ptsDtsIndicator) {
      case 2:
        writeTimeStamp(x.pts, 0x02, hpp, 9);
        break;
      case 3:
        writeTimeStamp(x.pts, 0x03, hpp, 9);
        writeTimeStamp(x.dts, 0x01, hpp, 14);
        break;
      default:
        break;
      }
      var dataPos = 0;
      var payloadStart = true;
      var available = 184 - hpp.length;
      while (dataPos < (data.length - available)) {
        var nextPacket = {
          type : 'TSPacket',
          packetSync : 0x47,
          transportErrorIndicator : false,
          payloadUnitStartIndicator : payloadStart,
          transportPriority : false,
          pid : x.pid,
          scramblingControl : 0,
          adaptationFieldControl : 1,
          continuityCounter : counter++,
          payload : payloadStart ? Buffer.concat(
            [hpp, data.slice(0, available)]) :
            data.slice(dataPos, dataPos + 184)
        };
        counter = counter % 16;
        dataPos += available;
        packets.push(nextPacket);
        payloadStart = false;
        available = 184;
      }
      if (data.length - dataPos > 0) {
        var adaptationLength = 183 - (payloadStart ?
          (data.length - dataPos) + hpp.length :
          (data.length - dataPos));
        var finalPacket = {
          type : 'TSPacket',
          packetSync : 0x47,
          transportErrorIndicator : false,
          payloadUnitStartIndicator : payloadStart,
          transportPriority : false,
          pid : x.pid,
          scramblingControl : 0,
          adaptationFieldControl : 3,
          continuityCounter : counter++,
          adaptationField : { // padding
            type : 'AdaptationField',
            adaptationFieldLength : adaptationLength,
            discontinuityIndicator : false,
            randomAccessIndicator : false,
            elementaryStreamPriorityIndicator : false,
            pcrFlag : false,
            opcrFlag : false,
            splicingPointFlag : false,
            transportPrivateDataFlag : false,
            adaptationFieldExtensionFlag : false
          },
          payload : payloadStart ?
            Buffer.concat([hpp, data]) : data.slice(dataPos)
        };
        if (finalPacket.adaptationField.adaptationFieldLength === -1) {
          delete finalPacket.adaptationField;
          finalPacket.adaptationFieldControl = 1;
        } else if (finalPacket.adaptationField.adaptationFieldLength === 0) {
          finalPacket.adaptationField = {
            type : 'AdaptationField',
            adaptationFieldLength : 0
          };
        }
        packets.push(finalPacket);
        return H(packets);
      }
      continuityCounters[x.pid] = counter % 16;
    } else { // Is PESPacket
      return H([x]);
    }
  };

  return H.pipeline(H.flatMap(pesWriter));
}
817581
def _make_regex(pem_type):
    '''
    
    '''
    return re.compile(
        r"\s*(?P<pem_header>-----BEGIN {0}-----)\s+"
        r"(?:(?P<proc_type>Proc-Type: 4,ENCRYPTED)\s*)?"
        r"(?:(?P<dek_info>DEK-Info: (?:DES-[3A-Z\-]+,[0-9A-F]{{16}}|[0-9A-Z\-]+,[0-9A-F]{{32}}))\s*)?"
        r"(?P<pem_body>.+?)\s+(?P<pem_footer>"
        r"-----END {0}-----)\s*".format(pem_type),
        re.DOTALL
    )
660897
public void processPacketAESCM(RawPacket pkt, int index) {
        long ssrc = pkt.getRTCPSSRC();

        /* Compute the CM IV (refer to chapter 4.1.1 in RFC 3711):
        *
        * k_s   XX XX XX XX XX XX XX XX XX XX XX XX XX XX
        * SSRC              XX XX XX XX
        * index                               XX XX XX XX
        * ------------------------------------------------------XOR
        * IV    XX XX XX XX XX XX XX XX XX XX XX XX XX XX 00 00
        *        0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
        */
        ivStore[0] = saltKey[0];
        ivStore[1] = saltKey[1];
        ivStore[2] = saltKey[2];
        ivStore[3] = saltKey[3];

        // The shifts transform the ssrc and index into network order
        ivStore[4] = (byte) (((ssrc >> 24) & 0xff) ^ this.saltKey[4]);
        ivStore[5] = (byte) (((ssrc >> 16) & 0xff) ^ this.saltKey[5]);
        ivStore[6] = (byte) (((ssrc >> 8) & 0xff) ^ this.saltKey[6]);
        ivStore[7] = (byte) ((ssrc & 0xff) ^ this.saltKey[7]);

        ivStore[8] = saltKey[8];
        ivStore[9] = saltKey[9];

        ivStore[10] = (byte) (((index >> 24) & 0xff) ^ this.saltKey[10]);
        ivStore[11] = (byte) (((index >> 16) & 0xff) ^ this.saltKey[11]);
        ivStore[12] = (byte) (((index >> 8) & 0xff) ^ this.saltKey[12]);
        ivStore[13] = (byte) ((index & 0xff) ^ this.saltKey[13]);

        ivStore[14] = ivStore[15] = 0;

        // Encrypted part excludes fixed header (8 bytes)  
        final int payloadOffset = 8;
        final int payloadLength = pkt.getLength() - payloadOffset;
        cipherCtr.process(cipher, pkt.getBuffer(), payloadOffset, payloadLength, ivStore);
    }
222761
def get_pks(self, field_vals):
        """"""
        field_name = self.schema.pk.name
        return self.in_field(field_name, field_vals).get()
717136
def _parse_pubkey(stream, packet_type='pubkey'):
    """"""
    p = {'type': packet_type}
    packet = io.BytesIO()
    with stream.capture(packet):
        p['version'] = stream.readfmt('B')
        p['created'] = stream.readfmt('>L')
        p['algo'] = stream.readfmt('B')
        if p['algo'] in ECDSA_ALGO_IDS:
            log.debug('parsing elliptic curve key')
            # https://tools.ietf.org/html/rfc6637#section-11
            oid_size = stream.readfmt('B')
            oid = stream.read(oid_size)
            assert oid in SUPPORTED_CURVES, util.hexlify(oid)
            p['curve_oid'] = oid

            mpi = parse_mpi(stream)
            log.debug('mpi: %x (%d bits)', mpi, mpi.bit_length())
            leftover = stream.read()
            if leftover:
                leftover = io.BytesIO(leftover)
                # https://tools.ietf.org/html/rfc6637#section-8
                # should be b'\x03\x01\x08\x07': SHA256 + AES128
                size, = util.readfmt(leftover, 'B')
                p['kdf'] = leftover.read(size)
                p['secret'] = leftover.read()

            parse_func, keygrip_func = SUPPORTED_CURVES[oid]
            keygrip = keygrip_func(parse_func(mpi))
            log.debug('keygrip: %s', util.hexlify(keygrip))
            p['keygrip'] = keygrip

        elif p['algo'] == DSA_ALGO_ID:
            parse_mpis(stream, n=4)  # DSA keys are not supported
        elif p['algo'] == ELGAMAL_ALGO_ID:
            parse_mpis(stream, n=3)  # ElGamal keys are not supported
        else:  # assume RSA
            parse_mpis(stream, n=2)  # RSA keys are not supported
        assert not stream.read()

    # https://tools.ietf.org/html/rfc4880#section-12.2
    packet_data = packet.getvalue()
    data_to_hash = (b'\x99' + struct.pack('>H', len(packet_data)) +
                    packet_data)
    p['key_id'] = hashlib.sha1(data_to_hash).digest()[-8:]
    p['_to_hash'] = data_to_hash
    log.debug('key ID: %s', util.hexlify(p['key_id']))
    return p
1141685
private static FieldRef getExtensionField(FieldDescriptor descriptor) {
    Preconditions.checkArgument(descriptor.isExtension(), "%s is not an extension", descriptor);
    String extensionFieldName = getFieldName(descriptor, false);
    if (descriptor.getExtensionScope() != null) {
      TypeInfo owner = messageRuntimeType(descriptor.getExtensionScope());
      return FieldRef.createPublicStaticField(owner, extensionFieldName, EXTENSION_TYPE);
    }
    // else we have a 'top level extension'
    String containingClass =
        JavaQualifiedNames.getPackage(descriptor.getFile())
            + "."
            + JavaQualifiedNames.getOuterClassname(descriptor.getFile());
    return FieldRef.createPublicStaticField(
        TypeInfo.create(containingClass), extensionFieldName, EXTENSION_TYPE);
  }
977661
def rpad(self, ecc, k=None):
        ''''''
        if not k: k = self.k
        pad = None
        if len(ecc) < self.n-k:
            print("Warning: the ecc field may have been truncated (entrymarker or field_delim misdetection?).")
            #pad = "\x00" * (self.n-k-len(ecc))
            pad = bytearray(self.n-k-len(ecc))
            ecc = ecc + pad
        return [ecc, pad]
280094
def _checksum(self, packet):
        ''''''
        xorsum = 0
        for s in packet:
            xorsum ^= ord(s)
        return xorsum
897468
def _der_to_pem(der_key, marker):
    
    pem_key_chunks = [('-----BEGIN %s-----' % marker).encode('utf-8')]

    # Limit base64 output lines to 64 characters by limiting input lines to 48 characters.
    for chunk_start in range(0, len(der_key), 48):
        pem_key_chunks.append(b64encode(der_key[chunk_start:chunk_start + 48]))

    pem_key_chunks.append(('-----END %s-----' % marker).encode('utf-8'))

    return b'\n'.join(pem_key_chunks)
514282
def filenames(self):
        """"""
        return [p.name for p in self.packets if isinstance(p, FileDescriptionPacket)]
342253
def from_bytes(cls, bitstream):
        '''
        
        '''
        packet = cls()

        # Convert to ConstBitStream (if not already provided)
        if not isinstance(bitstream, ConstBitStream):
            if isinstance(bitstream, Bits):
                bitstream = ConstBitStream(auto=bitstream)
            else:
                bitstream = ConstBitStream(bytes=bitstream)

        # Read the source and destination ports
        (packet.source_port,
         packet.destination_port) = bitstream.readlist('2*uint:16')

        # Store the length
        length = bitstream.read('uint:16')
        if length < 8:
            raise ValueError('Invalid UDP length')

        # Read the checksum
        packet.checksum = bitstream.read('uint:16')

        # And the rest is payload
        payload_bytes = length - 8
        packet.payload = bitstream.read('bytes:%d' % payload_bytes)

        # LISP-specific handling
        if packet.source_port == 4341 or packet.destination_port == 4341:
            # Payload is a LISP data packet
            from pylisp.packet.lisp.data import DataPacket
            packet.payload = DataPacket.from_bytes(packet.payload)
        elif packet.source_port == 4342 or packet.destination_port == 4342:
            # Payload is a LISP control message
            from pylisp.packet.lisp.control.base import ControlMessage
            packet.payload = ControlMessage.from_bytes(packet.payload)

        # There should be no remaining bits
        if bitstream.pos != bitstream.len:
            raise ValueError('Bits remaining after processing packet')

        # Verify that the properties make sense
        packet.sanitize()

        return packet
497666
def packetToDict(pkt):
	

	d = {
		'id': pkt[4],
		# 'instruction': xl320.InstrToStr[pkt[7]],
		# 'length': (pkt[6] << 8) + pkt[5],
		# 'params': pkt[8:-2],
		'Model Number': (pkt[10] << 8) + pkt[9],
		'Firmware Ver': pkt[11],
		'Error': ErrorStatusMsg[pkt[8]],
		# 'crc': pkt[-2:]
	}

	return d
255382
def decode_network_values(ptype, plen, buf):
    
    nvalues = short.unpack_from(buf, header.size)[0]
    off = header.size + short.size + nvalues
    valskip = double.size

    # Check whether our expected packet size is the reported one
    assert ((valskip + 1) * nvalues + short.size + header.size) == plen
    assert double.size == number.size

    result = []
    for dstype in [ord(x) for x in buf[header.size + short.size:off]]:
        if dstype == DS_TYPE_COUNTER:
            result.append((dstype, number.unpack_from(buf, off)[0]))
            off += valskip
        elif dstype == DS_TYPE_GAUGE:
            result.append((dstype, double.unpack_from(buf, off)[0]))
            off += valskip
        elif dstype == DS_TYPE_DERIVE:
            result.append((dstype, number.unpack_from(buf, off)[0]))
            off += valskip
        elif dstype == DS_TYPE_ABSOLUTE:
            result.append((dstype, number.unpack_from(buf, off)[0]))
            off += valskip
        else:
            raise ValueError("DS type %i unsupported" % dstype)

    return result
242993
private static void exploreExtensions(ByteBuffer input, List<String> alpnProtocols, ByteArrayOutputStream out)
            throws SSLException {
        ByteArrayOutputStream extensionOut = out == null ? null : new ByteArrayOutputStream();
        int length = getInt16(input);           // length of extensions
        writeInt16(extensionOut, 0); //placeholder
        while (length > 0) {
            int extType = getInt16(input);      // extenson type
            writeInt16(extensionOut, extType);
            int extLen = getInt16(input);       // length of extension data
            writeInt16(extensionOut, extLen);
            if (extType == 16) {      // 0x00: ty
                if(out == null) {
                    exploreALPNExt(input, alpnProtocols);
                } else {
                    throw new ALPNPresentException();
                }
            } else {                    // ignore other extensions
                processByteVector(input, extLen, extensionOut);
            }

            length -= extLen + 4;
        }
        if(out != null) {
            byte[] alpnBits = generateAlpnExtension(alpnProtocols);
            extensionOut.write(alpnBits,0 ,alpnBits.length);
            byte[] extensionsData = extensionOut.toByteArray();
            int newLength = extensionsData.length - 2;
            extensionsData[0] = (byte) ((newLength >> 8) & 0xFF);
            extensionsData[1] = (byte) (newLength & 0xFF);
            out.write(extensionsData, 0, extensionsData.length);
        }

    }
521342
def der2pem(der_string, obj="UNKNOWN"):
    """"""
    # Encode a byte string in PEM format. Header advertizes <obj> type.
    pem_string = ("-----BEGIN %s-----\n" % obj).encode()
    base64_string = base64.b64encode(der_string)
    chunks = [base64_string[i:i + 64] for i in range(0, len(base64_string), 64)]  # noqa: E501
    pem_string += b'\n'.join(chunks)
    pem_string += ("\n-----END %s-----\n" % obj).encode()
    return pem_string
537144
def read_packets(self):
        """"""
        while self.running:
            packet_length = self.client.recv(2)
            if len(packet_length) < 2:
                self.stop()
                continue
            packet_length = struct.unpack("<h", packet_length)[0] - 2

            data = self.client.recv(packet_length)
            packno = data[0]

            try:
                parser = "Packet" + format(packno, 'x').upper() + "Parser"
                packet_class = getattr(packets, parser)
                packet_class().parse(self.world, self.player, data, self._evman)
            except AttributeError as e:
                pass

            if packno == 2:
                self.stop()
                continue
839402
def packet(tag, blob):
    """"""
    assert len(blob) < 2**32

    if len(blob) < 2**8:
        length_type = 0
    elif len(blob) < 2**16:
        length_type = 1
    else:
        length_type = 2

    fmt = ['>B', '>H', '>L'][length_type]
    leading_byte = 0x80 | (tag << 2) | (length_type)
    return struct.pack('>B', leading_byte) + util.prefix_len(fmt, blob)