AuRead.c revision ae545d91
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);
61ae545d91Smrg#elif HAVE_EXPLICIT_MEMSET
62ae545d91Smrg	    explicit_memset (data, 0, len);
6395b7a5c8Smrg#else
6427702724Smrg	    bzero (data, len);
6595b7a5c8Smrg#endif
6627702724Smrg	    free (data);
6727702724Smrg	    return 0;
6827702724Smrg    	}
6927702724Smrg    }
7027702724Smrg    *stringp = data;
7127702724Smrg    *countp = len;
7227702724Smrg    return 1;
7327702724Smrg}
7427702724Smrg
7527702724SmrgXauth *
7627702724SmrgXauReadAuth (FILE *auth_file)
7727702724Smrg{
7895b7a5c8Smrg    Xauth   local = { 0, 0, NULL, 0, NULL, 0, NULL, 0, NULL };
7927702724Smrg    Xauth   *ret;
8027702724Smrg
8195b7a5c8Smrg    if (read_short (&local.family, auth_file) == 0) {
8295b7a5c8Smrg	goto fail;
8395b7a5c8Smrg    }
8495b7a5c8Smrg    if (read_counted_string (&local.address_length, &local.address, auth_file)
8595b7a5c8Smrg        == 0) {
8695b7a5c8Smrg	goto fail;
8795b7a5c8Smrg    }
8895b7a5c8Smrg    if (read_counted_string (&local.number_length, &local.number, auth_file)
8995b7a5c8Smrg        == 0) {
9095b7a5c8Smrg	goto fail;
9127702724Smrg    }
9227702724Smrg    if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) {
9395b7a5c8Smrg	goto fail;
9427702724Smrg    }
9527702724Smrg    if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) {
9695b7a5c8Smrg	goto fail;
9727702724Smrg    }
9895b7a5c8Smrg    ret = malloc (sizeof (Xauth));
9995b7a5c8Smrg    if (ret == NULL) {
10095b7a5c8Smrg	goto fail;
10127702724Smrg    }
10227702724Smrg    *ret = local;
10327702724Smrg    return ret;
10495b7a5c8Smrg
10595b7a5c8Smrg  fail:
10695b7a5c8Smrg    free (local.address);
10795b7a5c8Smrg    free (local.number);
10895b7a5c8Smrg    free (local.name);
10995b7a5c8Smrg    if (local.data) {
11095b7a5c8Smrg#ifdef HAVE_EXPLICIT_BZERO
11195b7a5c8Smrg	explicit_bzero (local.data, local.data_length);
112ae545d91Smrg#elif HAVE_EXPLICIT_MEMSET
113ae545d91Smrg	explicit_memset (local.data, 0, local.data_length);
11495b7a5c8Smrg#else
11595b7a5c8Smrg	bzero (local.data, local.data_length);
11695b7a5c8Smrg#endif
11795b7a5c8Smrg	free (local.data);
11895b7a5c8Smrg    }
11995b7a5c8Smrg    return NULL;
12027702724Smrg}
121