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 #include "grib_api_internal.h"
12 /*
13    This is used by make_class.pl
14 
15    START_CLASS_DEF
16    CLASS      = accessor
17    SUPER      = grib_accessor_class_bitmap
18    IMPLEMENTS = pack_double
19    IMPLEMENTS = value_count
20    IMPLEMENTS = init;
21    MEMBERS=const char* numberOfValues
22    END_CLASS_DEF
23 
24  */
25 
26 /* START_CLASS_IMP */
27 
28 /*
29 
30 Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
31 Instead edit values between START_CLASS_DEF and END_CLASS_DEF
32 or edit "accessor.class" and rerun ./make_class.pl
33 
34 */
35 
36 static int pack_double(grib_accessor*, const double* val,size_t *len);
37 static int value_count(grib_accessor*,long*);
38 static void init(grib_accessor*,const long, grib_arguments* );
39 static void init_class(grib_accessor_class*);
40 
41 typedef struct grib_accessor_g2bitmap {
42     grib_accessor          att;
43 /* Members defined in gen */
44 /* Members defined in bytes */
45 /* Members defined in bitmap */
46 	const char* tableReference;
47 	const char* missing_value;
48 	const char* offsetbsec;
49 	const char* sLength;
50 /* Members defined in g2bitmap */
51 	const char* numberOfValues;
52 } grib_accessor_g2bitmap;
53 
54 extern grib_accessor_class* grib_accessor_class_bitmap;
55 
56 static grib_accessor_class _grib_accessor_class_g2bitmap = {
57     &grib_accessor_class_bitmap,                      /* super                     */
58     "g2bitmap",                      /* name                      */
59     sizeof(grib_accessor_g2bitmap),  /* size                      */
60     0,                           /* inited */
61     &init_class,                 /* init_class */
62     &init,                       /* init                      */
63     0,                  /* post_init                      */
64     0,                    /* free mem                       */
65     0,                       /* describes himself         */
66     0,                /* get length of section     */
67     0,              /* get length of string      */
68     &value_count,                /* get number of values      */
69     0,                 /* get number of bytes      */
70     0,                /* get offset to bytes           */
71     0,            /* get native type               */
72     0,                /* get sub_section                */
73     0,               /* grib_pack procedures long      */
74     0,               /* grib_pack procedures long      */
75     0,                  /* grib_pack procedures long      */
76     0,                /* grib_unpack procedures long    */
77     &pack_double,                /* grib_pack procedures double    */
78     0,              /* grib_unpack procedures double  */
79     0,                /* grib_pack procedures string    */
80     0,              /* grib_unpack procedures string  */
81     0,                 /* grib_pack procedures bytes     */
82     0,               /* grib_unpack procedures bytes   */
83     0,            /* pack_expression */
84     0,              /* notify_change   */
85     0,                /* update_size   */
86     0,            /* preferred_size   */
87     0,                    /* resize   */
88     0,      /* nearest_smaller_value */
89     0,                       /* next accessor    */
90     0,                    /* compare vs. another accessor   */
91     0,     /* unpack only ith value          */
92     0,     /* unpack a subarray         */
93     0,             		/* clear          */
94 };
95 
96 
97 grib_accessor_class* grib_accessor_class_g2bitmap = &_grib_accessor_class_g2bitmap;
98 
99 
init_class(grib_accessor_class * c)100 static void init_class(grib_accessor_class* c)
101 {
102 	c->dump	=	(*(c->super))->dump;
103 	c->next_offset	=	(*(c->super))->next_offset;
104 	c->string_length	=	(*(c->super))->string_length;
105 	c->byte_count	=	(*(c->super))->byte_count;
106 	c->byte_offset	=	(*(c->super))->byte_offset;
107 	c->get_native_type	=	(*(c->super))->get_native_type;
108 	c->sub_section	=	(*(c->super))->sub_section;
109 	c->pack_missing	=	(*(c->super))->pack_missing;
110 	c->is_missing	=	(*(c->super))->is_missing;
111 	c->pack_long	=	(*(c->super))->pack_long;
112 	c->unpack_long	=	(*(c->super))->unpack_long;
113 	c->unpack_double	=	(*(c->super))->unpack_double;
114 	c->pack_string	=	(*(c->super))->pack_string;
115 	c->unpack_string	=	(*(c->super))->unpack_string;
116 	c->pack_bytes	=	(*(c->super))->pack_bytes;
117 	c->unpack_bytes	=	(*(c->super))->unpack_bytes;
118 	c->pack_expression	=	(*(c->super))->pack_expression;
119 	c->notify_change	=	(*(c->super))->notify_change;
120 	c->update_size	=	(*(c->super))->update_size;
121 	c->preferred_size	=	(*(c->super))->preferred_size;
122 	c->resize	=	(*(c->super))->resize;
123 	c->nearest_smaller_value	=	(*(c->super))->nearest_smaller_value;
124 	c->next	=	(*(c->super))->next;
125 	c->compare	=	(*(c->super))->compare;
126 	c->unpack_double_element	=	(*(c->super))->unpack_double_element;
127 	c->unpack_double_subarray	=	(*(c->super))->unpack_double_subarray;
128 	c->clear	=	(*(c->super))->clear;
129 }
130 
131 /* END_CLASS_IMP */
132 
133 
134 
init(grib_accessor * a,const long len,grib_arguments * arg)135 static void init(grib_accessor* a, const long len , grib_arguments* arg )
136 {
137 
138 	grib_accessor_g2bitmap* self = (grib_accessor_g2bitmap*)a;
139 
140 	self->numberOfValues     = grib_arguments_get_name(a->parent->h,arg,4);
141 }
142 
143 
grib_set_bit_on(unsigned char * p,long * bitp)144 static void grib_set_bit_on( unsigned char* p, long *bitp){
145 	unsigned char o =  1;
146 	p += (*bitp >> 3);
147 	o <<= 7-((*bitp)%8);
148 	*p |= o;
149 	(*bitp)+=1;
150 }
151 
pack_double(grib_accessor * a,const double * val,size_t * len)152 static int pack_double(grib_accessor* a, const double* val,size_t *len){
153 	grib_accessor_g2bitmap* self = (grib_accessor_g2bitmap*)a;
154 
155 	size_t tlen;
156 
157 	unsigned char* buf = NULL;
158 	size_t i;
159 	int err = 0;
160 	long pos = 0;
161 	long bmaplen = 0;
162 	double miss_values = 0;
163 	tlen = (*len+7)/8;
164 
165 	if((err = grib_get_double_internal(a->parent->h, self->missing_value, &miss_values))
166        != GRIB_SUCCESS)
167 		return err;
168 
169 	buf = (unsigned char*)grib_context_malloc_clear(a->parent->h->context,tlen);
170 	if(!buf) return GRIB_OUT_OF_MEMORY;
171 	pos=0;
172 	for(i=0;i<*len;i++)
173 	{
174 		if (val[i] == miss_values)
175 			pos++;
176 		else{
177 			bmaplen++;
178 			grib_set_bit_on(buf, &pos);
179 		}
180 	}
181 
182 	if((err = grib_set_long_internal(a->parent->h, self->numberOfValues,*len )) != GRIB_SUCCESS) {
183 		grib_context_free(a->parent->h->context,buf);
184 		return err;
185 	}
186 
187 	grib_buffer_replace(a, buf, tlen,1,1);
188 
189 	grib_context_free(a->parent->h->context,buf);
190 
191 	return GRIB_SUCCESS;
192 }
193 
194 
value_count(grib_accessor * a,long * tlen)195 static int value_count(grib_accessor* a,long* tlen)
196 {
197 	grib_accessor_g2bitmap* self = (grib_accessor_g2bitmap*)a;
198 	int err;
199 	*tlen=0;
200 
201 	err=grib_get_long_internal(a->parent->h, self->numberOfValues, tlen);
202 	return err;
203 }
204 
205