U8, U16, U32 data type changes
[o-du/l2.git] / src / cm / cm_lib.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /********************************************************************20**
20
21      Name:     common library functions
22   
23      Type:     C source file
24   
25      Desc:     Common functions that are implemented in
26                both a portable and a performance-efficient manner. These
27                functions are selected based on the operating system.
28
29      File:     cm_lib.c
30
31 *********************************************************************21*/
32 \f
33 /***********************************************************************
34  * This file provides memory and string library functions implemented in
35  * one of two ways:
36  *
37  *     1. portable: does not assume library support for any of the functions
38  *                  needed.
39  *
40  *     2. using environment specific: this uses existing library
41  *        functions and is therefore, for an environment where such
42  *        functionality is available and , a more desirable choice!
43  *
44  *  The following functions are available in this file:
45  *
46  *       1. cmMemcpy: copies specified number of octets from one memory
47  *          location to another.
48  *       2. cmMemcmp: compares specified number of octets at one memory
49  *          locaton with the same number from another.
50  *       3. cmMemset: sets specified number of octets starting at given
51  *          memory location with the given value.
52  *       4. cmStrCmp: compares two strings, until the '\0' character is
53  *          encountered.
54  *       5. cmStrncmp: compares two strings, until \0 is encountered or
55  *          until the specified length is exceeded.
56  *       6. cmStrlen: finds length of a string until the \0 character.
57  *
58  *  The following flags are used in this file:
59  *
60  *     1. DONT_USE_SYS_LIB: If defined, this will compile in the portable
61  *        versions of each function in this file. This overrides all other
62  *        flags defined in this file.
63  *
64  *  For the SOLARIS environment:
65  *
66  *     The functionality defined in this file is generally available as library
67  *     functions in the native operating systems' libraries. We have provided
68  *     the functionality using the SOLARIS libraries, in this file.
69  *
70  *     If you want to take advantage of these library functions, all you need
71  *     to do is the following:
72  *
73  *     1. Do not turn on the DONT_USE_SYS_LIB flag.
74  *     2. Turn on the SUNOS51 flag.
75  *
76  *     This, in turn, turns on the following flags
77  *
78  *        1. MEMCPY_AVAIL  : maps cmMemcpy to C library function memcpy
79  *        2. MEMCMP_AVAIL  : maps cmMemcmp to C library function memcmp
80  *        3. MEMSET_AVAIL  : maps cmMemset to C library function memset
81  *        4. STRCMP_AVAIL  : maps cmStrcmp to Strings library function strcmp
82  *        5. STRNCMP_AVAIL : maps cmStrncmp to Strings library function strncmp
83  *        5. STRLEN_AVAIL  : maps cmStrlen to Strings library function strlen
84  *
85  *  For an environment different from SOLARIS:
86  *        
87  *    You will need to modify this file to do more or less the same stuff
88  *    as has been done for Solaris. i.e.
89  *
90  *     1. create a section inside the #ifndef DONT_USE_SYS_LIB section
91  *        similar to the #ifdef SUNOS51 section that:
92  *             1. includes system header files.
93  *             2. defines MEMCPY_AVAIL etc. as needed.
94  *     2. modify code inside functions to make use of the system library
95  *        primitive.
96  *     3. communicate your changes to Trillium so they can be incorporated
97  *        in the next release of this file
98  *
99  *  To add a new library primitive to this file:
100  *     1. it should be implemented in both a portable and environment specific
101  *        manner.
102  *     2. the portable implementation will not be the default
103  *     3. the portable version and the environment specif versions must be
104  *        enclosed in #ifdef XXXXXX_AVAIL
105  *                     <environment specific implementation>
106  *                    #else
107  *                     <portable implementation>
108  *                    #endif
109  *     4. It must be communicated to Trillium so it will be included in the
110  *        next release of the file.
111  *     5. Trillium engineering must have all changes approved by engineering
112  *        management.
113  *     6. Trillium reserves the right to not incorporate any changes submitted
114  *        by customers, if not approved by Trillium engineering management.
115  *     7. Trillium reserves the right to modify code submitted by customers to
116  *        enhance this file.
117  ************************************************************************/
118 \f
119 /*cm_lib_c_001.main_13 - Moved env files inclusion to the top*/
120 #include "envopt.h"        /* environment options */
121 #include "envind.h"        /* environment options */
122 #include "envdep.h"        /* environment options */
123
124 #ifndef DONT_USE_SYS_LIB
125
126 #include <stdio.h>
127 #include <ctype.h>
128 #include <stdlib.h>
129 #include <string.h>
130 #define MEMCPY_AVAIL   1
131 #define MEMCMP_AVAIL   1
132 #define MEMSET_AVAIL   1
133 #define STRCMP_AVAIL   1
134 #define STRNCMP_AVAIL  1
135 #define STRLEN_AVAIL   1
136
137 #else  /* DONT_USE_SYS_LIB */
138 #define MEMCPY_AVAIL   0
139 #define MEMCMP_AVAIL   0
140 #define MEMSET_AVAIL   0
141 #define STRCMP_AVAIL   0
142 #define STRNCMP_AVAIL  0
143 #define STRLEN_AVAIL   0
144 #endif /* not DONT_USE_SYS_LIB */
145
146
147  
148 #include "gen.h"           /* general layer */
149 #include "ssi.h"           /* system services */
150
151 /* header/extern include files (.x) */
152  
153 #include "gen.x"           /* general layer */
154 #include "ssi.x"           /* system services */
155 #include "cm_lib.x"        /* prototypes of primitives in this file */
156
157 #if (ERRCLASS & ERRCLS_DEBUG)
158
159 #define ECMLIBBASE     0
160 #define ECMLIB001      ECMLIBBASE + 1
161 #define ECMLIB002      ECMLIBBASE + 2
162 #define ECMLIB003      ECMLIBBASE + 3
163 #define ECMLIB004      ECMLIBBASE + 4
164 #define ECMLIB005      ECMLIBBASE + 5
165 #define ECMLIB006      ECMLIBBASE + 6
166 #define ECMLIB007      ECMLIBBASE + 7
167
168 #define CMLIBERR(_eCode, _eVal, _eDesc) \
169     SLogError ((Ent) 0, (Inst)0, (ProcId)0, __FILE__, __LINE__, \
170                (ErrCls)ERRCLS_DEBUG, (ErrCode)_eCode, (ErrVal) _eVal, \
171                (Txt *) _eDesc)
172 #else
173 #define CMLIBERR(_eCode, _eVal, _eDesc)
174 #endif
175
176 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
177 EXTERN Data *startPtr128;
178 EXTERN Size regMemSize;
179 #endif
180 \f
181 /*
182 *
183 *       Fun:   cmMemcpy
184 *
185 *       Desc:  common primitive to copy a contiguous string of bytes
186 *              optimized for when memcpy is available. It uses memcpy
187 *              when available. Otherwise, copies in a 'for' loop.
188 *
189 *              sets "len" memory locations starting from "tgt" to the values
190 *              of corresponding memory locations starting from "src".
191 *
192 *       Ret:   pointer to target string
193 *
194 *       Notes: None
195 *
196 *       File:  cm_lib.c
197 *
198 */
199 #ifdef ANSI
200 uint8_t *cmMemcpy
201 (
202 uint8_t           *tgt,
203 CONSTANT uint8_t  *src,
204 PTR          len
205 )
206 #else
207 uint8_t *cmMemcpy(tgt, src, len)
208 uint8_t           *tgt;
209 CONSTANT uint8_t  *src;
210 PTR          len;
211 #endif
212 {
213    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
214
215 #ifdef MS_MBUF_CORRUPTION /* checking for valid memory address */
216 if ((tgt > startPtr128) && (tgt < (startPtr128+regMemSize)))
217 {
218    if ((*((uint32_t *)(tgt + 4)) == 0xDEADDEAD) || (*((uint32_t *)(tgt + 24)) == 0xDEADDEAD) ||
219       (*((uint32_t *)(tgt + 44)) == 0xDEADDEAD) || (*((uint32_t *)(tgt + 80)) == 0xDEADDEAD))
220    {
221       Data *crashPtr=NULLP;
222       *crashPtr = 9;
223    }
224 }
225 if ((src > startPtr128) && (src < (startPtr128+regMemSize)))
226 {
227    if ((*((uint32_t *)(src + 4)) == 0xDEADDEAD) || (*((uint32_t *)(src + 24)) == 0xDEADDEAD) ||
228       (*((uint32_t *)(src + 44)) == 0xDEADDEAD) || (*((uint32_t *)(src + 80)) == 0xDEADDEAD))
229    {
230       Data *crashPtr=NULLP;
231       *crashPtr = 9;
232    }
233 }
234 #endif 
235 #if (MEMCPY_AVAIL) /* memcpy is available */
236    return ( memcpy(tgt, src, len));
237 #else
238    while (len--)
239       *tgt++ = *src++;
240
241    return (tgt);
242 #endif /* MEMCPY_AVAIL */
243
244 } /* end of cmMemcpy */
245
246 \f
247 /*
248 *
249 *       Fun:   cmMemcmp
250 *
251 *       Desc:  common primitive to compare a contiguous string of bytes
252 *              optimized for when memcmp is available. It uses memcmp
253 *              when available. Otherwise, compares in a 'for' loop.
254 *
255 *       Ret:    < 0 => s1 < s2
256 *               > 0 => s1 > s2
257 *               = 0 => s1 = s2
258 *
259 *       Notes: None
260 *
261 *       File:  cm_lib.c
262 *
263 */
264 #ifdef ANSI
265 S16 cmMemcmp
266 (
267 CONSTANT uint8_t     *s1,
268 CONSTANT uint8_t     *s2,
269 PTR             len
270 )
271 #else
272 S16 cmMemcmp (s1, s2, len)
273 CONSTANT uint8_t     *s1;
274 CONSTANT uint8_t     *s2;
275 PTR             len;
276 #endif
277 {
278    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
279 #if MEMCMP_AVAIL /* memcmp is available */
280    return ((S16) memcmp((CONSTANT Void *)s1, (CONSTANT Void *)s2, (size_t)len));
281 #else  /* MEMCMP_AVAIL: memcmp is not available */
282    while (len--)
283    {
284       if (*s1 ^ *s2)
285          return ((S16) (*s1 - *s2));
286       s1++;
287       s2++;
288    }
289    return (0);
290 #endif /* MEMCMP_AVAIL */
291 } /* end of cmMemcmp */
292
293 \f
294 /*
295 *
296 *       Fun:   cmMemset
297 *
298 *       Desc:  common primitive to set a contiguous string of bytes
299 *              with a specified value optimized for when memset is available.
300 *              It uses memset when available. Otherwise, uses a 'for' loop.
301 *
302 *              sets "len" memory locations starting from "str" to the value
303 *              "val".
304
305 *       Ret:   pointer to string
306 *
307 *       Notes: None
308 *
309 *       File:  cm_lib.c
310 *
311 */
312 #ifdef ANSI
313 uint8_t *cmMemset
314 (
315 uint8_t           *str,
316 uint8_t           val,
317 PTR          len
318 )
319 #else
320 uint8_t *cmMemset(str, val, len)
321 uint8_t           *str;
322 uint8_t           val;
323 PTR          len;
324 #endif
325 {
326    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
327 #if MS_MBUF_CORRUPTION /* checking for valid memory address */
328 if ((str > startPtr128) && (str < (startPtr128+regMemSize)))
329 {
330    if ((*((uint32_t *)(str + 4)) == 0xDEADDEAD) || (*((uint32_t *)(str + 24)) == 0xDEADDEAD) ||
331       (*((uint32_t *)(str + 44)) == 0xDEADDEAD) || (*((uint32_t *)(str + 80)) == 0xDEADDEAD))
332    {
333       Data *crashPtr=NULLP;
334       *crashPtr = 9;
335    }
336 }
337 #endif
338 #if (MEMSET_AVAIL) /* memset is available */
339    if (val==0)
340    {  
341       bzero((void *)str,(size_t)len);
342    }
343    else
344    {
345       memset(str,val, len);
346    }
347 #else  /* MEMSET_AVAIL: memset is not available */
348    while (len --)
349       *str++ = val;
350
351 #endif /* MEMSET_AVAIL */
352    return (str);
353 } /* end of cmMemset */
354
355 \f
356 /*
357 *
358 *       Fun:   cmStrcmp
359 *
360 *       Desc:  common primitive to compare a contiguous string of characters
361 *              terminated by the '\0' character.
362 *
363 *              when strcmp is available, it uses that. otherwise, it
364 *              compares the strings using a for loop.
365 *
366 *              The following is the "strcmp" description from the SunOS 5.4
367 *              man-page. cmStrcmp follows this.
368 *
369 *             strcmp() compares two strings byte-by-byte, according to the
370 *             ordering  of  your  machine's  character  set.  The function
371 *             returns an integer greater than, equal to, or less  than  0,
372 *             if the string pointed to by s1 is greater than, equal to, or
373 *             less than the string pointed to by s2 respectively. The sign
374 *             of  a non-zero return value is determined by the sign of the
375 *             difference between the values of the  first  pair  of  bytes
376 *             that  differ in the strings being compared.
377 *
378 *             Bytes following a null byte are not compared.
379 *
380 *
381 *       Ret:    < 0 => s1 < s2
382 *               > 0 => s1 > s2
383 *               = 0 => s1 = s2
384 *
385 *       Notes: None
386 *
387 *       File:  cm_lib.c
388 *
389 */
390 #ifdef ANSI
391 S16 cmStrcmp
392 (
393 CONSTANT uint8_t *s1,
394 CONSTANT uint8_t *s2
395 )
396 #else
397 S16 cmStrcmp (s1, s2)
398 CONSTANT uint8_t *s1;
399 CONSTANT uint8_t *s2;
400 #endif
401 {
402    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
403 #if (STRCMP_AVAIL)
404    return (strcmp((CONSTANT S8 *)s1, (CONSTANT S8 *)s2));
405 #else   /* STRCMP_AVAIL */
406   
407    while (*s1 && *s2)
408    {
409       if (*s1 ^ *s2)
410          return (*s1 - *s2);
411       s1++;
412       s2++;
413    }
414    return (0);
415 #endif      /* strcmp is not available */
416
417 } /* end of cmStrcmp */
418
419
420 \f
421 /*
422 *
423 *       Fun:   cmStrncmp
424 *
425 *       Desc:  common primitive to compare a contiguous string of characters
426 *              terminated by the '\0' character.
427 *              
428 *              when strncmp is available, it uses that. otherwise, it
429 *              compares the strings using a for loop.
430 *              
431 *              The following is the "strncmp" description from the SunOS 5.4
432 *              man-page. cmStrncmp follows this.
433 *
434 *              strcmp() compares two strings byte-by-byte, according to the
435 *              ordering  of  your  machine's  character  set.  The function
436 *              returns an integer greater than, equal to, or less  than  0,
437 *              if the string pointed to by s1 is greater than, equal to, or
438 *              less than the string pointed to by s2 respectively. The sign
439 *              of  a non-zero return value is determined by the sign of the
440 *              difference between the values of the  first  pair  of  bytes
441 *              that  differ in the strings being compared.  strncmp() makes
442 *              the same comparison but looks  at  a  maximum  of  n  bytes.
443 *              Bytes following a null byte are not compared.
444 *
445 *       Ret:    < 0 => s1 < s2
446 *               > 0 => s1 > s2
447 *               = 0 => s1 = s2
448 *
449 *       Notes: None
450 *
451 *       File:  cm_lib.c
452 *
453 */
454 #ifdef ANSI
455 S16 cmStrncmp
456 (
457 CONSTANT uint8_t  *s1,
458 CONSTANT uint8_t  *s2,
459 MsgLen       len /* cm_lib_c_001.main_12: Changing from S16 to MsgLen.*/
460 )
461 #else
462 S16 cmStrncmp (s1, s2, len)
463 CONSTANT uint8_t  *s1;
464 CONSTANT uint8_t  *s2;
465 MsgLen       len;
466 #endif
467 {
468    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
469 #if (STRNCMP_AVAIL)
470    return (strncmp((CONSTANT S8 *)s1, (CONSTANT S8 *)s2, (size_t) len));
471 #else   /* STRNCMP_AVAIL */
472   
473    while (*s1 && *s2 && len--)
474    {
475       if (*s1 ^ *s2)
476          return (*s1 - *s2);
477       s1++;
478       s2++;
479    }
480    return (0);
481 #endif   /* strncmp is not available */
482 } /* end of cmStrncmp */
483
484 \f
485 /*
486 *
487 *       Fun:   cmStrlen
488 *
489 *       Desc:  common primitive to compute the length of a NULL terminated
490 *              string.
491 *              
492 *              when strlen is available, it uses that. otherwise, it
493 *              inspects the string using a for loop.
494 *              
495 *              The following is the "strlen" description from the SunOS 5.4
496 *              man-page. cmStrlen follows this.
497 *
498 *              strlen() returns the number of bytes in s, not including the
499 *              terminating null character.
500 *
501 *       Ret:   length of string
502 *
503 *       Notes: None
504 *
505 *       File:  cm_lib.c
506 *
507 */
508 #ifdef ANSI
509 MsgLen cmStrlen
510 (
511 CONSTANT uint8_t *s
512 )
513 #else
514 /* cm_lib_c_001.main_12: Changing from S16 to MsgLen.*/
515 MsgLen cmStrlen (s)
516 CONSTANT uint8_t *s;
517 #endif
518 {
519 #if (STRLEN_AVAIL)
520    /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
521    return ((MsgLen)strlen((CONSTANT S8 *)s));
522 #else   /* STRLEN_AVAIL */
523    MsgLen i;
524   
525    /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
526
527    for (i = 0; *s; i++, s++);
528    return (i);
529 #endif   /* strlen is not available */
530 } /* end of cmStrlen */
531
532 /**********************************************************************
533          End of file
534 **********************************************************************/