]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cdsa_utils/lib/cuFileIo.c
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / libsecurity_cdsa_utils / lib / cuFileIo.c
1 /*
2 * Copyright (c) 2001-2003,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please
7 * obtain a copy of the License at http://www.apple.com/publicsource and
8 * read it before using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
12 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
13 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
15 * Please see the License for the specific language governing rights and
16 * limitations under the License.
17 */
18
19 /*
20 File: cuFileIo.c
21
22 Description: simple file read/write utilities
23 */
24
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include "cuFileIo.h"
33
34 int writeFile(
35 const char *fileName,
36 const unsigned char *bytes,
37 unsigned numBytes)
38 {
39 size_t n = numBytes;
40 return writeFileSizet(fileName, bytes, n);
41 }
42
43 int writeFileSizet(
44 const char *fileName,
45 const unsigned char *bytes,
46 size_t numBytes)
47 {
48 int rtn;
49 int fd;
50
51 fd = open(fileName, O_RDWR | O_CREAT | O_TRUNC, 0600);
52 if(fd < 0) {
53 return errno;
54 }
55 rtn = (int)lseek(fd, 0, SEEK_SET);
56 if(rtn < 0) {
57 return errno;
58 }
59 rtn = (int)write(fd, bytes, (size_t)numBytes);
60 if(rtn != (int)numBytes) {
61 if(rtn >= 0) {
62 printf("writeFile: short write\n");
63 }
64 rtn = EIO;
65 }
66 else {
67 rtn = 0;
68 }
69 close(fd);
70 return rtn;
71 }
72
73 /*
74 * Read entire file.
75 */
76 int readFile(
77 const char *fileName,
78 unsigned char **bytes, // mallocd and returned
79 unsigned *numBytes) // returned
80 {
81 int rtn;
82 int fd;
83 unsigned char *buf;
84 struct stat sb;
85 unsigned size;
86
87 *numBytes = 0;
88 *bytes = NULL;
89 fd = open(fileName, O_RDONLY, 0);
90 if(fd < 0) {
91 return errno;
92 }
93 rtn = fstat(fd, &sb);
94 if(rtn) {
95 goto errOut;
96 }
97 size = (unsigned)sb.st_size;
98 buf = malloc(size);
99 if(buf == NULL) {
100 rtn = ENOMEM;
101 goto errOut;
102 }
103 rtn = (int)lseek(fd, 0, SEEK_SET);
104 if(rtn < 0) {
105 free(buf);
106 goto errOut;
107 }
108 rtn = (int)read(fd, buf, (size_t)size);
109 if(rtn != (int)size) {
110 if(rtn >= 0) {
111 printf("readFile: short read\n");
112 }
113 free(buf);
114 rtn = EIO;
115 }
116 else {
117 rtn = 0;
118 *bytes = buf;
119 *numBytes = size;
120 }
121 errOut:
122 close(fd);
123 return rtn;
124 }