Adding new commiter to ODU-High repo
[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 Data *startPtr128;
178 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 uint8_t *cmMemcpy(uint8_t *tgt,const uint8_t *src,PTR len)
200 {
201    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
202
203 #ifdef MS_MBUF_CORRUPTION /* checking for valid memory address */
204 if ((tgt > startPtr128) && (tgt < (startPtr128+regMemSize)))
205 {
206    if ((*((uint32_t *)(tgt + 4)) == 0xDEADDEAD) || (*((uint32_t *)(tgt + 24)) == 0xDEADDEAD) ||
207       (*((uint32_t *)(tgt + 44)) == 0xDEADDEAD) || (*((uint32_t *)(tgt + 80)) == 0xDEADDEAD))
208    {
209       Data *crashPtr=NULLP;
210       *crashPtr = 9;
211    }
212 }
213 if ((src > startPtr128) && (src < (startPtr128+regMemSize)))
214 {
215    if ((*((uint32_t *)(src + 4)) == 0xDEADDEAD) || (*((uint32_t *)(src + 24)) == 0xDEADDEAD) ||
216       (*((uint32_t *)(src + 44)) == 0xDEADDEAD) || (*((uint32_t *)(src + 80)) == 0xDEADDEAD))
217    {
218       Data *crashPtr=NULLP;
219       *crashPtr = 9;
220    }
221 }
222 #endif 
223 #if (MEMCPY_AVAIL) /* memcpy is available */
224    return ( memcpy(tgt, src, len));
225 #else
226    while (len--)
227       *tgt++ = *src++;
228
229    return (tgt);
230 #endif /* MEMCPY_AVAIL */
231
232 } /* end of cmMemcpy */
233
234 \f
235 /*
236 *
237 *       Fun:   cmMemcmp
238 *
239 *       Desc:  common primitive to compare a contiguous string of bytes
240 *              optimized for when memcmp is available. It uses memcmp
241 *              when available. Otherwise, compares in a 'for' loop.
242 *
243 *       Ret:    < 0 => s1 < s2
244 *               > 0 => s1 > s2
245 *               = 0 => s1 = s2
246 *
247 *       Notes: None
248 *
249 *       File:  cm_lib.c
250 *
251 */
252 S16 cmMemcmp(const uint8_t *s1,const uint8_t *s2,PTR len)
253 {
254    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
255 #if MEMCMP_AVAIL /* memcmp is available */
256    return ((S16) memcmp((const Void *)s1, (const Void *)s2, (size_t)len));
257 #else  /* MEMCMP_AVAIL: memcmp is not available */
258    while (len--)
259    {
260       if (*s1 ^ *s2)
261          return ((S16) (*s1 - *s2));
262       s1++;
263       s2++;
264    }
265    return (0);
266 #endif /* MEMCMP_AVAIL */
267 } /* end of cmMemcmp */
268
269 \f
270 /*
271 *
272 *       Fun:   cmMemset
273 *
274 *       Desc:  common primitive to set a contiguous string of bytes
275 *              with a specified value optimized for when memset is available.
276 *              It uses memset when available. Otherwise, uses a 'for' loop.
277 *
278 *              sets "len" memory locations starting from "str" to the value
279 *              "val".
280
281 *       Ret:   pointer to string
282 *
283 *       Notes: None
284 *
285 *       File:  cm_lib.c
286 *
287 */
288 uint8_t *cmMemset(uint8_t *str,uint8_t val,PTR len)
289 {
290    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
291 #if MS_MBUF_CORRUPTION /* checking for valid memory address */
292 if ((str > startPtr128) && (str < (startPtr128+regMemSize)))
293 {
294    if ((*((uint32_t *)(str + 4)) == 0xDEADDEAD) || (*((uint32_t *)(str + 24)) == 0xDEADDEAD) ||
295       (*((uint32_t *)(str + 44)) == 0xDEADDEAD) || (*((uint32_t *)(str + 80)) == 0xDEADDEAD))
296    {
297       Data *crashPtr=NULLP;
298       *crashPtr = 9;
299    }
300 }
301 #endif
302 #if (MEMSET_AVAIL) /* memset is available */
303    if (val==0)
304    {  
305       bzero((void *)str,(size_t)len);
306    }
307    else
308    {
309       memset(str,val, len);
310    }
311 #else  /* MEMSET_AVAIL: memset is not available */
312    while (len --)
313       *str++ = val;
314
315 #endif /* MEMSET_AVAIL */
316    return (str);
317 } /* end of cmMemset */
318
319 \f
320 /*
321 *
322 *       Fun:   cmStrcmp
323 *
324 *       Desc:  common primitive to compare a contiguous string of characters
325 *              terminated by the '\0' character.
326 *
327 *              when strcmp is available, it uses that. otherwise, it
328 *              compares the strings using a for loop.
329 *
330 *              The following is the "strcmp" description from the SunOS 5.4
331 *              man-page. cmStrcmp follows this.
332 *
333 *             strcmp() compares two strings byte-by-byte, according to the
334 *             ordering  of  your  machine's  character  set.  The function
335 *             returns an integer greater than, equal to, or less  than  0,
336 *             if the string pointed to by s1 is greater than, equal to, or
337 *             less than the string pointed to by s2 respectively. The sign
338 *             of  a non-zero return value is determined by the sign of the
339 *             difference between the values of the  first  pair  of  bytes
340 *             that  differ in the strings being compared.
341 *
342 *             Bytes following a null byte are not compared.
343 *
344 *
345 *       Ret:    < 0 => s1 < s2
346 *               > 0 => s1 > s2
347 *               = 0 => s1 = s2
348 *
349 *       Notes: None
350 *
351 *       File:  cm_lib.c
352 *
353 */
354 S16 cmStrcmp(const uint8_t *s1,const uint8_t *s2)
355 {
356    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
357 #if (STRCMP_AVAIL)
358    return (strcmp((const char *)s1, (const char *)s2));
359 #else   /* STRCMP_AVAIL */
360   
361    while (*s1 && *s2)
362    {
363       if (*s1 ^ *s2)
364          return (*s1 - *s2);
365       s1++;
366       s2++;
367    }
368    return (0);
369 #endif      /* strcmp is not available */
370
371 } /* end of cmStrcmp */
372
373
374 \f
375 /*
376 *
377 *       Fun:   cmStrncmp
378 *
379 *       Desc:  common primitive to compare a contiguous string of characters
380 *              terminated by the '\0' character.
381 *              
382 *              when strncmp is available, it uses that. otherwise, it
383 *              compares the strings using a for loop.
384 *              
385 *              The following is the "strncmp" description from the SunOS 5.4
386 *              man-page. cmStrncmp follows this.
387 *
388 *              strcmp() compares two strings byte-by-byte, according to the
389 *              ordering  of  your  machine's  character  set.  The function
390 *              returns an integer greater than, equal to, or less  than  0,
391 *              if the string pointed to by s1 is greater than, equal to, or
392 *              less than the string pointed to by s2 respectively. The sign
393 *              of  a non-zero return value is determined by the sign of the
394 *              difference between the values of the  first  pair  of  bytes
395 *              that  differ in the strings being compared.  strncmp() makes
396 *              the same comparison but looks  at  a  maximum  of  n  bytes.
397 *              Bytes following a null byte are not compared.
398 *
399 *       Ret:    < 0 => s1 < s2
400 *               > 0 => s1 > s2
401 *               = 0 => s1 = s2
402 *
403 *       Notes: None
404 *
405 *       File:  cm_lib.c
406 *
407 */
408 S16 cmStrncmp
409 (
410 const uint8_t  *s1,
411 const uint8_t  *s2,
412 MsgLen       len /* cm_lib_c_001.main_12: Changing from S16 to MsgLen.*/
413 )
414 {
415    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
416 #if (STRNCMP_AVAIL)
417    return (strncmp((const char *)s1, (const char *)s2, (size_t) len));
418 #else   /* STRNCMP_AVAIL */
419   
420    while (*s1 && *s2 && len--)
421    {
422       if (*s1 ^ *s2)
423          return (*s1 - *s2);
424       s1++;
425       s2++;
426    }
427    return (0);
428 #endif   /* strncmp is not available */
429 } /* end of cmStrncmp */
430
431 \f
432 /*
433 *
434 *       Fun:   cmStrlen
435 *
436 *       Desc:  common primitive to compute the length of a NULL terminated
437 *              string.
438 *              
439 *              when strlen is available, it uses that. otherwise, it
440 *              inspects the string using a for loop.
441 *              
442 *              The following is the "strlen" description from the SunOS 5.4
443 *              man-page. cmStrlen follows this.
444 *
445 *              strlen() returns the number of bytes in s, not including the
446 *              terminating null character.
447 *
448 *       Ret:   length of string
449 *
450 *       Notes: None
451 *
452 *       File:  cm_lib.c
453 *
454 */
455 MsgLen cmStrlen(const uint8_t *s)
456 {
457 #if (STRLEN_AVAIL)
458    /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
459    return ((MsgLen)strlen((const char *)s));
460 #else   /* STRLEN_AVAIL */
461    MsgLen i;
462   
463    /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
464
465    for (i = 0; *s; i++, s++);
466    return (i);
467 #endif   /* strlen is not available */
468 } /* end of cmStrlen */
469
470 /**********************************************************************
471          End of file
472 **********************************************************************/