VERSION 5.00
Begin VB.Form frmWiz2 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Motomu"
   ClientHeight    =   4650
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   6255
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   Icon            =   "frmWiz2.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   310
   ScaleMode       =   3  'Pixel
   ScaleWidth      =   417
   StartUpPosition =   2  'CenterScreen
   Begin VB.CommandButton cmdPatch 
      Caption         =   "Unlock"
      Height          =   495
      Left            =   2280
      TabIndex        =   4
      Top             =   4080
      Width           =   1815
   End
   Begin VB.ListBox lstApps 
      Height          =   2700
      IntegralHeight  =   0   'False
      Left            =   120
      Style           =   1  'Checkbox
      TabIndex        =   3
      Top             =   1320
      Width           =   6015
   End
   Begin VB.Label Label3 
      Caption         =   "Remember that you'll have to upload a single file for each application you unlock."
      Height          =   255
      Left            =   120
      TabIndex        =   2
      Top             =   1080
      Width           =   6015
   End
   Begin VB.Label Label2 
      Caption         =   "These are the midlets installed into your phone. Choose the ones you want to unlock Java privileges."
      Height          =   615
      Left            =   120
      TabIndex        =   1
      Top             =   600
      Width           =   6015
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      Caption         =   "Step 2 - Chose midlets"
      BeginProperty Font 
         Name            =   "Tahoma"
         Size            =   14.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   6015
   End
End
Attribute VB_Name = "frmWiz2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Option Base 0
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)


Private Sub cmdPatch_Click()
If MsgBox("Proceed to unlock?" & vbCrLf & "Note that some files will be created in Motomu's directory!", vbYesNo + vbQuestion) = vbNo Then Exit Sub

Dim I As Long, unlocked As String, unlockedp4() As Byte, hash(0 To 19) As Byte, hs As String, J As Long
unlocked = StrConv(LoadResData("UNLOCKED", "PAT"), vbUnicode)
unlockedp4 = LoadResData("UNLOCKEDP4", "PAT")

For I = 0 To lstApps.ListCount - 1
    If lstApps.Selected(I) Then
        CopyMemory unlockedp4(1192), J2MEST(lstApps.ItemData(I)).nInstallationCounter, 2
        unlockedp4(1194) = lstApps.ItemData(I)
        unlockedp4(1195) = IIf(RfInternalMemory, 0, 128)
        
        
        cmdPatch.Caption = "Unlocking J2ME" & lstApps.ItemData(I) & "..."
        DoEvents
        hs = Sha1(StrConv(unlockedp4, vbUnicode))
        
        For J = 0 To 19
            hash(J) = Val("&h" & Mid$(hs, J * 2 + 1, 2))
        Next J
        
        CopyMemory J2MEST(lstApps.ItemData(I)).bPATHash(0), hash(0), 20
        
        On Error Resume Next
        Open App.Path & "\j2me" & lstApps.ItemData(I) & ".pat" For Output As 1
        If Err.Number <> 0 Then
            MsgBox "Unable to create:" & vbCrLf & """j2me" & lstApps.ItemData(I) & ".pat""" & vbCrLf & "in folder:" & vbCrLf & App.Path
        Else
            Print #1, unlocked;
            Close 1
        End If
        
        On Error GoTo 0
    End If
Next I

Dim fn As String
fn = fncGetFileNametoSave("", "", "Save J2MEST")

If Len(fn) = 0 Then Exit Sub

Open fn For Output As 1
Close 1
Open fn For Binary As 1
Put 1, , J2MEST
Close 1

frmWiz3.Show
Unload Me
End Sub

Private Sub Form_Load()
Dim I As Long

For I = 0 To 30
    If J2MEST(I).bInstalled Then
        On Error Resume Next
        
        ' Controllo correttezza del file: terminazione stringhe AppTitle e AppVendor: devono essere terminate con zero
        lstApps.AddItem "J2ME" & I & " - " & StringSZ(J2MEST(I).sAppTitle) & " (" & J2MEST(I).bInstalled & ")"
        If Err.Number <> 0 Then
            MsgBox "Error " & Err.Number & ". The file you have chosen may be damaged.", vbCritical
            cmdPatch.Enabled = False
            Exit Sub
        End If
        
        
        lstApps.ItemData(lstApps.NewIndex) = I
    End If
Next I
End Sub

Function StringSZ(sz As String) As String
StringSZ = Left$(sz, InStr(1, sz, vbNullChar) - 1)
End Function
