# Path: IMiscSource.py
# class FieldType(object):
#     TYPE_STR = 1
#     TYPE_INT = 2
#     TYPE_DATA = 3
#     #XML TYPE_CONTAINER searches for children in given element
#     TYPE_CONTAINER = 4
#     #XML TYPE_ARRAY searches for all nodes in given xml doc
#     TYPE_ARRAY = 5 
#     TYPE_DATE = 6
# 
# class ReadTypeXML(object):
#     READ_TEXT = 0,
#     READ_ATTR = 1
# 
# class BinaryRead(object):
#     '''
#     The BinaryRead field designates how many bytes will be consumed to read this
#     field. Eg, "how big this field is".
#         - BYTE, consume 1 byte value
#         - SHORT, consume 2 byte value
#         - INTEGER, consume 4 byte value
#         - ASCII_STRING, consume ANSI/UTF8 string until null byte is reached
#         - UTF16_STRING, consume UTF-16 string until "null byte" is reached
#         - EOF, consume until end of file
#     
#     **Note:**
#         1. Providing a string value to this field will attempt to read the
#             value of a previously parsed variable given a "field_name" attribute
#             equal to that string. Note that the given field must be an integral
#             value.
#         2. Providing a function to this field will call that function, providing
#             the dictionary of currently passed variable "field_name"s and 
#             values as argument.
#             The function must return an integer value signifying how many bytes
#             to consume.
#         3. Providing a number greater than zero specifies the exact number of
#             bytes to consume for this field.
#     '''
#     BYTE = -1
#     SHORT = -2
#     INTEGER = -3
#     ASCII_STRING = -4
#     UTF16_STRING = -5
#     EOF = -6
# 
# class BinaryClass(object):
#     '''
#     The BinaryClass object tells the parser what kind of value it should expect
#     in this field:
#         - PRIMITIVE, is for primitive types such as byte, short, int values
#         - ENUMERATION, this value is an enum. If ENUM then the "converter" value
#                             should contain the dictionary which translates
#                             integer values into strings. For example consider:
#                             C code:
#                             typedef enum VAL = { 
#                                 FIRST_ENUM=1,
#                                 SECOND_ENUM 
#                             }
#                             
#                             Enum dictionary for this field:
#                             enumdict = { 1: "FIRST_ENUM", 2: "SECOND_ENUM" }
#         - FLAG_BITWISE, similar to enum but this is for flag fields. The
#                         "converter" field must contain a similar dictionary with
#                         flag values that will be bitwise-compared to the entry
#                         to determine which flags are on or off.
#         - FLAG_NUMERICAL, similar to FLAG_BITWISE but the "converter" field
#                         contains actual numbers that are going to be compared
#                         directly to the value of the field.
#         - CONSUME, simply consume this field and don't populate it within the
#                     store.
#         - ANSI_STRING, specify that this is an ANSI string, the length can be
#                         given at the BinaryRead field.
#         - UNICODE_STRING, specify that this is a unicode string, it's length
#                         can be given at the BinaryRead field.
#         
#         The following haven't been implemented yet
#         - DECISION_VALUE, **NOT/MIGHT NOT BE IMPLEMENTED** 
#                             This value represents fields that "decide" what the
#                             next field would be. It would work a bit like the
#                             container=[] of xml and regex fields but at the
#                             moment I see no reason why this could be used. 
#                             Please prove me wrong and it shall be implemented
#                             promptly. 
#         - POINTER_ABSOLUTE, that this field is a pointer(forwarder) to another
#                             field that contains the offset to that field from
#                             the beginning of the file
#         - POINTER_RELATIVE, same as above but pointer is relative offset from
#                             current location
#     '''
#     PRIMITIVE = 1
#     POINTER_ABSOLUTE = 2
#     POINTER_RELATIVE = 3
#     ENUMERATION = 4
#     FLAG_BITWISE = 5
#     FLAG_NUMERICAL = 6
#     CONSUME = 7
#     ASCII_STRING = 8
#     UTF8 = 9
#     UTF16 = 10
#     DATE = 11
#     DECISION_VALUE = 12


import ExtractStore, SQLite3Initializer
import xml.etree.ElementTree as ET
import re,struct, itertools
import StringIO, sqlite3, tempfile, shutil,os
from IMiscSource import FieldType, ReadTypeXML, BinaryRead, BinaryClass
                    value,
                    item_name = known_field.internal_name
                ) 
            )
    return items

def parse_regex( filename, filepath, known_info, print_queue, settings=None ):
    file_handle = open( filepath, "rb")
    file_contents = file_handle.read()
    file_handle.close()
    
    subsections = []
    for descr, known_fields in known_info.knownfields.iteritems():
        print_queue.put("\t[Regex Parsing] Scanning: " + descr.label )
        items = _parse_regex_knownfields( known_fields, file_contents, 
                                          print_queue )
        subsection = ExtractStore.MiscSubSection( descr, items )
        subsections.append(subsection)
    return subsections

def parse_keyval( filename, filepath, known_info, section, print_queue, settings=None ):
    return

def parse_ascii():
    return


primitive_info = {
    BinaryRead.BYTE : {"fmt": 'b', "size": 1 },
    BinaryRead.SHORT: {"fmt": 'h', "size": 2 },

# Path: IMiscSource.py
# class FieldType(object):
#     TYPE_STR = 1
#     TYPE_INT = 2
#     TYPE_DATA = 3
#     #XML TYPE_CONTAINER searches for children in given element
#     TYPE_CONTAINER = 4
#     #XML TYPE_ARRAY searches for all nodes in given xml doc
#     TYPE_ARRAY = 5 
#     TYPE_DATE = 6
# 
# class ReadTypeXML(object):
#     READ_TEXT = 0,
#     READ_ATTR = 1
# 
# class BinaryRead(object):
#     '''
#     The BinaryRead field designates how many bytes will be consumed to read this
#     field. Eg, "how big this field is".
#         - BYTE, consume 1 byte value
#         - SHORT, consume 2 byte value
#         - INTEGER, consume 4 byte value
#         - ASCII_STRING, consume ANSI/UTF8 string until null byte is reached
#         - UTF16_STRING, consume UTF-16 string until "null byte" is reached
#         - EOF, consume until end of file
#     
#     **Note:**
#         1. Providing a string value to this field will attempt to read the
#             value of a previously parsed variable given a "field_name" attribute
#             equal to that string. Note that the given field must be an integral
#             value.
#         2. Providing a function to this field will call that function, providing
#             the dictionary of currently passed variable "field_name"s and 
#             values as argument.
#             The function must return an integer value signifying how many bytes
#             to consume.
#         3. Providing a number greater than zero specifies the exact number of
#             bytes to consume for this field.
#     '''
#     BYTE = -1
#     SHORT = -2
#     INTEGER = -3
#     ASCII_STRING = -4
#     UTF16_STRING = -5
#     EOF = -6
# 
# class BinaryClass(object):
#     '''
#     The BinaryClass object tells the parser what kind of value it should expect
#     in this field:
#         - PRIMITIVE, is for primitive types such as byte, short, int values
#         - ENUMERATION, this value is an enum. If ENUM then the "converter" value
#                             should contain the dictionary which translates
#                             integer values into strings. For example consider:
#                             C code:
#                             typedef enum VAL = { 
#                                 FIRST_ENUM=1,
#                                 SECOND_ENUM 
#                             }
#                             
#                             Enum dictionary for this field:
#                             enumdict = { 1: "FIRST_ENUM", 2: "SECOND_ENUM" }
#         - FLAG_BITWISE, similar to enum but this is for flag fields. The
#                         "converter" field must contain a similar dictionary with
#                         flag values that will be bitwise-compared to the entry
#                         to determine which flags are on or off.
#         - FLAG_NUMERICAL, similar to FLAG_BITWISE but the "converter" field
#                         contains actual numbers that are going to be compared
#                         directly to the value of the field.
#         - CONSUME, simply consume this field and don't populate it within the
#                     store.
#         - ANSI_STRING, specify that this is an ANSI string, the length can be
#                         given at the BinaryRead field.
#         - UNICODE_STRING, specify that this is a unicode string, it's length
#                         can be given at the BinaryRead field.
#         
#         The following haven't been implemented yet
#         - DECISION_VALUE, **NOT/MIGHT NOT BE IMPLEMENTED** 
#                             This value represents fields that "decide" what the
#                             next field would be. It would work a bit like the
#                             container=[] of xml and regex fields but at the
#                             moment I see no reason why this could be used. 
#                             Please prove me wrong and it shall be implemented
#                             promptly. 
#         - POINTER_ABSOLUTE, that this field is a pointer(forwarder) to another
#                             field that contains the offset to that field from
#                             the beginning of the file
#         - POINTER_RELATIVE, same as above but pointer is relative offset from
#                             current location
#     '''
#     PRIMITIVE = 1
#     POINTER_ABSOLUTE = 2
#     POINTER_RELATIVE = 3
#     ENUMERATION = 4
#     FLAG_BITWISE = 5
#     FLAG_NUMERICAL = 6
#     CONSUME = 7
#     ASCII_STRING = 8
#     UTF8 = 9
#     UTF16 = 10
#     DATE = 11
#     DECISION_VALUE = 12


# Path: ParseHandlers.py
import ExtractStore, SQLite3Initializer
import xml.etree.ElementTree as ET
import re,struct, itertools
import StringIO, sqlite3, tempfile, shutil,os
from IMiscSource import FieldType, ReadTypeXML, BinaryRead, BinaryClass

                    value,
                    item_name = known_field.internal_name
                ) 
            )
    return items

def parse_regex( filename, filepath, known_info, print_queue, settings=None ):
    file_handle = open( filepath, "rb")
    file_contents = file_handle.read()
    file_handle.close()
    
    subsections = []
    for descr, known_fields in known_info.knownfields.iteritems():
        print_queue.put("\t[Regex Parsing] Scanning: " + descr.label )
        items = _parse_regex_knownfields( known_fields, file_contents, 
                                          print_queue )
        subsection = ExtractStore.MiscSubSection( descr, items )
        subsections.append(subsection)
    return subsections

def parse_keyval( filename, filepath, known_info, section, print_queue, settings=None ):
    return

def parse_ascii():
    return


primitive_info = {
    BinaryRead.BYTE : {"fmt": 'b', "size": 1 },
    BinaryRead.SHORT: {"fmt": 'h', "size": 2 },

    BinaryRead.INTEGER: { "fmt": 'i', "size": 4 }