]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdcrc.c
begin reveng add-ons for lua
[proxmark3-svn] / client / cmdcrc.c
CommitLineData
fe81b478 1//-----------------------------------------------------------------------------
2// Copyright (C) 2015 iceman <iceman at iuse.se>
3//
4// This code is licensed to you under the terms of the GNU GPL, version 2 or,
5// at your option, any later version. See the LICENSE.txt file for the text of
6// the license.
7//-----------------------------------------------------------------------------
8// CRC Calculations from the software reveng commands
9//-----------------------------------------------------------------------------
10
7e599947 11#include <stdlib.h>
12#ifdef _WIN32
13# include <io.h>
14# include <fcntl.h>
15# ifndef STDIN_FILENO
16# define STDIN_FILENO 0
17# endif /* STDIN_FILENO */
18#endif /* _WIN32 */
19
fe81b478 20#include <stdio.h>
21#include <string.h>
f46c3663 22//#include <stdlib.h>
23//#include <ctype.h>
fe81b478 24#include "cmdmain.h"
fe81b478 25#include "cmdcrc.h"
26#include "reveng/reveng.h"
f46c3663 27#include "ui.h"
28#include "util.h"
fe81b478 29
f46c3663 30#define MAX_ARGS 20
fe81b478 31
f46c3663 32int split(char *str, char *arr[MAX_ARGS]){
7e599947 33 int beginIndex = 0;
34 int endIndex;
35 int maxWords = MAX_ARGS;
36 int wordCnt = 0;
fe81b478 37
7e599947 38 while(1){
39 while(isspace(str[beginIndex])){
40 ++beginIndex;
41 }
42 if(str[beginIndex] == '\0')
43 break;
44 endIndex = beginIndex;
45 while (str[endIndex] && !isspace(str[endIndex])){
46 ++endIndex;
47 }
48 int len = endIndex - beginIndex;
49 char *tmp = calloc(len + 1, sizeof(char));
50 memcpy(tmp, &str[beginIndex], len);
51 arr[wordCnt++] = tmp;
52 //PrintAndLog("cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
53 beginIndex = endIndex;
54 if (wordCnt == maxWords)
55 break;
56 }
57 return wordCnt;
fe81b478 58}
59
fe81b478 60int CmdCrc(const char *Cmd)
61{
f46c3663 62 char name[] = {"reveng "};
63 char Cmd2[50 + 7];
64 memcpy(Cmd2, name, 7);
65 memcpy(Cmd2 + 7, Cmd, 50);
66 char *argv[MAX_ARGS];
67 int argc = split(Cmd2, argv);
68 //PrintAndLog("argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
69 reveng_main(argc, argv);
70 for(int i = 0; i < argc; ++i){
71 //puts(arr[i]);
72 free(argv[i]);
73 }
fe81b478 74
f46c3663 75 return 0;
fe81b478 76}
f46c3663 77
7e599947 78int GetModels(char *Models[], int *count, uint32_t *width){
79 /* default values */
80 static model_t model = {
81 PZERO, /* no CRC polynomial, user must specify */
82 PZERO, /* Init = 0 */
83 P_BE, /* RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h */
84 PZERO, /* XorOut = 0 */
85 PZERO, /* check value unused */
86 NULL /* no model name */
87 };
88 //int ibperhx = 8, obperhx = 8;
89 //int rflags = 0, uflags = 0; /* search and UI flags */
90
91 //unsigned long width = 0UL;
92 //int c, mode = 0, args, psets, pass;
93 //poly_t apoly, crc, qpoly = PZERO, *apolys, *pptr = NULL, *qptr = NULL;
94 //model_t pset = model, *candmods, *mptr;
95 //char *string;
96
97 //myname = argv[0];
98
99 /* stdin must be binary */
100 #ifdef _WIN32
101 _setmode(STDIN_FILENO, _O_BINARY);
102 #endif /* _WIN32 */
103
104 SETBMP();
105
106 //pos=0;
107 //optind=1;
108
109 if (*width == 0) { //reveng -D
110 *count = mcount();
111 //PrintAndLog("Count: %d",*count);
112 if(!*count){
113 PrintAndLog("no preset models available");
114 return 0;
115 }
116 for(int mode = 0; mode < *count; ++mode) {
117 mbynum(&model, mode);
118 mcanon(&model);
119 size_t size = (model.name && *model.name) ? strlen(model.name) : 6;
120 //PrintAndLog("Size: %d, %s",size,model.name);
121 char *tmp = calloc(size+1, sizeof(char));
122 if (tmp==NULL){
123 PrintAndLog("out of memory?");
124 return 0;
125 }
126 memcpy(tmp, model.name, size);
127 Models[mode] = tmp;
128 //ufound(&model);
129 }
130 } else { //reveng -s
131
132 }
133 //PrintAndLog("DONE");
134 return 1;
135}
136
137//test call to GetModels
138int CmdrevengTest(const char *Cmd){
139 char *Models[80];
140 int count = 0;
141 uint32_t width = 0;
142 int ans = GetModels(Models, &count, &width);
143 if (!ans) return 0;
144
145 PrintAndLog("Count: %d",count);
146 for (int i = 0; i < count; i++){
147 PrintAndLog("Model %d: %s",i,Models[i]);
148 free(Models[i]);
149 }
150 return 1;
151}
Impressum, Datenschutz