constr_SET_OF_jer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
8 
9 /*
10  * Return a standardized complex structure.
11  */
12 #undef RETURN
13 #define RETURN(_code) \
14  do { \
15  rval.code = _code; \
16  rval.consumed = consumed_myself; \
17  return rval; \
18  } while(0)
19 
20 #undef JER_ADVANCE
21 #define JER_ADVANCE(num_bytes) \
22  do { \
23  size_t num = num_bytes; \
24  buf_ptr = ((const char *)buf_ptr) + num; \
25  size -= num; \
26  consumed_myself += num; \
27  } while(0)
28 
29 /*
30  * Decode the JER (JSON) data.
31  */
33 SET_OF_decode_jer(const asn_codec_ctx_t *opt_codec_ctx,
34  const asn_TYPE_descriptor_t *td,
35  const asn_jer_constraints_t *constraints,
36  void **struct_ptr, const void *buf_ptr, size_t size) {
37  /*
38  * Bring closer parts of structure description.
39  */
40  const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
41  const asn_TYPE_member_t *element = td->elements;
42 
43  /*
44  * ... and parts of the structure being constructed.
45  */
46  void *st = *struct_ptr; /* Target structure. */
47  asn_struct_ctx_t *ctx; /* Decoder context */
48 
49  asn_dec_rval_t rval = {RC_OK, 0}; /* Return value from a decoder */
50  ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
51 
52  /*
53  * Create the target structure if it is not present already.
54  */
55  if(st == 0) {
56  st = *struct_ptr = CALLOC(1, specs->struct_size);
57  if(st == 0) RETURN(RC_FAIL);
58  }
59 
60  /*
61  * Restore parsing context.
62  */
63  ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
64 
65  /*
66  * Phases of JER/JSON processing:
67  * Phase 0: Check that the opening tag matches our expectations.
68  * Phase 1: Processing body and reacting on closing token.
69  * Phase 2: Processing inner type.
70  */
71  for(; ctx->phase <= 2;) {
72  pjer_chunk_type_e ch_type; /* JER chunk type */
73  ssize_t ch_size; /* Chunk size */
74  jer_check_sym_e scv; /* Tag check value */
75 
76  /*
77  * Go inside the inner member of a set.
78  */
79  if(ctx->phase == 2) {
80  asn_dec_rval_t tmprval = {RC_OK, 0};
81 
82  /* Invoke the inner type decoder, m.b. multiple times */
83  ASN_DEBUG("JER/SET OF element [%s]",
84  (*element->name) ? element->name : element->type->xml_tag);
85  tmprval = element->type->op->jer_decoder(opt_codec_ctx,
86  element->type,
87  element->encoding_constraints.jer_constraints,
88  &ctx->ptr,
89  buf_ptr, size);
90  if(tmprval.code == RC_OK) {
91  asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
92  if(ASN_SET_ADD(list, ctx->ptr) != 0)
93  RETURN(RC_FAIL);
94  ctx->ptr = 0;
95  JER_ADVANCE(tmprval.consumed);
96  } else {
97  JER_ADVANCE(tmprval.consumed);
98  RETURN(tmprval.code);
99  }
100 
101  ctx->phase = 1; /* Back to body processing */
102  ASN_DEBUG("JER/SET OF phase => %d", ctx->phase);
103  /* Fall through */
104  }
105 
106  /*
107  * Get the next part of the JSON stream.
108  */
109  ch_size = jer_next_token(&ctx->context,
110  buf_ptr, size, &ch_type);
111  if(ch_size == -1) {
112  RETURN(RC_FAIL);
113  } else {
114  switch(ch_type) {
115  case PJER_WMORE:
116  RETURN(RC_WMORE);
117  case PJER_TEXT:
118  JER_ADVANCE(ch_size);
119  continue;
120 
121  case PJER_DLM:
122  case PJER_VALUE:
123  case PJER_KEY:
124  break; /* Check the rest down there */
125  }
126  }
127 
128  scv = jer_check_sym(buf_ptr, ch_size, NULL);
129  ASN_DEBUG("JER/SET OF: scv = %d, ph=%d t=%s",
130  scv, ctx->phase, td->name);
131  switch(scv) {
132  case JCK_AEND:
133  if(ctx->phase == 0) break;
134  ctx->phase = 0;
135 
136  if(ctx->phase == 0) {
137  /* No more things to decode */
138  JER_ADVANCE(ch_size);
139  ctx->phase = 3; /* Phase out */
140  RETURN(RC_OK);
141  }
142  /* Fall through */
143  case JCK_OEND:
144  case JCK_KEY:
145  case JCK_COMMA:
146  case JCK_ASTART:
147  if(ctx->phase == 0) {
148  JER_ADVANCE(ch_size);
149  ctx->phase = 1; /* Processing body phase */
150  continue;
151  }
152  /* Fall through */
153  case JCK_UNKNOWN:
154  case JCK_OSTART:
155  ASN_DEBUG("JER/SET OF: scv=%d, ph=%d", scv, ctx->phase);
156  if(ctx->phase == 1) {
157  /*
158  * Process a single possible member.
159  */
160  ctx->phase = 2;
161  continue;
162  }
163  /* Fall through */
164  default:
165  break;
166  }
167 
168  ASN_DEBUG("Unexpected JSON key in SET OF");
169  break;
170  }
171 
172  ctx->phase = 3; /* "Phase out" on hard failure */
173  RETURN(RC_FAIL);
174 }
175 
178  const asn_jer_constraints_t* constraints, const void *sptr,
179  int ilevel, enum jer_encoder_flags_e flags,
180  asn_app_consume_bytes_f *cb, void *app_key) {
181  asn_enc_rval_t er = {0,0,0};
182  const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
183  const asn_TYPE_member_t *elm = td->elements;
184  const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
185  int jmin = (flags & JER_F_MINIFIED);
186  int i;
187 
188  if(!sptr) ASN__ENCODE_FAILED;
189 
190  er.encoded = 0;
191  ASN__CALLBACK("[", 1);
192 
193  for(i = 0; i < list->count; i++) {
194  asn_enc_rval_t tmper = {0,0,0};
195 
196  void *memb_ptr = list->array[i];
197  if(!memb_ptr) continue;
198 
199  if(!jmin) ASN__TEXT_INDENT(1, ilevel + 1);
200  tmper = elm->type->op->jer_encoder(elm->type,
201  elm->encoding_constraints.jer_constraints,
202  memb_ptr,
203  ilevel + (specs->as_XMLValueList != 2),
204  flags, cb, app_key);
205  if(tmper.encoded == -1) return tmper;
206  er.encoded += tmper.encoded;
207  if(tmper.encoded == 0 && specs->as_XMLValueList) {
208  const char *name = elm->type->xml_tag;
209  size_t len = strlen(name);
210  ASN__CALLBACK3("\"", 1, name, len, "\"", 1);
211  }
212  if (i != list->count - 1) {
213  ASN__CALLBACK(",", 1);
214  }
215  }
216 
217  if(!jmin) ASN__TEXT_INDENT(1, ilevel);
218  ASN__CALLBACK("]", 1);
219 
220  goto cleanup;
221 cb_failed:
223 cleanup:
224  ASN__ENCODED_OK(er);
225 }
asn_struct_ctx_s
Definition: constr_TYPE.h:29
RETURN
#define RETURN(_code)
Definition: constr_SET_OF_jer.c:13
PJER_TEXT
@ PJER_TEXT
Definition: jer_decoder.h:68
JCK_COMMA
@ JCK_COMMA
Definition: jer_decoder.h:84
ASN__ENCODED_OK
#define ASN__ENCODED_OK(rval)
Definition: asn_codecs.h:67
asn_jer_constraints_s
Definition: jer_support.h:18
asn_enc_rval_s
Definition: asn_codecs.h:41
jer_encoder_flags_e
jer_encoder_flags_e
Definition: jer_encoder.h:20
RC_WMORE
@ RC_WMORE
Definition: asn_codecs.h:83
CALLOC
#define CALLOC(nmemb, size)
Definition: asn_internal.h:37
JER_F_MINIFIED
@ JER_F_MINIFIED
Definition: jer_encoder.h:23
SET_OF_decode_jer
asn_dec_rval_t SET_OF_decode_jer(const asn_codec_ctx_t *opt_codec_ctx, const asn_TYPE_descriptor_t *td, const asn_jer_constraints_t *constraints, void **struct_ptr, const void *buf_ptr, size_t size)
Definition: constr_SET_OF_jer.c:33
ASN__CALLBACK
#define ASN__CALLBACK(buf, size)
Definition: asn_internal.h:108
ASN__CALLBACK3
#define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3)
Definition: asn_internal.h:112
JCK_OEND
@ JCK_OEND
Definition: jer_decoder.h:86
pjer_chunk_type_e
enum pjer_chunk_type pjer_chunk_type_e
JER_ADVANCE
#define JER_ADVANCE(num_bytes)
Definition: constr_SET_OF_jer.c:21
JCK_UNKNOWN
@ JCK_UNKNOWN
Definition: jer_decoder.h:82
ASN__ENCODE_FAILED
#define ASN__ENCODE_FAILED
Definition: asn_codecs.h:59
asn_TYPE_descriptor_s
Definition: constr_TYPE.h:224
ASN_SET_ADD
#define ASN_SET_ADD(headptr, ptr)
Definition: asn_SET_OF.h:30
ASN__TEXT_INDENT
#define ASN__TEXT_INDENT(nl, level)
Definition: asn_internal.h:117
JCK_KEY
@ JCK_KEY
Definition: jer_decoder.h:83
asn_TYPE_descriptor_s::specifics
const void * specifics
Definition: constr_TYPE.h:259
_A_SET_FROM_VOID
#define _A_SET_FROM_VOID(ptr)
Definition: asn_SET_OF.h:65
RC_OK
@ RC_OK
Definition: asn_codecs.h:82
asn_dec_rval_s::consumed
size_t consumed
Definition: asn_codecs.h:88
PJER_DLM
@ PJER_DLM
Definition: jer_decoder.h:71
asn_SET_OF_specifics_s::as_XMLValueList
int as_XMLValueList
Definition: constr_SET_OF.h:23
constr_SET_OF.h
_A_CSET_FROM_VOID
#define _A_CSET_FROM_VOID(ptr)
Definition: asn_SET_OF.h:66
jer_check_sym
jer_check_sym
Definition: jer_decoder.h:80
PJER_VALUE
@ PJER_VALUE
Definition: jer_decoder.h:70
SET_OF_encode_jer
asn_enc_rval_t SET_OF_encode_jer(const asn_TYPE_descriptor_t *td, const asn_jer_constraints_t *constraints, const void *sptr, int ilevel, enum jer_encoder_flags_e flags, asn_app_consume_bytes_f *cb, void *app_key)
Definition: constr_SET_OF_jer.c:177
asn_app_consume_bytes_f
int() asn_app_consume_bytes_f(const void *buffer, size_t size, void *application_specific_key)
Definition: asn_application.h:124
asn_internal.h
JCK_ASTART
@ JCK_ASTART
Definition: jer_decoder.h:87
asn_codec_ctx_s
Definition: asn_codecs.h:23
asn_TYPE_descriptor_s::elements
struct asn_TYPE_member_s * elements
Definition: constr_TYPE.h:252
asn_dec_rval_s
Definition: asn_codecs.h:86
PJER_KEY
@ PJER_KEY
Definition: jer_decoder.h:69
jer_next_token
ssize_t jer_next_token(int *stateContext, const void *buffer, size_t size, pjer_chunk_type_e *_ch_type)
Definition: jer_decoder.c:64
asn_enc_rval_s::encoded
ssize_t encoded
Definition: asn_codecs.h:47
asn_TYPE_member_s
Definition: constr_TYPE.h:272
JCK_AEND
@ JCK_AEND
Definition: jer_decoder.h:88
jer_check_sym_e
enum jer_check_sym jer_check_sym_e
asn_SET_OF_specifics_s
Definition: constr_SET_OF.h:15
PJER_WMORE
@ PJER_WMORE
Definition: jer_decoder.h:67
RC_FAIL
@ RC_FAIL
Definition: asn_codecs.h:84
asn_dec_rval_s::code
enum asn_dec_rval_code_e code
Definition: asn_codecs.h:87
JCK_OSTART
@ JCK_OSTART
Definition: jer_decoder.h:85


etsi_its_vam_ts_coding
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:30:55