]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/util.c
6825d721d4622d9f383f79a6ac6ebe21984c2fea
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
19 #include <sys/ioctl.h>
24 static struct termios Otty
, Ntty
;
30 Ntty
.c_iflag
= 0; /* input mode */
31 Ntty
.c_oflag
= 0; /* output mode */
32 Ntty
.c_lflag
&= ~ICANON
; /* raw mode */
33 Ntty
.c_cc
[VMIN
] = CMIN
; /* minimum time to wait */
34 Ntty
.c_cc
[VTIME
] = CTIME
; /* minimum characters to wait for */
36 if (0 == (error
= tcsetattr(0, TCSANOW
, &Ntty
))) {
37 error
+= ioctl(0, FIONREAD
, &cnt
);
38 error
+= tcsetattr(0, TCSANOW
, &Otty
);
41 return ( error
== 0 ? cnt
: -1 );
52 void print_hex(const uint8_t * data
, const size_t len
)
56 for (i
=0; i
< len
; i
++)
57 printf("%02x ", data
[i
]);
62 char * sprint_hex(const uint8_t * data
, const size_t len
) {
63 static char buf
[1024];
67 for (i
=0; i
< len
&& i
< 1024/3; i
++, tmp
+= 3)
68 sprintf(tmp
, "%02x ", data
[i
]);
73 void num_to_bytes(uint64_t n
, size_t len
, uint8_t* dest
)
76 dest
[len
] = (uint8_t) n
;
81 uint64_t bytes_to_num(uint8_t* src
, size_t len
)
86 num
= (num
<< 8) | (*src
);
92 // -------------------------------------------------------------------------
94 // bg, en - symbol numbers in param line of beginning an ending parameter
95 // paramnum - param number (from 0)
96 // -------------------------------------------------------------------------
97 int param_getptr(const char *line
, int *bg
, int *en
, int paramnum
)
100 int len
= strlen(line
);
106 while (line
[*bg
] ==' ' || line
[*bg
]=='\t') (*bg
)++;
111 for (i
= 0; i
< paramnum
; i
++) {
112 while (line
[*bg
]!=' ' && line
[*bg
]!='\t' && line
[*bg
] != '\0') (*bg
)++;
113 while (line
[*bg
]==' ' || line
[*bg
]=='\t') (*bg
)++;
115 if (line
[*bg
] == '\0') return 1;
119 while (line
[*en
] != ' ' && line
[*en
] != '\t' && line
[*en
] != '\0') (*en
)++;
126 char param_getchar(const char *line
, int paramnum
)
130 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 0x00;
135 uint8_t param_get8(const char *line
, int paramnum
)
137 return param_get8ex(line
, paramnum
, 10, 0);
140 uint8_t param_get8ex(const char *line
, int paramnum
, int deflt
, int base
)
144 if (!param_getptr(line
, &bg
, &en
, paramnum
))
145 return strtol(&line
[bg
], NULL
, base
) & 0xff;
150 uint32_t param_get32ex(const char *line
, int paramnum
, int deflt
, int base
)
154 if (!param_getptr(line
, &bg
, &en
, paramnum
))
155 return strtol(&line
[bg
], NULL
, base
);
160 uint64_t param_get64ex(const char *line
, int paramnum
, int deflt
, int base
)
164 if (!param_getptr(line
, &bg
, &en
, paramnum
))
165 return strtol(&line
[bg
], NULL
, base
);
172 int param_gethex(const char *line
, int paramnum
, uint8_t * data
, int hexcnt
)
179 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 1;
181 if (en
- bg
+ 1 != hexcnt
)
184 for(i
= 0; i
< hexcnt
; i
+= 2) {
185 if (!(isxdigit(line
[bg
+ i
]) && isxdigit(line
[bg
+ i
+ 1])) ) return 1;
187 sscanf((char[]){line
[bg
+ i
], line
[bg
+ i
+ 1], 0}, "%X", &temp
);
188 data
[i
/ 2] = temp
& 0xff;