1 /*
2  * Copyright 2005-2018 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 /**
12     include private headers used for all internal functions of
13     grib_api, not seen by the user of the API
14   */
15 
16 #ifndef grib_api_internal_H
17 #define grib_api_internal_H
18 
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /* cmake config header */
25 #ifdef HAVE_GRIB_API_CONFIG_H
26 #include "grib_api_config.h"
27 #endif
28 
29 /* autoconf config header */
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #ifdef _LARGE_FILES
33 #undef _LARGE_FILE_API
34 #endif
35 #endif
36 
37 #ifndef GRIB_INLINE
38 #define GRIB_INLINE
39 #endif
40 
41 #if IS_BIG_ENDIAN
42 
43 #if GRIB_MEM_ALIGN
44 #define FAST_BIG_ENDIAN 0
45 #else
46 #define FAST_BIG_ENDIAN 1
47 #endif
48 
49 #endif
50 
51 #if IEEE_BE
52 #define IEEE
53 #else
54 #if IEEE_LE
55 #define IEEE
56 #endif
57 #endif
58 
59 #include <stdio.h>
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 
63 /* Microsoft Windows Visual Studio support */
64 #include "grib_api_windef.h"
65 
66 #ifndef GRIB_ON_WINDOWS
67    #include <dirent.h>
68    #include <unistd.h>
69    #include <inttypes.h>
70 #else
71    #include <direct.h>
72    #include <io.h>
73 
74    /* Replace C99/Unix rint() for Windows Visual C++ (only before VC++ 2013 versions) */
75    #if defined _MSC_VER && _MSC_VER < 1800
76       double rint(double x);
77    #endif
78 
79    #ifndef S_ISREG
80      #define S_ISREG(mode) (mode & S_IFREG)
81    #endif
82 
83    #ifndef S_ISDIR
84      #define S_ISDIR(mode) (mode & S_IFDIR)
85    #endif
86 
87    #ifndef M_PI
88    #define M_PI 3.14159265358979323846
89    #endif
90 
91    #define R_OK 04   /* Needed for Windows */
92 
93    #  ifndef F_OK
94    #    define F_OK  0
95    #  endif
96 
97    #  define mkdir(dirname,mode)   _mkdir(dirname)
98 
99    #  ifdef _MSC_VER
100    #    define access(path,mode)   _access(path,mode)
101    #    define chmod(path,mode)    _chmod(path,mode)
102    #    define strdup(str)         _strdup(str)
103    #  endif
104 
105 #endif
106 
107 
108 #include <errno.h>
109 #include <stdarg.h>
110 #include <stdlib.h>
111 #include <ctype.h>
112 
113 
114 #ifdef  HAVE_STRING_H
115 #include <string.h>
116 #else
117 #include <strings.h>
118 #endif
119 
120 #if GRIB_LINUX_PTHREADS
121  extern int pthread_mutexattr_settype(pthread_mutexattr_t* attr,int type);
122 #endif
123 
124 #if GRIB_PTHREADS
125  #include <pthread.h>
126  #define GRIB_MUTEX_INIT_ONCE(a,b) pthread_once(a,b);
127  #define GRIB_MUTEX_LOCK(a) pthread_mutex_lock(a);
128  #define GRIB_MUTEX_UNLOCK(a) pthread_mutex_unlock(a);
129  /*
130  #define GRIB_MUTEX_LOCK(a) {pthread_mutex_lock(a); printf("MUTEX LOCK %p %s line %d\n",(void*)a,__FILE__,__LINE__);}
131  #define GRIB_MUTEX_UNLOCK(a) {pthread_mutex_unlock(a);printf("MUTEX UNLOCK %p %s line %d\n",(void*)a,__FILE__,__LINE__);}
132  */
133 #elif GRIB_OMP_THREADS
134  #include <omp.h>
135  #ifdef _MSC_VER
136   #define GRIB_OMP_CRITICAL(a) __pragma(omp critical (a))
137  #else
138   #define GRIB_OMP_STR(a) #a
139   #define GRIB_OMP_XSTR(a) GRIB_OMP_STR(a)
140   #define GRIB_OMP_CRITICAL(a) _Pragma( GRIB_OMP_XSTR(omp critical (a) ) )
141  #endif
142  #define GRIB_MUTEX_INIT_ONCE(a,b) (*(b))();
143  #define GRIB_MUTEX_LOCK(a)  omp_set_nest_lock(a);
144  #define GRIB_MUTEX_UNLOCK(a)  omp_unset_nest_lock(a);
145 #else
146  #define GRIB_MUTEX_INIT_ONCE(a,b)
147  #define GRIB_MUTEX_LOCK(a)
148  #define GRIB_MUTEX_UNLOCK(a)
149 #endif
150 
151 #if GRIB_LINUX_PTHREADS
152  /* Note: in newer pthreads PTHREAD_MUTEX_RECURSIVE and PTHREAD_MUTEX_RECURSIVE_NP are enums */
153  #if !defined (PTHREAD_MUTEX_RECURSIVE)
154   #define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
155  #endif
156 #endif
157 
158 
159 #ifndef HAVE_FSEEKO
160 #define fseeko fseek
161 #define ftello ftell
162 #endif
163 
164 #define Assert(a)       {if(!(a)) grib_fail(#a,__FILE__,__LINE__);}
165 
166 #ifndef NDEBUG
167  #define DebugAssert(a) Assert(a)
168 #else
169  #define DebugAssert(a)
170 #endif
171 
172 /* Compile time assertion - Thanks to Ralf Holly */
173 #define COMPILE_TIME_ASSERT(e) \
174    do { \
175        enum { assert_static__ = 1/(e) }; \
176       } while (0)
177 
178 #define AssertAccess(array, index, size) \
179    do { \
180     if (!((index) >= 0 && (index) < (size)) ) {printf("ARRAY ACCESS ERROR: array=%s idx=%ld size=%ld @ %s +%d \n", #array, index, size, __FILE__, __LINE__); abort();} \
181    } while(0)
182 
183 #ifndef NDEBUG
184  #define DebugAssertAccess(array, index, size) \
185    do { \
186     if (!((index) >= 0 && (index) < (size)) ) {printf("ARRAY ACCESS ERROR: array=%s idx=%ld size=%ld @ %s +%d \n", #array, index, size, __FILE__, __LINE__); abort();} \
187    } while(0)
188 #else
189  #define DebugAssertAccess(array, index, size)
190 #endif
191 
192 
193 #include "grib_api.h"
194 
195 #define GRIB_UNKNOWN_VALUE   -9999.999
196 #define GRIB_KEY_UNDEF "undef"
197 
198 #define GRIB_HANDLE_BIG_ECMWF_GRIB1    1
199 
200 #define MAX_FILE_HANDLES_WITH_MULTI 10
201 #define ACCESSORS_ARRAY_SIZE 2000
202 #define MAX_NUM_CONCEPTS 2000
203 
204 #define GRIB_NAMESPACE      10
205 
206 #define GRIB_MY_BUFFER      0
207 #define GRIB_USER_BUFFER    1
208 
209 #define GRIB_REAL_MODE4    4
210 #define GRIB_REAL_MODE8    8
211 
212 #define MAX_NUM_SECTIONS  9
213 
214 #define GRIB_DISPOSABLE_MEMORY      0
215 #define GRIB_LONG_LASTING_MEMORY    1
216 
217 #define GRIB_LOG_PERROR          (1<<10)
218 
219 /* ACCESSOR COMPARE FLAGS */
220 #define GRIB_COMPARE_NAMES          (1<<0)
221 #define GRIB_COMPARE_TYPES          (1<<1)
222 
223 typedef               struct     grib_expression         grib_expression;
224 typedef               struct     grib_arguments          grib_arguments;
225 
226 typedef               struct     grib_action_file        grib_action_file;
227 typedef               struct     grib_action_file_list   grib_action_file_list;
228 typedef               struct     grib_block_of_accessors grib_block_of_accessors;
229 typedef               struct     grib_buffer             grib_buffer;
230 typedef               struct     grib_accessor_class     grib_accessor_class;
231 typedef               struct     grib_action             grib_action;
232 typedef               struct     grib_action_class       grib_action_class;
233 typedef               struct     grib_section            grib_section;
234 typedef               struct     grib_packer             grib_packer;
235 typedef               struct     grib_codetable          grib_codetable;
236 
237 typedef               struct     grib_accessor           grib_accessor;
238 typedef               struct     grib_iterator_class     grib_iterator_class;
239 typedef               struct     grib_nearest_class      grib_nearest_class;
240 typedef               struct     grib_box_class          grib_box_class;
241 typedef               struct     grib_dumper             grib_dumper;
242 typedef               struct     grib_dumper_class       grib_dumper_class;
243 typedef               struct     grib_dependency         grib_dependency;
244 typedef               struct     string_feed             string_feed;
245 
246 typedef void           (*nearest_init_class_proc)       (grib_nearest_class*);
247 typedef int            (*nearest_init_proc)             (grib_nearest* i,grib_handle*,grib_arguments*);
248 
249 typedef int            (*nearest_find_proc)    (grib_nearest* nearest, grib_handle* h,
250                                                   double inlat, double inlon,
251                                                   unsigned long flags, double* outlats,
252                                                   double* outlons,double *values,
253                                                   double* distances, int* indexes,size_t *len);
254 typedef int            (*nearest_destroy_proc)   (grib_nearest* nearest);
255 
256 typedef void           (*box_init_class_proc)    (grib_box_class*);
257 typedef int            (*box_destroy_proc)       (grib_box*);
258 typedef int            (*box_init_proc)          (grib_box* ,grib_handle*,grib_arguments*);
259 typedef grib_points*   (*box_get_points_proc)    (grib_box*, double, double, double,double, int*);
260 
261 typedef void           (*iterator_init_class_proc)       (grib_iterator_class*);
262 typedef int            (*iterator_init_proc)             (grib_iterator* i,grib_handle*,grib_arguments*);
263 
264 typedef int            (*iterator_next_proc)             (grib_iterator* i, double *lat, double *lon, double *val);
265 typedef int            (*iterator_previous_proc)         (grib_iterator* i, double *lat, double *lon, double *val);
266 typedef int            (*iterator_reset_proc)            (grib_iterator* i);
267 typedef int            (*iterator_destroy_proc)          (grib_iterator* i);
268 typedef long           (*iterator_has_next_proc)         (grib_iterator* i);
269 
270 typedef  int  (*grib_pack_proc)                          (grib_handle* h,const double* in, size_t inlen, void*   out, size_t* outlen);
271 typedef  int  (*grib_unpack_proc)                        (grib_handle* h,const void*   in, size_t inlen, double* out, size_t* outlen);
272 
273 
274 typedef  void   (*accessor_destroy_proc)                  (grib_context* ,   grib_accessor* );
275 
276 typedef  int     (*accessor_unpack_long_proc)             (grib_accessor*, long*,   size_t *len);
277 typedef  int     (*accessor_unpack_double_proc)           (grib_accessor*, double*, size_t *len);
278 typedef  int     (*accessor_unpack_double_element_proc)         (grib_accessor*, size_t, double*);
279 typedef  int     (*accessor_unpack_double_subarray_proc)         (grib_accessor*, double*,size_t , size_t);
280 typedef  int     (*accessor_unpack_string_proc)           (grib_accessor*, char*,   size_t *len);
281 typedef  int     (*accessor_unpack_bytes_proc)            (grib_accessor*, unsigned char*, size_t *len);
282 typedef  int     (*accessor_get_native_type_proc)         (grib_accessor*);
283 typedef  int     (*accessor_notify_change_proc)           (grib_accessor*,grib_accessor*);
284 typedef  void    (*accessor_update_size_proc)             (grib_accessor*,size_t);
285 typedef  size_t  (*accessor_preferred_size_proc)          (grib_accessor*,int);
286 typedef  void    (*accessor_resize_proc)                  (grib_accessor*,size_t);
287 
288 typedef grib_accessor*  (*accessor_next_proc)            (grib_accessor*,int);
289 typedef  grib_section*   (*accessor_sub_section_proc)    (grib_accessor*);
290 
291 
292 typedef  int   (*accessor_pack_missing_proc)                (grib_accessor*);
293 typedef  int   (*accessor_pack_is_missing_proc)                (grib_accessor*);
294 typedef  int   (*accessor_pack_long_proc)                (grib_accessor*, const long*,   size_t *len);
295 typedef  int   (*accessor_pack_double_proc)              (grib_accessor*, const double*, size_t *len);
296 typedef  int   (*accessor_pack_string_proc)              (grib_accessor*, const char*,   size_t *len);
297 typedef  int   (*accessor_pack_bytes_proc)               (grib_accessor*, const unsigned char*, size_t *len);
298 typedef  int   (*accessor_pack_expression_proc)           (grib_accessor*, grib_expression*);
299 typedef  int   (*accessor_clear_proc)           (grib_accessor*);
300 
301 typedef  void  (*accessor_init_class_proc)               (grib_accessor_class*);
302 
303 typedef  int   (*accessor_compare_proc)                  (grib_accessor*, grib_accessor*);
304 typedef  size_t  (*accessor_string_proc)                 (grib_accessor*);
305 typedef  int  (*accessor_value_with_ret_proc)            (grib_accessor*,long*);
306 typedef  long  (*accessor_value_proc)                    (grib_accessor*);
307 typedef  void  (*accessor_dump_proc)                     (grib_accessor*, grib_dumper*);
308 typedef  void  (*accessor_init_proc)                     (grib_accessor*, const long len, grib_arguments*);
309 typedef  void  (*accessor_post_init_proc)                (grib_accessor*);
310 
311 typedef int (*accessor_nearest_proc) (grib_accessor*, double,double*);
312 
313 typedef  long  (*grib_binop_long_proc)                        (long,long);
314 typedef  long  (*grib_unop_long_proc)                         (long);
315 
316 typedef  double  (*grib_binop_double_proc)                        (double,double);
317 typedef  double  (*grib_unop_double_proc)                         (double);
318 
319 typedef  int  (*grib_binop_string_proc)                        (char*,char*);
320 
321 typedef struct second_order_packed second_order_packed;
322 typedef  void  grib_expression_visit_proc                (void* udata, grib_expression *e);
323 
324 
325 
326 struct grib_key_value_list {
327   const char* name;
328   int         type;
329   int         size;
330   long*       long_value;
331   double*     double_value;
332   grib_key_value_list* namespace_value;
333   char* string_value;
334   int         has_value;
335   int         error;
336   grib_key_value_list* next;
337 } ;
338 
339 
340 struct second_order_packed {
341   unsigned long nbits_per_widths;
342   unsigned long nbits_per_group_size;
343   size_t size_of_group_array;
344   size_t packed_byte_count;
345   unsigned long *array_of_group_size;
346   unsigned long *array_of_group_width;
347    long *array_of_group_refs;
348 } ;
349 
350 /**
351 *  an grib_compression
352 *  Structure supporting the packing and unpacking procedures
353 *
354 *  @see  grib_action_create_data
355 */
356 struct grib_packer {
357     const char* name;
358     grib_pack_proc   pack;     /** <  packing procedure                    */
359     grib_unpack_proc unpack;   /** < unpacking procedure                    */
360 };
361 
362 
363 /* --------------- */
364 
365 typedef struct grib_loader grib_loader;
366 typedef int (*grib_loader_init_accessor_proc)(grib_loader*,grib_accessor*,grib_arguments*);
367 typedef int (*grib_loader_lookup_long_proc) (grib_context*,grib_loader*,const char* name, long* value);
368 
369 struct grib_loader {
370   void                          *data;
371   grib_loader_init_accessor_proc init_accessor;
372   grib_loader_lookup_long_proc   lookup_long;
373   int                            list_is_resized; /** will be true if we resize a list */
374   int  changing_edition;
375 };
376 
377 /**
378 *  an action
379 *  Structure supporting the creation of accessor, resulting of a statement during a definition file parsing
380 *
381 *  @see  grib_action_class
382 */
383 struct grib_action
384 {
385     char                     *name;   /**  name of the definition statement            */
386     char                     *op;     /**  operator of the definition statement        */
387     char                     *name_space;   /**  namspace of the definition statement  */
388     grib_action              *next;   /**  next action in the list                     */
389     grib_action_class        *cclass; /**  link to the structure containing a specific behavior */
390     grib_context             *context;/**  Context                                     */
391     unsigned long            flags;
392     char                    *defaultkey; /** name of the key used as default if not found  */
393     grib_arguments*         default_value; /** default expression as in .def file */
394     char*                   set;
395     /* If you had something, don't forget to update grib_action_compile */
396 };
397 
398 typedef struct grib_accessor_list grib_accessor_list;
399 
400 struct grib_accessor_list {
401    grib_accessor* accessor;
402    grib_accessor_list* next;
403 };
404 
405 /* compile */
406 
407 typedef struct grib_compiler {
408     int   cnt;
409     int   max;
410     FILE *out;
411     const char *var;
412 } grib_compiler;
413 
414 
415 
416 typedef  int  (*action_create_accessors_handle_proc)        (grib_section* p, grib_action* a, grib_loader* h);
417 typedef  int  (*action_notify_change_proc)                   (grib_action* a, grib_accessor* observer,grib_accessor * observed);
418 
419 typedef  void  (*grib_dump_proc)                         (grib_action*, FILE*, int );
420 typedef  void  (*grib_xref_proc)                         (grib_action*, FILE*,const char*);
421 typedef  void  (*grib_compile_proc)                         (grib_action*, grib_compiler*);
422 typedef  void  (*action_init_class_proc)            (grib_action_class* a);
423 typedef  void  (*action_init_proc)                  (grib_action* a);
424 typedef  void  (*action_destroy_proc)                  (grib_context* context,  grib_action* a);
425 typedef  grib_action*  (*action_reparse_proc)              (grib_action* a,grib_accessor*,int*);
426 typedef  int  (*action_execute_proc)              (grib_action* a,grib_handle*);
427 
428 /**
429 *  an action_class
430 *  Structure supporting the specific behavior of an action
431 *
432 *  @see  grib_action
433 */
434 struct grib_action_class
435 {
436     grib_action_class          **super; /** < link to a more general behavior                         */
437     const char*                name;    /** < name of the behavior class                              */
438     size_t                     size;    /** < size in bytes of the structure                          */
439 
440   int                        inited;
441   action_init_class_proc     init_class;
442 
443     action_init_proc           init;
444     action_destroy_proc        destroy;
445                                         /** < destructor method to realease the memory    */
446 
447     grib_dump_proc             dump;    /** < dump method of the action                               */
448     grib_xref_proc             xref;    /** < dump method of the action                               */
449     action_create_accessors_handle_proc       create_accessor;
450                                         /** < method to create the corresponding accessor from a handle*/
451     action_notify_change_proc                      notify_change;
452                                         /** < method to create the corresponding accessor from a handle*/
453 
454   action_reparse_proc              reparse;
455   action_execute_proc              execute;
456 
457     grib_compile_proc             compile;    /** < compile method of the action                               */
458 };
459 
460 
461 
462 /**
463 *  a buffer
464 *  Structure containing the datas of a Grib
465 */
466 struct grib_buffer
467 {
468     int              property;   /** < property parameter of buffer         */
469     int              validity;   /** < validity parameter of buffer         */
470     int              growable;   /** < buffer can be grown         */
471     size_t           length;     /** < Buffer length                        */
472     size_t           ulength;    /** < length used of the buffer            */
473     unsigned char*   data;       /** < the data byte array                  */
474 };
475 
476 /**
477 *  an Accessor
478 *  Structure supporting each single data unit and allowing its access
479 *  @see  grib_accessor_class
480 */
481 
482 #define MAX_ACCESSOR_NAMES 20
483 
484 typedef struct grib_virtual_value grib_virtual_value;
485 
486 struct grib_virtual_value {
487   long     lval;
488   double   dval;
489   char*    cval;
490   int      missing;
491   int      length;
492   int      type;
493 };
494 
495 struct grib_accessor
496 {
497   const char             *name  ;     /** < name of the accessor                       */
498   const char*             name_space;  /** < namespace to which the accessor belongs    */
499   grib_action            *creator  ;  /** < action that created the accessor           */
500   long                   length ;     /** < byte length of the accessor                */
501   long                   offset ;     /** < offset of the data in the buffer           */
502   grib_section           *parent;     /** < section to which the accessor is attached  */
503   grib_accessor          *next  ;     /** < next accessor in list                      */
504   grib_accessor          *previous;   /** < next accessor in list                      */
505   grib_accessor_class    *cclass;     /** < behavior of the accessor                   */
506   unsigned long           flags;      /** < Various flags                              */
507   grib_section*           sub_section;
508 
509   const char*             all_names[MAX_ACCESSOR_NAMES]  ;   /** < name of the accessor  */
510   const char*             all_name_spaces[MAX_ACCESSOR_NAMES]; /** < namespace to which the accessor belongs    */
511   int                     dirty;
512 
513   grib_accessor          *same;      /** < accessors with the same name */
514   long                   loop;      /** < used in lists */
515   grib_virtual_value*    vvalue;    /** < virtual value used when transient flag on **/
516   const char*            set;
517 
518 };
519 
520 
521 #define GRIB_ACCESSOR_FLAG_READ_ONLY        (1<<1)
522 #define GRIB_ACCESSOR_FLAG_DUMP             (1<<2)
523 #define GRIB_ACCESSOR_FLAG_EDITION_SPECIFIC (1<<3)
524 #define GRIB_ACCESSOR_FLAG_CAN_BE_MISSING   (1<<4)
525 #define GRIB_ACCESSOR_FLAG_HIDDEN           (1<<5)
526 #define GRIB_ACCESSOR_FLAG_CONSTRAINT       (1<<6)
527 #define GRIB_ACCESSOR_FLAG_OVERRIDE         (1<<7)
528 #define GRIB_ACCESSOR_FLAG_NO_COPY          (1<<8)
529 #define GRIB_ACCESSOR_FLAG_COPY_OK          (1<<9)
530 #define GRIB_ACCESSOR_FLAG_FUNCTION         (1<<10)
531 #define GRIB_ACCESSOR_FLAG_DATA             (1<<11)
532 #define GRIB_ACCESSOR_FLAG_NO_FAIL          (1<<12)
533 #define GRIB_ACCESSOR_FLAG_TRANSIENT        (1<<13)
534 #define GRIB_ACCESSOR_FLAG_STRING_TYPE      (1<<14)
535 #define GRIB_ACCESSOR_FLAG_LONG_TYPE        (1<<15)
536 #define GRIB_ACCESSOR_FLAG_DOUBLE_TYPE      (1<<16)
537 #define GRIB_ACCESSOR_FLAG_LOWERCASE        (1<<17)
538 /* when adding a flag, update grib_compile_flags*/
539 
540 /**
541 *  a section accessor
542 *  Structure supporting hierarchical naming of the accessors
543 *  @see  grib_accessor
544 */
545 struct grib_section
546 {
547   grib_accessor                *owner;
548   grib_handle                  *h;         /** < Handles of all accessors and buffer  */
549   grib_accessor                *aclength;  /** < block of the length of the block     */
550   grib_block_of_accessors      *block;     /** < block                                */
551   grib_action                  *branch;    /** < branch that created the bolck        */
552   size_t                       length;
553   size_t                       padding;
554 };
555 
556 
557 
558 struct grib_iterator_class{
559    grib_iterator_class**           super;
560    char* name;
561    size_t                         size;
562 
563    int                            inited;
564    iterator_init_class_proc       init_class;
565 
566    iterator_init_proc             init;
567    iterator_destroy_proc        destroy;
568 
569    iterator_next_proc        next;
570    iterator_previous_proc    previous;
571    iterator_reset_proc       reset;
572    iterator_has_next_proc    has_next;
573 
574 };
575 
576 struct grib_nearest_class{
577    grib_nearest_class**           super;
578    char* name;
579    size_t                         size;
580 
581    int                            inited;
582    nearest_init_class_proc       init_class;
583 
584    nearest_init_proc             init;
585    nearest_destroy_proc          destroy;
586 
587    nearest_find_proc             find;
588 
589 };
590 
591 struct grib_box_class{
592    grib_box_class**           super;
593    char* name;
594    size_t                     size;
595    int                        inited;
596    box_init_class_proc        init_class;
597    box_init_proc              init;
598    box_destroy_proc           destroy;
599    box_get_points_proc        get_points;
600 
601 };
602 
603 /* --------------- */
604 /* --------------- */
605 typedef void (*search_all_callback_proc)(grib_accessor*,void* data);
606 /* --------------- */
607 
608 
609 
610 typedef int  (*dumper_init_proc)        (grib_dumper*);
611 typedef void (*dumper_dump_proc)        (grib_dumper*,grib_accessor*,const char* comment);
612 typedef void (*dumper_dump_section_proc)(grib_dumper*,grib_accessor*,grib_block_of_accessors* block);
613 typedef void (*dumper_dump_values_proc) (grib_dumper*,grib_accessor*);
614 typedef int  (*dumper_destroy_proc)     (grib_dumper*);
615 typedef void (*dumper_header_proc)      (grib_dumper*,grib_handle*);
616 typedef void (*dumper_footer_proc)      (grib_dumper*,grib_handle*);
617 typedef void (*dumper_init_class_proc)  (grib_dumper_class*);
618 
619 struct grib_dumper {
620   FILE*            out;
621    unsigned long     option_flags;
622    void*             arg;
623    int               depth;
624    grib_handle       *handle;
625    grib_dumper_class *cclass;
626  };
627 
628 struct grib_dumper_class {
629    grib_dumper_class**    super;
630    char*                    name;
631    size_t                   size;
632    int                      inited;
633    dumper_init_class_proc   init_class;
634    dumper_init_proc         init;
635    dumper_destroy_proc      destroy;
636    dumper_dump_proc         dump_long;
637    dumper_dump_proc         dump_double;
638    dumper_dump_proc         dump_string;
639    dumper_dump_proc         dump_label;
640    dumper_dump_proc         dump_bytes;
641    dumper_dump_proc         dump_bits;
642    dumper_dump_section_proc dump_section;
643    dumper_dump_values_proc  dump_values;
644    dumper_header_proc       header;
645    dumper_footer_proc       footer;
646 };
647 
648 struct grib_iterator{
649    grib_arguments  *args;                   /**  args of iterator   */
650    grib_handle* h;
651    long        e;                           /**  current element    */
652    size_t     nv;                           /**  number of values   */
653    double*  data;                           /**  data values        */
654    grib_iterator_class* cclass;
655    unsigned long flags;
656 };
657 
658 struct grib_nearest{
659    grib_arguments              *args;      /**  args of iterator   */
660    grib_handle*                h;
661    grib_context*               context;
662    double*                     values;
663    size_t                      values_count;
664    grib_nearest_class*         cclass;
665    unsigned long               flags;
666 
667 };
668 
669 struct grib_box {
670    grib_box_class*             cclass;
671    grib_context*               context;
672    grib_arguments              *args;
673    grib_handle*                h;
674    unsigned long               flags;
675    grib_points*                points;
676 };
677 
678 
679 struct grib_dependency {
680    grib_dependency* next;
681    grib_accessor*   observed;
682    grib_accessor*   observer;
683    int              run;
684 };
685 
686 
687 struct grib_block_of_accessors
688 {
689     grib_accessor*    first;
690     grib_accessor*    last ;
691 };
692 
693 
694 typedef struct grib_trie grib_trie;
695 typedef struct grib_itrie grib_itrie;
696 
697 
698 struct grib_darray {
699   double* v;
700   size_t size;
701   size_t n;
702   size_t incsize;
703 } ;
704 
705 struct grib_iarray {
706   long* v;
707   size_t size;
708   size_t n;
709   size_t incsize;
710 } ;
711 
712 
713 #define MAX_SET_VALUES      10
714 #define MAX_ACCESSOR_CACHE  100
715 
716 
717 
718 struct grib_handle
719 {
720     grib_context*           context;       /** < context attached to this handle    */
721     grib_buffer*            buffer ;       /** < buffer attached to the handle      */
722     grib_section*           root;          /**  the root      section*/
723     grib_section*           asserts;       /** the assertion section*/
724     grib_section*           rules;         /** the rules     section*/
725     grib_dependency*        dependencies;  /** List of dependencies */
726     grib_handle*            main;           /** Used during reparsing */
727     grib_handle*            kid;           /** Used during reparsing */
728     grib_loader*            loader;        /** Used during reparsing */
729     int                     values_stack;
730     const grib_values*      values[MAX_SET_VALUES];       /** Used when setting multiple values at once */
731     size_t                  values_count[MAX_SET_VALUES];  /** Used when setting multiple values at once */
732     int                     dont_trigger;  /** Don't notify triggers */
733     int                     partial;       /** Not a complete message (just headers) */
734     int                     header_mode;   /** Header not jet complete */
735     char* gts_header;
736     size_t gts_header_len;
737     int  use_trie;
738     int  trie_invalid;
739     grib_accessor* accessors[ACCESSORS_ARRAY_SIZE];
740     char* section_offset[MAX_NUM_SECTIONS];
741     char* section_length[MAX_NUM_SECTIONS];
742     int sections_count;
743     off_t offset;
744 };
745 
746 struct grib_multi_handle {
747   grib_context*           context;       /** < context attached to this handle    */
748   grib_buffer*            buffer ;       /** < buffer attached to the handle      */
749   size_t                  offset ;
750   size_t                  length ;
751 };
752 
753 
754 struct grib_accessor_class
755 {
756     grib_accessor_class             **super;
757     const char*                     name;
758     size_t                          size;
759 
760     int                             inited;
761     accessor_init_class_proc        init_class;
762 
763     accessor_init_proc              init;
764     accessor_post_init_proc         post_init;
765     accessor_destroy_proc           destroy;
766 
767     accessor_dump_proc              dump;
768     accessor_value_proc             next_offset;
769 
770     accessor_string_proc            string_length;
771     accessor_value_with_ret_proc    value_count;
772 
773     accessor_value_proc             byte_count;
774     accessor_value_proc             byte_offset;
775 
776     accessor_get_native_type_proc   get_native_type;
777 
778     accessor_sub_section_proc       sub_section;
779 
780     accessor_pack_missing_proc      pack_missing  ;
781     accessor_pack_is_missing_proc   is_missing  ;
782 
783     accessor_pack_long_proc         pack_long  ;
784     accessor_unpack_long_proc       unpack_long  ;
785 
786     accessor_pack_double_proc       pack_double;
787     accessor_unpack_double_proc     unpack_double;
788 
789     accessor_pack_string_proc       pack_string;
790     accessor_unpack_string_proc     unpack_string;
791 
792     accessor_pack_bytes_proc        pack_bytes;
793     accessor_unpack_bytes_proc      unpack_bytes;
794 
795     accessor_pack_expression_proc   pack_expression;
796 
797     accessor_notify_change_proc     notify_change;
798     accessor_update_size_proc       update_size;
799 
800     accessor_preferred_size_proc    preferred_size;
801     accessor_resize_proc            resize;
802 
803     accessor_nearest_proc           nearest_smaller_value;
804     accessor_next_proc              next;
805     accessor_compare_proc           compare;
806     accessor_unpack_double_element_proc     unpack_double_element;
807     accessor_unpack_double_subarray_proc    unpack_double_subarray;
808     accessor_clear_proc             clear;
809 };
810 
811 typedef struct grib_multi_support grib_multi_support;
812 
813 struct grib_multi_support {
814     FILE*                           file;
815     size_t                          offset;
816     unsigned char*                  message;
817     size_t                          message_length;
818     unsigned char*                  sections[8];
819     unsigned char*                  bitmap_section;
820     size_t                          bitmap_section_length;
821     size_t                          sections_length[9];
822     int                             section_number;
823     grib_multi_support*             next;
824 };
825 
826 /* Concepts */
827 typedef struct grib_concept_condition grib_concept_condition;
828 
829 struct grib_concept_condition {
830   grib_concept_condition* next;
831   char*               name;
832   grib_expression*    expression;
833 };
834 
835 typedef struct grib_concept_value_name grib_concept_value_name;
836 struct grib_concept_value_name {
837   grib_concept_value_name*          next;
838   char*               name;
839 } ;
840 
841 typedef struct grib_concept_value grib_concept_value;
842 
843 struct grib_concept_value {
844   grib_concept_value*          next;
845   char*                        name;
846   grib_concept_condition*      conditions;
847   grib_trie*                   index;
848 };
849 
850 /* ----------*/
851 
852 struct grib_context
853 {
854     int                             inited;
855     int                             debug;
856     int                             write_on_fail;
857     int                             no_abort;
858     int                             io_buffer_size;
859     int                             no_big_group_split;
860     int                             no_spd;
861     int                             keep_matrix;
862     char*                           grib_definition_files_path;
863     char*                           grib_samples_path;
864     char*                           grib_concept_path;
865 
866     grib_action_file_list*          grib_reader;
867     void*                           user_data;
868     int                             real_mode;
869 
870     grib_free_proc                  free_mem;
871     grib_malloc_proc                alloc_mem;
872     grib_realloc_proc               realloc_mem;
873 
874     grib_free_proc                  free_persistent_mem;
875     grib_malloc_proc                alloc_persistent_mem;
876 
877     grib_free_proc                  free_buffer_mem;
878     grib_malloc_proc                alloc_buffer_mem;
879     grib_realloc_proc               realloc_buffer_mem;
880 
881     grib_data_read_proc             read;
882     grib_data_write_proc            write;
883     grib_data_tell_proc             tell;
884     grib_data_seek_proc             seek;
885     grib_data_eof_proc              eof;
886 
887     grib_log_proc                   output_log;
888     grib_print_proc                 print;
889 
890     grib_codetable*                 codetable;
891     char*                           outfilename;
892     int                             multi_support_on;
893     grib_multi_support*             multi_support;
894     grib_string_list*               grib_definition_files_dir;
895     int                             handle_file_count;
896     int                             handle_total_count;
897     off_t                           message_file_offset;
898     int                             no_fail_on_wrong_length;
899     int                             gts_header_on;
900     int                             gribex_mode_on;
901     int                             large_constant_fields;
902     grib_itrie*                     keys;
903     int                             keys_count;
904     grib_itrie*                     concepts_index;
905     int                             concepts_count;
906     grib_concept_value*             concepts[MAX_NUM_CONCEPTS];
907     grib_trie*                      def_files;
908 
909     grib_string_list*                blacklist;
910     int                             ieee_packing;
911     FILE*                           log_stream;
912     grib_trie*                      classes;
913 #if GRIB_PTHREADS
914     pthread_mutex_t                 mutex;
915 #elif GRIB_OMP_THREADS
916     omp_nest_lock_t                 mutex;
917 #endif
918 
919 };
920 
921 /* file_pool */
922 extern grib_string_list grib_file_not_found;
923 
924 typedef struct grib_file grib_file;
925 typedef struct grib_file_pool grib_file_pool;
926 
927 struct grib_file {
928   grib_context* context;
929   char* name;
930   FILE* handle;
931   char* mode;
932   char* buffer;
933   long refcount;
934   grib_file* next;
935   short id;
936 } ;
937 
938 struct grib_file_pool {
939   grib_context* context;
940   grib_file* first;
941   grib_file* current;
942   size_t size;
943   int number_of_opened_files;
944   int max_opened_files;
945 };
946 
947 /* fieldset */
948 typedef struct grib_field grib_field;
949 typedef struct grib_column grib_column;
950 typedef struct grib_fields grib_fields;
951 typedef struct grib_int_array grib_int_array;
952 
953 struct grib_where {
954   grib_context* context;
955   char* string;
956 };
957 
958 struct grib_column {
959   grib_context* context;
960   int refcount;
961   char* name;
962   int type;
963   size_t size;
964   size_t values_array_size;
965   long* long_values;
966   double* double_values;
967   char** string_values;
968   int* errors;
969 } ;
970 
971 struct grib_order_by {
972   char* key;
973   int idkey;
974   int mode;
975   grib_order_by* next;
976 } ;
977 
978 #ifdef NEWDB
979 struct grib_query {
980   grib_context* context;
981   char* where_string;
982   grib_order_by* order_by;
983 };
984 #endif
985 
986 struct grib_field {
987   grib_file* file;
988   off_t offset;
989   long length;
990   grib_field* next;
991 };
992 
993 struct grib_int_array {
994   grib_context* context;
995   size_t size;
996   int* el;
997 } ;
998 
999 #ifndef NEWDB
1000 struct grib_fieldset {
1001   grib_context* context;
1002   grib_int_array* filter;
1003   grib_int_array* order;
1004   size_t fields_array_size;
1005   size_t size;
1006   grib_column* columns;
1007   size_t columns_size;
1008   grib_where* where;
1009   grib_order_by* order_by;
1010   long current;
1011   grib_field** fields;
1012 };
1013 #endif
1014 
1015 #ifdef NEWDB
1016 /* grib db */
1017 struct grib_db {
1018   grib_context* context;
1019   size_t size;
1020   size_t fields_array_size;
1021   grib_column* columns;
1022   size_t columns_size;
1023   grib_field** fields;
1024 };
1025 
1026 struct grib_fieldset {
1027   grib_context* context;
1028   grib_db* db;
1029   grib_int_array* filter;
1030   grib_int_array* order;
1031   size_t size;
1032   grib_query* query;
1033   long current;
1034 };
1035 #endif
1036 
1037 /* concept index structures */
1038 
1039 typedef struct grib_concept_index_key grib_concept_index_key;
1040 typedef struct grib_concept_index grib_concept_index;
1041 typedef struct grib_conditions_tree grib_conditions_tree;
1042 typedef struct grib_concept_entry grib_concept_entry;
1043 typedef struct grib_concept_key grib_concept_key;
1044 
1045 struct grib_concept_index_entry {
1046   char* name;
1047   char* value;
1048   int type;
1049   grib_concept_entry* next;
1050 };
1051 
1052 struct grib_concept_index_key {
1053   char* name;
1054   int type;
1055   grib_concept_key* next;
1056 };
1057 
1058 struct grib_concept_index {
1059    grib_context* context;
1060    grib_concept_key* keys;
1061    grib_conditions_tree* conditions;
1062 };
1063 
1064 struct grib_conditions_tree {
1065    char*   value;
1066    void*   object;
1067    grib_conditions_tree* next;
1068    grib_conditions_tree* next_key;
1069 };
1070 
1071 /* index structures */
1072 
1073 #define STRING_VALUE_LEN 100
1074 
1075 typedef struct grib_field_tree grib_field_tree;
1076 
1077 struct grib_field_tree {
1078   grib_field* field;
1079   char* value;
1080   grib_field_tree* next;
1081   grib_field_tree* next_level;
1082 };
1083 
1084 typedef struct grib_index_key grib_index_key;
1085 
1086 struct grib_index_key {
1087   char* name;
1088   int type;
1089   char value[STRING_VALUE_LEN];
1090   grib_string_list* values;
1091   grib_string_list* current;
1092   int values_count;
1093   int count;
1094   grib_index_key* next;
1095 };
1096 
1097 typedef struct grib_field_list grib_field_list;
1098 struct grib_field_list {
1099   grib_field* field;
1100   grib_field_list* next;
1101 };
1102 
1103 
1104 struct grib_index {
1105   grib_context* context;
1106   grib_index_key* keys;
1107   int rewind;
1108   int orderby;
1109   grib_index_key* orederby_keys;
1110   grib_field_tree* fields;
1111   grib_field_list* fieldset;
1112   grib_field_list* current;
1113   grib_file* files;
1114   int count;
1115 };
1116 
1117 /* header compute */
1118 typedef struct grib_math grib_math;
1119 
1120 struct grib_math{
1121   struct grib_math *left;
1122   struct grib_math *right;
1123   char        *name;
1124   int         arity;
1125 };
1126 
1127 typedef double (*mathproc)(void);
1128 typedef int    (*funcproc)(grib_math*,mathproc);
1129 
1130 typedef struct func {
1131   char    *name;
1132   funcproc addr;
1133   mathproc proc;
1134   int      arity;
1135   char     *info;
1136 } func;
1137 
1138 /* action file */
1139 struct grib_action_file
1140 {
1141     char*             filename   ;
1142     grib_action*      root       ;
1143     grib_action_file* next       ;
1144 };
1145 
1146 struct grib_action_file_list
1147 {
1148     grib_action_file * first;
1149     grib_action_file * last ;
1150 };
1151 
1152 #include "grib_expression.h"
1153 
1154 /* ----------*/
1155 /* md5 */
1156 typedef unsigned long cvs_uint32;
1157 
1158 struct cvs_MD5Context {
1159   cvs_uint32 buf[4];
1160   cvs_uint32 bits[2];
1161   unsigned char in[64];
1162 };
1163 /* --- */
1164 
1165 typedef struct grib_rule_entry grib_rule_entry;
1166 
1167 struct grib_rule_entry {
1168   grib_rule_entry *next;
1169   char            *name;
1170   grib_expression *value;
1171 };
1172 
1173 typedef struct grib_rule grib_rule;
1174 
1175 struct grib_rule {
1176   grib_rule        *next;
1177   grib_expression  *condition;
1178   grib_rule_entry  *entries;
1179 };
1180 
1181 typedef struct grib_case grib_case;
1182 
1183 struct grib_case {
1184    grib_arguments* values;
1185    grib_action* action;
1186    grib_case* next;
1187 };
1188 
1189 /* ----------*/
1190 
1191 typedef struct code_table_entry {
1192   char* abbreviation;
1193   char* title;
1194   char* units;
1195 } code_table_entry;
1196 
1197 struct grib_codetable {
1198   char*            filename[2];
1199   char*            recomposed_name[2];
1200   grib_codetable*  next;
1201   size_t           size;
1202   code_table_entry entries[1];
1203 };
1204 
1205 #if GRIB_TIMER
1206 typedef struct grib_timer {
1207 
1208     struct timeval start_;
1209     double timer_;
1210     int   active_;
1211     char   *name_;
1212     int    count_;
1213     long total_;
1214 
1215     int elapsed_;
1216     double  cpu_;
1217     double  total_cpu_;
1218 
1219     char   *statname_;
1220     grib_context* context;
1221 
1222     struct grib_timer *next_;
1223 } grib_timer;
1224 #else
1225 typedef struct grib_timer {
1226     char nothing;
1227 } grib_timer;
1228 #endif
1229 
1230 typedef struct j2k_encode_helper {
1231 
1232   size_t           buffer_size;
1233 
1234   long             width;
1235   long             height;
1236   long             bits_per_value;
1237 
1238   float            compression;
1239 
1240   long             no_values;
1241   const double    *values;
1242   double           reference_value;
1243   double           divisor;
1244   double           decimal;
1245 
1246   long            jpeg_length;
1247   unsigned char*  jpeg_buffer;
1248 
1249 } j2k_encode_helper;
1250 
1251 
1252 #include "grib_api_prototypes.h"
1253 
1254 
1255 #ifdef __cplusplus
1256 }
1257 #endif
1258 #endif
1259 /* This part is automatically generated by ./errors.pl, do not edit */
1260 #ifndef grib_errors_internal_H
1261 #define grib_errors_internal_H
1262 /** Value mismatch */
1263 #define GRIB_VALUE_MISMATCH		1
1264 /** double values are different */
1265 #define GRIB_DOUBLE_VALUE_MISMATCH		2
1266 /** long values are different */
1267 #define GRIB_LONG_VALUE_MISMATCH		3
1268 /** byte values are different */
1269 #define GRIB_BYTE_VALUE_MISMATCH		4
1270 /** string values are different */
1271 #define GRIB_STRING_VALUE_MISMATCH		5
1272 /** Offset mismatch */
1273 #define GRIB_OFFSET_MISMATCH		6
1274 /** Count mismatch */
1275 #define GRIB_COUNT_MISMATCH		7
1276 /** Name mismatch */
1277 #define GRIB_NAME_MISMATCH		8
1278 /** Type mismatch */
1279 #define GRIB_TYPE_MISMATCH		9
1280 /** Type and value mismatch */
1281 #define GRIB_TYPE_AND_VALUE_MISMATCH		10
1282 /** Unable to compare accessors */
1283 #define GRIB_UNABLE_TO_COMPARE_ACCESSORS		11
1284 /** Unable to reset iterator */
1285 #define GRIB_UNABLE_TO_RESET_ITERATOR		12
1286 /** Assertion failure */
1287 #define GRIB_ASSERTION_FAILURE		13
1288 #endif
1289