This repository was archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathUnixCodec.php
More file actions
executable file
·125 lines (106 loc) · 3.95 KB
/
UnixCodec.php
File metadata and controls
executable file
·125 lines (106 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* OWASP Enterprise Security API (ESAPI)
*
* This file is part of the Open Web Application Security Project (OWASP)
* Enterprise Security API (ESAPI) project.
*
* LICENSE: This source file is subject to the New BSD license. You should read
* and accept the LICENSE before you use, modify, and/or redistribute this
* software.
*
* PHP version 5.2
*
* @category OWASP
* @package ESAPI_Codecs
* @author Linden Darling <Linden.Darling@jds.net.au>
* @author Mike Boberski <boberski_michael@bah.com>
* @copyright 2009-2010 The OWASP Foundation
* @license http://www.opensource.org/licenses/bsd-license.php New BSD license
* @version SVN: $Id$
* @link http://www.owasp.org/index.php/ESAPI
*/
require_once 'Codec.php';
/**
* Reference implementation of the Unix codec.
*
* @category OWASP
* @package ESAPI_Codecs
* @author Linden Darling <Linden.Darling@jds.net.au>
* @author Mike Boberski <boberski_michael@bah.com>
* @copyright 2009-2010 The OWASP Foundation
* @license http://www.opensource.org/licenses/bsd-license.php New BSD license
* @version Release: @package_version@
* @link http://www.owasp.org/index.php/ESAPI
*/
class UnixCodec extends Codec
{
/**
* Public Constructor
*/
function __construct()
{
parent::__construct();
}
/**
* {@inheritdoc}
*/
public function encodeCharacter($immune, $c)
{
//detect encoding, special-handling for chr(172) and chr(128) to chr(159)
//which fail to be detected by mb_detect_encoding()
$initialEncoding = $this->detectEncoding($c);
// Normalize encoding to UTF-32
$_4ByteUnencodedOutput = $this->normalizeEncoding($c);
// Start with nothing; format it to match the encoding of the string passed
//as an argument.
$encodedOutput = mb_convert_encoding("", $initialEncoding);
// Grab the 4 byte character.
$_4ByteCharacter = $this->forceToSingleCharacter($_4ByteUnencodedOutput);
// Get the ordinal value of the character.
list(, $ordinalValue) = unpack("N", $_4ByteCharacter);
// check for immune characters
if ($this->containsCharacter($_4ByteCharacter, $immune)) {
return $encodedOutput . chr($ordinalValue);
}
// Check for alphanumeric characters
$hex = $this->getHexForNonAlphanumeric($_4ByteCharacter);
if ($hex === null) {
return $encodedOutput . chr($ordinalValue);
}
return $encodedOutput . "\\" . $c;
}
/**
* {@inheritdoc}
*/
public function decodeCharacter($input)
{
// Assumption/prerequisite: $c is a UTF-32 encoded string
$_4ByteEncodedInput = $input;
if (mb_substr($_4ByteEncodedInput, 0, 1, "UTF-32") === null) {
// 1st character is null, so return null
// eat the 1st character off the string and return null
//todo: no point in doing this
$_4ByteEncodedInput = mb_substr(
$input, 1, mb_strlen($_4ByteEncodedInput, "UTF-32"), "UTF-32");
return array(
'decodedCharacter' => null,
'encodedString' => null
);
}
// if this is not an encoded character, return null
if (mb_substr($_4ByteEncodedInput, 0, 1, "UTF-32") != $this->normalizeEncoding('\\')) {
// 1st character is not part of encoding pattern, so return null
return array(
'decodedCharacter' => null,
'encodedString' => null
);
}
// 1st character is part of encoding pattern...
$second = mb_substr($_4ByteEncodedInput, 1, 1, "UTF-32");
return array(
'decodedCharacter' => $second,
'encodedString' => mb_substr($input, 0, 2, "UTF-32")
);
}
}