<commit_before>from __future__ import division
from __future__ import print_function

from future import standard_library

standard_library.install_aliases()
from builtins import str
from builtins import object
from androguard.core import androconf
from androguard.util import read, get_certificate_name_string

from androguard.core.bytecodes.axml import ARSCParser, AXMLPrinter, ARSCResTableConfig

import io
from zlib import crc32
import re
import binascii
import zipfile
import logging

import lxml.sax
from xml.dom.pulldom import SAX2DOM

# Used for reading Certificates
from pyasn1.codec.der.decoder import decode
from pyasn1.codec.der.encoder import encode
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes

NS_ANDROID_URI = 'http://schemas.android.com/apk/res/android'
NS_ANDROID = '{http://schemas.android.com/apk/res/android}'

log = logging.getLogger("androguard.apk")


def parse_lxml_dom(tree):
    handler = SAX2DOM()
    lxml.sax.saxify(tree, handler)
    return handler.document


class Error(Exception):
    """Base class for exceptions in this module."""
    pass


class FileNotPresent(Error):
    pass


class BrokenAPKError(Error):
    pass


######################################################## APK FORMAT ########################################################
class APK(object):
    def __init__(self,
                 filename,
                 raw=False,
                 magic_file=None,
                 skip_analysis=False,
                 testzip=False):
        """
            This class can access to all elements in an APK file

            :param filename: specify the path of the file, or raw data
            :param raw: specify if the filename is a path or raw data (optional)
            :param magic_file: specify the magic file (optional)
            :param skip_analysis: Skip the analysis, e.g. no manifest files are read. (default: False)
            :param testzip: Test the APK for integrity, e.g. if the ZIP file is broken. Throw an exception on failure (default False)

            :type filename: string
            :type raw: boolean
            :type magic_file: string
            :type skip_analysis: boolean
            :type testzip: boolean

            :Example:
              APK("myfile.apk")
              APK(read("myfile.apk"), raw=True)
        """
        self.filename = filename

        self.xml = {}
        self.axml = {}
        self.arsc = {}

        self.package = ""
        self.androidversion = {}
        self.permissions = []
        self.declared_permissions = {}
        self.valid_apk = False

        self.files = {}
        self.files_crc32 = {}

        self.magic_file = magic_file

        if raw is True:
            self.__raw = bytearray(filename)
        else:
            self.__raw = bytearray(read(filename))

        self.zip = zipfile.ZipFile(io.BytesIO(self.__raw), mode="r")

        if testzip:
            # Test the zipfile for integrity before continuing.
            # This process might be slow, as the whole file is read.
            # Therefore it is possible to enable it as a separate feature.<commit_msg>edit what refactoring missed<commit_after>from __future__ import division
from __future__ import print_function

from future import standard_library

standard_library.install_aliases()
from builtins import str
from builtins import object
from androguard.core import androconf
from androguard.util import read, get_certificate_name_string

from androguard.core.bytecodes.axml import ARSCParser, AXMLPrinter, ARSCResTableConfig

import io
from zlib import crc32
import re
import binascii
import zipfile
import logging

import lxml.sax
from xml.dom.pulldom import SAX2DOM

# Used for reading Certificates
from pyasn1.codec.der.decoder import decode
from pyasn1.codec.der.encoder import encode
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes

NS_ANDROID_URI = 'http://schemas.android.com/apk/res/android'
NS_ANDROID = '{http://schemas.android.com/apk/res/android}'

log = logging.getLogger("androguard.apk")


def parse_lxml_dom(tree):
    handler = SAX2DOM()
    lxml.sax.saxify(tree, handler)
    return handler.document


class Error(Exception):
    """Base class for exceptions in this module."""
    pass


class FileNotPresent(Error):
    pass


class BrokenAPKError(Error):
    pass


######################################################## APK FORMAT ########################################################
class APK(object):
    def __init__(self,
                 filename,
                 raw=False,
                 magic_file=None,
                 skip_analysis=False,
                 testzip=False):
        """
            This class can access to all elements in an APK file

            :param filename: specify the path of the file, or raw data
            :param raw: specify if the filename is a path or raw data (optional)
            :param magic_file: specify the magic file (optional)
            :param skip_analysis: Skip the analysis, e.g. no manifest files are read. (default: False)
            :param testzip: Test the APK for integrity, e.g. if the ZIP file is broken. Throw an exception on failure (default False)

            :type filename: string
            :type raw: boolean
            :type magic_file: string
            :type skip_analysis: boolean
            :type testzip: boolean

            :Example:
              APK("myfile.apk")
              APK(read("myfile.apk"), raw=True)
        """
        self.filename = filename

        self.xml = {}
        self.axml = {}
        self.arsc = {}

        self.package = ""
        self.androidversion = {}
        self.permissions = []
        self.declared_permissions = {}
        self.valid_apk = False

        self._files = {}
        self.files_crc32 = {}

        self.magic_file = magic_file

        if raw is True:
            self.__raw = bytearray(filename)
        else:
            self.__raw = bytearray(read(filename))

        self.zip = zipfile.ZipFile(io.BytesIO(self.__raw), mode="r")

        if testzip:
            # Test the zipfile for integrity before continuing.
            # This process might be slow, as the whole file is read.
            # Therefore it is possible to enable it as a separate feature.
{'fraction_non_alphanumeric': 0.10865504358655044, 'fraction_numerical': 0.003424657534246575, 'mean_word_length': 2.5694444444444446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6178932', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1724, 'n_words': 550}