Line | Count | Source |
1 | | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | | * Copyright by The HDF Group. * |
3 | | * All rights reserved. * |
4 | | * * |
5 | | * This file is part of HDF5. The full HDF5 copyright notice, including * |
6 | | * terms governing use, modification, and redistribution, is contained in * |
7 | | * the LICENSE file, which can be found at the root of the source code * |
8 | | * distribution tree, or in https://www.hdfgroup.org/licenses. * |
9 | | * If you do not have access to either file, you may request a copy from * |
10 | | * help@hdfgroup.org. * |
11 | | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
12 | | |
13 | | /****************/ |
14 | | /* Module Setup */ |
15 | | /****************/ |
16 | | |
17 | | #include "H5Rmodule.h" /* This source code file is part of the H5R module */ |
18 | | |
19 | | /***********/ |
20 | | /* Headers */ |
21 | | /***********/ |
22 | | #include "H5private.h" /* Generic Functions */ |
23 | | #include "H5CXprivate.h" /* API Contexts */ |
24 | | #include "H5Eprivate.h" /* Error handling */ |
25 | | #include "H5HGprivate.h" /* Global Heaps */ |
26 | | #include "H5Iprivate.h" /* IDs */ |
27 | | #include "H5MMprivate.h" /* Memory management */ |
28 | | #include "H5Oprivate.h" /* Object headers */ |
29 | | #include "H5Rpkg.h" /* References */ |
30 | | #include "H5Sprivate.h" /* Dataspaces */ |
31 | | |
32 | | #include "H5VLnative_private.h" /* Native VOL connector */ |
33 | | |
34 | | /****************/ |
35 | | /* Local Macros */ |
36 | | /****************/ |
37 | | |
38 | 0 | #define H5R_MAX_STRING_LEN (1 << 16) /* Max encoded string length */ |
39 | | |
40 | | /* Encode macro */ |
41 | | #define H5R_ENCODE(func, val, buf, buf_size, actual, m) \ |
42 | 0 | do { \ |
43 | 0 | size_t __nalloc = buf_size; \ |
44 | 0 | if (func(val, buf, &__nalloc) < 0) \ |
45 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTENCODE, FAIL, m); \ |
46 | 0 | if (buf && buf_size >= __nalloc) { \ |
47 | 0 | buf += __nalloc; \ |
48 | 0 | buf_size -= __nalloc; \ |
49 | 0 | } \ |
50 | 0 | actual += __nalloc; \ |
51 | 0 | } while (0) |
52 | | |
53 | | #define H5R_ENCODE_VAR(func, var, size, buf, buf_size, actual, m) \ |
54 | 20 | do { \ |
55 | 20 | size_t __nalloc = buf_size; \ |
56 | 20 | if (func(var, size, buf, &__nalloc) < 0) \ |
57 | 20 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTENCODE, FAIL, m); \ |
58 | 20 | if (buf && buf_size >= __nalloc) { \ |
59 | 0 | p += __nalloc; \ |
60 | 0 | buf_size -= __nalloc; \ |
61 | 0 | } \ |
62 | 20 | actual += __nalloc; \ |
63 | 20 | } while (0) |
64 | | |
65 | | /* Decode macro */ |
66 | | #define H5R_DECODE(func, val, buf, buf_size, actual, m) \ |
67 | 0 | do { \ |
68 | 0 | size_t __nbytes = buf_size; \ |
69 | 0 | if (func(buf, &__nbytes, val) < 0) \ |
70 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, m); \ |
71 | 0 | buf += __nbytes; \ |
72 | 0 | buf_size -= __nbytes; \ |
73 | 0 | actual += __nbytes; \ |
74 | 0 | } while (0) |
75 | | |
76 | | #define H5R_DECODE_VAR(func, var, size, buf, buf_size, actual, m) \ |
77 | 0 | do { \ |
78 | 0 | size_t __nbytes = buf_size; \ |
79 | 0 | if (func(buf, &__nbytes, var, size) < 0) \ |
80 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, m); \ |
81 | 0 | p += __nbytes; \ |
82 | 0 | buf_size -= __nbytes; \ |
83 | 0 | actual += __nbytes; \ |
84 | 0 | } while (0) |
85 | | |
86 | | /* Debug */ |
87 | | #ifdef H5R_DEBUG |
88 | | #define H5R_LOG_DEBUG(...) \ |
89 | | do { \ |
90 | | fprintf(stdout, " # %s(): ", __func__); \ |
91 | | fprintf(stdout, __VA_ARGS__); \ |
92 | | fprintf(stdout, "\n"); \ |
93 | | fflush(stdout); \ |
94 | | } while (0) |
95 | | static const char * |
96 | | H5R__print_token(const H5O_token_t token) |
97 | | { |
98 | | static char string[64]; |
99 | | |
100 | | /* Print the raw token. */ |
101 | | snprintf(string, 64, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", |
102 | | (unsigned char)token.__data[15], (unsigned char)token.__data[14], |
103 | | (unsigned char)token.__data[13], (unsigned char)token.__data[12], |
104 | | (unsigned char)token.__data[11], (unsigned char)token.__data[10], (unsigned char)token.__data[9], |
105 | | (unsigned char)token.__data[8], (unsigned char)token.__data[7], (unsigned char)token.__data[6], |
106 | | (unsigned char)token.__data[5], (unsigned char)token.__data[4], (unsigned char)token.__data[3], |
107 | | (unsigned char)token.__data[2], (unsigned char)token.__data[1], (unsigned char)token.__data[0]); |
108 | | |
109 | | return string; |
110 | | } |
111 | | #else |
112 | | #define H5R_LOG_DEBUG(...) \ |
113 | 20 | do { \ |
114 | 20 | } while (0) |
115 | | #endif |
116 | | |
117 | | /******************/ |
118 | | /* Local Typedefs */ |
119 | | /******************/ |
120 | | |
121 | | /********************/ |
122 | | /* Local Prototypes */ |
123 | | /********************/ |
124 | | |
125 | | static herr_t H5R__encode_obj_token(const H5O_token_t *obj_token, size_t token_size, unsigned char *buf, |
126 | | size_t *nalloc); |
127 | | static herr_t H5R__decode_obj_token(const unsigned char *buf, size_t *nbytes, H5O_token_t *obj_token, |
128 | | uint8_t *token_size); |
129 | | static herr_t H5R__encode_region(H5S_t *space, unsigned char *buf, size_t *nalloc); |
130 | | static herr_t H5R__decode_region(const unsigned char *buf, size_t *nbytes, H5S_t **space_ptr); |
131 | | static herr_t H5R__encode_string(const char *string, unsigned char *buf, size_t *nalloc); |
132 | | static herr_t H5R__decode_string(const unsigned char *buf, size_t *nbytes, char **string_ptr); |
133 | | |
134 | | /*********************/ |
135 | | /* Package Variables */ |
136 | | /*********************/ |
137 | | |
138 | | /* Package initialization variable */ |
139 | | bool H5_PKG_INIT_VAR = false; |
140 | | |
141 | | /*****************************/ |
142 | | /* Library Private Variables */ |
143 | | /*****************************/ |
144 | | |
145 | | /*******************/ |
146 | | /* Local Variables */ |
147 | | /*******************/ |
148 | | |
149 | | /*-------------------------------------------------------------------------- |
150 | | NAME |
151 | | H5R__init_package -- Initialize interface-specific information |
152 | | USAGE |
153 | | herr_t H5R__init_package() |
154 | | RETURNS |
155 | | Non-negative on success/Negative on failure |
156 | | DESCRIPTION |
157 | | Initializes any interface-specific data or routines. |
158 | | --------------------------------------------------------------------------*/ |
159 | | herr_t |
160 | | H5R__init_package(void) |
161 | 1 | { |
162 | 1 | FUNC_ENTER_PACKAGE_NOERR |
163 | | |
164 | | /* Sanity check, if assert fails, H5R_REF_BUF_SIZE must be increased */ |
165 | 1 | HDcompile_assert(sizeof(H5R_ref_priv_t) <= H5R_REF_BUF_SIZE); |
166 | | |
167 | 1 | FUNC_LEAVE_NOAPI(SUCCEED) |
168 | 1 | } /* end H5R__init_package() */ |
169 | | |
170 | | /*------------------------------------------------------------------------- |
171 | | * Function: H5R__create_object |
172 | | * |
173 | | * Purpose: Creates an object reference |
174 | | * |
175 | | * Return: SUCCEED/FAIL |
176 | | * |
177 | | *------------------------------------------------------------------------- |
178 | | */ |
179 | | herr_t |
180 | | H5R__create_object(const H5O_token_t *obj_token, size_t token_size, H5R_ref_priv_t *ref) |
181 | 0 | { |
182 | 0 | size_t encode_size; |
183 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
184 | |
|
185 | 0 | FUNC_ENTER_PACKAGE |
186 | |
|
187 | 0 | assert(ref); |
188 | | |
189 | | /* Create new reference */ |
190 | 0 | ref->info.obj.filename = NULL; |
191 | 0 | ref->loc_id = H5I_INVALID_HID; |
192 | 0 | ref->type = (uint8_t)H5R_OBJECT2; |
193 | 0 | if (H5R__set_obj_token(ref, obj_token, token_size) < 0) |
194 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTSET, FAIL, "unable to set object token"); |
195 | | |
196 | | /* Cache encoding size (assume no external reference) */ |
197 | 0 | if (H5R__encode(NULL, ref, NULL, &encode_size, 0) < 0) |
198 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTENCODE, FAIL, "unable to determine encoding size"); |
199 | 0 | ref->encode_size = (uint32_t)encode_size; |
200 | |
|
201 | 0 | H5R_LOG_DEBUG("Created object reference, %d, filename=%s, obj_addr=%s, encode size=%u", |
202 | 0 | (int)sizeof(H5R_ref_priv_t), ref->info.obj.filename, H5R__print_token(ref->info.obj.token), |
203 | 0 | ref->encode_size); |
204 | |
|
205 | 0 | done: |
206 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
207 | 0 | } /* end H5R__create_object() */ |
208 | | |
209 | | /*------------------------------------------------------------------------- |
210 | | * Function: H5R__create_region |
211 | | * |
212 | | * Purpose: Creates a region reference |
213 | | * |
214 | | * Return: SUCCEED/FAIL |
215 | | * |
216 | | *------------------------------------------------------------------------- |
217 | | */ |
218 | | herr_t |
219 | | H5R__create_region(const H5O_token_t *obj_token, size_t token_size, H5S_t *space, H5R_ref_priv_t *ref) |
220 | 0 | { |
221 | 0 | size_t encode_size; |
222 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
223 | |
|
224 | 0 | FUNC_ENTER_PACKAGE |
225 | |
|
226 | 0 | assert(space); |
227 | 0 | assert(ref); |
228 | | |
229 | | /* Create new reference */ |
230 | 0 | ref->info.obj.filename = NULL; |
231 | 0 | if (NULL == (ref->info.reg.space = H5S_copy(space, false, true))) |
232 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTCOPY, FAIL, "unable to copy dataspace"); |
233 | | |
234 | 0 | ref->loc_id = H5I_INVALID_HID; |
235 | 0 | ref->type = (uint8_t)H5R_DATASET_REGION2; |
236 | 0 | if (H5R__set_obj_token(ref, obj_token, token_size) < 0) |
237 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTSET, FAIL, "unable to set object token"); |
238 | | |
239 | | /* Cache encoding size (assume no external reference) */ |
240 | 0 | if (H5R__encode(NULL, ref, NULL, &encode_size, 0) < 0) |
241 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTENCODE, FAIL, "unable to determine encoding size"); |
242 | 0 | ref->encode_size = (uint32_t)encode_size; |
243 | |
|
244 | 0 | H5R_LOG_DEBUG("Created region reference, %d, filename=%s, obj_addr=%s, encode size=%u", |
245 | 0 | (int)sizeof(H5R_ref_priv_t), ref->info.obj.filename, H5R__print_token(ref->info.obj.token), |
246 | 0 | ref->encode_size); |
247 | |
|
248 | 0 | done: |
249 | 0 | if (ret_value < 0) |
250 | 0 | if (ref->info.reg.space) { |
251 | 0 | H5S_close(ref->info.reg.space); |
252 | 0 | ref->info.reg.space = NULL; |
253 | 0 | } /* end if */ |
254 | |
|
255 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
256 | 0 | } /* H5R__create_region */ |
257 | | |
258 | | /*------------------------------------------------------------------------- |
259 | | * Function: H5R__create_attr |
260 | | * |
261 | | * Purpose: Creates an attribute reference |
262 | | * |
263 | | * Return: SUCCEED/FAIL |
264 | | * |
265 | | *------------------------------------------------------------------------- |
266 | | */ |
267 | | herr_t |
268 | | H5R__create_attr(const H5O_token_t *obj_token, size_t token_size, const char *attr_name, H5R_ref_priv_t *ref) |
269 | 0 | { |
270 | 0 | size_t encode_size; |
271 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
272 | |
|
273 | 0 | FUNC_ENTER_PACKAGE |
274 | |
|
275 | 0 | assert(attr_name); |
276 | 0 | assert(ref); |
277 | | |
278 | | /* Make sure that attribute name is not longer than supported encode size */ |
279 | 0 | if (strlen(attr_name) > H5R_MAX_STRING_LEN) |
280 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_ARGS, FAIL, "attribute name too long (%d > %d)", |
281 | 0 | (int)strlen(attr_name), H5R_MAX_STRING_LEN); |
282 | | |
283 | | /* Create new reference */ |
284 | 0 | ref->info.obj.filename = NULL; |
285 | 0 | if (NULL == (ref->info.attr.name = strdup(attr_name))) |
286 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTCOPY, FAIL, "Cannot copy attribute name"); |
287 | | |
288 | 0 | ref->loc_id = H5I_INVALID_HID; |
289 | 0 | ref->type = (uint8_t)H5R_ATTR; |
290 | 0 | if (H5R__set_obj_token(ref, obj_token, token_size) < 0) |
291 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTSET, FAIL, "unable to set object token"); |
292 | | |
293 | | /* Cache encoding size (assume no external reference) */ |
294 | 0 | if (H5R__encode(NULL, ref, NULL, &encode_size, 0) < 0) |
295 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTENCODE, FAIL, "unable to determine encoding size"); |
296 | 0 | ref->encode_size = (uint32_t)encode_size; |
297 | |
|
298 | 0 | H5R_LOG_DEBUG("Created attribute reference, %d, filename=%s, obj_addr=%s, attr name=%s, encode size=%u", |
299 | 0 | (int)sizeof(H5R_ref_priv_t), ref->info.obj.filename, H5R__print_token(ref->info.obj.token), |
300 | 0 | ref->info.attr.name, ref->encode_size); |
301 | |
|
302 | 0 | done: |
303 | 0 | if (ret_value < 0) { |
304 | 0 | H5MM_xfree(ref->info.attr.name); |
305 | 0 | ref->info.attr.name = NULL; |
306 | 0 | } /* end if */ |
307 | |
|
308 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
309 | 0 | } /* H5R__create_attr */ |
310 | | |
311 | | /*------------------------------------------------------------------------- |
312 | | * Function: H5R__destroy |
313 | | * |
314 | | * Purpose: Destroy a reference |
315 | | * |
316 | | * Return: SUCCEED/FAIL |
317 | | * |
318 | | *------------------------------------------------------------------------- |
319 | | */ |
320 | | herr_t |
321 | | H5R__destroy(H5R_ref_priv_t *ref) |
322 | 0 | { |
323 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
324 | |
|
325 | 0 | FUNC_ENTER_PACKAGE |
326 | |
|
327 | 0 | assert(ref != NULL); |
328 | |
|
329 | 0 | H5R_LOG_DEBUG("Destroying reference, filename=%s, obj_addr=%s, encode size=%u", ref->info.obj.filename, |
330 | 0 | H5R__print_token(ref->info.obj.token), ref->encode_size); |
331 | |
|
332 | 0 | H5MM_xfree(ref->info.obj.filename); |
333 | 0 | ref->info.obj.filename = NULL; |
334 | |
|
335 | 0 | switch (ref->type) { |
336 | 0 | case H5R_OBJECT2: |
337 | 0 | break; |
338 | | |
339 | 0 | case H5R_DATASET_REGION2: |
340 | 0 | if (H5S_close(ref->info.reg.space) < 0) |
341 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTFREE, FAIL, "Cannot close dataspace"); |
342 | 0 | ref->info.reg.space = NULL; |
343 | 0 | break; |
344 | | |
345 | 0 | case H5R_ATTR: |
346 | 0 | H5MM_xfree(ref->info.attr.name); |
347 | 0 | ref->info.attr.name = NULL; |
348 | 0 | break; |
349 | | |
350 | 0 | case H5R_OBJECT1: |
351 | 0 | case H5R_DATASET_REGION1: |
352 | 0 | break; |
353 | 0 | case H5R_BADTYPE: |
354 | 0 | case H5R_MAXTYPE: |
355 | 0 | assert("invalid reference type" && 0); |
356 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (invalid reference type)"); |
357 | | |
358 | 0 | default: |
359 | 0 | assert("unknown reference type" && 0); |
360 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (unknown reference type)"); |
361 | 0 | } /* end switch */ |
362 | | |
363 | | /* Decrement refcount of attached loc_id */ |
364 | 0 | if (ref->type && (ref->loc_id != H5I_INVALID_HID)) { |
365 | 0 | if (ref->app_ref) { |
366 | 0 | if (H5I_dec_app_ref(ref->loc_id) < 0) |
367 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDEC, FAIL, "decrementing location ID failed"); |
368 | 0 | } |
369 | 0 | else { |
370 | 0 | if (H5I_dec_ref(ref->loc_id) < 0) |
371 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDEC, FAIL, "decrementing location ID failed"); |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | 0 | done: |
376 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
377 | 0 | } /* end H5R__destroy() */ |
378 | | |
379 | | /*------------------------------------------------------------------------- |
380 | | * Function: H5R__set_loc_id |
381 | | * |
382 | | * Purpose: Attach location ID to reference and increment location refcount. |
383 | | * |
384 | | * Return: SUCCEED/FAIL |
385 | | * |
386 | | *------------------------------------------------------------------------- |
387 | | */ |
388 | | herr_t |
389 | | H5R__set_loc_id(H5R_ref_priv_t *ref, hid_t id, bool inc_ref, bool app_ref) |
390 | 0 | { |
391 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
392 | |
|
393 | 0 | FUNC_ENTER_PACKAGE |
394 | |
|
395 | 0 | assert(ref != NULL); |
396 | 0 | assert(id != H5I_INVALID_HID); |
397 | | |
398 | | /* If a location ID was previously assigned, decrement refcount and |
399 | | * assign new one */ |
400 | 0 | if ((ref->loc_id != H5I_INVALID_HID)) { |
401 | 0 | if (ref->app_ref) { |
402 | 0 | if (H5I_dec_app_ref(ref->loc_id) < 0) |
403 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDEC, FAIL, "decrementing location ID failed"); |
404 | 0 | } |
405 | 0 | else { |
406 | 0 | if (H5I_dec_ref(ref->loc_id) < 0) |
407 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDEC, FAIL, "decrementing location ID failed"); |
408 | 0 | } |
409 | 0 | } |
410 | 0 | ref->loc_id = id; |
411 | | |
412 | | /* Prevent location ID from being freed until reference is destroyed, |
413 | | * set app_ref if necessary as references are exposed to users and are |
414 | | * expected to be destroyed, this allows the loc_id to be cleanly released |
415 | | * on shutdown if users fail to call H5Rdestroy(). */ |
416 | 0 | if (inc_ref && H5I_inc_ref(ref->loc_id, app_ref) < 0) |
417 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTINC, FAIL, "incrementing location ID failed"); |
418 | 0 | ref->app_ref = app_ref; |
419 | |
|
420 | 0 | done: |
421 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
422 | 0 | } /* end H5R__set_loc_id() */ |
423 | | |
424 | | /*------------------------------------------------------------------------- |
425 | | * Function: H5R__get_loc_id |
426 | | * |
427 | | * Purpose: Retrieve location ID attached to existing reference. |
428 | | * |
429 | | * Return: Valid ID on success / H5I_INVALID_HID on failure |
430 | | * |
431 | | *------------------------------------------------------------------------- |
432 | | */ |
433 | | hid_t |
434 | | H5R__get_loc_id(const H5R_ref_priv_t *ref) |
435 | 0 | { |
436 | 0 | hid_t ret_value = H5I_INVALID_HID; /* Return value */ |
437 | |
|
438 | 0 | FUNC_ENTER_PACKAGE_NOERR |
439 | |
|
440 | 0 | assert(ref != NULL); |
441 | |
|
442 | 0 | ret_value = ref->loc_id; |
443 | |
|
444 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
445 | 0 | } /* end H5R__get_loc_id() */ |
446 | | |
447 | | /*------------------------------------------------------------------------- |
448 | | * Function: H5R__reopen_file |
449 | | * |
450 | | * Purpose: Re-open referenced file using file access property list. |
451 | | * |
452 | | * Return: Valid ID on success / H5I_INVALID_HID on failure |
453 | | * |
454 | | *------------------------------------------------------------------------- |
455 | | */ |
456 | | hid_t |
457 | | H5R__reopen_file(H5R_ref_priv_t *ref, hid_t fapl_id) |
458 | 0 | { |
459 | 0 | H5P_genplist_t *plist; /* Property list for FAPL */ |
460 | 0 | void *new_file = NULL; /* File object opened */ |
461 | 0 | H5VL_connector_prop_t connector_prop; /* Property for VOL connector ID & info */ |
462 | 0 | H5VL_object_t *vol_obj = NULL; /* VOL object for file */ |
463 | 0 | uint64_t supported; /* Whether 'post open' operation is supported by VOL connector */ |
464 | 0 | hid_t ret_value = H5I_INVALID_HID; |
465 | |
|
466 | 0 | FUNC_ENTER_PACKAGE |
467 | | |
468 | | /* TODO add search path */ |
469 | | |
470 | | /* Verify access property list and set up collective metadata if appropriate */ |
471 | 0 | if (H5CX_set_apl(&fapl_id, H5P_CLS_FACC, H5I_INVALID_HID, true) < 0) |
472 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTSET, H5I_INVALID_HID, "can't set access property list info"); |
473 | | |
474 | | /* Get the VOL info from the fapl */ |
475 | 0 | if (NULL == (plist = (H5P_genplist_t *)H5I_object(fapl_id))) |
476 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "not a file access property list"); |
477 | 0 | if (H5P_peek(plist, H5F_ACS_VOL_CONN_NAME, &connector_prop) < 0) |
478 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTGET, H5I_INVALID_HID, "can't get VOL connector info"); |
479 | | |
480 | | /* Stash a copy of the "top-level" connector property, before any pass-through |
481 | | * connectors modify or unwrap it. |
482 | | */ |
483 | 0 | if (H5CX_set_vol_connector_prop(&connector_prop) < 0) |
484 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTSET, H5I_INVALID_HID, |
485 | 0 | "can't set VOL connector info in API context"); |
486 | | |
487 | | /* Open the file */ |
488 | | /* (Must open file read-write to allow for object modifications) */ |
489 | 0 | if (NULL == (new_file = H5VL_file_open(connector_prop.connector, H5R_REF_FILENAME(ref), H5F_ACC_RDWR, |
490 | 0 | fapl_id, H5P_DATASET_XFER_DEFAULT, H5_REQUEST_NULL))) |
491 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTOPENFILE, H5I_INVALID_HID, "unable to open file"); |
492 | | |
493 | | /* Get an ID for the file */ |
494 | 0 | if ((ret_value = H5VL_register(H5I_FILE, new_file, connector_prop.connector, true)) < 0) |
495 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to register file handle"); |
496 | | |
497 | | /* Get the file object */ |
498 | 0 | if (NULL == (vol_obj = H5VL_vol_object(ret_value))) |
499 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTGET, H5I_INVALID_HID, "invalid object identifier"); |
500 | | |
501 | | /* Make the 'post open' callback */ |
502 | 0 | supported = 0; |
503 | 0 | if (H5VL_introspect_opt_query(vol_obj, H5VL_SUBCLS_FILE, H5VL_NATIVE_FILE_POST_OPEN, &supported) < 0) |
504 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTGET, H5I_INVALID_HID, "can't check for 'post open' operation"); |
505 | 0 | if (supported & H5VL_OPT_QUERY_SUPPORTED) { |
506 | 0 | H5VL_optional_args_t vol_cb_args; /* Arguments to VOL callback */ |
507 | | |
508 | | /* Set up VOL callback arguments */ |
509 | 0 | vol_cb_args.op_type = H5VL_NATIVE_FILE_POST_OPEN; |
510 | 0 | vol_cb_args.args = NULL; |
511 | | |
512 | | /* Make the 'post open' callback */ |
513 | 0 | if (H5VL_file_optional(vol_obj, &vol_cb_args, H5P_DATASET_XFER_DEFAULT, H5_REQUEST_NULL) < 0) |
514 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTINIT, H5I_INVALID_HID, |
515 | 0 | "unable to make file 'post open' callback"); |
516 | 0 | } /* end if */ |
517 | | |
518 | | /* Attach loc_id to reference */ |
519 | 0 | if (H5R__set_loc_id((H5R_ref_priv_t *)ref, ret_value, false, true) < 0) |
520 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTSET, H5I_INVALID_HID, "unable to attach location id to reference"); |
521 | | |
522 | 0 | done: |
523 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
524 | 0 | } /* end H5R__reopen_file() */ |
525 | | |
526 | | /*------------------------------------------------------------------------- |
527 | | * Function: H5R__get_type |
528 | | * |
529 | | * Purpose: Given a reference to some object, return the type of that |
530 | | * reference. |
531 | | * |
532 | | * Return: Type of the reference |
533 | | * |
534 | | *------------------------------------------------------------------------- |
535 | | */ |
536 | | H5R_type_t |
537 | | H5R__get_type(const H5R_ref_priv_t *ref) |
538 | 0 | { |
539 | 0 | H5R_type_t ret_value = H5R_BADTYPE; |
540 | |
|
541 | 0 | FUNC_ENTER_PACKAGE_NOERR |
542 | |
|
543 | 0 | assert(ref != NULL); |
544 | 0 | ret_value = (H5R_type_t)ref->type; |
545 | |
|
546 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
547 | 0 | } /* end H5R__get_type() */ |
548 | | |
549 | | /*------------------------------------------------------------------------- |
550 | | * Function: H5R__equal |
551 | | * |
552 | | * Purpose: Compare two references |
553 | | * |
554 | | * Return: true if equal, false if unequal, FAIL if error |
555 | | * |
556 | | *------------------------------------------------------------------------- |
557 | | */ |
558 | | htri_t |
559 | | H5R__equal(const H5R_ref_priv_t *ref1, const H5R_ref_priv_t *ref2) |
560 | 0 | { |
561 | 0 | htri_t ret_value = true; |
562 | |
|
563 | 0 | FUNC_ENTER_PACKAGE |
564 | |
|
565 | 0 | assert(ref1 != NULL); |
566 | 0 | assert(ref2 != NULL); |
567 | | |
568 | | /* Compare reference types */ |
569 | 0 | if (ref1->type != ref2->type) |
570 | 0 | HGOTO_DONE(false); |
571 | | |
572 | | /* Compare object addresses */ |
573 | 0 | if (ref1->token_size != ref2->token_size) |
574 | 0 | HGOTO_DONE(false); |
575 | 0 | if (0 != memcmp(&ref1->info.obj.token, &ref2->info.obj.token, ref1->token_size)) |
576 | 0 | HGOTO_DONE(false); |
577 | | |
578 | | /* Compare filenames */ |
579 | 0 | if ((ref1->info.obj.filename && (NULL == ref2->info.obj.filename)) || |
580 | 0 | ((NULL == ref1->info.obj.filename) && ref2->info.obj.filename)) |
581 | 0 | HGOTO_DONE(false); |
582 | 0 | if (ref1->info.obj.filename && ref1->info.obj.filename && |
583 | 0 | (0 != strcmp(ref1->info.obj.filename, ref2->info.obj.filename))) |
584 | 0 | HGOTO_DONE(false); |
585 | | |
586 | 0 | switch (ref1->type) { |
587 | 0 | case H5R_OBJECT2: |
588 | 0 | break; |
589 | 0 | case H5R_DATASET_REGION2: |
590 | 0 | if ((ret_value = H5S_extent_equal(ref1->info.reg.space, ref2->info.reg.space)) < 0) |
591 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTCOMPARE, FAIL, "cannot compare dataspace extents"); |
592 | 0 | break; |
593 | 0 | case H5R_ATTR: |
594 | 0 | assert(ref1->info.attr.name && ref2->info.attr.name); |
595 | 0 | if (0 != strcmp(ref1->info.attr.name, ref2->info.attr.name)) |
596 | 0 | HGOTO_DONE(false); |
597 | 0 | break; |
598 | 0 | case H5R_OBJECT1: |
599 | 0 | case H5R_DATASET_REGION1: |
600 | 0 | case H5R_BADTYPE: |
601 | 0 | case H5R_MAXTYPE: |
602 | 0 | assert("invalid reference type" && 0); |
603 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (invalid reference type)"); |
604 | 0 | default: |
605 | 0 | assert("unknown reference type" && 0); |
606 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (unknown reference type)"); |
607 | 0 | } /* end switch */ |
608 | | |
609 | 0 | done: |
610 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
611 | 0 | } /* end H5R__equal() */ |
612 | | |
613 | | /*------------------------------------------------------------------------- |
614 | | * Function: H5R__copy |
615 | | * |
616 | | * Purpose: Copy a reference |
617 | | * |
618 | | * Return: SUCCEED/FAIL |
619 | | * |
620 | | *------------------------------------------------------------------------- |
621 | | */ |
622 | | herr_t |
623 | | H5R__copy(const H5R_ref_priv_t *src_ref, H5R_ref_priv_t *dst_ref) |
624 | 0 | { |
625 | 0 | herr_t ret_value = SUCCEED; |
626 | |
|
627 | 0 | FUNC_ENTER_PACKAGE |
628 | |
|
629 | 0 | assert((src_ref != NULL) && (dst_ref != NULL)); |
630 | |
|
631 | 0 | H5MM_memcpy(&dst_ref->info.obj.token, &src_ref->info.obj.token, sizeof(H5O_token_t)); |
632 | 0 | dst_ref->encode_size = src_ref->encode_size; |
633 | 0 | dst_ref->type = src_ref->type; |
634 | 0 | dst_ref->token_size = src_ref->token_size; |
635 | |
|
636 | 0 | switch (src_ref->type) { |
637 | 0 | case H5R_OBJECT2: |
638 | 0 | break; |
639 | 0 | case H5R_DATASET_REGION2: |
640 | 0 | if (NULL == (dst_ref->info.reg.space = H5S_copy(src_ref->info.reg.space, false, true))) |
641 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTCOPY, FAIL, "unable to copy dataspace"); |
642 | 0 | break; |
643 | 0 | case H5R_ATTR: |
644 | 0 | if (NULL == (dst_ref->info.attr.name = strdup(src_ref->info.attr.name))) |
645 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTCOPY, FAIL, "Cannot copy attribute name"); |
646 | 0 | break; |
647 | 0 | case H5R_OBJECT1: |
648 | 0 | case H5R_DATASET_REGION1: |
649 | 0 | assert("invalid reference type" && 0); |
650 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (invalid reference type)"); |
651 | 0 | case H5R_BADTYPE: |
652 | 0 | case H5R_MAXTYPE: |
653 | 0 | default: |
654 | 0 | assert("unknown reference type" && 0); |
655 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (unknown reference type)"); |
656 | 0 | } /* end switch */ |
657 | | |
658 | | /* We only need to keep a copy of the filename if we don't have the loc_id */ |
659 | 0 | if (src_ref->loc_id == H5I_INVALID_HID) { |
660 | 0 | assert(src_ref->info.obj.filename); |
661 | |
|
662 | 0 | if (NULL == (dst_ref->info.obj.filename = strdup(src_ref->info.obj.filename))) |
663 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTCOPY, FAIL, "Cannot copy filename"); |
664 | 0 | dst_ref->loc_id = H5I_INVALID_HID; |
665 | 0 | } |
666 | 0 | else { |
667 | 0 | dst_ref->info.obj.filename = NULL; |
668 | | |
669 | | /* Set location ID and hold reference to it */ |
670 | 0 | dst_ref->loc_id = src_ref->loc_id; |
671 | 0 | if (H5I_inc_ref(dst_ref->loc_id, true) < 0) |
672 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTINC, FAIL, "incrementing location ID failed"); |
673 | 0 | dst_ref->app_ref = true; |
674 | 0 | } |
675 | | |
676 | 0 | done: |
677 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
678 | 0 | } /* end H5R__copy() */ |
679 | | |
680 | | /*------------------------------------------------------------------------- |
681 | | * Function: H5R__get_obj_token |
682 | | * |
683 | | * Purpose: Given a reference to some object, get the encoded token. |
684 | | * |
685 | | * Return: SUCCEED/FAIL |
686 | | * |
687 | | *------------------------------------------------------------------------- |
688 | | */ |
689 | | herr_t |
690 | | H5R__get_obj_token(const H5R_ref_priv_t *ref, H5O_token_t *obj_token, size_t *token_size) |
691 | 0 | { |
692 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
693 | |
|
694 | 0 | FUNC_ENTER_PACKAGE |
695 | |
|
696 | 0 | assert(ref != NULL); |
697 | 0 | assert(ref->token_size <= H5O_MAX_TOKEN_SIZE); |
698 | |
|
699 | 0 | if (obj_token) { |
700 | 0 | if (0 == ref->token_size) |
701 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTCOPY, FAIL, "NULL token size"); |
702 | 0 | H5MM_memcpy(obj_token, &ref->info.obj.token, sizeof(H5O_token_t)); |
703 | 0 | } |
704 | 0 | if (token_size) |
705 | 0 | *token_size = ref->token_size; |
706 | |
|
707 | 0 | done: |
708 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
709 | 0 | } /* end H5R__get_obj_token() */ |
710 | | |
711 | | /*------------------------------------------------------------------------- |
712 | | * Function: H5R__set_obj_token |
713 | | * |
714 | | * Purpose: Given a reference to some object, set the encoded token. |
715 | | * |
716 | | * Return: SUCCEED/FAIL |
717 | | * |
718 | | *------------------------------------------------------------------------- |
719 | | */ |
720 | | herr_t |
721 | | H5R__set_obj_token(H5R_ref_priv_t *ref, const H5O_token_t *obj_token, size_t token_size) |
722 | 0 | { |
723 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
724 | |
|
725 | 0 | FUNC_ENTER_PACKAGE_NOERR |
726 | |
|
727 | 0 | assert(ref != NULL); |
728 | 0 | assert(obj_token); |
729 | 0 | assert(token_size); |
730 | 0 | assert(token_size <= H5O_MAX_TOKEN_SIZE); |
731 | |
|
732 | 0 | H5MM_memcpy(&ref->info.obj.token, obj_token, token_size); |
733 | 0 | assert(token_size <= 255); |
734 | 0 | ref->token_size = (uint8_t)token_size; |
735 | |
|
736 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
737 | 0 | } /* end H5R__set_obj_token() */ |
738 | | |
739 | | /*------------------------------------------------------------------------- |
740 | | * Function: H5R__get_region |
741 | | * |
742 | | * Purpose: Given a reference to some object, creates a copy of the |
743 | | * dataset pointed to's dataspace and defines a selection in |
744 | | * the copy which is the region pointed to. |
745 | | * |
746 | | * Return: SUCCEED/FAIL |
747 | | * |
748 | | *------------------------------------------------------------------------- |
749 | | */ |
750 | | herr_t |
751 | | H5R__get_region(const H5R_ref_priv_t *ref, H5S_t *space) |
752 | 0 | { |
753 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
754 | |
|
755 | 0 | FUNC_ENTER_PACKAGE |
756 | |
|
757 | 0 | assert(ref != NULL); |
758 | 0 | assert(ref->type == H5R_DATASET_REGION2); |
759 | 0 | assert(space); |
760 | | |
761 | | /* Copy reference selection to destination */ |
762 | 0 | if (H5S_select_copy(space, ref->info.reg.space, false) < 0) |
763 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTCOPY, FAIL, "unable to copy selection"); |
764 | | |
765 | 0 | done: |
766 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
767 | 0 | } /* end H5R__get_region() */ |
768 | | |
769 | | /*------------------------------------------------------------------------- |
770 | | * Function: H5R__get_file_name |
771 | | * |
772 | | * Purpose: Given a reference to some object, determine a file name of |
773 | | * the object located into. |
774 | | * |
775 | | * Return: Non-negative length of the path on success / -1 on failure |
776 | | * |
777 | | *------------------------------------------------------------------------- |
778 | | */ |
779 | | ssize_t |
780 | | H5R__get_file_name(const H5R_ref_priv_t *ref, char *buf, size_t size) |
781 | 0 | { |
782 | 0 | size_t copy_len; |
783 | 0 | ssize_t ret_value = -1; /* Return value */ |
784 | |
|
785 | 0 | FUNC_ENTER_PACKAGE |
786 | | |
787 | | /* Check args */ |
788 | 0 | assert(ref != NULL); |
789 | | |
790 | | /* Return if that reference has no filename set */ |
791 | 0 | if (!ref->info.obj.filename) |
792 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_ARGS, (-1), "no filename available for that reference"); |
793 | | |
794 | | /* Get the file name length */ |
795 | 0 | copy_len = strlen(ref->info.obj.filename); |
796 | 0 | assert(copy_len <= H5R_MAX_STRING_LEN); |
797 | | |
798 | | /* Copy the file name */ |
799 | 0 | if (buf) { |
800 | 0 | copy_len = MIN(copy_len, size - 1); |
801 | 0 | H5MM_memcpy(buf, ref->info.obj.filename, copy_len); |
802 | 0 | buf[copy_len] = '\0'; |
803 | 0 | } |
804 | 0 | ret_value = (ssize_t)(copy_len + 1); |
805 | |
|
806 | 0 | done: |
807 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
808 | 0 | } /* end H5R__get_file_name() */ |
809 | | |
810 | | /*------------------------------------------------------------------------- |
811 | | * Function: H5R__get_attr_name |
812 | | * |
813 | | * Purpose: Given a reference to some attribute, determine its name. |
814 | | * |
815 | | * Return: Non-negative length of the path on success / -1 on failure |
816 | | * |
817 | | *------------------------------------------------------------------------- |
818 | | */ |
819 | | ssize_t |
820 | | H5R__get_attr_name(const H5R_ref_priv_t *ref, char *buf, size_t size) |
821 | 0 | { |
822 | 0 | ssize_t ret_value = -1; /* Return value */ |
823 | 0 | size_t attr_name_len; /* Length of the attribute name */ |
824 | |
|
825 | 0 | FUNC_ENTER_PACKAGE_NOERR |
826 | | |
827 | | /* Check args */ |
828 | 0 | assert(ref != NULL); |
829 | 0 | assert(ref->type == H5R_ATTR); |
830 | | |
831 | | /* Get the attribute name length */ |
832 | 0 | attr_name_len = strlen(ref->info.attr.name); |
833 | 0 | assert(attr_name_len <= H5R_MAX_STRING_LEN); |
834 | | |
835 | | /* Get the attribute name */ |
836 | 0 | if (buf) { |
837 | 0 | size_t copy_len = MIN(attr_name_len, size - 1); |
838 | 0 | H5MM_memcpy(buf, ref->info.attr.name, copy_len); |
839 | 0 | buf[copy_len] = '\0'; |
840 | 0 | } |
841 | |
|
842 | 0 | ret_value = (ssize_t)(attr_name_len); |
843 | |
|
844 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
845 | 0 | } /* end H5R__get_attr_name() */ |
846 | | |
847 | | /*------------------------------------------------------------------------- |
848 | | * Function: H5R__encode |
849 | | * |
850 | | * Purpose: Private function for H5Rencode |
851 | | * |
852 | | * Return: SUCCEED/FAIL |
853 | | * |
854 | | *------------------------------------------------------------------------- |
855 | | */ |
856 | | herr_t |
857 | | H5R__encode(const char *filename, const H5R_ref_priv_t *ref, unsigned char *buf, size_t *nalloc, |
858 | | unsigned flags) |
859 | 20 | { |
860 | 20 | uint8_t *p = (uint8_t *)buf; |
861 | 20 | size_t buf_size = 0, encode_size = 0; |
862 | 20 | herr_t ret_value = SUCCEED; |
863 | | |
864 | 20 | FUNC_ENTER_PACKAGE |
865 | | |
866 | 20 | assert(ref); |
867 | 20 | assert(nalloc); |
868 | | |
869 | | /** |
870 | | * Encoding format: |
871 | | * | Reference type (8 bits) | Flags (8 bits) | Token (token size) |
872 | | * | | |
873 | | * | |----> H5R_IS_EXTERNAL: File info |
874 | | * | |
875 | | * |----> H5R_DATASET_REGION2: Serialized selection |
876 | | * | |
877 | | * |----> H5R_ATTR: Attribute name len + name |
878 | | * |
879 | | */ |
880 | | |
881 | | /* Don't encode if buffer size isn't big enough or buffer is empty */ |
882 | 20 | if (buf && *nalloc >= H5R_ENCODE_HEADER_SIZE) { |
883 | | /* Encode the type of the reference */ |
884 | 0 | *p++ = (uint8_t)ref->type; |
885 | | |
886 | | /* Encode the flags */ |
887 | 0 | *p++ = (uint8_t)flags; |
888 | |
|
889 | 0 | buf_size = *nalloc - H5R_ENCODE_HEADER_SIZE; |
890 | 0 | } /* end if */ |
891 | 20 | encode_size += H5R_ENCODE_HEADER_SIZE; |
892 | | |
893 | | /* Encode object token */ |
894 | 20 | H5R_ENCODE_VAR(H5R__encode_obj_token, &ref->info.obj.token, ref->token_size, p, buf_size, encode_size, |
895 | 20 | "Cannot encode object address"); |
896 | | |
897 | | /** |
898 | | * TODO Encode VOL info |
899 | | * When we have a better way of storing blobs, we should add |
900 | | * support for referencing files in external VOLs. |
901 | | * There are currently multiple limitations: |
902 | | * - avoid duplicating VOL info on each reference |
903 | | * - must query terminal VOL connector to avoid passthrough confusion |
904 | | */ |
905 | 20 | if (flags & H5R_IS_EXTERNAL) |
906 | | /* Encode file name */ |
907 | 0 | H5R_ENCODE(H5R__encode_string, filename, p, buf_size, encode_size, "Cannot encode filename"); |
908 | | |
909 | 20 | switch (ref->type) { |
910 | 20 | case H5R_OBJECT2: |
911 | 20 | break; |
912 | | |
913 | 0 | case H5R_DATASET_REGION2: |
914 | | /* Encode dataspace */ |
915 | 0 | H5R_ENCODE(H5R__encode_region, ref->info.reg.space, p, buf_size, encode_size, |
916 | 0 | "Cannot encode region"); |
917 | 0 | break; |
918 | | |
919 | 0 | case H5R_ATTR: |
920 | | /* Encode attribute name */ |
921 | 0 | H5R_ENCODE(H5R__encode_string, ref->info.attr.name, p, buf_size, encode_size, |
922 | 0 | "Cannot encode attribute name"); |
923 | 0 | break; |
924 | | |
925 | 0 | case H5R_OBJECT1: |
926 | 0 | case H5R_DATASET_REGION1: |
927 | 0 | case H5R_BADTYPE: |
928 | 0 | case H5R_MAXTYPE: |
929 | 0 | assert("invalid reference type" && 0); |
930 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (invalid reference type)"); |
931 | | |
932 | 0 | default: |
933 | 0 | assert("unknown reference type" && 0); |
934 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (unknown reference type)"); |
935 | 20 | } /* end switch */ |
936 | | |
937 | 20 | H5R_LOG_DEBUG("Encoded reference, filename=%s, obj_addr=%s, encode size=%u", ref->info.obj.filename, |
938 | 20 | H5R__print_token(ref->info.obj.token), encode_size); |
939 | | |
940 | 20 | *nalloc = encode_size; |
941 | | |
942 | 20 | done: |
943 | 20 | FUNC_LEAVE_NOAPI(ret_value) |
944 | 20 | } /* end H5R__encode() */ |
945 | | |
946 | | /*------------------------------------------------------------------------- |
947 | | * Function: H5R__decode |
948 | | * |
949 | | * Purpose: Private function for H5Rdecode |
950 | | * |
951 | | * Return: SUCCEED/FAIL |
952 | | * |
953 | | *------------------------------------------------------------------------- |
954 | | */ |
955 | | herr_t |
956 | | H5R__decode(const unsigned char *buf, size_t *nbytes, H5R_ref_priv_t *ref) |
957 | 0 | { |
958 | 0 | const uint8_t *p = (const uint8_t *)buf; |
959 | 0 | size_t buf_size = 0, decode_size = 0; |
960 | 0 | uint8_t flags; |
961 | 0 | bool decoded_filename = false; /* Whether filename was decoded, for error handling */ |
962 | 0 | bool decoded_attrname = false; /* Whether attribute name was decoded, for error handling */ |
963 | 0 | bool decoded_dataspace = false; /* Whether dataspace was decoded, for error handling */ |
964 | 0 | herr_t ret_value = SUCCEED; |
965 | |
|
966 | 0 | FUNC_ENTER_PACKAGE |
967 | | |
968 | | /* Sanity checks */ |
969 | 0 | assert(buf); |
970 | 0 | assert(nbytes); |
971 | 0 | assert(ref); |
972 | | |
973 | | /* Don't decode if buffer size isn't big enough */ |
974 | 0 | buf_size = *nbytes; |
975 | 0 | if (buf_size < H5R_ENCODE_HEADER_SIZE) |
976 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Buffer size is too small"); |
977 | | |
978 | | /* Set new reference */ |
979 | 0 | ref->type = (int8_t)*p++; |
980 | 0 | if (ref->type <= H5R_BADTYPE || ref->type >= H5R_MAXTYPE) |
981 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid reference type"); |
982 | | |
983 | | /* Set flags */ |
984 | 0 | flags = *p++; |
985 | |
|
986 | 0 | buf_size -= H5R_ENCODE_HEADER_SIZE; |
987 | 0 | decode_size += H5R_ENCODE_HEADER_SIZE; |
988 | | |
989 | | /* Decode object token */ |
990 | 0 | H5R_DECODE_VAR(H5R__decode_obj_token, &ref->info.obj.token, &ref->token_size, p, buf_size, decode_size, |
991 | 0 | "Cannot decode object address"); |
992 | | |
993 | | /* We do not need to store the filename if the reference is internal */ |
994 | 0 | if (flags & H5R_IS_EXTERNAL) { |
995 | | /* Decode file name */ |
996 | 0 | H5R_DECODE(H5R__decode_string, &ref->info.obj.filename, p, buf_size, decode_size, |
997 | 0 | "Cannot decode filename"); |
998 | 0 | decoded_filename = true; |
999 | 0 | } |
1000 | 0 | else |
1001 | 0 | ref->info.obj.filename = NULL; |
1002 | | |
1003 | 0 | switch (ref->type) { |
1004 | 0 | case H5R_OBJECT2: |
1005 | 0 | break; |
1006 | | |
1007 | 0 | case H5R_DATASET_REGION2: |
1008 | | /* Decode dataspace */ |
1009 | 0 | H5R_DECODE(H5R__decode_region, &ref->info.reg.space, p, buf_size, decode_size, |
1010 | 0 | "Cannot decode region"); |
1011 | 0 | decoded_dataspace = true; |
1012 | 0 | break; |
1013 | | |
1014 | 0 | case H5R_ATTR: |
1015 | | /* Decode attribute name */ |
1016 | 0 | H5R_DECODE(H5R__decode_string, &ref->info.attr.name, p, buf_size, decode_size, |
1017 | 0 | "Cannot decode attribute name"); |
1018 | 0 | decoded_attrname = true; |
1019 | 0 | break; |
1020 | | |
1021 | 0 | case H5R_OBJECT1: |
1022 | 0 | case H5R_DATASET_REGION1: |
1023 | 0 | case H5R_BADTYPE: |
1024 | 0 | case H5R_MAXTYPE: |
1025 | 0 | assert("invalid reference type" && 0); |
1026 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (invalid reference type)"); |
1027 | | |
1028 | 0 | default: |
1029 | 0 | assert("unknown reference type" && 0); |
1030 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_UNSUPPORTED, FAIL, "internal error (unknown reference type)"); |
1031 | 0 | } /* end switch */ |
1032 | | |
1033 | | /* Set loc ID to invalid */ |
1034 | 0 | ref->loc_id = H5I_INVALID_HID; |
1035 | | |
1036 | | /* Set encoding size */ |
1037 | 0 | ref->encode_size = (uint32_t)decode_size; |
1038 | |
|
1039 | 0 | H5R_LOG_DEBUG("Decoded reference, filename=%s, obj_addr=%s, encode size=%u", ref->info.obj.filename, |
1040 | 0 | H5R__print_token(ref->info.obj.token), ref->encode_size); |
1041 | | |
1042 | | /* Set output info */ |
1043 | 0 | *nbytes = decode_size; |
1044 | |
|
1045 | 0 | done: |
1046 | 0 | if (ret_value < 0) { |
1047 | 0 | if (decoded_filename) { |
1048 | 0 | H5MM_xfree(ref->info.obj.filename); |
1049 | 0 | ref->info.obj.filename = NULL; |
1050 | 0 | } |
1051 | 0 | if (decoded_attrname) { |
1052 | 0 | H5MM_xfree(ref->info.attr.name); |
1053 | 0 | ref->info.attr.name = NULL; |
1054 | 0 | } |
1055 | 0 | if (decoded_dataspace) { |
1056 | 0 | if (H5S_close(ref->info.reg.space) < 0) |
1057 | 0 | HDONE_ERROR(H5E_REFERENCE, H5E_CLOSEERROR, FAIL, "unable to release dataspace"); |
1058 | 0 | ref->info.reg.space = NULL; |
1059 | 0 | } |
1060 | 0 | } |
1061 | |
|
1062 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1063 | 0 | } /* end H5R__decode() */ |
1064 | | |
1065 | | /*------------------------------------------------------------------------- |
1066 | | * Function: H5R__encode_obj_token |
1067 | | * |
1068 | | * Purpose: Encode an object address. |
1069 | | * |
1070 | | * Return: SUCCEED/FAIL |
1071 | | * |
1072 | | *------------------------------------------------------------------------- |
1073 | | */ |
1074 | | static herr_t |
1075 | | H5R__encode_obj_token(const H5O_token_t *obj_token, size_t token_size, unsigned char *buf, size_t *nalloc) |
1076 | 20 | { |
1077 | 20 | herr_t ret_value = SUCCEED; |
1078 | | |
1079 | 20 | FUNC_ENTER_PACKAGE_NOERR |
1080 | | |
1081 | 20 | assert(nalloc); |
1082 | | |
1083 | | /* Don't encode if buffer size isn't big enough or buffer is empty */ |
1084 | 20 | if (buf && *nalloc >= token_size) { |
1085 | 0 | uint8_t *p = (uint8_t *)buf; |
1086 | | |
1087 | | /* Encode token size */ |
1088 | 0 | *p++ = (uint8_t)(token_size & 0xff); |
1089 | | |
1090 | | /* Encode token */ |
1091 | 0 | H5MM_memcpy(p, obj_token, token_size); |
1092 | 0 | } |
1093 | 20 | *nalloc = token_size + sizeof(uint8_t); |
1094 | | |
1095 | 20 | FUNC_LEAVE_NOAPI(ret_value) |
1096 | 20 | } /* end H5R__encode_obj_token() */ |
1097 | | |
1098 | | /*------------------------------------------------------------------------- |
1099 | | * Function: H5R__decode_obj_token |
1100 | | * |
1101 | | * Purpose: Decode an object address. |
1102 | | * |
1103 | | * Return: SUCCEED/FAIL |
1104 | | * |
1105 | | *------------------------------------------------------------------------- |
1106 | | */ |
1107 | | static herr_t |
1108 | | H5R__decode_obj_token(const unsigned char *buf, size_t *nbytes, H5O_token_t *obj_token, uint8_t *token_size) |
1109 | 0 | { |
1110 | 0 | const uint8_t *p = (const uint8_t *)buf; |
1111 | 0 | herr_t ret_value = SUCCEED; |
1112 | |
|
1113 | 0 | FUNC_ENTER_PACKAGE |
1114 | |
|
1115 | 0 | assert(buf); |
1116 | 0 | assert(nbytes); |
1117 | 0 | assert(obj_token); |
1118 | 0 | assert(token_size); |
1119 | | |
1120 | | /* Don't decode if buffer size isn't big enough */ |
1121 | 0 | if (*nbytes < sizeof(uint8_t)) |
1122 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Buffer size is too small"); |
1123 | | |
1124 | | /* Get token size */ |
1125 | 0 | *token_size = *p++; |
1126 | 0 | if (*token_size > sizeof(H5O_token_t)) |
1127 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Invalid token size (%u)", *token_size); |
1128 | | |
1129 | | /* Make sure that token is initialized */ |
1130 | 0 | memset(obj_token, 0, sizeof(H5O_token_t)); |
1131 | | |
1132 | | /* Decode token */ |
1133 | 0 | H5MM_memcpy(obj_token, p, *token_size); |
1134 | |
|
1135 | 0 | *nbytes = (size_t)(*token_size + sizeof(uint8_t)); |
1136 | |
|
1137 | 0 | done: |
1138 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1139 | 0 | } /* end H5R__decode_obj_token() */ |
1140 | | |
1141 | | /*------------------------------------------------------------------------- |
1142 | | * Function: H5R__encode_region |
1143 | | * |
1144 | | * Purpose: Encode a selection. |
1145 | | * |
1146 | | * Return: SUCCEED/FAIL |
1147 | | * |
1148 | | *------------------------------------------------------------------------- |
1149 | | */ |
1150 | | static herr_t |
1151 | | H5R__encode_region(H5S_t *space, unsigned char *buf, size_t *nalloc) |
1152 | 0 | { |
1153 | 0 | uint8_t *p = NULL; /* Pointer to data to store */ |
1154 | 0 | hssize_t buf_size = 0; |
1155 | 0 | herr_t ret_value = SUCCEED; |
1156 | |
|
1157 | 0 | FUNC_ENTER_PACKAGE |
1158 | |
|
1159 | 0 | assert(space); |
1160 | 0 | assert(nalloc); |
1161 | | |
1162 | | /* Get the amount of space required to serialize the selection */ |
1163 | 0 | if ((buf_size = H5S_SELECT_SERIAL_SIZE(space)) < 0) |
1164 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTENCODE, FAIL, |
1165 | 0 | "Cannot determine amount of space needed for serializing selection"); |
1166 | | |
1167 | | /* Don't encode if buffer size isn't big enough or buffer is empty */ |
1168 | 0 | if (buf && *nalloc >= ((size_t)buf_size + 2 * sizeof(uint32_t))) { |
1169 | 0 | int rank; |
1170 | 0 | p = (uint8_t *)buf; |
1171 | | |
1172 | | /* Encode the size for safety check */ |
1173 | 0 | UINT32ENCODE(p, (uint32_t)buf_size); |
1174 | | |
1175 | | /* Encode the extent rank */ |
1176 | 0 | if ((rank = H5S_get_simple_extent_ndims(space)) < 0) |
1177 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTGET, FAIL, "can't get extent rank for selection"); |
1178 | 0 | UINT32ENCODE(p, (uint32_t)rank); |
1179 | | |
1180 | | /* Serialize the selection */ |
1181 | 0 | if (H5S_SELECT_SERIALIZE(space, (unsigned char **)&p) < 0) |
1182 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTENCODE, FAIL, "can't serialize selection"); |
1183 | 0 | } /* end if */ |
1184 | 0 | *nalloc = (size_t)buf_size + 2 * sizeof(uint32_t); |
1185 | |
|
1186 | 0 | done: |
1187 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1188 | 0 | } /* end H5R__encode_region() */ |
1189 | | |
1190 | | /*------------------------------------------------------------------------- |
1191 | | * Function: H5R__decode_region |
1192 | | * |
1193 | | * Purpose: Decode a selection. |
1194 | | * |
1195 | | * Return: SUCCEED/FAIL |
1196 | | * |
1197 | | *------------------------------------------------------------------------- |
1198 | | */ |
1199 | | static herr_t |
1200 | | H5R__decode_region(const unsigned char *buf, size_t *nbytes, H5S_t **space_ptr) |
1201 | 0 | { |
1202 | 0 | const uint8_t *p = (const uint8_t *)buf; |
1203 | 0 | const uint8_t *p_end = p + *nbytes - 1; |
1204 | 0 | size_t buf_size = 0; |
1205 | 0 | unsigned rank; |
1206 | 0 | H5S_t *space = NULL; |
1207 | 0 | herr_t ret_value = SUCCEED; |
1208 | |
|
1209 | 0 | FUNC_ENTER_PACKAGE |
1210 | |
|
1211 | 0 | assert(buf); |
1212 | 0 | assert(nbytes); |
1213 | 0 | assert(space_ptr); |
1214 | | |
1215 | | /* Don't decode if buffer size isn't big enough */ |
1216 | 0 | if (*nbytes < (2 * sizeof(uint32_t))) |
1217 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Buffer size is too small"); |
1218 | | |
1219 | | /* Decode the selection size */ |
1220 | 0 | UINT32DECODE(p, buf_size); |
1221 | 0 | buf_size += sizeof(uint32_t); |
1222 | | |
1223 | | /* Decode the extent rank */ |
1224 | 0 | UINT32DECODE(p, rank); |
1225 | 0 | buf_size += sizeof(uint32_t); |
1226 | | |
1227 | | /* Don't decode if buffer size isn't big enough */ |
1228 | 0 | if (*nbytes < buf_size) |
1229 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Buffer size is too small"); |
1230 | | |
1231 | | /* Deserialize the selection (dataspaces need the extent rank information) */ |
1232 | 0 | if (NULL == (space = H5S_create(H5S_SIMPLE))) |
1233 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Buffer size is too small"); |
1234 | 0 | if (H5S_set_extent_simple(space, rank, NULL, NULL) < 0) |
1235 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTSET, FAIL, "can't set extent rank for selection"); |
1236 | | |
1237 | 0 | if (p - 1 > p_end) |
1238 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Ran off end of buffer while decoding"); |
1239 | | |
1240 | 0 | if (H5S_SELECT_DESERIALIZE(&space, &p, (size_t)(p_end - p + 1)) < 0) |
1241 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "can't deserialize selection"); |
1242 | | |
1243 | 0 | *nbytes = buf_size; |
1244 | 0 | *space_ptr = space; |
1245 | |
|
1246 | 0 | done: |
1247 | 0 | if (ret_value < 0) |
1248 | 0 | if (space && H5S_close(space) < 0) |
1249 | 0 | HDONE_ERROR(H5E_REFERENCE, H5E_CLOSEERROR, FAIL, "unable to release dataspace"); |
1250 | |
|
1251 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1252 | 0 | } /* end H5R__decode_region() */ |
1253 | | |
1254 | | /*------------------------------------------------------------------------- |
1255 | | * Function: H5R__encode_string |
1256 | | * |
1257 | | * Purpose: Encode a string. |
1258 | | * |
1259 | | * Return: SUCCEED/FAIL |
1260 | | * |
1261 | | *------------------------------------------------------------------------- |
1262 | | */ |
1263 | | static herr_t |
1264 | | H5R__encode_string(const char *string, unsigned char *buf, size_t *nalloc) |
1265 | 0 | { |
1266 | 0 | size_t string_len, buf_size; |
1267 | 0 | herr_t ret_value = SUCCEED; |
1268 | |
|
1269 | 0 | FUNC_ENTER_PACKAGE |
1270 | |
|
1271 | 0 | assert(string); |
1272 | 0 | assert(nalloc); |
1273 | | |
1274 | | /* Get the amount of space required to serialize the string */ |
1275 | 0 | string_len = strlen(string); |
1276 | 0 | if (string_len > H5R_MAX_STRING_LEN) |
1277 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_ARGS, FAIL, "string too long"); |
1278 | | |
1279 | | /* Compute buffer size, allow for the attribute name length and object address */ |
1280 | 0 | buf_size = string_len + sizeof(uint16_t); |
1281 | |
|
1282 | 0 | if (buf && *nalloc >= buf_size) { |
1283 | 0 | uint8_t *p = (uint8_t *)buf; |
1284 | | /* Serialize information for string length into the buffer */ |
1285 | 0 | UINT16ENCODE(p, string_len); |
1286 | | /* Copy the string into the buffer */ |
1287 | 0 | H5MM_memcpy(p, string, string_len); |
1288 | 0 | } |
1289 | 0 | *nalloc = buf_size; |
1290 | |
|
1291 | 0 | done: |
1292 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1293 | 0 | } /* end H5R__encode_string() */ |
1294 | | |
1295 | | /*------------------------------------------------------------------------- |
1296 | | * Function: H5R__decode_string |
1297 | | * |
1298 | | * Purpose: Decode a string. |
1299 | | * |
1300 | | * Return: SUCCEED/FAIL |
1301 | | * |
1302 | | *------------------------------------------------------------------------- |
1303 | | */ |
1304 | | static herr_t |
1305 | | H5R__decode_string(const unsigned char *buf, size_t *nbytes, char **string_ptr) |
1306 | 0 | { |
1307 | 0 | const uint8_t *p = (const uint8_t *)buf; |
1308 | 0 | size_t string_len; |
1309 | 0 | char *string = NULL; |
1310 | 0 | herr_t ret_value = SUCCEED; |
1311 | |
|
1312 | 0 | FUNC_ENTER_PACKAGE |
1313 | |
|
1314 | 0 | assert(buf); |
1315 | 0 | assert(nbytes); |
1316 | 0 | assert(string_ptr); |
1317 | | |
1318 | | /* Don't decode if buffer size isn't big enough */ |
1319 | 0 | if (*nbytes < sizeof(uint16_t)) |
1320 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Buffer size is too small"); |
1321 | | |
1322 | | /* Get the string length */ |
1323 | 0 | UINT16DECODE(p, string_len); |
1324 | 0 | assert(string_len <= H5R_MAX_STRING_LEN); |
1325 | | |
1326 | | /* Allocate the string */ |
1327 | 0 | if (NULL == (string = (char *)H5MM_malloc(string_len + 1))) |
1328 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTALLOC, FAIL, "Cannot allocate string"); |
1329 | | |
1330 | | /* Copy the string */ |
1331 | 0 | H5MM_memcpy(string, p, string_len); |
1332 | 0 | string[string_len] = '\0'; |
1333 | |
|
1334 | 0 | *string_ptr = string; |
1335 | 0 | *nbytes = sizeof(uint16_t) + string_len; |
1336 | |
|
1337 | 0 | done: |
1338 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1339 | 0 | } /* end H5R__decode_string() */ |
1340 | | |
1341 | | /*------------------------------------------------------------------------- |
1342 | | * Function: H5R__encode_heap |
1343 | | * |
1344 | | * Purpose: Encode data and insert into heap (native only). |
1345 | | * |
1346 | | * Return: SUCCEED/FAIL |
1347 | | * |
1348 | | *------------------------------------------------------------------------- |
1349 | | */ |
1350 | | herr_t |
1351 | | H5R__encode_heap(H5F_t *f, unsigned char *buf, size_t *nalloc, const unsigned char *data, size_t data_size) |
1352 | 0 | { |
1353 | 0 | size_t buf_size; |
1354 | 0 | herr_t ret_value = SUCCEED; |
1355 | |
|
1356 | 0 | FUNC_ENTER_PACKAGE |
1357 | |
|
1358 | 0 | assert(f); |
1359 | 0 | assert(nalloc); |
1360 | |
|
1361 | 0 | buf_size = H5HG_HEAP_ID_SIZE(f); |
1362 | 0 | if (buf && *nalloc >= buf_size) { |
1363 | 0 | H5HG_t hobjid; |
1364 | 0 | uint8_t *p = (uint8_t *)buf; |
1365 | | |
1366 | | /* Write the reference information to disk (allocates space also) */ |
1367 | 0 | if (H5HG_insert(f, data_size, data, &hobjid) < 0) |
1368 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_WRITEERROR, FAIL, "Unable to write reference information"); |
1369 | | |
1370 | | /* Encode the heap information */ |
1371 | 0 | H5F_addr_encode(f, &p, hobjid.addr); |
1372 | 0 | UINT32ENCODE(p, hobjid.idx); |
1373 | 0 | } /* end if */ |
1374 | 0 | *nalloc = buf_size; |
1375 | |
|
1376 | 0 | done: |
1377 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1378 | 0 | } /* end H5R__encode_heap() */ |
1379 | | |
1380 | | /*------------------------------------------------------------------------- |
1381 | | * Function: H5R__decode_heap |
1382 | | * |
1383 | | * Purpose: Decode data inserted into heap (native only). |
1384 | | * |
1385 | | * Return: SUCCEED/FAIL |
1386 | | * |
1387 | | *------------------------------------------------------------------------- |
1388 | | */ |
1389 | | herr_t |
1390 | | H5R__decode_heap(H5F_t *f, const unsigned char *buf, size_t *nbytes, unsigned char **data_ptr, |
1391 | | size_t *data_size) |
1392 | 0 | { |
1393 | 0 | const uint8_t *p = (const uint8_t *)buf; |
1394 | 0 | H5HG_t hobjid; |
1395 | 0 | size_t buf_size; |
1396 | 0 | herr_t ret_value = SUCCEED; |
1397 | |
|
1398 | 0 | FUNC_ENTER_PACKAGE |
1399 | |
|
1400 | 0 | assert(f); |
1401 | 0 | assert(buf); |
1402 | 0 | assert(nbytes); |
1403 | 0 | assert(data_ptr); |
1404 | |
|
1405 | 0 | buf_size = H5HG_HEAP_ID_SIZE(f); |
1406 | | /* Don't decode if buffer size isn't big enough */ |
1407 | 0 | if (*nbytes < buf_size) |
1408 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Buffer size is too small"); |
1409 | | |
1410 | | /* Get the heap information */ |
1411 | 0 | H5F_addr_decode(f, &p, &(hobjid.addr)); |
1412 | 0 | if (!H5_addr_defined(hobjid.addr) || hobjid.addr == 0) |
1413 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "Undefined reference pointer"); |
1414 | 0 | UINT32DECODE(p, hobjid.idx); |
1415 | | |
1416 | | /* Read the information from disk */ |
1417 | 0 | if (NULL == (*data_ptr = (unsigned char *)H5HG_read(f, &hobjid, (void *)*data_ptr, data_size))) |
1418 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_READERROR, FAIL, "Unable to read reference data"); |
1419 | | |
1420 | 0 | *nbytes = buf_size; |
1421 | |
|
1422 | 0 | done: |
1423 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1424 | 0 | } /* end H5R__decode_heap() */ |
1425 | | |
1426 | | /*------------------------------------------------------------------------- |
1427 | | * Function: H5R__encode_token_obj_compat |
1428 | | * |
1429 | | * Purpose: Encode an object token. (native only) |
1430 | | * |
1431 | | * Return: SUCCEED/FAIL |
1432 | | * |
1433 | | *------------------------------------------------------------------------- |
1434 | | */ |
1435 | | herr_t |
1436 | | H5R__encode_token_obj_compat(const H5O_token_t *obj_token, size_t token_size, unsigned char *buf, |
1437 | | size_t *nalloc) |
1438 | 0 | { |
1439 | 0 | herr_t ret_value = SUCCEED; |
1440 | |
|
1441 | 0 | FUNC_ENTER_PACKAGE_NOERR |
1442 | |
|
1443 | 0 | assert(obj_token); |
1444 | 0 | assert(token_size); |
1445 | 0 | assert(nalloc); |
1446 | | |
1447 | | /* Don't encode if buffer size isn't big enough or buffer is empty */ |
1448 | 0 | if (buf && *nalloc >= token_size) |
1449 | 0 | H5MM_memcpy(buf, obj_token, token_size); |
1450 | |
|
1451 | 0 | *nalloc = token_size; |
1452 | |
|
1453 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1454 | 0 | } /* end H5R__encode_token_obj_compat() */ |
1455 | | |
1456 | | /*------------------------------------------------------------------------- |
1457 | | * Function: H5R__decode_token_obj_compat |
1458 | | * |
1459 | | * Purpose: Decode an object token. (native only) |
1460 | | * |
1461 | | * Return: SUCCEED/FAIL |
1462 | | * |
1463 | | *------------------------------------------------------------------------- |
1464 | | */ |
1465 | | herr_t |
1466 | | H5R__decode_token_obj_compat(const unsigned char *buf, size_t *nbytes, H5O_token_t *obj_token, |
1467 | | size_t token_size) |
1468 | 11.5k | { |
1469 | 11.5k | herr_t ret_value = SUCCEED; |
1470 | | |
1471 | 11.5k | FUNC_ENTER_PACKAGE |
1472 | | |
1473 | 11.5k | assert(buf); |
1474 | 11.5k | assert(nbytes); |
1475 | 11.5k | assert(obj_token); |
1476 | 11.5k | assert(token_size); |
1477 | | |
1478 | | /* Don't decode if buffer size isn't big enough */ |
1479 | 11.5k | if (*nbytes < token_size) |
1480 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Buffer size is too small"); |
1481 | | |
1482 | 11.5k | H5MM_memcpy(obj_token, buf, token_size); |
1483 | | |
1484 | 11.5k | *nbytes = token_size; |
1485 | | |
1486 | 11.5k | done: |
1487 | 11.5k | FUNC_LEAVE_NOAPI(ret_value) |
1488 | 11.5k | } /* H5R__decode_token_obj_compat() */ |
1489 | | |
1490 | | /*------------------------------------------------------------------------- |
1491 | | * Function: H5R__decode_token_region_compat |
1492 | | * |
1493 | | * Purpose: Decode dataset selection from data inserted into heap |
1494 | | * (native only). |
1495 | | * |
1496 | | * Return: SUCCEED/FAIL |
1497 | | * |
1498 | | *------------------------------------------------------------------------- |
1499 | | */ |
1500 | | herr_t |
1501 | | H5R__decode_token_region_compat(H5F_t *f, const unsigned char *buf, size_t *nbytes, H5O_token_t *obj_token, |
1502 | | size_t token_size, H5S_t **space_ptr) |
1503 | 0 | { |
1504 | 0 | unsigned char *data = NULL; |
1505 | 0 | H5O_token_t token = {0}; |
1506 | 0 | size_t data_size; |
1507 | 0 | const uint8_t *p = NULL; |
1508 | 0 | const uint8_t *p_end = NULL; |
1509 | 0 | H5S_t *space = NULL; |
1510 | 0 | herr_t ret_value = SUCCEED; |
1511 | |
|
1512 | 0 | FUNC_ENTER_PACKAGE |
1513 | |
|
1514 | 0 | assert(f); |
1515 | 0 | assert(buf); |
1516 | 0 | assert(nbytes); |
1517 | 0 | assert(token_size); |
1518 | | |
1519 | | /* Read from heap */ |
1520 | 0 | if (H5R__decode_heap(f, buf, nbytes, &data, &data_size) < 0) |
1521 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "invalid location identifier"); |
1522 | | |
1523 | | /* Get object address */ |
1524 | 0 | p = (const uint8_t *)data; |
1525 | 0 | p_end = p + data_size - 1; |
1526 | 0 | H5MM_memcpy(&token, p, token_size); |
1527 | 0 | p += token_size; |
1528 | |
|
1529 | 0 | if (space_ptr) { |
1530 | 0 | H5O_loc_t oloc; /* Object location */ |
1531 | | |
1532 | | /* Initialize the object location */ |
1533 | 0 | H5O_loc_reset(&oloc); |
1534 | 0 | oloc.file = f; |
1535 | |
|
1536 | 0 | if (H5VL_native_token_to_addr(f, H5I_FILE, token, &oloc.addr) < 0) |
1537 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTUNSERIALIZE, FAIL, |
1538 | 0 | "can't deserialize object token into address"); |
1539 | | |
1540 | | /* Open and copy the dataset's dataspace */ |
1541 | 0 | if (NULL == (space = H5S_read(&oloc))) |
1542 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_NOTFOUND, FAIL, "not found"); |
1543 | | |
1544 | | /* Unserialize the selection */ |
1545 | | |
1546 | 0 | if (p - 1 >= p_end) |
1547 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "Ran off end of buffer while deserializing"); |
1548 | | |
1549 | 0 | if (H5S_SELECT_DESERIALIZE(&space, &p, (size_t)(p_end - p + 1)) < 0) |
1550 | 0 | HGOTO_ERROR(H5E_REFERENCE, H5E_CANTDECODE, FAIL, "can't deserialize selection"); |
1551 | | |
1552 | 0 | *space_ptr = space; |
1553 | 0 | } |
1554 | 0 | if (obj_token) |
1555 | 0 | H5MM_memcpy(obj_token, &token, sizeof(H5O_token_t)); |
1556 | |
|
1557 | 0 | done: |
1558 | 0 | H5MM_free(data); |
1559 | |
|
1560 | 0 | if (ret_value < 0) { |
1561 | 0 | if (space && H5S_close(space) < 0) |
1562 | 0 | HDONE_ERROR(H5E_DATASET, H5E_CLOSEERROR, FAIL, "unable to release dataspace"); |
1563 | 0 | } |
1564 | |
|
1565 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1566 | 0 | } /* end H5R__decode_token_region_compat() */ |