#include "lib.h" #include "polarssl/base64.h" #define IO_BLOCKSIZE 5*MB // Memory int CopyData(u8 **dest, const u8 *source, u64 size) { if(!*dest){ *dest = malloc(size); if(!*dest) return -1; } memcpy(*dest,source,size); return 0; } void rndset(void *ptr, u64 num) { u8 *tmp = (u8*)ptr; for(u64 i = 0; i < num ; i++) tmp[i] = u8GetRand(); } void clrmem(void *ptr, u64 num) { memset(ptr,0,num); } // Misc u64 roundup(u64 value, u64 alignment) { return value + alignment - value % alignment; } u64 align(u64 value, u64 alignment) { if(value % alignment != 0) return roundup(value,alignment); else return value; } u64 min64(u64 a, u64 b) { if(a < b) return a; return b; } u64 max64(u64 a, u64 b) { if(a > b) return a; return b; } // Strings char* replace_filextention(const char *input, const char *new_ext) { if(input == NULL || new_ext == NULL) return NULL; char *new_name; char *ext = strrchr(input, '.'); // If there is no existing extention, just append new_ext if (!ext) { new_name = calloc(strlen(input) + strlen(new_ext), 1); sprintf(new_name, "%s%s", input, new_ext); } else { u32 size = ext - input; new_name = calloc(size + strlen(new_ext) + 1, 1); strncpy(new_name, input, size); sprintf(new_name, "%s%s", new_name, new_ext); } return new_name; } void memdump(FILE* fout, const char* prefix, const u8* data, u32 size) { u32 i; u32 prefixlen = strlen(prefix); u32 offs = 0; u32 line = 0; while(size) { u32 max = 32; if (max > size) max = size; if (line==0) fprintf(fout, "%s", prefix); else fprintf(fout, "%*s", prefixlen, ""); for(i=0; i