39497fc3a9a702cc84813997eef8f2e1c1dde71a
[tprouty/samba.git] / source / heimdal / lib / hx509 / file.c
1 /*
2  * Copyright (c) 2005 - 2006 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #include "hx_locl.h"
35 RCSID("$ID$");
36
37 int
38 _hx509_map_file(const char *fn, void **data, size_t *length, struct stat *rsb)
39 {
40     struct stat sb;
41     size_t len;
42     ssize_t l;
43     int ret;
44     void *d;
45     int fd;
46
47     *data = NULL;
48     *length = 0;
49
50     fd = open(fn, O_RDONLY);
51     if (fd < 0)
52         return errno;
53     
54     if (fstat(fd, &sb) < 0) {
55         ret = errno;
56         close(fd);
57         return ret;
58     }
59
60     len = sb.st_size;
61
62     d = malloc(len);
63     if (d == NULL) {
64         close(fd);
65         return ENOMEM;
66     }
67     
68     l = read(fd, d, len);
69     close(fd);
70     if (l < 0 || l != len) {
71         free(d);
72         return EINVAL;
73     }
74
75     if (rsb)
76         *rsb = sb;
77     *data = d;
78     *length = len;
79     return 0;
80 }
81
82 void
83 _hx509_unmap_file(void *data, size_t len)
84 {
85     free(data);
86 }
87
88 int
89 _hx509_write_file(const char *fn, const void *data, size_t length)
90 {
91     ssize_t sz;
92     const unsigned char *p = data;
93     int fd;
94
95     fd = open(fn, O_WRONLY|O_TRUNC|O_CREAT, 0644);
96     if (fd < 0)
97         return errno;
98
99     do {
100         sz = write(fd, p, length);
101         if (sz < 0) {
102             int saved_errno = errno;
103             close(fd);
104             return saved_errno;
105         }
106         if (sz == 0)
107             break;
108         length -= sz;
109     } while (length > 0);
110                 
111     if (close(fd) == -1)
112         return errno;
113
114     return 0;
115 }