1 /*
2  * (C) Copyright 2005- ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 
11 #include "grib_api_internal.h"
12 
13 /*
14    This is used by make_class.pl
15 
16    START_CLASS_DEF
17    CLASS      = accessor
18    SUPER      = grib_accessor_class_gen
19    IMPLEMENTS = init
20    IMPLEMENTS = dump;destroy
21    IMPLEMENTS = next_offset
22    IMPLEMENTS = byte_count; value_count
23    IMPLEMENTS = byte_offset; unpack_double
24    IMPLEMENTS = get_native_type
25    IMPLEMENTS = compare
26    IMPLEMENTS = pack_long; unpack_double; pack_double
27    MEMBERS    = const char* bufrDataEncodedName
28    MEMBERS    = const char* numberOfSubsetsName
29    MEMBERS    = const char* expandedDescriptorsName
30    MEMBERS    = const char* flagsName
31    MEMBERS    = const char* unitsName
32    MEMBERS    = const char* elementsDescriptorsIndexName
33    MEMBERS    = const char* compressedDataName
34    MEMBERS    = bufr_descriptors_array* expanded
35    MEMBERS    = grib_accessor* expandedAccessor
36    MEMBERS    = int* canBeMissing
37    MEMBERS    = long numberOfSubsets
38    MEMBERS    = long compressedData
39    MEMBERS    = grib_vdarray* numericValues
40    MEMBERS    = grib_vsarray* stringValues
41    MEMBERS    = grib_viarray* elementsDescriptorsIndex
42    MEMBERS    = int do_decode
43    MEMBERS    = int bitmapStartElementsDescriptorsIndex
44    MEMBERS    = int bitmapCurrentElementsDescriptorsIndex
45    MEMBERS    = int bitmapSize
46    MEMBERS    = int bitmapStart
47    MEMBERS    = int bitmapCurrent
48    MEMBERS    = grib_accessors_list* dataAccessors
49    MEMBERS    = int unpackMode
50    MEMBERS    = int bitsToEndData
51    MEMBERS    = grib_section* dataKeys
52    MEMBERS    = double* inputBitmap
53    MEMBERS    = int nInputBitmap
54    MEMBERS    = int iInputBitmap
55    MEMBERS    = long* inputReplications
56    MEMBERS    = int nInputReplications
57    MEMBERS    = int iInputReplications
58    MEMBERS    = long* inputExtendedReplications
59    MEMBERS    = int nInputExtendedReplications
60    MEMBERS    = int iInputExtendedReplications
61    MEMBERS    = long* inputShortReplications
62    MEMBERS    = int nInputShortReplications
63    MEMBERS    = int iInputShortReplications
64    MEMBERS    = grib_iarray* iss_list
65    MEMBERS    = grib_trie_with_rank* dataAccessorsTrie
66    MEMBERS    = grib_sarray* tempStrings
67    MEMBERS    = int change_ref_value_operand
68    MEMBERS    = size_t refValListSize
69    MEMBERS    = long* refValList
70    MEMBERS    = long refValIndex
71    MEMBERS    = bufr_tableb_override* tableb_override
72    MEMBERS    = int set_to_missing_if_out_of_range
73 
74    END_CLASS_DEF
75 
76  */
77 
78 /* START_CLASS_IMP */
79 
80 /*
81 
82 Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
83 Instead edit values between START_CLASS_DEF and END_CLASS_DEF
84 or edit "accessor.class" and rerun ./make_class.pl
85 
86 */
87 
88 static int get_native_type(grib_accessor*);
89 static int pack_double(grib_accessor*, const double* val, size_t* len);
90 static int pack_long(grib_accessor*, const long* val, size_t* len);
91 static int unpack_double(grib_accessor*, double* val, size_t* len);
92 static long byte_count(grib_accessor*);
93 static long byte_offset(grib_accessor*);
94 static long next_offset(grib_accessor*);
95 static int value_count(grib_accessor*, long*);
96 static void destroy(grib_context*, grib_accessor*);
97 static void dump(grib_accessor*, grib_dumper*);
98 static void init(grib_accessor*, const long, grib_arguments*);
99 static void init_class(grib_accessor_class*);
100 static int compare(grib_accessor*, grib_accessor*);
101 
102 typedef struct grib_accessor_bufr_data_array
103 {
104     grib_accessor att;
105     /* Members defined in gen */
106     /* Members defined in bufr_data_array */
107     const char* bufrDataEncodedName;
108     const char* numberOfSubsetsName;
109     const char* expandedDescriptorsName;
110     const char* flagsName;
111     const char* unitsName;
112     const char* elementsDescriptorsIndexName;
113     const char* compressedDataName;
114     bufr_descriptors_array* expanded;
115     grib_accessor* expandedAccessor;
116     int* canBeMissing;
117     long numberOfSubsets;
118     long compressedData;
119     grib_vdarray* numericValues;
120     grib_vsarray* stringValues;
121     grib_viarray* elementsDescriptorsIndex;
122     int do_decode;
123     int bitmapStartElementsDescriptorsIndex;
124     int bitmapCurrentElementsDescriptorsIndex;
125     int bitmapSize;
126     int bitmapStart;
127     int bitmapCurrent;
128     grib_accessors_list* dataAccessors;
129     int unpackMode;
130     int bitsToEndData;
131     grib_section* dataKeys;
132     double* inputBitmap;
133     int nInputBitmap;
134     int iInputBitmap;
135     long* inputReplications;
136     int nInputReplications;
137     int iInputReplications;
138     long* inputExtendedReplications;
139     int nInputExtendedReplications;
140     int iInputExtendedReplications;
141     long* inputShortReplications;
142     int nInputShortReplications;
143     int iInputShortReplications;
144     grib_iarray* iss_list;
145     grib_trie_with_rank* dataAccessorsTrie;
146     grib_sarray* tempStrings;
147     int change_ref_value_operand;
148     size_t refValListSize;
149     long* refValList;
150     long refValIndex;
151     bufr_tableb_override* tableb_override;
152     int set_to_missing_if_out_of_range;
153 } grib_accessor_bufr_data_array;
154 
155 extern grib_accessor_class* grib_accessor_class_gen;
156 
157 static grib_accessor_class _grib_accessor_class_bufr_data_array = {
158     &grib_accessor_class_gen,              /* super                     */
159     "bufr_data_array",                     /* name                      */
160     sizeof(grib_accessor_bufr_data_array), /* size                      */
161     0,                                     /* inited */
162     &init_class,                           /* init_class */
163     &init,                                 /* init                      */
164     0,                                     /* post_init                      */
165     &destroy,                              /* free mem                       */
166     &dump,                                 /* describes himself         */
167     &next_offset,                          /* get length of section     */
168     0,                                     /* get length of string      */
169     &value_count,                          /* get number of values      */
170     &byte_count,                           /* get number of bytes      */
171     &byte_offset,                          /* get offset to bytes           */
172     &get_native_type,                      /* get native type               */
173     0,                                     /* get sub_section                */
174     0,                                     /* grib_pack procedures long      */
175     0,                                     /* grib_pack procedures long      */
176     &pack_long,                            /* grib_pack procedures long      */
177     0,                                     /* grib_unpack procedures long    */
178     &pack_double,                          /* grib_pack procedures double    */
179     &unpack_double,                        /* grib_unpack procedures double  */
180     0,                                     /* grib_pack procedures string    */
181     0,                                     /* grib_unpack procedures string  */
182     0,                                     /* grib_pack array procedures string    */
183     0,                                     /* grib_unpack array procedures string  */
184     0,                                     /* grib_pack procedures bytes     */
185     0,                                     /* grib_unpack procedures bytes   */
186     0,                                     /* pack_expression */
187     0,                                     /* notify_change   */
188     0,                                     /* update_size   */
189     0,                                     /* preferred_size   */
190     0,                                     /* resize   */
191     0,                                     /* nearest_smaller_value */
192     0,                                     /* next accessor    */
193     &compare,                              /* compare vs. another accessor   */
194     0,                                     /* unpack only ith value          */
195     0,                                     /* unpack a subarray         */
196     0,                                     /* clear          */
197     0,                                     /* clone accessor          */
198 };
199 
200 
201 grib_accessor_class* grib_accessor_class_bufr_data_array = &_grib_accessor_class_bufr_data_array;
202 
203 
init_class(grib_accessor_class * c)204 static void init_class(grib_accessor_class* c)
205 {
206     c->string_length          = (*(c->super))->string_length;
207     c->sub_section            = (*(c->super))->sub_section;
208     c->pack_missing           = (*(c->super))->pack_missing;
209     c->is_missing             = (*(c->super))->is_missing;
210     c->unpack_long            = (*(c->super))->unpack_long;
211     c->pack_string            = (*(c->super))->pack_string;
212     c->unpack_string          = (*(c->super))->unpack_string;
213     c->pack_string_array      = (*(c->super))->pack_string_array;
214     c->unpack_string_array    = (*(c->super))->unpack_string_array;
215     c->pack_bytes             = (*(c->super))->pack_bytes;
216     c->unpack_bytes           = (*(c->super))->unpack_bytes;
217     c->pack_expression        = (*(c->super))->pack_expression;
218     c->notify_change          = (*(c->super))->notify_change;
219     c->update_size            = (*(c->super))->update_size;
220     c->preferred_size         = (*(c->super))->preferred_size;
221     c->resize                 = (*(c->super))->resize;
222     c->nearest_smaller_value  = (*(c->super))->nearest_smaller_value;
223     c->next                   = (*(c->super))->next;
224     c->unpack_double_element  = (*(c->super))->unpack_double_element;
225     c->unpack_double_subarray = (*(c->super))->unpack_double_subarray;
226     c->clear                  = (*(c->super))->clear;
227     c->make_clone             = (*(c->super))->make_clone;
228 }
229 
230 /* END_CLASS_IMP */
231 
232 #define MAX_NESTED_REPLICATIONS 8
233 
234 #define PROCESS_DECODE 0
235 #define PROCESS_NEW_DATA 1
236 #define PROCESS_ENCODE 2
237 
238 #define OVERRIDDEN_REFERENCE_VALUES_KEY "inputOverriddenReferenceValues"
239 
240 /* Set the error code, if it is bad and we should fail (default case), return */
241 /* variable 'err' is assumed to be pointer to int */
242 /* If BUFRDC mode is enabled, then we tolerate problems like wrong data section length */
243 #define CHECK_END_DATA_RETURN(ctx, bd, b, size, retval) \
244     {                                               \
245         *err = check_end_data(ctx, bd, b, size);        \
246         if (*err != 0 && ctx->bufrdc_mode == 0)     \
247             return retval;                          \
248     }
249 
250 static int process_elements(grib_accessor* a, int flag, long onlySubset, long startSubset, long endSubset);
251 
252 typedef int (*codec_element_proc)(grib_context* c, grib_accessor_bufr_data_array* self, int subsetIndex, grib_buffer* b, unsigned char* data, long* pos, int i, bufr_descriptor* descriptor, long elementIndex, grib_darray* dval, grib_sarray* sval);
253 typedef int (*codec_replication_proc)(grib_context* c, grib_accessor_bufr_data_array* self, int subsetIndex, grib_buffer* buff, unsigned char* data, long* pos, int i, long elementIndex, grib_darray* dval, long* numberOfRepetitions);
254 
255 static int create_keys(const grib_accessor* a, long onlySubset, long startSubset, long endSubset);
256 
restart_bitmap(grib_accessor_bufr_data_array * self)257 static void restart_bitmap(grib_accessor_bufr_data_array* self)
258 {
259     self->bitmapCurrent                         = -1;
260     self->bitmapCurrentElementsDescriptorsIndex = self->bitmapStartElementsDescriptorsIndex - 1;
261 }
262 
cancel_bitmap(grib_accessor_bufr_data_array * self)263 static void cancel_bitmap(grib_accessor_bufr_data_array* self)
264 {
265     self->bitmapCurrent = -1;
266     self->bitmapStart   = -1;
267 }
268 
is_bitmap_start_defined(grib_accessor_bufr_data_array * self)269 static int is_bitmap_start_defined(grib_accessor_bufr_data_array* self)
270 {
271     return self->bitmapStart == -1 ? 0 : 1;
272 }
273 
get_length(grib_accessor * a)274 static size_t get_length(grib_accessor* a)
275 {
276     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
277     size_t len                          = 0;
278 
279     grib_handle* h = grib_handle_of_accessor(a);
280 
281     grib_get_size(h, self->bufrDataEncodedName, &len);
282 
283     return len;
284 }
285 
286 /* Operator 203YYY: Store the TableB code and changed reference value in linked list */
tableB_override_store_ref_val(grib_context * c,grib_accessor_bufr_data_array * self,int code,long new_ref_val)287 static void tableB_override_store_ref_val(grib_context* c, grib_accessor_bufr_data_array* self, int code, long new_ref_val)
288 {
289     bufr_tableb_override* tb = (bufr_tableb_override*)grib_context_malloc_clear(c, sizeof(bufr_tableb_override));
290     tb->code                 = code;
291     tb->new_ref_val          = new_ref_val;
292     if (!self->tableb_override) {
293         self->tableb_override = tb;
294     }
295     else {
296         /*Add to end of linked list*/
297         bufr_tableb_override* q = self->tableb_override;
298         while (q->next)
299             q = q->next;
300         q->next = tb;
301     }
302 }
303 /* Operator 203YYY: Retrieve changed reference value from linked list */
tableB_override_get_ref_val(grib_accessor_bufr_data_array * self,int code,long * out_ref_val)304 static int tableB_override_get_ref_val(grib_accessor_bufr_data_array* self, int code, long* out_ref_val)
305 {
306     bufr_tableb_override* p = self->tableb_override;
307     while (p) {
308         if (p->code == code) {
309             *out_ref_val = p->new_ref_val;
310             return GRIB_SUCCESS;
311         }
312         p = p->next;
313     }
314     return GRIB_NOT_FOUND;
315 }
316 /* Operator 203YYY: Clear and free linked list */
tableB_override_clear(grib_context * c,grib_accessor_bufr_data_array * self)317 static void tableB_override_clear(grib_context* c, grib_accessor_bufr_data_array* self)
318 {
319     bufr_tableb_override* tb = self->tableb_override;
320     while (tb) {
321         bufr_tableb_override* n = tb->next;
322         grib_context_free(c, tb);
323         tb = n;
324     }
325     self->tableb_override = NULL;
326 }
327 /* Operator 203YYY: Copy contents of linked list to the transient array key */
tableB_override_set_key(grib_handle * h,grib_accessor_bufr_data_array * self)328 static int tableB_override_set_key(grib_handle* h, grib_accessor_bufr_data_array* self)
329 {
330     int err                  = GRIB_SUCCESS;
331     size_t size              = 0;
332     long* refVals            = NULL;
333     grib_iarray* refValArray = grib_iarray_new(h->context, 10, 10);
334     bufr_tableb_override* p  = self->tableb_override;
335     while (p) {
336         grib_iarray_push(refValArray, p->new_ref_val);
337         p = p->next;
338     }
339     size = grib_iarray_used_size(refValArray);
340     if (size > 0) {
341         refVals = grib_iarray_get_array(refValArray);
342         err     = grib_set_long_array(h, OVERRIDDEN_REFERENCE_VALUES_KEY, refVals, size);
343         grib_context_free(h->context, refVals);
344     }
345     grib_iarray_delete(refValArray);
346     return err;
347 }
348 /* Check numBits is sufficient for entries in the overridden reference values list*/
check_overridden_reference_values(const grib_context * c,long * refValList,size_t refValListSize,int numBits)349 static int check_overridden_reference_values(const grib_context* c, long* refValList, size_t refValListSize, int numBits)
350 {
351     const long maxval = (1 << (numBits - 1)) - 1;
352     const long minval = -(1 << (numBits - 1));
353     size_t i          = 0;
354     for (i = 0; i < refValListSize; ++i) {
355         grib_context_log(c, GRIB_LOG_DEBUG, "check_overridden_reference_values: refValList[%ld]=%ld", i, refValList[i]);
356         if (refValList[i] < minval || refValList[i] > maxval) {
357             grib_context_log(c, GRIB_LOG_ERROR, "Overridden reference value: entry %ld (%ld) does not fit in %d bits (specified by operator 203)",
358                              refValList[i], i, numBits);
359             return GRIB_OUT_OF_RANGE;
360         }
361     }
362     return GRIB_SUCCESS;
363 }
364 /*
365 static void tableB_override_dump(grib_accessor_bufr_data_array *self)
366 {
367     bufr_tableb_override* p = self->tableb_override;
368     int i = 1;
369     while (p) {
370         printf("ECCODES DEBUG: Table B Override: [%d] code=%d, rv=%ld\n", i, p->code, p->new_ref_val);
371         p = p->next;
372         ++i;
373     }
374 }
375  */
376 
377 #define DYN_ARRAY_SIZE_INIT 1000 /* Initial size for grib_iarray_new and grib_darray_new */
378 #define DYN_ARRAY_SIZE_INCR 1000 /* Increment size for grib_iarray_new and grib_darray_new */
379 
init(grib_accessor * a,const long v,grib_arguments * params)380 static void init(grib_accessor* a, const long v, grib_arguments* params)
381 {
382     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
383     int n                               = 0;
384     const char* dataKeysName            = NULL;
385     grib_accessor* dataKeysAcc          = NULL;
386 
387     self->bufrDataEncodedName          = grib_arguments_get_name(grib_handle_of_accessor(a), params, n++);
388     self->numberOfSubsetsName          = grib_arguments_get_name(grib_handle_of_accessor(a), params, n++);
389     self->expandedDescriptorsName      = grib_arguments_get_name(grib_handle_of_accessor(a), params, n++);
390     self->flagsName                    = grib_arguments_get_name(grib_handle_of_accessor(a), params, n++);
391     self->elementsDescriptorsIndexName = grib_arguments_get_name(grib_handle_of_accessor(a), params, n++);
392     self->compressedDataName           = grib_arguments_get_name(grib_handle_of_accessor(a), params, n++);
393     dataKeysName                       = grib_arguments_get_name(grib_handle_of_accessor(a), params, n++);
394 
395     dataKeysAcc                    = grib_find_accessor(grib_handle_of_accessor(a), dataKeysName);
396     self->dataKeys                 = dataKeysAcc->parent;
397     self->do_decode                = 1;
398     self->elementsDescriptorsIndex = 0;
399     self->numericValues            = 0;
400     self->stringValues             = 0;
401     cancel_bitmap(self);
402     self->expanded                       = 0;
403     self->expandedAccessor               = 0;
404     self->dataAccessorsTrie              = 0;
405     self->change_ref_value_operand       = 0;    /* Operator 203YYY: 0, 255 or YYY */
406     self->refValListSize                 = 0;    /* Operator 203YYY: size of overridden reference values array */
407     self->refValList                     = NULL; /* Operator 203YYY: overridden reference values array */
408     self->refValIndex                    = 0;    /* Operator 203YYY: index into overridden reference values array */
409     self->tableb_override                = NULL; /* Operator 203YYY: Table B lookup linked list */
410     self->set_to_missing_if_out_of_range = 0;    /* By default fail if out of range */
411 
412     a->length           = 0;
413     self->bitsToEndData = get_length(a) * 8;
414     self->unpackMode    = CODES_BUFR_UNPACK_STRUCTURE;
415     self->inputBitmap   = NULL;
416     /* Assert(a->length>=0); */
417 }
418 
419 /*
420 static void clean_string(char* s,int len)
421 {
422     int i=len-1;
423     while (i) {
424         if (!isalnum(s[i])) s[i]=0;
425         else break;
426         i--;
427     }
428 }
429  */
430 
check_end_data(grib_context * c,bufr_descriptor * bd,grib_accessor_bufr_data_array * self,int size)431 static int check_end_data(grib_context* c, bufr_descriptor* bd, grib_accessor_bufr_data_array* self, int size)
432 {
433     const int saved_bitsToEndData = self->bitsToEndData;
434     if (c->debug == 1)
435         grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: \tbitsToEndData=%d elementSize=%d", self->bitsToEndData, size);
436     self->bitsToEndData -= size;
437     if (self->bitsToEndData < 0) {
438         grib_context_log(c, GRIB_LOG_ERROR, "BUFR data decoding: Number of bits left=%d but element size=%d", saved_bitsToEndData, size);
439         if (bd)
440             grib_context_log(c, GRIB_LOG_ERROR, "BUFR data decoding: code=%06ld key=%s", bd->code, bd->shortName);
441         return GRIB_DECODING_ERROR;
442     }
443     return 0;
444 }
445 
self_clear(grib_context * c,grib_accessor_bufr_data_array * self)446 static void self_clear(grib_context* c, grib_accessor_bufr_data_array* self)
447 {
448     grib_context_free(c, self->canBeMissing);
449     grib_vdarray_delete_content(c, self->numericValues);
450     grib_vdarray_delete(c, self->numericValues);
451     if (self->stringValues) {
452         /*printf("dbg self_clear: clear %p\n", (void*)(self->stringValues));*/
453         grib_vsarray_delete_content(c, self->stringValues);
454         grib_vsarray_delete(c, self->stringValues);
455         self->stringValues = NULL;
456     }
457     grib_viarray_delete_content(c, self->elementsDescriptorsIndex);
458     grib_viarray_delete(c, self->elementsDescriptorsIndex);
459     if (self->inputReplications)
460         grib_context_free(c, self->inputReplications);
461     if (self->inputExtendedReplications)
462         grib_context_free(c, self->inputExtendedReplications);
463     if (self->inputShortReplications)
464         grib_context_free(c, self->inputShortReplications);
465     self->change_ref_value_operand = 0;
466     self->refValListSize           = 0;
467     if (self->refValList)
468         grib_context_free(c, self->refValList);
469     self->refValIndex = 0;
470     tableB_override_clear(c, self);
471     self->set_to_missing_if_out_of_range = 0;
472     if (self->inputBitmap) grib_context_free(c, self->inputBitmap);
473 }
474 
get_native_type(grib_accessor * a)475 static int get_native_type(grib_accessor* a)
476 {
477     return GRIB_TYPE_DOUBLE;
478 }
479 
byte_count(grib_accessor * a)480 static long byte_count(grib_accessor* a)
481 {
482     return 0;
483 }
484 
byte_offset(grib_accessor * a)485 static long byte_offset(grib_accessor* a)
486 {
487     return a->offset;
488 }
489 
next_offset(grib_accessor * a)490 static long next_offset(grib_accessor* a)
491 {
492     return a->offset;
493 }
494 
compare(grib_accessor * a,grib_accessor * b)495 static int compare(grib_accessor* a, grib_accessor* b)
496 {
497     return GRIB_NOT_IMPLEMENTED;
498 }
499 
pack_long(grib_accessor * a,const long * val,size_t * len)500 static int pack_long(grib_accessor* a, const long* val, size_t* len)
501 {
502     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
503     self->do_decode                     = 1;
504 
505     return GRIB_NOT_IMPLEMENTED;
506 }
507 
pack_double(grib_accessor * a,const double * val,size_t * len)508 static int pack_double(grib_accessor* a, const double* val, size_t* len)
509 {
510     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
511     self->do_decode                     = 1;
512     return process_elements(a, PROCESS_ENCODE, 0, 0, 0);
513 }
514 
accessor_bufr_data_array_get_stringValues(grib_accessor * a)515 grib_vsarray* accessor_bufr_data_array_get_stringValues(grib_accessor* a)
516 {
517     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
518     process_elements(a, PROCESS_DECODE, 0, 0, 0);
519     return self->stringValues;
520 }
521 
accessor_bufr_data_array_get_dataAccessors(grib_accessor * a)522 grib_accessors_list* accessor_bufr_data_array_get_dataAccessors(grib_accessor* a)
523 {
524     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
525     return self->dataAccessors;
526 }
527 
accessor_bufr_data_array_get_dataAccessorsTrie(grib_accessor * a)528 grib_trie_with_rank* accessor_bufr_data_array_get_dataAccessorsTrie(grib_accessor* a)
529 {
530     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
531     return self->dataAccessorsTrie;
532 }
533 
accessor_bufr_data_array_set_unpackMode(grib_accessor * a,int unpackMode)534 void accessor_bufr_data_array_set_unpackMode(grib_accessor* a, int unpackMode)
535 {
536     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
537     self->unpackMode                    = unpackMode;
538 }
539 
get_descriptors(grib_accessor * a)540 static int get_descriptors(grib_accessor* a)
541 {
542     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
543     int ret                             = 0, i, numberOfDescriptors;
544     grib_handle* h                      = grib_handle_of_accessor(a);
545     grib_context* c                     = a->context;
546 
547     if (!self->expandedAccessor)
548         self->expandedAccessor = grib_find_accessor(grib_handle_of_accessor(a), self->expandedDescriptorsName);
549     self->expanded = grib_accessor_class_expanded_descriptors_get_expanded(self->expandedAccessor, &ret);
550     if (ret != GRIB_SUCCESS)
551         return ret;
552 
553     numberOfDescriptors = grib_bufr_descriptors_array_used_size(self->expanded);
554     if (self->canBeMissing ) grib_context_free(c, self->canBeMissing);
555     self->canBeMissing  = (int*)grib_context_malloc_clear(c, numberOfDescriptors * sizeof(int));
556     for (i = 0; i < numberOfDescriptors; i++)
557         self->canBeMissing[i] = grib_bufr_descriptor_can_be_missing(self->expanded->v[i]);
558 
559     ret = grib_get_long(h, self->numberOfSubsetsName, &(self->numberOfSubsets));
560     if (ret != GRIB_SUCCESS)
561         return ret;
562     ret = grib_get_long(h, self->compressedDataName, &(self->compressedData));
563 
564     return ret;
565 }
566 
decode_string_array(grib_context * c,unsigned char * data,long * pos,bufr_descriptor * bd,grib_accessor_bufr_data_array * self)567 static int decode_string_array(grib_context* c, unsigned char* data, long* pos, bufr_descriptor* bd,
568                                grib_accessor_bufr_data_array* self)
569 {
570     int ret    = 0;
571     int* err   = &ret;
572     char* sval = 0;
573     int j, modifiedWidth, width;
574     grib_sarray* sa                        = grib_sarray_new(c, self->numberOfSubsets, 10);
575     int bufr_multi_element_constant_arrays = c->bufr_multi_element_constant_arrays;
576 
577     modifiedWidth = bd->width;
578 
579     sval = (char*)grib_context_malloc_clear(c, modifiedWidth / 8 + 1);
580     CHECK_END_DATA_RETURN(c, bd, self, modifiedWidth, *err);
581     if (*err) {
582         grib_sarray_push(c, sa, sval);
583         grib_vsarray_push(c, self->stringValues, sa);
584         return ret;
585     }
586     grib_decode_string(data, pos, modifiedWidth / 8, sval);
587     CHECK_END_DATA_RETURN(c, bd, self, 6, *err);
588     if (*err) {
589         grib_sarray_push(c, sa, sval);
590         grib_vsarray_push(c, self->stringValues, sa);
591         return ret;
592     }
593     width = grib_decode_unsigned_long(data, pos, 6);
594     if (width) {
595         CHECK_END_DATA_RETURN(c, bd, self, width * 8 * self->numberOfSubsets, *err);
596         if (*err) {
597             grib_sarray_push(c, sa, sval);
598             grib_vsarray_push(c, self->stringValues, sa);
599             return ret;
600         }
601         grib_context_free(c, sval);
602         for (j = 0; j < self->numberOfSubsets; j++) {
603             sval = (char*)grib_context_malloc_clear(c, width + 1);
604             grib_decode_string(data, pos, width, sval);
605             grib_sarray_push(c, sa, sval);
606         }
607     }
608     else {
609         if (bufr_multi_element_constant_arrays) {
610             for (j = 0; j < self->numberOfSubsets; j++) {
611                 char* pStr = sval;
612                 if (j > 0)
613                     pStr = strdup(sval);
614                 grib_sarray_push(c, sa, pStr);
615             }
616         }
617         else {
618             grib_sarray_push(c, sa, sval);
619         }
620     }
621     grib_vsarray_push(c, self->stringValues, sa);
622     return ret;
623 }
624 
decode_double_array(grib_context * c,unsigned char * data,long * pos,bufr_descriptor * bd,int canBeMissing,grib_accessor_bufr_data_array * self,int * err)625 static grib_darray* decode_double_array(grib_context* c, unsigned char* data, long* pos,
626                                         bufr_descriptor* bd, int canBeMissing,
627                                         grib_accessor_bufr_data_array* self, int* err)
628 {
629     grib_darray* ret = NULL;
630     int j;
631     size_t lval;
632     int localReference, localWidth, modifiedWidth, modifiedReference;
633     double modifiedFactor, dval;
634     int bufr_multi_element_constant_arrays = c->bufr_multi_element_constant_arrays;
635 
636     *err = 0;
637 
638     modifiedReference = bd->reference;
639     modifiedFactor    = bd->factor;
640     modifiedWidth     = bd->width;
641 
642     CHECK_END_DATA_RETURN(c, bd, self, modifiedWidth + 6, NULL);
643     if (*err) {
644         dval = GRIB_MISSING_DOUBLE;
645         lval = 0;
646         grib_context_log(c, GRIB_LOG_DEBUG, " modifiedWidth=%d lval=%ld dval=%g", modifiedWidth, lval, dval);
647         ret = grib_darray_new(c, DYN_ARRAY_SIZE_INIT, DYN_ARRAY_SIZE_INCR);
648         grib_darray_push(c, ret, dval);
649         *err = 0;
650         return ret;
651     }
652     lval           = grib_decode_size_t(data, pos, modifiedWidth);
653     localReference = (long)lval + modifiedReference;
654     localWidth     = grib_decode_unsigned_long(data, pos, 6);
655     grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: \tlocalWidth=%d", localWidth);
656     ret = grib_darray_new(c, self->numberOfSubsets, 50);
657     if (localWidth) {
658         CHECK_END_DATA_RETURN(c, bd, self, localWidth * self->numberOfSubsets, NULL);
659         if (*err) {
660             dval = GRIB_MISSING_DOUBLE;
661             lval = 0;
662             grib_context_log(c, GRIB_LOG_DEBUG, " modifiedWidth=%d lval=%ld dval=%g", modifiedWidth, lval, dval);
663             ret = grib_darray_new(c, DYN_ARRAY_SIZE_INIT, DYN_ARRAY_SIZE_INCR);
664             grib_darray_push(c, ret, dval);
665             *err = 0;
666             return ret;
667         }
668         for (j = 0; j < self->numberOfSubsets; j++) {
669             lval = grib_decode_size_t(data, pos, localWidth);
670             if (canBeMissing && grib_is_all_bits_one(lval, localWidth)) {
671                 dval = GRIB_MISSING_DOUBLE;
672             }
673             else {
674                 dval = ((long)lval + localReference) * modifiedFactor;
675             }
676             grib_darray_push(c, ret, dval);
677         }
678     }
679     else {
680         /* ECC-428 */
681         if (canBeMissing && grib_is_all_bits_one(lval, modifiedWidth)) {
682             dval = GRIB_MISSING_DOUBLE;
683         }
684         else {
685             dval = localReference * modifiedFactor;
686         }
687 
688         /* dataPresentIndicator is special and has to have SINGLE VALUE if constant array */
689         if (bufr_multi_element_constant_arrays == 1 && bd->code == 31031) {
690             bufr_multi_element_constant_arrays = 0;
691         }
692 
693         if (bufr_multi_element_constant_arrays) {
694             grib_context_log(c, GRIB_LOG_DEBUG, " modifiedWidth=%d lval=%ld dval=%g (const array multi values) %6.6ld", modifiedWidth, lval, dval, bd->code);
695             for (j = 0; j < self->numberOfSubsets; j++) {
696                 grib_darray_push(c, ret, dval);
697             }
698         }
699         else {
700             grib_context_log(c, GRIB_LOG_DEBUG, " modifiedWidth=%d lval=%ld dval=%g (const array single value) %6.6ld", modifiedWidth, lval, dval, bd->code);
701             grib_darray_push(c, ret, dval);
702         }
703     }
704 
705     return ret;
706 }
707 
encode_string_array(grib_context * c,grib_buffer * buff,long * pos,bufr_descriptor * bd,grib_accessor_bufr_data_array * self,grib_sarray * stringValues)708 static int encode_string_array(grib_context* c, grib_buffer* buff, long* pos, bufr_descriptor* bd,
709                                grib_accessor_bufr_data_array* self, grib_sarray* stringValues)
710 {
711     int err = 0, n, ival;
712     int k, j, modifiedWidth, width;
713 
714     if (self->iss_list == NULL) {
715         grib_context_log(c, GRIB_LOG_ERROR, "encode_string_array: self->iss_list==NULL");
716         return GRIB_INTERNAL_ERROR;
717     }
718     if (!stringValues) {
719         return GRIB_INTERNAL_ERROR;
720     }
721     n = grib_iarray_used_size(self->iss_list);
722 
723     if (n <= 0)
724         return GRIB_NO_VALUES;
725 
726     if (grib_sarray_used_size(stringValues) == 1) {
727         n    = 1;
728         ival = 0;
729     }
730     else {
731         ival = self->iss_list->v[0];
732     }
733 
734     if (n > grib_sarray_used_size(stringValues))
735         return GRIB_ARRAY_TOO_SMALL;
736 
737     modifiedWidth = bd->width;
738 
739     grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + modifiedWidth);
740     grib_encode_string(buff->data, pos, modifiedWidth / 8, stringValues->v[ival]);
741     width = n > 1 ? modifiedWidth : 0;
742 
743     grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + 6);
744     grib_encode_unsigned_longb(buff->data, width / 8, pos, 6);
745     if (width) {
746         grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + width * n);
747         for (j = 0; j < n; j++) {
748             k = self->iss_list->v[j];
749             grib_encode_string(buff->data, pos, width / 8, stringValues->v[k]);
750         }
751     }
752     return err;
753 }
754 
set_missing_long_to_double(grib_darray * dvalues)755 static void set_missing_long_to_double(grib_darray* dvalues)
756 {
757     size_t i, n = grib_darray_used_size(dvalues);
758     for (i = 0; i < n; i++) {
759         if (dvalues->v[i] == GRIB_MISSING_LONG)
760             dvalues->v[i] = GRIB_MISSING_DOUBLE;
761     }
762 }
763 
764 /* ECC-750: The 'factor' argument is 10^-scale */
descriptor_get_min_max(bufr_descriptor * bd,long width,long reference,double factor,double * minAllowed,double * maxAllowed)765 static int descriptor_get_min_max(bufr_descriptor* bd, long width, long reference, double factor,
766                                   double* minAllowed, double* maxAllowed)
767 {
768     /* Maximum value is allowed to be the largest number (all bits 1) which means it's MISSING */
769     unsigned long max1 = (1UL << width) - 1; /* Highest value for number with 'width' bits */
770     DebugAssert(width > 0 && width <= 32);
771 
772     *maxAllowed = (max1 + reference) * factor;
773     *minAllowed = reference * factor;
774     return GRIB_SUCCESS;
775 }
776 
encode_double_array(grib_context * c,grib_buffer * buff,long * pos,bufr_descriptor * bd,grib_accessor_bufr_data_array * self,grib_darray * dvalues)777 static int encode_double_array(grib_context* c, grib_buffer* buff, long* pos, bufr_descriptor* bd,
778                                grib_accessor_bufr_data_array* self, grib_darray* dvalues)
779 {
780     int err = 0;
781     int j, i;
782     size_t lval;
783     long localReference = 0, localWidth = 0, modifiedWidth, modifiedReference;
784     long reference, allone;
785     double localRange, modifiedFactor, inverseFactor;
786     size_t ii, index_of_min, index_of_max;
787     int nvals  = 0;
788     double min = 0, max = 0, maxAllowed, minAllowed;
789     double* v           = NULL;
790     double* values      = NULL;
791     int thereIsAMissing = 0;
792     int is_constant;
793     double val0;
794     /* ECC-379, ECC-830 */
795     const int dont_fail_if_out_of_range = self->set_to_missing_if_out_of_range;
796 
797     if (self->iss_list == NULL) {
798         grib_context_log(c, GRIB_LOG_ERROR, "encode_double_array: self->iss_list==NULL");
799         return GRIB_INTERNAL_ERROR;
800     }
801 
802     modifiedReference = bd->reference;
803     modifiedFactor    = bd->factor;
804     inverseFactor     = grib_power(bd->scale, 10);
805     modifiedWidth     = bd->width;
806 
807     descriptor_get_min_max(bd, modifiedWidth, modifiedReference, modifiedFactor, &minAllowed, &maxAllowed);
808 
809     nvals = grib_iarray_used_size(self->iss_list);
810     if (nvals <= 0)
811         return GRIB_NO_VALUES;
812 
813     if (!dvalues)
814         return GRIB_ENCODING_ERROR;
815 
816     set_missing_long_to_double(dvalues);
817 
818     v = dvalues->v;
819 
820     /* is constant */
821     if (grib_darray_is_constant(dvalues, modifiedFactor * .5)) {
822         localWidth = 0;
823         grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + modifiedWidth);
824         if (*v == GRIB_MISSING_DOUBLE) {
825             grib_set_bits_on(buff->data, pos, modifiedWidth);
826         }
827         else {
828             if (*v > maxAllowed || *v < minAllowed) {
829                 if (dont_fail_if_out_of_range) {
830                     fprintf(stderr,
831                             "ECCODES WARNING :  encode_double_array: %s. Value (%g) out of range (minAllowed=%g, maxAllowed=%g)."
832                             " Setting it to missing value\n",
833                             bd->shortName, *v, minAllowed, maxAllowed);
834                     grib_set_bits_on(buff->data, pos, modifiedWidth);
835                 }
836                 else {
837                     grib_context_log(c, GRIB_LOG_ERROR, "encode_double_array: %s. Value (%g) out of range (minAllowed=%g, maxAllowed=%g).",
838                                      bd->shortName, *v, minAllowed, maxAllowed);
839                     return GRIB_OUT_OF_RANGE; /* ECC-611 */
840                 }
841             }
842             else {
843                 lval = round(*v * inverseFactor) - modifiedReference;
844                 grib_encode_size_tb(buff->data, lval, pos, modifiedWidth);
845             }
846         }
847         grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + 6);
848         grib_encode_unsigned_longb(buff->data, localWidth, pos, 6);
849         return err;
850     }
851 
852     if (nvals > grib_darray_used_size(dvalues))
853         return GRIB_ARRAY_TOO_SMALL;
854     values      = (double*)grib_context_malloc_clear(c, sizeof(double) * nvals);
855     val0        = dvalues->v[self->iss_list->v[0]];
856     is_constant = 1;
857     for (i = 0; i < nvals; i++) {
858         values[i] = dvalues->v[self->iss_list->v[i]];
859         if (val0 != values[i])
860             is_constant = 0;
861     }
862     v = values;
863 
864     /* encoding a range with constant values*/
865     if (is_constant == 1) {
866         localWidth = 0;
867         grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + modifiedWidth);
868         if (*v == GRIB_MISSING_DOUBLE) {
869             grib_set_bits_on(buff->data, pos, modifiedWidth);
870         }
871         else {
872             lval = round(*v * inverseFactor) - modifiedReference;
873             grib_encode_size_tb(buff->data, lval, pos, modifiedWidth);
874         }
875         grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + 6);
876         grib_encode_unsigned_longb(buff->data, localWidth, pos, 6);
877         grib_context_free(c, values);
878         return err;
879     }
880 
881     ii = 0;
882     while (ii < nvals && *v == GRIB_MISSING_DOUBLE) {
883         thereIsAMissing = 1;
884         v++;
885         ii++;
886     }
887     /* ECC-379: First determine if we need to change any out-of-range value */
888     if (dont_fail_if_out_of_range) {
889         while (ii < nvals) {
890             /* Turn out-of-range values into 'missing' */
891             if (*v != GRIB_MISSING_DOUBLE && (*v < minAllowed || *v > maxAllowed)) {
892                 fprintf(stderr,
893                         "ECCODES WARNING :  encode_double_array: %s. Value at index %ld (%g) out of range (minAllowed=%g, maxAllowed=%g)."
894                         " Setting it to missing value\n",
895                         bd->shortName, (long)ii, *v, minAllowed, maxAllowed);
896                 *v = GRIB_MISSING_DOUBLE;
897             }
898             ii++;
899             v++;
900         }
901     }
902     /* Determine min and max values. */
903     /* Note: value[0] could be missing so cannot set min/max to this.
904      * Find first value which is non-missing as a starting point */
905     for (i = 0; i < nvals; i++) {
906         if (values[i] != GRIB_MISSING_DOUBLE) {
907             min = max = values[i];
908             break;
909         }
910     }
911     ii           = 0;
912     index_of_min = index_of_max = 0;
913     v                           = values;
914     while (ii < nvals) {
915         if (*v < min && *v != GRIB_MISSING_DOUBLE) {
916             min          = *v;
917             index_of_min = ii;
918         }
919         if (*v > max && *v != GRIB_MISSING_DOUBLE) {
920             max          = *v;
921             index_of_max = ii;
922         }
923         if (*v == GRIB_MISSING_DOUBLE)
924             thereIsAMissing = 1;
925         ii++;
926         v++;
927     }
928     if (max > maxAllowed && max != GRIB_MISSING_DOUBLE) {
929         grib_context_log(c, GRIB_LOG_ERROR, "encode_double_array: %s. Maximum value (value[%lu]=%g) out of range (maxAllowed=%g).",
930                          bd->shortName, index_of_max, max, maxAllowed);
931         return GRIB_OUT_OF_RANGE;
932     }
933     if (min < minAllowed && min != GRIB_MISSING_DOUBLE) {
934         grib_context_log(c, GRIB_LOG_ERROR, "encode_double_array: %s. Minimum value (value[%lu]=%g) out of range (minAllowed=%g).",
935                          bd->shortName, index_of_min, min, minAllowed);
936         return GRIB_OUT_OF_RANGE;
937     }
938 
939     reference      = round(min * inverseFactor);
940     localReference = reference - modifiedReference;
941     if (max != min) {
942         localRange = (max - min) * inverseFactor + 1;
943         localWidth = ceil(log(localRange) / log(2.0));
944         lval       = round(max * inverseFactor) - reference;
945         allone     = grib_power(localWidth, 2) - 1;
946         while (allone <= lval) {
947             localWidth++;
948             allone = grib_power(localWidth, 2) - 1;
949         }
950         if (localWidth == 1)
951             localWidth++;
952     }
953     else {
954         if (thereIsAMissing == 1)
955             localWidth = 1;
956         else
957             localWidth = 0;
958     }
959 
960     grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + modifiedWidth);
961     if (localWidth) {
962         grib_encode_unsigned_longb(buff->data, localReference, pos, modifiedWidth);
963     }
964     else {
965         if (min == GRIB_MISSING_DOUBLE) {
966             grib_set_bits_on(buff->data, pos, modifiedWidth);
967         }
968         else {
969             lval = localReference - modifiedReference;
970             grib_encode_size_tb(buff->data, lval, pos, modifiedWidth);
971         }
972     }
973     grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + 6);
974     grib_encode_unsigned_longb(buff->data, localWidth, pos, 6);
975 
976     if (localWidth) {
977         grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + nvals * localWidth);
978         for (j = 0; j < nvals; j++) {
979             if (values[j] == GRIB_MISSING_DOUBLE) {
980                 grib_set_bits_on(buff->data, pos, localWidth);
981             }
982             else {
983                 lval = round(values[j] * inverseFactor) - reference;
984                 grib_encode_size_tb(buff->data, lval, pos, localWidth);
985             }
986         }
987     }
988 
989     grib_context_free(c, values);
990 
991     return err;
992 }
993 
encode_double_value(grib_context * c,grib_buffer * buff,long * pos,bufr_descriptor * bd,grib_accessor_bufr_data_array * self,double value)994 static int encode_double_value(grib_context* c, grib_buffer* buff, long* pos, bufr_descriptor* bd,
995                                grib_accessor_bufr_data_array* self, double value)
996 {
997     size_t lval;
998     double maxAllowed, minAllowed;
999     int err = 0;
1000     int modifiedWidth, modifiedReference;
1001     double modifiedFactor;
1002     /* ECC-379, ECC-830 */
1003     const int dont_fail_if_out_of_range = self->set_to_missing_if_out_of_range;
1004 
1005     modifiedReference = bd->reference;
1006     modifiedFactor    = bd->factor;
1007     modifiedWidth     = bd->width;
1008 
1009     descriptor_get_min_max(bd, modifiedWidth, modifiedReference, modifiedFactor, &minAllowed, &maxAllowed);
1010 
1011     grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + modifiedWidth);
1012     if (value == GRIB_MISSING_DOUBLE) {
1013         grib_set_bits_on(buff->data, pos, modifiedWidth);
1014     }
1015     else if (value > maxAllowed || value < minAllowed) {
1016         if (dont_fail_if_out_of_range) {
1017             fprintf(stderr,
1018                     "ECCODES WARNING :  encode_double_value: %s. Value (%g) out of range (minAllowed=%g, maxAllowed=%g)."
1019                     " Setting it to missing value\n",
1020                     bd->shortName, value, minAllowed, maxAllowed);
1021             /* Ignore the bad value and instead use 'missing' */
1022             grib_set_bits_on(buff->data, pos, modifiedWidth);
1023         }
1024         else {
1025             grib_context_log(c, GRIB_LOG_ERROR, "encode_double_value: %s. Value (%g) out of range (minAllowed=%g, maxAllowed=%g).",
1026                              bd->shortName, value, minAllowed, maxAllowed);
1027             return GRIB_OUT_OF_RANGE;
1028         }
1029     }
1030     else {
1031         lval = round(value / modifiedFactor) - modifiedReference;
1032         if (c->debug)
1033             grib_context_log(c, GRIB_LOG_DEBUG, "encode_double_value %s: value=%.15f lval=%lu\n", bd->shortName, value, lval);
1034         grib_encode_size_tb(buff->data, lval, pos, modifiedWidth);
1035     }
1036 
1037     return err;
1038 }
1039 
encode_string_value(grib_context * c,grib_buffer * buff,long * pos,bufr_descriptor * bd,grib_accessor_bufr_data_array * self,char * sval)1040 static int encode_string_value(grib_context* c, grib_buffer* buff, long* pos, bufr_descriptor* bd,
1041                                grib_accessor_bufr_data_array* self, char* sval)
1042 {
1043     int err = 0;
1044     int len;
1045 
1046     len = bd->width / 8;
1047     grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + bd->width);
1048 
1049     grib_encode_string(buff->data, pos, len, sval);
1050 
1051     return err;
1052 }
1053 
decode_string_value(grib_context * c,unsigned char * data,long * pos,bufr_descriptor * bd,grib_accessor_bufr_data_array * self,int * err)1054 static char* decode_string_value(grib_context* c, unsigned char* data, long* pos, bufr_descriptor* bd,
1055                                  grib_accessor_bufr_data_array* self, int* err)
1056 {
1057     char* sval = 0;
1058     int len;
1059 
1060     *err = 0;
1061 
1062     len = bd->width / 8;
1063 
1064     CHECK_END_DATA_RETURN(c, bd, self, bd->width, NULL);
1065     sval = (char*)grib_context_malloc_clear(c, len + 1);
1066     if (*err) {
1067         *err = 0;
1068         return sval;
1069     }
1070     grib_decode_string(data, pos, len, sval);
1071 
1072     /* clean_string(sval,len); */
1073 
1074     return sval;
1075 }
1076 
decode_double_value(grib_context * c,unsigned char * data,long * pos,bufr_descriptor * bd,int canBeMissing,grib_accessor_bufr_data_array * self,int * err)1077 static double decode_double_value(grib_context* c, unsigned char* data, long* pos,
1078                                   bufr_descriptor* bd, int canBeMissing,
1079                                   grib_accessor_bufr_data_array* self, int* err)
1080 {
1081     size_t lval;
1082     int modifiedWidth, modifiedReference;
1083     double modifiedFactor;
1084     double dval = 0;
1085 
1086     *err = 0;
1087 
1088     modifiedReference = bd->reference;
1089     modifiedFactor    = bd->factor;
1090     modifiedWidth     = bd->width;
1091 
1092     CHECK_END_DATA_RETURN(c, bd, self, modifiedWidth, 0);
1093     if (*err) {
1094         *err = 0;
1095         return GRIB_MISSING_DOUBLE;
1096     }
1097 
1098     lval = grib_decode_size_t(data, pos, modifiedWidth);
1099     if (canBeMissing && grib_is_all_bits_one(lval, modifiedWidth)) {
1100         dval = GRIB_MISSING_DOUBLE;
1101     }
1102     else {
1103         dval = ((long)lval + modifiedReference) * modifiedFactor;
1104     }
1105     return dval;
1106 }
1107 
decode_element(grib_context * c,grib_accessor_bufr_data_array * self,int subsetIndex,grib_buffer * b,unsigned char * data,long * pos,int i,bufr_descriptor * descriptor,long elementIndex,grib_darray * dval,grib_sarray * sval)1108 static int decode_element(grib_context* c, grib_accessor_bufr_data_array* self, int subsetIndex,
1109                           grib_buffer* b, unsigned char* data, long* pos, int i, bufr_descriptor* descriptor, long elementIndex,
1110                           grib_darray* dval, grib_sarray* sval)
1111 {
1112     grib_accessor* a    = (grib_accessor*)self;
1113     grib_darray* dar    = 0;
1114     grib_sarray* sar    = 0;
1115     int index           = 0, ii, stringValuesLen;
1116     char* csval         = 0;
1117     double cdval        = 0, x;
1118     int err             = 0;
1119     bufr_descriptor* bd = descriptor == NULL ? self->expanded->v[i] : descriptor;
1120     /* Assert( b->data == data); */
1121 
1122     if (self->change_ref_value_operand > 0 && self->change_ref_value_operand != 255) {
1123         /* Operator 203YYY: Change Reference Values: Definition phase */
1124         const int number_of_bits = self->change_ref_value_operand;
1125         long new_ref_val         = grib_decode_signed_longb(data, pos, number_of_bits);
1126         grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: -**- \tcode=203YYY width=%d pos=%ld -> %ld",
1127                          number_of_bits, (long)*pos, (long)(*pos - a->offset * 8));
1128         grib_context_log(c, GRIB_LOG_DEBUG, "Operator 203YYY: Store for code %6.6ld => new ref val %ld", bd->code, new_ref_val);
1129         tableB_override_store_ref_val(c, self, bd->code, new_ref_val);
1130         bd->nokey = 1;
1131         err       = check_end_data(c, NULL, self, number_of_bits); /*advance bitsToEnd*/
1132         return err;
1133     }
1134     grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: -%d- \tcode=%6.6ld width=%ld scale=%ld ref=%ld type=%d (pos=%ld -> %ld)",
1135                      i, bd->code, bd->width, bd->scale, bd->reference, bd->type,
1136                      (long)*pos, (long)(*pos - a->offset * 8));
1137     if (bd->type == BUFR_DESCRIPTOR_TYPE_STRING) {
1138         /* string */
1139         if (self->compressedData) {
1140             err   = decode_string_array(c, data, pos, bd, self);
1141             index = grib_vsarray_used_size(self->stringValues);
1142             dar   = grib_darray_new(c, self->numberOfSubsets, 10);
1143             index = self->numberOfSubsets * (index - 1);
1144             for (ii = 1; ii <= self->numberOfSubsets; ii++) {
1145                 x = (index + ii) * 1000 + bd->width / 8;
1146                 grib_darray_push(c, dar, x);
1147             }
1148             grib_vdarray_push(c, self->numericValues, dar);
1149         }
1150         else {
1151             csval = decode_string_value(c, data, pos, bd, self, &err);
1152             grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: \t %s = %s",
1153                              bd->shortName, csval);
1154             sar = grib_sarray_push(c, sar, csval);
1155             grib_vsarray_push(c, self->stringValues, sar);
1156             stringValuesLen = grib_vsarray_used_size(self->stringValues);
1157             index           = 0;
1158             for (ii = 0; ii < stringValuesLen; ii++) {
1159                 index += grib_sarray_used_size(self->stringValues->v[ii]);
1160             }
1161             cdval = index * 1000 + bd->width / 8;
1162             grib_darray_push(c, dval, cdval);
1163         }
1164     }
1165     else {
1166         /* numeric or codetable or flagtable */
1167         /* Operator 203YYY: Check if we have changed ref value for this element. If so modify bd->reference */
1168         if (self->change_ref_value_operand != 0 && tableB_override_get_ref_val(self, bd->code, &(bd->reference)) == GRIB_SUCCESS) {
1169             grib_context_log(c, GRIB_LOG_DEBUG, "Operator 203YYY: For code %6.6ld, changed ref val: %ld", bd->code, bd->reference);
1170         }
1171 
1172         if (bd->width > 64) {
1173             grib_context_log(c, GRIB_LOG_ERROR, "Descriptor %6.6ld has bit width %ld!", bd->code, bd->width);
1174             return GRIB_DECODING_ERROR;
1175         }
1176         if (self->compressedData) {
1177             dar = decode_double_array(c, data, pos, bd, self->canBeMissing[i], self, &err);
1178             grib_vdarray_push(c, self->numericValues, dar);
1179         }
1180         else {
1181             /* Uncompressed */
1182             cdval = decode_double_value(c, data, pos, bd, self->canBeMissing[i], self, &err);
1183             grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: \t %s = %g",
1184                              bd->shortName, cdval);
1185             grib_darray_push(c, dval, cdval);
1186         }
1187     }
1188     return err;
1189 }
1190 
decode_replication(grib_context * c,grib_accessor_bufr_data_array * self,int subsetIndex,grib_buffer * buff,unsigned char * data,long * pos,int i,long elementIndex,grib_darray * dval,long * numberOfRepetitions)1191 static int decode_replication(grib_context* c, grib_accessor_bufr_data_array* self, int subsetIndex,
1192                               grib_buffer* buff, unsigned char* data, long* pos, int i, long elementIndex, grib_darray* dval, long* numberOfRepetitions)
1193 {
1194     int ret = 0;
1195     int* err;
1196     int localReference, width;
1197     bufr_descriptor** descriptors = 0;
1198     err                           = &ret;
1199     descriptors                   = self->expanded->v;
1200 
1201     /* Assert(buff->data == data); */
1202 
1203     grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: -%d- \tcode=%6.6ld width=%ld ",
1204                      i, self->expanded->v[i]->code, self->expanded->v[i]->width);
1205     if (self->compressedData) {
1206         grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: \tdelayed replication localReference width=%ld", descriptors[i]->width);
1207         CHECK_END_DATA_RETURN(c, NULL, self, descriptors[i]->width + 6, *err);
1208         if (*err) {
1209             *numberOfRepetitions = 0;
1210         }
1211         else {
1212             localReference = grib_decode_unsigned_long(data, pos, descriptors[i]->width) + descriptors[i]->reference;
1213             grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: \tdelayed replication localWidth width=6");
1214             width = grib_decode_unsigned_long(data, pos, 6);
1215             if (width) {
1216                 grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: \tdelayed replication is NOT constant for compressed data!");
1217                 /* delayed replication number is not constant. NOT IMPLEMENTED */
1218                 return GRIB_NOT_IMPLEMENTED;
1219             }
1220             else {
1221                 *numberOfRepetitions = localReference * descriptors[i]->factor;
1222                 grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: \tdelayed replication value=%ld", *numberOfRepetitions);
1223             }
1224         }
1225     }
1226     else {
1227         CHECK_END_DATA_RETURN(c, NULL, self, descriptors[i]->width, *err);
1228         if (*err) {
1229             *numberOfRepetitions = 0;
1230         }
1231         else {
1232             *numberOfRepetitions = grib_decode_unsigned_long(data, pos, descriptors[i]->width) +
1233                                    descriptors[i]->reference * descriptors[i]->factor;
1234             grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data decoding: \tdelayed replication value=%ld", *numberOfRepetitions);
1235         }
1236     }
1237     if (self->compressedData) {
1238         dval = grib_darray_new(c, 1, 100);
1239         if (c->bufr_multi_element_constant_arrays) {
1240             long j;
1241             for (j = 0; j < self->numberOfSubsets; j++) {
1242                 grib_darray_push(c, dval, (double)(*numberOfRepetitions));
1243             }
1244         }
1245         else {
1246             grib_darray_push(c, dval, (double)(*numberOfRepetitions));
1247         }
1248         grib_vdarray_push(c, self->numericValues, dval);
1249     }
1250     else {
1251         grib_darray_push(c, dval, (double)(*numberOfRepetitions));
1252     }
1253     return ret;
1254 }
1255 
encode_new_bitmap(grib_context * c,grib_buffer * buff,long * pos,int idx,grib_accessor_bufr_data_array * self)1256 static int encode_new_bitmap(grib_context* c, grib_buffer* buff, long* pos, int idx, grib_accessor_bufr_data_array* self)
1257 {
1258     grib_darray* doubleValues = NULL;
1259     int err                   = 0;
1260     double cdval              = 0;
1261     if (self->nInputBitmap > 0) {
1262         if (self->nInputBitmap < self->iInputBitmap)
1263             return GRIB_ARRAY_TOO_SMALL;
1264         cdval = self->inputBitmap[self->iInputBitmap++];
1265     }
1266     if (self->compressedData) {
1267         doubleValues = grib_darray_new(c, 1, 1);
1268         grib_darray_push(c, doubleValues, cdval);
1269         err = encode_double_array(c, buff, pos, self->expanded->v[idx], self, doubleValues);
1270         grib_darray_delete(c, doubleValues);
1271     }
1272     else {
1273         err = encode_double_value(c, buff, pos, self->expanded->v[idx], self, cdval);
1274     }
1275     return err;
1276 }
1277 
1278 /* Operator 203YYY: Change Reference Values: Encoding definition phase */
encode_overridden_reference_value(grib_context * c,grib_accessor_bufr_data_array * self,grib_buffer * buff,long * pos,bufr_descriptor * bd)1279 static int encode_overridden_reference_value(grib_context* c, grib_accessor_bufr_data_array* self,
1280                                              grib_buffer* buff, long* pos, bufr_descriptor* bd)
1281 {
1282     int err         = 0;
1283     long currRefVal = -1;
1284     long numBits    = self->change_ref_value_operand;
1285     /* We must be encoding between 203YYY and 203255 */
1286     Assert(self->change_ref_value_operand > 0 && self->change_ref_value_operand != 255);
1287     if (self->refValListSize == 0) {
1288         grib_context_log(c, GRIB_LOG_ERROR,
1289                          "encode_new_element: Overridden Reference Values array is empty! "
1290                          "(Hint: set the key '%s')",
1291                          OVERRIDDEN_REFERENCE_VALUES_KEY);
1292         grib_context_log(c, GRIB_LOG_ERROR,
1293                          "The number of overridden reference values must be equal to "
1294                          "number of descriptors between operator 203YYY and 203255");
1295         return GRIB_ENCODING_ERROR;
1296     }
1297     if (self->refValIndex >= self->refValListSize) {
1298         grib_context_log(c, GRIB_LOG_ERROR,
1299                          "encode_new_element: Overridden Reference Values: index=%ld, size=%ld. "
1300                          "\nThe number of overridden reference values must be equal to "
1301                          "number of descriptors between operator 203YYY and 203255",
1302                          self->refValIndex, self->refValListSize);
1303         return GRIB_ENCODING_ERROR;
1304     }
1305     currRefVal = self->refValList[self->refValIndex];
1306     grib_context_log(c, GRIB_LOG_DEBUG, "encode_new_element: Operator 203YYY: writing ref val %ld (self->refValIndex=%ld)",
1307                      currRefVal, self->refValIndex);
1308     grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + numBits);
1309     err = grib_encode_signed_longb(buff->data, currRefVal, pos, numBits);
1310     if (err) {
1311         grib_context_log(c, GRIB_LOG_ERROR, "Encoding overridden reference value %ld for %s (code=%6.6ld)",
1312                          currRefVal, bd->shortName, bd->code);
1313     }
1314     self->refValIndex++;
1315     return err;
1316 }
1317 
encode_new_element(grib_context * c,grib_accessor_bufr_data_array * self,int subsetIndex,grib_buffer * buff,unsigned char * data,long * pos,int i,bufr_descriptor * descriptor,long elementIndex,grib_darray * dval,grib_sarray * sval)1318 static int encode_new_element(grib_context* c, grib_accessor_bufr_data_array* self, int subsetIndex,
1319                               grib_buffer* buff, unsigned char* data, long* pos, int i, bufr_descriptor* descriptor,
1320                               long elementIndex, grib_darray* dval, grib_sarray* sval)
1321 {
1322     int ii;
1323     char* csval               = 0;
1324     unsigned char missingChar = 0xFF;
1325     double cdval              = GRIB_MISSING_DOUBLE;
1326     int err                   = 0;
1327     size_t slen;
1328     bufr_descriptor* bd = descriptor == NULL ? self->expanded->v[i] : descriptor;
1329 
1330     grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data encoding: \tcode=%6.6ld width=%ld pos=%ld ulength=%ld ulength_bits=%ld",
1331                      bd->code, bd->width, (long)*pos, buff->ulength, buff->ulength_bits);
1332 
1333     if (self->change_ref_value_operand > 0 && self->change_ref_value_operand != 255) {
1334         /* Operator 203YYY: Change Reference Values: Encoding definition phase */
1335         err = encode_overridden_reference_value(c, self, buff, pos, bd);
1336         return err;
1337     }
1338 
1339     if (bd->type == BUFR_DESCRIPTOR_TYPE_STRING) {
1340         /* string */
1341         slen  = bd->width / 8;
1342         csval = (char*)grib_context_malloc_clear(c, slen + 1);
1343         for (ii = 0; ii < slen; ii++)
1344             csval[ii] = missingChar;
1345         grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data encoding: \t %s = %s",
1346                          bd->shortName, csval);
1347         if (self->compressedData) {
1348             grib_sarray* stringValues = grib_sarray_new(c, 1, 1);
1349             grib_sarray_push(c, stringValues, csval);
1350             err = encode_string_array(c, buff, pos, bd, self, stringValues);
1351             grib_sarray_delete_content(c, stringValues);
1352             grib_sarray_delete(c, stringValues);
1353         }
1354         else {
1355             err = encode_string_value(c, buff, pos, bd, self, csval);
1356             grib_context_free(c, csval);
1357         }
1358     }
1359     else {
1360         /* numeric or codetable or flagtable */
1361         grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data encoding: \t %s = %g",
1362                          bd->shortName, cdval);
1363         if (bd->code == 31031)
1364             return encode_new_bitmap(c, buff, pos, i, self);
1365         if (self->compressedData) {
1366             grib_darray* doubleValues = grib_darray_new(c, 1, 1);
1367             grib_darray_push(c, doubleValues, cdval);
1368             err = encode_double_array(c, buff, pos, bd, self, doubleValues);
1369             grib_darray_delete(c, doubleValues);
1370         }
1371         else {
1372             err = encode_double_value(c, buff, pos, bd, self, cdval);
1373         }
1374     }
1375     return err;
1376 }
1377 
encode_new_replication(grib_context * c,grib_accessor_bufr_data_array * self,int subsetIndex,grib_buffer * buff,unsigned char * data,long * pos,int i,long elementIndex,grib_darray * dval,long * numberOfRepetitions)1378 static int encode_new_replication(grib_context* c, grib_accessor_bufr_data_array* self, int subsetIndex,
1379                                   grib_buffer* buff, unsigned char* data, long* pos, int i, long elementIndex, grib_darray* dval, long* numberOfRepetitions)
1380 {
1381     int err                       = 0;
1382     unsigned long repetitions     = 1;
1383     bufr_descriptor** descriptors = self->expanded->v;
1384     /* Assert( buff->data==data); */
1385 
1386     switch (descriptors[i]->code) {
1387         case 31000:
1388             if (self->nInputShortReplications >= 0) {
1389                 if (self->iInputShortReplications >= self->nInputShortReplications) {
1390                     grib_context_log(c, GRIB_LOG_ERROR, "Array inputShortDelayedDescriptorReplicationFactor: dimension mismatch (nInputShortReplications=%d)",
1391                                      self->nInputShortReplications);
1392                     return GRIB_ARRAY_TOO_SMALL;
1393                 }
1394                 repetitions = self->inputShortReplications[self->iInputShortReplications];
1395                 self->iInputShortReplications++;
1396             }
1397             break;
1398         case 31001:
1399             if (self->nInputReplications >= 0) {
1400                 if (self->iInputReplications >= self->nInputReplications) {
1401                     grib_context_log(c, GRIB_LOG_ERROR, "Array inputDelayedDescriptorReplicationFactor: dimension mismatch (nInputReplications=%d)",
1402                                      self->nInputReplications);
1403                     return GRIB_ARRAY_TOO_SMALL;
1404                 }
1405                 repetitions = self->inputReplications[self->iInputReplications];
1406                 self->iInputReplications++;
1407             }
1408             break;
1409         case 31002:
1410             if (self->nInputExtendedReplications >= 0) {
1411                 if (self->iInputExtendedReplications >= self->nInputExtendedReplications) {
1412                     grib_context_log(c, GRIB_LOG_ERROR, "Array inputExtendedDelayedDescriptorReplicationFactor: dimension mismatch (nInputExtendedReplications=%d)",
1413                                      self->nInputExtendedReplications);
1414                     return GRIB_ARRAY_TOO_SMALL;
1415                 }
1416                 repetitions = self->inputExtendedReplications[self->iInputExtendedReplications];
1417                 self->iInputExtendedReplications++;
1418             }
1419             break;
1420         default:
1421             grib_context_log(c, GRIB_LOG_ERROR, "Unsupported descriptor code %ld\n", descriptors[i]->code);
1422             return GRIB_INTERNAL_ERROR;
1423     }
1424 
1425     grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data encoding replication: \twidth=%ld pos=%ld ulength=%ld ulength_bits=%ld",
1426                      (long)descriptors[i]->width, (long)*pos, (long)buff->ulength, (long)buff->ulength_bits);
1427     grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + descriptors[i]->width);
1428     grib_encode_unsigned_longb(data, repetitions, pos, descriptors[i]->width);
1429 
1430     *numberOfRepetitions = repetitions;
1431 
1432     if (self->compressedData) {
1433         grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + 6);
1434         grib_encode_unsigned_longb(buff->data, 0, pos, 6);
1435     }
1436 
1437     return err;
1438 }
1439 
encode_element(grib_context * c,grib_accessor_bufr_data_array * self,int subsetIndex,grib_buffer * buff,unsigned char * data,long * pos,int i,bufr_descriptor * descriptor,long elementIndex,grib_darray * dval,grib_sarray * sval)1440 static int encode_element(grib_context* c, grib_accessor_bufr_data_array* self, int subsetIndex,
1441                           grib_buffer* buff, unsigned char* data, long* pos, int i, bufr_descriptor* descriptor,
1442                           long elementIndex, grib_darray* dval, grib_sarray* sval)
1443 {
1444     int idx, j;
1445     int err             = 0;
1446     bufr_descriptor* bd = descriptor == NULL ? self->expanded->v[i] : descriptor;
1447     /* Assert( buff->data == data); */
1448 
1449     grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data encoding: -%d- \tcode=%6.6ld width=%ld pos=%ld ulength=%ld ulength_bits=%ld",
1450                      i, bd->code, bd->width, (long)*pos, buff->ulength, buff->ulength_bits);
1451 
1452     if (self->change_ref_value_operand > 0 && self->change_ref_value_operand != 255) {
1453         /* Operator 203YYY: Change Reference Values: Encoding definition phase */
1454         err = encode_overridden_reference_value(c, self, buff, pos, bd);
1455         return err;
1456     }
1457 
1458     if (bd->type == BUFR_DESCRIPTOR_TYPE_STRING) {
1459         /* string */
1460         /* grib_context_log(c, GRIB_LOG_DEBUG,"BUFR data encoding: \t %s = %s",
1461                  bd->shortName,csval); */
1462         if (self->compressedData) {
1463             idx = ((int)self->numericValues->v[elementIndex]->v[0] / 1000 - 1) / self->numberOfSubsets;
1464             err = encode_string_array(c, buff, pos, bd, self, self->stringValues->v[idx]);
1465         }
1466         else {
1467             if (self->numericValues->v[subsetIndex] == NULL) {
1468                 grib_context_log(c, GRIB_LOG_ERROR, "Invalid subset index %d (number of subsets=%ld)", subsetIndex, self->numberOfSubsets);
1469                 return GRIB_INVALID_ARGUMENT;
1470             }
1471             idx = (int)self->numericValues->v[subsetIndex]->v[elementIndex] / 1000 - 1;
1472             if (idx < 0 || idx >= self->stringValues->n) {
1473                 grib_context_log(c, GRIB_LOG_ERROR, "encode_element '%s': Invalid index %d", bd->shortName, idx);
1474                 return GRIB_INVALID_ARGUMENT;
1475             }
1476             err = encode_string_value(c, buff, pos, bd, self, self->stringValues->v[idx]->v[0]);
1477         }
1478     }
1479     else {
1480         /* numeric or codetable or flagtable */
1481         if (self->compressedData) {
1482             err = encode_double_array(c, buff, pos, bd, self, self->numericValues->v[elementIndex]);
1483             if (err) {
1484                 grib_darray* varr = self->numericValues->v[elementIndex];
1485                 grib_context_log(c, GRIB_LOG_ERROR, "Encoding key '%s' ( code=%6.6ld width=%ld scale=%ld reference=%ld )",
1486                                  bd->shortName, bd->code, bd->width,
1487                                  bd->scale, bd->reference);
1488                 if (varr) {
1489                     for (j = 0; j < grib_darray_used_size(varr); j++)
1490                         grib_context_log(c, GRIB_LOG_ERROR, "value[%d]\t= %g", j, varr->v[j]);
1491                 } else {
1492                     grib_context_log(c, GRIB_LOG_ERROR, "Empty array: Check the order of keys being set!");
1493                 }
1494             }
1495         }
1496         else {
1497             if (self->numericValues->v[subsetIndex] == NULL) {
1498                 grib_context_log(c, GRIB_LOG_ERROR, "Invalid subset index %d (number of subsets=%ld)", subsetIndex, self->numberOfSubsets);
1499                 return GRIB_INVALID_ARGUMENT;
1500             }
1501             err = encode_double_value(c, buff, pos, bd, self, self->numericValues->v[subsetIndex]->v[elementIndex]);
1502             if (err) {
1503                 grib_context_log(c, GRIB_LOG_ERROR, "Cannot encode %s=%g (subset=%d)", /*subsetIndex starts from 0*/
1504                                  bd->shortName, self->numericValues->v[subsetIndex]->v[elementIndex], subsetIndex + 1);
1505             }
1506         }
1507     }
1508     return err;
1509 }
1510 
encode_replication(grib_context * c,grib_accessor_bufr_data_array * self,int subsetIndex,grib_buffer * buff,unsigned char * data,long * pos,int i,long elementIndex,grib_darray * dval,long * numberOfRepetitions)1511 static int encode_replication(grib_context* c, grib_accessor_bufr_data_array* self, int subsetIndex,
1512                               grib_buffer* buff, unsigned char* data, long* pos, int i, long elementIndex, grib_darray* dval, long* numberOfRepetitions)
1513 {
1514     /* Assert( buff->data == data); */
1515     if (self->compressedData) {
1516         DebugAssert(grib_darray_used_size(self->numericValues->v[elementIndex]) == 1);
1517         *numberOfRepetitions = self->numericValues->v[elementIndex]->v[0];
1518     }
1519     else {
1520         *numberOfRepetitions = self->numericValues->v[subsetIndex]->v[elementIndex];
1521     }
1522 
1523     return encode_element(c, self, subsetIndex, buff, data, pos, i, 0, elementIndex, dval, 0);
1524 }
1525 
build_bitmap(grib_accessor_bufr_data_array * self,unsigned char * data,long * pos,int iel,grib_iarray * elementsDescriptorsIndex,int iBitmapOperator)1526 static int build_bitmap(grib_accessor_bufr_data_array* self, unsigned char* data, long* pos, int iel, grib_iarray* elementsDescriptorsIndex, int iBitmapOperator)
1527 {
1528     int bitmapSize = 0, iDelayedReplication = 0;
1529     int i, localReference, width, bitmapEndElementsDescriptorsIndex;
1530     long ppos, n;
1531     grib_accessor* a              = (grib_accessor*)self;
1532     grib_context* c               = a->context;
1533     bufr_descriptor** descriptors = self->expanded->v;
1534     long* edi                     = elementsDescriptorsIndex->v;
1535     /* int iel=grib_iarray_used_size(elementsDescriptorsIndex)-1; */
1536     int err = 0;
1537 
1538     switch (descriptors[iBitmapOperator]->code) {
1539         case 222000:
1540         case 223000:
1541         case 236000:
1542             cancel_bitmap(self);
1543             if (iel < 0) {
1544                 return GRIB_ENCODING_ERROR;
1545             }
1546             while (descriptors[edi[iel]]->code >= 100000 || iel == 0) {
1547                 iel--;
1548                 if (iel < 0) {
1549                     return GRIB_ENCODING_ERROR;
1550                 }
1551             }
1552             bitmapEndElementsDescriptorsIndex = iel;
1553             /*looking for another bitmap and pointing before it.
1554           This behaviour is not documented in the Manual on codes it is copied from BUFRDC
1555           ECC-243
1556          */
1557             while (iel > 0) {
1558                 while (descriptors[edi[iel]]->code != 236000 && descriptors[edi[iel]]->code != 222000 && descriptors[edi[iel]]->code != 223000 && iel != 0)
1559                     iel--;
1560                 if (iel != 0) {
1561                     while (descriptors[edi[iel]]->code >= 100000 && iel != 0)
1562                         iel--;
1563                     bitmapEndElementsDescriptorsIndex = iel;
1564                 }
1565             }
1566 
1567             i = iBitmapOperator + 1;
1568             if (descriptors[i]->code == 101000) {
1569                 iDelayedReplication = iBitmapOperator + 2;
1570                 Assert(descriptors[iDelayedReplication]->code == 31001 ||
1571                        descriptors[iDelayedReplication]->code == 31002);
1572                 i = iDelayedReplication;
1573                 if (self->compressedData) {
1574                     ppos = *pos;
1575                     if (err)
1576                         return err;
1577                     localReference = grib_decode_unsigned_long(data, pos, descriptors[i]->width) + descriptors[i]->reference;
1578                     width          = grib_decode_unsigned_long(data, pos, 6);
1579                     *pos           = ppos;
1580                     if (width) {
1581                         /* delayed replication number is not constant. NOT IMPLEMENTED */
1582                         grib_context_log(c, GRIB_LOG_ERROR, "Delayed replication number is not constant");
1583                         return GRIB_NOT_IMPLEMENTED;
1584                     }
1585                     else {
1586                         bitmapSize = localReference * descriptors[i]->factor;
1587                     }
1588                 }
1589                 else {
1590                     ppos = *pos;
1591                     if (err)
1592                         return err;
1593                     bitmapSize = grib_decode_unsigned_long(data, pos, descriptors[i]->width) +
1594                                  descriptors[i]->reference * descriptors[i]->factor;
1595                     *pos = ppos;
1596                 }
1597             }
1598             else if (descriptors[i]->code == 31031) {
1599                 bitmapSize = 0;
1600                 while (descriptors[i]->code == 31031) {
1601                     bitmapSize++;
1602                     i++;
1603                 }
1604             }
1605             iel = bitmapEndElementsDescriptorsIndex;
1606             n   = bitmapSize - 1;
1607             while (n > 0 && iel >= 0) {
1608                 if (descriptors[edi[iel]]->code < 100000)
1609                     n--;
1610                 iel--;
1611             }
1612             self->bitmapStartElementsDescriptorsIndex = iel;
1613             restart_bitmap(self);
1614             break;
1615         default:
1616             grib_context_log(c, GRIB_LOG_ERROR, "build_bitmap: unsupported operator %ld\n",
1617                              descriptors[iBitmapOperator]->code);
1618             return GRIB_INTERNAL_ERROR;
1619     }
1620     return GRIB_SUCCESS;
1621 }
1622 
consume_bitmap(grib_accessor_bufr_data_array * self,int iBitmapOperator)1623 static int consume_bitmap(grib_accessor_bufr_data_array* self, int iBitmapOperator)
1624 {
1625     int bitmapSize = 0, iDelayedReplication;
1626     int i;
1627     bufr_descriptor** descriptors = self->expanded->v;
1628 
1629     i = iBitmapOperator + 1;
1630     if (descriptors[i]->code == 101000) {
1631         iDelayedReplication = iBitmapOperator + 2;
1632         switch (descriptors[iDelayedReplication]->code) {
1633             case 31001:
1634                 bitmapSize = self->inputReplications[self->iInputReplications];
1635                 break;
1636             case 31002:
1637                 bitmapSize = self->inputExtendedReplications[self->iInputExtendedReplications];
1638                 break;
1639             default:
1640                 Assert(0);
1641         }
1642     }
1643     else if (descriptors[i]->code == 31031) {
1644         bitmapSize = 0;
1645         while (descriptors[i]->code == 31031) {
1646             bitmapSize++;
1647             i++;
1648         }
1649     }
1650     self->bitmapCurrent += bitmapSize;
1651     return GRIB_SUCCESS;
1652 }
1653 
build_bitmap_new_data(grib_accessor_bufr_data_array * self,unsigned char * data,long * pos,int iel,grib_iarray * elementsDescriptorsIndex,int iBitmapOperator)1654 static int build_bitmap_new_data(grib_accessor_bufr_data_array* self, unsigned char* data, long* pos, int iel, grib_iarray* elementsDescriptorsIndex, int iBitmapOperator)
1655 {
1656     int bitmapSize = 0, iDelayedReplication = 0;
1657     int i, bitmapEndElementsDescriptorsIndex;
1658     long n;
1659     grib_accessor* a              = (grib_accessor*)self;
1660     grib_context* c               = a->context;
1661     bufr_descriptor** descriptors = self->expanded->v;
1662     long* edi                     = elementsDescriptorsIndex->v;
1663 
1664     switch (descriptors[iBitmapOperator]->code) {
1665         case 222000:
1666         case 223000:
1667         case 236000:
1668             if (iel < 0) {
1669                 return GRIB_ENCODING_ERROR;
1670             }
1671             while (descriptors[edi[iel]]->code >= 100000) {
1672                 iel--;
1673                 if (iel < 0) {
1674                     return GRIB_ENCODING_ERROR;
1675                 }
1676             }
1677             bitmapEndElementsDescriptorsIndex = iel;
1678             /*looking for another bitmap and pointing before it.
1679           This behaviour is not documented in the Manual on codes it is copied from BUFRDC
1680           ECC-243
1681          */
1682             while (iel > 0) {
1683                 while (descriptors[edi[iel]]->code != 236000 && descriptors[edi[iel]]->code != 222000 && descriptors[edi[iel]]->code != 223000 && iel != 0)
1684                     iel--;
1685                 if (iel != 0) {
1686                     while (descriptors[edi[iel]]->code >= 100000 && iel != 0)
1687                         iel--;
1688                     bitmapEndElementsDescriptorsIndex = iel;
1689                 }
1690             }
1691 
1692             i = iBitmapOperator + 1;
1693             if (descriptors[i]->code == 101000) {
1694                 iDelayedReplication = iBitmapOperator + 2;
1695                 switch (descriptors[iDelayedReplication]->code) {
1696                     case 31001:
1697                         if (!self->inputReplications) {
1698                             grib_context_log(c, GRIB_LOG_ERROR, "build_bitmap_new_data: No inputReplications");
1699                             return GRIB_ENCODING_ERROR;
1700                         }
1701                         bitmapSize = self->inputReplications[self->iInputReplications];
1702                         break;
1703                     case 31002:
1704                         if (!self->inputExtendedReplications) {
1705                             grib_context_log(c, GRIB_LOG_ERROR, "build_bitmap_new_data: No inputExtendedReplications");
1706                             return GRIB_ENCODING_ERROR;
1707                         }
1708                         bitmapSize = self->inputExtendedReplications[self->iInputExtendedReplications];
1709                         break;
1710                     default:
1711                         Assert(0);
1712                 }
1713             }
1714             else if (descriptors[i]->code == 31031) {
1715                 bitmapSize = 0;
1716                 while (descriptors[i]->code == 31031) {
1717                     bitmapSize++;
1718                     i++;
1719                 }
1720             }
1721             iel = bitmapEndElementsDescriptorsIndex;
1722             n   = bitmapSize - 1;
1723             while (n > 0 && iel >= 0) {
1724                 if (descriptors[edi[iel]]->code < 100000)
1725                     n--;
1726                 iel--;
1727             }
1728             self->bitmapStartElementsDescriptorsIndex   = iel;
1729             self->bitmapCurrentElementsDescriptorsIndex = iel - 1;
1730             break;
1731         default:
1732             grib_context_log(c, GRIB_LOG_ERROR, "build_bitmap_new_data: unsupported operator %ld\n",
1733                              descriptors[iBitmapOperator]->code);
1734             return GRIB_INTERNAL_ERROR;
1735     }
1736     return GRIB_SUCCESS;
1737 }
1738 
get_next_bitmap_descriptor_index_new_bitmap(grib_accessor_bufr_data_array * self,grib_iarray * elementsDescriptorsIndex,int compressedData)1739 static int get_next_bitmap_descriptor_index_new_bitmap(grib_accessor_bufr_data_array* self, grib_iarray* elementsDescriptorsIndex, int compressedData)
1740 {
1741     int i;
1742     bufr_descriptor** descriptors = self->expanded->v;
1743 
1744     self->bitmapCurrent++;
1745     self->bitmapCurrentElementsDescriptorsIndex++;
1746     i = self->bitmapCurrent;
1747 
1748     if (self->compressedData) {
1749         DebugAssert(i < self->nInputBitmap);
1750         if (i >= self->nInputBitmap)
1751             return GRIB_WRONG_BITMAP_SIZE;
1752         while (self->inputBitmap[i] == 1) {
1753             self->bitmapCurrent++;
1754             self->bitmapCurrentElementsDescriptorsIndex++;
1755             while (descriptors[elementsDescriptorsIndex->v[self->bitmapCurrentElementsDescriptorsIndex]]->code > 100000)
1756                 self->bitmapCurrentElementsDescriptorsIndex++;
1757             i++;
1758         }
1759     }
1760     else {
1761         if (i >= self->nInputBitmap)
1762             return GRIB_WRONG_BITMAP_SIZE;
1763         while (self->inputBitmap[i] == 1) {
1764             self->bitmapCurrent++;
1765             self->bitmapCurrentElementsDescriptorsIndex++;
1766             while (descriptors[elementsDescriptorsIndex->v[self->bitmapCurrentElementsDescriptorsIndex]]->code > 100000)
1767                 self->bitmapCurrentElementsDescriptorsIndex++;
1768             i++;
1769         }
1770     }
1771     while (descriptors[elementsDescriptorsIndex->v[self->bitmapCurrentElementsDescriptorsIndex]]->code > 100000)
1772         self->bitmapCurrentElementsDescriptorsIndex++;
1773     return elementsDescriptorsIndex->v[self->bitmapCurrentElementsDescriptorsIndex];
1774 }
1775 
get_next_bitmap_descriptor_index(grib_accessor_bufr_data_array * self,grib_iarray * elementsDescriptorsIndex,grib_darray * numericValues)1776 static int get_next_bitmap_descriptor_index(grib_accessor_bufr_data_array* self, grib_iarray* elementsDescriptorsIndex, grib_darray* numericValues)
1777 {
1778     int i;
1779     bufr_descriptor** descriptors = self->expanded->v;
1780 
1781     if (self->compressedData) {
1782         if (self->numericValues->n == 0)
1783             return get_next_bitmap_descriptor_index_new_bitmap(self, elementsDescriptorsIndex, 1);
1784 
1785         self->bitmapCurrent++;
1786         self->bitmapCurrentElementsDescriptorsIndex++;
1787         i = self->bitmapCurrent + self->bitmapStart;
1788         DebugAssert(i < self->numericValues->n);
1789         while (self->numericValues->v[i]->v[0] == 1) {
1790             self->bitmapCurrent++;
1791             self->bitmapCurrentElementsDescriptorsIndex++;
1792             while (descriptors[elementsDescriptorsIndex->v[self->bitmapCurrentElementsDescriptorsIndex]]->code > 100000)
1793                 self->bitmapCurrentElementsDescriptorsIndex++;
1794             i++;
1795         }
1796     }
1797     else {
1798         if (numericValues->n == 0)
1799             return get_next_bitmap_descriptor_index_new_bitmap(self, elementsDescriptorsIndex, 0);
1800 
1801         self->bitmapCurrent++;
1802         self->bitmapCurrentElementsDescriptorsIndex++;
1803         i = self->bitmapCurrent + self->bitmapStart;
1804         DebugAssert(i < numericValues->n);
1805         while (numericValues->v[i] == 1) {
1806             self->bitmapCurrent++;
1807             self->bitmapCurrentElementsDescriptorsIndex++;
1808             while (descriptors[elementsDescriptorsIndex->v[self->bitmapCurrentElementsDescriptorsIndex]]->code > 100000)
1809                 self->bitmapCurrentElementsDescriptorsIndex++;
1810             i++;
1811         }
1812     }
1813     while (descriptors[elementsDescriptorsIndex->v[self->bitmapCurrentElementsDescriptorsIndex]]->code > 100000)
1814         self->bitmapCurrentElementsDescriptorsIndex++;
1815     return elementsDescriptorsIndex->v[self->bitmapCurrentElementsDescriptorsIndex];
1816 }
1817 
push_zero_element(grib_accessor_bufr_data_array * self,grib_darray * dval)1818 static void push_zero_element(grib_accessor_bufr_data_array* self, grib_darray* dval)
1819 {
1820     grib_darray* d   = 0;
1821     grib_accessor* a = (grib_accessor*)self;
1822     grib_context* c  = a->context;
1823     if (self->compressedData) {
1824         d = grib_darray_new(c, 1, 100);
1825         grib_darray_push(c, d, 0);
1826         grib_vdarray_push(c, self->numericValues, d);
1827     }
1828     else {
1829         grib_darray_push(c, dval, 0);
1830     }
1831 }
1832 
create_attribute_variable(char * name,grib_section * section,int type,char * sval,double dval,long lval,unsigned long flags)1833 static grib_accessor* create_attribute_variable(char* name, grib_section* section, int type, char* sval, double dval, long lval, unsigned long flags)
1834 {
1835     grib_accessor* a    = NULL;
1836     grib_action creator = {0,};
1837     size_t len;
1838     creator.op         = "variable";
1839     creator.name_space = "";
1840     creator.flags      = GRIB_ACCESSOR_FLAG_READ_ONLY | flags;
1841     creator.set        = 0;
1842 
1843     creator.name = name;
1844     a            = grib_accessor_factory(section, &creator, 0, NULL);
1845     a->parent    = NULL;
1846     a->h         = section->h;
1847     accessor_variable_set_type(a, type);
1848     len = 1;
1849     switch (type) {
1850         case GRIB_TYPE_LONG:
1851             grib_pack_long(a, &lval, &len);
1852             break;
1853         case GRIB_TYPE_DOUBLE:
1854             grib_pack_double(a, &dval, &len);
1855             break;
1856         case GRIB_TYPE_STRING:
1857             if (!sval)
1858                 return NULL;
1859             len = strlen(sval);
1860             grib_pack_string(a, sval, &len);
1861             break;
1862     }
1863 
1864     return a;
1865 }
1866 
set_creator_name(grib_action * creator,int code)1867 static void set_creator_name(grib_action* creator, int code)
1868 {
1869     switch (code) {
1870         case 222000:
1871             creator->name = (char*)"qualityInformationFollows";
1872             break;
1873         case 223000:
1874             creator->name = (char*)"substitutedValuesOperator";
1875             break;
1876         case 223255:
1877             creator->name = (char*)"substitutedValue";
1878             break;
1879         case 224000:
1880             creator->name = (char*)"firstOrderStatiticalValuesFollow";
1881             break;
1882         case 224255:
1883             creator->name = (char*)"firstOrderStatisticalValue";
1884             break;
1885         case 225000:
1886             creator->name = (char*)"differenceStatisticalValuesFollow";
1887             break;
1888         case 225255:
1889             creator->name = (char*)"differenceStatisticalValue";
1890             break;
1891         case 232000:
1892             creator->name = (char*)"replacedRetainedValuesFollow";
1893             break;
1894         case 232255:
1895             creator->name = (char*)"replacedRetainedValue";
1896             break;
1897         case 235000:
1898             creator->name = (char*)"cancelBackwardDataReference";
1899             break;
1900         case 236000:
1901             creator->name = (char*)"defineDataPresentBitmap";
1902             break;
1903         case 237000:
1904             creator->name = (char*)"useDefinedDataPresentBitmap";
1905             break;
1906         case 237255:
1907             creator->name = (char*)"cancelUseDefinedDataPresentBitmap";
1908             break;
1909         case 241000:
1910             creator->name = (char*)"defineEvent";
1911             break;
1912         case 241255:
1913             creator->name = (char*)"cancelDefineEvent";
1914             break;
1915         case 242000:
1916             creator->name = (char*)"defineConditioningEvent";
1917             break;
1918         case 242255:
1919             creator->name = (char*)"canceDefineConditioningEvent";
1920             break;
1921         case 243000:
1922             creator->name = (char*)"categoricalForecastValuesFollow";
1923             break;
1924         case 243255:
1925             creator->name = (char*)"cancelCategoricalForecastValuesFollow";
1926             break;
1927         case 999999:
1928             creator->name = (char*)"associatedField";
1929             break;
1930         default:
1931             if (code > 204999 && code < 206000)
1932                 creator->name = (char*)"text";
1933             else
1934                 creator->name = (char*)"operator";
1935             break;
1936     }
1937 }
1938 
1939 /* See ECC-741 */
adding_extra_key_attributes(grib_handle * h)1940 static int adding_extra_key_attributes(grib_handle* h)
1941 {
1942     long skip = 0; /* default is to add */
1943     int err   = 0;
1944     err       = grib_get_long(h, "skipExtraKeyAttributes", &skip);
1945     if (err)
1946         return 1;
1947     return (!skip);
1948 }
1949 
create_accessor_from_descriptor(const grib_accessor * a,grib_accessor * attribute,grib_section * section,long ide,long subset,int add_dump_flag,int count,int add_extra_attributes)1950 static grib_accessor* create_accessor_from_descriptor(const grib_accessor* a, grib_accessor* attribute, grib_section* section,
1951                                                       long ide, long subset, int add_dump_flag, int count, int add_extra_attributes)
1952 {
1953     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
1954     char code[10]               = {0,};
1955     char* temp_str              = NULL;
1956     int idx                     = 0;
1957     unsigned long flags         = GRIB_ACCESSOR_FLAG_READ_ONLY;
1958     grib_action operatorCreator = {0,};
1959     grib_accessor* elementAccessor = NULL;
1960     grib_action creator            = {0,};
1961     creator.op         = "bufr_data_element";
1962     creator.name_space = "";
1963     creator.set        = 0;
1964 
1965     operatorCreator.op         = "variable";
1966     operatorCreator.name_space = "";
1967     operatorCreator.flags      = GRIB_ACCESSOR_FLAG_READ_ONLY;
1968     operatorCreator.set        = 0;
1969     operatorCreator.name       = "operator";
1970 
1971     if (attribute) {
1972         DebugAssert(attribute->parent == NULL);
1973     }
1974 
1975     if (add_dump_flag) {
1976         creator.flags = GRIB_ACCESSOR_FLAG_DUMP;
1977         operatorCreator.flags |= GRIB_ACCESSOR_FLAG_DUMP;
1978     }
1979 
1980     idx = self->compressedData ? self->elementsDescriptorsIndex->v[0]->v[ide] : self->elementsDescriptorsIndex->v[subset]->v[ide];
1981 
1982     switch (self->expanded->v[idx]->F) {
1983         case 0:
1984         case 1:
1985             creator.name = grib_context_strdup(a->context, self->expanded->v[idx]->shortName);
1986             /* ECC-325: store alloc'd string (due to strdup) for clean up later */
1987             grib_sarray_push(a->context, self->tempStrings, creator.name);
1988             elementAccessor = grib_accessor_factory(section, &creator, 0, NULL);
1989             if (self->canBeMissing[idx])
1990                 elementAccessor->flags |= GRIB_ACCESSOR_FLAG_CAN_BE_MISSING;
1991             if (self->expanded->v[idx]->code == 31000 || self->expanded->v[idx]->code == 31001 || self->expanded->v[idx]->code == 31002 || self->expanded->v[idx]->code == 31031)
1992                 elementAccessor->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY;
1993             accessor_bufr_data_element_set_index(elementAccessor, ide);
1994             accessor_bufr_data_element_set_descriptors(elementAccessor, self->expanded);
1995             accessor_bufr_data_element_set_elementsDescriptorsIndex(elementAccessor, self->elementsDescriptorsIndex);
1996             accessor_bufr_data_element_set_numericValues(elementAccessor, self->numericValues);
1997             accessor_bufr_data_element_set_stringValues(elementAccessor, self->stringValues);
1998             accessor_bufr_data_element_set_compressedData(elementAccessor, self->compressedData);
1999             accessor_bufr_data_element_set_type(elementAccessor, self->expanded->v[idx]->type);
2000             accessor_bufr_data_element_set_numberOfSubsets(elementAccessor, self->numberOfSubsets);
2001             accessor_bufr_data_element_set_subsetNumber(elementAccessor, subset);
2002 
2003             self->expanded->v[idx]->a = elementAccessor;
2004 
2005             if (attribute) {
2006                 /* attribute->parent=elementAccessor->parent; */
2007                 /*
2008             for (i=0;i<MAX_ACCESSOR_ATTRIBUTES;i++) {
2009                 if (attribute->attributes[i]) attribute->attributes[i]->parent=elementAccessor->parent;
2010             }
2011             */
2012                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2013             }
2014 
2015             attribute = create_attribute_variable("index", section, GRIB_TYPE_LONG, 0, 0, count, flags);
2016             if (!attribute)
2017                 return NULL;
2018             grib_accessor_add_attribute(elementAccessor, attribute, 0);
2019 
2020             sprintf(code, "%06ld", self->expanded->v[idx]->code);
2021             temp_str  = grib_context_strdup(a->context, code);
2022             attribute = create_attribute_variable("code", section, GRIB_TYPE_STRING, temp_str, 0, 0, flags);
2023             if (!attribute)
2024                 return NULL;
2025             grib_sarray_push(a->context, self->tempStrings, temp_str); /* ECC-325: store alloc'd string (due to strdup) for clean up later */
2026             grib_accessor_add_attribute(elementAccessor, attribute, 0);
2027 
2028             if (add_extra_attributes) {
2029                 attribute = create_attribute_variable("units", section, GRIB_TYPE_STRING, self->expanded->v[idx]->units, 0, 0, GRIB_ACCESSOR_FLAG_DUMP | flags);
2030                 if (!attribute)
2031                     return NULL;
2032                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2033 
2034                 attribute = create_attribute_variable("scale", section, GRIB_TYPE_LONG, 0, 0, self->expanded->v[idx]->scale, flags);
2035                 if (!attribute)
2036                     return NULL;
2037                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2038 
2039                 attribute = create_attribute_variable("reference", section, GRIB_TYPE_DOUBLE, 0, self->expanded->v[idx]->reference, 0, flags);
2040                 if (!attribute)
2041                     return NULL;
2042                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2043 
2044                 attribute = create_attribute_variable("width", section, GRIB_TYPE_LONG, 0, 0, self->expanded->v[idx]->width, flags);
2045                 if (!attribute)
2046                     return NULL;
2047                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2048             }
2049             break;
2050         case 2:
2051             set_creator_name(&creator, self->expanded->v[idx]->code);
2052             if (bufr_descriptor_is_marker(self->expanded->v[idx])) {
2053                 elementAccessor = grib_accessor_factory(section, &creator, 0, NULL);
2054                 if (self->canBeMissing[idx])
2055                     elementAccessor->flags |= GRIB_ACCESSOR_FLAG_CAN_BE_MISSING;
2056                 accessor_bufr_data_element_set_index(elementAccessor, ide);
2057                 accessor_bufr_data_element_set_descriptors(elementAccessor, self->expanded);
2058                 accessor_bufr_data_element_set_elementsDescriptorsIndex(elementAccessor, self->elementsDescriptorsIndex);
2059                 accessor_bufr_data_element_set_numericValues(elementAccessor, self->numericValues);
2060                 accessor_bufr_data_element_set_stringValues(elementAccessor, self->stringValues);
2061                 accessor_bufr_data_element_set_compressedData(elementAccessor, self->compressedData);
2062                 accessor_bufr_data_element_set_type(elementAccessor, self->expanded->v[idx]->type);
2063                 accessor_bufr_data_element_set_numberOfSubsets(elementAccessor, self->numberOfSubsets);
2064                 accessor_bufr_data_element_set_subsetNumber(elementAccessor, subset);
2065 
2066                 attribute = create_attribute_variable("index", section, GRIB_TYPE_LONG, 0, 0, count, flags);
2067                 if (!attribute)
2068                     return NULL;
2069                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2070             }
2071             else {
2072                 elementAccessor = grib_accessor_factory(section, &operatorCreator, 0, NULL);
2073                 accessor_variable_set_type(elementAccessor, GRIB_TYPE_LONG);
2074 
2075                 attribute = create_attribute_variable("index", section, GRIB_TYPE_LONG, 0, 0, count, flags);
2076                 if (!attribute)
2077                     return NULL;
2078                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2079 
2080                 sprintf(code, "%06ld", self->expanded->v[idx]->code);
2081                 attribute = create_attribute_variable("code", section, GRIB_TYPE_STRING, code, 0, 0, flags);
2082                 if (!attribute)
2083                     return NULL;
2084                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2085             }
2086             self->expanded->v[idx]->a = elementAccessor;
2087             break;
2088         case 9:
2089             set_creator_name(&creator, self->expanded->v[idx]->code);
2090             elementAccessor = grib_accessor_factory(section, &creator, 0, NULL);
2091             accessor_bufr_data_element_set_index(elementAccessor, ide);
2092             accessor_bufr_data_element_set_descriptors(elementAccessor, self->expanded);
2093             accessor_bufr_data_element_set_elementsDescriptorsIndex(elementAccessor, self->elementsDescriptorsIndex);
2094             accessor_bufr_data_element_set_numericValues(elementAccessor, self->numericValues);
2095             accessor_bufr_data_element_set_stringValues(elementAccessor, self->stringValues);
2096             accessor_bufr_data_element_set_compressedData(elementAccessor, self->compressedData);
2097             accessor_bufr_data_element_set_type(elementAccessor, self->expanded->v[idx]->type);
2098             accessor_bufr_data_element_set_numberOfSubsets(elementAccessor, self->numberOfSubsets);
2099             accessor_bufr_data_element_set_subsetNumber(elementAccessor, subset);
2100 
2101             attribute = create_attribute_variable("index", section, GRIB_TYPE_LONG, 0, 0, count, flags);
2102             if (!attribute)
2103                 return NULL;
2104             grib_accessor_add_attribute(elementAccessor, attribute, 0);
2105 
2106             sprintf(code, "%06ld", self->expanded->v[idx]->code);
2107             attribute = create_attribute_variable("code", section, GRIB_TYPE_STRING, code, 0, 0, flags);
2108             if (!attribute)
2109                 return NULL;
2110             grib_accessor_add_attribute(elementAccessor, attribute, 0);
2111 
2112             if (add_extra_attributes) {
2113                 attribute = create_attribute_variable("units", section, GRIB_TYPE_STRING, self->expanded->v[idx]->units, 0, 0, GRIB_ACCESSOR_FLAG_DUMP);
2114                 if (!attribute)
2115                     return NULL;
2116                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2117 
2118                 attribute = create_attribute_variable("scale", section, GRIB_TYPE_LONG, 0, 0, self->expanded->v[idx]->scale, flags);
2119                 if (!attribute)
2120                     return NULL;
2121                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2122 
2123                 attribute = create_attribute_variable("reference", section, GRIB_TYPE_DOUBLE, 0, self->expanded->v[idx]->reference, 0, flags);
2124                 if (!attribute)
2125                     return NULL;
2126                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2127 
2128                 attribute = create_attribute_variable("width", section, GRIB_TYPE_LONG, 0, 0, self->expanded->v[idx]->width, flags);
2129                 if (!attribute)
2130                     return NULL;
2131                 grib_accessor_add_attribute(elementAccessor, attribute, 0);
2132             }
2133             break;
2134     }
2135 
2136     return elementAccessor;
2137 }
2138 
2139 /* Section 3.1.2.2 of WMO BUFR guide: classes 03 and 09 at present reserved for future use */
2140 #define IS_COORDINATE_DESCRIPTOR(a) (a == 8 || a == 1 || a == 2 || a == 4 || a == 5 || a == 6 || a == 7)
2141 #define NUMBER_OF_QUALIFIERS_PER_CATEGORY 256
2142 #define NUMBER_OF_QUALIFIERS_CATEGORIES 7
2143 #define MAX_NUMBER_OF_BITMAPS 5
2144 
2145 static const int number_of_qualifiers = NUMBER_OF_QUALIFIERS_PER_CATEGORY * NUMBER_OF_QUALIFIERS_CATEGORIES;
2146 static const int significanceQualifierIndexArray[] = { -1, 0, 1, -1, 2, 3, 4, 5, 6 };
2147 
reset_deeper_qualifiers(grib_accessor * significanceQualifierGroup[],const int * const significanceQualifierDepth,int numElements,int depth)2148 static GRIB_INLINE void reset_deeper_qualifiers(
2149     grib_accessor* significanceQualifierGroup[],
2150     const int* const significanceQualifierDepth,
2151     int numElements, int depth)
2152 {
2153     int i;
2154     for (i = 0; i < numElements; i++) {
2155         if (significanceQualifierDepth[i] > depth) {
2156             significanceQualifierGroup[i] = NULL;
2157         }
2158     }
2159 }
2160 
2161 typedef struct bitmap_s
2162 {
2163     grib_accessors_list* cursor;
2164     grib_accessors_list* referredElement;
2165     grib_accessors_list* referredElementStart;
2166 } bitmap_s;
2167 
get_element_from_bitmap(const grib_accessor * a,bitmap_s * bitmap)2168 static grib_accessor* get_element_from_bitmap(const grib_accessor* a, bitmap_s* bitmap)
2169 {
2170     int ret;
2171     long bitmapVal = 1;
2172     size_t len;
2173 
2174     while (bitmapVal) {
2175         len = 1;
2176         if (bitmap->cursor && bitmap->cursor->accessor) {
2177             ret = grib_unpack_long(bitmap->cursor->accessor, &bitmapVal, &len);
2178         }
2179         else {
2180             return NULL;
2181         }
2182         if (ret != 0)
2183             return NULL;
2184         bitmap->cursor = bitmap->cursor->next;
2185         if (bitmap->referredElement)
2186             bitmap->referredElement = bitmap->referredElement->next;
2187     }
2188 
2189     return bitmap->referredElement ? bitmap->referredElement->prev->accessor : NULL;
2190 }
2191 
2192 /*
2193 static GRIB_INLINE void reset_qualifiers(grib_accessor* significanceQualifierGroup[])
2194 {
2195     int i;
2196     for (i=0;i<number_of_qualifiers;i++)
2197         significanceQualifierGroup[i]=0;
2198 }
2199  */
2200 
grib_convert_to_attribute(grib_accessor * a)2201 static void grib_convert_to_attribute(grib_accessor* a)
2202 {
2203     if (a->h == NULL && a->parent != NULL) {
2204         a->h      = grib_handle_of_accessor(a);
2205         a->parent = NULL;
2206     }
2207 }
2208 
set_subset_list(grib_context * c,grib_accessor_bufr_data_array * self,long onlySubset,long startSubset,long endSubset,long * subsetList,size_t subsetListSize)2209 static grib_iarray* set_subset_list(grib_context* c, grib_accessor_bufr_data_array* self, long onlySubset, long startSubset, long endSubset, long* subsetList, size_t subsetListSize)
2210 {
2211     grib_iarray* list = grib_iarray_new(c, self->numberOfSubsets, 10);
2212     long s;
2213 
2214     if (startSubset > 0) {
2215         s = startSubset;
2216         while (s <= endSubset) {
2217             grib_iarray_push(list, s - 1);
2218             s++;
2219         }
2220     }
2221 
2222     if (onlySubset > 0)
2223         grib_iarray_push(list, onlySubset - 1);
2224 
2225     if (subsetList && subsetList[0] > 0) {
2226         for (s = 0; s < subsetListSize; s++)
2227             grib_iarray_push(list, subsetList[s] - 1);
2228     }
2229 
2230     if (grib_iarray_used_size(list) == 0) {
2231         for (s = 0; s < self->numberOfSubsets; s++)
2232             grib_iarray_push(list, s);
2233     }
2234 
2235     return list;
2236 }
2237 
bitmap_ref_skip(grib_accessors_list * al,int * err)2238 static int bitmap_ref_skip(grib_accessors_list* al, int* err)
2239 {
2240     grib_accessor* acode = NULL;
2241     long code[1];
2242     size_t l = 1;
2243 
2244     if (!al || !al->accessor)
2245         return 0;
2246 
2247     acode = grib_accessor_get_attribute(al->accessor, "code");
2248 
2249     if (acode)
2250         *err = grib_unpack_long(acode, code, &l);
2251     else
2252         return 1;
2253 
2254     switch (code[0]) {
2255         case 222000:
2256         case 223000:
2257         case 224000:
2258         case 225000:
2259         case 232000:
2260         case 236000:
2261         case 237000:
2262         case 243000:
2263         case 31000:
2264         case 31001:
2265         case 31002:
2266             return 1;
2267     }
2268     return 0;
2269 }
2270 
2271 /* Return 1 if the descriptor is an operator marking the start of a bitmap */
is_bitmap_start_descriptor(grib_accessors_list * al,int * err)2272 static int is_bitmap_start_descriptor(grib_accessors_list* al, int* err)
2273 {
2274     grib_accessor* acode = NULL;
2275     long code[1];
2276     size_t l = 1;
2277     if (!al || !al->accessor)
2278         return 0;
2279 
2280     acode = grib_accessor_get_attribute(al->accessor, "code");
2281     if (acode)
2282         *err = grib_unpack_long(acode, code, &l);
2283     else
2284         return 1;
2285 
2286     switch (code[0]) {
2287         case 222000:
2288         case 223000:
2289         case 224000:
2290         case 225000:
2291         case 232000:
2292             /*case 236000:*/
2293         case 237000:
2294             /*case 243000:*/
2295             {
2296 #if 0
2297         long index[1];
2298         grib_accessor* anindex=grib_accessor_get_attribute(al->accessor,"index");
2299         grib_unpack_long(anindex,index,&l);
2300 #endif
2301                 return 1;
2302             }
2303     }
2304     return 0;
2305 }
2306 
print_bitmap_debug_info(grib_context * c,bitmap_s * bitmap,grib_accessors_list * bitmapStart,int bitmapSize)2307 static void print_bitmap_debug_info(grib_context* c, bitmap_s* bitmap, grib_accessors_list* bitmapStart, int bitmapSize)
2308 {
2309     int i = 0, ret = 0;
2310     fprintf(stderr, "ECCODES DEBUG: bitmap_init: bitmapSize=%d\n", bitmapSize);
2311     bitmap->cursor          = bitmapStart->next;
2312     bitmap->referredElement = bitmapStart;
2313 
2314     while (bitmap_ref_skip(bitmap->referredElement, &ret)) {
2315         int is_bmp = 0;
2316         if (is_bitmap_start_descriptor(bitmap->referredElement, &ret)) {
2317             is_bmp = 1;
2318         }
2319         bitmap->referredElement = bitmap->referredElement->prev;
2320         if (is_bmp) {
2321             break;
2322         }
2323     }
2324 
2325     for (i = 1; i < bitmapSize; i++) {
2326         if (bitmap->referredElement) {
2327             fprintf(stderr, "ECCODES DEBUG:\t bitmap_init: i=%d |%s|\n", i, bitmap->referredElement->accessor->name);
2328             bitmap->referredElement = bitmap->referredElement->prev;
2329         }
2330     }
2331 }
2332 
bitmap_init(grib_context * c,bitmap_s * bitmap,grib_accessors_list * bitmapStart,int bitmapSize,grib_accessors_list * lastAccessorInList)2333 static int bitmap_init(grib_context* c, bitmap_s* bitmap,
2334                        grib_accessors_list* bitmapStart, int bitmapSize, grib_accessors_list* lastAccessorInList)
2335 {
2336     int ret        = 0, i;
2337     bitmap->cursor = bitmapStart->next;
2338     if (bitmap->referredElementStart != NULL) {
2339         bitmap->referredElement = bitmap->referredElementStart;
2340         return ret;
2341     }
2342     bitmap->referredElement = bitmapStart;
2343     /*while (bitmap_ref_skip(bitmap->referredElement,&ret)) bitmap->referredElement=bitmap->referredElement->prev;*/
2344     /* See ECC-869
2345      * We have to INCLUDE the replication factors that come after the bitmap operators
2346      */
2347     while (bitmap_ref_skip(bitmap->referredElement, &ret)) {
2348         int is_bmp = 0;
2349         if (is_bitmap_start_descriptor(bitmap->referredElement, &ret)) {
2350             is_bmp = 1;
2351         }
2352         bitmap->referredElement = bitmap->referredElement->prev;
2353         if (is_bmp) {
2354             break;
2355         }
2356     }
2357     /*printf("bitmap_init: bitmapSize=%d\n", bitmapSize);*/
2358     for (i = 1; i < bitmapSize; i++) {
2359         if (bitmap->referredElement == NULL) {
2360             grib_context_log(c, GRIB_LOG_ERROR, "bitmap_init: bitmap->referredElement==NULL");
2361             if (c->debug)
2362                 print_bitmap_debug_info(c, bitmap, bitmapStart, bitmapSize);
2363             return GRIB_INTERNAL_ERROR;
2364         }
2365         /*printf("  bitmap_init: i=%d  |%s|\n", i,bitmap->referredElement->accessor->name);*/
2366         bitmap->referredElement = bitmap->referredElement->prev;
2367     }
2368     bitmap->referredElementStart = bitmap->referredElement;
2369     return ret;
2370 }
2371 
accessor_or_attribute_with_same_name(grib_accessor * a,const char * name)2372 static grib_accessor* accessor_or_attribute_with_same_name(grib_accessor* a, const char* name)
2373 {
2374     if (grib_accessor_has_attributes(a) == 0) {
2375         return a;
2376     }
2377     else {
2378         grib_accessor* ok = a;
2379         grib_accessor* next;
2380         while ((next = grib_accessor_get_attribute(ok, name)) != NULL) {
2381             ok = next;
2382         }
2383         return ok;
2384     }
2385 }
2386 #if 0
2387 static int get_key_rank(grib_trie* accessorsRank,grib_accessor* a)
2388 {
2389     int* r=(int*)grib_trie_get(accessorsRank,a->name);
2390 
2391     if (r) (*r)++;
2392     else {
2393         r=(int*)grib_context_malloc(a->context,sizeof(int));
2394         *r=1;
2395         grib_trie_insert(accessorsRank,a->name,(void*)r);
2396     }
2397     return *r;
2398 }
2399 #endif
grib_data_accessors_trie_push(grib_trie_with_rank * accessorsTrie,grib_accessor * a)2400 static int grib_data_accessors_trie_push(grib_trie_with_rank* accessorsTrie, grib_accessor* a)
2401 {
2402     return grib_trie_with_rank_insert(accessorsTrie, a->name, a);
2403 }
2404 
create_keys(const grib_accessor * a,long onlySubset,long startSubset,long endSubset)2405 static int create_keys(const grib_accessor* a, long onlySubset, long startSubset, long endSubset)
2406 {
2407     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
2408     int err                             = 0;
2409     int rank;
2410     grib_accessor* elementAccessor                     = 0;
2411     grib_accessor* associatedFieldAccessor             = 0;
2412     grib_accessor* associatedFieldSignificanceAccessor = 0;
2413     long iss, end, elementsInSubset, ide;
2414     grib_section* section = NULL;
2415     /*grib_section* rootSection=NULL;*/
2416     bufr_descriptor* descriptor;
2417     /*grib_section* sectionUp=0;*/
2418     grib_section* groupSection = 0;
2419     long groupNumber           = 0;
2420     /*long indexOfGroupNumber=0;*/
2421     int depth;
2422     int max_depth = -1; /* highest value of depth */
2423     int idx;
2424     grib_context* c    = a->context;
2425     int qualityPresent = 0;
2426     bitmap_s bitmap    = {0,};
2427     int extraElement         = 0;
2428     int add_extra_attributes = 1;
2429 
2430     grib_accessor* gaGroup   = 0;
2431     grib_action creatorGroup = {0,};
2432     grib_accessor* significanceQualifierGroup[NUMBER_OF_QUALIFIERS_PER_CATEGORY * NUMBER_OF_QUALIFIERS_CATEGORIES] = {0,};
2433     int significanceQualifierDepth[NUMBER_OF_QUALIFIERS_PER_CATEGORY * NUMBER_OF_QUALIFIERS_CATEGORIES] = {0,};
2434 
2435     grib_accessor* bitmapGroup[MAX_NUMBER_OF_BITMAPS] = {0,};
2436     int bitmapDepth[MAX_NUMBER_OF_BITMAPS] = {0,};
2437     int bitmapSize[MAX_NUMBER_OF_BITMAPS] = {0,};
2438     grib_accessors_list* bitmapStart[MAX_NUMBER_OF_BITMAPS] = {0,};
2439     grib_accessors_list* lastAccessorInList = NULL;
2440     int bitmapIndex                         = -1;
2441     int incrementBitmapIndex                = 1;
2442     grib_accessor* elementFromBitmap        = NULL;
2443     grib_handle* hand = grib_handle_of_accessor(a);
2444     /*int reuseBitmap=0;*/
2445     int add_dump_flag = 1, count = 0;
2446     /*int forceGroupClosure=0;*/
2447 
2448     creatorGroup.op         = "bufr_group";
2449     creatorGroup.name       = "groupNumber";
2450     creatorGroup.name_space = "";
2451     creatorGroup.flags      = GRIB_ACCESSOR_FLAG_DUMP;
2452     creatorGroup.set        = 0;
2453 
2454     if (self->dataAccessors) {
2455         grib_accessors_list_delete(c, self->dataAccessors);
2456     }
2457     self->dataAccessors = grib_accessors_list_create(c);
2458 
2459     if (self->dataAccessorsTrie) {
2460         grib_trie_with_rank_delete(self->dataAccessorsTrie);
2461     }
2462     self->dataAccessorsTrie = grib_trie_with_rank_new(c);
2463 
2464     if (self->tempStrings) {
2465         grib_sarray_delete_content(c, self->tempStrings);
2466         grib_sarray_delete(c, self->tempStrings);
2467         self->tempStrings = NULL;
2468     }
2469     self->tempStrings = self->numberOfSubsets? grib_sarray_new(c, self->numberOfSubsets, 500) : NULL;
2470 
2471     end         = self->compressedData ? 1 : self->numberOfSubsets;
2472     groupNumber = 1;
2473 
2474     gaGroup                    = grib_accessor_factory(self->dataKeys, &creatorGroup, 0, NULL);
2475     gaGroup->bufr_group_number = groupNumber;
2476     gaGroup->sub_section       = grib_section_create(hand, gaGroup);
2477     section                    = gaGroup->sub_section;
2478     /*rootSection=section;*/
2479     /*sectionUp=self->dataKeys;*/
2480     accessor_constant_set_type(gaGroup, GRIB_TYPE_LONG);
2481     accessor_constant_set_dval(gaGroup, groupNumber);
2482     /* ECC-765: Don't empty out the section_4 keys otherwise there will be memory leaks. */
2483     /* Setting first and last to zero effectively masks out those section 4 keys! */
2484     /* self->dataKeys->block->first=0; */
2485     /* self->dataKeys->block->last=0;  */
2486     grib_push_accessor(gaGroup, self->dataKeys->block); /* Add group accessors to section 4 */
2487 
2488     /*indexOfGroupNumber=0;*/
2489     depth                = 0;
2490     extraElement         = 0;
2491     add_extra_attributes = adding_extra_key_attributes(hand);
2492 
2493     for (iss = 0; iss < end; iss++) {
2494         qualityPresent = 0;
2495         /*forceGroupClosure=0;*/
2496         elementsInSubset = self->compressedData ? grib_iarray_used_size(self->elementsDescriptorsIndex->v[0]) : grib_iarray_used_size(self->elementsDescriptorsIndex->v[iss]);
2497         /*if (associatedFieldAccessor) grib_accessor_delete(c, associatedFieldAccessor);*/
2498         associatedFieldAccessor = NULL;
2499         if (associatedFieldSignificanceAccessor) {
2500             grib_accessor_delete(c, associatedFieldSignificanceAccessor);
2501             associatedFieldSignificanceAccessor = NULL;
2502         }
2503         for (ide = 0; ide < elementsInSubset; ide++) {
2504             idx = self->compressedData ? self->elementsDescriptorsIndex->v[0]->v[ide] : self->elementsDescriptorsIndex->v[iss]->v[ide];
2505 
2506             descriptor = self->expanded->v[idx];
2507             if (descriptor->nokey == 1) {
2508                 continue; /* Descriptor does not have an associated key e.g. inside op 203YYY */
2509             }
2510             elementFromBitmap = NULL;
2511             if (descriptor->F == 0 && IS_COORDINATE_DESCRIPTOR(descriptor->X) &&
2512                 self->unpackMode == CODES_BUFR_UNPACK_STRUCTURE) {
2513                 const int sidx = descriptor->Y + significanceQualifierIndexArray[descriptor->X] * NUMBER_OF_QUALIFIERS_PER_CATEGORY;
2514                 DebugAssert(sidx > 0);
2515                 groupNumber++;
2516 
2517                 if (significanceQualifierGroup[sidx]) {
2518                     groupSection = significanceQualifierGroup[sidx]->parent;
2519                     depth        = significanceQualifierDepth[sidx];
2520                     if (depth < max_depth) {
2521                         /* If depth >= max_depth, then no entry will be deeper so no need for call */
2522                         reset_deeper_qualifiers(significanceQualifierGroup, significanceQualifierDepth,
2523                                                 number_of_qualifiers, depth);
2524                     }
2525                 }
2526                 else {
2527                     /* if (forceGroupClosure) { */
2528                     /* groupSection=sectionUp; */
2529                     /* forceGroupClosure=0; */
2530                     /* depth=0; */
2531                     /* } else { */
2532                     groupSection = section;
2533                     depth++;
2534                     /* } */
2535                 }
2536 
2537                 gaGroup                    = grib_accessor_factory(groupSection, &creatorGroup, 0, NULL);
2538                 gaGroup->sub_section       = grib_section_create(hand, gaGroup);
2539                 gaGroup->bufr_group_number = groupNumber;
2540                 accessor_constant_set_type(gaGroup, GRIB_TYPE_LONG);
2541                 accessor_constant_set_dval(gaGroup, groupNumber);
2542                 grib_push_accessor(gaGroup, groupSection->block);
2543 
2544                 section = gaGroup->sub_section;
2545                 /*sectionUp=gaGroup->parent;*/
2546 
2547                 significanceQualifierGroup[sidx] = gaGroup;
2548                 significanceQualifierDepth[sidx] = depth;
2549                 if (depth > max_depth)
2550                     max_depth = depth;
2551                 incrementBitmapIndex = 1;
2552                 add_dump_flag        = 1;
2553             }
2554             else if (descriptor->code == 31031 && incrementBitmapIndex != 0) {
2555                 /* bitmap */
2556                 bitmapIndex++;
2557                 groupNumber++;
2558                 incrementBitmapIndex = 0;
2559                 if (bitmapIndex >= MAX_NUMBER_OF_BITMAPS) {
2560                     grib_context_log(c, GRIB_LOG_ERROR, "Bitmap error: bitmap index=%d, max num bitmaps=%d\n", bitmapIndex, MAX_NUMBER_OF_BITMAPS);
2561                     err = GRIB_DECODING_ERROR;
2562                     return err;
2563                 }
2564                 bitmapStart[bitmapIndex] = grib_accessors_list_last(self->dataAccessors);
2565                 bitmapSize[bitmapIndex]  = 1;
2566                 if (self->expanded->v[idx - 1]->code == 31002 || self->expanded->v[idx - 1]->code == 31001)
2567                     extraElement += 1;
2568 
2569                 if (bitmapGroup[bitmapIndex]) {
2570                     groupSection = bitmapGroup[bitmapIndex]->parent;
2571                     depth        = bitmapDepth[bitmapIndex];
2572                     reset_deeper_qualifiers(significanceQualifierGroup, significanceQualifierDepth,
2573                                             number_of_qualifiers, depth);
2574                     /* TODO: This branch is not reached in our tests! */
2575                     reset_deeper_qualifiers(bitmapGroup, bitmapDepth, MAX_NUMBER_OF_BITMAPS, depth);
2576                 }
2577                 else {
2578                     groupSection = section;
2579                     depth++;
2580                 }
2581                 gaGroup                    = grib_accessor_factory(groupSection, &creatorGroup, 0, NULL);
2582                 gaGroup->sub_section       = grib_section_create(hand, gaGroup);
2583                 gaGroup->bufr_group_number = groupNumber;
2584                 accessor_constant_set_type(gaGroup, GRIB_TYPE_LONG);
2585                 accessor_constant_set_dval(gaGroup, groupNumber);
2586                 grib_push_accessor(gaGroup, groupSection->block);
2587 
2588                 section = gaGroup->sub_section;
2589                 /*sectionUp=gaGroup->parent;*/
2590                 bitmapGroup[bitmapIndex] = gaGroup;
2591                 bitmapDepth[bitmapIndex] = depth;
2592                 add_dump_flag            = 1;
2593             }
2594             else if (descriptor->code == 31031) {
2595                 add_dump_flag = 1;
2596                 bitmapSize[bitmapIndex]++;
2597                 bitmap.cursor = 0;
2598             }
2599             else if (descriptor->code == 222000 || descriptor->code == 223000 || descriptor->code == 224000 || descriptor->code == 225000) {
2600                 bitmap.referredElement = NULL;
2601                 qualityPresent         = 1;
2602                 incrementBitmapIndex   = 1;
2603                 add_dump_flag          = 1;
2604                 bitmap.cursor          = 0;
2605                 extraElement += 1;
2606             }
2607             else if (descriptor->code == 236000 || descriptor->code == 237000) {
2608                 bitmap.referredElement = NULL;
2609                 bitmap.cursor          = 0;
2610                 /*reuseBitmap=1;*/
2611                 extraElement += 1;
2612                 add_dump_flag = 1;
2613             }
2614             else if (descriptor->code == 237255) {
2615                 /*reuseBitmap=0;*/
2616                 incrementBitmapIndex = 1;
2617                 bitmap.cursor        = 0;
2618                 add_dump_flag        = 1;
2619             }
2620             else if ((descriptor->X == 33 || bufr_descriptor_is_marker(descriptor)) && qualityPresent) {
2621                 if (!bitmap.referredElement)
2622                     bitmap_init(c, &bitmap, bitmapStart[bitmapIndex], bitmapSize[bitmapIndex], lastAccessorInList);
2623                 elementFromBitmap = get_element_from_bitmap(a, &bitmap);
2624                 add_dump_flag     = 1;
2625                 /* } else if ( descriptor->Y==1 && IS_COORDINATE_DESCRIPTOR(self->expanded->v[idx-1]->X)==0) { */
2626                 /* forceGroupClosure=1; */
2627                 /* reset_qualifiers(significanceQualifierGroup); */
2628             }
2629             else if (descriptor->X == 33 && !qualityPresent) {
2630                 add_dump_flag = 1; /* ECC-690: percentConfidence WITHOUT a bitmap! e.g. NOAA GOES16 BUFR */
2631             }
2632 
2633             if (ide == 0 && !self->compressedData) {
2634                 grib_accessor* asn    = NULL;
2635                 long subsetNumber     = iss + 1;
2636                 size_t len            = 1;
2637                 grib_action creatorsn = {0,};
2638                 creatorsn.op         = "variable";
2639                 creatorsn.name_space = "";
2640                 creatorsn.flags      = GRIB_ACCESSOR_FLAG_READ_ONLY | GRIB_ACCESSOR_FLAG_DUMP;
2641                 creatorsn.set        = 0;
2642 
2643                 creatorsn.name = "subsetNumber";
2644                 asn            = grib_accessor_factory(section, &creatorsn, 0, NULL);
2645                 accessor_variable_set_type(asn, GRIB_TYPE_LONG);
2646                 grib_pack_long(asn, &subsetNumber, &len);
2647 
2648                 grib_push_accessor(asn, section->block);
2649                 rank = grib_data_accessors_trie_push(self->dataAccessorsTrie, asn);
2650                 grib_accessors_list_push(self->dataAccessors, asn, rank);
2651             }
2652             count++;
2653             elementAccessor = create_accessor_from_descriptor(a, associatedFieldAccessor, section, ide, iss, add_dump_flag, count, add_extra_attributes);
2654             if (!elementAccessor) {
2655                 err = GRIB_DECODING_ERROR;
2656                 return err;
2657             }
2658             if (elementAccessor->name == NULL) {
2659                 return GRIB_DECODING_ERROR;
2660             }
2661 
2662             /*if (associatedFieldAccessor) grib_accessor_delete(c, associatedFieldAccessor);*/
2663             associatedFieldAccessor = NULL;
2664             if (elementFromBitmap && self->unpackMode == CODES_BUFR_UNPACK_STRUCTURE) {
2665                 if (descriptor->code != 33007 && descriptor->code != 223255) {
2666                     char* aname                = grib_context_strdup(c, elementFromBitmap->name);
2667                     grib_accessor* newAccessor = grib_accessor_clone(elementAccessor, section, &err);
2668                     newAccessor->parent        = groupSection;
2669                     newAccessor->name          = aname;
2670                     grib_sarray_push(a->context, self->tempStrings, aname);
2671                     grib_push_accessor(newAccessor, groupSection->block);
2672                     rank = grib_data_accessors_trie_push(self->dataAccessorsTrie, newAccessor);
2673                     grib_accessors_list_push(self->dataAccessors, newAccessor, rank);
2674                 }
2675 
2676                 err = grib_accessor_add_attribute(accessor_or_attribute_with_same_name(elementFromBitmap, elementAccessor->name), elementAccessor, 1);
2677             }
2678             else if (elementAccessor) {
2679                 int add_key = 1;
2680                 switch (descriptor->code) {
2681                     case 999999:
2682                         /*if (associatedFieldAccessor) grib_accessor_delete(c, associatedFieldAccessor);*/
2683                         associatedFieldAccessor = elementAccessor;
2684                         grib_convert_to_attribute(associatedFieldAccessor);
2685                         if (associatedFieldSignificanceAccessor) {
2686                             grib_accessor* newAccessor = grib_accessor_clone(associatedFieldSignificanceAccessor, section, &err);
2687                             if (err) {
2688                                 grib_context_log(a->context, GRIB_LOG_ERROR,"Unable to clone accessor '%s'\n", associatedFieldSignificanceAccessor->name);
2689                                 return err;
2690                             }
2691                             grib_accessor_add_attribute(associatedFieldAccessor, newAccessor, 1);
2692                         }
2693                         break;
2694                     case 31021:
2695                         if (associatedFieldSignificanceAccessor)
2696                             grib_accessor_delete(c, associatedFieldSignificanceAccessor);
2697                         associatedFieldSignificanceAccessor = elementAccessor;
2698                         break;
2699                         /*case 33007:*/
2700                         /* ECC-690: See later */
2701                         /* break; */
2702                     default:
2703                         add_key = 1;
2704                         /* ECC-690: percentConfidence WITHOUT a bitmap! e.g. NOAA GOES16 BUFR */
2705                         if (descriptor->code == 33007) {
2706                             add_key = 0; /* Standard behaviour */
2707                             if (!qualityPresent) {
2708                                 add_key = 1;
2709                             }
2710                         }
2711                         if (add_key) {
2712                             grib_push_accessor(elementAccessor, section->block);
2713                             rank = grib_data_accessors_trie_push(self->dataAccessorsTrie, elementAccessor);
2714                             grib_accessors_list_push(self->dataAccessors, elementAccessor, rank);
2715                             lastAccessorInList = grib_accessors_list_last(self->dataAccessors);
2716                         }
2717                 }
2718             }
2719         }
2720     }
2721 
2722     return err;
2723 }
2724 
set_input_replications(grib_handle * h,grib_accessor_bufr_data_array * self)2725 static void set_input_replications(grib_handle* h, grib_accessor_bufr_data_array* self)
2726 {
2727     size_t nInputReplications;
2728     size_t nInputExtendedReplications;
2729     size_t nInputShortReplications;
2730     self->nInputReplications         = -1;
2731     self->nInputExtendedReplications = -1;
2732     self->nInputShortReplications    = -1;
2733     self->iInputReplications         = 0;
2734     self->iInputExtendedReplications = 0;
2735     self->iInputShortReplications    = 0;
2736     if (grib_get_size(h, "inputDelayedDescriptorReplicationFactor", &nInputReplications) == 0 && nInputReplications != 0) {
2737         if (self->inputReplications)
2738             grib_context_free(h->context, self->inputReplications);
2739         self->inputReplications = (long*)grib_context_malloc_clear(h->context, sizeof(long) * nInputReplications);
2740         grib_get_long_array(h, "inputDelayedDescriptorReplicationFactor", self->inputReplications, &nInputReplications);
2741         /* default-> no input replications*/
2742         if (self->inputReplications[0] < 0)
2743             self->nInputReplications = -1;
2744         else
2745             self->nInputReplications = nInputReplications;
2746     }
2747     if (grib_get_size(h, "inputExtendedDelayedDescriptorReplicationFactor", &nInputExtendedReplications) == 0 && nInputExtendedReplications != 0) {
2748         if (self->inputExtendedReplications)
2749             grib_context_free(h->context, self->inputExtendedReplications);
2750         self->inputExtendedReplications = (long*)grib_context_malloc_clear(h->context, sizeof(long) * nInputExtendedReplications);
2751         grib_get_long_array(h, "inputExtendedDelayedDescriptorReplicationFactor", self->inputExtendedReplications, &nInputExtendedReplications);
2752         /* default-> no input replications*/
2753         if (self->inputExtendedReplications[0] < 0)
2754             self->nInputExtendedReplications = -1;
2755         else
2756             self->nInputExtendedReplications = nInputExtendedReplications;
2757     }
2758     if (grib_get_size(h, "inputShortDelayedDescriptorReplicationFactor", &nInputShortReplications) == 0 && nInputShortReplications != 0) {
2759         if (self->inputShortReplications)
2760             grib_context_free(h->context, self->inputShortReplications);
2761         self->inputShortReplications = (long*)grib_context_malloc_clear(h->context, sizeof(long) * nInputShortReplications);
2762         grib_get_long_array(h, "inputShortDelayedDescriptorReplicationFactor", self->inputShortReplications, &nInputShortReplications);
2763         /* default-> no input replications*/
2764         if (self->inputShortReplications[0] < 0)
2765             self->nInputShortReplications = -1;
2766         else
2767             self->nInputShortReplications = nInputShortReplications;
2768     }
2769 }
2770 
set_input_bitmap(grib_handle * h,grib_accessor_bufr_data_array * self)2771 static void set_input_bitmap(grib_handle* h, grib_accessor_bufr_data_array* self)
2772 {
2773     size_t nInputBitmap;
2774     self->nInputBitmap = -1;
2775     self->iInputBitmap = 0;
2776     if (grib_get_size(h, "inputDataPresentIndicator", &nInputBitmap) == 0 && nInputBitmap != 0) {
2777         if (self->inputBitmap)
2778             grib_context_free(h->context, self->inputBitmap);
2779         self->inputBitmap = (double*)grib_context_malloc_clear(h->context, sizeof(double) * nInputBitmap);
2780         grib_get_double_array(h, "inputDataPresentIndicator", self->inputBitmap, &nInputBitmap);
2781         /* default-> no input bitmap*/
2782         if (self->inputBitmap[0] < 0)
2783             self->nInputBitmap = -1;
2784         else
2785             self->nInputBitmap = nInputBitmap;
2786     }
2787 }
2788 
set_to_missing_if_out_of_range(grib_handle * h)2789 static int set_to_missing_if_out_of_range(grib_handle* h)
2790 {
2791     /* First check if the transient key is set */
2792     long setToMissingIfOutOfRange = 0;
2793     if (grib_get_long(h, "setToMissingIfOutOfRange", &setToMissingIfOutOfRange) == GRIB_SUCCESS &&
2794         setToMissingIfOutOfRange != 0) {
2795         return 1;
2796     }
2797     /* Then check the environment variable via the context */
2798     return h->context->bufr_set_to_missing_if_out_of_range;
2799 }
2800 
process_elements(grib_accessor * a,int flag,long onlySubset,long startSubset,long endSubset)2801 static int process_elements(grib_accessor* a, int flag, long onlySubset, long startSubset, long endSubset)
2802 {
2803     int err = 0;
2804     long inr, innr, ir, ip;
2805     long n[MAX_NESTED_REPLICATIONS] = {0,};
2806     long nn[MAX_NESTED_REPLICATIONS] = {0,};
2807     long numberOfElementsToRepeat[MAX_NESTED_REPLICATIONS] = {0,};
2808     long numberOfRepetitions[MAX_NESTED_REPLICATIONS] = {0,};
2809     long startRepetition[MAX_NESTED_REPLICATIONS] = {0,};
2810     long numberOfNestedRepetitions = 0;
2811     unsigned char* data            = 0;
2812     size_t subsetListSize          = 0;
2813     long* subsetList               = 0;
2814     int i;
2815     grib_iarray* elementsDescriptorsIndex = 0;
2816 
2817     long pos = 0, dataOffset = 0;
2818     long iiss, iss, end, elementIndex, index;
2819     long numberOfDescriptors;
2820     long totalSize;
2821     bufr_descriptor** descriptors = 0;
2822     long icount;
2823     int decoding = 0, do_clean = 1;
2824     grib_buffer* buffer = NULL;
2825     codec_element_proc codec_element;
2826     codec_replication_proc codec_replication;
2827     grib_accessor* dataAccessor = NULL;
2828     bufr_descriptor* bd         = 0;
2829 
2830     grib_darray* dval                   = NULL;
2831     grib_sarray* sval                   = NULL;
2832     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
2833 
2834     grib_handle* h  = grib_handle_of_accessor(a);
2835     grib_context* c = h->context;
2836 
2837     totalSize = self->bitsToEndData;
2838 
2839     switch (flag) {
2840         case PROCESS_DECODE:
2841             if (!self->do_decode)
2842                 return 0;
2843             self->do_decode = 0;
2844             buffer          = h->buffer;
2845             decoding        = 1;
2846             do_clean        = 1;
2847             dataAccessor    = grib_find_accessor(grib_handle_of_accessor(a), self->bufrDataEncodedName);
2848             DebugAssert(dataAccessor);
2849             dataOffset        = accessor_raw_get_offset(dataAccessor);
2850             pos               = dataOffset * 8;
2851             codec_element     = &decode_element;
2852             codec_replication = &decode_replication;
2853             break;
2854         case PROCESS_NEW_DATA:
2855             buffer                               = grib_create_growable_buffer(c);
2856             decoding                             = 0;
2857             do_clean                             = 1;
2858             self->do_decode                      = 1;
2859             self->set_to_missing_if_out_of_range = set_to_missing_if_out_of_range(h);
2860             pos                                  = 0;
2861             codec_element                        = &encode_new_element;
2862             codec_replication                    = &encode_new_replication;
2863 
2864             set_input_replications(h, self);
2865             set_input_bitmap(h, self);
2866 
2867             break;
2868         case PROCESS_ENCODE:
2869             buffer                               = grib_create_growable_buffer(c);
2870             decoding                             = 0;
2871             do_clean                             = 0;
2872             self->do_decode                      = 0;
2873             self->set_to_missing_if_out_of_range = set_to_missing_if_out_of_range(h);
2874             pos                                  = 0;
2875             codec_element                        = &encode_element;
2876             grib_get_long(grib_handle_of_accessor(a), "extractSubset", &onlySubset);
2877             grib_get_long(grib_handle_of_accessor(a), "extractSubsetIntervalStart", &startSubset);
2878             grib_get_long(grib_handle_of_accessor(a), "extractSubsetIntervalEnd", &endSubset);
2879             err = grib_get_size(grib_handle_of_accessor(a), "extractSubsetList", &subsetListSize);
2880             if (err)
2881                 return err;
2882             if (subsetList)
2883                 grib_context_free(c, subsetList);
2884             subsetList = (long*)grib_context_malloc_clear(c, subsetListSize * sizeof(long));
2885             err        = grib_get_long_array(grib_handle_of_accessor(a), "extractSubsetList", subsetList, &subsetListSize);
2886             if (err)
2887                 return err;
2888 
2889             codec_replication = &encode_replication;
2890             break;
2891         default:
2892             return GRIB_NOT_IMPLEMENTED;
2893     }
2894     data = buffer->data;
2895 
2896     err = get_descriptors(a);
2897     if (err)
2898         return err;
2899 
2900     descriptors = self->expanded->v;
2901     if (!descriptors) {
2902         grib_context_log(c, GRIB_LOG_ERROR, "No descriptors found!");
2903         return GRIB_INTERNAL_ERROR;
2904     }
2905 
2906     if (do_clean == 1 && self->numericValues) {
2907         grib_vdarray_delete_content(c, self->numericValues);
2908         grib_vdarray_delete(c, self->numericValues);
2909         /*printf("dbg process_elements: clear %p\n", (void*)(self->stringValues));*/
2910         grib_vsarray_delete_content(c, self->stringValues);
2911         grib_vsarray_delete(c, self->stringValues);
2912         self->stringValues = NULL;
2913     }
2914 
2915     if (flag != PROCESS_ENCODE) {
2916         self->numericValues = grib_vdarray_new(c, 1000, 1000);
2917         self->stringValues  = grib_vsarray_new(c, 10, 10);
2918         /*printf("dbg process_elements: Create a new one %p\n", (void*)(self->stringValues));*/
2919 
2920         if (self->elementsDescriptorsIndex)
2921             grib_viarray_delete(c, self->elementsDescriptorsIndex);
2922         self->elementsDescriptorsIndex = grib_viarray_new(c, 100, 100);
2923     }
2924 
2925     if (flag != PROCESS_DECODE) { /* Operator 203YYY: key OVERRIDDEN_REFERENCE_VALUES_KEY */
2926         err = grib_get_size(h, OVERRIDDEN_REFERENCE_VALUES_KEY, &self->refValListSize);
2927         if (err)
2928             return err;
2929         if (self->refValList)
2930             grib_context_free(c, self->refValList);
2931         if (self->refValListSize > 0) {
2932             self->refValList = (long*)grib_context_malloc_clear(c, self->refValListSize * sizeof(long));
2933             err              = grib_get_long_array(grib_handle_of_accessor(a), OVERRIDDEN_REFERENCE_VALUES_KEY, self->refValList, &self->refValListSize);
2934             if (err)
2935                 return err;
2936         }
2937     }
2938 
2939     numberOfDescriptors = grib_bufr_descriptors_array_used_size(self->expanded);
2940 
2941     if (self->iss_list) {
2942         grib_iarray_delete(self->iss_list);
2943         self->iss_list = 0;
2944     }
2945     end = self->compressedData == 1 ? 1 : self->numberOfSubsets;
2946 
2947     if (flag != PROCESS_DECODE) {
2948         self->iss_list = set_subset_list(c, self, onlySubset, startSubset, endSubset, subsetList, subsetListSize);
2949         end            = self->compressedData == 1 ? 1 : grib_iarray_used_size(self->iss_list);
2950     }
2951 
2952     for (iiss = 0; iiss < end; iiss++) {
2953         icount = 1;
2954         if (self->compressedData == 0 && self->iss_list) {
2955             iss = self->iss_list->v[iiss];
2956         }
2957         else {
2958             iss = iiss;
2959         }
2960 
2961         grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data processing: subsetNumber=%ld", iss + 1);
2962         self->refValIndex = 0;
2963 
2964         if (flag != PROCESS_ENCODE) {
2965             elementsDescriptorsIndex = grib_iarray_new(c, DYN_ARRAY_SIZE_INIT, DYN_ARRAY_SIZE_INCR);
2966             if (!self->compressedData) {
2967                 dval = grib_darray_new(c, DYN_ARRAY_SIZE_INIT, DYN_ARRAY_SIZE_INCR);
2968                 /* sval=grib_sarray_new(c,10,10); */
2969             }
2970         }
2971         else {
2972             if (self->elementsDescriptorsIndex == NULL) {
2973                 return GRIB_ENCODING_ERROR; /* See ECC-359 */
2974             }
2975             elementsDescriptorsIndex = self->elementsDescriptorsIndex->v[iss];
2976             dval                     = self->numericValues->v[iss];
2977         }
2978         elementIndex = 0;
2979 
2980         numberOfNestedRepetitions = 0;
2981 
2982         for (i = 0; i < numberOfDescriptors; i++) {
2983             if (c->debug) grib_context_log(c, GRIB_LOG_DEBUG, "BUFR data processing: elementNumber=%ld code=%6.6ld",
2984                                            icount++, descriptors[i]->code);
2985             switch (descriptors[i]->F) {
2986                 case 0:
2987                     /* Table B element */
2988                     if (flag != PROCESS_ENCODE)
2989                         grib_iarray_push(elementsDescriptorsIndex, i);
2990                     if (descriptors[i]->code == 31031 && !is_bitmap_start_defined(self)) {
2991                         /* self->bitmapStart=grib_iarray_used_size(elementsDescriptorsIndex)-1; */
2992                         self->bitmapStart = elementIndex;
2993                     }
2994 
2995                     err = codec_element(c, self, iss, buffer, data, &pos, i, 0, elementIndex, dval, sval);
2996                     if (err)
2997                         return err;
2998                     elementIndex++;
2999                     break;
3000                 case 1:
3001                     /* Delayed replication */
3002                     inr = numberOfNestedRepetitions;
3003                     numberOfNestedRepetitions++;
3004                     DebugAssert(numberOfNestedRepetitions <= MAX_NESTED_REPLICATIONS);
3005                     numberOfElementsToRepeat[inr] = descriptors[i]->X;
3006                     n[inr]                        = numberOfElementsToRepeat[inr];
3007                     i++;
3008 
3009                     /* ECC-517 */
3010                     data = buffer->data;
3011                     err  = codec_replication(c, self, iss, buffer, data, &pos, i, elementIndex, dval, &(numberOfRepetitions[inr]));
3012                     if (err)
3013                         return err;
3014 
3015                     startRepetition[inr] = i;
3016                     nn[inr]              = numberOfRepetitions[inr];
3017                     if (flag != PROCESS_ENCODE)
3018                         grib_iarray_push(elementsDescriptorsIndex, i);
3019                     elementIndex++;
3020                     if (numberOfRepetitions[inr] == 0) {
3021                         i += numberOfElementsToRepeat[inr];
3022                         if (inr > 0) {
3023                             n[inr - 1] -= numberOfElementsToRepeat[inr] + 2;
3024                             /* if the empty nested repetition is at the end of the nesting repetition
3025                            we need to re-point to the start of the nesting repetition */
3026                             ip = inr - 1;
3027                             while (ip >= 0 && n[ip] == 0) {
3028                                 nn[ip]--;
3029                                 if (nn[ip] <= 0) {
3030                                     numberOfNestedRepetitions--;
3031                                 }
3032                                 else {
3033                                     n[ip] = numberOfElementsToRepeat[ip];
3034                                     i     = startRepetition[ip];
3035                                 }
3036                                 ip--;
3037                             }
3038                         }
3039                         numberOfNestedRepetitions--;
3040                     }
3041                     continue;
3042                 case 2:
3043                     /* Operator */
3044                     switch (descriptors[i]->X) {
3045                         case 3:
3046                             /* Change reference values */
3047                             if (self->compressedData == 1 && flag != PROCESS_DECODE) {
3048                                 grib_context_log(c, GRIB_LOG_ERROR, "process_elements: operator %d not supported for encoding compressed data", descriptors[i]->X);
3049                                 return GRIB_INTERNAL_ERROR;
3050                             }
3051                             if (descriptors[i]->Y == 255) {
3052                                 grib_context_log(c, GRIB_LOG_DEBUG, "Operator 203YYY: Y=255, definition of new reference values is concluded");
3053                                 self->change_ref_value_operand = 255;
3054                                 /*if (c->debug) tableB_override_dump(self);*/
3055                                 if (iss == 0 && flag == PROCESS_DECODE) {
3056                                     /*Write out the contents of the TableB overridden reference values to the transient array key*/
3057                                     err = tableB_override_set_key(h, self);
3058                                     if (err)
3059                                         return err;
3060                                 }
3061                                 if (flag != PROCESS_DECODE) {
3062                                     /* Encoding operator 203YYY */
3063                                     if (self->refValIndex != self->refValListSize) {
3064                                         grib_context_log(c, GRIB_LOG_ERROR,
3065                                                          "process_elements: The number of overridden reference values (%ld) different from"
3066                                                          " number of descriptors between operator 203YYY and 203255 (%ld)",
3067                                                          self->refValListSize, self->refValIndex);
3068                                         return GRIB_ENCODING_ERROR;
3069                                     }
3070                                 }
3071                             }
3072                             else if (descriptors[i]->Y == 0) {
3073                                 grib_context_log(c, GRIB_LOG_DEBUG, "Operator 203YYY: Y=0, clearing override of table B");
3074                                 tableB_override_clear(c, self);
3075                                 self->change_ref_value_operand = 0;
3076                             }
3077                             else {
3078                                 const int numBits = descriptors[i]->Y;
3079                                 grib_context_log(c, GRIB_LOG_DEBUG, "Operator 203YYY: Definition phase: Num bits=%d", numBits);
3080                                 self->change_ref_value_operand = numBits;
3081                                 tableB_override_clear(c, self);
3082                                 if (flag != PROCESS_DECODE) {
3083                                     err = check_overridden_reference_values(c, self->refValList, self->refValListSize, numBits);
3084                                     if (err)
3085                                         return err;
3086                                 }
3087                             }
3088                             /*grib_iarray_push(elementsDescriptorsIndex,i);*/
3089                             if (decoding)
3090                                 push_zero_element(self, dval);
3091                             break;
3092 
3093                         case 5: /* Signify character */
3094                             descriptors[i]->width = descriptors[i]->Y * 8;
3095                             descriptors[i]->type  = BUFR_DESCRIPTOR_TYPE_STRING;
3096                             err                   = codec_element(c, self, iss, buffer, data, &pos, i, 0, elementIndex, dval, sval);
3097                             if (err)
3098                                 return err;
3099                             if (flag != PROCESS_ENCODE)
3100                                 grib_iarray_push(elementsDescriptorsIndex, i);
3101                             elementIndex++;
3102                             break;
3103                         case 22: /* Quality information follows */
3104                             if (descriptors[i]->Y == 0) {
3105                                 if (flag == PROCESS_DECODE) {
3106                                     grib_iarray_push(elementsDescriptorsIndex, i);
3107                                     push_zero_element(self, dval);
3108                                 }
3109                                 else if (flag == PROCESS_ENCODE) {
3110                                     if (descriptors[i + 1] && descriptors[i + 1]->code != 236000 && descriptors[i + 1]->code != 237000)
3111                                         restart_bitmap(self);
3112                                 }
3113                                 else if (flag == PROCESS_NEW_DATA) {
3114                                     grib_iarray_push(elementsDescriptorsIndex, i);
3115                                     if (descriptors[i + 1] && descriptors[i + 1]->code != 236000 && descriptors[i + 1]->code != 237000)
3116                                         consume_bitmap(self, i);
3117                                 }
3118                                 elementIndex++;
3119                             }
3120                             break;
3121                         case 26:
3122                         case 27:
3123                         case 29:
3124                         case 30:
3125                         case 31:
3126                         case 33:
3127                         case 34:
3128                         case 38:
3129                         case 39:
3130                         case 40:
3131                         case 41:
3132                         case 42:
3133                             if (flag != PROCESS_ENCODE)
3134                                 grib_iarray_push(elementsDescriptorsIndex, i);
3135                             if (decoding)
3136                                 push_zero_element(self, dval);
3137                             elementIndex++;
3138                             break;
3139                         case 24:
3140                             /*first-order statistical values marker operator*/
3141                         case 32:
3142                             /*replaced/retained values marker operator*/
3143                             if (descriptors[i]->Y == 255) {
3144                                 index = get_next_bitmap_descriptor_index(self, elementsDescriptorsIndex, dval);
3145                                 err   = codec_element(c, self, iss, buffer, data, &pos, index, 0, elementIndex, dval, sval);
3146                                 if (err)
3147                                     return err;
3148                                 /* self->expanded->v[index] */
3149                                 if (flag != PROCESS_ENCODE)
3150                                     grib_iarray_push(elementsDescriptorsIndex, i);
3151                                 elementIndex++;
3152                             }
3153                             else {
3154                                 if (flag != PROCESS_ENCODE)
3155                                     grib_iarray_push(elementsDescriptorsIndex, i);
3156                                 if (decoding) {
3157                                     push_zero_element(self, dval);
3158                                 }
3159                                 elementIndex++;
3160                             }
3161                             break;
3162                         case 23:
3163                             if (descriptors[i]->Y == 255) {
3164                                 index = get_next_bitmap_descriptor_index(self, elementsDescriptorsIndex, dval);
3165                                 err   = codec_element(c, self, iss, buffer, data, &pos, index, 0, elementIndex, dval, sval);
3166                                 if (err)
3167                                     return err;
3168                                 /* self->expanded->v[index] */
3169                                 if (flag != PROCESS_ENCODE)
3170                                     grib_iarray_push(elementsDescriptorsIndex, i);
3171                                 elementIndex++;
3172                             }
3173                             else {
3174                                 if (flag == PROCESS_DECODE) {
3175                                     grib_iarray_push(elementsDescriptorsIndex, i);
3176                                     push_zero_element(self, dval);
3177                                     if (descriptors[i + 1] && descriptors[i + 1]->code != 236000 && descriptors[i + 1]->code != 237000) {
3178                                         err = build_bitmap(self, data, &pos, elementIndex, elementsDescriptorsIndex, i);
3179                                         if (err)
3180                                             return err;
3181                                     }
3182                                 }
3183                                 else if (flag == PROCESS_ENCODE) {
3184                                     if (descriptors[i + 1] && descriptors[i + 1]->code != 236000 && descriptors[i + 1]->code != 237000)
3185                                         restart_bitmap(self);
3186                                 }
3187                                 else if (flag == PROCESS_NEW_DATA) {
3188                                     grib_iarray_push(elementsDescriptorsIndex, i);
3189                                     if (descriptors[i + 1] && descriptors[i + 1]->code != 236000 && descriptors[i + 1]->code != 237000) {
3190                                         err = build_bitmap_new_data(self, data, &pos, elementIndex, elementsDescriptorsIndex, i);
3191                                         if (err)
3192                                             return err;
3193                                     }
3194                                 }
3195                                 elementIndex++;
3196                             }
3197                             break;
3198                         case 25:
3199                             /*difference statistical values marker operator*/
3200                             if (descriptors[i]->Y == 255) {
3201                                 index         = get_next_bitmap_descriptor_index(self, elementsDescriptorsIndex, dval);
3202                                 bd            = grib_bufr_descriptor_clone(self->expanded->v[index]);
3203                                 bd->reference = -grib_power(bd->width, 2);
3204                                 bd->width++;
3205 
3206                                 err = codec_element(c, self, iss, buffer, data, &pos, index, bd, elementIndex, dval, sval);
3207                                 grib_bufr_descriptor_delete(bd);
3208                                 if (err)
3209                                     return err;
3210                                 /* self->expanded->v[index] */
3211                                 if (flag != PROCESS_ENCODE)
3212                                     grib_iarray_push(elementsDescriptorsIndex, i);
3213                                 elementIndex++;
3214                             }
3215                             else {
3216                                 if (flag != PROCESS_ENCODE)
3217                                     grib_iarray_push(elementsDescriptorsIndex, i);
3218                                 if (decoding)
3219                                     push_zero_element(self, dval);
3220                                 elementIndex++;
3221                             }
3222                             break;
3223                         case 35:
3224                             /* cancel bitmap */
3225                             if (flag != PROCESS_ENCODE) {
3226                                 grib_iarray_push(elementsDescriptorsIndex, i);
3227                                 if (decoding)
3228                                     push_zero_element(self, dval);
3229                                 if (descriptors[i]->Y == 0)
3230                                     cancel_bitmap(self);
3231                             }
3232                             elementIndex++;
3233                             break;
3234                         case 36:
3235                             /* bitmap */
3236                             if (flag == PROCESS_DECODE) {
3237                                 grib_iarray_push(elementsDescriptorsIndex, i);
3238                                 if (decoding)
3239                                     push_zero_element(self, dval);
3240                                 err = build_bitmap(self, data, &pos, elementIndex, elementsDescriptorsIndex, i);
3241                                 if (err)
3242                                     return err;
3243                             }
3244                             else if (flag == PROCESS_ENCODE) {
3245                                 restart_bitmap(self);
3246                             }
3247                             else if (flag == PROCESS_NEW_DATA) {
3248                                 grib_iarray_push(elementsDescriptorsIndex, i);
3249                                 err = build_bitmap_new_data(self, data, &pos, elementIndex, elementsDescriptorsIndex, i);
3250                                 if (err)
3251                                     return err;
3252                             }
3253                             elementIndex++;
3254                             break;
3255                         case 37:
3256                             /* reuse defined bitmap */
3257                             if (flag != PROCESS_ENCODE) {
3258                                 grib_iarray_push(elementsDescriptorsIndex, i);
3259                                 if (decoding)
3260                                     push_zero_element(self, dval);
3261                             }
3262                             if (descriptors[i]->Y == 0)
3263                                 restart_bitmap(self);
3264                             /* cancel reuse */
3265                             else
3266                                 cancel_bitmap(self);
3267                             elementIndex++;
3268                             break;
3269                         default:
3270                             grib_context_log(c, GRIB_LOG_ERROR, "process_elements: unsupported operator %d\n", descriptors[i]->X);
3271                             return GRIB_INTERNAL_ERROR;
3272                     } /* F == 2 */
3273                     break;
3274                 case 9:
3275                     /* associated field */
3276                     if (descriptors[i]->X == 99 && descriptors[i]->Y == 999) {
3277                         err = codec_element(c, self, iss, buffer, data, &pos, i, 0, elementIndex, dval, sval);
3278                         if (err)
3279                             return err;
3280                         if (flag != PROCESS_ENCODE)
3281                             grib_iarray_push(elementsDescriptorsIndex, i);
3282                         elementIndex++;
3283                     }
3284                     else {
3285                         return GRIB_INTERNAL_ERROR;
3286                     }
3287                     break;
3288                 default:
3289                     err = GRIB_INTERNAL_ERROR;
3290                     return err;
3291             } /* switch F */
3292 
3293             /* delayed repetition check */
3294             innr = numberOfNestedRepetitions - 1;
3295             for (ir = innr; ir >= 0; ir--) {
3296                 if (nn[ir]) {
3297                     if (n[ir] > 1) {
3298                         n[ir]--;
3299                         break;
3300                     }
3301                     else {
3302                         n[ir] = numberOfElementsToRepeat[ir];
3303                         nn[ir]--;
3304                         if (nn[ir]) {
3305                             i = startRepetition[ir];
3306                             break;
3307                         }
3308                         else {
3309                             if (ir > 0) {
3310                                 n[ir - 1] -= numberOfElementsToRepeat[ir] + 1;
3311                             }
3312                             i = startRepetition[ir] + numberOfElementsToRepeat[ir];
3313                             numberOfNestedRepetitions--;
3314                         }
3315                     }
3316                 }
3317                 else {
3318                     if (ir == 0) {
3319                         i                         = startRepetition[ir] + numberOfElementsToRepeat[ir] + 1;
3320                         numberOfNestedRepetitions = 0;
3321                     }
3322                     else {
3323                         numberOfNestedRepetitions--;
3324                     }
3325                 }
3326             }
3327         }
3328         if (flag != PROCESS_ENCODE)
3329             grib_viarray_push(c, self->elementsDescriptorsIndex, elementsDescriptorsIndex);
3330         if (decoding && !self->compressedData) {
3331             grib_vdarray_push(c, self->numericValues, dval);
3332         }
3333     }
3334 
3335     /*if (c->debug) grib_vdarray_print("process_elements: self->numericValues", self->numericValues);*/
3336 
3337     if (decoding) {
3338         err                 = create_keys(a, 0, 0, 0);
3339         self->bitsToEndData = totalSize;
3340     }
3341     else {
3342         self->bitsToEndData = buffer->ulength * 8;
3343         grib_set_bytes(grib_handle_of_accessor(a), self->bufrDataEncodedName, buffer->data, &(buffer->ulength));
3344         grib_buffer_delete(c, buffer);
3345         if (self->numberOfSubsets != grib_iarray_used_size(self->iss_list)) {
3346             grib_set_long(h, self->numberOfSubsetsName, grib_iarray_used_size(self->iss_list));
3347         }
3348     }
3349 
3350     return err;
3351 }
3352 
dump(grib_accessor * a,grib_dumper * dumper)3353 static void dump(grib_accessor* a, grib_dumper* dumper)
3354 {
3355     /* grib_accessor_bufr_data_array *self =(grib_accessor_bufr_data_array*)a; */
3356     /* int err=process_elements(a,PROCESS_DECODE); */
3357 
3358     /* grib_dump_section(dumper,a,self->dataKeys->block); */
3359 
3360     return;
3361 }
3362 
value_count(grib_accessor * a,long * count)3363 static int value_count(grib_accessor* a, long* count)
3364 {
3365     int err = 0, l;
3366     long i;
3367     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
3368 
3369     err = process_elements(a, PROCESS_DECODE, 0, 0, 0);
3370     if (err)
3371         return err;
3372 
3373     if (self->compressedData) {
3374         l = grib_vdarray_used_size(self->numericValues);
3375 
3376         *count = l * self->numberOfSubsets;
3377     }
3378     else {
3379         *count = 0;
3380         for (i = 0; i < self->numberOfSubsets; i++)
3381             *count += grib_iarray_used_size(self->elementsDescriptorsIndex->v[i]);
3382     }
3383 
3384     return err;
3385 }
3386 
unpack_double(grib_accessor * a,double * val,size_t * len)3387 static int unpack_double(grib_accessor* a, double* val, size_t* len)
3388 {
3389     int err                             = 0, i, k, ii;
3390     int proc_flag                       = PROCESS_DECODE;
3391     size_t l                            = 0, elementsInSubset;
3392     long numberOfSubsets                = 0;
3393     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
3394 
3395     if (self->unpackMode == CODES_BUFR_NEW_DATA)
3396         proc_flag = PROCESS_NEW_DATA;
3397 
3398     err = process_elements(a, proc_flag, 0, 0, 0);
3399     if (err)
3400         return err;
3401     if (!val)
3402         return GRIB_SUCCESS;
3403 
3404     /* When we set unpack=1, then the 'val' argument is NULL and we return
3405      * but when client requests a key like 'numericValues', then we end up here
3406      */
3407 
3408     l   = grib_vdarray_used_size(self->numericValues);
3409     err = grib_get_long(grib_handle_of_accessor(a), self->numberOfSubsetsName, &numberOfSubsets);
3410     if (err)
3411         return err;
3412 
3413     if (self->compressedData) {
3414         const size_t rlen = l * self->numberOfSubsets;
3415         ii = 0;
3416         if (*len < rlen) {
3417             grib_context_log(a->context, GRIB_LOG_ERROR,
3418                          "wrong size (%ld) for %s, it contains %ld values ", *len, a->name, rlen);
3419             *len = 0;
3420             return GRIB_ARRAY_TOO_SMALL;
3421         }
3422         for (k = 0; k < numberOfSubsets; k++) {
3423             for (i = 0; i < l; i++) {
3424                 val[ii++] = self->numericValues->v[i]->n > 1 ? self->numericValues->v[i]->v[k] : self->numericValues->v[i]->v[0];
3425             }
3426         }
3427     }
3428     else {
3429         ii = 0;
3430         for (k = 0; k < numberOfSubsets; k++) {
3431             elementsInSubset = grib_iarray_used_size(self->elementsDescriptorsIndex->v[k]);
3432             for (i = 0; i < elementsInSubset; i++) {
3433                 val[ii++] = self->numericValues->v[k]->v[i];
3434             }
3435         }
3436     }
3437 
3438     return GRIB_SUCCESS;
3439 }
3440 
destroy(grib_context * c,grib_accessor * a)3441 static void destroy(grib_context* c, grib_accessor* a)
3442 {
3443     grib_accessor_bufr_data_array* self = (grib_accessor_bufr_data_array*)a;
3444     self_clear(c, self);
3445     if (self->dataAccessors)
3446         grib_accessors_list_delete(c, self->dataAccessors);
3447     if (self->dataAccessorsTrie)
3448         grib_trie_with_rank_delete_container(self->dataAccessorsTrie);
3449     if (self->tempStrings) {
3450         grib_sarray_delete_content(c, self->tempStrings);
3451         grib_sarray_delete(c, self->tempStrings);
3452     }
3453     grib_iarray_delete(self->iss_list);
3454 }
3455