Don't cast the return from realloc()
[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 = 0;
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             result = realloc(result, l + i + 1);
97         else
98             result = (unsigned char *)malloc(i + 1);
99         if (!result)
100             return ENOMEM;
101         memcpy(result + l, buf, i);
102         result[l + i] = 0;
103         l += i;
104         if (i < BUFSIZE)
105             break;
106     }
107     *val = result;
108     return 0;
109 }
110
111
112 afs_uint32
113 WriteByte(XFILE * X, unsigned char val)
114 {
115     return xfwrite(X, &val, 1);
116 }
117
118 afs_uint32
119 WriteInt16(XFILE * X, afs_uint16 val)
120 {
121     val = htons(val);
122     return xfwrite(X, &val, 2);
123 }
124
125 afs_uint32
126 WriteInt32(XFILE * X, afs_uint32 val)
127 {
128     val = htonl(val);
129     return xfwrite(X, &val, 4);
130 }
131
132 afs_uint32
133 WriteString(XFILE * X, unsigned char *str)
134 {
135     int len = strlen((char *)str) + 1;
136     return xfwrite(X, str, len);
137 }
138
139 afs_uint32
140 WriteTagByte(XFILE * X, unsigned char tag, unsigned char val)
141 {
142     char buffer[2];
143     buffer[0] = tag;
144     buffer[1] = val;
145     return xfwrite(X, buffer, 2);
146 }
147
148 afs_uint32
149 WriteTagInt16(XFILE * X, unsigned char tag, afs_uint16 val)
150 {
151     char buffer[3];
152     buffer[0] = tag;
153     buffer[1] = (val & 0xff00) >> 8;
154     buffer[2] = val & 0xff;
155     return xfwrite(X, buffer, 3);
156 }
157
158 afs_uint32
159 WriteTagInt32(XFILE * X, unsigned char tag, afs_uint32 val)
160 {
161     char buffer[5];
162     buffer[0] = tag;
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);
168 }
169
170 afs_uint32
171 WriteTagInt32Pair(XFILE * X, unsigned char tag, afs_uint32 val1,
172                   afs_uint32 val2)
173 {
174     char buffer[9];
175     buffer[0] = tag;
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);
185 }