comparison src/blob.c @ 15515:99a4cc4782ac v8.1.0765

patch 8.1.0765: string format of a Blob can't be parsed back commit https://github.com/vim/vim/commit/4131fd5509b283e978e8c6161f09643b64719787 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Jan 17 16:32:53 2019 +0100 patch 8.1.0765: string format of a Blob can't be parsed back Problem: String format of a Blob can't be parsed back. Solution: Use 0z format.
author Bram Moolenaar <Bram@vim.org>
date Thu, 17 Jan 2019 16:45:06 +0100
parents 55ccc2d353bd
children c2382f0d1279
comparison
equal deleted inserted replaced
15514:c72617e8f153 15515:99a4cc4782ac
166 } 166 }
167 return OK; 167 return OK;
168 } 168 }
169 169
170 /* 170 /*
171 * Convert a blob to a readable form: "[0x11,0x34]" 171 * Convert a blob to a readable form: "0z00112233.44556677.8899"
172 */ 172 */
173 char_u * 173 char_u *
174 blob2string(blob_T *blob, char_u **tofree, char_u *numbuf) 174 blob2string(blob_T *blob, char_u **tofree, char_u *numbuf)
175 { 175 {
176 int i; 176 int i;
177 garray_T ga; 177 garray_T ga;
178 178
179 if (blob == NULL) 179 if (blob == NULL)
180 { 180 {
181 *tofree = NULL; 181 *tofree = NULL;
182 return (char_u *)"[]"; 182 return (char_u *)"0z";
183 } 183 }
184 184
185 // Store bytes in the growarray. 185 // Store bytes in the growarray.
186 ga_init2(&ga, 1, 4000); 186 ga_init2(&ga, 1, 4000);
187 ga_append(&ga, '['); 187 ga_concat(&ga, (char_u *)"0z");
188 for (i = 0; i < blob_len(blob); i++) 188 for (i = 0; i < blob_len(blob); i++)
189 { 189 {
190 if (i > 0) 190 if (i > 0 && (i & 3) == 0)
191 ga_concat(&ga, (char_u *)","); 191 ga_concat(&ga, (char_u *)".");
192 vim_snprintf((char *)numbuf, NUMBUFLEN, "0x%02X", (int)blob_get(blob, i)); 192 vim_snprintf((char *)numbuf, NUMBUFLEN, "%02X", (int)blob_get(blob, i));
193 ga_concat(&ga, numbuf); 193 ga_concat(&ga, numbuf);
194 } 194 }
195 ga_append(&ga, ']');
196 *tofree = ga.ga_data; 195 *tofree = ga.ga_data;
197 return *tofree; 196 return *tofree;
198 } 197 }
199 198
200 /* 199 /*
205 string2blob(char_u *str) 204 string2blob(char_u *str)
206 { 205 {
207 blob_T *blob = blob_alloc(); 206 blob_T *blob = blob_alloc();
208 char_u *s = str; 207 char_u *s = str;
209 208
210 if (*s != '[') 209 if (s[0] != '0' || (s[1] != 'z' && s[1] != 'Z'))
211 goto failed; 210 goto failed;
212 s = skipwhite(s + 1); 211 s += 2;
213 while (*s != ']') 212 while (vim_isxdigit(*s))
214 { 213 {
215 if (s[0] != '0' || s[1] != 'x' 214 if (!vim_isxdigit(s[1]))
216 || !vim_isxdigit(s[2]) || !vim_isxdigit(s[3]))
217 goto failed; 215 goto failed;
218 ga_append(&blob->bv_ga, (hex2nr(s[2]) << 4) + hex2nr(s[3])); 216 ga_append(&blob->bv_ga, (hex2nr(s[0]) << 4) + hex2nr(s[1]));
219 s += 4; 217 s += 2;
220 if (*s == ',') 218 if (*s == '.' && vim_isxdigit(s[1]))
221 s = skipwhite(s + 1); 219 ++s;
222 else if (*s != ']') 220 }
223 goto failed; 221 if (*skipwhite(s) != NUL)
224 } 222 goto failed; // text after final digit
225 s = skipwhite(s + 1);
226 if (*s != NUL)
227 goto failed; // text after final ']'
228 223
229 ++blob->bv_refcount; 224 ++blob->bv_refcount;
230 return blob; 225 return blob;
231 226
232 failed: 227 failed: