<commit_before>/*
 * Copyright (c) 2013 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME>
 *    <NAME>
 *    <NAME>
 */

package mqtt

import (
	"bufio"
	"encoding/binary"
)

//decodeQos returns the QoS of the message
func decodeQos(header byte) QoS {
	qos := (header & 0x06) >> 1
	return QoS(qos)
}

//decodeMsgType returns the type of the message
func decodeMsgType(header byte) MsgType {
	mtype := (header & 0xF0) >> 4
	return MsgType(mtype)
}

// return (number of bytes needed, the remaining length)
func decodeRemlen(bytes []byte) (int, uint32) {
	// bytes[1,2,3,4] are the only ones we could care about
	idx := uint32(0)
	mult := uint32(1)
	value := uint32(0)
	for {
		idx++
		digit := uint32((bytes[idx]))
		value += (digit & 127) * mult
		mult *= 128
		if digit&128 == 0 {
			break
		}
	}
	return int(idx), value
}

func decodeRemlenFromNetwork(src *bufio.ReadWriter) ([]byte, uint32) {
	var bytes []byte
	var rLength uint32
	var count int
	var multiplier uint32 = 1
	var digit byte
	count = 1
	for {
		digit, _ = src.ReadByte()
		bytes = append(bytes, digit)
		rLength += uint32(digit&127) * multiplier
		if (digit & 128) == 0 {
			break
		}
		multiplier *= 128
		count++
	}
	return bytes, rLength
}

// return length of topic string, the topic string
func decodeTopic(bytes []byte) (tlen uint16, t string) {
	tlen = binary.BigEndian.Uint16(bytes[:2])
	t = string(bytes[2 : 2+tlen])
	return tlen, t
}

//decode takes a slice of bytes as received over the network<commit_msg>export decode function<commit_after>/*
 * Copyright (c) 2013 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME>
 *    <NAME>
 *    <NAME>
 */

package mqtt

import (
	"bufio"
	"encoding/binary"
)

//decodeQos returns the QoS of the message
func decodeQos(header byte) QoS {
	qos := (header & 0x06) >> 1
	return QoS(qos)
}

//decodeMsgType returns the type of the message
func DecodeMsgType(header byte) MsgType {
	return decodeMsgType(header)
}

//decodeMsgType returns the type of the message
func decodeMsgType(header byte) MsgType {
	mtype := (header & 0xF0) >> 4
	return MsgType(mtype)
}

// return (number of bytes needed, the remaining length)
func decodeRemlen(bytes []byte) (int, uint32) {
	// bytes[1,2,3,4] are the only ones we could care about
	idx := uint32(0)
	mult := uint32(1)
	value := uint32(0)
	for {
		idx++
		digit := uint32((bytes[idx]))
		value += (digit & 127) * mult
		mult *= 128
		if digit&128 == 0 {
			break
		}
	}
	return int(idx), value
}

func decodeRemlenFromNetwork(src *bufio.ReadWriter) ([]byte, uint32) {
	var bytes []byte
	var rLength uint32
	var count int
	var multiplier uint32 = 1
	var digit byte
	count = 1
	for {
		digit, _ = src.ReadByte()
		bytes = append(bytes, digit)
		rLength += uint32(digit&127) * multiplier
		if (digit & 128) == 0 {
			break
		}
		multiplier *= 128
		count++
	}
	return bytes, rLength
}

// return length of topic string, the topic string
func decodeTopic(bytes []byte) (tlen uint16, t string) {
	tlen = binary.BigEndian.Uint16(bytes[:2])
	t = string(bytes[2 : 2+tlen])
	return tlen, t
}

func Decode(bytes []byte, protocolVersion byte) *Message{
	return decode(bytes, protocolVersion)
}

//decode takes a slice of bytes as received over the network
{'fraction_non_alphanumeric': 0.12024972855591748, 'fraction_numerical': 0.03745928338762215, 'mean_word_length': 3.6527777777777777, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4116510', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1272, 'n_words': 425}