blob: a95403033006de7a967c1b5996f710bf7142041f [file] [log] [blame]
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * blob.c: Blob support by Yasuhiro Matsumoto
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18/*
19 * Allocate an empty blob.
20 * Caller should take care of the reference count.
21 */
22 blob_T *
23blob_alloc(void)
24{
25 blob_T *blob = (blob_T *)alloc_clear(sizeof(blob_T));
26
27 if (blob != NULL)
28 ga_init2(&blob->bv_ga, 1, 100);
29 return blob;
30}
31
32/*
33 * Allocate an empty blob for a return value, with reference count set.
34 * Returns OK or FAIL.
35 */
36 int
37rettv_blob_alloc(typval_T *rettv)
38{
39 blob_T *b = blob_alloc();
40
41 if (b == NULL)
42 return FAIL;
43
44 rettv_blob_set(rettv, b);
45 return OK;
46}
47
48/*
49 * Set a blob as the return value.
50 */
51 void
52rettv_blob_set(typval_T *rettv, blob_T *b)
53{
54 rettv->v_type = VAR_BLOB;
55 rettv->vval.v_blob = b;
56 if (b != NULL)
57 ++b->bv_refcount;
58}
59
Bram Moolenaardd29ea12019-01-23 21:56:21 +010060 int
61blob_copy(typval_T *from, typval_T *to)
62{
63 int ret = OK;
64
65 to->v_type = VAR_BLOB;
66 if (from->vval.v_blob == NULL)
67 to->vval.v_blob = NULL;
68 else if (rettv_blob_alloc(to) == FAIL)
69 ret = FAIL;
70 else
71 {
72 int len = from->vval.v_blob->bv_ga.ga_len;
73
74 if (len > 0)
75 to->vval.v_blob->bv_ga.ga_data =
76 vim_memsave(from->vval.v_blob->bv_ga.ga_data, len);
77 to->vval.v_blob->bv_ga.ga_len = len;
78 }
79 return ret;
80}
81
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010082 void
83blob_free(blob_T *b)
84{
85 ga_clear(&b->bv_ga);
86 vim_free(b);
87}
88
89/*
90 * Unreference a blob: decrement the reference count and free it when it
91 * becomes zero.
92 */
93 void
94blob_unref(blob_T *b)
95{
96 if (b != NULL && --b->bv_refcount <= 0)
97 blob_free(b);
98}
99
100/*
101 * Get the length of data.
102 */
103 long
104blob_len(blob_T *b)
105{
106 if (b == NULL)
107 return 0L;
108 return b->bv_ga.ga_len;
109}
110
111/*
112 * Get byte "idx" in blob "b".
113 * Caller must check that "idx" is valid.
114 */
115 char_u
116blob_get(blob_T *b, int idx)
117{
118 return ((char_u*)b->bv_ga.ga_data)[idx];
119}
120
121/*
122 * Store one byte "c" in blob "b" at "idx".
123 * Caller must make sure that "idx" is valid.
124 */
125 void
126blob_set(blob_T *b, int idx, char_u c)
127{
128 ((char_u*)b->bv_ga.ga_data)[idx] = c;
129}
130
131/*
132 * Return TRUE when two blobs have exactly the same values.
133 */
134 int
135blob_equal(
136 blob_T *b1,
137 blob_T *b2)
138{
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100139 int i;
140 int len1 = blob_len(b1);
141 int len2 = blob_len(b2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100142
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100143 // empty and NULL are considered the same
144 if (len1 == 0 && len2 == 0)
145 return TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100146 if (b1 == b2)
147 return TRUE;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100148 if (len1 != len2)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100149 return FALSE;
150
151 for (i = 0; i < b1->bv_ga.ga_len; i++)
152 if (blob_get(b1, i) != blob_get(b2, i)) return FALSE;
153 return TRUE;
154}
155
156/*
157 * Read "blob" from file "fd".
158 * Return OK or FAIL.
159 */
160 int
161read_blob(FILE *fd, blob_T *blob)
162{
163 struct stat st;
164
165 if (fstat(fileno(fd), &st) < 0)
166 return FAIL;
167 if (ga_grow(&blob->bv_ga, st.st_size) == FAIL)
168 return FAIL;
169 blob->bv_ga.ga_len = st.st_size;
170 if (fread(blob->bv_ga.ga_data, 1, blob->bv_ga.ga_len, fd)
171 < (size_t)blob->bv_ga.ga_len)
172 return FAIL;
173 return OK;
174}
175
176/*
177 * Write "blob" to file "fd".
178 * Return OK or FAIL.
179 */
180 int
181write_blob(FILE *fd, blob_T *blob)
182{
183 if (fwrite(blob->bv_ga.ga_data, 1, blob->bv_ga.ga_len, fd)
184 < (size_t)blob->bv_ga.ga_len)
185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100186 emsg(_(e_write));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100187 return FAIL;
188 }
189 return OK;
190}
191
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100192/*
Bram Moolenaar4131fd52019-01-17 16:32:53 +0100193 * Convert a blob to a readable form: "0z00112233.44556677.8899"
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100194 */
195 char_u *
196blob2string(blob_T *blob, char_u **tofree, char_u *numbuf)
197{
198 int i;
199 garray_T ga;
200
201 if (blob == NULL)
202 {
203 *tofree = NULL;
Bram Moolenaar4131fd52019-01-17 16:32:53 +0100204 return (char_u *)"0z";
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100205 }
206
207 // Store bytes in the growarray.
208 ga_init2(&ga, 1, 4000);
Bram Moolenaar4131fd52019-01-17 16:32:53 +0100209 ga_concat(&ga, (char_u *)"0z");
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100210 for (i = 0; i < blob_len(blob); i++)
211 {
Bram Moolenaar4131fd52019-01-17 16:32:53 +0100212 if (i > 0 && (i & 3) == 0)
213 ga_concat(&ga, (char_u *)".");
214 vim_snprintf((char *)numbuf, NUMBUFLEN, "%02X", (int)blob_get(blob, i));
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100215 ga_concat(&ga, numbuf);
216 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100217 *tofree = ga.ga_data;
218 return *tofree;
219}
220
221/*
222 * Convert a string variable, in the format of blob2string(), to a blob.
223 * Return NULL when conversion failed.
224 */
225 blob_T *
226string2blob(char_u *str)
227{
228 blob_T *blob = blob_alloc();
229 char_u *s = str;
230
Bram Moolenaar4131fd52019-01-17 16:32:53 +0100231 if (s[0] != '0' || (s[1] != 'z' && s[1] != 'Z'))
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100232 goto failed;
Bram Moolenaar4131fd52019-01-17 16:32:53 +0100233 s += 2;
234 while (vim_isxdigit(*s))
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100235 {
Bram Moolenaar4131fd52019-01-17 16:32:53 +0100236 if (!vim_isxdigit(s[1]))
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100237 goto failed;
Bram Moolenaar4131fd52019-01-17 16:32:53 +0100238 ga_append(&blob->bv_ga, (hex2nr(s[0]) << 4) + hex2nr(s[1]));
239 s += 2;
240 if (*s == '.' && vim_isxdigit(s[1]))
241 ++s;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100242 }
Bram Moolenaar4131fd52019-01-17 16:32:53 +0100243 if (*skipwhite(s) != NUL)
244 goto failed; // text after final digit
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +0100245
246 ++blob->bv_refcount;
247 return blob;
248
249failed:
250 blob_free(blob);
251 return NULL;
252}
253
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100254#endif /* defined(FEAT_EVAL) */