]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/util.c
ae213e43f55a4521dddafd70393ae6e950119e71
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 // -------------------------------------------------------------------------
93 // string parameters lib
94 // -------------------------------------------------------------------------
96 // -------------------------------------------------------------------------
98 // bg, en - symbol numbers in param line of beginning an ending parameter
99 // paramnum - param number (from 0)
100 // -------------------------------------------------------------------------
101 int param_getptr(const char *line
, int *bg
, int *en
, int paramnum
)
104 int len
= strlen(line
);
110 while (line
[*bg
] ==' ' || line
[*bg
]=='\t') (*bg
)++;
115 for (i
= 0; i
< paramnum
; i
++) {
116 while (line
[*bg
]!=' ' && line
[*bg
]!='\t' && line
[*bg
] != '\0') (*bg
)++;
117 while (line
[*bg
]==' ' || line
[*bg
]=='\t') (*bg
)++;
119 if (line
[*bg
] == '\0') return 1;
123 while (line
[*en
] != ' ' && line
[*en
] != '\t' && line
[*en
] != '\0') (*en
)++;
130 char param_getchar(const char *line
, int paramnum
)
134 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 0x00;
139 uint8_t param_get8(const char *line
, int paramnum
)
141 return param_get8ex(line
, paramnum
, 10, 0);
144 uint8_t param_get8ex(const char *line
, int paramnum
, int deflt
, int base
)
148 if (!param_getptr(line
, &bg
, &en
, paramnum
))
149 return strtol(&line
[bg
], NULL
, base
) & 0xff;
154 uint32_t param_get32ex(const char *line
, int paramnum
, int deflt
, int base
)
158 if (!param_getptr(line
, &bg
, &en
, paramnum
))
159 return strtol(&line
[bg
], NULL
, base
);
164 uint64_t param_get64ex(const char *line
, int paramnum
, int deflt
, int base
)
168 if (!param_getptr(line
, &bg
, &en
, paramnum
))
169 return strtol(&line
[bg
], NULL
, base
);
176 int param_gethex(const char *line
, int paramnum
, uint8_t * data
, int hexcnt
)
183 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 1;
185 if (en
- bg
+ 1 != hexcnt
)
188 for(i
= 0; i
< hexcnt
; i
+= 2) {
189 if (!(isxdigit(line
[bg
+ i
]) && isxdigit(line
[bg
+ i
+ 1])) ) return 1;
191 sscanf((char[]){line
[bg
+ i
], line
[bg
+ i
+ 1], 0}, "%X", &temp
);
192 data
[i
/ 2] = temp
& 0xff;
198 int param_getstr(const char *line
, int paramnum
, char * str
)
202 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 0;
204 memcpy(str
, line
+ bg
, en
- bg
+ 1);
205 str
[en
- bg
+ 1] = 0;