r110158 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110157‎ | r110158 | r110159 >
Date:20:05, 27 January 2012
Author:reedy
Status:ok
Tags:
Comment:
Trim trailing whitespace from r105244
Modified paths:
  • /trunk/extensions/TorBlock/ASN1Parser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TorBlock/ASN1Parser.php
@@ -18,7 +18,7 @@
1919 22 => 'IA5String',
2020 23 => 'UTCTime'
2121 );
22 -
 22+
2323 const INTEGER = 2;
2424 const BIT_STRING = 3;
2525 const OCTET_STRING = 4;
@@ -30,43 +30,43 @@
3131 const T61String = 20;
3232 const IA5String = 22;
3333 const UTCTime = 23;
34 -
 34+
3535 static function encodeLength($number) {
3636 if ($number < 128)
3737 return chr($number);
38 -
 38+
3939 $out = pack("N*", $number);
4040 $out = ltrim( $out, "\0" );
41 -
 41+
4242 return chr( 0x80 | strlen($out) ) . $out;
4343 }
4444
45 - static function decode($buffer) {
 45+ static function decode($buffer) {
4646 if ( strlen( $buffer ) < 2 )
4747 throw new ASN1Exception( 'ASN1 string is too short' );
48 -
 48+
4949 $i = 0;
5050 $result = array();
5151 while ( $i < strlen( $buffer ) ) {
5252 $item = array();
53 -
 53+
5454 $tag = ord( $buffer[$i] );
5555 $item['tag-class'] = self::$tagClasses[$tag >> 6];
56 -
 56+
5757 $i++;
5858 $constructed = $tag & 0x20; // Primitive/Constructed bit
5959 $tag &= 0x1f;
60 -
 60+
6161 if ( $tag != 0x1f ) {
6262 // Great! it's in one octet
63 -
 63+
6464 $item['tag'] = $tag;
6565 } else {
6666 $tag = 0;
6767 for (; $i < strlen($buffer); $i++) {
6868 $t = ord( $buffer[$i] );
6969 $tag = ( $tag << 7 ) | ( $t & 0x7f );
70 -
 70+
7171 // The last octet of the tag identifier will have the high-bit set to 0
7272 if ( ( $t & 0x80 ) == 0 ) {
7373 break;
@@ -78,11 +78,11 @@
7979 $item['tag'] = $tag;
8080 $i++;
8181 }
82 -
 82+
8383 /* Parse length */
8484 $length = ord( $buffer[$i] );
8585 $i++;
86 -
 86+
8787 if ( ( $length & 0x80 ) == 0 ) {
8888 $item['length'] = $length;
8989 } else {
@@ -91,7 +91,7 @@
9292 if ( strlen( $lengthBytes ) != $l ) {
9393 throw new ASN1Exception( 'Not enough bytes for long-form length' );
9494 }
95 -
 95+
9696 $length = 0;
9797 for ($j = 0; $j < $l; $j++, $i++) {
9898 $length = ( $length << 8 ) | ord( $lengthBytes[$j] );
@@ -100,41 +100,41 @@
101101 throw new ASN1Exception( 'Overflow calculating length' );
102102 }
103103 $item['length'] = $length;
104 -
 104+
105105 }
106106 $item['contents'] = substr( $buffer, $i, $length );
107107 $i += $length;
108 -
 108+
109109 if ( $constructed ) {
110110 $item['contents'] = self::decode( $item['contents'] );
111111 } elseif ( $tag == 6 ) {
112112 /* We could show the pretty name here instead of the hex dump
113 - *
 113+ *
114114 * a source could be crypto/objects/objects.txt from openssl
115 - * project or crypto/objects/obj_dat.txt (where hexadecimal
 115+ * project or crypto/objects/obj_dat.txt (where hexadecimal
116116 * data is available )
117117 */
118118 $item['contents'] = $item['contents'];
119119 } else {
120120 $item['contents'] = $item['contents'];
121121 }
122 -
 122+
123123 $result[] = $item;
124124 }
125 -
 125+
126126 return count ( $result ) > 1 ? $result : $result[0];
127127 }
128 -
 128+
129129 /**
130130 * Prettifies an array output by decode()
131131 */
132132 static function prettyDecode($decodedArray) {
133133 $decoded = $decodedArray;
134134 array_walk_recursive($decoded, array( __CLASS__, 'prettyItem' ) );
135 -
 135+
136136 return $decoded;
137137 }
138 -
 138+
139139 static protected function prettyItem(&$value, $key) {
140140 switch ($key) {
141141 case 'contents': // Not called when contents is an array
@@ -147,7 +147,7 @@
148148 break;
149149 }
150150 }
151 -
 151+
152152 static function buildTag($tagId, $contents) {
153153 if ( is_int( $tagId ) && $tagId < 31 ) {
154154 if ( $tagId == self::SEQUENCE || $tagId == self::SET ) {
@@ -159,7 +159,7 @@
160160 }
161161 $out .= self::encodeLength( strlen( $contents ) );
162162 $out .= $contents;
163 -
 163+
164164 return $out;
165165 }
166166 }

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r105244Parser of ASN1 syntax used in RSA keys.platonides23:28, 5 December 2011

Status & tagging log