XCP_SIM
xcp_util.c
1 /*
2  * BlueParrot XCP
3  *
4  * (C) 2007-2022 by Christoph Schueler <github.com/Christoph2,
5  * cpu12.gems@googlemail.com>
6  *
7  * All Rights Reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * s. FLOSS-EXCEPTION.txt
24  */
25 
27 #include "xcp_util.h"
28 
29 #include "xcp_macros.h"
32 void XcpUtl_MemCopy(/*@out@*/ void *dst, /*@in@*/ void const *src,
33  uint32_t len) {
34  uint8_t *pd = (uint8_t *)dst;
35  uint8_t const *ps = (uint8_t const *)src;
36 
37  XCP_ASSERT(dst != XCP_NULL);
38  //XCP_ASSERT(pd >= ps + len || ps >= pd + len);
39  XCP_ASSERT(len != 0UL);
40 
41  while (len--) {
42  *pd++ = *ps++;
43  }
44 }
45 
46 void XcpUtl_MemSet(/*@out@*/ void *dest, uint8_t fill_char, uint32_t len) {
47  uint8_t *p = (uint8_t *)dest;
48 
49  XCP_ASSERT(XCP_NULL != dest);
50 
51  while (len--) {
52  *p++ = fill_char;
53  }
54 }
55 
56 bool XcpUtl_MemCmp(/*@in@*/ void const *lhs, /*@in@*/ void const *rhs,
57  uint32_t len) {
58  uint8_t const *pl = (uint8_t const *)lhs;
59  uint8_t const *pr = (uint8_t const *)rhs;
60 
61  XCP_ASSERT(XCP_NULL != lhs);
62  XCP_ASSERT(XCP_NULL != rhs);
63 
64  if (len == UINT32(0)) {
65  return XCP_FALSE;
66  }
67  while ((*pl++ == *pr++) && (len != UINT32(0))) {
68  --len;
69  }
70  return (bool)(len == UINT32(0));
71 }
72 
73 #if XCP_BUILD_TYPE == XCP_DEBUG_BUILD
74 void XcpUtl_Hexdump(/*@in@*/ uint8_t const *buf, uint16_t sz) {
75  uint16_t idx;
76 
77  for (idx = UINT16(0); idx < sz; ++idx) {
78  DBG_PRINT2("%02X ", buf[idx]);
79  }
80  DBG_PRINT1("\n\r");
81 }
82 
83 void XcpUtl_Itoa(uint32_t value, uint8_t base, uint8_t *buf) {
84  uint8_t mod;
85  uint8_t pos = (uint8_t)0x00, swap_pos = (uint8_t)0x00;
86  uint8_t ch;
87 
88  /* ASSERT(buf != (void *)NULL); */
89  if (((int32_t)value) < 0L && base == (uint8_t)10) {
90  value = (uint32_t)((int32_t)value * -1L);
91  buf[0] = '-';
92  swap_pos = 1;
93  pos = 1;
94  }
95 
96  if (value == 0L) {
97  buf[pos++] = '0';
98  }
99 
100  while (value) {
101  mod = value % base;
102  if (mod < 10) {
103  buf[pos++] = '0' + mod;
104  } else {
105  buf[pos++] = 'A' + mod - (uint8_t)10;
106  }
107  value /= base;
108  }
109  buf[pos--] = '\0';
110  while (pos > swap_pos) {
111  ch = buf[swap_pos];
112  buf[swap_pos] = buf[pos];
113  buf[pos] = ch;
114  swap_pos++;
115  pos--;
116  }
117 }
118 #endif