1 /**
2  *
3  * \file
4  *
5  * \brief This module contains common APIs declarations.
6  *
7  * Copyright (c) 2016 Atmel Corporation. All rights reserved.
8  *
9  * \asf_license_start
10  *
11  * \page License
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright notice,
17  *    this list of conditions and the following disclaimer.
18  *
19  * 2. Redistributions in binary form must reproduce the above copyright notice,
20  *    this list of conditions and the following disclaimer in the documentation
21  *    and/or other materials provided with the distribution.
22  *
23  * 3. The name of Atmel may not be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
29  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
30  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *
38  * \asf_license_stop
39  *
40  */
41 #include "common/include/nm_common.h"
42 
m2m_memcpy(uint8 * pDst,uint8 * pSrc,uint32 sz)43 void m2m_memcpy(uint8* pDst,uint8* pSrc,uint32 sz)
44 {
45 	if(sz == 0) return;
46 	do
47 	{
48 		*pDst = *pSrc;
49 		pDst++;
50 		pSrc++;
51 	}while(--sz);
52 }
m2m_checksum(uint8 * buf,int sz)53 uint8 m2m_checksum(uint8* buf, int sz)
54 {
55 	uint8 cs = 0;
56 	while(--sz)
57 	{
58 		cs ^= *buf;
59 		buf++;
60 	}
61 
62 	return cs;
63 }
64 
m2m_memset(uint8 * pBuf,uint8 val,uint32 sz)65 void m2m_memset(uint8* pBuf,uint8 val,uint32 sz)
66 {
67 	if(sz == 0) return;
68 	do
69 	{
70 		*pBuf = val;
71 		pBuf++;
72 	}while(--sz);
73 }
74 
m2m_strlen(uint8 * pcStr)75 uint16 m2m_strlen(uint8 * pcStr)
76 {
77 	uint16	u16StrLen = 0;
78 	while(*pcStr)
79 	{
80 		u16StrLen ++;
81 		pcStr++;
82 	}
83 	return u16StrLen;
84 }
85 
m2m_strncmp(uint8 * pcS1,uint8 * pcS2,uint16 u16Len)86 uint8 m2m_strncmp(uint8 *pcS1, uint8 *pcS2, uint16 u16Len)
87 {
88     for ( ; u16Len > 0; pcS1++, pcS2++, --u16Len)
89 	if (*pcS1 != *pcS2)
90 	    return ((*(uint8 *)pcS1 < *(uint8 *)pcS2) ? -1 : +1);
91 	else if (*pcS1 == '\0')
92 	    return 0;
93     return 0;
94 }
95 
96 /* Finds the occurance of pcStr in pcIn.
97 If pcStr is part of pcIn it returns a valid pointer to the start of pcStr within pcIn.
98 Otherwise a NULL Pointer is returned.
99 */
m2m_strstr(uint8 * pcIn,uint8 * pcStr)100 uint8 * m2m_strstr(uint8 *pcIn, uint8 *pcStr)
101 {
102     uint8 u8c;
103     uint16 u16StrLen;
104 
105     u8c = *pcStr++;
106     if (!u8c)
107         return (uint8 *) pcIn;	// Trivial empty string case
108 
109     u16StrLen = m2m_strlen(pcStr);
110     do {
111         uint8 u8Sc;
112 
113         do {
114             u8Sc = *pcIn++;
115             if (!u8Sc)
116                 return (uint8 *) 0;
117         } while (u8Sc != u8c);
118     } while (m2m_strncmp(pcIn, pcStr, u16StrLen) != 0);
119 
120     return (uint8 *) (pcIn - 1);
121 }
122 
m2m_memcmp(uint8 * pu8Buff1,uint8 * pu8Buff2,uint32 u32Size)123 sint8 m2m_memcmp(uint8 *pu8Buff1,uint8 *pu8Buff2 ,uint32 u32Size)
124 {
125 	uint32	i;
126 	sint8		s8Result = 0;
127 	for(i	 = 0 ; i < u32Size ; i++)
128 	{
129 		if(pu8Buff1[i] != pu8Buff2[i])
130 		{
131 			s8Result = 1;
132 			break;
133 		}
134 	}
135 	return s8Result;
136 }
137