• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) sendInternalFailureToHost(msg)
23 
24 #else
25 
26 #include <gtest/gtest.h>
27 #define REPORT_INTERNAL_ERROR(msg) FAIL() << msg
28 
29 #endif
30 
31 namespace nanoapp_testing {
32 
memset(void * mem,int val,size_t count)33 void memset(void *mem, int val, size_t count) {
34   uint8_t *bytes = static_cast<uint8_t *>(mem);
35   for (size_t i = 0; i < count; i++) {
36     bytes[i] = static_cast<uint8_t>(val);
37   }
38 }
39 
memcpy(void * d,const void * s,size_t bytes)40 void memcpy(void *d, const void *s, size_t bytes) {
41   uint8_t *dst = static_cast<uint8_t *>(d);
42   const uint8_t *src = static_cast<const uint8_t *>(s);
43   for (size_t i = 0; i < bytes; i++) {
44     dst[i] = src[i];
45   }
46 }
47 
strlen(char const * str)48 size_t strlen(char const *str) {
49   size_t ret = 0;
50   for (; str[ret] != '\0'; ret++) {
51   }
52   return ret;
53 }
54 
strncpy(char * dest,const char * src,size_t len)55 char *strncpy(char *dest, const char *src, size_t len) {
56   size_t i;
57   for (i = 0; (i < len) && (src[i] != '\0'); i++) {
58     dest[i] = src[i];
59   }
60   for (; i < len; i++) {
61     dest[i] = '\0';
62   }
63   return dest;
64 }
65 
uint32ToHexAscii(char * buffer,size_t buffer_len,uint32_t value)66 void uint32ToHexAscii(char *buffer, size_t buffer_len, uint32_t value) {
67   constexpr char lookup[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
68                                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
69   if (buffer_len < kUint32ToHexAsciiBufferMinLen) {
70     // We chose not to send our buffer_len here, as that would invoke
71     // another call to this method and risk infinite recursion if something
72     // was really screwed up.
73     REPORT_INTERNAL_ERROR("uint32ToHexAscii got undersized buffer_len");
74     return;
75   }
76   buffer[0] = '0';
77   buffer[1] = 'x';
78   for (size_t i = 0, shift = 28; i < 8; i++, shift -= 4) {
79     buffer[2 + i] = lookup[(value >> shift) & 0xF];
80   }
81 }
82 
83 }  // namespace nanoapp_testing
84