#!/bin/sh -e
## Convert-ASN1-svn102.patch by Peter Marschall <peter@adpm.de>
##
## DP: adapt Convert::ASN1 to SVN revision 100

if [ $# -lt 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi

[ -f debian/patches/00patch-opts ] && . debian/patches/00patch-opts
patch_opts="${patch_opts:--f --no-backup-if-mismatch} ${2:+-d $2}"

case "$1" in
    -patch)
	patch $patch_opts -p0 < $0
	test -d debian  &&  echo `basename $0` >> debian/patchlist
	;;
    -unpatch)
	test -f debian/patchlist  &&  rm -f debian/patchlist
	patch $patch_opts -p0 -R < $0
	;;
    *)
	echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
	exit 1
	;;
esac

exit 0

--- lib/Convert/ASN1/Debug.pm
+++ lib/Convert/ASN1/Debug.pm	2005-09-29 10:19:28.000000000 +0200
@@ -1,11 +1,9 @@
-# Copyright (c) 2000-2002 Graham Barr <gbarr@pobox.com>. All rights reserved.
+# Copyright (c) 2000-2005 Graham Barr <gbarr@pobox.com>. All rights reserved.
 # This program is free software; you can redistribute it and/or
 # modify it under the same terms as Perl itself.
 
 package Convert::ASN1;
 
-# $Id: Debug.pm,v 1.8 2003/05/06 14:17:43 gbarr Exp $
-
 ##
 ## just for debug :-)
 ##
--- lib/Convert/ASN1/_decode.pm
+++ lib/Convert/ASN1/_decode.pm	2005-09-29 10:19:28.000000000 +0200
@@ -1,16 +1,12 @@
-# Copyright (c) 2000-2002 Graham Barr <gbarr@pobox.com>. All rights reserved.
+# Copyright (c) 2000-2005 Graham Barr <gbarr@pobox.com>. All rights reserved.
 # This program is free software; you can redistribute it and/or
 # modify it under the same terms as Perl itself.
 
 package Convert::ASN1;
 
-# $Id: _decode.pm,v 1.18 2003/05/07 09:26:36 gbarr Exp $
-
 BEGIN {
-  unless (CHECK_UTF8) {
     local $SIG{__DIE__};
-    eval { require bytes } and 'bytes'->import
-  }
+  eval { require bytes and 'bytes'->import };
 }
 
 # These are the subs that do the decode, they are called with
@@ -508,7 +504,7 @@
 
   my $mode = $_dec_time_opt{$_[0]->{'decode_time'} || ''} || 0;
 
-  if ($mode == 2) {
+  if ($mode == 2 or $_[6] == 0) {
     $_[3] = substr($_[4],$_[5],$_[6]);
     return;
   }
--- lib/Convert/ASN1/_encode.pm
+++ lib/Convert/ASN1/_encode.pm	2005-10-07 18:58:25.000000000 +0200
@@ -1,11 +1,9 @@
-# Copyright (c) 2000-2002 Graham Barr <gbarr@pobox.com>. All rights reserved.
+# Copyright (c) 2000-2005 Graham Barr <gbarr@pobox.com>. All rights reserved.
 # This program is free software; you can redistribute it and/or
 # modify it under the same terms as Perl itself.
 
 package Convert::ASN1;
 
-# $Id: _encode.pm,v 1.19 2003/10/08 12:28:09 gbarr Exp $
-
 BEGIN {
   unless (CHECK_UTF8) {
     local $SIG{__DIE__};
@@ -116,7 +114,7 @@
 
   if (ref($_[3])) {
     my $less = (8 - ($_[3]->[1] & 7)) & 7;
-    my $len = ($_[3]->[1] + 7)/8;
+    my $len = ($_[3]->[1] + 7) >> 3;
     $_[4] .= asn_encode_length(1+$len);
     $_[4] .= chr($less);
     $_[4] .= substr($$vref, 0, $len);
@@ -398,7 +396,7 @@
 sub _enc_bcd {
 # 0      1    2       3     4     5      6
 # $optn, $op, $stash, $var, $buf, $loop, $path
-  my $str = sprintf("%d",$_[3]);
+  my $str = ("$_[3]" =~ /^(\d+)/) ? $1 : "";
   $str .= "F" if length($str) & 1;
   $_[4] .= asn_encode_length(length($str) / 2);
   $_[4] .= pack("H*", $str);
--- lib/Convert/ASN1/IO.pm
+++ lib/Convert/ASN1/IO.pm	2005-09-29 10:19:28.000000000 +0200
@@ -1,11 +1,9 @@
-# Copyright (c) 2000-2004 Graham Barr <gbarr@pobox.com>. All rights reserved.
+# Copyright (c) 2000-2005 Graham Barr <gbarr@pobox.com>. All rights reserved.
 # This program is free software; you can redistribute it and/or
 # modify it under the same terms as Perl itself.
 
 package Convert::ASN1;
 
-# $Id: IO.pm 87 2005-03-04 02:03:40Z gbarr $
-
 use strict;
 use Socket;
 
--- lib/Convert/ASN1/parser.pm
+++ lib/Convert/ASN1/parser.pm	2005-09-29 10:19:28.000000000 +0200
@@ -3,14 +3,12 @@
 
 # 22 "parser.y"
 
-;# Copyright (c) 2000-2002 Graham Barr <gbarr@pobox.com>. All rights reserved.
+;# Copyright (c) 2000-2005 Graham Barr <gbarr@pobox.com>. All rights reserved.
 ;# This program is free software; you can redistribute it and/or
 ;# modify it under the same terms as Perl itself.
 
 package Convert::ASN1::parser;
 
-;# $Id: parser.pm,v 1.12 2003/05/07 15:13:28 gbarr Exp $
-
 use strict;
 use Convert::ASN1 qw(:all);
 use vars qw(
--- lib/Convert/ASN1.pod
+++ lib/Convert/ASN1.pod	2005-10-03 13:43:23.000000000 +0200
@@ -1,6 +1,6 @@
 =head1 NAME
 
-  Convert::ASN1 - ASN.1 Encode/Decode library
+Convert::ASN1 - ASN.1 Encode/Decode library
 
 =head1 SYNOPSYS
 
@@ -436,7 +436,7 @@
 
 =head1 COPYRIGHT
 
-Copyright (c) 2000-2002 Graham Barr <gbarr@pobox.com>. All rights reserved.
+Copyright (c) 2000-2005 Graham Barr <gbarr@pobox.com>. All rights reserved.
 This program is free software; you can redistribute it and/or
 modify it under the same terms as Perl itself.
 
--- Makefile.PL
+++ Makefile.PL	2005-09-29 10:19:29.000000000 +0200
@@ -22,7 +22,7 @@
 
   return <<POSTAMBLE;
 
-distdir : distsign
+mydist : manifest distsign dist
 
 run_byacc:
 	\$(PERL) mkparse parser.y lib/Convert/ASN1/parser.pm
--- parser.y
+++ parser.y	2005-09-29 10:19:29.000000000 +0200
@@ -20,14 +20,12 @@
 %token BY 20
 
 %{
-# Copyright (c) 2000-2002 Graham Barr <gbarr@pobox.com>. All rights reserved.
+# Copyright (c) 2000-2005 Graham Barr <gbarr@pobox.com>. All rights reserved.
 # This program is free software; you can redistribute it and/or
 # modify it under the same terms as Perl itself.
 
 package Convert::ASN1::parser;
 
-;# $Id: parser.y,v 1.11 2003/05/07 15:13:27 gbarr Exp $
-
 use strict;
 use Convert::ASN1 qw(:all);
 use vars qw(
--- README
+++ README	2005-09-29 10:19:29.000000000 +0200
@@ -7,7 +7,7 @@
 Convert::ASN1 will parse ASN.1 descriptions and will encode from and
 decode to perl data structures using a hierarchy of references.
 
-Copyright (c) 2000-2002 Graham Barr. All rights reserved.
+Copyright (c) 2000-2005 Graham Barr. All rights reserved.
 This package is free software; you can redistribute it and/or
 modify it under the same terms as Perl itself.
 
@@ -33,5 +33,3 @@
 If you find any bugs with Convert::ASN1, please report then via
 
   bugs-Convert-ASN1@rt.cpan.org
-
-$Id: README,v 1.4 2002/08/19 23:59:18 gbarr Exp $
--- t/00prim.t
+++ t/00prim.t	2005-09-29 10:19:28.000000000 +0200
@@ -6,7 +6,7 @@
 
 use Convert::ASN1 qw(:all);
 
-print "1..166\n";
+print "1..182\n";
 
 BEGIN { require 't/funcs.pl' }
 
@@ -237,8 +237,13 @@
 ##
 
 my %BCD = (
+  pack("C*", 0x04, 0x09, 0x12, 0x34, 0x56, 0x78, 0x91, 0x23, 0x45, 0x67, 0x89), "123456789123456789",
   pack("C*", 0x04, 0x04, 0x12, 0x34, 0x56, 0x78), 12345678,
   pack("C*", 0x04, 0x02, 0x56, 0x4f),             564,
+  pack("C*", 0x04, 0x00),             "",
+  pack("C*", 0x04, 0x00),             -1,
+  pack("C*", 0x04, 0x01, 0x0f),             0,
+  pack("C*", 0x04, 0x01, 0x2f),             2.2,
 );
 
 
@@ -248,6 +253,7 @@
   btest $test++, $asn->prepare('bcd BCDString') or warn $asn->error;
   stest $test++, $result, $asn->encode(bcd => $val) or warn $asn->error;
   btest $test++, $ret = $asn->decode($result) or warn $asn->error;
+  $val =~ s/\D.*//;
   stest $test++, $val, $ret->{'bcd'};
 }
 
--- t/06bigint.t
+++ t/06bigint.t	2005-09-29 10:19:28.000000000 +0200
@@ -9,7 +9,7 @@
 
 $^W=0 if $] < 5.005; # BigInt in 5.004 has undef issues
 
-print "1..59\n";
+print "1..67\n";
 
 btest 1, $asn = Convert::ASN1->new or warn $asn->error;
 btest 2, $asn->prepare(q(
@@ -107,3 +107,18 @@
 
 }
 
+my %BCD = (
+  pack("C*", 0x04, 0x05, 0x10, 0x73, 0x74, 0x18, 0x24),	     2**30,
+  pack("C*", 0x04, 0x00),	     -2**30,
+);
+
+while(($result,$val) = each %BCD) {
+  print "# BCDString $val\n";
+
+  btest $test++, $asn->prepare('bcd BCDString') or warn $asn->error;
+  stest $test++, $result, $asn->encode(bcd => $val) or warn $asn->error;
+  btest $test++, $ret = $asn->decode($result) or warn $asn->error;
+  $val =~ s/\D.*//;
+  stest $test++, $val, $ret->{'bcd'};
+}
+
