Skip to content

Commit cba3369

Browse files
committed
Style: General refactoring.
Bring code in line with RuboCop. Freeze constant objects, raise exceptions without #new where possible. Eliminate unnecessary `self` prefixes and correct formatting. Use 1.9-style hashes where possible. Align parameters and array items. Rename 'set_*' methods to 'update_*'. Rename 'get_*' methods to 'populate_*'
1 parent 1ca459c commit cba3369

File tree

9 files changed

+169
-157
lines changed

9 files changed

+169
-157
lines changed

lib/macho/exceptions.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ def initialize(offset)
146146
class HeaderPadError < ModificationError
147147
# @param filename [String] the filename
148148
def initialize(filename)
149-
super "Updated load commands do not fit in the header of " +
150-
"#{filename}. #{filename} needs to be relinked, possibly with " +
151-
"-headerpad or -headerpad_max_install_names"
149+
super "Updated load commands do not fit in the header of " \
150+
"#{filename}. #{filename} needs to be relinked, possibly with " \
151+
"-headerpad or -headerpad_max_install_names"
152152
end
153153
end
154154

lib/macho/fat_file.rb

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ def self.new_from_bin(bin)
3030
# @param filename [String] the fat file to load from
3131
# @raise [ArgumentError] if the given file does not exist
3232
def initialize(filename)
33-
raise ArgumentError.new("#{filename}: no such file") unless File.file?(filename)
33+
raise ArgumentError, "#{filename}: no such file" unless File.file?(filename)
3434

3535
@filename = filename
36-
@raw_data = File.open(@filename, "rb") { |f| f.read }
37-
@header = get_fat_header
38-
@fat_archs = get_fat_archs
39-
@machos = get_machos
36+
@raw_data = File.open(@filename, "rb", &:read)
37+
@header = populate_fat_header
38+
@fat_archs = populate_fat_archs
39+
@machos = populate_machos
4040
end
4141

4242
# Initializes a new FatFile instance from a binary string.
@@ -45,9 +45,9 @@ def initialize(filename)
4545
def initialize_from_bin(bin)
4646
@filename = nil
4747
@raw_data = bin
48-
@header = get_fat_header
49-
@fat_archs = get_fat_archs
50-
@machos = get_machos
48+
@header = populate_fat_header
49+
@fat_archs = populate_fat_archs
50+
@machos = populate_machos
5151
end
5252

5353
# The file's raw fat data.
@@ -142,13 +142,8 @@ def dylib_id
142142
# @raise [ArgumentError] if `new_id` is not a String
143143
# @see MachO::MachOFile#linked_dylibs
144144
def change_dylib_id(new_id, options = {})
145-
if !new_id.is_a?(String)
146-
raise ArgumentError.new("argument must be a String")
147-
end
148-
149-
if !machos.all?(&:dylib?)
150-
return nil
151-
end
145+
raise ArgumentError, "argument must be a String" unless new_id.is_a?(String)
146+
return unless machos.all?(&:dylib?)
152147

153148
each_macho(options) do |macho|
154149
macho.change_dylib_id(new_id, options)
@@ -157,7 +152,7 @@ def change_dylib_id(new_id, options = {})
157152
synchronize_raw_data
158153
end
159154

160-
alias :dylib_id= :change_dylib_id
155+
alias dylib_id= change_dylib_id
161156

162157
# All shared libraries linked to the file's Mach-Os.
163158
# @return [Array<String>] an array of all shared libraries
@@ -188,7 +183,7 @@ def change_install_name(old_name, new_name, options = {})
188183
synchronize_raw_data
189184
end
190185

191-
alias :change_dylib :change_install_name
186+
alias change_dylib change_install_name
192187

193188
# All runtime paths associated with the file's Mach-Os.
194189
# @return [Array<String>] an array of all runtime paths
@@ -265,7 +260,7 @@ def write(filename)
265260
# @note Overwrites all data in the file!
266261
def write!
267262
if filename.nil?
268-
raise MachOError.new("cannot write to a default file when initialized from a binary string")
263+
raise MachOError, "cannot write to a default file when initialized from a binary string"
269264
else
270265
File.open(@filename, "wb") { |f| f.write(@raw_data) }
271266
end
@@ -280,14 +275,14 @@ def write!
280275
# @raise [MachO::MachOBinaryError] if the magic is for a non-fat Mach-O file
281276
# @raise [MachO::JavaClassFileError] if the file is a Java classfile
282277
# @api private
283-
def get_fat_header
278+
def populate_fat_header
284279
# the smallest fat Mach-O header is 8 bytes
285-
raise TruncatedFileError.new if @raw_data.size < 8
280+
raise TruncatedFileError if @raw_data.size < 8
286281

287282
fh = FatHeader.new_from_bin(:big, @raw_data[0, FatHeader.bytesize])
288283

289-
raise MagicError.new(fh.magic) unless Utils.magic?(fh.magic)
290-
raise MachOBinaryError.new unless Utils.fat_magic?(fh.magic)
284+
raise MagicError, fh.magic unless Utils.magic?(fh.magic)
285+
raise MachOBinaryError unless Utils.fat_magic?(fh.magic)
291286

292287
# Rationale: Java classfiles have the same magic as big-endian fat
293288
# Mach-Os. Classfiles encode their version at the same offset as
@@ -296,15 +291,15 @@ def get_fat_header
296291
# technically possible for a fat Mach-O to have over 30 architectures,
297292
# but this is extremely unlikely and in practice distinguishes the two
298293
# formats.
299-
raise JavaClassFileError.new if fh.nfat_arch > 30
294+
raise JavaClassFileError if fh.nfat_arch > 30
300295

301296
fh
302297
end
303298

304299
# Obtain an array of fat architectures from raw file data.
305300
# @return [Array<MachO::FatArch>] an array of fat architectures
306301
# @api private
307-
def get_fat_archs
302+
def populate_fat_archs
308303
archs = []
309304

310305
fa_off = FatHeader.bytesize
@@ -319,7 +314,7 @@ def get_fat_archs
319314
# Obtain an array of Mach-O blobs from raw file data.
320315
# @return [Array<MachO::MachOFile>] an array of Mach-Os
321316
# @api private
322-
def get_machos
317+
def populate_machos
323318
machos = []
324319

325320
fat_archs.each do |arch|

lib/macho/headers.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ module MachO
3232
MH_MAGIC => "MH_MAGIC",
3333
MH_CIGAM => "MH_CIGAM",
3434
MH_MAGIC_64 => "MH_MAGIC_64",
35-
MH_CIGAM_64 => "MH_CIGAM_64"
36-
}
35+
MH_CIGAM_64 => "MH_CIGAM_64",
36+
}.freeze
3737

3838
# mask for CPUs with 64-bit architectures (when running a 64-bit ABI?)
3939
# @api private
@@ -85,7 +85,7 @@ module MachO
8585
CPU_TYPE_ARM64 => :arm64,
8686
CPU_TYPE_POWERPC => :ppc,
8787
CPU_TYPE_POWERPC64 => :ppc64,
88-
}
88+
}.freeze
8989

9090
# mask for CPU subtype capabilities
9191
# @api private
@@ -114,7 +114,7 @@ module MachO
114114

115115
# @see CPU_SUBTYPE_586
116116
# @api private
117-
CPU_SUBTYPE_PENT = CPU_SUBTYPE_586
117+
CPU_SUBTYPE_PENT = CPU_SUBTYPE_586
118118

119119
# the Pentium Pro (P6) sub-type for `CPU_TYPE_I386`
120120
# @api private
@@ -412,7 +412,7 @@ module MachO
412412
MH_DYLIB_STUB => :dylib_stub,
413413
MH_DSYM => :dsym,
414414
MH_KEXT_BUNDLE => :kext_bundle,
415-
}
415+
}.freeze
416416

417417
# association of mach header flag symbols to values
418418
# @api private
@@ -442,8 +442,8 @@ module MachO
442442
:MH_DEAD_STRIPPABLE_DYLIB => 0x400000,
443443
:MH_HAS_TLV_DESCRIPTORS => 0x800000,
444444
:MH_NO_HEAP_EXECUTION => 0x1000000,
445-
:MH_APP_EXTENSION_SAFE => 0x02000000
446-
}
445+
:MH_APP_EXTENSION_SAFE => 0x02000000,
446+
}.freeze
447447

448448
# Fat binary header structure
449449
# @see MachO::FatArch
@@ -457,7 +457,7 @@ class FatHeader < MachOStructure
457457
# always big-endian
458458
# @see MachOStructure::FORMAT
459459
# @api private
460-
FORMAT = "N2"
460+
FORMAT = "N2".freeze
461461

462462
# @see MachOStructure::SIZEOF
463463
# @api private
@@ -492,7 +492,7 @@ class FatArch < MachOStructure
492492
# always big-endian
493493
# @see MachOStructure::FORMAT
494494
# @api private
495-
FORMAT = "N5"
495+
FORMAT = "N5".freeze
496496

497497
# @see MachOStructure::SIZEOF
498498
# @api private
@@ -533,15 +533,15 @@ class MachHeader < MachOStructure
533533

534534
# @see MachOStructure::FORMAT
535535
# @api private
536-
FORMAT = "L=7"
536+
FORMAT = "L=7".freeze
537537

538538
# @see MachOStructure::SIZEOF
539539
# @api private
540540
SIZEOF = 28
541541

542542
# @api private
543543
def initialize(magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds,
544-
flags)
544+
flags)
545545
@magic = magic
546546
@cputype = cputype
547547
# For now we're not interested in additional capability bits also to be
@@ -571,15 +571,15 @@ class MachHeader64 < MachHeader
571571

572572
# @see MachOStructure::FORMAT
573573
# @api private
574-
FORMAT = "L=8"
574+
FORMAT = "L=8".freeze
575575

576576
# @see MachOStructure::SIZEOF
577577
# @api private
578578
SIZEOF = 32
579579

580580
# @api private
581581
def initialize(magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds,
582-
flags, reserved)
582+
flags, reserved)
583583
super(magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags)
584584
@reserved = reserved
585585
end

0 commit comments

Comments
 (0)