FreeCalypso > hg > fc-tourmaline
comparison src/gpf/ccd/bcd_mnc.c @ 0:4e78acac3d88
src/{condat,cs,gpf,nucleus}: import from Selenite
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 16 Oct 2020 06:23:26 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4e78acac3d88 |
---|---|
1 /* | |
2 +----------------------------------------------------------------------------- | |
3 | Project : | |
4 | Modul : bcd_mnc.c | |
5 +----------------------------------------------------------------------------- | |
6 | Copyright 2002 Texas Instruments Berlin, AG | |
7 | All rights reserved. | |
8 | | |
9 | This file is confidential and a trade secret of Texas | |
10 | Instruments Berlin, AG | |
11 | The receipt of or possession of this file does not convey | |
12 | any rights to reproduce or disclose its contents or to | |
13 | manufacture, use, or sell anything it may describe, in | |
14 | whole, or in part, without the specific written consent of | |
15 | Texas Instruments Berlin, AG. | |
16 +----------------------------------------------------------------------------- | |
17 | Purpose : Definition of encoding and decoding functions for BCD_MNC elements | |
18 +----------------------------------------------------------------------------- | |
19 */ | |
20 | |
21 | |
22 /* | |
23 * standard definitions like GLOBAL, UCHAR, ERROR etc. | |
24 */ | |
25 #include "typedefs.h" | |
26 #include "header.h" | |
27 | |
28 /* | |
29 * Prototypes of ccd (USE_DRIVER EQ undef) for prototypes only | |
30 * look at ccdapi.h | |
31 */ | |
32 #undef USE_DRIVER | |
33 #include "ccdapi.h" | |
34 | |
35 /* | |
36 * Types and functions for bit access and manipulation | |
37 */ | |
38 #include "ccd_globs.h" | |
39 #include "bitfun.h" | |
40 | |
41 /* | |
42 * Prototypes of ccd internal functions | |
43 */ | |
44 #include "ccd.h" | |
45 | |
46 /* | |
47 * Declaration of coder/decoder tables | |
48 */ | |
49 #include "ccdtable.h" | |
50 #include "ccddata.h" | |
51 | |
52 #ifndef RUN_INT_RAM | |
53 /* | |
54 +--------------------------------------------------------------------+ | |
55 | PROJECT : CCD (6144) MODULE : CCD | | |
56 | STATE : code ROUTINE : cdc_bcd_mnc_decode | | |
57 +--------------------------------------------------------------------+ | |
58 | |
59 PURPOSE : decoding a byte array, that contains a Mobile Network Code, | |
60 from the bitstream: | |
61 | |
62 MSBit LSBit | |
63 7 8 6 5 4 3 2 1 | |
64 DIGIT_3 XXXXXXX Octett n-1 | |
65 DIGIT_2 DIGIT_1 Octett n | |
66 | |
67 The current decoding position is expected after Octett n-1 | |
68 The byte array should be of dimension [2..3] (preferred) | |
69 or [3] or [2] (also supported) | |
70 | |
71 */ | |
72 | |
73 | |
74 SHORT cdc_bcd_mnc_decode (const ULONG c_ref, const ULONG e_ref, T_CCD_Globs *globs) | |
75 { | |
76 ULONG repeat, max_rep; | |
77 BOOL is_variable; | |
78 UBYTE digBuffer[3]; | |
79 UBYTE *addr_c_xxx; | |
80 ULONG i; | |
81 ULONG cix_ref, num_prolog_steps, prolog_step_ref; | |
82 register UBYTE *digits; | |
83 | |
84 #ifdef DEBUG_CCD | |
85 #ifndef CCD_SYMBOLS | |
86 TRACE_CCD (globs, "cdc_bcd_mnc_decode()"); | |
87 #else | |
88 TRACE_CCD (globs, "cdc_bcd_mnc_decode() %s", ccddata_get_alias((USHORT) e_ref, 1)); | |
89 #endif | |
90 #endif | |
91 | |
92 cix_ref = melem[e_ref].calcIdxRef; | |
93 num_prolog_steps = calcidx[cix_ref].numPrologSteps; | |
94 prolog_step_ref = calcidx[cix_ref].prologStepRef; | |
95 | |
96 /* | |
97 * if this element is conditional, check the condition | |
98 */ | |
99 if (calcidx[cix_ref].numCondCalcs NEQ 0 | |
100 AND ! ccd_conditionOK (e_ref, globs)) | |
101 return 1; | |
102 | |
103 /* | |
104 * if this element have a defined Prolog | |
105 * we have to process it before decoding the bitstream | |
106 */ | |
107 if (num_prolog_steps) | |
108 { | |
109 ccd_performOperations (num_prolog_steps, prolog_step_ref, globs); | |
110 } | |
111 | |
112 /* | |
113 * if this element is repeatable, and the number of | |
114 * repeats depends on another element, calculate the repeater | |
115 */ | |
116 if (melem[e_ref].repType NEQ ' ') | |
117 { | |
118 is_variable = ccd_calculateRep (e_ref, &repeat, &max_rep, globs); | |
119 } | |
120 else | |
121 { | |
122 repeat = 1; | |
123 is_variable = FALSE; | |
124 } | |
125 | |
126 /* | |
127 * setup the offset into the C-structure for this element | |
128 */ | |
129 globs->pstructOffs = melem[e_ref].structOffs; | |
130 | |
131 if (melem[e_ref].optional) | |
132 { | |
133 /* | |
134 * for optional elements set the valid-flag | |
135 */ | |
136 globs->pstruct[globs->pstructOffs++] = (UBYTE) TRUE; | |
137 } | |
138 | |
139 if (is_variable) | |
140 { | |
141 if (max_rep < 2 OR max_rep > 3) | |
142 { | |
143 ccd_setError (globs, ERR_INVALID_TYPE, BREAK, (USHORT) (globs->bitpos), | |
144 (USHORT) -1); | |
145 } | |
146 /* | |
147 * for variable sized elements store the min-value | |
148 * as counter into the C-Structure (c_xxx). | |
149 */ | |
150 addr_c_xxx = (UBYTE *) (globs->pstruct + globs->pstructOffs++); | |
151 if (max_rep > 255) | |
152 globs->pstructOffs++; | |
153 } | |
154 else | |
155 addr_c_xxx = NULL; | |
156 | |
157 digits = (UBYTE *) (globs->pstruct + globs->pstructOffs); | |
158 | |
159 bf_setBitpos ((globs->bitpos - 8), globs); | |
160 | |
161 /* | |
162 * read the BCD digits out of the bitstream. | |
163 * The read order is 3,X,2,1 | |
164 */ | |
165 digBuffer[2] = bf_decodeByteNumber (4, globs); | |
166 bf_incBitpos (4, globs); | |
167 | |
168 digBuffer[1] = bf_decodeByteNumber (4, globs); | |
169 digBuffer[0] = bf_decodeByteNumber (4, globs); | |
170 | |
171 if (addr_c_xxx NEQ NULL) | |
172 { | |
173 /* | |
174 * store the number of digits into the | |
175 * c_xxx variable if there is one. | |
176 */ | |
177 repeat = (ULONG) ((digBuffer[2] EQ 0x0f) ? 2 : 3); | |
178 if (max_rep > 65535) | |
179 { | |
180 ULONG *addr_c_xxx_u32; | |
181 addr_c_xxx_u32 = (ULONG *)addr_c_xxx; | |
182 *addr_c_xxx_u32 = repeat; | |
183 } | |
184 else if (max_rep > 255) | |
185 { | |
186 USHORT *addr_c_xxx_u16; | |
187 addr_c_xxx_u16 = (USHORT *)addr_c_xxx; | |
188 *addr_c_xxx_u16 = (USHORT) repeat; | |
189 } | |
190 else | |
191 *addr_c_xxx = (UBYTE) repeat; | |
192 } | |
193 else | |
194 { | |
195 if (max_rep EQ 2 AND digBuffer[2] NEQ 0xf) | |
196 ccd_setError (globs, ERR_PATTERN_MISMATCH, | |
197 CONTINUE, | |
198 (USHORT) (globs->bitpos-16), | |
199 (USHORT) -1); | |
200 | |
201 repeat = max_rep; | |
202 } | |
203 /* | |
204 * store the digits into the C-Structure variable | |
205 */ | |
206 for (i=0; i<repeat; i++) | |
207 digits[i] = digBuffer[i]; | |
208 | |
209 return 1; | |
210 } | |
211 #endif /* !RUN_INT_RAM */ | |
212 | |
213 #ifndef RUN_INT_RAM | |
214 /* | |
215 +--------------------------------------------------------------------+ | |
216 | PROJECT : CCD (6144) MODULE : CCD | | |
217 | STATE : code ROUTINE : cdc_bcd_mnc_encode | | |
218 +--------------------------------------------------------------------+ | |
219 | |
220 PURPOSE : encoding a byte array, that contains a Mobile Network Code, | |
221 into the bitstream: | |
222 | |
223 MSBit LSBit | |
224 7 8 6 5 4 3 2 1 | |
225 DIGIT_3 XXXXXXX Octett n-1 | |
226 DIGIT_2 DIGIT_1 Octett n | |
227 | |
228 The current coding position is expected after Octett n-1 | |
229 */ | |
230 | |
231 | |
232 SHORT cdc_bcd_mnc_encode (const ULONG c_ref, const ULONG e_ref, T_CCD_Globs *globs) | |
233 { | |
234 ULONG repeat; | |
235 UBYTE dig3; | |
236 ULONG cix_ref, num_prolog_steps, prolog_step_ref; | |
237 register UBYTE *digits; | |
238 | |
239 #ifdef DEBUG_CCD | |
240 #ifndef CCD_SYMBOLS | |
241 TRACE_CCD (globs, "cdc_bcd_mnc_encode()"); | |
242 #else | |
243 TRACE_CCD (globs, "cdc_bcd_mnc_encode() %s", ccddata_get_alias((USHORT) e_ref, 1)); | |
244 #endif | |
245 #endif | |
246 | |
247 cix_ref = melem[e_ref].calcIdxRef; | |
248 num_prolog_steps = calcidx[cix_ref].numPrologSteps; | |
249 prolog_step_ref = calcidx[cix_ref].prologStepRef; | |
250 | |
251 /* | |
252 * if this element is conditional, check the condition | |
253 */ | |
254 if (calcidx[cix_ref].numCondCalcs NEQ 0 | |
255 AND ! ccd_conditionOK (e_ref, globs)) | |
256 return 1; | |
257 | |
258 /* | |
259 * if this element have a defined Prolog | |
260 * we have to process it before decoding the bitstream | |
261 */ | |
262 if (num_prolog_steps) | |
263 { | |
264 ccd_performOperations (num_prolog_steps, prolog_step_ref, globs); | |
265 } | |
266 | |
267 /* | |
268 * setup the offset into the C-structure for this element | |
269 */ | |
270 globs->pstructOffs = melem[e_ref].structOffs; | |
271 | |
272 if (melem[e_ref].optional) | |
273 { | |
274 /* | |
275 * for optional elements check the valid-flag | |
276 */ | |
277 if (globs->pstruct[globs->pstructOffs++] == FALSE) | |
278 { | |
279 return 1; | |
280 } | |
281 #ifdef DEBUG_CCD | |
282 else if (globs->pstruct [melem[e_ref].structOffs] != TRUE) | |
283 { | |
284 TRACE_CCD (globs, "Ambiguous value for valid flag!\n...assumed 1 for ccdID=%d", | |
285 e_ref); | |
286 } | |
287 #endif | |
288 } | |
289 | |
290 /* | |
291 * if this element is repeatable, and the number of | |
292 * repeats depends on another element, calculate the repeater | |
293 */ | |
294 if (melem[e_ref].repType EQ 'v' OR melem[e_ref].repType EQ 'i') | |
295 { | |
296 /* | |
297 * for variable sized elements read the amount | |
298 * of repeats out of the C-Structure (c_xxx). | |
299 * If the number of repeats given by the C-Structure | |
300 * exceeds the allowed value (max_repeat) CCD gives a warning! | |
301 */ | |
302 if (melem[e_ref].maxRepeat > 255) | |
303 { | |
304 ULONG count = (ULONG) (* (USHORT *)(globs->pstruct + globs->pstructOffs++)); | |
305 repeat = MINIMUM (count, (ULONG)melem[e_ref].maxRepeat); | |
306 if (repeat < count) | |
307 ccd_recordFault (globs, ERR_MAX_REPEAT, CONTINUE, | |
308 (USHORT) e_ref, globs->pstruct + globs->pstructOffs); | |
309 } | |
310 else | |
311 { | |
312 repeat = (ULONG) MINIMUM (globs->pstruct[globs->pstructOffs], | |
313 melem[e_ref].maxRepeat); | |
314 if ( repeat < (ULONG) (globs->pstruct[globs->pstructOffs]) ) | |
315 ccd_recordFault (globs, ERR_MAX_REPEAT, CONTINUE, | |
316 (USHORT) e_ref, globs->pstruct + globs->pstructOffs); | |
317 } | |
318 | |
319 globs->pstructOffs++; | |
320 } | |
321 else | |
322 if (melem[e_ref].repType EQ 'c') | |
323 repeat = (ULONG) melem[e_ref].maxRepeat; | |
324 else | |
325 repeat = 1; | |
326 | |
327 /* | |
328 * setup the read pointer to the byte array that contain | |
329 * the BCD number. | |
330 */ | |
331 digits = (UBYTE *) (globs->pstruct + globs->pstructOffs); | |
332 | |
333 if (repeat EQ 2) | |
334 dig3 = 0x0f; | |
335 else if (repeat EQ 3) | |
336 dig3 = digits[2]; | |
337 else | |
338 { | |
339 ccd_setError (globs, ERR_INVALID_TYPE, | |
340 BREAK, | |
341 (USHORT) (globs->bitpos), | |
342 (USHORT) -1); | |
343 return 1; | |
344 } | |
345 | |
346 bf_setBitpos ((globs->bitpos-8), globs); | |
347 bf_codeByteNumber (4, dig3, globs); | |
348 bf_incBitpos (4, globs); | |
349 bf_codeByteNumber (4, digits[1], globs); | |
350 bf_codeByteNumber (4, digits[0], globs); | |
351 #ifdef DEBUG_CCD | |
352 TRACE_CCD (globs, "skipping back 8 bits"); | |
353 TRACE_CCD (globs, "BCD digit (%X) written", (USHORT) dig3); | |
354 TRACE_CCD (globs, "skipping 4 bits"); | |
355 TRACE_CCD (globs, "BCD digit (%X) written", (USHORT) digits[1]); | |
356 TRACE_CCD (globs, "BCD digit (%X) written", (USHORT) digits[0]); | |
357 #endif | |
358 | |
359 return 1; | |
360 } | |
361 #endif /* !RUN_INT_RAM */ |