/src/gdal/netcdf-c-4.7.4/libdispatch/ncbytes.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc. |
2 | | See the COPYRIGHT file for more information. */ |
3 | | |
4 | | #include <stdlib.h> |
5 | | #include <stdio.h> |
6 | | #include <string.h> |
7 | | |
8 | | #include "ncbytes.h" |
9 | | |
10 | | #ifndef TRUE |
11 | 144 | #define TRUE 1 |
12 | | #endif |
13 | | #ifndef FALSE |
14 | 0 | #define FALSE 0 |
15 | | #endif |
16 | | |
17 | 9 | #define DEFAULTALLOC 1024 |
18 | | #define ALLOCINCR 1024 |
19 | | |
20 | | #define NCBYTESDEBUG 1 |
21 | | |
22 | | static int |
23 | | ncbytesfail(void) |
24 | 0 | { |
25 | 0 | fflush(stdout); |
26 | 0 | fprintf(stderr,"bytebuffer failure\n"); |
27 | 0 | fflush(stderr); |
28 | 0 | #ifdef NCBYTESDEBUG |
29 | 0 | abort(); |
30 | 0 | #endif |
31 | 0 | return FALSE; |
32 | 0 | } |
33 | | |
34 | | NCbytes* |
35 | | ncbytesnew(void) |
36 | 9 | { |
37 | 9 | NCbytes* bb = (NCbytes*)malloc(sizeof(NCbytes)); |
38 | 9 | if(bb == NULL) return (ncbytesfail(),NULL); |
39 | 9 | bb->alloc=0; |
40 | 9 | bb->length=0; |
41 | 9 | bb->content=NULL; |
42 | 9 | bb->nonextendible = 0; |
43 | 9 | return bb; |
44 | 9 | } |
45 | | |
46 | | int |
47 | | ncbytessetalloc(NCbytes* bb, unsigned long sz) |
48 | 36 | { |
49 | 36 | char* newcontent; |
50 | 36 | if(bb == NULL) return ncbytesfail(); |
51 | 36 | if(sz == 0) {sz = (bb->alloc?2*bb->alloc:DEFAULTALLOC);} |
52 | 36 | if(bb->alloc >= sz) return TRUE; |
53 | 9 | if(bb->nonextendible) return ncbytesfail(); |
54 | 9 | newcontent=(char*)calloc(sz,sizeof(char)); |
55 | 9 | if(newcontent == NULL) return FALSE; |
56 | 9 | if(bb->alloc > 0 && bb->length > 0 && bb->content != NULL) { |
57 | 0 | memcpy((void*)newcontent,(void*)bb->content,sizeof(char)*bb->length); |
58 | 0 | } |
59 | 9 | if(bb->content != NULL) free(bb->content); |
60 | 9 | bb->content=newcontent; |
61 | 9 | bb->alloc=sz; |
62 | 9 | return TRUE; |
63 | 9 | } |
64 | | |
65 | | void |
66 | | ncbytesfree(NCbytes* bb) |
67 | 9 | { |
68 | 9 | if(bb == NULL) return; |
69 | 9 | if(!bb->nonextendible && bb->content != NULL) free(bb->content); |
70 | 9 | free(bb); |
71 | 9 | } |
72 | | |
73 | | int |
74 | | ncbytessetlength(NCbytes* bb, unsigned long sz) |
75 | 0 | { |
76 | 0 | if(bb == NULL) return ncbytesfail(); |
77 | 0 | if(bb->length < sz) { |
78 | 0 | if(sz > bb->alloc) {if(!ncbytessetalloc(bb,sz)) return ncbytesfail();} |
79 | 0 | } |
80 | 0 | bb->length = sz; |
81 | 0 | return TRUE; |
82 | 0 | } |
83 | | |
84 | | int |
85 | | ncbytesfill(NCbytes* bb, char fill) |
86 | 0 | { |
87 | 0 | unsigned long i; |
88 | 0 | if(bb == NULL) return ncbytesfail(); |
89 | 0 | for(i=0;i<bb->length;i++) bb->content[i] = fill; |
90 | 0 | return TRUE; |
91 | 0 | } |
92 | | |
93 | | int |
94 | | ncbytesget(NCbytes* bb, unsigned long index) |
95 | 0 | { |
96 | 0 | if(bb == NULL) return -1; |
97 | 0 | if(index >= bb->length) return -1; |
98 | 0 | return bb->content[index]; |
99 | 0 | } |
100 | | |
101 | | int |
102 | | ncbytesset(NCbytes* bb, unsigned long index, char elem) |
103 | 0 | { |
104 | 0 | if(bb == NULL) return ncbytesfail(); |
105 | 0 | if(index >= bb->length) return ncbytesfail(); |
106 | 0 | bb->content[index] = elem; |
107 | 0 | return TRUE; |
108 | 0 | } |
109 | | |
110 | | int |
111 | | ncbytesappend(NCbytes* bb, char elem) |
112 | 27 | { |
113 | 27 | if(bb == NULL) return ncbytesfail(); |
114 | | /* We need space for the char + null */ |
115 | 27 | ncbytessetalloc(bb,bb->length+2); |
116 | 27 | bb->content[bb->length] = (char)(elem & 0xFF); |
117 | 27 | bb->length++; |
118 | 27 | bb->content[bb->length] = '\0'; |
119 | 27 | return TRUE; |
120 | 27 | } |
121 | | |
122 | | /* This assumes s is a null terminated string*/ |
123 | | int |
124 | | ncbytescat(NCbytes* bb, const char* s) |
125 | 81 | { |
126 | 81 | if(s == NULL) { |
127 | 0 | return 1; |
128 | 0 | } |
129 | 81 | ncbytesappendn(bb,(void*)s,strlen(s)+1); /* include trailing null*/ |
130 | | /* back up over the trailing null*/ |
131 | 81 | if(bb->length == 0) return ncbytesfail(); |
132 | 81 | bb->length--; |
133 | 81 | return 1; |
134 | 81 | } |
135 | | |
136 | | int |
137 | | ncbytesappendn(NCbytes* bb, const void* elem, unsigned long n) |
138 | 81 | { |
139 | 81 | if(bb == NULL || elem == NULL) return ncbytesfail(); |
140 | 81 | if(n == 0) {n = strlen((char*)elem);} |
141 | 90 | while(!ncbytesavail(bb,n+1)) { |
142 | 9 | if(!ncbytessetalloc(bb,0)) return ncbytesfail(); |
143 | 9 | } |
144 | 81 | memcpy((void*)&bb->content[bb->length],(void*)elem,n); |
145 | 81 | bb->length += n; |
146 | 81 | bb->content[bb->length] = '\0'; |
147 | 81 | return TRUE; |
148 | 81 | } |
149 | | |
150 | | int |
151 | | ncbytesprepend(NCbytes* bb, char elem) |
152 | 0 | { |
153 | 0 | int i; /* do not make unsigned */ |
154 | 0 | if(bb == NULL) return ncbytesfail(); |
155 | 0 | if(bb->length >= bb->alloc) if(!ncbytessetalloc(bb,0)) return ncbytesfail(); |
156 | | /* could we trust memcpy? instead */ |
157 | 0 | for(i=(int)bb->alloc;i>=1;i--) {bb->content[i]=bb->content[i-1];} |
158 | 0 | bb->content[0] = elem; |
159 | 0 | bb->length++; |
160 | 0 | return TRUE; |
161 | 0 | } |
162 | | |
163 | | char* |
164 | | ncbytesdup(NCbytes* bb) |
165 | 0 | { |
166 | 0 | char* result = (char*)malloc(bb->length+1); |
167 | 0 | memcpy((void*)result,(const void*)bb->content,bb->length); |
168 | 0 | result[bb->length] = '\0'; /* just in case it is a string*/ |
169 | 0 | return result; |
170 | 0 | } |
171 | | |
172 | | char* |
173 | | ncbytesextract(NCbytes* bb) |
174 | 9 | { |
175 | 9 | char* result = bb->content; |
176 | 9 | bb->alloc = 0; |
177 | 9 | bb->length = 0; |
178 | 9 | bb->content = NULL; |
179 | 9 | return result; |
180 | 9 | } |
181 | | |
182 | | int |
183 | | ncbytessetcontents(NCbytes* bb, char* contents, unsigned long alloc) |
184 | 0 | { |
185 | 0 | if(bb == NULL) return ncbytesfail(); |
186 | 0 | ncbytesclear(bb); |
187 | 0 | if(!bb->nonextendible && bb->content != NULL) free(bb->content); |
188 | 0 | bb->content = contents; |
189 | 0 | bb->length = 0; |
190 | 0 | bb->alloc = alloc; |
191 | 0 | bb->nonextendible = 1; |
192 | 0 | return 1; |
193 | 0 | } |
194 | | |
195 | | /* Null terminate the byte string without extending its length */ |
196 | | int |
197 | | ncbytesnull(NCbytes* bb) |
198 | 9 | { |
199 | 9 | ncbytesappend(bb,'\0'); |
200 | 9 | bb->length--; |
201 | 9 | return 1; |
202 | 9 | } |
203 | | |
204 | | /* Remove char at position i */ |
205 | | int |
206 | | ncbytesremove(NCbytes* bb, unsigned long pos) |
207 | 0 | { |
208 | 0 | if(bb == NULL) return ncbytesfail(); |
209 | 0 | if(bb->length <= pos) return ncbytesfail(); |
210 | 0 | if(pos < (bb->length - 1)) { |
211 | 0 | int copylen = (bb->length - pos) - 1; |
212 | 0 | memmove(bb->content+pos,bb->content+pos+1,copylen); |
213 | 0 | } |
214 | 0 | bb->length--; |
215 | 0 | return TRUE; |
216 | 0 | } |