calculate MAC,Lisence,Checksum and generate muti-file

/*************************************************************************
 *    calculate MAC,Lisence,Checksum and generate muti-file
 * 声明:
 *  1. 以升序的方式批量生成MAC地址方法;
 *  2. 以升序、降序的方式批量生成数字形式的Lisence方法;
 *  3. 以累加的方式计算MAC地址、Lisence的Checksum;
 *  4. 不提供源数据处理文件,仅仅是处理数据代码;
 *  5. 本软件主要是为后续软件开发提供更好的解决数据处理的方式。
 *  6. 本人并没有对代码进行优化,主要是考虑到保留编写代码过程中的一些细节。
 *
 *                                   2015-8-24 晴 深圳 南山平山村 曾剑锋
 ************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define debug 

#ifdef debug
    #define Debug(...) fprintf(stdout,"debug: " __VA_ARGS__);
#else 
    #define Debug(...)
#endif

int  htoi( char s[] );
int  createDir( const char *sPathName );
int  changeLisenceByte( char * lisenceByte, int number );
void calculateNextMacAddress( int mac[], int i );
void calculateNextLisence( char lisence[], int i );

int main ( int argc, char ** argv ) {

    int  i               = 0;
    int  ret             = 0;
    int  count           = 0;
    int  mac[6]          = {0};
    char lisence[18]     = {0};
    FILE *mtd5OutFile    = NULL;
    FILE *mtd6OutFile    = NULL;
    char readBuffer[512] = {0};

    if ( argc != 4 ) {                                    // check arguments number
        printf ( " USAGE:\n" );
        printf ( "     macLis <mac> <lisence> <count>\n" );
        printf ( "     example:\n" );
        printf ( "         macLis 11:22:33:44:55:66 20150609622300214 10   \n" );
        return -1;
    }

    printf ( "mac : %s\n", argv[1] );
    printf ( "lisence : %s\n", argv[2] );
    printf ( "count : %s\n", argv[3] );

    for ( i = 0; i < 6; i++ ) {
        argv[1][ ( i + 1 ) * 3 - 1] = '\0';                // change ':' to '\0' 
        mac[i] = htoi ( argv[1] + ( i * 3 ) );
    }
    Debug ( "mac : %2x:%2x:%2x:%2x:%2x:%2x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] );

    memcpy ( lisence, argv[2], sizeof(lisence) - 1 );
    Debug ( "lisence : %s\n", lisence );

    count = atoi ( argv[3] );                            // how many diretory to generate
    Debug ( "count : %d\n", count);

    FILE *mtd5SourceFile = fopen ( "temp5", "rb" );        // open input file with binary
    FILE *mtd6SourceFile = fopen ( "temp6", "rb" );

    char dirPath[10]              = {0};
    char outMtd5FilePath[20]      = {0};
    char outMtd6FilePath[20]      = {0};
    char macString[12]            = {0};
    long sum                      = 0;

    for ( i = 0; i < count ; i++ ) {

        sprintf ( dirPath, "%03d", i );                    // generator diretory
        sprintf ( outMtd5FilePath, "%03d/temp5", i );   // generator temp5 file
        sprintf ( outMtd6FilePath, "%03d/temp6", i );   // generator temp6 file

        createDir( dirPath );
        mtd5OutFile = fopen ( outMtd5FilePath, "w+b" );    // open output file with binary
        mtd6OutFile = fopen ( outMtd6FilePath, "w+b" );

        rewind ( mtd5SourceFile );                        // go head
        rewind ( mtd6SourceFile );

        // copy file content
        while ( ret = fread ( readBuffer, 1, sizeof(readBuffer), mtd5SourceFile ) ) 
            fwrite ( readBuffer, 1, sizeof(readBuffer), mtd5OutFile );

        // copy file content
        while ( ret = fread ( readBuffer, 1, sizeof(readBuffer), mtd6SourceFile ) ) 
            fwrite ( readBuffer, 1, sizeof(readBuffer), mtd6OutFile );

        // generate mac string and write to file
        sprintf ( macString, "%c%c%c%c%c%c", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] );
        fseek(mtd5OutFile, (0x670) + 9, SEEK_SET);
        fwrite ( macString, 1, 6, mtd5OutFile );
        fseek(mtd6OutFile, (0x30) + 5, SEEK_SET);
        fwrite ( macString, 1, 6, mtd6OutFile );

        // write Lisence number to file
        fseek(mtd5OutFile, (0x680) + 5, SEEK_SET);
        fwrite ( lisence, 1, 17, mtd5OutFile );
        fseek(mtd6OutFile, (0x40) + 1, SEEK_SET);
        fwrite ( lisence, 1, 17, mtd6OutFile );

        // clear the data has used 
        bzero ( dirPath, sizeof(dirPath) );
        bzero ( outMtd5FilePath, sizeof(outMtd5FilePath) );
        bzero ( outMtd6FilePath, sizeof(outMtd5FilePath) );

        // change MAC address with increase number 1
        calculateNextMacAddress( mac, 1 );
        // change lisence with decrease number 1
        calculateNextLisence( lisence, -1 );


            /**
             * go to calculate checksum
             */

            // read check data from file 
            bzero ( readBuffer, sizeof(readBuffer) );
            fseek ( mtd5OutFile, (0x640) + 0x0d, SEEK_SET );
            fread ( readBuffer, 1, 9*16, mtd5OutFile);

            // add all data as checksum, be carefull to use unsigned char to keep data was byte
            int j = 0;
            sum = 0;
            for ( j = 0; j < ( 9 * 16 ); j++ ) 
                sum += ( unsigned char ) readBuffer[j];   // must type change to keep is positive
            char checkSum[20] = {0};
            sprintf ( checkSum, "%04x", sum );
            Debug ( " temp5 checkSum: %s\n", checkSum );

            // write checksum to file
            fseek ( mtd5OutFile, (0x640) + 0x08, SEEK_SET );
            fwrite ( &sum, 1, 4, mtd5OutFile);
            
            bzero ( readBuffer, sizeof(readBuffer) );
            bzero ( checkSum, sizeof(checkSum) );
        
            // work way as above
            fseek ( mtd6OutFile, (0x000) + 0x09, SEEK_SET );
            fread ( readBuffer, 1, 9*16, mtd6OutFile);

            sum = 0;
            for ( j = 0; j < ( 9 * 16 ); j++ ) 
                sum += ( unsigned char ) readBuffer[j];   // must type change to keep is positive
            sprintf ( checkSum, "%04x", sum );
            Debug ( " temp6 checkSum: %s\n", checkSum );

            fseek ( mtd6OutFile, (0x000) + 0x04, SEEK_SET );
            fwrite ( &sum, 1, 4, mtd6OutFile);
            
            bzero ( readBuffer, sizeof(readBuffer) );

        // flush to file and close 
        fflush ( mtd5OutFile );        
        fclose ( mtd5OutFile );

        fflush ( mtd6OutFile );
        fclose ( mtd6OutFile );
    }

    fclose ( mtd5SourceFile );
    fclose ( mtd6SourceFile );

    return 0;
}

int htoi(char s[]) 
{ 
    int i; 
    int n = 0; 

    // skip "0x" or "0X"
    if (s[0] == '0' && (s[1]=='x' || s[1]=='X')) { 
        i = 2; 
    } else { 
        i = 0; 
    } 

    for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) {   

        if (tolower(s[i]) > '9') { 
            n = 16 * n + (10 + tolower(s[i]) - 'a'); 
        } else { 
            n = 16 * n + (tolower(s[i]) - '0'); 
        } 

    } 

    return n; 
} 

int createDir(const char *sPathName)  
{  
    char  DirName[256];  
    int   i;
    int   len = strlen ( DirName );  

    strcpy ( DirName,   sPathName );  

    if ( DirName[ len-1 ] != '/' )  
        strcat ( DirName,   "/" );  
      
    len = strlen ( DirName );  
      
    for ( i = 1; i < len; i++ ) {  
        if ( DirName[i] == '/' ) {  
            DirName[i] = 0;  
            if( access ( DirName, NULL ) != 0 ) {  
                if(mkdir(DirName, 0755)==-1) {   
                     perror("mkdir  error");   
                    return -1;   
                }  
            }  
            DirName[i] = '/';  
        }  
    }  
      
    return   0;  
}   

/**
 * i: this can only be positive
 */
void calculateNextMacAddress( int mac[], int i ) {

    int macByte5 = ( i + mac[5] ) % 256;
    int macByte4 = ( ( i + mac[5] ) / 256  + mac[4] ) % 256;
    int macByte3 = ( ( ( ( i + mac[5] ) / 256 ) + mac[4] ) / 256  + mac[3] ) % 256;
    int macByte2 = ( ( ( ( ( i + mac[5] ) / 256 ) + mac[4] ) / 256  + mac[3] ) / 256  + mac[2] ) % 256;
    int macByte1 = ( ( ( ( ( ( i + mac[5] ) / 256 ) + mac[4] ) / 256  + mac[3] ) / 256  + mac[2] ) / 256 + mac[1] ) % 256;
    int macByte0 = ( ( ( ( ( ( ( i + mac[5] ) / 256 ) + mac[4] ) / 256  + mac[3] ) / 256  + mac[2] ) / 256 + mac[1] ) / 256 + mac[0] ) % 256;

    mac[0] = macByte0;
    mac[1] = macByte1;
    mac[2] = macByte2;
    mac[3] = macByte3;
    mac[4] = macByte4;
    mac[5] = macByte5;

    Debug ( "calculate next Mac : %02x:%02x:%02x:%02x:%02x:%02x\n", macByte0, macByte1, macByte2, macByte3, macByte4, macByte5 );
}

void calculateNextLisence( char lisence[], int i ) {
    int tmp = 0;
    tmp = changeLisenceByte( &lisence[16], i );
    tmp = changeLisenceByte( &lisence[15], tmp );
    tmp = changeLisenceByte( &lisence[14], tmp );
    tmp = changeLisenceByte( &lisence[13], tmp );
    tmp = changeLisenceByte( &lisence[12], tmp );
    tmp = changeLisenceByte( &lisence[11], tmp );
    tmp = changeLisenceByte( &lisence[10], tmp );
    tmp = changeLisenceByte( &lisence[9], tmp );
    tmp = changeLisenceByte( &lisence[8], tmp );
    tmp = changeLisenceByte( &lisence[7], tmp );
    tmp = changeLisenceByte( &lisence[6], tmp );
    tmp = changeLisenceByte( &lisence[5], tmp );
    tmp = changeLisenceByte( &lisence[4], tmp );
    tmp = changeLisenceByte( &lisence[3], tmp );
    tmp = changeLisenceByte( &lisence[2], tmp );
    tmp = changeLisenceByte( &lisence[1], tmp );
    tmp = changeLisenceByte( &lisence[0], tmp );
    
    Debug ( "calculate next lisence : %s\n", lisence );
}

/**
 * number: this can be positive or negative
 */
int changeLisenceByte( char * lisenceByte, int number ) {
    int tmp = 0;
    if ( ( lisenceByte[0] - 48 ) + number < 0 ) {
        tmp = ( ( lisenceByte[0] - 48 ) + number ) / 10 - 1;
        lisenceByte[0] = ( 10 + ( ( lisenceByte[0] - 48 ) + number ) % 10 ) + 48;
    } else {
        tmp = ( ( lisenceByte[0] - 48 ) + number ) / 10;
        lisenceByte[0] = ( ( lisenceByte[0] - 48 ) + number ) % 10 + 48;
    }

    return tmp;
}

 

posted on 2015-08-24 19:28  zengjf  阅读(477)  评论(0编辑  收藏  举报

导航