| 1 | # Package Tap3edit (https://github.com/tap3edit/TAP3-Tap3edit) | ||
| 2 | # designed to decode, modify and encode Roaming GSM TAP/RAP/ | ||
| 3 | # NRT files | ||
| 4 | # | ||
| 5 | # Copyright (c) 2004-2018 Javier Gutierrez. All rights | ||
| 6 | # reserved. | ||
| 7 | # This program is free software; you can redistribute | ||
| 8 | # it and/or modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # This program contains TAP, RAP and NRTRDE ASN.1 | ||
| 11 | # Specification. The ownership of the TAP/RAP/NRTRDE ASN.1 | ||
| 12 | # Specifications belong to the GSM MoU Association | ||
| 13 | # (http://www.gsm.org) and should be used under following | ||
| 14 | # conditions: | ||
| 15 | # | ||
| 16 | # Copyright (c) 2000 GSM MoU Association. Restricted − Con | ||
| 17 | # fidential Information. Access to and distribution of this | ||
| 18 | # document is restricted to the persons listed under the | ||
| 19 | # heading Security Classification Category*. This document | ||
| 20 | # is confidential to the Association and is subject to copy | ||
| 21 | # right protection. This document is to be used only for | ||
| 22 | # the purposes for which it has been supplied and informa | ||
| 23 | # tion contained in it must not be disclosed or in any other | ||
| 24 | # way made available, in whole or in part, to persons other | ||
| 25 | # than those listed under Security Classification Category* | ||
| 26 | # without the prior written approval of the Association. The | ||
| 27 | # GSM MoU Association (âAssociationâ) makes no representa | ||
| 28 | # tion, warranty or undertaking (express or implied) with | ||
| 29 | # respect to and does not accept any responsibility for, and | ||
| 30 | # hereby disclaims liability for the accuracy or complete | ||
| 31 | # ness or timeliness of the information contained in this | ||
| 32 | # document. The information contained in this document may | ||
| 33 | # be subject to change without prior notice. | ||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | package TAP3::Tap3edit; | ||
| 38 | |||
| 39 | use strict; | ||
| 40 | use Convert::ASN1 qw(:io :debug); # Handler of ASN1 Codes. Should be installed first. | ||
| 41 | use File::Spec; | ||
| 42 | use File::Basename; | ||
| 43 | use Carp; | ||
| 44 | |||
| 45 | |||
| 46 | BEGIN { | ||
| 47 | use Exporter; | ||
| 48 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); | ||
| 49 | $VERSION = "0.35"; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub new { | ||
| 54 | my $proto = shift; | ||
| 55 | my $class = ref($proto) || $proto ; | ||
| 56 | my $self = {}; | ||
| 57 | $self->{_filename} = undef; | ||
| 58 | $self->{_spec_file} = undef; | ||
| 59 | $self->{_supl_spec_file} = undef; | ||
| 60 | $self->{_asn} = Convert::ASN1->new(); | ||
| 61 | $self->{_dic_decode} = {}; # Stores the file decode with $self->{_dic_asn} | ||
| 62 | $self->{_dic_asn} = $self->{_asn}; # Stores the ASN Specification | ||
| 63 | $self->{spec_path} = [ ( grep(-d $_, map(File::Spec->catdir($_, qw(TAP3 Spec)), @INC)), File::Spec->curdir) ]; | ||
| 64 | $self->{_version} = undef; | ||
| 65 | $self->{_release} = undef; | ||
| 66 | $self->{_supl_version} = undef; # Tap version inside the RAP file | ||
| 67 | $self->{_supl_release} = undef; # Tap release inside the RAP file | ||
| 68 | $self->{_file_type} = undef; # TAP, RAP or NRT | ||
| 69 | $self->{error} = undef; | ||
| 70 | bless ($self, $class); | ||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | #---------------------------------------------------------------- | ||
| 76 | # Method: structure | ||
| 77 | # Description: Contains the structure of the TAP/RAP/NRT file | ||
| 78 | # into a HASH | ||
| 79 | # Parameters: N/A | ||
| 80 | # Returns: HASH | ||
| 81 | # Type: Public | ||
| 82 | # Restrictions: N/A | ||
| 83 | #---------------------------------------------------------------- | ||
| 84 | sub structure { | ||
| 85 | my $self = shift; | ||
| 86 | if (@_) { $self->{_dic_decode} = shift } | ||
| 87 | return $self->{_dic_decode}; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | #---------------------------------------------------------------- | ||
| 92 | # Method: version | ||
| 93 | # Description: contains and updates the main version of the | ||
| 94 | # TAP/RAP/NRT file | ||
| 95 | # Parameters: N/A | ||
| 96 | # Returns: SCALAR: version number | ||
| 97 | # Type: Public | ||
| 98 | # Restrictions: N/A | ||
| 99 | #---------------------------------------------------------------- | ||
| 100 | sub version { | ||
| 101 | my $self = shift; | ||
| 102 | if ( @_ ) { | ||
| 103 | if ( ! $self->{_version} ) { | ||
| 104 | $self->{_version} = shift ; | ||
| 105 | } else { | ||
| 106 | $self->{error}="The Version cannot be changed"; | ||
| 107 | croak $self->error(); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | return $self->{_version}; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | #---------------------------------------------------------------- | ||
| 115 | # Method: supl_version | ||
| 116 | # Description: contains and updates the suplementary version of the | ||
| 117 | # RAP file | ||
| 118 | # Parameters: N/A | ||
| 119 | # Returns: SCALAR: release number | ||
| 120 | # Type: Public | ||
| 121 | # Restrictions: Valid just for RAP files | ||
| 122 | #---------------------------------------------------------------- | ||
| 123 | sub supl_version { | ||
| 124 | my $self = shift; | ||
| 125 | if ( @_ ) { | ||
| 126 | if ( ! $self->{_supl_version} ) { | ||
| 127 | $self->{_supl_version} = shift ; | ||
| 128 | } else { | ||
| 129 | $self->{error}="The Suplementary Version cannot be changed"; | ||
| 130 | croak $self->error(); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | return $self->{_supl_version}; | ||
| 134 | } | ||
| 135 | |||
| 136 | #---------------------------------------------------------------- | ||
| 137 | # Method: release | ||
| 138 | # Description: contains and updates the main release of the | ||
| 139 | # TAP/RAP/NRT file | ||
| 140 | # Parameters: N/A | ||
| 141 | # Returns: SCALAR: release number | ||
| 142 | # Type: Public | ||
| 143 | # Restrictions: N/A | ||
| 144 | #---------------------------------------------------------------- | ||
| 145 | sub release { | ||
| 146 | my $self = shift; | ||
| 147 | if ( @_ ) { | ||
| 148 | if ( ! $self->{_release} ) { | ||
| 149 | $self->{_release} = shift ; | ||
| 150 | } else { | ||
| 151 | $self->{error}="The Release cannot be changed"; | ||
| 152 | croak $self->error(); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | return $self->{_release}; | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | #---------------------------------------------------------------- | ||
| 160 | # Method: supl_release | ||
| 161 | # Description: contains and updates the suplementary release of the | ||
| 162 | # RAP file | ||
| 163 | # Parameters: N/A | ||
| 164 | # Returns: SCALAR: release number | ||
| 165 | # Type: Public | ||
| 166 | # Restrictions: Valid just for RAP files | ||
| 167 | #---------------------------------------------------------------- | ||
| 168 | sub supl_release { | ||
| 169 | my $self = shift; | ||
| 170 | if ( @_ ) { | ||
| 171 | if ( ! $self->{_supl_release} ) { | ||
| 172 | $self->{_supl_release} = shift ; | ||
| 173 | } else { | ||
| 174 | $self->{error}="The Suplementary Release cannot be changed"; | ||
| 175 | croak $self->error(); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | return $self->{_supl_release}; | ||
| 179 | } | ||
| 180 | |||
| 181 | #---------------------------------------------------------------- | ||
| 182 | # Method: file_type | ||
| 183 | # Description: contains and updates the type of the file | ||
| 184 | # the values can be: TAP/RAP/NRT. | ||
| 185 | # Parameters: N/A | ||
| 186 | # Returns: SCALAR: file type ("RAP","TAP","NRT") | ||
| 187 | # Type: Public | ||
| 188 | # Restrictions: N/A | ||
| 189 | #---------------------------------------------------------------- | ||
| 190 | sub file_type { | ||
| 191 | my $self = shift; | ||
| 192 | if ( @_ ) { | ||
| 193 | if ( ! $self->{_file_type} ) { | ||
| 194 | my $file_type = shift; | ||
| 195 | |||
| 196 | unless ($file_type =~ /^[TR]AP$|^NRT$/) { | ||
| 197 | croak("Unsupported File Type $file_type"); | ||
| 198 | } | ||
| 199 | |||
| 200 | $self->{_file_type} = $file_type ; | ||
| 201 | } else { | ||
| 202 | $self->{error}="The File Type cannot be changed"; | ||
| 203 | croak $self->error(); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | return $self->{_file_type}; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | #---------------------------------------------------------------- | ||
| 211 | # Method: get_info | ||
| 212 | # Description: gets the basic information of the TAP/RAP/NRT | ||
| 213 | # files: version, release, supl_version (for RAP | ||
| 214 | # files), supl_release (for RAP files), file type. | ||
| 215 | # Parameters: filename | ||
| 216 | # Returns: N/A | ||
| 217 | # Type: Public | ||
| 218 | # Restrictions: N/A | ||
| 219 | #---------------------------------------------------------------- | ||
| 220 | sub get_info { | ||
| 221 | my $self = shift; | ||
| 222 | my $filename = shift; | ||
| 223 | $self->_filename($filename); | ||
| 224 | $self->_get_file_version || return undef ; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | #---------------------------------------------------------------- | ||
| 229 | # Method: _filename | ||
| 230 | # Description: contains and updates the name of the TAP/RAP/NRT | ||
| 231 | # files | ||
| 232 | # Parameters: filename | ||
| 233 | # Returns: filename | ||
| 234 | # Type: Private | ||
| 235 | # Restrictions: N/A | ||
| 236 | #---------------------------------------------------------------- | ||
| 237 | sub _filename { | ||
| 238 | my $self = shift; | ||
| 239 | if (@_) { $self->{_filename} = shift } | ||
| 240 | return $self->{_filename}; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | #---------------------------------------------------------------- | ||
| 245 | # Method: spec_file | ||
| 246 | # Description: contains and updates the name of the file | ||
| 247 | # with specifications ASN.1 | ||
| 248 | # Parameters: filename of specifications ASN.1 | ||
| 249 | # Returns: filename of specifications ASN.1 | ||
| 250 | # Type: Public | ||
| 251 | # Restrictions: N/A | ||
| 252 | #---------------------------------------------------------------- | ||
| 253 | sub spec_file { | ||
| 254 | my $self = shift; | ||
| 255 | if (@_) { $self->{_spec_file} = shift } | ||
| 256 | return $self->{_spec_file}; | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | #---------------------------------------------------------------- | ||
| 261 | # Method: supl_spec_file | ||
| 262 | # Description: contains and updates the name of the file | ||
| 263 | # with specifications ASN.1 for the version of | ||
| 264 | # the TAP file included in the RAP file. | ||
| 265 | # Parameters: filename of specifications ASN.1 | ||
| 266 | # Returns: filename of specifications ASN.1 | ||
| 267 | # Type: Public | ||
| 268 | # Restrictions: Valid just for RAP files | ||
| 269 | #---------------------------------------------------------------- | ||
| 270 | sub supl_spec_file { | ||
| 271 | my $self = shift; | ||
| 272 | if (@_) { $self->{_supl_spec_file} = shift } | ||
| 273 | return $self->{_supl_spec_file}; | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | #---------------------------------------------------------------- | ||
| 278 | # Method: _dic_decode | ||
| 279 | # Description: contains and updates the HASH which stores | ||
| 280 | # the decoded information from the TAP/RAP/NRT file. | ||
| 281 | # This variable is also used for the method: | ||
| 282 | # "structure". | ||
| 283 | # Parameters: HASH | ||
| 284 | # Returns: HASH | ||
| 285 | # Type: Private | ||
| 286 | # Restrictions: N/A | ||
| 287 | #---------------------------------------------------------------- | ||
| 288 | sub _dic_decode { | ||
| 289 | my $self = shift; | ||
| 290 | if (@_) { $self->{_dic_decode} = shift } | ||
| 291 | return $self->{_dic_decode}; | ||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | #---------------------------------------------------------------- | ||
| 296 | # Method: _dic_asn | ||
| 297 | # Description: contains and updates the object used to store | ||
| 298 | # the tree of the specifictions ASN.1 starting | ||
| 299 | # from the DataInterChange/RapDataInterChange tag. | ||
| 300 | # Parameters: object | ||
| 301 | # Returns: object | ||
| 302 | # Type: Private | ||
| 303 | # Restrictions: N/A | ||
| 304 | #---------------------------------------------------------------- | ||
| 305 | sub _dic_asn { | ||
| 306 | my $self = shift; | ||
| 307 | if (@_) { $self->{_dic_asn} = shift } | ||
| 308 | return $self->{_dic_asn}; | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | #---------------------------------------------------------------- | ||
| 313 | # Method: _asn | ||
| 314 | # Description: contains and updates the object used to store | ||
| 315 | # the constructor of Convert::ASN1 | ||
| 316 | # Parameters: object | ||
| 317 | # Returns: object | ||
| 318 | # Type: Private | ||
| 319 | # Restrictions: N/A | ||
| 320 | #---------------------------------------------------------------- | ||
| 321 | sub _asn { | ||
| 322 | my $self = shift; | ||
| 323 | if (@_) { $self->{_asn} = shift } | ||
| 324 | return $self->{_asn}; | ||
| 325 | } | ||
| 326 | |||
| 327 | |||
| 328 | #---------------------------------------------------------------- | ||
| 329 | # Method: _asn_path | ||
| 330 | # Description: contains the list of PATH where | ||
| 331 | # to find the specifications ASN.1. | ||
| 332 | # The default values are "TAP3/Spec" from the insta- | ||
| 333 | # llation and "." (current directory). The used | ||
| 334 | # array (spec_path) can be updated with new PATHs | ||
| 335 | # Parameters: ARRAY | ||
| 336 | # Returns: ARRAY | ||
| 337 | # Type: Private | ||
| 338 | # Restrictions: N/A | ||
| 339 | #---------------------------------------------------------------- | ||
| 340 | sub _asn_path { | ||
| 341 | my $self = shift; | ||
| 342 | return $self->{spec_path}; | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | #---------------------------------------------------------------- | ||
| 348 | # Function: bcd_to_hexa | ||
| 349 | # Description: Converts the input binary format from the | ||
| 350 | # TAP/RAP/NRT files into Hexadecimal string. | ||
| 351 | # Parameters: binary_string | ||
| 352 | # Returns: hexadecimal value | ||
| 353 | # Type: Private | ||
| 354 | # Restrictions: N/A | ||
| 355 | #---------------------------------------------------------------- | ||
| 356 | sub bcd_to_hexa | ||
| 357 | { | ||
| 358 | my $in=shift; | ||
| 359 | unpack("H*",$in); | ||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 363 | #---------------------------------------------------------------- | ||
| 364 | # Function: bcd_to_asc | ||
| 365 | # Description: Converts the input binary format from the | ||
| 366 | # TAP/RAP/NRT files into decimal. | ||
| 367 | # Parameters: binary_string | ||
| 368 | # Returns: ascii value | ||
| 369 | # Type: Private | ||
| 370 | # Restrictions: N/A | ||
| 371 | #---------------------------------------------------------------- | ||
| 372 | sub bcd_to_asc | ||
| 373 | { | ||
| 374 | my $in=shift; | ||
| 375 | my $out=0; | ||
| 376 | for (my $i=0;$i<length($in);$i++) { | ||
| 377 | $out.=sprintf("%03d", ord(substr($in,$i,1))); | ||
| 378 | } | ||
| 379 | return $out; | ||
| 380 | } | ||
| 381 | |||
| 382 | |||
| 383 | |||
| 384 | #---------------------------------------------------------------- | ||
| 385 | # Method: _get_file_version | ||
| 386 | # Description: sets the file version/release of the TAP/RAP/NRT | ||
| 387 | # file by matching patterns | ||
| 388 | # Parameters: N/A | ||
| 389 | # Returns: N/A | ||
| 390 | # Type: Private | ||
| 391 | # Restrictions: N/A | ||
| 392 | #---------------------------------------------------------------- | ||
| 393 | sub _get_file_version | ||
| 394 | { | ||
| 395 | my $self=shift; | ||
| 396 | |||
| 397 | my $filename=$self->_filename; | ||
| 398 | |||
| 399 | my $file_type=undef; | ||
| 400 | my $version=undef; | ||
| 401 | my $release=undef; | ||
| 402 | my $rap_version=undef; | ||
| 403 | my $rap_release=undef; | ||
| 404 | my $buf_in; | ||
| 405 | |||
| 406 | |||
| 407 | ## | ||
| 408 | ## 1. If we decode the file we just encoded the file type, version and release should be empty | ||
| 409 | ## | ||
| 410 | |||
| 411 | $self->{_version} = undef; | ||
| 412 | $self->{_release} = undef; | ||
| 413 | $self->{_supl_version} = undef; # Tap version inside the RAP file | ||
| 414 | $self->{_supl_release} = undef; # Tap release inside the RAP file | ||
| 415 | $self->{_file_type}=undef; | ||
| 416 | |||
| 417 | |||
| 418 | ## | ||
| 419 | ## 2. We get the file_type, version and release by matching strings | ||
| 420 | ## | ||
| 421 | |||
| 422 | open FILE, "<$filename" or do { $self->{error}="$! for file $filename" ; return undef }; | ||
| 423 | binmode FILE; | ||
| 424 | |||
| 425 | # 1 Kb should be more than enough to find the Release and Version | ||
| 426 | read FILE, $buf_in, 1000; | ||
| 427 | |||
| 428 | close FILE; | ||
| 429 | |||
| 430 | $buf_in=unpack("H*", $buf_in); | ||
| 431 | |||
| 432 | |||
| 433 | ## | ||
| 434 | ## 3. Here we scan the buffer matching the patterns | ||
| 435 | ## | ||
| 436 | |||
| 437 | while ($buf_in =~ /(?: | ||
| 438 | (^61.+5f814405) (?# For Tap files) | ||
| 439 | | | ||
| 440 | (^62) (?# For Notification files) | ||
| 441 | | | ||
| 442 | (^7f8416) (?# For Rap files) | ||
| 443 | | | ||
| 444 | (^7f8417) (?# For Acknowledment files) | ||
| 445 | | | ||
| 446 | (?:5f814901)(..) (?# Will match: SpecificationVersionNumber ) | ||
| 447 | | | ||
| 448 | (?:5f813d01)(..) (?# Will match: ReleaseVersionNumber ) | ||
| 449 | | | ||
| 450 | (?:5f842001)(..) (?# Will match: RapSpecificationVersionNumber ) | ||
| 451 | | | ||
| 452 | (?:5f841f01)(..) (?# Will match: RapReleaseVersionNumber ) | ||
| 453 | | | ||
| 454 | (^61.+5f2901)(..) (?# For NRTRDE files, and SpecificationVersionNumber for NRTRDE) | ||
| 455 | | | ||
| 456 | (?:5f2501)(..) (?# Will match: ReleaseVersionNumber for NRTRDE ) | ||
| 457 | | | ||
| 458 | . | ||
| 459 | )/sgxo ) { | ||
| 460 | if (defined $1) { | ||
| 461 | $file_type="TAP"; | ||
| 462 | } | ||
| 463 | if (defined $2) { | ||
| 464 | $file_type="NOT"; | ||
| 465 | } | ||
| 466 | if (defined $3) { | ||
| 467 | $file_type="RAP"; | ||
| 468 | } | ||
| 469 | if (defined $4) { | ||
| 470 | $file_type="ACK"; | ||
| 471 | } | ||
| 472 | if (defined $5) { | ||
| 473 | $version=ord(pack("H*",$5)); | ||
| 474 | } | ||
| 475 | if (defined $6) { | ||
| 476 | $release=ord(pack("H*",$6)); | ||
| 477 | } | ||
| 478 | if (defined $7) { | ||
| 479 | $rap_version=ord(pack("H*",$7)); | ||
| 480 | } | ||
| 481 | if (defined $8) { | ||
| 482 | $rap_release=ord(pack("H*",$8)); | ||
| 483 | } | ||
| 484 | if (defined $9) { | ||
| 485 | $file_type="NRT"; | ||
| 486 | } | ||
| 487 | if (defined $10) { | ||
| 488 | $version=ord(pack("H*",$10)); | ||
| 489 | } | ||
| 490 | if (defined $11) { | ||
| 491 | $release=ord(pack("H*",$11)); | ||
| 492 | } | ||
| 493 | } | ||
| 494 | |||
| 495 | ## | ||
| 496 | ## 4. According to what is found we set the file_type, version and release. | ||
| 497 | ## | ||
| 498 | |||
| 499 | if (!defined $file_type) { | ||
| 500 | $self->{error}="Unknown File format. Cannot decode."; | ||
| 501 | croak $self->error(); | ||
| 502 | } | ||
| 503 | |||
| 504 | if ($file_type eq "TAP" or $file_type eq "NOT") { | ||
| 505 | if (! $release or ! $version ) { | ||
| 506 | $self->{error}="'specificationVersionNumer' or 'releaseVersionNumber' not found in TAP File"; | ||
| 507 | croak $self->error(); | ||
| 508 | } else { | ||
| 509 | $self->{_version}=$version; | ||
| 510 | $self->{_release}=$release; | ||
| 511 | $self->{_file_type}="TAP"; | ||
| 512 | } | ||
| 513 | } elsif ($file_type eq "RAP") { | ||
| 514 | if ( $rap_version && $rap_release ) { | ||
| 515 | if (! $release or ! $version ) { | ||
| 516 | $self->{error}="'specificationVersionNumer' or 'releaseVersionNumber' not found in RAP File"; | ||
| 517 | croak $self->error(); | ||
| 518 | } else { | ||
| 519 | $self->{_version}=$rap_version; | ||
| 520 | $self->{_release}=$rap_release; | ||
| 521 | $self->{_supl_version}=$version; | ||
| 522 | $self->{_supl_release}=$release; | ||
| 523 | $self->{_file_type}="RAP"; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | } elsif ($file_type eq "ACK") { | ||
| 527 | $self->{_version}=1; | ||
| 528 | $self->{_release}=4; | ||
| 529 | $self->{_supl_version}=3; | ||
| 530 | $self->{_supl_release}=10; | ||
| 531 | $self->{_file_type}="RAP"; | ||
| 532 | } elsif ($file_type eq "NRT") { | ||
| 533 | if (! $release or ! $version ) { | ||
| 534 | $self->{error}="'specificationVersionNumer' or 'releaseVersionNumber' not found in NRT File"; | ||
| 535 | croak $self->error(); | ||
| 536 | } else { | ||
| 537 | $self->{_version}=$version; | ||
| 538 | $self->{_release}=$release; | ||
| 539 | $self->{_file_type}="NRT"; | ||
| 540 | } | ||
| 541 | } else { | ||
| 542 | $self->{error}="Unknown File format. Cannot decode."; | ||
| 543 | croak $self->error(); | ||
| 544 | } | ||
| 545 | |||
| 546 | 1; | ||
| 547 | } | ||
| 548 | |||
| 549 | |||
| 550 | #---------------------------------------------------------------- | ||
| 551 | # Method: _select_spec_file | ||
| 552 | # Description: Selects the file with the ASN Specifications | ||
| 553 | # according to the version of the file. | ||
| 554 | # Nomenclature specified: TAP0309.asn for the spec- | ||
| 555 | # ifications of the TAP3r9 and RAP0102.asn for the | ||
| 556 | # specifications of the RAP1r2. | ||
| 557 | # Parameters: version | ||
| 558 | # release | ||
| 559 | # file_type | ||
| 560 | # Returns: filename of the Specification ASN.1 | ||
| 561 | # Type: Private | ||
| 562 | # Restrictions: N/A | ||
| 563 | #---------------------------------------------------------------- | ||
| 564 | sub _select_spec_file | ||
| 565 | { | ||
| 566 | my $self=shift; | ||
| 567 | |||
| 568 | my $version=shift; | ||
| 569 | my $release=shift; | ||
| 570 | my $file_type=shift; | ||
| 571 | |||
| 572 | $version=sprintf("%02d", $version); | ||
| 573 | $release=sprintf("%02d", $release); | ||
| 574 | |||
| 575 | my $spec_file; | ||
| 576 | |||
| 577 | NEXT_CYCLE1: foreach ( @{$self->_asn_path} ) { | ||
| 578 | $spec_file=$_."/".$file_type.$version.$release.".asn"; | ||
| 579 | if ( $spec_file ) { | ||
| 580 | last NEXT_CYCLE1; | ||
| 581 | } | ||
| 582 | } | ||
| 583 | |||
| 584 | return $spec_file || return undef; | ||
| 585 | } | ||
| 586 | |||
| 587 | |||
| 588 | |||
| 589 | #---------------------------------------------------------------- | ||
| 590 | # Method: _select_asn_struct | ||
| 591 | # Description: Selects and prepares the ASN specification | ||
| 592 | # to be used. | ||
| 593 | # Parameters: N/A | ||
| 594 | # Returns: N/A | ||
| 595 | # Type: Private | ||
| 596 | # Restrictions: $self->version, $self->release and | ||
| 597 | # $self->file_type should defined. | ||
| 598 | #---------------------------------------------------------------- | ||
| 599 | sub _select_asn_struct | ||
| 600 | { | ||
| 601 | my $self=shift; | ||
| 602 | |||
| 603 | my $size; | ||
| 604 | my $spec_buf_in; | ||
| 605 | my $spec_buf_in_tmp; | ||
| 606 | |||
| 607 | ## | ||
| 608 | ## 1. Select the ASN.1 structure | ||
| 609 | ## | ||
| 610 | |||
| 611 | ## | ||
| 612 | ## 1.1. Main ASN.1 structure file. | ||
| 613 | ## | ||
| 614 | |||
| 615 | if ( ! $self->spec_file ) { | ||
| 616 | $self->spec_file($self->_select_spec_file($self->{_version}, $self->{_release}, $self->file_type)) || return undef; | ||
| 617 | } | ||
| 618 | |||
| 619 | ## | ||
| 620 | ## 1.2. If we are working with a RAP file we need to know also the version of TAP Inside the RAP. | ||
| 621 | ## | ||
| 622 | |||
| 623 | if ( ! $self->supl_spec_file and $self->file_type eq "RAP" ) { | ||
| 624 | $self->supl_spec_file($self->_select_spec_file($self->{_supl_version}, $self->{_supl_release}, "TAP")) || return undef; | ||
| 625 | } | ||
| 626 | |||
| 627 | ## | ||
| 628 | ## 2. The content of the definitions files are stored into a scalar. | ||
| 629 | ## | ||
| 630 | |||
| 631 | ## | ||
| 632 | ## 2.1. First the definition file is opend and the content filtered and stored into $spec_buf_in | ||
| 633 | ## | ||
| 634 | |||
| 635 | ($size) = (stat($self->spec_file))[7] or do { $self->{error}="$! reading ".$self->spec_file; return undef }; | ||
| 636 | open FILE, "<".$self->spec_file or do { $self->{error}="$! opening ".$self->spec_file; return undef }; | ||
| 637 | |||
| 638 | while (<FILE>) { | ||
| 639 | if ( /^...Structure of a (... batch|...... record)/.../END/ ) { | ||
| 640 | if ( $_ !~ m/Structure of a (Tap batch|NRTRDE)/ and $_ !~ m/END/ ) { | ||
| 641 | $spec_buf_in_tmp=$spec_buf_in_tmp.$_; | ||
| 642 | } | ||
| 643 | } | ||
| 644 | } | ||
| 645 | |||
| 646 | close FILE; | ||
| 647 | |||
| 648 | ## | ||
| 649 | ## 2.2. If it is a RAP file, we read as well the specification of its tap file. | ||
| 650 | ## | ||
| 651 | |||
| 652 | if ( $self->file_type eq "RAP" ) { | ||
| 653 | ($size) = (stat($self->supl_spec_file))[7] or do { $self->{error}="$! reading ".$self->supl_spec_file; return undef }; | ||
| 654 | open FILE, "<".$self->supl_spec_file or do { $self->{error}="$! opening ".$self->supl_spec_file; return undef }; | ||
| 655 | while (<FILE>) { | ||
| 656 | if ( /^...Structure of a ... batch/.../END/ ) { | ||
| 657 | if ( $_ !~ m/Structure of a Tap batch/ and $_ !~ m/END/ ) { | ||
| 658 | $spec_buf_in_tmp=$spec_buf_in_tmp.$_; | ||
| 659 | } | ||
| 660 | } | ||
| 661 | } | ||
| 662 | close FILE; | ||
| 663 | } | ||
| 664 | |||
| 665 | # Following algorithm will strip the chain ",\n..." since the three dots and a comma | ||
| 666 | # in the last element is not supported by Convert::ASN1 | ||
| 667 | |||
| 668 | ### 20120501: Following code was commented out because of performance. | ||
| 669 | ### The specifications were modified instead. | ||
| 670 | |||
| 671 | # while($spec_buf_in_tmp =~ /(?: | ||
| 672 | # (,[^\n]*\n(?:\s|\t)*?\.\.\.[^\n,]*\n) | ||
| 673 | # | | ||
| 674 | # ([\s|\t]*?\.\.\.(?:\s|\t)*?,[^\n]*\n) | ||
| 675 | # | | ||
| 676 | # (\(SIZE\(\d+(?:\.\.\d+)*?\)\)) | ||
| 677 | # | | ||
| 678 | # (.*?) | ||
| 679 | # )/sgxo) { | ||
| 680 | # if (defined $1 or defined $2 or defined $3) { | ||
| 681 | # $spec_buf_in=$spec_buf_in."\n"; | ||
| 682 | # } else { | ||
| 683 | # $spec_buf_in=$spec_buf_in."$+"; | ||
| 684 | # } | ||
| 685 | # } | ||
| 686 | |||
| 687 | $spec_buf_in = $spec_buf_in_tmp; | ||
| 688 | |||
| 689 | ## | ||
| 690 | ## 3. let's prepare the asn difinition. | ||
| 691 | ## | ||
| 692 | |||
| 693 | my $asn = $self->_asn; | ||
| 694 | $asn->prepare( $spec_buf_in ) or do { $self->{error}=$asn->error; return undef }; | ||
| 695 | |||
| 696 | |||
| 697 | ## | ||
| 698 | ## 4. Initialization with DataInterChange | ||
| 699 | ## | ||
| 700 | |||
| 701 | my $dic_asn; | ||
| 702 | if ( $self->file_type eq "TAP" ) { | ||
| 703 | $dic_asn = $asn->find('DataInterChange') or do { $self->{error}=$asn->error; return undef }; | ||
| 704 | } elsif ( $self->file_type eq "NRT" ) { | ||
| 705 | $dic_asn = $asn->find('Nrtrde') or do { $self->{error}=$asn->error; return undef }; | ||
| 706 | } else { | ||
| 707 | $dic_asn = $asn->find('RapDataInterChange') or do { $self->{error}=$asn->error; return undef }; | ||
| 708 | } | ||
| 709 | $self->_dic_asn($dic_asn); | ||
| 710 | |||
| 711 | } | ||
| 712 | |||
| 713 | |||
| 714 | |||
| 715 | #---------------------------------------------------------------- | ||
| 716 | # Method: decode | ||
| 717 | # Description: decodes the TAP/RAP/NRT file into a HASH for its | ||
| 718 | # later editing. | ||
| 719 | # Parameters: filename | ||
| 720 | # Returns: N/A | ||
| 721 | # Type: Public | ||
| 722 | # Restrictions: N/A | ||
| 723 | #---------------------------------------------------------------- | ||
| 724 | sub decode { | ||
| 725 | my $self=shift; | ||
| 726 | |||
| 727 | my $filename=shift; | ||
| 728 | my $buf_in; | ||
| 729 | my $size; | ||
| 730 | |||
| 731 | $self->_filename($filename); | ||
| 732 | |||
| 733 | |||
| 734 | ## | ||
| 735 | ## 1. Get the version to decode the file. | ||
| 736 | ## | ||
| 737 | |||
| 738 | $self->_get_file_version || return undef; | ||
| 739 | |||
| 740 | |||
| 741 | ## | ||
| 742 | ## 2. Selection of ASN Structure. | ||
| 743 | ## | ||
| 744 | |||
| 745 | $self->_select_asn_struct || return undef; | ||
| 746 | |||
| 747 | |||
| 748 | ## | ||
| 749 | ## 3. We open and read all the TAP/RAP/NRT file at once. | ||
| 750 | ## | ||
| 751 | |||
| 752 | my $FILE; | ||
| 753 | open $FILE, "<$filename" or do { $self->{error}="$! opening $filename"; return undef }; | ||
| 754 | binmode $FILE; | ||
| 755 | asn_read ($FILE, $buf_in); | ||
| 756 | close $FILE; | ||
| 757 | |||
| 758 | |||
| 759 | ## | ||
| 760 | ## 4. Decode file buffer into the ASN1 tree. | ||
| 761 | ## | ||
| 762 | |||
| 763 | $self->{_dic_decode} = $self->_dic_asn->decode($buf_in) or do { $self->{error}=$self->_dic_asn->error; croak $self->error() }; | ||
| 764 | |||
| 765 | } | ||
| 766 | |||
| 767 | |||
| 768 | |||
| 769 | #---------------------------------------------------------------- | ||
| 770 | # Method: encode | ||
| 771 | # Description: encode the HASH structure into a new TAP/RAP/NRT | ||
| 772 | # file | ||
| 773 | # Parameters: filename | ||
| 774 | # Returns: N/A | ||
| 775 | # Type: Public | ||
| 776 | # Restrictions: N/A | ||
| 777 | #---------------------------------------------------------------- | ||
| 778 | sub encode { | ||
| 779 | |||
| 780 | my $self = shift; | ||
| 781 | |||
| 782 | my $filename=shift; | ||
| 783 | $self->_filename($filename); | ||
| 784 | |||
| 785 | |||
| 786 | ## | ||
| 787 | ## 1. _dic_decode will be the decoded tree of a real tap file | ||
| 788 | ## | ||
| 789 | |||
| 790 | ## | ||
| 791 | ## 2. Select structure according to version, release and type. | ||
| 792 | ## | ||
| 793 | |||
| 794 | ## In the case we want just to encode, we need to select and prepare | ||
| 795 | ## the structure we want to use. E.g If we want to get a TAP3r9 | ||
| 796 | ## we need to select the ASN.1 structure for the TAP3r9 | ||
| 797 | $self->_select_asn_struct || return undef; | ||
| 798 | |||
| 799 | |||
| 800 | ## | ||
| 801 | ## 3. Encode ASN1 tree into the file. | ||
| 802 | ## | ||
| 803 | |||
| 804 | my $buf_out = $self->_dic_asn->encode($self->_dic_decode) or do { $self->{error}=$self->_dic_asn->error; croak $self->error() }; | ||
| 805 | |||
| 806 | |||
| 807 | ## | ||
| 808 | ## 4. Write and close file | ||
| 809 | ## | ||
| 810 | |||
| 811 | open FILE_OUT, ">$filename" or do { $self->{error}="$! writing $filename"; croak $self->error() }; | ||
| 812 | binmode FILE_OUT; | ||
| 813 | print FILE_OUT $buf_out ; | ||
| 814 | close FILE_OUT; | ||
| 815 | } | ||
| 816 | |||
| 817 | sub DESTROY {} | ||
| 818 | |||
| 819 | sub error { $_[0]->{error} } | ||
| 820 | |||
| 821 | 1; |