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 = ;
- int ret = ;
- int count = ;
- int mac[] = {};
- char lisence[] = {};
- FILE *mtd5OutFile = NULL;
- FILE *mtd6OutFile = NULL;
- char readBuffer[] = {};
- if ( argc != ) { // 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 -;
- }
- printf ( "mac : %s\n", argv[] );
- printf ( "lisence : %s\n", argv[] );
- printf ( "count : %s\n", argv[] );
- for ( i = ; i < ; i++ ) {
- argv[][ ( i + ) * - ] = '\0'; // change ':' to '\0'
- mac[i] = htoi ( argv[] + ( i * ) );
- }
- Debug ( "mac : %2x:%2x:%2x:%2x:%2x:%2x\n", mac[], mac[], mac[], mac[], mac[], mac[] );
- memcpy ( lisence, argv[], sizeof(lisence) - );
- Debug ( "lisence : %s\n", lisence );
- count = atoi ( argv[] ); // 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[] = {};
- char outMtd5FilePath[] = {};
- char outMtd6FilePath[] = {};
- char macString[] = {};
- long sum = ;
- for ( i = ; 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, , sizeof(readBuffer), mtd5SourceFile ) )
- fwrite ( readBuffer, , sizeof(readBuffer), mtd5OutFile );
- // copy file content
- while ( ret = fread ( readBuffer, , sizeof(readBuffer), mtd6SourceFile ) )
- fwrite ( readBuffer, , sizeof(readBuffer), mtd6OutFile );
- // generate mac string and write to file
- sprintf ( macString, "%c%c%c%c%c%c", mac[], mac[], mac[], mac[], mac[], mac[] );
- fseek(mtd5OutFile, (0x670) + , SEEK_SET);
- fwrite ( macString, , , mtd5OutFile );
- fseek(mtd6OutFile, (0x30) + , SEEK_SET);
- fwrite ( macString, , , mtd6OutFile );
- // write Lisence number to file
- fseek(mtd5OutFile, (0x680) + , SEEK_SET);
- fwrite ( lisence, , , mtd5OutFile );
- fseek(mtd6OutFile, (0x40) + , SEEK_SET);
- fwrite ( lisence, , , 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, );
- // change lisence with decrease number 1
- calculateNextLisence( lisence, - );
- /**
- * go to calculate checksum
- */
- // read check data from file
- bzero ( readBuffer, sizeof(readBuffer) );
- fseek ( mtd5OutFile, (0x640) + 0x0d, SEEK_SET );
- fread ( readBuffer, , *, mtd5OutFile);
- // add all data as checksum, be carefull to use unsigned char to keep data was byte
- int j = ;
- sum = ;
- for ( j = ; j < ( * ); j++ )
- sum += ( unsigned char ) readBuffer[j]; // must type change to keep is positive
- char checkSum[] = {};
- sprintf ( checkSum, "%04x", sum );
- Debug ( " temp5 checkSum: %s\n", checkSum );
- // write checksum to file
- fseek ( mtd5OutFile, (0x640) + 0x08, SEEK_SET );
- fwrite ( &sum, , , mtd5OutFile);
- bzero ( readBuffer, sizeof(readBuffer) );
- bzero ( checkSum, sizeof(checkSum) );
- // work way as above
- fseek ( mtd6OutFile, (0x000) + 0x09, SEEK_SET );
- fread ( readBuffer, , *, mtd6OutFile);
- sum = ;
- for ( j = ; j < ( * ); 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, , , mtd6OutFile);
- bzero ( readBuffer, sizeof(readBuffer) );
- // flush to file and close
- fflush ( mtd5OutFile );
- fclose ( mtd5OutFile );
- fflush ( mtd6OutFile );
- fclose ( mtd6OutFile );
- }
- fclose ( mtd5SourceFile );
- fclose ( mtd6SourceFile );
- return ;
- }
- int htoi(char s[])
- {
- int i;
- int n = ;
- // skip "0x" or "0X"
- if (s[] == '' && (s[]=='x' || s[]=='X')) {
- i = ;
- } else {
- i = ;
- }
- for (; (s[i] >= '' && s[i] <= '') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) {
- if (tolower(s[i]) > '') {
- n = * n + ( + tolower(s[i]) - 'a');
- } else {
- n = * n + (tolower(s[i]) - '');
- }
- }
- return n;
- }
- int createDir(const char *sPathName)
- {
- char DirName[];
- int i;
- int len = strlen ( DirName );
- strcpy ( DirName, sPathName );
- if ( DirName[ len- ] != '/' )
- strcat ( DirName, "/" );
- len = strlen ( DirName );
- for ( i = ; i < len; i++ ) {
- if ( DirName[i] == '/' ) {
- DirName[i] = ;
- if( access ( DirName, NULL ) != ) {
- if(mkdir(DirName, )==-) {
- perror("mkdir error");
- return -;
- }
- }
- DirName[i] = '/';
- }
- }
- return ;
- }
- /**
- * i: this can only be positive
- */
- void calculateNextMacAddress( int mac[], int i ) {
- int macByte5 = ( i + mac[] ) % ;
- int macByte4 = ( ( i + mac[] ) / + mac[] ) % ;
- int macByte3 = ( ( ( ( i + mac[] ) / ) + mac[] ) / + mac[] ) % ;
- int macByte2 = ( ( ( ( ( i + mac[] ) / ) + mac[] ) / + mac[] ) / + mac[] ) % ;
- int macByte1 = ( ( ( ( ( ( i + mac[] ) / ) + mac[] ) / + mac[] ) / + mac[] ) / + mac[] ) % ;
- int macByte0 = ( ( ( ( ( ( ( i + mac[] ) / ) + mac[] ) / + mac[] ) / + mac[] ) / + mac[] ) / + mac[] ) % ;
- mac[] = macByte0;
- mac[] = macByte1;
- mac[] = macByte2;
- mac[] = macByte3;
- mac[] = macByte4;
- mac[] = 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 = ;
- tmp = changeLisenceByte( &lisence[], i );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- tmp = changeLisenceByte( &lisence[], tmp );
- Debug ( "calculate next lisence : %s\n", lisence );
- }
- /**
- * number: this can be positive or negative
- */
- int changeLisenceByte( char * lisenceByte, int number ) {
- int tmp = ;
- if ( ( lisenceByte[] - ) + number < ) {
- tmp = ( ( lisenceByte[] - ) + number ) / - ;
- lisenceByte[] = ( + ( ( lisenceByte[] - ) + number ) % ) + ;
- } else {
- tmp = ( ( lisenceByte[] - ) + number ) / ;
- lisenceByte[] = ( ( lisenceByte[] - ) + number ) % + ;
- }
- return tmp;
- }
calculate MAC,Lisence,Checksum and generate muti-file的更多相关文章
- 【MySQL】InnoDB: Error: checksum mismatch in data file 报错
参考:http://www.jb51.net/article/66951.htm 用5.7版本启动原5.5实例后,再用5.5启动出现以下报错 InnoDB: Error: checksum misma ...
- Generate BKS File( Bouncy Castle KeyStore)
echo "Enter BKS output file name : \c" read filename echo "Enter BKS Password : \c&qu ...
- generate eml file
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...
- Generate input file for OVITO
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- Mac Terminal open app with a file opened
open -a /Applications/Sublime Text.app test.cpp
- Mac OS build caffe2 Error:This file was generated by an older version of protoc which is
问题所在 我们可以发现这个错误跟protobuf的版本有关,因此我们可以执行script/diagnose_protobuf.py 我们可以看到,pip install protobuf 和 brew ...
- Windows平台CUDA开发之前的准备工作
CUDA是NVIDIA的GPU开发工具,眼下在大规模并行计算领域有着广泛应用. windows平台上面的CUDA开发之前.最好去NVIDIA官网查看说明,然后下载对应的driver. ToolKits ...
- How to generate a new dictionary file of mmseg
How to generate a new dictionary file of mmseg 0.Usage about mmseg-node memtioned in github : var mm ...
- IAR EWARM Checksum Technical Note
IELFTOOL Checksum - Basic actions EW targets: ARM, RH850, RX, SH, STM8 EW component: General issues ...
随机推荐
- Spring 注解 @Resource和@Autowired
@Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spr ...
- vs2010的VCVARS32.BAT所在位置
1. C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat 2. ZC:vs08 和 vs2010 安装好后, ...
- [原][OSG][osgBullet][osgworks][bullet]编译osgBullet尝试物理引擎
相关网址: 类似文章:http://blog.csdn.net/lh1162810317/article/details/17475297 osgBullet官网:http://osgbullet.v ...
- Codeforces 862B - Mahmoud and Ehab and the bipartiteness
862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...
- Insert Delete GetRandom O(1)
2018-07-15 18:36:29 问题描述: 问题求解: private ArrayList<Integer> ls; private HashMap<Integer, Int ...
- C#复制文件
string pLocalFilePath ="";//要复制的文件路径 string pSaveFilePath ="";//指定存储的路径 if (File ...
- getpagesize.c:32: __getpagesize: Assertion `_rtld_global_ro._dl_pagesize != 0' failed
为arm 编译 mysql , 执行的时候出现了这个问题. 好像是个bug, https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=626379 重新编译 ...
- SpringBoot导入excle文件数据
本文主要描述,Springboot框架下上传excel,处理里面相关数据做逻辑分析,由于用到的是前后端分离技术,这里记录的主要是后端java部分,通过与前端接口进行对接实现功能 1.在pom.xml文 ...
- [Java学习] Java Object类
Object 类位于 java.lang 包中,是所有 Java 类的祖先,Java 中的每个类都由它扩展而来. 定义Java类时如果没有显示的指明父类,那么就默认继承了 Object 类.例如: 1 ...
- python-day45--mysql索引
一 .介绍 为何要有索引? 一些复杂的查询操作,对查询语句的优化显然是重中之重.说起加速查询,就不得不提到索引了. 什么是索引? 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结 ...