]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/util.c
8e7ef01b10d63d37f4beb75bfeaba10baaafa246
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 //-----------------------------------------------------------------------------
22 static struct termios Otty
, Ntty
;
28 Ntty
.c_iflag
= 0; /* input mode */
29 Ntty
.c_oflag
= 0; /* output mode */
30 Ntty
.c_lflag
&= ~ICANON
; /* raw mode */
31 Ntty
.c_cc
[VMIN
] = CMIN
; /* minimum time to wait */
32 Ntty
.c_cc
[VTIME
] = CTIME
; /* minimum characters to wait for */
34 if (0 == (error
= tcsetattr(0, TCSANOW
, &Ntty
))) {
35 error
+= ioctl(0, FIONREAD
, &cnt
);
36 error
+= tcsetattr(0, TCSANOW
, &Otty
);
39 return ( error
== 0 ? cnt
: -1 );
50 void print_hex(const uint8_t * data
, const size_t len
)
54 for (i
=0; i
< len
; i
++)
55 printf("%02x ", data
[i
]);
60 char * sprint_hex(const uint8_t * data
, const size_t len
) {
61 static char buf
[1024];
65 for (i
=0; i
< len
&& i
< 1024/3; i
++, tmp
+= 3)
66 sprintf(tmp
, "%02x ", data
[i
]);
71 void num_to_bytes(uint64_t n
, size_t len
, uint8_t* dest
)
74 dest
[len
] = (uint8_t) n
;
79 uint64_t bytes_to_num(uint8_t* src
, size_t len
)
84 num
= (num
<< 8) | (*src
);
90 // -------------------------------------------------------------------------
92 // bg, en - symbol numbers in param line of beginning an ending parameter
93 // paramnum - param number (from 0)
94 // -------------------------------------------------------------------------
95 int param_getptr(const char *line
, int *bg
, int *en
, int paramnum
)
98 int len
= strlen(line
);
104 while (line
[*bg
] ==' ' || line
[*bg
]=='\t') (*bg
)++;
109 for (i
= 0; i
< paramnum
; i
++) {
110 while (line
[*bg
]!=' ' && line
[*bg
]!='\t' && line
[*bg
] != '\0') (*bg
)++;
111 while (line
[*bg
]==' ' || line
[*bg
]=='\t') (*bg
)++;
113 if (line
[*bg
] == '\0') return 1;
117 while (line
[*en
] != ' ' && line
[*en
] != '\t' && line
[*en
] != '\0') (*en
)++;
124 char param_getchar(const char *line
, int paramnum
)
128 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 0x00;
133 uint8_t param_get8(const char *line
, int paramnum
)
135 return param_get8ex(line
, paramnum
, 10, 0);
138 uint8_t param_get8ex(const char *line
, int paramnum
, int deflt
, int base
)
142 if (!param_getptr(line
, &bg
, &en
, paramnum
))
143 return strtol(&line
[bg
], NULL
, base
) & 0xff;
148 uint32_t param_get32ex(const char *line
, int paramnum
, int deflt
, int base
)
152 if (!param_getptr(line
, &bg
, &en
, paramnum
))
153 return strtol(&line
[bg
], NULL
, base
);
158 uint64_t param_get64ex(const char *line
, int paramnum
, int deflt
, int base
)
162 if (!param_getptr(line
, &bg
, &en
, paramnum
))
163 return strtol(&line
[bg
], NULL
, base
);
170 int param_gethex(const char *line
, int paramnum
, uint8_t * data
, int hexcnt
)
177 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 1;
179 if (en
- bg
+ 1 != hexcnt
)
182 for(i
= 0; i
< hexcnt
; i
+= 2) {
183 if (!(isxdigit(line
[bg
+ i
]) && isxdigit(line
[bg
+ i
+ 1])) ) return 1;
185 sscanf((char[]){line
[bg
+ i
], line
[bg
+ i
+ 1], 0}, "%X", &temp
);
186 data
[i
/ 2] = temp
& 0xff;