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