def getBase64(self, args, fftp = False): lftp = False if fftp else FTP args = du(args) FN = "getBase64" url = self.normUrl(args, fftp) commonError = { "type": "error", "info": "getting failed", "url": self.shortUrl(url, fftp), "funcName": FN } success={ "type":"ok", "funcName":FN, "url":self.shortUrl(url, fftp), "content":"" } chunkSize = 3*8 if self.isDir(url, fftp): commonError["info"] = "obj is dir" return commonError if lftp: try: bio = BytesIO() def add_binary(data): bio.write(data) ftpcon.retrbinary('RETR ' + url, add_binary) bio.seek(0) success["mime"]=magic_from_buffer(bio.read(), mime=True) bio.seek(0) tmp = bio.read(chunkSize) while tmp: success["content"]+=b64encode(tmp).decode("utf-8") tmp = bio.read(chunkSize) except: return commonError else: try: with open(url,"rb") as f: f.seek(0) tmp = f.read(chunkSize) while tmp: success["content"]+=b64encode(tmp).decode("utf-8") tmp = f.read(chunkSize) f.seek(0) success["mime"]=magic_from_buffer(f.read(), mime=True) except: return commonError return success
def compile_input_file(input_file: str) -> Program: path = Path(input_file) if path.is_dir(): # A directory must be a project containing VBA source files return Compiler.compile_project(input_file) with open(input_file, "rb") as f: content = f.read() # Try to deserialize an already compiled program try: serializer = Serializer() obj = serializer.deserialize(content) if type(obj) == Program: return obj except SerializationError: pass # Compile an Office document or VBA source files units = [] if magic_from_buffer(content, mime=True) == "text/plain": if not input_file.endswith(".vbs"): input_file += ".vbs" units.append(Unit.from_content(content.decode("utf-8"), input_file)) else: vba_parser = VBA_Parser(input_file, data=content) for _, _, vba_filename, vba_code in vba_parser.extract_all_macros(): units.append(Unit.from_content(vba_code, vba_filename)) return Compiler.compile_units(units)
def get_file_type(file, filename): try: file_type = magic_from_buffer(file.read(1024), mime=True) file.seek(0) return maybe_decode(file_type) except NameError: return mimetypes.guess_type(filename)[0]
def calculate_mimetype(file_buffer, file_name): read_file_buffer = file_buffer.read() calculated_mimetype = magic_from_buffer(read_file_buffer, mime=True) if file_name: if file_name.endswith(".js") or file_name.endswith(".jse"): calculated_mimetype = "application/javascript" elif file_name.endswith(".vbs") or file_name.endswith(".vbe"): calculated_mimetype = "application/x-vbscript" elif file_name.endswith(".iqy"): calculated_mimetype = "text/x-ms-iqy" return calculated_mimetype
def filetype_hilite(data): hilite = "" # no hilite filetype = magic_from_buffer(data) if filetype.startswith("ASCII text"): pytraceback_re = re.compile(r'^Traceback \(most recent call last\):$', re.MULTILINE) if pytraceback_re.search(data[:2048]): filetype = "pytb" html = urllib.urlopen('http://33ad.org/pb').read() soup = BeautifulSoup(html) hilite_values = [f['value'] for f in soup.findAll('option') if f['value']] for h in hilite_values: if h in filetype.split(): hilite = h break return hilite
def filetype_hilite(data): hilite = "" # no hilite filetype = magic_from_buffer(data) if filetype.startswith("ASCII text"): pytraceback_re = re.compile( r'^Traceback \(most recent call last\):$', re.MULTILINE) if pytraceback_re.search(data[:2048]): filetype = "pytb" html = urllib.urlopen('http://33ad.org/pb').read() soup = BeautifulSoup(html) hilite_values = [ f['value'] for f in soup.findAll('option') if f['value'] ] for h in hilite_values: if h in filetype.split(): hilite = h break return hilite
def calculate_mimetype(file_pointer, file_name) -> str: mimetype = None if file_name: if file_name.endswith(".js") or file_name.endswith(".jse"): mimetype = "application/javascript" elif file_name.endswith(".vbs") or file_name.endswith(".vbe"): mimetype = "application/x-vbscript" elif file_name.endswith(".iqy"): mimetype = "text/x-ms-iqy" elif file_name.endswith(".apk"): mimetype = "application/vnd.android.package-archive" elif file_name.endswith(".dex"): mimetype = "application/x-dex" if not mimetype: buffer = file_pointer.read() mimetype = magic_from_buffer(buffer, mime=True) return mimetype