venus: Remove dedebug
[openafs.git] / src / tools / dumpscan / primitive.c
1 /*
2  * CMUCS AFStools
3  * dumpscan - routines for scanning and manipulating AFS volume dumps
4  *
5  * Copyright (c) 1998 Carnegie Mellon University
6  * All Rights Reserved.
7  *
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.
13  *
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.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie Mellon
26  * the rights to redistribute these changes.
27  */
28
29 /* primitive.c - Routines for reading and writing low-level things */
30
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "dumpscan.h"
38
39 #define BUFSIZE 256
40
41
42 afs_uint32
43 ReadByte(XFILE * X, unsigned char *val)
44 {
45     return xfread(X, val, 1);
46 }
47
48 afs_uint32
49 ReadInt16(XFILE * X, afs_uint16 * val)
50 {
51     afs_uint32 r;
52
53     if ((r = xfread(X, val, 2)))
54         return r;
55     *val = ntohs(*val);
56     return 0;
57 }
58
59 afs_uint32
60 ReadInt32(XFILE * X, afs_uint32 * val)
61 {
62     afs_uint32 r;
63
64     if ((r = xfread(X, val, 4)))
65         return r;
66     *val = ntohl(*val);
67     return 0;
68 }
69
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.
73  */
74 afs_uint32
75 ReadString(XFILE * X, unsigned char **val)
76 {
77     static unsigned char buf[BUFSIZE];
78     unsigned char *result = NULL, *old_result = NULL;
79     afs_uint32 r;
80     int i, l = 0;
81
82     *val = 0;
83     for (;;) {
84         for (i = 0; i < BUFSIZE; i++) {
85             r = ReadByte(X, buf + i);
86             if (r) {
87                 if (result)
88                     free(result);
89                 return r;
90             }
91             if (!buf[i])
92                 break;
93         }
94         /* iff we found a null, i < BUFSIZE and buf[i] holds the NUL */
95         if (result) {
96             old_result = result;
97             result = realloc(old_result, l + i + 1);
98             if (!result) /* realloc failed, manually free old mem */
99                 free(old_result);
100         } else
101             result = (unsigned char *)malloc(i + 1);
102         if (!result)
103             return ENOMEM;
104         memcpy(result + l, buf, i);
105         result[l + i] = 0;
106         l += i;
107         if (i < BUFSIZE)
108             break;
109     }
110     *val = result;
111     return 0;
112 }
113
114
115 afs_uint32
116 WriteByte(XFILE * X, unsigned char val)
117 {
118     return xfwrite(X, &val, 1);
119 }
120
121 afs_uint32
122 WriteInt16(XFILE * X, afs_uint16 val)
123 {
124     val = htons(val);
125     return xfwrite(X, &val, 2);
126 }
127
128 afs_uint32
129 WriteInt32(XFILE * X, afs_uint32 val)
130 {
131     val = htonl(val);
132     return xfwrite(X, &val, 4);
133 }
134
135 afs_uint32
136 WriteString(XFILE * X, unsigned char *str)
137 {
138     int len = strlen((char *)str) + 1;
139     return xfwrite(X, str, len);
140 }
141
142 afs_uint32
143 WriteTagByte(XFILE * X, unsigned char tag, unsigned char val)
144 {
145     char buffer[2];
146     buffer[0] = tag;
147     buffer[1] = val;
148     return xfwrite(X, buffer, 2);
149 }
150
151 afs_uint32
152 WriteTagInt16(XFILE * X, unsigned char tag, afs_uint16 val)
153 {
154     char buffer[3];
155     buffer[0] = tag;
156     buffer[1] = (val & 0xff00) >> 8;
157     buffer[2] = val & 0xff;
158     return xfwrite(X, buffer, 3);
159 }
160
161 afs_uint32
162 WriteTagInt32(XFILE * X, unsigned char tag, afs_uint32 val)
163 {
164     char buffer[5];
165     buffer[0] = tag;
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);
171 }
172
173 afs_uint32
174 WriteTagInt32Pair(XFILE * X, unsigned char tag, afs_uint32 val1,
175                   afs_uint32 val2)
176 {
177     char buffer[9];
178     buffer[0] = tag;
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);
188 }