I’m currently trying to translate a python script to ruby. Now I’m stuck on a part that uses a raw string for a regex.
This is the original python code:
JavaScript
x
4
1
pat = re.compile(r'.{4}xAAxEExAAx76x1BxECxBBx20xF1xE6x51.{1}x78x9C')
2
match = pat.search(string)
3
(start_match, end_match) = match.span()
4
This is my attempt to translate it to ruby:
JavaScript
1
3
1
pat = Regexp.compile('.{4}\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51.{1}\x78\x9C')
2
start_match, end_match = string.enum_for(:scan, pat).map { Regexp.last_match.begin(0) }
3
Unfortunately I must be doing it wrong because I get this error:
JavaScript
1
2
1
invalid multibyte escape: /.{4}\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51.{1}\x78\x9C/ (RegexpError)
2
I also tried:
JavaScript
1
6
1
regex_String = <<'TEXT'
2
.{4}xAAxEExAAx76x1BxECxBBx20xF1xE6x51.{1}x78x9C
3
TEXT
4
pat = Regexp.compile(regex_String)
5
start_match, end_match = string.enum_for(:scan, pat).map { Regexp.last_match.begin(0) }
6
But it results in the same error.
To place it in context, here is the whole script:
JavaScript
1
60
60
1
# Commented lines are the original python code
2
# Uncommented lines are the translated ruby code
3
4
#import zlib
5
#import sys
6
#import re
7
#import binascii
8
require "zlib"
9
require "hex_string"
10
11
#if(len(sys.argv) < 2 or sys.argv[1] == "-h"):
12
# print "usage: python DecompNewDell.py <biosupdate.exe>"
13
# exit()
14
15
if ARGV.length < 1 or ARGV[0] == "-h"
16
puts "usage: ruby DecompNewDell.rb <biosupdate.exe>";
17
exit
18
end
19
20
#f = open(sys.argv[1], "rb")
21
#string = f.read()
22
f = File.open(ARGV[0], 'rb')
23
string = f.read
24
25
#pat = re.compile(r'.{4}xAAxEExAAx76x1BxECxBBx20xF1xE6x51.{1}x78x9C')
26
#match = pat.search(string)
27
#(start_match, end_match) = match.span()
28
pat = Regexp.compile('.{4}\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51.{1}\x78\x9C')
29
start_match, end_match = string.enum_for(:scan, pat).map { Regexp.last_match.begin(0) }
30
31
#compessed_len = string[start_match:start_match+4]
32
compressed_len = string[start_match..start_match+4]
33
34
#compessed_len = binascii.b2a_hex(compessed_len[::-1])
35
compessed_len.reverse!
36
compessed_len = compessed_len.to_hex_string(false)
37
38
#compessed_len = long(compessed_len, 16)
39
compessed_len = compessed_len.to_i(16)
40
41
#read len bytes out of the file into the new string to decompress
42
#f.seek(start_match+16)
43
#string = f.read(compessed_len)
44
f.seek start_match+16
45
string = f.read compessed_len
46
47
#o = zlib.decompress(string)
48
o = Zlib::Inflate.inflate(string)
49
50
#f2 = open(sys.argv[1] + "_decompressed.hdr", "wb")
51
#f2.write(o)
52
#f.close()
53
#f2.close()
54
#print "Decompressed data written to %s_decompressed.hdr" % sys.argv[1]
55
f2 = File.open(ARGV[0] + "_decompressed.hdr", 'wb')
56
f2.write(o)
57
f.close()
58
f2.close()
59
puts "Decompressed data written to #{ARGV[0]}_decompressed.hdr"
60
Advertisement
Answer
This answer shows why the problem raised.
https://stackoverflow.com/a/47785810/12349985
And there has a solution for this situation.
https://techoverflow.net/2013/12/29/solving-invalid-multibyte-escape-xfexff-in-ruby-vpim/