home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xau / AuRead.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-08  |  2.7 KB  |  101 lines

  1. /*
  2.  * Xau - X Authorization Database Library
  3.  *
  4.  * $XConsortium: AuRead.c,v 1.5 91/01/08 15:09:31 gildea Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of M.I.T. not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  M.I.T. makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * Author:  Keith Packard, MIT X Consortium
  19.  */
  20.  
  21. #include <X11/Xauth.h>
  22.  
  23. static
  24. read_short (shortp, file)
  25. unsigned short    *shortp;
  26. FILE        *file;
  27. {
  28.     unsigned char   file_short[2];
  29.  
  30.     if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
  31.     return 0;
  32.     *shortp = file_short[0] * 256 + file_short[1];
  33.     return 1;
  34. }
  35.  
  36. static
  37. read_counted_string (countp, stringp, file)
  38. unsigned short    *countp;
  39. char    **stringp;
  40. FILE    *file;
  41. {
  42.     unsigned short  len;
  43.     char        *data, *malloc ();
  44.  
  45.     if (read_short (&len, file) == 0)
  46.     return 0;
  47.     if (len == 0) {
  48.     data = 0;
  49.     } else {
  50.         data = malloc ((unsigned) len);
  51.         if (!data)
  52.         return 0;
  53.         if (fread (data, (int) sizeof (char), (int) len, file) != len) {
  54.         free (data);
  55.         return 0;
  56.         }
  57.     }
  58.     *stringp = data;
  59.     *countp = len;
  60.     return 1;
  61. }
  62.  
  63. Xauth *
  64. XauReadAuth (auth_file)
  65. FILE    *auth_file;
  66. {
  67.     Xauth   local;
  68.     Xauth   *ret;
  69.     char    *malloc ();
  70.  
  71.     if (read_short (&local.family, auth_file) == 0)
  72.     return 0;
  73.     if (read_counted_string (&local.address_length, &local.address, auth_file) == 0)
  74.     return 0;
  75.     if (read_counted_string (&local.number_length, &local.number, auth_file) == 0) {
  76.     if (local.address) free (local.address);
  77.     return 0;
  78.     }
  79.     if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) {
  80.     if (local.address) free (local.address);
  81.     if (local.number) free (local.number);
  82.     return 0;
  83.     }
  84.     if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) {
  85.     if (local.address) free (local.address);
  86.     if (local.number) free (local.number);
  87.     if (local.name) free (local.name);
  88.     return 0;
  89.     }
  90.     ret = (Xauth *) malloc (sizeof (Xauth));
  91.     if (!ret) {
  92.     if (local.address) free (local.address);
  93.     if (local.number) free (local.number);
  94.     if (local.name) free (local.name);
  95.     if (local.data) free (local.data);
  96.     return 0;
  97.     }
  98.     *ret = local;
  99.     return ret;
  100. }
  101.