X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/SecurityTests/cspxutils/utilLib/fileIo.c diff --git a/SecurityTests/cspxutils/utilLib/fileIo.c b/SecurityTests/cspxutils/utilLib/fileIo.c new file mode 100644 index 00000000..3ad1fae5 --- /dev/null +++ b/SecurityTests/cspxutils/utilLib/fileIo.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +#include +#include "fileIo.h" + +int cspWriteFile( + const char *fileName, + const unsigned char *bytes, + unsigned numBytes) +{ + int rtn; + int fd; + + fd = open(fileName, O_RDWR | O_CREAT | O_TRUNC, 0600); + if(fd <= 0) { + return errno; + } + rtn = lseek(fd, 0, SEEK_SET); + if(rtn < 0) { + return errno; + } + rtn = write(fd, bytes, (size_t)numBytes); + if(rtn != (int)numBytes) { + if(rtn >= 0) { + printf("writeFile: short write\n"); + } + rtn = EIO; + } + else { + rtn = 0; + } + close(fd); + return rtn; +} + +/* + * Read entire file. + */ +int cspReadFile( + const char *fileName, + unsigned char **bytes, // mallocd and returned + unsigned *numBytes) // returned +{ + int ourRtn = 0; + int fd; + char *buf; + char *thisBuf; + struct stat sb; + unsigned size; + size_t toMove; + ssize_t thisMoved; + int irtn; + off_t lrtn = 0; + + *numBytes = 0; + *bytes = NULL; + fd = open(fileName, O_RDONLY, 0); + if(fd <= 0) { + perror("open"); + return errno; + } + irtn = fstat(fd, &sb); + if(irtn) { + ourRtn = errno; + if(ourRtn == 0) { + fprintf(stderr, "***Bogus zero error on fstat\n"); + ourRtn = -1; + } + else { + perror("fstat"); + } + goto errOut; + } + size = sb.st_size; + buf = thisBuf = (char *)malloc(size); + if(buf == NULL) { + ourRtn = ENOMEM; + goto errOut; + } + lrtn = lseek(fd, 0, SEEK_SET); + if(lrtn < 0) { + ourRtn = errno; + if(ourRtn == 0) { + fprintf(stderr, "***Bogus zero error on lseek\n"); + ourRtn = -1; + } + else { + perror("lseek"); + } + goto errOut; + } + toMove = size; + + /* + * On ppc this read ALWAYS returns the entire file. On i386, not so. + */ + do { + thisMoved = read(fd, thisBuf, toMove); + if(thisMoved == 0) { + /* reading empty file: done */ + break; + } + else if(thisMoved < 0) { + ourRtn = errno; + perror("read"); + break; + } + size_t uThisMoved = (size_t)thisMoved; + if(uThisMoved != toMove) { + fprintf(stderr, "===Short read: asked for %ld, got %lu\n", + toMove, uThisMoved); + } + toMove -= thisMoved; + thisBuf += thisMoved; + } while(toMove); + + if(ourRtn == 0) { + *bytes = (unsigned char *)buf; + *numBytes = size; + } +errOut: + close(fd); + return ourRtn; +}