1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <unistd.h>56#include <sys/stat.h>7#include <sys/types.h>89#define MIN(A, B) ((A) < (B) ? (A) : (B))1011int12xmemcmp(void *s1, size_t l1, void *s2, size_t l2)13{14 return memcmp(s1, s2, MIN(l1, l2));15}1617unsigned char*18readfile(char *fp)19{20 FILE *fd;21 struct stat st;22 size_t len, read;23 unsigned char *fc;2425 if (stat(fp, &st))26 return NULL;27 len = st.st_size;2829 fc = malloc(len + 1);30 if (!fc)31 return NULL;32 if (!(fd = fopen(fp, "r")))33 return NULL;3435 read = fread(fc, sizeof(unsigned char), len, fd);36 if (ferror(fd))37 return NULL;38 if (fclose(fd))39 return NULL;4041 fc[read] = '\0';42 return fc;43}