Blame AuRead.c

Packit 56e0ee
/*
Packit 56e0ee
Packit 56e0ee
Copyright 1988, 1998  The Open Group
Packit 56e0ee
Packit 56e0ee
Permission to use, copy, modify, distribute, and sell this software and its
Packit 56e0ee
documentation for any purpose is hereby granted without fee, provided that
Packit 56e0ee
the above copyright notice appear in all copies and that both that
Packit 56e0ee
copyright notice and this permission notice appear in supporting
Packit 56e0ee
documentation.
Packit 56e0ee
Packit 56e0ee
The above copyright notice and this permission notice shall be included in
Packit 56e0ee
all copies or substantial portions of the Software.
Packit 56e0ee
Packit 56e0ee
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 56e0ee
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 56e0ee
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
Packit 56e0ee
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit 56e0ee
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 56e0ee
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit 56e0ee
Packit 56e0ee
Except as contained in this notice, the name of The Open Group shall not be
Packit 56e0ee
used in advertising or otherwise to promote the sale, use or other dealings
Packit 56e0ee
in this Software without prior written authorization from The Open Group.
Packit 56e0ee
Packit 56e0ee
*/
Packit 56e0ee
Packit 56e0ee
#ifdef HAVE_CONFIG_H
Packit 56e0ee
#include <config.h>
Packit 56e0ee
#endif
Packit 56e0ee
#include <X11/Xauth.h>
Packit 56e0ee
#include <stdlib.h>
Packit 56e0ee
Packit 56e0ee
static int
Packit 56e0ee
read_short (unsigned short *shortp, FILE *file)
Packit 56e0ee
{
Packit 56e0ee
    unsigned char   file_short[2];
Packit 56e0ee
Packit 56e0ee
    if (fread ((char *) file_short, sizeof (file_short), 1, file) != 1)
Packit 56e0ee
	return 0;
Packit 56e0ee
    *shortp = file_short[0] * 256 + file_short[1];
Packit 56e0ee
    return 1;
Packit 56e0ee
}
Packit 56e0ee
Packit 56e0ee
static int
Packit 56e0ee
read_counted_string (unsigned short *countp, char **stringp, FILE *file)
Packit 56e0ee
{
Packit 56e0ee
    unsigned short  len;
Packit 56e0ee
    char	    *data;
Packit 56e0ee
Packit 56e0ee
    if (read_short (&len, file) == 0)
Packit 56e0ee
	return 0;
Packit 56e0ee
    if (len == 0) {
Packit 56e0ee
	data = NULL;
Packit 56e0ee
    } else {
Packit 56e0ee
    	data = malloc ((unsigned) len);
Packit 56e0ee
    	if (!data)
Packit 56e0ee
	    return 0;
Packit 56e0ee
	if (fread (data, sizeof (char), len, file) != len) {
Packit 56e0ee
	    bzero (data, len);
Packit 56e0ee
	    free (data);
Packit 56e0ee
	    return 0;
Packit 56e0ee
    	}
Packit 56e0ee
    }
Packit 56e0ee
    *stringp = data;
Packit 56e0ee
    *countp = len;
Packit 56e0ee
    return 1;
Packit 56e0ee
}
Packit 56e0ee
Packit 56e0ee
Xauth *
Packit 56e0ee
XauReadAuth (FILE *auth_file)
Packit 56e0ee
{
Packit 56e0ee
    Xauth   local;
Packit 56e0ee
    Xauth   *ret;
Packit 56e0ee
Packit 56e0ee
    if (read_short (&local.family, auth_file) == 0)
Packit 56e0ee
	return NULL;
Packit 56e0ee
    if (read_counted_string (&local.address_length, &local.address, auth_file) == 0)
Packit 56e0ee
	return NULL;
Packit 56e0ee
    if (read_counted_string (&local.number_length, &local.number, auth_file) == 0) {
Packit 56e0ee
	free (local.address);
Packit 56e0ee
	return NULL;
Packit 56e0ee
    }
Packit 56e0ee
    if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) {
Packit 56e0ee
	free (local.address);
Packit 56e0ee
	free (local.number);
Packit 56e0ee
	return NULL;
Packit 56e0ee
    }
Packit 56e0ee
    if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) {
Packit 56e0ee
	free (local.address);
Packit 56e0ee
	free (local.number);
Packit 56e0ee
	free (local.name);
Packit 56e0ee
	return NULL;
Packit 56e0ee
    }
Packit 56e0ee
    ret = (Xauth *) malloc (sizeof (Xauth));
Packit 56e0ee
    if (!ret) {
Packit 56e0ee
	free (local.address);
Packit 56e0ee
	free (local.number);
Packit 56e0ee
	free (local.name);
Packit 56e0ee
	if (local.data) {
Packit 56e0ee
	    bzero (local.data, local.data_length);
Packit 56e0ee
	    free (local.data);
Packit 56e0ee
	}
Packit 56e0ee
	return NULL;
Packit 56e0ee
    }
Packit 56e0ee
    *ret = local;
Packit 56e0ee
    return ret;
Packit 56e0ee
}