+
+// xor two arrays together for len items. The dst array contains the new xored values.
+void xor(unsigned char *dst, unsigned char *src, size_t len) {
+ for( ; len > 0; len--,dst++,src++)
+ *dst ^= *src;
+}
+
+int32_t le24toh (uint8_t data[3]) {
+ return (data[2] << 16) | (data[1] << 8) | data[0];
+}
+uint32_t le32toh (uint8_t *data) {
+ return (uint32_t)( (data[3]<<24) | (data[2]<<16) | (data[1]<<8) | data[0]);
+}
+
+// RotateLeft - Ultralight, Desfire, works on byte level
+// 00-01-02 >> 01-02-00
+void rol(uint8_t *data, const size_t len){
+ uint8_t first = data[0];
+ for (size_t i = 0; i < len-1; i++) {
+ data[i] = data[i+1];
+ }
+ data[len-1] = first;
+}
+
+
+// Replace unprintable characters with a dot in char buffer
+void clean_ascii(unsigned char *buf, size_t len) {
+ for (size_t i = 0; i < len; i++) {
+ if (!isprint(buf[i]))
+ buf[i] = '.';
+ }
+}
+
+// replace \r \n to \0
+void strcleanrn(char *buf, size_t len) {
+ strcreplace(buf, len, '\n', '\0');
+ strcreplace(buf, len, '\r', '\0');
+}
+
+// replace char in buffer
+void strcreplace(char *buf, size_t len, char from, char to) {
+ for (size_t i = 0; i < len; i++) {
+ if (buf[i] == from)
+ buf[i] = to;
+ }
+}
+
+char *strmcopy(char *buf) {
+ char * str = NULL;
+ if ((str = (char*) malloc(strlen(buf) + 1)) != NULL) {
+ memset(str, 0, strlen(buf) + 1);
+ strcpy(str, buf);
+ }
+ return str;
+}
+
+
+// determine number of logical CPU cores (use for multithreaded functions)
+extern int num_CPUs(void)
+{
+#if defined(_WIN32)
+ #include <sysinfoapi.h>
+ SYSTEM_INFO sysinfo;
+ GetSystemInfo(&sysinfo);
+ return sysinfo.dwNumberOfProcessors;
+#elif defined(__linux__) || defined(__APPLE__)
+ #include <unistd.h>
+ return sysconf(_SC_NPROCESSORS_ONLN);
+#else
+ return 1;
+#endif
+}
+