39
39
use FG \ASN1 \Universal \ObjectDescriptor ;
40
40
41
41
/**
42
- * @property ASN1Object $parent
43
- * @property ASN1Object[] $children
42
+ * @property ASN1Object $parent
43
+ * @property ASN1Object[] $children
44
44
* @property ContentLength $contentLength
45
- * @property Content $content
46
- * @property Identifier $identifier
47
- *
45
+ * @property Content $content
46
+ * @property Identifier $identifier
48
47
* Class Object is the base class for all concrete ASN.1 objects.
49
48
*/
50
49
abstract class ASN1Object
@@ -63,7 +62,8 @@ public function __construct(
63
62
ContentLength $ contentLength ,
64
63
Content $ content ,
65
64
array $ children = []
66
- ) {
65
+ )
66
+ {
67
67
$ this ->identifier = $ identifier ;
68
68
$ this ->contentLength = $ contentLength ;
69
69
$ this ->content = $ content ;
@@ -91,9 +91,7 @@ private function addChildren(array $children)
91
91
92
92
/**
93
93
* Encode the object using DER encoding.
94
- *
95
94
* @see http://en.wikipedia.org/wiki/X.690#DER_encoding
96
- *
97
95
* @return string the binary representation of an objects value
98
96
*/
99
97
abstract protected function getEncodedValue ();
@@ -139,10 +137,11 @@ public function getObjectLength()
139
137
abstract public function __toString (): string ;
140
138
141
139
/**
142
- * @param $binaryData
143
- * @param int $offsetIndex
144
- * @param Identifier $identifier
140
+ * @param $binaryData
141
+ * @param int $offsetIndex
142
+ * @param Identifier $identifier
145
143
* @param ContentLength $contentLength
144
+ *
146
145
* @return Object[]
147
146
* @throws ParserException
148
147
*/
@@ -153,9 +152,9 @@ public static function parseChildren(&$binaryData, &$offsetIndex = 0, ContentLen
153
152
$ octetsToRead = $ contentLength ->getLength ();
154
153
while ($ octetsToRead > 0 ) {
155
154
$ newChild = ASN1Object::fromBinary ($ binaryData , $ offsetIndex );
156
- if (is_null ($ newChild )) throw new ParserException ('Children not found ' , $ offsetIndex );
155
+ if (is_null ($ newChild )) throw new ParserException ('Children not found ' , $ offsetIndex );
157
156
$ octetsToRead -= ($ newChild ->contentLength ->getLength () + $ newChild ->identifier ->getNrOfOctets () + $ newChild ->contentLength ->getNrOfOctets ());
158
- $ children [] = $ newChild ;
157
+ $ children [] = $ newChild ;
159
158
}
160
159
} else {
161
160
/*try {*/
@@ -173,7 +172,7 @@ public static function parseChildren(&$binaryData, &$offsetIndex = 0, ContentLen
173
172
174
173
/**
175
174
* @param string $binaryData
176
- * @param int $offsetIndex
175
+ * @param int $offsetIndex
177
176
*
178
177
* @throws ParserException
179
178
* @return ASN1Object
@@ -199,11 +198,11 @@ public static function fromBinary(&$binaryData, &$offsetIndex = 0)
199
198
//разница между текущем положением сдвига и стартовым - длина детей, блина контента составного элемента
200
199
$ contentLength ->length = abs ($ startPos - $ offsetIndex );
201
200
} else {
202
- if ($ contentLength ->form === ContentLength::INDEFINITE_FORM ) {
201
+ if ($ contentLength ->form === ContentLength::INDEFINITE_FORM ) {
203
202
for (; ;) {
204
- $ firstOctet = $ binaryData [$ offsetIndex ];
203
+ $ firstOctet = $ binaryData [$ offsetIndex ];
205
204
$ secondOctet = $ binaryData [$ offsetIndex ++];
206
- if ($ firstOctet. $ secondOctet === chr (0 ) . chr (0 )) {
205
+ if ($ firstOctet . $ secondOctet === chr (0 ) . chr (0 )) {
207
206
$ contentLength ->length = abs ($ startPos - $ offsetIndex ) + 1 ;
208
207
break ;
209
208
}
@@ -217,7 +216,7 @@ public static function fromBinary(&$binaryData, &$offsetIndex = 0)
217
216
}
218
217
219
218
//todo exception raises when object not constructed and length form is indefinite - its wrong
220
- if (!is_int ($ contentLength ->length )) {
219
+ if (!is_int ($ contentLength ->length )) {
221
220
throw new ParserException ('Length of Object not determined ' , $ offsetIndex );
222
221
}
223
222
@@ -316,7 +315,7 @@ protected static function parseBinaryIdentifier($binaryData, &$offsetIndex)
316
315
if (strlen ($ binaryData ) <= $ offsetIndex ) {
317
316
throw new ParserException ('Can not parse identifier (long form) from data: Offset index larger than input size ' , $ offsetIndex );
318
317
}
319
- $ nextOctet = $ binaryData [$ offsetIndex ++];
318
+ $ nextOctet = $ binaryData [$ offsetIndex ++];
320
319
$ identifier .= $ nextOctet ;
321
320
322
321
if ((ord ($ nextOctet ) & 0x80 ) === 0 ) {
@@ -335,7 +334,7 @@ protected static function parseContentLength(&$binaryData, &$offsetIndex)
335
334
}
336
335
337
336
$ contentLengthOctets = $ binaryData [$ offsetIndex ++];
338
- $ firstOctet = ord ($ contentLengthOctets );
337
+ $ firstOctet = ord ($ contentLengthOctets );
339
338
340
339
if (($ firstOctet & 0x80 ) != 0 ) {
341
340
// bit 8 is set -> this is the long form
@@ -371,14 +370,14 @@ public function findByOid(string $oidString): array
371
370
{
372
371
$ objects = [];
373
372
374
- if ($ this instanceof ObjectIdentifier && (string ) $ this === $ oidString ) {
373
+ if ($ this instanceof ObjectIdentifier && (string )$ this === $ oidString ) {
375
374
return [$ this ];
376
375
}
377
376
378
- if ($ this ->isConstructed ()) {
377
+ if ($ this ->isConstructed ()) {
379
378
foreach ($ this ->children as $ child ) {
380
379
$ objectsFound = $ child ->findByOid ($ oidString );
381
- if (count ($ objectsFound ) > 0 ) {
380
+ if (count ($ objectsFound ) > 0 ) {
382
381
array_push ($ objects , ...$ objectsFound );
383
382
}
384
383
}
@@ -391,12 +390,12 @@ public function findByOid(string $oidString): array
391
390
392
391
public function remove ()
393
392
{
394
- if ($ this ->parent ) {
393
+ if ($ this ->parent ) {
395
394
foreach ($ this ->parent ->children as $ key => $ child ) {
396
- if ($ child === $ this ) {
397
- unset($ this ->parent ->children [$ key ]);
398
- $ this ->parent ->children = $ this ->parent ->getChildren ();
399
- $ this ->parent ->rebuildTree ();
395
+ if ($ child === $ this ) {
396
+ unset($ this ->parent ->children [$ key ]);
397
+ $ this ->parent ->children = $ this ->parent ->getChildren ();
398
+ $ this ->parent ->rebuildTree ();
400
399
}
401
400
}
402
401
@@ -420,13 +419,13 @@ public function getNrOfOctets(): int
420
419
public function rebuildTree ()
421
420
{
422
421
$ this ->restoreContentFromParts ();
423
- if ($ this ->validateLengthContent ()) {
422
+ if ($ this ->validateLengthContent ()) {
424
423
//nothing to rebuild
425
424
return true ;
426
425
} else {
427
426
//если форма неопределенная, то и у родителя она тоже неопределенная
428
427
// и энкодировать длину ни у одного родителя не нужно - просто изменить контент у всех предков
429
- if ($ this ->contentLength ->getLengthForm () === ContentLength::INDEFINITE_FORM ) {
428
+ if ($ this ->contentLength ->getLengthForm () === ContentLength::INDEFINITE_FORM ) {
430
429
$ this ->contentLength ->length = $ this ->content ->getNrOfOctets ();
431
430
} else {
432
431
$ this ->contentLength = ElementBuilder::createContentLength (
@@ -435,12 +434,12 @@ public function rebuildTree()
435
434
);
436
435
}
437
436
438
- if ($ this ->parent ) {
437
+ if ($ this ->parent ) {
439
438
$ this ->parent ->rebuildTree ();
440
439
}
441
440
}
442
441
443
- if ($ this ->validateLengthContent ()) {
442
+ if ($ this ->validateLengthContent ()) {
444
443
return true ;
445
444
} else {
446
445
throw new \Exception ('Дерево не восстановлено ' );
@@ -454,7 +453,7 @@ private function validateLengthContent()
454
453
455
454
private function restoreContentFromParts ()
456
455
{
457
- if ($ this ->identifier ->isConstructed ()) {
456
+ if ($ this ->identifier ->isConstructed ()) {
458
457
$ contentOctets = '' ;
459
458
foreach ($ this ->getChildren () as $ child ) {
460
459
$ contentOctets .= $ child ->getBinary ();
@@ -478,8 +477,8 @@ final public function isConstructed(): bool
478
477
*/
479
478
public function getSiblings (): array
480
479
{
481
- if ($ this ->parent && $ this ->parent ->isConstructed ()) {
482
- $ siblings = array_filter ($ this ->parent ->getChildren (), function ($ value ) {
480
+ if ($ this ->parent && $ this ->parent ->isConstructed ()) {
481
+ $ siblings = array_filter ($ this ->parent ->getChildren (), function ($ value ) {
483
482
return $ value !== $ this ;
484
483
});
485
484
@@ -507,10 +506,10 @@ public function getParent()
507
506
508
507
public function insertAfter (ASN1Object $ object )
509
508
{
510
- if ($ this ->parent ) {
509
+ if ($ this ->parent ) {
511
510
foreach ($ this ->parent ->children as $ key => $ child ) {
512
- if ($ child === $ this ) {
513
- if ($ key + 1 === count ($ this ->parent ->children )) {
511
+ if ($ child === $ this ) {
512
+ if ($ key + 1 === count ($ this ->parent ->children )) {
514
513
array_push ($ this ->parent ->children , $ object );
515
514
} else {
516
515
array_splice ($ this ->parent ->children , $ key + 1 , 0 , [$ object ]);
@@ -526,9 +525,9 @@ public function insertAfter(ASN1Object $object)
526
525
527
526
public function insertBefore (ASN1Object $ object )
528
527
{
529
- if ($ this ->parent ) {
528
+ if ($ this ->parent ) {
530
529
foreach ($ this ->parent ->children as $ key => $ child ) {
531
- if ($ child === $ this ) {
530
+ if ($ child === $ this ) {
532
531
array_splice ($ this ->parent ->children , $ key , 0 , [$ object ]);
533
532
$ object ->parent = $ this ->parent ;
534
533
$ this ->parent ->rebuildTree ();
@@ -541,16 +540,17 @@ public function insertBefore(ASN1Object $object)
541
540
542
541
/**
543
542
* @param string $className
543
+ *
544
544
* @return \FG\ASN1\ASN1Object[]
545
545
* @throws \Exception
546
546
*/
547
547
public function findChildrenByType ($ className )
548
548
{
549
- if (!class_exists ($ className )) {
549
+ if (!class_exists ($ className )) {
550
550
throw new Exception ('Unknown class type object ' );
551
551
}
552
552
553
- $ children = array_filter ($ this ->children , function ($ value ) use ($ className ) {
553
+ $ children = array_filter ($ this ->children , function ($ value ) use ($ className ) {
554
554
return is_a ($ value , $ className );
555
555
});
556
556
@@ -559,18 +559,18 @@ public function findChildrenByType($className)
559
559
560
560
/**
561
561
* @param $fileContent
562
- * @return \FG\ASN1\ASN1Object
563
562
*
563
+ * @return \FG\ASN1\ASN1Object
564
564
* @throws \Exception
565
565
*/
566
566
public final static function fromFile ($ fileContent )
567
567
{
568
568
$ temp = trim ($ fileContent );
569
- if (substr ($ fileContent , 0 , 1 ) === '- ' ) {
569
+ if (substr ($ fileContent , 0 , 1 ) === '- ' ) {
570
570
$ temp = preg_replace ('#.*?^-+[^-]+-+#ms ' , '' , $ fileContent , 1 );
571
- if (is_null ($ temp )) throw new \Exception ('Preg_error: ' . preg_last_error ());
571
+ if (is_null ($ temp )) throw new \Exception ('Preg_error: ' . preg_last_error ());
572
572
$ temp = preg_replace ('#--+[^-]+--+# ' , '' , $ temp );
573
- if (is_null ($ temp )) throw new \Exception ('Preg_error: ' . preg_last_error ());
573
+ if (is_null ($ temp )) throw new \Exception ('Preg_error: ' . preg_last_error ());
574
574
}
575
575
576
576
$ temp = str_replace (array ("\r" , "\n" , ' ' ), '' , $ temp );
@@ -582,7 +582,7 @@ public final static function fromFile($fileContent)
582
582
583
583
public function detach ()
584
584
{
585
- $ object = clone $ this ;
585
+ $ object = clone $ this ;
586
586
$ this ->parent = null ;
587
587
588
588
return $ object ;
@@ -593,7 +593,7 @@ public function detach()
593
593
*/
594
594
public function getRoot ()
595
595
{
596
- if (is_null ($ this ->parent )) return $ this ;
596
+ if (is_null ($ this ->parent )) return $ this ;
597
597
598
598
return $ this ->parent ->getRoot ();
599
599
}
0 commit comments