Skip to content

Commit

Permalink
lib/ldap: Use the indefinite form of the length octets for encoded so…
Browse files Browse the repository at this point in the history
…rt rules where appropriate

I guess we may never need this, but hey :)

refs #9364
  • Loading branch information
lippserd committed Sep 4, 2015
1 parent 9b22b24 commit 297a433
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions library/Icinga/Protocol/Ldap/LdapConnection.php
Expand Up @@ -936,24 +936,44 @@ public function cleanupAttributes($attributes, array $requestedFields)
*/
protected function encodeSortRules(array $sortRules)
{
// TODO(el): Indefinite form of length octet
$sequenceOf = '';

foreach ($sortRules as $rule) {
$reversed = '0101' . ($rule[1] === Sortable::SORT_DESC ? 'ff' : '00');
if (false && $rule[1] === Sortable::SORT_DESC) {
$reversed = '0101ff';
} else {
$reversed = '';
}

$attributeType = unpack('H*', $rule[0]);
$attributeType = $attributeType[1];
$attributeType = '04' . str_pad(dechex(strlen($attributeType) / 2), 2, '0', STR_PAD_LEFT) . $attributeType;
$sequence = '30'
. str_pad(dechex(strlen($attributeType . $reversed) / 2), 2, '0', STR_PAD_LEFT)
. $attributeType
. $reversed;
$attributeOctets = strlen($attributeType) / 2;
if ($attributeOctets >= 127) {
// Use the indefinite form of the length octets (the long form would be another option)
$attributeType = '0440' . $attributeType . '0000';

} else {
$attributeType = '04' . str_pad(dechex($attributeOctets), 2, '0', STR_PAD_LEFT) . $attributeType;
}

$sequence = $attributeType . $reversed;
$sequenceOctects = strlen($sequence) / 2;
if ($sequenceOctects >= 127) {
$sequence = '3040' . $sequence . '0000';
} else {
$sequence = '30' . str_pad(dechex($sequenceOctects), 2, '0', STR_PAD_LEFT) . $sequence;
}

$sequenceOf .= $sequence;
}
$sequenceOf = '30'
. str_pad(dechex(strlen($sequenceOf) / 2), 2, '0', STR_PAD_LEFT)
. $sequenceOf;

$sequenceOfOctets = strlen($sequenceOf) / 2;
if ($sequenceOfOctets >= 127) {
$sequenceOf = '3040' . $sequenceOf . '0000';
} else {
$sequenceOf = '30' . str_pad(dechex($sequenceOfOctets), 2, '0', STR_PAD_LEFT) . $sequenceOf;
}

return hex2bin($sequenceOf);
}

Expand Down

0 comments on commit 297a433

Please sign in to comment.