@@ -18,31 +18,42 @@ public function __construct(array $config)
18
18
19
19
protected function decodeChain (array $ tokens ): array
20
20
{
21
- $ newTokens = [];
22
21
$ previousByteTokens = [];
22
+ $ newTokens = [];
23
23
24
24
foreach ($ tokens as $ token ) {
25
25
$ bytes = null ;
26
+
27
+ // Check if the token is of the form <0xXX>
26
28
if (strlen ($ token ) === 6 && str_starts_with ($ token , '<0x ' ) && str_ends_with ($ token , '> ' )) {
29
+ // Extract the hexadecimal value from the token
27
30
$ byte = hexdec (substr ($ token , 3 , 2 ));
28
31
if (!is_nan ($ byte )) {
29
32
$ bytes = $ byte ;
30
33
}
31
34
}
35
+
32
36
if ($ bytes !== null ) {
37
+ // Add byte to previousByteTokens
33
38
$ previousByteTokens [] = $ bytes ;
34
39
} else {
35
- if (count ($ previousByteTokens ) > 0 ) {
36
- $ string = $ this ->bytesToString ($ previousByteTokens );
37
- $ newTokens [] = $ string ;
38
- $ previousByteTokens = [];
40
+ // If we have accumulated byte tokens, decode them to a string
41
+ if (!empty ($ previousByteTokens )) {
42
+ $ string = pack ('C* ' , ...$ previousByteTokens ); // Convert bytes back to string
43
+ $ newTokens [] = $ string ; // Add decoded string to newTokens
44
+ $ previousByteTokens = []; // Reset byte accumulator
39
45
}
46
+ // Add the non-byte token to newTokens
40
47
$ newTokens [] = $ token ;
41
48
}
42
49
}
43
- if (count ($ previousByteTokens ) > 0 ) {
44
- $ string = $ this ->bytesToString ($ previousByteTokens );
50
+
51
+
52
+ // After the loop, if there are still byte tokens, decode them
53
+ if (!empty ($ previousByteTokens )) {
54
+ $ string = pack ('C* ' , ...$ previousByteTokens ); // Convert remaining bytes to string
45
55
$ newTokens [] = $ string ;
56
+ $ previousByteTokens = []; // Reset byte accumulator
46
57
}
47
58
48
59
return $ newTokens ;
@@ -59,4 +70,4 @@ protected function bytesToString(array $bytes): string
59
70
$ binaryString = pack ('C* ' , ...$ bytes );
60
71
return mb_convert_encoding ($ binaryString , 'ISO-8859-1 ' );
61
72
}
62
- }
73
+ }
0 commit comments