4
4
require 'stringio'
5
5
require 'cgi'
6
6
require 'rex/powershell'
7
-
8
- %W{ iconv zlib } . each do |libname |
9
- begin
10
- old_verbose = $VERBOSE
11
- $VERBOSE = nil
12
- require libname
13
- rescue ::LoadError
14
- ensure
15
- $VERBOSE = old_verbose
16
- end
17
- end
7
+ require 'zlib'
18
8
19
9
module Rex
20
10
@@ -55,7 +45,8 @@ module Text
55
45
56
46
DefaultPatternSets = [ Rex ::Text ::UpperAlpha , Rex ::Text ::LowerAlpha , Rex ::Text ::Numerals ]
57
47
58
- # In case Iconv isn't loaded
48
+ # The Iconv translation table. The Iconv gem is deprecated in favor of
49
+ # String#encode, yet there is no encoding for EBCDIC. See #4525
59
50
Iconv_EBCDIC = [
60
51
"\x00 " , "\x01 " , "\x02 " , "\x03 " , "7" , "-" , "." , "/" , "\x16 " , "\x05 " ,
61
52
"%" , "\v " , "\f " , "\r " , "\x0E " , "\x0F " , "\x10 " , "\x11 " , "\x12 " , "\x13 " ,
@@ -374,31 +365,26 @@ def self.to_raw(str)
374
365
return str
375
366
end
376
367
368
+ # Converts US-ASCII to UTF-8, skipping over any characters which don't
369
+ # convert cleanly. This is a convenience method that wraps
370
+ # String#encode with non-raising default paramaters.
377
371
#
378
- # Converts ISO-8859-1 to UTF-8
379
- #
372
+ # @param str [String] An encodable ASCII string
373
+ # @return [String] a UTF-8 equivalent
374
+ # @note This method will discard invalid characters
380
375
def self . to_utf8 ( str )
381
-
382
- if str . respond_to? ( :encode )
383
- # Skip over any bytes that fail to convert to UTF-8
384
- return str . encode ( 'utf-8' , { :invalid => :replace , :undef => :replace , :replace => '' } )
385
- end
386
-
387
- begin
388
- Iconv . iconv ( "utf-8" , "iso-8859-1" , str ) . join ( " " )
389
- rescue
390
- raise ::RuntimeError , "Your installation does not support iconv (needed for utf8 conversion)"
391
- end
376
+ str . encode ( 'utf-8' , { :invalid => :replace , :undef => :replace , :replace => '' } )
392
377
end
393
378
394
- #
395
- # Converts ASCII to EBCDIC
396
- #
397
379
class IllegalSequence < ArgumentError ; end
398
380
399
- # A native implementation of the ASCII->EBCDIC table, used to fall back from using
400
- # Iconv
401
- def self . to_ebcdic_rex ( str )
381
+ # A native implementation of the ASCII to EBCDIC conversion table, since
382
+ # EBCDIC isn't available to String#encode as of Ruby 2.1
383
+ #
384
+ # @param str [String] An encodable ASCII string
385
+ # @return [String] an EBCDIC encoded string
386
+ # @note This method will raise in the event of invalid characters
387
+ def self . to_ebcdic ( str )
402
388
new_str = [ ]
403
389
str . each_byte do |x |
404
390
if Iconv_ASCII . index ( x . chr )
@@ -410,9 +396,13 @@ def self.to_ebcdic_rex(str)
410
396
new_str . join
411
397
end
412
398
413
- # A native implementation of the EBCDIC->ASCII table, used to fall back from using
414
- # Iconv
415
- def self . from_ebcdic_rex ( str )
399
+ # A native implementation of the EBCIDC to ASCII conversion table, since
400
+ # EBCDIC isn't available to String#encode as of Ruby 2.1
401
+ #
402
+ # @param str [String] an EBCDIC encoded string
403
+ # @return [String] An encodable ASCII string
404
+ # @note This method will raise in the event of invalid characters
405
+ def self . from_ebcdic ( str )
416
406
new_str = [ ]
417
407
str . each_byte do |x |
418
408
if Iconv_EBCDIC . index ( x . chr )
@@ -424,29 +414,6 @@ def self.from_ebcdic_rex(str)
424
414
new_str . join
425
415
end
426
416
427
- def self . to_ebcdic ( str )
428
- begin
429
- Iconv . iconv ( "EBCDIC-US" , "ASCII" , str ) . first
430
- rescue ::Iconv ::IllegalSequence => e
431
- raise e
432
- rescue
433
- self . to_ebcdic_rex ( str )
434
- end
435
- end
436
-
437
- #
438
- # Converts EBCIDC to ASCII
439
- #
440
- def self . from_ebcdic ( str )
441
- begin
442
- Iconv . iconv ( "ASCII" , "EBCDIC-US" , str ) . first
443
- rescue ::Iconv ::IllegalSequence => e
444
- raise e
445
- rescue
446
- self . from_ebcdic_rex ( str )
447
- end
448
- end
449
-
450
417
#
451
418
# Returns the words in +str+ as an Array.
452
419
#
0 commit comments