Decode a wire format UUID

    It handles the rather particular scheme where half is little endian
    and half is big endian.  It returns a string like dmidecode would output.
487015
def decode_wireformat_uuid(rawguid):
    
    if isinstance(rawguid, list):
        rawguid = bytearray(rawguid)
    lebytes = struct.unpack_from('<IHH', buffer(rawguid[:8]))
    bebytes = struct.unpack_from('>HHI', buffer(rawguid[8:]))
    return '{0:08X}-{1:04X}-{2:04X}-{3:04X}-{4:04X}{5:08X}'.format(
        lebytes[0], lebytes[1], lebytes[2], bebytes[0], bebytes[1], bebytes[2])
528996
private static void decodeEdifactSegment(BitSource bits, StringBuilder result) {
    do {
      // If there is only two or less bytes left then it will be encoded as ASCII
      if (bits.available() <= 16) {
        return;
      }

      for (int i = 0; i < 4; i++) {
        int edifactValue = bits.readBits(6);

        // Check for the unlatch character
        if (edifactValue == 0x1F) {  // 011111
          // Read rest of byte, which should be 0, and stop
          int bitsLeft = 8 - bits.getBitOffset();
          if (bitsLeft != 8) {
            bits.readBits(bitsLeft);
          }
          return;
        }

        if ((edifactValue & 0x20) == 0) {  // no 1 in the leading (6th) bit
          edifactValue |= 0x40;  // Add a leading 01 to the 6 bit binary value
        }
        result.append((char) edifactValue);
      }
    } while (bits.available() > 0);
  }
147941
public final byte[] decode(byte[] source, int off, int len) {
		int	len34 = len * 3 / 4;
		byte[] outBuff = new byte[len34]; // upper limit on size of output
		int	outBuffPosn = 0;
		
		byte[] b4 = new byte[4];
		int	b4Posn = 0;
		int	i = 0;
		byte sbiCrop = 0;
		byte sbiDecode = 0;
		for (i = off; i < off + len; i++) {
			sbiCrop = (byte)(source[i] & 0x7f); // only the low seven bits
			sbiDecode = DECODABET[sbiCrop];
			
			if (sbiDecode >= WHITE_SPACE_ENC) { // white space, equals sign or better
				if (sbiDecode >= PADDING_CHAR_ENC) {
					b4[b4Posn++] = sbiCrop;
					if (b4Posn > 3) {
						outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn);
						b4Posn = 0;
						
						// if that was the padding char, break out of 'for' loop
						if (sbiCrop == PADDING_CHAR) {
							break;
						}
					} // end if: quartet built
				} // end if: equals sign or better
			} // end if: white space, equals sign or better
			else {
				//discard
			} 
		} // each input character
								   
		byte[] out = new byte[outBuffPosn];
		System.arraycopy(outBuff, 0, out, 0, outBuffPosn); 
		return out;
	}
1291031
def decode_value(stream):
    
    length = decode_length(stream)
    (value,) = unpack_value(">{:d}s".format(length), stream)
    return value
1371988
def decode(string)
      padding_size  = string.bytesize % 4
      padded_string = padding_size > 0 ? string + '=' * (4 - padding_size) : string

      Base64.urlsafe_decode64(padded_string)
    end
415205
function tinf_decode_symbol(d, t) {
  while (d.bitcount < 24) {
    d.tag |= d.source[d.sourceIndex++] << d.bitcount;
    d.bitcount += 8;
  }
  
  var sum = 0, cur = 0, len = 0;
  var tag = d.tag;

  /* get more bits while code value is above sum */
  do {
    cur = 2 * cur + (tag & 1);
    tag >>>= 1;
    ++len;

    sum += t.table[len];
    cur -= t.table[len];
  } while (cur >= 0);
  
  d.tag = tag;
  d.bitcount -= len;

  return t.trans[sum + cur];
}
284483
def readEncodedU32(self):
        
        self.reset_bits_pending();
        result = self.readUI8();
        if result & 0x80 != 0:
            result = (result & 0x7f) | (self.readUI8() << 7)
            if result & 0x4000 != 0:
                result = (result & 0x3fff) | (self.readUI8() << 14)
                if result & 0x200000 != 0:
                    result = (result & 0x1fffff) | (self.readUI8() << 21)
                    if result & 0x10000000 != 0:
                        result = (result & 0xfffffff) | (self.readUI8() << 28)
        return result
148131
static String readUnicodeString(final DataInput pInput) throws IOException {
        int length = pInput.readInt();

        if (length == 0) {
            return "";
        }

        byte[] bytes = new byte[length * 2];
        pInput.readFully(bytes);

        return StringUtil.decode(bytes, 0, bytes.length, "UTF-16");
    }
148025
private function uuid(): string
    {
        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff), mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
        );
    }
1339958
def decint(n, signed=False):  # pylint: disable=invalid-name,too-many-branches
    

    if isinstance(n, str):
        n = utils.to_string(n)

    if n is True:
        return 1

    if n is False:
        return 0

    if n is None:
        return 0

    if is_numeric(n):
        if signed:
            if not -TT255 <= n <= TT255 - 1:
                raise EncodingError('Number out of range: %r' % n)
        else:
            if not 0 <= n <= TT256 - 1:
                raise EncodingError('Number out of range: %r' % n)

        return n

    if is_string(n):
        if len(n) > 32:
            raise EncodingError('String too long: %r' % n)

        if len(n) == 40:
            int_bigendian = decode_hex(n)
        else:
            int_bigendian = n  # pylint: disable=redefined-variable-type

        result = big_endian_to_int(int_bigendian)
        if signed:
            if result >= TT255:
                result -= TT256

            if not -TT255 <= result <= TT255 - 1:
                raise EncodingError('Number out of range: %r' % n)
        else:
            if not 0 <= result <= TT256 - 1:
                raise EncodingError('Number out of range: %r' % n)

        return result

    raise EncodingError('Cannot decode integer: %r' % n)
1303199
def uuidify(val):
    
    if uuidutils.is_uuid_like(val):
        return val
    else:
        try:
            int_val = int(val, 16)
        except ValueError:
            with excutils.save_and_reraise_exception():
                LOG.error("Invalid UUID format %s. Please provide an "
                          "integer in decimal (0-9) or hex (0-9a-e) "
                          "format", val)
        res = str(int_val)
        num = 12 - len(res)
        return "00000000-0000-0000-0000-" + "0" * num + res
235060
def _DoubleDecoder():
  

  local_unpack = struct.unpack

  def InnerDecode(buffer, pos):
    # We expect a 64-bit value in little-endian byte order.  Bit 1 is the sign
    # bit, bits 2-12 represent the exponent, and bits 13-64 are the significand.
    new_pos = pos + 8
    double_bytes = buffer[pos:new_pos]

    # If this value has all its exponent bits set and at least one significand
    # bit set, it's not a number.  In Python 2.4, struct.unpack will treat it
    # as inf or -inf.  To avoid that, we treat it specially.
    if ((double_bytes[7:8] in b'\x7F\xFF')
        and (double_bytes[6:7] >= b'\xF0')
        and (double_bytes[0:7] != b'\x00\x00\x00\x00\x00\x00\xF0')):
      return (_NAN, new_pos)

    # Note that we expect someone up-stack to catch struct.error and convert
    # it to _DecodeError -- this way we don't have to set up exception-
    # handling blocks every time we parse one value.
    result = local_unpack('<d', double_bytes)[0]
    return (result, new_pos)
  return _SimpleDecoder(wire_format.WIRETYPE_FIXED64, InnerDecode)
942880
def _encode_uuid(name, value, dummy, opts):
    """"""
    uuid_representation = opts.uuid_representation
    # Python Legacy Common Case
    if uuid_representation == OLD_UUID_SUBTYPE:
        return b"\x05" + name + b'\x10\x00\x00\x00\x03' + value.bytes
    # Java Legacy
    elif uuid_representation == JAVA_LEGACY:
        from_uuid = value.bytes
        data = from_uuid[0:8][::-1] + from_uuid[8:16][::-1]
        return b"\x05" + name + b'\x10\x00\x00\x00\x03' + data
    # C# legacy
    elif uuid_representation == CSHARP_LEGACY:
        # Microsoft GUID representation.
        return b"\x05" + name + b'\x10\x00\x00\x00\x03' + value.bytes_le
    # New
    else:
        return b"\x05" + name + b'\x10\x00\x00\x00\x04' + value.bytes
1135
def _encode_ndef_uri_type(self, data):
        
        t = 0x0
        for (code, prefix) in uri_identifiers:
            if data[:len(prefix)].decode('latin-1').lower() == prefix:
                t = code
                data = data[len(prefix):]
                break
        data = yubico_util.chr_byte(t) + data
        return data
1265375
def decode(s):
    
    if isinstance(s, bytes):
        return s.decode(sys.getdefaultencoding())
    else:
        return s
811781
def encode(self, uuid, pad_length=22):
        
        return self._num_to_string(uuid.int, pad_to_length=pad_length)
627670
def decode_devid(devid, pname):
    ''''''
    devid = int(devid)
    if devid == 0:
        return

    bus_type=devid & 0x07
    bus=(devid>>3) & 0x1F
    address=(devid>>8)&0xFF
    devtype=(devid>>16)

    bustypes = {
        1: "I2C",
        2: "SPI",
        3: "UAVCAN",
        4: "SITL"
        }

    compass_types = {
        0x01 : "DEVTYPE_HMC5883_OLD",
        0x07 : "DEVTYPE_HMC5883",
        0x02 : "DEVTYPE_LSM303D",
        0x04 : "DEVTYPE_AK8963 ",
        0x05 : "DEVTYPE_BMM150 ",
        0x06 : "DEVTYPE_LSM9DS1",
        0x08 : "DEVTYPE_LIS3MDL",
        0x09 : "DEVTYPE_AK09916",
        0x0A : "DEVTYPE_IST8310",
        0x0B : "DEVTYPE_ICM20948",
        0x0C : "DEVTYPE_MMC3416",
        0x0D : "DEVTYPE_QMC5883L",
        0x0E : "DEVTYPE_MAG3110",
        0x0F : "DEVTYPE_SITL",
        0x10 : "DEVTYPE_IST8308",
        0x11 : "DEVTYPE_RM3100",
        }

    imu_types = {
        0x09 : "DEVTYPE_BMI160",
        0x10 : "DEVTYPE_L3G4200D",
        0x11 : "DEVTYPE_ACC_LSM303D",
        0x12 : "DEVTYPE_ACC_BMA180",
        0x13 : "DEVTYPE_ACC_MPU6000",
        0x16 : "DEVTYPE_ACC_MPU9250",
        0x17 : "DEVTYPE_ACC_IIS328DQ",
        0x21 : "DEVTYPE_GYR_MPU6000",
        0x22 : "DEVTYPE_GYR_L3GD20",
        0x24 : "DEVTYPE_GYR_MPU9250",
        0x25 : "DEVTYPE_GYR_I3G4250D",
        0x26 : "DEVTYPE_GYR_LSM9DS1",
        0x27 : "DEVTYPE_INS_ICM20789",
        0x28 : "DEVTYPE_INS_ICM20689",
        0x29 : "DEVTYPE_INS_BMI055",
        0x2A : "DEVTYPE_SITL",
        0x2B : "DEVTYPE_INS_BMI088",
        0x2C : "DEVTYPE_INS_ICM20948",
        0x2D : "DEVTYPE_INS_ICM20648",
        0x2E : "DEVTYPE_INS_ICM20649",
        0x2F : "DEVTYPE_INS_ICM20602",
        }

    decoded_devname = ""

    if pname.startswith("COMPASS"):
        decoded_devname = compass_types.get(devtype, "UNKNOWN")

    if pname.startswith("INS"):
        decoded_devname = imu_types.get(devtype, "UNKNOWN")

    print("%s: bus_type:%s(%u)  bus:%u address:%u(0x%x) devtype:%u(0x%x) %s" % (
        pname,
        bustypes.get(bus_type,"UNKNOWN"), bus_type,
        bus, address, address, devtype, devtype, decoded_devname))