AuRead.c revision 95b7a5c8
127702724Smrg/*
227702724Smrg
327702724SmrgCopyright 1988, 1998  The Open Group
427702724Smrg
527702724SmrgPermission to use, copy, modify, distribute, and sell this software and its
627702724Smrgdocumentation for any purpose is hereby granted without fee, provided that
727702724Smrgthe above copyright notice appear in all copies and that both that
827702724Smrgcopyright notice and this permission notice appear in supporting
927702724Smrgdocumentation.
1027702724Smrg
1127702724SmrgThe above copyright notice and this permission notice shall be included in
1227702724Smrgall copies or substantial portions of the Software.
1327702724Smrg
1427702724SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1527702724SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1627702724SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1727702724SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1827702724SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1927702724SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2027702724Smrg
2127702724SmrgExcept as contained in this notice, the name of The Open Group shall not be
2227702724Smrgused in advertising or otherwise to promote the sale, use or other dealings
2327702724Smrgin this Software without prior written authorization from The Open Group.
2427702724Smrg
2527702724Smrg*/
2627702724Smrg
2727702724Smrg#ifdef HAVE_CONFIG_H
2827702724Smrg#include <config.h>
2927702724Smrg#endif
3027702724Smrg#include <X11/Xauth.h>
3127702724Smrg#include <stdlib.h>
3227702724Smrg
3327702724Smrgstatic int
3427702724Smrgread_short (unsigned short *shortp, FILE *file)
3527702724Smrg{
3627702724Smrg    unsigned char   file_short[2];
3727702724Smrg
38313a12fdSmrg    if (fread ((char *) file_short, sizeof (file_short), 1, file) != 1)
3927702724Smrg	return 0;
4027702724Smrg    *shortp = file_short[0] * 256 + file_short[1];
4127702724Smrg    return 1;
4227702724Smrg}
4327702724Smrg
4427702724Smrgstatic int
4527702724Smrgread_counted_string (unsigned short *countp, char **stringp, FILE *file)
4627702724Smrg{
4727702724Smrg    unsigned short  len;
4827702724Smrg    char	    *data;
4927702724Smrg
5027702724Smrg    if (read_short (&len, file) == 0)
5127702724Smrg	return 0;
5227702724Smrg    if (len == 0) {
5327702724Smrg	data = NULL;
5427702724Smrg    } else {
5527702724Smrg    	data = malloc ((unsigned) len);
5627702724Smrg    	if (!data)
5727702724Smrg	    return 0;
58313a12fdSmrg	if (fread (data, sizeof (char), len, file) != len) {
5995b7a5c8Smrg#ifdef HAVE_EXPLICIT_BZERO
6095b7a5c8Smrg	    explicit_bzero (data, len);
6195b7a5c8Smrg#else
6227702724Smrg	    bzero (data, len);
6395b7a5c8Smrg#endif
6427702724Smrg	    free (data);
6527702724Smrg	    return 0;
6627702724Smrg    	}
6727702724Smrg    }
6827702724Smrg    *stringp = data;
6927702724Smrg    *countp = len;
7027702724Smrg    return 1;
7127702724Smrg}
7227702724Smrg
7327702724SmrgXauth *
7427702724SmrgXauReadAuth (FILE *auth_file)
7527702724Smrg{
7695b7a5c8Smrg    Xauth   local = { 0, 0, NULL, 0, NULL, 0, NULL, 0, NULL };
7727702724Smrg    Xauth   *ret;
7827702724Smrg
7995b7a5c8Smrg    if (read_short (&local.family, auth_file) == 0) {
8095b7a5c8Smrg	goto fail;
8195b7a5c8Smrg    }
8295b7a5c8Smrg    if (read_counted_string (&local.address_length, &local.address, auth_file)
8395b7a5c8Smrg        == 0) {
8495b7a5c8Smrg	goto fail;
8595b7a5c8Smrg    }
8695b7a5c8Smrg    if (read_counted_string (&local.number_length, &local.number, auth_file)
8795b7a5c8Smrg        == 0) {
8895b7a5c8Smrg	goto fail;
8927702724Smrg    }
9027702724Smrg    if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) {
9195b7a5c8Smrg	goto fail;
9227702724Smrg    }
9327702724Smrg    if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) {
9495b7a5c8Smrg	goto fail;
9527702724Smrg    }
9695b7a5c8Smrg    ret = malloc (sizeof (Xauth));
9795b7a5c8Smrg    if (ret == NULL) {
9895b7a5c8Smrg	goto fail;
9927702724Smrg    }
10027702724Smrg    *ret = local;
10127702724Smrg    return ret;
10295b7a5c8Smrg
10395b7a5c8Smrg  fail:
10495b7a5c8Smrg    free (local.address);
10595b7a5c8Smrg    free (local.number);
10695b7a5c8Smrg    free (local.name);
10795b7a5c8Smrg    if (local.data) {
10895b7a5c8Smrg#ifdef HAVE_EXPLICIT_BZERO
10995b7a5c8Smrg	explicit_bzero (local.data, local.data_length);
11095b7a5c8Smrg#else
11195b7a5c8Smrg	bzero (local.data, local.data_length);
11295b7a5c8Smrg#endif
11395b7a5c8Smrg	free (local.data);
11495b7a5c8Smrg    }
11595b7a5c8Smrg    return NULL;
11627702724Smrg}
117