-
Notifications
You must be signed in to change notification settings - Fork 0
Octets
tpkarras edited this page May 13, 2025
·
7 revisions
Octets are numbers containing a binary sequence that indicates either the number or a continuation of said sequence.
Each octet is an 8 bit string, bits 1-8 can be either 1 or 0, bit 8 indicates whether it's a continuation (1) or a terminus (0).
Octets must be 0-128 or 255, examples are provided below.
- The number 0.
00000000 - The number 127.
11111110 - The number 128.
10000000 - The number 255.
11111111
You will have to create a function to parse numbers into a series of octets. As mentioned before, octets must have a length number containing the number of octets preceding the content of octets.
An example function is provided below.
$length = 0;
while(true){
if(!isset($number)){
$number = $element;
if($number > 128){
if(is_string($length)){
$length .= hex2bin(dechex(255));
} else {
$current_octet .= hex2bin(dechex(255));
}
$number = $number - 128;
} else {
$number = dechex($number);
if(strlen($number) & 1){
$number = "0".$number;
}
if(is_string($length)){
$length .= hex2bin($number);
$current_octet = $length.$current_octet;
$length = null;
$octets .= $current_octet;
$current_octet = "";
unset($number);
break;
} else {
$current_octet .= hex2bin($number);
}
}
if(!is_string($length)){
$length++;
}
if(is_string($number)){
if(isset($key) && isset($element[$key + 1])){
$key++;
$number = $element[$key];
continue;
} else if($length > 0){
$number = $length;
$length = "";
continue;
}
}
}
}An example function to parse octets is provided below.
$data = "";
$current_position = 0;
while(true){
$number = hexdec(bin2hex(substr($data, $current_position, 1)));
$current_position++;
if($number === 255){
$element = $element + ($number >> 1) + 1;
} else if($number <= 128){
$element = $element + $number;
} else if ($number > 128){
//return exception;
}
}The sequence of octets and what format to parse them in follows below.
- Cipher: An array of octets parsed as numbers.
- Original length: The result of all octets in the sequence added up.
- Byte range: The result of all octets in the sequence added up.
- Content Type: A string filled with all the octets in the sequence converted from ISO-10646-1 to UTF-8.
- Original MD5 Checksum: A string filled with all the octets in the sequence converted from integer to a 2 byte hexadecimal string.
- Size Array: An array of octets parsed as numbers, each number represents the length in bytes of the length character.
It is ultimately up to you on how to implement the octet creating/parsing function.