# HG changeset patch # User Alessio Caiazza # Date 1269169725 -3600 # Branch stable-0.x.x # Node ID e0a3ab14e4aa7314724aacee8d3d32a3bd67712b # Parent c9f74665d8ef42057396d66d91167216dc46d2b4 Backporting some fixes from 1.0.0 release diff -r c9f74665d8ef42057396d66d91167216dc46d2b4 -r e0a3ab14e4aa7314724aacee8d3d32a3bd67712b .hgignore --- a/.hgignore Tue Feb 16 11:21:14 2010 +0100 +++ b/.hgignore Sun Mar 21 12:08:45 2010 +0100 @@ -15,6 +15,7 @@ *.framework *.pbxuser *.gem +*.bundle Makefile syntax: regexp diff -r c9f74665d8ef42057396d66d91167216dc46d2b4 -r e0a3ab14e4aa7314724aacee8d3d32a3bd67712b ext/libyubikey/libyubikey.c --- a/ext/libyubikey/libyubikey.c Tue Feb 16 11:21:14 2010 +0100 +++ b/ext/libyubikey/libyubikey.c Sun Mar 21 12:08:45 2010 +0100 @@ -33,6 +33,8 @@ #include #include "yubikey.h" +//#undef NDEBUG + #define MODHEX_SING_ENCODE "encode" #define MODHEX_SING_DECODE "decode" #define MODHEX_ENCODE "modhex_encode" @@ -98,7 +100,7 @@ the terminating zero. */ yubikey_modhex_decode(dst, RSTRING(str)->ptr, src_size); - return rb_str_new2(dst); + return rb_str_new(dst, src_size/2); } /* diff -r c9f74665d8ef42057396d66d91167216dc46d2b4 -r e0a3ab14e4aa7314724aacee8d3d32a3bd67712b lib/hex.rb --- a/lib/hex.rb Tue Feb 16 11:21:14 2010 +0100 +++ b/lib/hex.rb Sun Mar 21 12:08:45 2010 +0100 @@ -47,7 +47,7 @@ # def self.encode( obj ) s = obj.to_str - s.unpack('U'*s.length).collect {|x| x.to_s 16}.join + s.unpack('U'*s.length).collect {|x| tmp = x.to_s 16; tmp.length == 1 ? "0#{tmp}" : tmp }.join rescue ArgumentError #non UTF-8 string s = obj.to_str @@ -68,17 +68,19 @@ end # call-seq: - # YubiRuby::HEX.decode("hex string") -> "string" or "" + # YubiRuby::HEX.decode("hex string") -> "string" # # Decodes obj.to_str into a string. # - # An hex string length must be pair, if not an - # empty string is returned. + # An hex string length must be pair, if not a + # NoHexEncodedError excpetion is raised def self.decode( obj ) s = obj.to_str dec = "" - if (s.length % 2 == 0) + if hex?(s) (s.length/2).times { |i| dec << s[i*2,2].hex.chr } + else + raise NoHexEncodedError.new end return dec @@ -91,5 +93,25 @@ def hex_decode HEX.decode(self) end + + # call-seq: + # YubiRuby::HEX.hex?("hex string") -> true or false + # + # Tests if obj.to_str is a valid a hex string. + # + # An hex string length must be pair. + def self.hex?( obj ) + s = obj.to_str + return false unless (s.length % 2 == 0) + return (s.upcase =~ /[^A-F0-9]/).nil? + end + + # call-seq: + # hex? -> true or false + # + # Invokes YubiRuby::HEX.hex? on +self+. + def hex? + HEX.hex?(self) + end end end diff -r c9f74665d8ef42057396d66d91167216dc46d2b4 -r e0a3ab14e4aa7314724aacee8d3d32a3bd67712b lib/yubiruby.rb --- a/lib/yubiruby.rb Tue Feb 16 11:21:14 2010 +0100 +++ b/lib/yubiruby.rb Sun Mar 21 12:08:45 2010 +0100 @@ -27,6 +27,7 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +require 'exceptions' require 'libyubikey' require 'hex' diff -r c9f74665d8ef42057396d66d91167216dc46d2b4 -r e0a3ab14e4aa7314724aacee8d3d32a3bd67712b tests/tc_modhex.rb --- a/tests/tc_modhex.rb Tue Feb 16 11:21:14 2010 +0100 +++ b/tests/tc_modhex.rb Sun Mar 21 12:08:45 2010 +0100 @@ -1,8 +1,4 @@ -require "test/unit" - -require "libyubikey" - -class TestLibyubikey < Test::Unit::TestCase +class TestModHex < Test::Unit::TestCase def test_string str = "pippo234strd" assert_equal(enc = str.modhex_encode, YubiRuby::ModHex.encode(str)) diff -r c9f74665d8ef42057396d66d91167216dc46d2b4 -r e0a3ab14e4aa7314724aacee8d3d32a3bd67712b tests/ts_yubiruby.rb --- a/tests/ts_yubiruby.rb Tue Feb 16 11:21:14 2010 +0100 +++ b/tests/ts_yubiruby.rb Sun Mar 21 12:08:45 2010 +0100 @@ -3,4 +3,8 @@ require "test/unit" +require 'yubiruby' + +String.send(:include, YubiRuby::HEX) + require "tc_modhex"