]> git.zerfleddert.de Git - proxmark3-svn/blame - client/util.c
Fix PrintAndLogEx ERR (#748)
[proxmark3-svn] / client / util.c
CommitLineData
20f9a2a1
M
1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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// utilities
9//-----------------------------------------------------------------------------
10
11#include "util.h"
acf0582d 12
13#include <stdint.h>
14#include <string.h>
15#include <ctype.h>
16#include <stdlib.h>
17#include <stdio.h>
18#include <time.h>
03439be3 19#include <stdarg.h>
acf0582d 20
ec9c7112 21#ifdef _WIN32
22#include <windows.h>
23#endif
24
ace26dbd 25#define MAX_BIN_BREAK_LENGTH (3072+384+1)
534983d7 26
cb64309e 27#ifndef _WIN32
873014de
M
28#include <termios.h>
29#include <sys/ioctl.h>
8c0ccdef 30#include <unistd.h>
79544b28 31
f397b5cc
M
32int ukbhit(void)
33{
34 int cnt = 0;
35 int error;
36 static struct termios Otty, Ntty;
37
8c0ccdef 38 if ( tcgetattr(STDIN_FILENO, &Otty) == -1 ) return -1;
f397b5cc
M
39 Ntty = Otty;
40
8c0ccdef 41 Ntty.c_iflag = 0x0000; // input mode
42 Ntty.c_oflag = 0x0000; // output mode
43 Ntty.c_lflag &= ~ICANON; // control mode = raw
44 Ntty.c_cc[VMIN] = 1; // return if at least 1 character is in the queue
45 Ntty.c_cc[VTIME] = 0; // no timeout. Wait forever
46
47 if (0 == (error = tcsetattr(STDIN_FILENO, TCSANOW, &Ntty))) { // set new attributes
48 error += ioctl(STDIN_FILENO, FIONREAD, &cnt); // get number of characters availabe
49 error += tcsetattr(STDIN_FILENO, TCSANOW, &Otty); // reset attributes
f397b5cc
M
50 }
51
52 return ( error == 0 ? cnt : -1 );
53}
54
55#else
acf0582d 56
f397b5cc
M
57#include <conio.h>
58int ukbhit(void) {
59 return kbhit();
60}
61#endif
62
55acbb2a 63// log files functions
b915fda3 64void AddLogLine(char *file, char *extData, char *c) {
55acbb2a 65 FILE *fLog = NULL;
b915fda3 66 char filename[FILE_PATH_SIZE] = {0x00};
67 int len = 0;
68
69 len = strlen(file);
70 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
71 memcpy(filename, file, len);
72
73 fLog = fopen(filename, "a");
55acbb2a 74 if (!fLog) {
b915fda3 75 printf("Could not append log file %s", filename);
55acbb2a
M
76 return;
77 }
78
79 fprintf(fLog, "%s", extData);
80 fprintf(fLog, "%s\n", c);
81 fclose(fLog);
82}
83
84void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){
85 AddLogLine(fileName, extData, sprint_hex(data, len));
86}
87
88void AddLogUint64(char *fileName, char *extData, const uint64_t data) {
89 char buf[100] = {0};
90 sprintf(buf, "%x%x", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF));
91 AddLogLine(fileName, extData, buf);
92}
93
94void AddLogCurrentDT(char *fileName) {
95 char buff[20];
96 struct tm *curTime;
97
98 time_t now = time(0);
99 curTime = gmtime(&now);
100
101 strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", curTime);
102 AddLogLine(fileName, "\nanticollision: ", buff);
103}
104
e0c635d1 105void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) {
55acbb2a
M
106 char * fnameptr = fileName;
107 memset(fileName, 0x00, 200);
108
e0c635d1 109 for (int j = 0; j < byteCount; j++, fnameptr += 2)
d1869c33 110 sprintf(fnameptr, "%02x", (unsigned int) uid[j]);
55acbb2a 111 sprintf(fnameptr, "%s", ext);
55acbb2a
M
112}
113
6b882a39
OM
114// fill buffer from structure [{uint8_t data, size_t length},...]
115int FillBuffer(uint8_t *data, size_t maxDataLength, size_t *dataLength, ...) {
116 *dataLength = 0;
117 va_list valist;
118 va_start(valist, dataLength);
119
120 uint8_t *vdata = NULL;
121 size_t vlength = 0;
122 do{
123 vdata = va_arg(valist, uint8_t *);
124 if (!vdata)
125 break;
126
127 vlength = va_arg(valist, size_t);
128 if (*dataLength + vlength > maxDataLength) {
129 va_end(valist);
130 return 1;
131 }
132
133 memcpy(&data[*dataLength], vdata, vlength);
134 *dataLength += vlength;
135
136 } while (vdata);
137
138 va_end(valist);
139
140 return 0;
141}
142
0bb51450
OM
143bool CheckStringIsHEXValue(const char *value) {
144 for (int i = 0; i < strlen(value); i++)
145 if (!isxdigit(value[i]))
146 return false;
147
148 if (strlen(value) % 2)
149 return false;
150
151 return true;
152}
153
66efdc1f 154void hex_to_buffer(const uint8_t *buf, const uint8_t *hex_data, const size_t hex_len, const size_t hex_max_len,
155 const size_t min_str_len, const size_t spaces_between, bool uppercase) {
156
157 char *tmp = (char *)buf;
158 size_t i;
edd4c838 159 memset(tmp, 0x00, hex_max_len);
66efdc1f 160
161 int maxLen = ( hex_len > hex_max_len) ? hex_max_len : hex_len;
162
163 for (i = 0; i < maxLen; ++i, tmp += 2 + spaces_between) {
164 sprintf(tmp, (uppercase) ? "%02X" : "%02x", (unsigned int) hex_data[i]);
165
166 for (int j = 0; j < spaces_between; j++)
167 sprintf(tmp + 2 + j, " ");
168 }
169
170 i *= (2 + spaces_between);
171 int minStrLen = min_str_len > i ? min_str_len : 0;
172 if (minStrLen > hex_max_len)
173 minStrLen = hex_max_len;
174 for(; i < minStrLen; i++, tmp += 1)
175 sprintf(tmp, " ");
176
177 return;
178}
179
55acbb2a 180// printing and converting functions
f397b5cc 181
4973f23d 182char *sprint_hex(const uint8_t *data, const size_t len) {
39cc1c87 183 static char buf[4097] = {0};
b915fda3 184
66efdc1f 185 hex_to_buffer((uint8_t *)buf, data, len, sizeof(buf) - 1, 0, 1, false);
534983d7 186
187 return buf;
188}
f89c7050 189
3c5fce2b 190char *sprint_hex_inrow_ex(const uint8_t *data, const size_t len, const size_t min_str_len) {
39cc1c87 191 static char buf[4097] = {0};
3c5fce2b 192
66efdc1f 193 hex_to_buffer((uint8_t *)buf, data, len, sizeof(buf) - 1, min_str_len, 0, false);
3c5fce2b
OM
194
195 return buf;
196}
197
198char *sprint_hex_inrow(const uint8_t *data, const size_t len) {
199 return sprint_hex_inrow_ex(data, len, 0);
200}
201
2767fc02 202char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t breaks) {
ace26dbd 203 // make sure we don't go beyond our char array memory
7bc6fac3 204 int max_len;
205 if (breaks==0)
206 max_len = ( len > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len;
207 else
208 max_len = ( len+(len/breaks) > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len+(len/breaks);
209
ace26dbd 210 static char buf[MAX_BIN_BREAK_LENGTH]; // 3072 + end of line characters if broken at 8 bits
211 //clear memory
212 memset(buf, 0x00, sizeof(buf));
2767fc02 213 char *tmp = buf;
79544b28 214
ace26dbd 215 size_t in_index = 0;
216 // loop through the out_index to make sure we don't go too far
b97311b1 217 for (size_t out_index=0; out_index < max_len; out_index++) {
8ea57060 218 // set character - (should be binary but verify it isn't more than 1 digit)
219 if (data[in_index]<10)
d1869c33 220 sprintf(tmp++, "%u", (unsigned int) data[in_index]);
6ca1477c 221 // check if a line break is needed and we have room to print it in our array
b9957414 222 if ( (breaks > 0) && !((in_index+1) % breaks) && (out_index+1 < max_len) ) {
ace26dbd 223 // increment and print line break
224 out_index++;
2767fc02 225 sprintf(tmp++, "%s","\n");
ace26dbd 226 }
227 in_index++;
2767fc02 228 }
79544b28 229
230 return buf;
231}
232
2767fc02 233char *sprint_bin(const uint8_t *data, const size_t len) {
234 return sprint_bin_break(data, len, 0);
235}
59f726c9 236
3c5fce2b 237char *sprint_ascii_ex(const uint8_t *data, const size_t len, const size_t min_str_len) {
59f726c9 238 static char buf[1024];
239 char *tmp = buf;
240 memset(buf, 0x00, 1024);
241 size_t max_len = (len > 1010) ? 1010 : len;
242 size_t i = 0;
243 while(i < max_len){
244 char c = data[i];
245 tmp[i] = ((c < 32) || (c == 127)) ? '.' : c;
246 ++i;
247 }
3c5fce2b
OM
248
249 int minStrLen = min_str_len > i ? min_str_len : 0;
250 for(; i < minStrLen; ++i)
251 tmp[i] = ' ';
252
59f726c9 253 return buf;
254}
255
f89c7050
M
256void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
257{
258 while (len--) {
259 dest[len] = (uint8_t) n;
260 n >>= 8;
261 }
262}
263
264uint64_t bytes_to_num(uint8_t* src, size_t len)
265{
266 uint64_t num = 0;
267 while (len--)
268 {
269 num = (num << 8) | (*src);
270 src++;
271 }
272 return num;
273}
f397b5cc 274
709665b5 275void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest) {
276 while (len--) {
277 dest[len] = n & 1;
278 n >>= 1;
279 }
280}
281
59f726c9 282//least significant bit first
283void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest) {
284 for(int i = 0 ; i < len ; ++i) {
285 dest[i] = n & 1;
286 n >>= 1;
287 }
288}
289
2d42ea1e 290// Swap bit order on a uint32_t value. Can be limited by nrbits just use say 8bits reversal
291// And clears the rest of the bits.
292uint32_t SwapBits(uint32_t value, int nrbits) {
293 uint32_t newvalue = 0;
294 for(int i = 0; i < nrbits; i++) {
295 newvalue ^= ((value >> i) & 1) << (nrbits - 1 - i);
296 }
297 return newvalue;
298}
59f726c9 299
f9848fd6 300// aa,bb,cc,dd,ee,ff,gg,hh, ii,jj,kk,ll,mm,nn,oo,pp
301// to
302// hh,gg,ff,ee,dd,cc,bb,aa, pp,oo,nn,mm,ll,kk,jj,ii
303// up to 64 bytes or 512 bits
2b3af97d 304uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize){
305 static uint8_t buf[64];
306 memset(buf, 0x00, 64);
307 uint8_t *tmp = buf;
308 for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){
309 for (size_t i = 0; i < blockSize; i++){
310 tmp[i+(blockSize*block)] = src[(blockSize-1-i)+(blockSize*block)];
f9848fd6 311 }
312 }
2b3af97d 313 return tmp;
f9848fd6 314}
315
79544b28 316//assumes little endian
317char * printBits(size_t const size, void const * const ptr)
318{
319 unsigned char *b = (unsigned char*) ptr;
320 unsigned char byte;
321 static char buf[1024];
322 char * tmp = buf;
323 int i, j;
324
325 for (i=size-1;i>=0;i--)
326 {
327 for (j=7;j>=0;j--)
328 {
329 byte = b[i] & (1<<j);
330 byte >>= j;
d1869c33 331 sprintf(tmp, "%u", (unsigned int)byte);
79544b28 332 tmp++;
333 }
334 }
335 return buf;
336}
337
a37725fa
OM
338char * printBitsPar(const uint8_t *b, size_t len) {
339 static char buf1[512] = {0};
340 static char buf2[512] = {0};
341 static char *buf;
342 if (buf != buf1)
343 buf = buf1;
344 else
345 buf = buf2;
346 memset(buf, 0x00, 512);
347
348 for (int i = 0; i < len; i++) {
349 buf[i] = ((b[i / 8] << (i % 8)) & 0x80) ? '1':'0';
350 }
351 return buf;
352}
353
354
9ca155ba
M
355// -------------------------------------------------------------------------
356// string parameters lib
357// -------------------------------------------------------------------------
358
f397b5cc
M
359// -------------------------------------------------------------------------
360// line - param line
3a05a1e7 361// bg, en - symbol numbers in param line of beginning and ending parameter
f397b5cc
M
362// paramnum - param number (from 0)
363// -------------------------------------------------------------------------
364int param_getptr(const char *line, int *bg, int *en, int paramnum)
365{
366 int i;
367 int len = strlen(line);
368
369 *bg = 0;
370 *en = 0;
371
372 // skip spaces
373 while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++;
374 if (*bg >= len) {
375 return 1;
376 }
377
378 for (i = 0; i < paramnum; i++) {
379 while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++;
380 while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++;
381
382 if (line[*bg] == '\0') return 1;
383 }
384
385 *en = *bg;
386 while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++;
387
388 (*en)--;
389
390 return 0;
391}
392
31abe49f 393
3a05a1e7
OM
394int param_getlength(const char *line, int paramnum)
395{
396 int bg, en;
397
398 if (param_getptr(line, &bg, &en, paramnum)) return 0;
399
400 return en - bg + 1;
401}
402
8019540b 403char param_getchar(const char *line, int paramnum) {
404 return param_getchar_indx(line, 0, paramnum);
405}
406
407char param_getchar_indx(const char *line, int indx, int paramnum) {
f397b5cc
M
408 int bg, en;
409
410 if (param_getptr(line, &bg, &en, paramnum)) return 0x00;
411
8019540b 412 if (bg + indx > en)
413 return '\0';
414
415 return line[bg + indx];
f397b5cc
M
416}
417
418uint8_t param_get8(const char *line, int paramnum)
419{
6ca1477c 420 return param_get8ex(line, paramnum, 0, 10);
f397b5cc
M
421}
422
f6d9fb17 423/**
31abe49f 424 * @brief Reads a decimal integer (actually, 0-254, not 255)
f6d9fb17
MHS
425 * @param line
426 * @param paramnum
31abe49f 427 * @return -1 if error
f6d9fb17
MHS
428 */
429uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination)
430{
31abe49f 431 uint8_t val = param_get8ex(line, paramnum, 255, 10);
31abe49f 432 if( (int8_t) val == -1) return 1;
f6d9fb17
MHS
433 (*destination) = val;
434 return 0;
435}
436/**
437 * @brief Checks if param is decimal
438 * @param line
439 * @param paramnum
440 * @return
441 */
442uint8_t param_isdec(const char *line, int paramnum)
443{
444 int bg, en;
445 //TODO, check more thorougly
446 if (!param_getptr(line, &bg, &en, paramnum)) return 1;
447 // return strtoul(&line[bg], NULL, 10) & 0xff;
448
449 return 0;
450}
451
f397b5cc
M
452uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base)
453{
454 int bg, en;
455
456 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 457 return strtoul(&line[bg], NULL, base) & 0xff;
f397b5cc
M
458 else
459 return deflt;
460}
461
462uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base)
463{
464 int bg, en;
465
466 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 467 return strtoul(&line[bg], NULL, base);
f397b5cc
M
468 else
469 return deflt;
470}
471
472uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base)
473{
474 int bg, en;
475
476 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 477 return strtoull(&line[bg], NULL, base);
f397b5cc
M
478 else
479 return deflt;
f397b5cc
M
480}
481
482int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt)
483{
484 int bg, en, temp, i;
485
486 if (hexcnt % 2)
487 return 1;
488
489 if (param_getptr(line, &bg, &en, paramnum)) return 1;
490
491 if (en - bg + 1 != hexcnt)
492 return 1;
493
494 for(i = 0; i < hexcnt; i += 2) {
3ded0f97 495 if (!(isxdigit((unsigned char)line[bg + i]) && isxdigit((unsigned char)line[bg + i + 1])) ) return 1;
f397b5cc
M
496
497 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
498 data[i / 2] = temp & 0xff;
499 }
500
501 return 0;
502}
1a5a73ab 503int param_gethex_ex(const char *line, int paramnum, uint8_t * data, int *hexcnt)
504{
505 int bg, en, temp, i;
506
507 //if (hexcnt % 2)
508 // return 1;
509
510 if (param_getptr(line, &bg, &en, paramnum)) return 1;
511
512 *hexcnt = en - bg + 1;
513 if (*hexcnt % 2) //error if not complete hex bytes
514 return 1;
aea4d766 515
1a5a73ab 516 for(i = 0; i < *hexcnt; i += 2) {
3ded0f97 517 if (!(isxdigit((unsigned char)line[bg + i]) && isxdigit((unsigned char)line[bg + i + 1])) ) return 1;
1a5a73ab 518
519 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
520 data[i / 2] = temp & 0xff;
521 }
522
523 return 0;
524}
8019540b 525
526int param_gethex_to_eol(const char *line, int paramnum, uint8_t * data, int maxdatalen, int *datalen) {
527 int bg, en;
528 uint32_t temp;
529 char buf[5] = {0};
530
531 if (param_getptr(line, &bg, &en, paramnum)) return 1;
532
533 *datalen = 0;
534
535 int indx = bg;
536 while (line[indx]) {
3c5fce2b
OM
537 if (line[indx] == '\t' || line[indx] == ' ') {
538 indx++;
8019540b 539 continue;
3c5fce2b 540 }
8019540b 541
3ded0f97 542 if (isxdigit((unsigned char)line[indx])) {
8019540b 543 buf[strlen(buf) + 1] = 0x00;
544 buf[strlen(buf)] = line[indx];
545 } else {
546 // if we have symbols other than spaces and hex
547 return 1;
548 }
549
550 if (*datalen >= maxdatalen) {
551 // if we dont have space in buffer and have symbols to translate
552 return 2;
553 }
554
555 if (strlen(buf) >= 2) {
556 sscanf(buf, "%x", &temp);
557 data[*datalen] = (uint8_t)(temp & 0xff);
558 *buf = 0;
559 (*datalen)++;
560 }
561
562 indx++;
563 }
564
565 if (strlen(buf) > 0)
566 //error when not completed hex bytes
567 return 3;
568
569 return 0;
570}
571
874572d4 572int param_getstr(const char *line, int paramnum, char * str, size_t buffersize)
aea4d766 573{
574 int bg, en;
575
874572d4
WM
576 if (param_getptr(line, &bg, &en, paramnum)) {
577 return 0;
578 }
579
580 // Prevent out of bounds errors
581 if (en - bg + 1 >= buffersize) {
c95affa8 582 printf("out of bounds error: want %d bytes have %zd bytes\n", en - bg + 1 + 1, buffersize);
874572d4
WM
583 return 0;
584 }
aea4d766 585
586 memcpy(str, line + bg, en - bg + 1);
587 str[en - bg + 1] = 0;
588
589 return en - bg + 1;
590}
79544b28 591
592/*
593The following methods comes from Rfidler sourcecode.
594https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/src/
595*/
596
597// convert hex to sequence of 0/1 bit values
598// returns number of bits converted
599int hextobinarray(char *target, char *source)
600{
601 int length, i, count= 0;
874572d4 602 char* start = source;
79544b28 603 char x;
604
605 length = strlen(source);
606 // process 4 bits (1 hex digit) at a time
607 while(length--)
608 {
609 x= *(source++);
610 // capitalize
611 if (x >= 'a' && x <= 'f')
612 x -= 32;
613 // convert to numeric value
614 if (x >= '0' && x <= '9')
615 x -= '0';
616 else if (x >= 'A' && x <= 'F')
617 x -= 'A' - 10;
874572d4 618 else {
3ded0f97 619 printf("Discovered unknown character %c %d at idx %tu of %s\n", x, x, source - start, start);
79544b28 620 return 0;
874572d4 621 }
79544b28 622 // output
623 for(i= 0 ; i < 4 ; ++i, ++count)
624 *(target++)= (x >> (3 - i)) & 1;
625 }
626
627 return count;
628}
629
79544b28 630// convert binary array of 0x00/0x01 values to hex (safe to do in place as target will always be shorter than source)
631// return number of bits converted
1c4c0b06 632int binarraytohex(char *target,char *source, int length)
79544b28 633{
634 unsigned char i, x;
635 int j = length;
636
637 if(j % 4)
638 return 0;
639
640 while(j)
641 {
642 for(i= x= 0 ; i < 4 ; ++i)
643 x += ( source[i] << (3 - i));
d1869c33 644 sprintf(target,"%X", (unsigned int)x);
79544b28 645 ++target;
646 source += 4;
647 j -= 4;
648 }
649 return length;
650}
651
79544b28 652// return parity bit required to match type
709665b5 653uint8_t GetParity( uint8_t *bits, uint8_t type, int length)
79544b28 654{
655 int x;
656
657 for(x= 0 ; length > 0 ; --length)
658 x += bits[length - 1];
659 x %= 2;
660
661 return x ^ type;
662}
663
664// add HID parity to binary array: EVEN prefix for 1st half of ID, ODD suffix for 2nd half
709665b5 665void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length)
79544b28 666{
667 *(target++)= GetParity(source, EVEN, length / 2);
668 memcpy(target, source, length);
669 target += length;
670 *(target)= GetParity(source + length / 2, ODD, length / 2);
671}
4973f23d 672
59f726c9 673// xor two arrays together for len items. The dst array contains the new xored values.
4973f23d 674void xor(unsigned char *dst, unsigned char *src, size_t len) {
675 for( ; len > 0; len--,dst++,src++)
676 *dst ^= *src;
677}
678
c4c3af7c 679// RotateLeft - Ultralight, Desfire, works on byte level
680// 00-01-02 >> 01-02-00
681void rol(uint8_t *data, const size_t len){
682 uint8_t first = data[0];
683 for (size_t i = 0; i < len-1; i++) {
684 data[i] = data[i+1];
685 }
686 data[len-1] = first;
687}
d172c17c
JC
688
689
690// Replace unprintable characters with a dot in char buffer
691void clean_ascii(unsigned char *buf, size_t len) {
692 for (size_t i = 0; i < len; i++) {
693 if (!isprint(buf[i]))
694 buf[i] = '.';
695 }
696}
acf0582d 697
aa757f71
OM
698// replace \r \n to \0
699void strcleanrn(char *buf, size_t len) {
700 strcreplace(buf, len, '\n', '\0');
701 strcreplace(buf, len, '\r', '\0');
702}
acf0582d 703
aa757f71
OM
704// replace char in buffer
705void strcreplace(char *buf, size_t len, char from, char to) {
706 for (size_t i = 0; i < len; i++) {
707 if (buf[i] == from)
708 buf[i] = to;
709 }
710}
711
712char *strmcopy(char *buf) {
713 char * str = NULL;
714 if ((str = (char*) malloc(strlen(buf) + 1)) != NULL) {
715 memset(str, 0, strlen(buf) + 1);
716 strcpy(str, buf);
717 }
718 return str;
719}
acf0582d 720
c48c4d78 721
722// determine number of logical CPU cores (use for multithreaded functions)
723extern int num_CPUs(void)
724{
725#if defined(_WIN32)
726 #include <sysinfoapi.h>
727 SYSTEM_INFO sysinfo;
728 GetSystemInfo(&sysinfo);
729 return sysinfo.dwNumberOfProcessors;
730#elif defined(__linux__) || defined(__APPLE__)
731 #include <unistd.h>
732 return sysconf(_SC_NPROCESSORS_ONLN);
733#else
734 return 1;
735#endif
736}
ec9c7112 737
Impressum, Datenschutz