AuRead.c revision 95b7a5c8
1/* 2 3Copyright 1988, 1998 The Open Group 4 5Permission to use, copy, modify, distribute, and sell this software and its 6documentation for any purpose is hereby granted without fee, provided that 7the above copyright notice appear in all copies and that both that 8copyright notice and this permission notice appear in supporting 9documentation. 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 21Except as contained in this notice, the name of The Open Group shall not be 22used in advertising or otherwise to promote the sale, use or other dealings 23in this Software without prior written authorization from The Open Group. 24 25*/ 26 27#ifdef HAVE_CONFIG_H 28#include <config.h> 29#endif 30#include <X11/Xauth.h> 31#include <stdlib.h> 32 33static int 34read_short (unsigned short *shortp, FILE *file) 35{ 36 unsigned char file_short[2]; 37 38 if (fread ((char *) file_short, sizeof (file_short), 1, file) != 1) 39 return 0; 40 *shortp = file_short[0] * 256 + file_short[1]; 41 return 1; 42} 43 44static int 45read_counted_string (unsigned short *countp, char **stringp, FILE *file) 46{ 47 unsigned short len; 48 char *data; 49 50 if (read_short (&len, file) == 0) 51 return 0; 52 if (len == 0) { 53 data = NULL; 54 } else { 55 data = malloc ((unsigned) len); 56 if (!data) 57 return 0; 58 if (fread (data, sizeof (char), len, file) != len) { 59#ifdef HAVE_EXPLICIT_BZERO 60 explicit_bzero (data, len); 61#else 62 bzero (data, len); 63#endif 64 free (data); 65 return 0; 66 } 67 } 68 *stringp = data; 69 *countp = len; 70 return 1; 71} 72 73Xauth * 74XauReadAuth (FILE *auth_file) 75{ 76 Xauth local = { 0, 0, NULL, 0, NULL, 0, NULL, 0, NULL }; 77 Xauth *ret; 78 79 if (read_short (&local.family, auth_file) == 0) { 80 goto fail; 81 } 82 if (read_counted_string (&local.address_length, &local.address, auth_file) 83 == 0) { 84 goto fail; 85 } 86 if (read_counted_string (&local.number_length, &local.number, auth_file) 87 == 0) { 88 goto fail; 89 } 90 if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) { 91 goto fail; 92 } 93 if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) { 94 goto fail; 95 } 96 ret = malloc (sizeof (Xauth)); 97 if (ret == NULL) { 98 goto fail; 99 } 100 *ret = local; 101 return ret; 102 103 fail: 104 free (local.address); 105 free (local.number); 106 free (local.name); 107 if (local.data) { 108#ifdef HAVE_EXPLICIT_BZERO 109 explicit_bzero (local.data, local.data_length); 110#else 111 bzero (local.data, local.data_length); 112#endif 113 free (local.data); 114 } 115 return NULL; 116} 117