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