1. /*************************************************************************
  2. * calculate MAC,Lisence,Checksum and generate muti-file
  3. * 声明:
  4. * 1. 以升序的方式批量生成MAC地址方法;
  5. * 2. 以升序、降序的方式批量生成数字形式的Lisence方法;
  6. * 3. 以累加的方式计算MAC地址、Lisence的Checksum;
  7. * 4. 不提供源数据处理文件,仅仅是处理数据代码;
  8. * 5. 本软件主要是为后续软件开发提供更好的解决数据处理的方式。
  9. * 6. 本人并没有对代码进行优化,主要是考虑到保留编写代码过程中的一些细节。
  10. *
  11. * 2015-8-24 晴 深圳 南山平山村 曾剑锋
  12. ************************************************************************/
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16.  
  17. #define debug
  18.  
  19. #ifdef debug
  20. #define Debug(...) fprintf(stdout,"debug: " __VA_ARGS__);
  21. #else
  22. #define Debug(...)
  23. #endif
  24.  
  25. int htoi( char s[] );
  26. int createDir( const char *sPathName );
  27. int changeLisenceByte( char * lisenceByte, int number );
  28. void calculateNextMacAddress( int mac[], int i );
  29. void calculateNextLisence( char lisence[], int i );
  30.  
  31. int main ( int argc, char ** argv ) {
  32.  
  33. int i = ;
  34. int ret = ;
  35. int count = ;
  36. int mac[] = {};
  37. char lisence[] = {};
  38. FILE *mtd5OutFile = NULL;
  39. FILE *mtd6OutFile = NULL;
  40. char readBuffer[] = {};
  41.  
  42. if ( argc != ) { // check arguments number
  43. printf ( " USAGE:\n" );
  44. printf ( " macLis <mac> <lisence> <count>\n" );
  45. printf ( " example:\n" );
  46. printf ( " macLis 11:22:33:44:55:66 20150609622300214 10 \n" );
  47. return -;
  48. }
  49.  
  50. printf ( "mac : %s\n", argv[] );
  51. printf ( "lisence : %s\n", argv[] );
  52. printf ( "count : %s\n", argv[] );
  53.  
  54. for ( i = ; i < ; i++ ) {
  55. argv[][ ( i + ) * - ] = '\0'; // change ':' to '\0'
  56. mac[i] = htoi ( argv[] + ( i * ) );
  57. }
  58. Debug ( "mac : %2x:%2x:%2x:%2x:%2x:%2x\n", mac[], mac[], mac[], mac[], mac[], mac[] );
  59.  
  60. memcpy ( lisence, argv[], sizeof(lisence) - );
  61. Debug ( "lisence : %s\n", lisence );
  62.  
  63. count = atoi ( argv[] ); // how many diretory to generate
  64. Debug ( "count : %d\n", count);
  65.  
  66. FILE *mtd5SourceFile = fopen ( "temp5", "rb" ); // open input file with binary
  67. FILE *mtd6SourceFile = fopen ( "temp6", "rb" );
  68.  
  69. char dirPath[] = {};
  70. char outMtd5FilePath[] = {};
  71. char outMtd6FilePath[] = {};
  72. char macString[] = {};
  73. long sum = ;
  74.  
  75. for ( i = ; i < count ; i++ ) {
  76.  
  77. sprintf ( dirPath, "%03d", i ); // generator diretory
  78. sprintf ( outMtd5FilePath, "%03d/temp5", i ); // generator temp5 file
  79. sprintf ( outMtd6FilePath, "%03d/temp6", i ); // generator temp6 file
  80.  
  81. createDir( dirPath );
  82. mtd5OutFile = fopen ( outMtd5FilePath, "w+b" ); // open output file with binary
  83. mtd6OutFile = fopen ( outMtd6FilePath, "w+b" );
  84.  
  85. rewind ( mtd5SourceFile ); // go head
  86. rewind ( mtd6SourceFile );
  87.  
  88. // copy file content
  89. while ( ret = fread ( readBuffer, , sizeof(readBuffer), mtd5SourceFile ) )
  90. fwrite ( readBuffer, , sizeof(readBuffer), mtd5OutFile );
  91.  
  92. // copy file content
  93. while ( ret = fread ( readBuffer, , sizeof(readBuffer), mtd6SourceFile ) )
  94. fwrite ( readBuffer, , sizeof(readBuffer), mtd6OutFile );
  95.  
  96. // generate mac string and write to file
  97. sprintf ( macString, "%c%c%c%c%c%c", mac[], mac[], mac[], mac[], mac[], mac[] );
  98. fseek(mtd5OutFile, (0x670) + , SEEK_SET);
  99. fwrite ( macString, , , mtd5OutFile );
  100. fseek(mtd6OutFile, (0x30) + , SEEK_SET);
  101. fwrite ( macString, , , mtd6OutFile );
  102.  
  103. // write Lisence number to file
  104. fseek(mtd5OutFile, (0x680) + , SEEK_SET);
  105. fwrite ( lisence, , , mtd5OutFile );
  106. fseek(mtd6OutFile, (0x40) + , SEEK_SET);
  107. fwrite ( lisence, , , mtd6OutFile );
  108.  
  109. // clear the data has used
  110. bzero ( dirPath, sizeof(dirPath) );
  111. bzero ( outMtd5FilePath, sizeof(outMtd5FilePath) );
  112. bzero ( outMtd6FilePath, sizeof(outMtd5FilePath) );
  113.  
  114. // change MAC address with increase number 1
  115. calculateNextMacAddress( mac, );
  116. // change lisence with decrease number 1
  117. calculateNextLisence( lisence, - );
  118.  
  119. /**
  120. * go to calculate checksum
  121. */
  122.  
  123. // read check data from file
  124. bzero ( readBuffer, sizeof(readBuffer) );
  125. fseek ( mtd5OutFile, (0x640) + 0x0d, SEEK_SET );
  126. fread ( readBuffer, , *, mtd5OutFile);
  127.  
  128. // add all data as checksum, be carefull to use unsigned char to keep data was byte
  129. int j = ;
  130. sum = ;
  131. for ( j = ; j < ( * ); j++ )
  132. sum += ( unsigned char ) readBuffer[j]; // must type change to keep is positive
  133. char checkSum[] = {};
  134. sprintf ( checkSum, "%04x", sum );
  135. Debug ( " temp5 checkSum: %s\n", checkSum );
  136.  
  137. // write checksum to file
  138. fseek ( mtd5OutFile, (0x640) + 0x08, SEEK_SET );
  139. fwrite ( &sum, , , mtd5OutFile);
  140.  
  141. bzero ( readBuffer, sizeof(readBuffer) );
  142. bzero ( checkSum, sizeof(checkSum) );
  143.  
  144. // work way as above
  145. fseek ( mtd6OutFile, (0x000) + 0x09, SEEK_SET );
  146. fread ( readBuffer, , *, mtd6OutFile);
  147.  
  148. sum = ;
  149. for ( j = ; j < ( * ); j++ )
  150. sum += ( unsigned char ) readBuffer[j]; // must type change to keep is positive
  151. sprintf ( checkSum, "%04x", sum );
  152. Debug ( " temp6 checkSum: %s\n", checkSum );
  153.  
  154. fseek ( mtd6OutFile, (0x000) + 0x04, SEEK_SET );
  155. fwrite ( &sum, , , mtd6OutFile);
  156.  
  157. bzero ( readBuffer, sizeof(readBuffer) );
  158.  
  159. // flush to file and close
  160. fflush ( mtd5OutFile );
  161. fclose ( mtd5OutFile );
  162.  
  163. fflush ( mtd6OutFile );
  164. fclose ( mtd6OutFile );
  165. }
  166.  
  167. fclose ( mtd5SourceFile );
  168. fclose ( mtd6SourceFile );
  169.  
  170. return ;
  171. }
  172.  
  173. int htoi(char s[])
  174. {
  175. int i;
  176. int n = ;
  177.  
  178. // skip "0x" or "0X"
  179. if (s[] == '' && (s[]=='x' || s[]=='X')) {
  180. i = ;
  181. } else {
  182. i = ;
  183. }
  184.  
  185. for (; (s[i] >= '' && s[i] <= '') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) {
  186.  
  187. if (tolower(s[i]) > '') {
  188. n = * n + ( + tolower(s[i]) - 'a');
  189. } else {
  190. n = * n + (tolower(s[i]) - '');
  191. }
  192.  
  193. }
  194.  
  195. return n;
  196. }
  197.  
  198. int createDir(const char *sPathName)
  199. {
  200. char DirName[];
  201. int i;
  202. int len = strlen ( DirName );
  203.  
  204. strcpy ( DirName, sPathName );
  205.  
  206. if ( DirName[ len- ] != '/' )
  207. strcat ( DirName, "/" );
  208.  
  209. len = strlen ( DirName );
  210.  
  211. for ( i = ; i < len; i++ ) {
  212. if ( DirName[i] == '/' ) {
  213. DirName[i] = ;
  214. if( access ( DirName, NULL ) != ) {
  215. if(mkdir(DirName, )==-) {
  216. perror("mkdir error");
  217. return -;
  218. }
  219. }
  220. DirName[i] = '/';
  221. }
  222. }
  223.  
  224. return ;
  225. }
  226.  
  227. /**
  228. * i: this can only be positive
  229. */
  230. void calculateNextMacAddress( int mac[], int i ) {
  231.  
  232. int macByte5 = ( i + mac[] ) % ;
  233. int macByte4 = ( ( i + mac[] ) / + mac[] ) % ;
  234. int macByte3 = ( ( ( ( i + mac[] ) / ) + mac[] ) / + mac[] ) % ;
  235. int macByte2 = ( ( ( ( ( i + mac[] ) / ) + mac[] ) / + mac[] ) / + mac[] ) % ;
  236. int macByte1 = ( ( ( ( ( ( i + mac[] ) / ) + mac[] ) / + mac[] ) / + mac[] ) / + mac[] ) % ;
  237. int macByte0 = ( ( ( ( ( ( ( i + mac[] ) / ) + mac[] ) / + mac[] ) / + mac[] ) / + mac[] ) / + mac[] ) % ;
  238.  
  239. mac[] = macByte0;
  240. mac[] = macByte1;
  241. mac[] = macByte2;
  242. mac[] = macByte3;
  243. mac[] = macByte4;
  244. mac[] = macByte5;
  245.  
  246. Debug ( "calculate next Mac : %02x:%02x:%02x:%02x:%02x:%02x\n", macByte0, macByte1, macByte2, macByte3, macByte4, macByte5 );
  247. }
  248.  
  249. void calculateNextLisence( char lisence[], int i ) {
  250. int tmp = ;
  251. tmp = changeLisenceByte( &lisence[], i );
  252. tmp = changeLisenceByte( &lisence[], tmp );
  253. tmp = changeLisenceByte( &lisence[], tmp );
  254. tmp = changeLisenceByte( &lisence[], tmp );
  255. tmp = changeLisenceByte( &lisence[], tmp );
  256. tmp = changeLisenceByte( &lisence[], tmp );
  257. tmp = changeLisenceByte( &lisence[], tmp );
  258. tmp = changeLisenceByte( &lisence[], tmp );
  259. tmp = changeLisenceByte( &lisence[], tmp );
  260. tmp = changeLisenceByte( &lisence[], tmp );
  261. tmp = changeLisenceByte( &lisence[], tmp );
  262. tmp = changeLisenceByte( &lisence[], tmp );
  263. tmp = changeLisenceByte( &lisence[], tmp );
  264. tmp = changeLisenceByte( &lisence[], tmp );
  265. tmp = changeLisenceByte( &lisence[], tmp );
  266. tmp = changeLisenceByte( &lisence[], tmp );
  267. tmp = changeLisenceByte( &lisence[], tmp );
  268.  
  269. Debug ( "calculate next lisence : %s\n", lisence );
  270. }
  271.  
  272. /**
  273. * number: this can be positive or negative
  274. */
  275. int changeLisenceByte( char * lisenceByte, int number ) {
  276. int tmp = ;
  277. if ( ( lisenceByte[] - ) + number < ) {
  278. tmp = ( ( lisenceByte[] - ) + number ) / - ;
  279. lisenceByte[] = ( + ( ( lisenceByte[] - ) + number ) % ) + ;
  280. } else {
  281. tmp = ( ( lisenceByte[] - ) + number ) / ;
  282. lisenceByte[] = ( ( lisenceByte[] - ) + number ) % + ;
  283. }
  284.  
  285. return tmp;
  286. }

calculate MAC,Lisence,Checksum and generate muti-file的更多相关文章

  1. 【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 ...

  2. Generate BKS File( Bouncy Castle KeyStore)

    echo "Enter BKS output file name : \c" read filename echo "Enter BKS Password : \c&qu ...

  3. generate eml file

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  4. Generate input file for OVITO

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  5. Mac Terminal open app with a file opened

    open -a /Applications/Sublime Text.app test.cpp

  6. 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 ...

  7. Windows平台CUDA开发之前的准备工作

    CUDA是NVIDIA的GPU开发工具,眼下在大规模并行计算领域有着广泛应用. windows平台上面的CUDA开发之前.最好去NVIDIA官网查看说明,然后下载对应的driver. ToolKits ...

  8. 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 ...

  9. IAR EWARM Checksum Technical Note

    IELFTOOL Checksum - Basic actions EW targets: ARM, RH850, RX, SH, STM8 EW component: General issues ...

随机推荐

  1. Spring 注解 @Resource和@Autowired

    @Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spr ...

  2. vs2010的VCVARS32.BAT所在位置

    1. C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat 2. ZC:vs08 和 vs2010 安装好后, ...

  3. [原][OSG][osgBullet][osgworks][bullet]编译osgBullet尝试物理引擎

    相关网址: 类似文章:http://blog.csdn.net/lh1162810317/article/details/17475297 osgBullet官网:http://osgbullet.v ...

  4. Codeforces 862B - Mahmoud and Ehab and the bipartiteness

    862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...

  5. Insert Delete GetRandom O(1)

    2018-07-15 18:36:29 问题描述: 问题求解: private ArrayList<Integer> ls; private HashMap<Integer, Int ...

  6. C#复制文件

    string pLocalFilePath ="";//要复制的文件路径 string pSaveFilePath ="";//指定存储的路径 if (File ...

  7. 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 重新编译 ...

  8. SpringBoot导入excle文件数据

    本文主要描述,Springboot框架下上传excel,处理里面相关数据做逻辑分析,由于用到的是前后端分离技术,这里记录的主要是后端java部分,通过与前端接口进行对接实现功能 1.在pom.xml文 ...

  9. [Java学习] Java Object类

    Object 类位于 java.lang 包中,是所有 Java 类的祖先,Java 中的每个类都由它扩展而来. 定义Java类时如果没有显示的指明父类,那么就默认继承了 Object 类.例如: 1 ...

  10. python-day45--mysql索引

    一 .介绍 为何要有索引? 一些复杂的查询操作,对查询语句的优化显然是重中之重.说起加速查询,就不得不提到索引了. 什么是索引? 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结 ...