add specialize_as(), use it to speed up .smali comparison in APKs
Possible fix for #324.
NB: this does not currently take into account the possibility that there could be files that are not text files but do have the extension .smali in an APK; I've never encountered that before and I see no reason for them to be present, but they could be, theoretically.
To summarise:
- as part of
ApkContainer,diffoscopecallsapktoolwhich decompiles the classes in the.dexfiles in the APK tosmali{,_classesN}/**/*.smalifiles; - these are text files and there are often thousands of them;
- using
libmagicto identity the file type for all these files (as automatically happens via.recognizes()) takes minutes; - we know we don't need this identification step (and can safely skip it) since these files are generated by
diffoscopeitself usingapktool.
This MR:
- splits off
specialize_as()fromtry_recognize()to allow us to explicitly specialize a file in those rare cases we need to; - modifies
specialize()- to perform the
isinstance()check on allComparatorManager().classesbefore running.recognizes()on any of them (instead of just before calling.recognizes()on each class in turn); - which IMO makes more sense regardless of also enabling this optimisation (i.e. avoiding use of
libmagicvia.recognizes()), since currently a file already specialized as aBarFilewould not be returned as-is but turned into aFooFileas well ifFooFileis earlier in the list thanBarFileand.recognizes()the file;
- to perform the
- uses
specialize_as()inApkContainer.get_member()to explicitly specializesmali{,_classesN}/**/*.smaliArchiveMembers asTextFileto avoid thelibmagiccall that is for these specific cases both unnecessary and expensive; - does not affect any other files that happen to have the extension
.smalior rely on the order ofComparatorManager().classes.
Edited by FC (Fay) Stegerman