3 * dumpscan - routines for scanning and manipulating AFS volume dumps
5 * Copyright (c) 1998 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software_Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie Mellon
26 * the rights to redistribute these changes.
29 /* primitive.c - Routines for reading and writing low-level things */
31 #include <sys/types.h>
32 #include <netinet/in.h>
43 ReadByte(XFILE * X, unsigned char *val)
45 return xfread(X, val, 1);
49 ReadInt16(XFILE * X, afs_uint16 * val)
53 if ((r = xfread(X, val, 2)))
60 ReadInt32(XFILE * X, afs_uint32 * val)
64 if ((r = xfread(X, val, 4)))
70 /* Read in a NUL-terminated string. This method is kind of messy, but
71 * has the advantage that it reads the data stream only once, doesn't
72 * read anything extra, and never has to seek on the data stream.
75 ReadString(XFILE * X, unsigned char **val)
77 static unsigned char buf[BUFSIZE];
78 unsigned char *result = NULL, *old_result = NULL;
84 for (i = 0; i < BUFSIZE; i++) {
85 r = ReadByte(X, buf + i);
94 /* iff we found a null, i < BUFSIZE and buf[i] holds the NUL */
97 result = realloc(old_result, l + i + 1);
98 if (!result) /* realloc failed, manually free old mem */
101 result = (unsigned char *)malloc(i + 1);
104 memcpy(result + l, buf, i);
116 WriteByte(XFILE * X, unsigned char val)
118 return xfwrite(X, &val, 1);
122 WriteInt16(XFILE * X, afs_uint16 val)
125 return xfwrite(X, &val, 2);
129 WriteInt32(XFILE * X, afs_uint32 val)
132 return xfwrite(X, &val, 4);
136 WriteString(XFILE * X, unsigned char *str)
138 int len = strlen((char *)str) + 1;
139 return xfwrite(X, str, len);
143 WriteTagByte(XFILE * X, unsigned char tag, unsigned char val)
148 return xfwrite(X, buffer, 2);
152 WriteTagInt16(XFILE * X, unsigned char tag, afs_uint16 val)
156 buffer[1] = (val & 0xff00) >> 8;
157 buffer[2] = val & 0xff;
158 return xfwrite(X, buffer, 3);
162 WriteTagInt32(XFILE * X, unsigned char tag, afs_uint32 val)
166 buffer[1] = (val & 0xff000000) >> 24;
167 buffer[2] = (val & 0xff0000) >> 16;
168 buffer[3] = (val & 0xff00) >> 8;
169 buffer[4] = val & 0xff;
170 return xfwrite(X, buffer, 5);
174 WriteTagInt32Pair(XFILE * X, unsigned char tag, afs_uint32 val1,
179 buffer[1] = (val1 & 0xff000000) >> 24;
180 buffer[2] = (val1 & 0xff0000) >> 16;
181 buffer[3] = (val1 & 0xff00) >> 8;
182 buffer[4] = val1 & 0xff;
183 buffer[5] = (val2 & 0xff000000) >> 24;
184 buffer[6] = (val2 & 0xff0000) >> 16;
185 buffer[7] = (val2 & 0xff00) >> 8;
186 buffer[8] = val2 & 0xff;
187 return xfwrite(X, buffer, 9);