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
= 0;
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 */
96 result
= realloc(result
, l
+ i
+ 1);
98 result
= (unsigned char *)malloc(i
+ 1);
101 memcpy(result
+ l
, buf
, i
);
113 WriteByte(XFILE
* X
, unsigned char val
)
115 return xfwrite(X
, &val
, 1);
119 WriteInt16(XFILE
* X
, afs_uint16 val
)
122 return xfwrite(X
, &val
, 2);
126 WriteInt32(XFILE
* X
, afs_uint32 val
)
129 return xfwrite(X
, &val
, 4);
133 WriteString(XFILE
* X
, unsigned char *str
)
135 int len
= strlen((char *)str
) + 1;
136 return xfwrite(X
, str
, len
);
140 WriteTagByte(XFILE
* X
, unsigned char tag
, unsigned char val
)
145 return xfwrite(X
, buffer
, 2);
149 WriteTagInt16(XFILE
* X
, unsigned char tag
, afs_uint16 val
)
153 buffer
[1] = (val
& 0xff00) >> 8;
154 buffer
[2] = val
& 0xff;
155 return xfwrite(X
, buffer
, 3);
159 WriteTagInt32(XFILE
* X
, unsigned char tag
, afs_uint32 val
)
163 buffer
[1] = (val
& 0xff000000) >> 24;
164 buffer
[2] = (val
& 0xff0000) >> 16;
165 buffer
[3] = (val
& 0xff00) >> 8;
166 buffer
[4] = val
& 0xff;
167 return xfwrite(X
, buffer
, 5);
171 WriteTagInt32Pair(XFILE
* X
, unsigned char tag
, afs_uint32 val1
,
176 buffer
[1] = (val1
& 0xff000000) >> 24;
177 buffer
[2] = (val1
& 0xff0000) >> 16;
178 buffer
[3] = (val1
& 0xff00) >> 8;
179 buffer
[4] = val1
& 0xff;
180 buffer
[5] = (val2
& 0xff000000) >> 24;
181 buffer
[6] = (val2
& 0xff0000) >> 16;
182 buffer
[7] = (val2
& 0xff00) >> 8;
183 buffer
[8] = val2
& 0xff;
184 return xfwrite(X
, buffer
, 9);