Jump to content

Please, Can anyone translate this C small function to autoit


adelbak
 Share

Recommended Posts

Please, Can anyone translate this C small function to autoit

MTK IMEI Algorithm:

int calc_imei(char inp_imei[15], char out_imei[12])
{
    char out_mask[12] = {0xAB, 0xA0, 0x6F, 0x2F, 0x1F, 0x1E, 0x9A, 0x45, 0x0, 0x0, 0x0, 0x0};
    int i=0, j=0;

    for (i=0, j=0; i < 15; i++, j++)
    {

        if (inp_imei[i] < '0' || inp_imei[i] > '9')
        {
            return 1;
        }
        out_imei[j] = (inp_imei[i] - '0');

        if (i >= 14)
            break;

        if (inp_imei[i+1] < '0' || inp_imei[i+1] > '9')
        {
             return 1;
        }
        out_imei[j] += ((inp_imei[i+1] - '0') << 4);

        out_imei[j] = out_imei[j] ^ out_mask[j];
        i++;
    }

    out_imei[j] = out_imei[j] ^ out_mask[j];

    out_imei[8] = 0x57; //Может быть и 0x0, ни на что не влияет
    out_imei[9] = 0xDB; //Может быть и 0x0, ни на что не влияет

    out_imei[10] = out_imei[11] = 0;

    for (i = 0; i < 10; i++)
    {
        if (i & 0x1)
        {
            out_imei[11] += out_imei[i];
        }
        else
        {
            out_imei[10] += out_imei[i];
        }
    }

    return 0;
}

you can test  EXE  (Down it from attach)

THANKS

 

Edited by Jos
Removed attachment due to too many (false?) virus warnings
Link to comment
Share on other sites

Im coding MTK imei repair tool, and i need this algo to generate imei file and inject it NVRAM file

This algo convert IMEI to HEX

Examble from original tool:

imei.exe 531302165213560 501939357823635

FBkRpTY.png

This generate New IMEI in file (MP0B_001_NEW) with this content:

OcD9rQu.png

 

‍‘ON:/ےEWغ}.®1ü|ک,¬@WغEô

IN hex:

9E 91 4F 4E 3A 2F FF 45 57 DB 7D 2E AE 31 FC 7C 98 2C AC 40 57 DB 45 F4

Link to comment
Share on other sites

  • Developers
2 hours ago, Ascer said:

Please do not help this Seeker, this post will blocked in next few hours.

I don't see any report for this thread yet with a reason why this is against our forum rules ...  

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Ok. I’ll look at it when I get in office but he better post code here. Not Gimme deh Codez

im about learning. If they’re not coming here to learn and they’re wasting their time and mine

He’s already got most of the difficult stuff done just need to create the arrays and fill them with the shit that he needs

I noticed that he’s not passing two arrays in the main function and he needs two of them

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

Hello adelbak,

I need to ask you a question: Are you intending to increment twice the "i" variable in the for loop ?

the first for statement initialize i to 0 and j to 0 and an the end of the for loop, I can see is i++, which means, you are incrementing "i" by 1 and also, the for loop when it reaches the close brace "}" will also check for the condition if "i" still less than 15 , if that ok, also increment "i" again. So you are incrementing "i" twice . Also, I can see that you are incrementing "j" at the same time. My question is : Are you intentionally incrementing "i" twice ? if yes, then last "i++" should be removed and then the for loop at the beginning should be like this if I understood your code correctly:

    int i=0, j=0;
    //for (i=0, j=0; i < 15; i++, j++)
    for (; i < 15;)
    {

        if (inp_imei[i] < '0' || inp_imei[i] > '9')
        {
            return 1;
        }
        out_imei[j] = (inp_imei[i] - '0');

        if (i >= 14)
            break;

        if (inp_imei[i+1] < '0' || inp_imei[i+1] > '9')
        {
             return 1;
        }
        out_imei[j] += ((inp_imei[i+1] - '0') << 4);

        out_imei[j] = out_imei[j] ^ out_mask[j];
        //i++;
        i+=2;
        j++;
    }

is that what you intended ? to increment "i" twice ? and "j" will be always incremented to 1 ?

Link to comment
Share on other sites

10 hours ago, Jowy said:

Hello adelbak,

I need to ask you a question: Are you intending to increment twice the "i" variable in the for loop ?

the first for statement initialize i to 0 and j to 0 and an the end of the for loop, I can see is i++, which means, you are incrementing "i" by 1 and also, the for loop when it reaches the close brace "}" will also check for the condition if "i" still less than 15 , if that ok, also increment "i" again. So you are incrementing "i" twice . Also, I can see that you are incrementing "j" at the same time. My question is : Are you intentionally incrementing "i" twice ? if yes, then last "i++" should be removed and then the for loop at the beginning should be like this if I understood your code correctly:


    int i=0, j=0;
    //for (i=0, j=0; i < 15; i++, j++)
    for (; i < 15;)
    {

        if (inp_imei[i] < '0' || inp_imei[i] > '9')
        {
            return 1;
        }
        out_imei[j] = (inp_imei[i] - '0');

        if (i >= 14)
            break;

        if (inp_imei[i+1] < '0' || inp_imei[i+1] > '9')
        {
             return 1;
        }
        out_imei[j] += ((inp_imei[i+1] - '0') << 4);

        out_imei[j] = out_imei[j] ^ out_mask[j];
        //i++;
        i+=2;
        j++;
    }

is that what you intended ? to increment "i" twice ? and "j" will be always incremented to 1 ?

Thanx Jowy

 

1-Are you intending to increment twice the "i" variable in the for loop ?  YES

For help, This is the full source:

 

#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

//#include "calcimei.h"

#define OUT_FILE "MP0B_001_NEW"

int main(int argc, char *argv[])
{
    char *str = "357369035621901";


    FILE* output;
    char inp_imei[15];
    char out_imei[12] = {0};
    int i, ret;
    char *mode;

    if (argc < 2 || strlen(argv[1])!=15 || (argc==3 && strlen(argv[2])!=15))
    {
        fprintf(stdout, "Use: %s <IMEI_1 15 digits> [IMEI_2 15 digits]\n", argv[0]);
        return 1;
    }
    for (i=1; i < argc; i++)
    {
        memset(inp_imei, 0, sizeof(inp_imei));
        memset(out_imei, 0, sizeof(out_imei));

        strncpy(inp_imei, argv[i], sizeof(inp_imei));

        fprintf(stdout, "New IMEI %d (15 digits):%s\n", i, inp_imei);

        ret = calc_imei(&inp_imei, &out_imei);
        if ( ret )
        {
            fprintf(stderr, "Invalid IMEI %d format!\n", i);
            return 1;
        }

        mode="w";
        if (i==2)
        {
            mode="a";
        }
        output = fopen(OUT_FILE, mode);
        fwrite(out_imei, 1, sizeof(out_imei), output);
        fclose(output);

    }


    fprintf(stdout, "New IMEI in file = %s\n", OUT_FILE);

    return 0;
}
int calc_imei(char inp_imei[15], char out_imei[12])
{
    char out_mask[12] = {0xAB, 0xA0, 0x6F, 0x2F, 0x1F, 0x1E, 0x9A, 0x45, 0x0, 0x0, 0x0, 0x0};
    int i=0, j=0;

    for (i=0, j=0; i < 15; i++, j++)
    {

        if (inp_imei[i] < '0' || inp_imei[i] > '9')
        {
            return 1;
        }
        out_imei[j] = (inp_imei[i] - '0');

        if (i >= 14)
            break;

        if (inp_imei[i+1] < '0' || inp_imei[i+1] > '9')
        {
             return 1;
        }
        out_imei[j] += ((inp_imei[i+1] - '0') << 4);

        out_imei[j] = out_imei[j] ^ out_mask[j];
        i++;
    }

    out_imei[j] = out_imei[j] ^ out_mask[j];

    out_imei[8] = 0x57; //Может быть и 0x0, ни на что не влияет
    out_imei[9] = 0xDB; //Может быть и 0x0, ни на что не влияет

    out_imei[10] = out_imei[11] = 0;

    for (i = 0; i < 10; i++)
    {
        if (i & 0x1)
        {
            out_imei[11] += out_imei[i];
        }
        else
        {
            out_imei[10] += out_imei[i];
        }
    }

    return 0;
}


 

Edited by adelbak
Link to comment
Share on other sites

14 hours ago, adelbak said:

Thanx Jowy

 

1-Are you intending to increment twice the "i" variable in the for loop ?  YES

For help, This is the full source:

 

#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

//#include "calcimei.h"

#define OUT_FILE "MP0B_001_NEW"

int main(int argc, char *argv[])
{
    char *str = "357369035621901";


    FILE* output;
    char inp_imei[15];
    char out_imei[12] = {0};
    int i, ret;
    char *mode;

    if (argc < 2 || strlen(argv[1])!=15 || (argc==3 && strlen(argv[2])!=15))
    {
        fprintf(stdout, "Use: %s <IMEI_1 15 digits> [IMEI_2 15 digits]\n", argv[0]);
        return 1;
    }
    for (i=1; i < argc; i++)
    {
        memset(inp_imei, 0, sizeof(inp_imei));
        memset(out_imei, 0, sizeof(out_imei));

        strncpy(inp_imei, argv[i], sizeof(inp_imei));

        fprintf(stdout, "New IMEI %d (15 digits):%s\n", i, inp_imei);

        ret = calc_imei(&inp_imei, &out_imei);
        if ( ret )
        {
            fprintf(stderr, "Invalid IMEI %d format!\n", i);
            return 1;
        }

        mode="w";
        if (i==2)
        {
            mode="a";
        }
        output = fopen(OUT_FILE, mode);
        fwrite(out_imei, 1, sizeof(out_imei), output);
        fclose(output);

    }


    fprintf(stdout, "New IMEI in file = %s\n", OUT_FILE);

    return 0;
}
int calc_imei(char inp_imei[15], char out_imei[12])
{
    char out_mask[12] = {0xAB, 0xA0, 0x6F, 0x2F, 0x1F, 0x1E, 0x9A, 0x45, 0x0, 0x0, 0x0, 0x0};
    int i=0, j=0;

    for (i=0, j=0; i < 15; i++, j++)
    {

        if (inp_imei[i] < '0' || inp_imei[i] > '9')
        {
            return 1;
        }
        out_imei[j] = (inp_imei[i] - '0');

        if (i >= 14)
            break;

        if (inp_imei[i+1] < '0' || inp_imei[i+1] > '9')
        {
             return 1;
        }
        out_imei[j] += ((inp_imei[i+1] - '0') << 4);

        out_imei[j] = out_imei[j] ^ out_mask[j];
        i++;
    }

    out_imei[j] = out_imei[j] ^ out_mask[j];

    out_imei[8] = 0x57; //Может быть и 0x0, ни на что не влияет
    out_imei[9] = 0xDB; //Может быть и 0x0, ни на что не влияет

    out_imei[10] = out_imei[11] = 0;

    for (i = 0; i < 10; i++)
    {
        if (i & 0x1)
        {
            out_imei[11] += out_imei[i];
        }
        else
        {
            out_imei[10] += out_imei[i];
        }
    }

    return 0;
}


 

Ok, I'll write for you the code, but hope you will do it next time. I'll publish it in couple of hours. You need to learn AutoIT basics and the documentation will help you out later on..

Link to comment
Share on other sites

Here you go :dance: @adelbak, I need to mention that you can pass as many IMEIs params as you want, it's not limited to 1 or 2 params

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; Written by Jowy
; Creation of IMEI file
; Creation Date: 28/02/2018

#Region Include
#include <FileConstants.au3>
#include <Array.au3>
#include <String.au3>
#EndRegion Include

#Region Variables
#cs
    test params: 531302165213560 501939357823635
    Global Const $sStr = "357369035621901"
#ce
Global Const $aOutMask = [0xAB, 0xA0, 0x6F, 0x2F, 0x1F, 0x1E, 0x9A, 0x45, 0x0, 0x0, 0x0, 0x0]
Global $hOutputFile = 0
Global Const $sOUT_FILE = "MP0B_001_NEW"
Global Const $iZero = Asc("0")
Global $sFileContent = ""
#EndRegion Variables

#Region Main

;~ check if no parameters where provided; i.e. no imei
If $CmdLine[0] = 0 Then
    PrintUsage()
    Exit 1
EndIf

For $i = 1 To $CmdLine[0]
    ConsoleWrite("New IMEI " & $i & " (15 digits): " & $CmdLine[$i] & @CRLF)
    Local $aReturn = ParseIMEI($CmdLine[$i])
    If Not @error Then
        WriteToFile($aReturn)
    Else
        ConsoleWrite("Invalid IMEI " & $CmdLine[$i] & " format!" & @CRLF)
        CloseFile()
        Exit 1
    EndIf
Next

ConsoleWrite("New IMEI in file = " & $sOUT_FILE & @CRLF)
CloseFile()

#EndRegion Main

#Region Functions
Func CloseFile()
    If $hOutputFile <> 0 Then
        FileClose($hOutputFile)
    EndIf
EndFunc   ;==>CloseFile


Func WriteToFile(ByRef $aArray)
    If $hOutputFile = 0 Then
        $hOutputFile = FileOpen(@ScriptDir & "\" & $sOUT_FILE, $FO_OVERWRITE + $FO_ANSI) ; I'm using overwrite, so if previous file exists, it will be overwritten.
    EndIf
    If $hOutputFile <> -1 Then
        For $xItem In $aArray
            FileWrite($hOutputFile, BinaryMid($xItem, 1, 1))
        Next
    Else
        ConsoleWrite("Unable to open file for writing !" & @CRLF)
        Exit 1
    EndIf
EndFunc   ;==>WriteToFile



Func PrintUsage()
    ConsoleWrite("Usage: " & @CRLF & @ScriptName & " <IEMI_1 15 digits> [IMEI_2 15 digits] ... [IMEI_X 15 digits]" & @CRLF)
EndFunc   ;==>PrintUsage

Func ParseIMEI($sIMEI)
    Local $aOutput
    If StringLen($sIMEI) = 15 And StringIsDigit($sIMEI) Then
        $aOutput = Calc_IMEI($sIMEI)
        Return SetError(0, 0, $aOutput)
    Else
        Return SetError(1, 1, "")
    EndIf
EndFunc   ;==>ParseIMEI

Func Calc_IMEI($sInput)
    Local $j = 0
    Local $aOutput[12] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
;~  Local $aInput = StringRegExp($sInput, "(?i)(\d)", 3)
    Local $aInput = StringToASCIIArray($sInput)
;~  _ArrayDisplay($aInput)
    For $i = 0 To UBound($aInput) - 1 Step 2 ; (15 - 1 = 14)
;~  since $sInput is always having digits from 0 to 9, there is no need to check any digit as all the string have been checked
        $aOutput[$j] = ($aInput[$i] - $iZero)
        If $i >= 14 Then
            ExitLoop
        EndIf
        $aOutput[$j] += BitShift(($aInput[$i + 1] - $iZero), -4)
        $aOutput[$j] = BitXOR($aOutput[$j], $aOutMask[$j])
        $j += 1
    Next
    $aOutput[$j] = BitXOR($aOutput[$j], $aOutMask[$j])
    $aOutput[8] = 0x57
    $aOutput[9] = 0xDB
    $aOutput[10] = 0
    $aOutput[11] = 0
    For $i = 0 To 9
        If BitAND($i, 0x1) Then
            $aOutput[11] += $aOutput[$i]
        Else
            $aOutput[10] += $aOutput[$i]
        EndIf
    Next
    Return $aOutput
EndFunc   ;==>Calc_IMEI

#EndRegion Functions

Here are my results:

 

IMEI.png

Edited by Jowy
Link to comment
Share on other sites

Well darn I guess I should have checked to see if someone else was doing it

before I started and you did a full conversion too nice jowy

$sImei = InputBox("test imei", "imei", 0)
$aRet = calc_imei($sImei)
If @error Then MsgBox(0, "error", "pos: " & @extended & " is invalid")
Local $sOut = ""
For $i = 0 To UBound($aRet) - 1
    $sOut &= " " & Hex($aRet[$i], 2)
Next
MsgBox(0, "Hex Out", $sOut)


Func calc_imei($s_Imei)
    Local Const $aInvalid[12] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    Local Const $aMask[12] = [0xAB, 0xA0, 0x6F, 0x2F, 0x1F, 0x1E, 0x9A, 0x45, 0x0, 0x0, 0x0, 0x0]
    Local $a_Imei = StringToASCIIArray($s_Imei, "")
    Local $a_Out = $aInvalid

    If Not IsArray($a_Imei) Or UBound($a_Imei) <> 15 Then Return SetError(1, 0, $aInvalid) ;wrong length

    $j = 0
    For $i = 0 To UBound($a_Imei) - 1 Step 2 ; for (i=0, j=0; i < 15; i++, j++);; i++;

        If $a_Imei[$i] < Asc('0') Or $a_Imei[$i] > Asc('9') Then Return SetError(2, $i, $aInvalid) ;if (inp_imei[i] < '0' || inp_imei[i] > '9')

        $a_Out[$j] = $a_Imei[$i] - Asc('0') ;out_imei[j] = (inp_imei[i] - '0');
        
        If $i < 14 Then ;if (i >= 14) break;
            If $a_Imei[$i + 1] < Asc('0') Or $a_Imei[$i + 1] > Asc('9') Then Return SetError(2, $i + 1, $aInvalid)
            $a_Out[$j] += BitShift($a_Imei[$i + 1] - Asc('0'), -4) ;out_imei[j] += ((inp_imei[i+1] - '0') << 4);left shift 4

            $a_Out[$j] = BitXOR($a_Out[$j], $aMask[$j]) ;out_imei[j] = out_imei[j] ^ out_mask[j];
            $j += 1
        EndIf
    Next

    $a_Out[$j] = BitXOR($a_Out[$j], $aMask[$j]) ;out_imei[j] = out_imei[j] ^ out_mask[j];

    $a_Out[8] = 0x57 ;out_imei[8] = 0x57; //????? ???? ? 0x0, ?? ?? ??? ?? ??????
    $a_Out[9] = 0xDB ;out_imei[9] = 0xDB; //????? ???? ? 0x0, ?? ?? ??? ?? ??????

    $a_Out[10] = 0 ;out_imei[10] = out_imei[11] = 0
    $a_Out[11] = 0 ;

    ;simple checksum
    For $i = 0 To 9 Step 2 ;for (i = 0; i < 10; i++)
        $a_Out[10] += $a_Out[$i] ;;out_imei[10] += out_imei[i];
        $a_Out[11] += $a_Out[$i + 1] ;if (i & 0x1); out_imei[11] += out_imei[i]
    Next

    Return $a_Out
EndFunc

 

Edited by Bilgus
Link to comment
Share on other sites

@Earthshine, I agree with you, I wrote the code so @adelbak can learn how can be done in AutoIT, but for sure next time he will do things by his own. Also, the trick is not actually by writing the function only, it was also by saving the output to a file, because showing a message box by itself and trying to write the string will not solve the issue as 4 bytes of data for every char will be written, but instead we have to have 1 byte for every char. That's why I wrote all the whole script :) 

Edited by Jowy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...