@@ -125,20 +125,41 @@ def ia_cdm_path():
125125 return cdm_path
126126
127127
128+ def get_legacy_lib_version (path ):
129+ """
130+ Determines version of the Widevine library in binary mode using a regular expression.
131+ Returns empty string if not possible, which might indicate a problematic file/arch mismatch, so this can be used as a check.
132+ """
133+ if not path or not exists (path ):
134+ return '(Not found)'
135+ import re
136+ with open (compat_path (path ), 'rb' ) as library :
137+ match = re .search (br'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' , library .read ())
138+ if not match :
139+ return '(Undetected)'
140+ return to_unicode (match .group (0 ))
141+
142+
128143def get_lib_version (path ):
129144 """
130- Determines version of the Widevine library.
145+ Determines version of the Widevine library using the python ctypes module .
131146 Returns empty string if not possible, which might indicate a problematic file/arch mismatch, so this can be used as a check.
132147 """
133148 from ctypes import CDLL , c_char_p
149+ from _ctypes import dlclose
134150
135151 lib_version = ''
136152 try :
137153 lib = CDLL (compat_path (path ))
138154 lib .GetCdmVersion .restype = c_char_p
139155 lib_version = to_unicode (lib .GetCdmVersion ())
156+ dlclose (lib ._handle ) # pylint: disable=protected-access
140157 except (OSError , AttributeError ) as exc :
141- log (4 , 'Failed to determine lib version: ' + str (exc ))
158+ if 'wrong ELF class' in str (exc ):
159+ log (4 , 'Wrong elf class, falling back to legacy lib version detection' )
160+ lib_version = get_legacy_lib_version (path )
161+ else :
162+ log (4 , 'Failed to determine lib version: ' + str (exc ))
142163
143164 return lib_version
144165
0 commit comments