1/*
2
3Copyright 1988, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <X11/Xauth.h>
31#include <stdlib.h>
32
33static int
34read_short (unsigned short *shortp, FILE *file)
35{
36    unsigned char   file_short[2];
37
38    if (fread ((char *) file_short, sizeof (file_short), 1, file) != 1)
39	return 0;
40    *shortp = file_short[0] * 256 + file_short[1];
41    return 1;
42}
43
44static int
45read_counted_string (unsigned short *countp, char **stringp, FILE *file)
46{
47    unsigned short  len;
48    char	    *data;
49
50    if (read_short (&len, file) == 0)
51	return 0;
52    if (len == 0) {
53	data = NULL;
54    } else {
55    	data = malloc ((unsigned) len);
56    	if (!data)
57	    return 0;
58	if (fread (data, sizeof (char), len, file) != len) {
59#ifdef HAVE_EXPLICIT_BZERO
60	    explicit_bzero (data, len);
61#elif HAVE_EXPLICIT_MEMSET
62	    explicit_memset (data, 0, len);
63#else
64	    bzero (data, len);
65#endif
66	    free (data);
67	    return 0;
68    	}
69    }
70    *stringp = data;
71    *countp = len;
72    return 1;
73}
74
75Xauth *
76XauReadAuth (FILE *auth_file)
77{
78    Xauth   local = { 0, 0, NULL, 0, NULL, 0, NULL, 0, NULL };
79    Xauth   *ret;
80
81    if (read_short (&local.family, auth_file) == 0) {
82	goto fail;
83    }
84    if (read_counted_string (&local.address_length, &local.address, auth_file)
85        == 0) {
86	goto fail;
87    }
88    if (read_counted_string (&local.number_length, &local.number, auth_file)
89        == 0) {
90	goto fail;
91    }
92    if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) {
93	goto fail;
94    }
95    if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) {
96	goto fail;
97    }
98    ret = malloc (sizeof (Xauth));
99    if (ret == NULL) {
100	goto fail;
101    }
102    *ret = local;
103    return ret;
104
105  fail:
106    free (local.address);
107    free (local.number);
108    free (local.name);
109    if (local.data) {
110#ifdef HAVE_EXPLICIT_BZERO
111	explicit_bzero (local.data, local.data_length);
112#elif HAVE_EXPLICIT_MEMSET
113	explicit_memset (local.data, 0, local.data_length);
114#else
115	bzero (local.data, local.data_length);
116#endif
117	free (local.data);
118    }
119    return NULL;
120}
121