9979c1dfc7e51198afebf7bbfdaa3e2af26a9279
[framework/uifw/xorg/lib/libxau.git] / AuRead.c
1 /* $Xorg: AuRead.c,v 1.4 2001/02/09 02:03:42 xorgcvs Exp $ */
2
3 /*
4
5 Copyright 1988, 1998  The Open Group
6
7 Permission to use, copy, modify, distribute, and sell this software and its
8 documentation for any purpose is hereby granted without fee, provided that
9 the above copyright notice appear in all copies and that both that
10 copyright notice and this permission notice appear in supporting
11 documentation.
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 Except as contained in this notice, the name of The Open Group shall not be
24 used in advertising or otherwise to promote the sale, use or other dealings
25 in 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
36 static int
37 read_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
47 static int
48 read_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
72 Xauth *
73 XauReadAuth (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 }