1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <shared/nano_string.h>
18
19 #ifndef INTERNAL_TESTING
20
21 #include <shared/send_message.h>
22 #define REPORT_INTERNAL_ERROR(msg) \
23 sendInternalFailureToHost(msg)
24
25 #else
26
27 #include <gtest/gtest.h>
28 #define REPORT_INTERNAL_ERROR(msg) FAIL() << msg
29
30 #endif
31
32
33 namespace nanoapp_testing {
34
memset(void * mem,int val,size_t count)35 void memset(void *mem, int val, size_t count) {
36 uint8_t *bytes = static_cast<uint8_t*>(mem);
37 for (size_t i = 0; i < count; i++) {
38 bytes[i] = val;
39 }
40 }
41
memcpy(void * d,const void * s,size_t bytes)42 void memcpy(void *d, const void *s, size_t bytes) {
43 uint8_t *dst = static_cast<uint8_t*>(d);
44 const uint8_t *src = static_cast<const uint8_t*>(s);
45 for (size_t i = 0; i < bytes; i++) {
46 dst[i] = src[i];
47 }
48 }
49
strlen(char const * str)50 size_t strlen(char const *str) {
51 size_t ret = 0;
52 for (; str[ret] != '\0'; ret++) {}
53 return ret;
54 }
55
strncpy(char * dest,const char * src,size_t len)56 char *strncpy(char *dest, const char *src, size_t len) {
57 size_t i;
58 for (i = 0; (i < len) && (src[i] != '\0'); i++) {
59 dest[i] = src[i];
60 }
61 for (; i < len; i++) {
62 dest[i] = '\0';
63 }
64 return dest;
65 }
66
uint32ToHexAscii(char * buffer,size_t buffer_len,uint32_t value)67 void uint32ToHexAscii(char *buffer, size_t buffer_len, uint32_t value) {
68 constexpr char lookup[16] = {
69 '0', '1', '2', '3', '4', '5', '6', '7',
70 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
71 if (buffer_len < kUint32ToHexAsciiBufferMinLen) {
72 // We chose not to send our buffer_len here, as that would invoke
73 // another call to this method and risk infinite recursion if something
74 // was really screwed up.
75 REPORT_INTERNAL_ERROR("uint32ToHexAscii got undersized buffer_len");
76 return;
77 }
78 buffer[0] = '0';
79 buffer[1] = 'x';
80 for (size_t i = 0, shift = 28; i < 8; i++, shift -= 4) {
81 buffer[2 + i] = lookup[(value >> shift) & 0xF];
82 }
83 }
84
85 } // namespace nanoapp_testing
86