Attribute VB_Name = "modePs1000"
Option Explicit

'Public varibles
Public epsSerialNum(0 To 7) As Byte
Public epsFriendlyName As String
Public epsCapability As Long
Public epsMemSize As Long
Public epsAccessInfo As WAccessInfo
Public epsVersionInfo As WVersionInfo
Public epsSysInfo As WSysInfo
Public epsFileInfo As WFileInfo
Public epsDirInfo As WDirInfo

'Const defined by Feitian Tech.

'Public Const EPAS_MAX_LEVEL = &H8

Public Const EPAS_APP_NAME_SIZE = &H20
Public Const EPAS_FRIENDLY_NAME_SIZE = &H20

Public Const EPAS_NULL = &H0
Public Const EPAS_API_VERSION = &H200

Public Const EPAS_OPEN_NEXT = &H0
Public Const EPAS_OPEN_FIRST = &H1
Public Const EPAS_OPEN_CURRENT = &H2
Public Const EPAS_OPEN_SPECIFIC = &H3
Public Const EPAS_OPEN_MASK = &H7
Public Const EPAS_OPEN_BY_NAME = &H100
Public Const EPAS_OPEN_BY_GUID = &H200

Public Const EPAS_CONFIG_DES_KEY_SIZE = &H1
Public Const EPAS_CONFIG_RSA_MOD_SIZE = &H2

Public Const EPAS_PROP_CAPABILITIES = &H0
Public Const EPAS_PROP_MEM_SIZE = &H1
'Public Const EPAS_PROP_ROOT_ACCESSINFO = &H2
Public Const EPAS_PROP_ACCESSINFO = &H3
Public Const EPAS_PROP_APP_NAME = &H4
Public Const EPAS_PROP_APP_GUID = &H5
Public Const EPAS_PROP_VERSIONINFO = &H6
Public Const EPAS_PROP_SERNUM = &H7
Public Const EPAS_PROP_LED_ON = &H8
Public Const EPAS_PROP_LED_OFF = &H9
Public Const EPAS_PROP_CONFIGURATION = &HA
Public Const EPAS_PROP_FRIENDLY_NAME = &HB

Public Const EPAS_ROOT_DIR = &H0
Public Const EPAS_7816_ROOT_DIR = &H3F00
Public Const EPAS_AUTO_ID_BASE = &HF000
Public Const EPAS_INDEX_FILE = &HFFFF
Public Const EPAS_TOKEN_NAME_FILE = &HFFFE

Public Const EPAS_DIR_BY_ID = &H100
Public Const EPAS_DIR_BY_LONG_ID = &H200
Public Const EPAS_DIR_BY_NAME = &H300
Public Const EPAS_DIR_BY_GUID = &H400
Public Const EPAS_DIR_BY_MASK = &HF00

Public Const EPAS_FILETYPE_UNUSED = &H0
Public Const EPAS_FILETYPE_DIR = &H1
Public Const EPAS_FILETYPE_DATA = &H2
Public Const EPAS_FILETYPE_COUNTER = &H3
Public Const EPAS_FILETYPE_KEY = &H4
Public Const EPAS_FILETYPE_UNKNOWN = &HFF

Public Const EPAS_ACCESS_ANYONE = &H0
Public Const EPAS_ACCESS_USER = &H1
Public Const EPAS_ACCESS_OFFICER = &H2
Public Const EPAS_ACCESS_APP = &H3
Public Const EPAS_ACCESS_NONE = &H7

Public Const EPAS_ACCESS_READ = &H1
Public Const EPAS_ACCESS_WRITE = &H2
Public Const EPAS_ACCESS_CRYPT = &H4

Public Const EPAS_CAPS_MD5HMAC = &H1
'Public Const EPAS_CAPS_MD5XOR = &H2
'Public Const EPAS_CAPS_MD5CHAP = &H4
'Public Const EPAS_CAPS_DES = &H8
'Public Const EPAS_CAPS_APPAUTH = &H20

Public Const EPAS_DIR_FROM_MF = &H0
Public Const EPAS_DIR_FROM_CUR_DF = &H10
Public Const EPAS_DIR_TO_PARENT = &H20

Public Const EPAS_FILE_READ = &H10
Public Const EPAS_FILE_WRITE = &H20
Public Const EPAS_FILE_CRYPT = &H40

Public Const EPAS_CREATE_AUTO_ID = &H10000

Public Const EPAS_DELETE_RECURSIVE = &H10000

Public Const EPAS_VERIFY_USER_PIN = &H0
Public Const EPAS_VERIFY_SO_PIN = &H1
Public Const EPAS_VERIFY_APPKEY = &H2
Public Const EPAS_VERIFY_NO_HASH = &H1000
Public Const EPAS_VERIFY_HASH_UPIN = &H2000

Public Const EPAS_CHANGE_USER_PIN = &H0
Public Const EPAS_UNBLOCK_USER_PIN = &H1
Public Const EPAS_CHANGE_SO_PIN = &H2
Public Const EPAS_CHANGE_APP_KEY = &H3
Public Const EPAS_CHANGE_MASK = &HF
Public Const EPAS_CHANGE_NO_HASH = &H1000
Public Const EPAS_CHANGE_HASH_UPIN = &H2000

Public Const EPAS_SCOPE_MF = &H0
Public Const EPAS_SCOPE_DF = &H1

Public Const EPAS_HASH_MD5_XOR = &H0
Public Const EPAS_HASH_MD5_HMAC = &H1
Public Const EPAS_HASH_MD5_CHAP = &H2
Public Const EPAS_HASH_DES_MAC = &H10
Public Const EPAS_HASH_TYPE_MASK = &HFF

Public Const EPAS_CRYPT_DES = &H0
Public Const EPAS_CRYPT_TYPE_MASK = &HFF

Public Const EPAS_GENKEY_ENCRYPT = &H100
Public Const EPAS_GENKEY_CREATE = &H200
Public Const EPAS_DES_ENCRYPT = &H80
Public Const EPAS_DES_DECRYPT = &H40

Public Const FT_SUCCESS = &H0
Public Const FT_CANNOT_OPEN_DRIVER = &H1 '&H80100001
Public Const FT_INVALID_DRVR_VERSION = &H2 '&H80100002
Public Const FT_INVALID_COMMAND = &H3
Public Const FT_ACCESS_DENIED = &H4
Public Const FT_ALREADY_ZERO = &H5
Public Const FT_UNIT_NOT_FOUND = &H6
Public Const FT_DEVICE_REMOVED = &H7
Public Const FT_COMMUNICATIONS_ERROR = &H8
Public Const FT_DIR_NOT_FOUND = &H9
Public Const FT_FILE_NOT_FOUND = &HA '&H8010000A
Public Const FT_MEM_CORRUPT = &HB
Public Const FT_INTERNAL_HW_ERROR = &HC
Public Const FT_INVALID_RESP_SIZE = &HD
Public Const FT_PIN_EXPIRED = &HE
Public Const FT_ALREADY_EXISTS = &HF
Public Const FT_NOT_ENOUGH_MEMORY = &H10
Public Const FT_INVALID_PARAMETER = &H11
Public Const FT_INPUT_TOO_LONG = &H13
Public Const FT_INVALID_FILE_SELECTED = &H14
Public Const FT_DEVICE_IN_USE = &H15
Public Const FT_INVALID_API_VERSION = &H16
Public Const FT_TIME_OUT_ERROR = &H17
Public Const FT_ITEM_NOT_FOUND = &H18
Public Const FT_COMMAND_ABORTED = &H19
Public Const FT_INVALID_STATUS = &HFF

Public Sub StringToByteArray(ByteArray() As Byte, str As String, lSize As Long)
    Dim i As Long
    Dim S As String
    
    S = str
    For i = 0 To lSize - 1
        If Len(S) <> 0 Then
            ByteArray(i) = Asc(S)
            S = Right(S, Len(S) - 1)
        Else
            ByteArray(i) = 0
        End If
    Next i
End Sub

Public Sub ByteArrayToString(str As String, ByteArray() As Byte, lSize As Long)
    Dim i As Long
    
    str = ""
    For i = 0 To lSize - 1
        If ByteArray(i) <> 0 Then
            str = str & Chr(ByteArray(i))
        End If
    Next i
End Sub

Public Sub HexToString(ByteArray() As Byte, str As String, lSize As Long)
    Dim b As Byte
    Dim i As Long
    
    For i = lSize - 1 To 0 Step -1
        b = ByteArray(i) \ 16
        If b > 9 Then
            str = str & Chr(b + Asc("A") - 10)
        Else
            str = str & Chr(b + Asc("0"))
        End If
        
        b = ByteArray(i) Mod 16
        If b > 9 Then
            str = str & Chr(b + Asc("A") - 10)
        Else
            str = str & Chr(b + Asc("0"))
        End If
    Next i
End Sub

Public Sub StringToHex(ByteArray() As Byte, str As String, lSize As Long)
    Dim b As Byte
    Dim i As Long
    Dim S As String
    
    For i = 0 To lSize - 1
        ByteArray(i) = 0
    Next i
    
    If Left(str, 2) = "&H" Then
        S = UCase(Right(str, Len(str) - 2))
    Else
        S = UCase(str)
    End If
    
    For i = 0 To lSize - 1
        If Len(S) = 0 Then
            Exit For
        End If
        
        b = Asc(S)
        If b > 64 And b < 71 Then
            ByteArray(i) = b - 65 + 10
        ElseIf b > 47 And b < 58 Then
            ByteArray(i) = b - 48
        End If
        
        ByteArray(i) = ByteArray(i) * 16
        S = Right(S, Len(S) - 1)
        If Len(S) = 0 Then
            Exit For
        End If
        
        b = Asc(S)
        If b > 64 And b < 71 Then
            ByteArray(i) = b - 65 + 10 + ByteArray(i)
        ElseIf b > 47 And b < 58 Then
            ByteArray(i) = b - 48 + ByteArray(i)
        End If
        
        S = Right(S, Len(S) - 1)
        If Len(S) = 0 Then
            Exit For
        End If
    Next i
End Sub

Public Sub ByteArrayToLong(lRet As Long, ByteArray() As Byte)
    Dim S As String
    Dim M As String
    
    S = "&H"
    
    M = Hex(ByteArray(3))
    If Len(M) < 2 Then
        S = S + "0"
    End If
    S = S + M
    
    M = Hex(ByteArray(2))
    If Len(M) < 2 Then
        S = S + "0"
    End If
    S = S + M
    
    M = Hex(ByteArray(1))
    If Len(M) < 2 Then
        S = S + "0"
    End If
    S = S + M
    
    M = Hex(ByteArray(0))
    If Len(M) < 2 Then
        S = S + "0"
    End If
    S = S + M
    
    lRet = Val(S)
End Sub
Public Sub LongToByteArray(ByteArray() As Byte, l As Long)
    Dim S As String
    Dim M As String
    
    ByteArray(0) = ByteArray(1) = ByteArray(2) = ByteArray(3) = 0
    S = Hex(l)
    
    M = Right(S, 2)
    ByteArray(0) = Val("&H" & M)
    
    If Len(S) > 2 Then
        S = Left(S, Len(S) - 2)
        M = Right(S, 2)
        ByteArray(1) = Val("&H" & M)
    End If
    
    If Len(S) > 2 Then
        S = Left(S, Len(S) - 2)
        M = Right(S, 2)
        ByteArray(2) = Val("&H" & M)
    End If
    
    If Len(S) > 2 Then
        S = Left(S, Len(S) - 2)
        M = Right(S, 2)
        ByteArray(3) = Val("&H" & M)
    End If
End Sub

Public Sub VersionToString(Version As Long, Info As String)
    Dim Major As Byte
    Dim Minor As Byte
    Dim Build As Integer
    Dim S As String
    Dim M As String
    
    Major = Minor = Build = 0
    S = Hex(Version)

    M = Right(S, 4)
    Build = Val("&H" & M)

    If Len(S) > 4 Then
        S = Left(S, Len(S) - 4)
        M = Right(S, 2)
        Minor = Val("&H" & M)
    End If

    If Len(S) > 2 Then
        S = Left(S, Len(S) - 2)
        M = Right(S, 2)
        Major = Val("&H" & M)
    End If

    Info = CStr(Major) & "." & CStr(Minor) & "." & CStr(Build)
End Sub
