Skip to content

Commit f8097ec

Browse files
deal with array of integers before packing bytes (instead of string appends)
the former is a data structure that the runtime can improve processing of, and avoids realloc'ing the string on each call (as well as packing one byte at a time)
1 parent 157d720 commit f8097ec

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

lib/openssl/asn1.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ def put_length(length)
411411
length.chr
412412
else
413413
data = integer_to_octets(length)
414-
(data.size | 0x80).chr << data
414+
data.unshift(data.size | 0x80)
415+
data.pack("C*")
415416
end
416417
end
417418

@@ -432,16 +433,14 @@ def put_integer(value)
432433
end
433434

434435
def integer_to_octets(i)
435-
if i >= 0
436-
done = 0
437-
else
438-
done = -1
436+
done = i >= 0 ? 0 : -1
437+
438+
octets = []
439+
440+
until i == done
441+
octets.unshift(i & 0xff)
442+
i >>= 8
439443
end
440-
octets = "".b
441-
begin
442-
octets = (i & 0xff).chr << octets
443-
i = i >> 8
444-
end until i == done
445444
octets
446445
end
447446

0 commit comments

Comments
 (0)