1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "avb_sysdeps.h"
31
avb_memcmp(const void * src1,const void * src2,size_t n)32 int avb_memcmp(const void* src1, const void* src2, size_t n) {
33 return memcmp(src1, src2, n);
34 }
35
avb_memcpy(void * dest,const void * src,size_t n)36 void* avb_memcpy(void* dest, const void* src, size_t n) {
37 return memcpy(dest, src, n);
38 }
39
avb_memset(void * dest,const int c,size_t n)40 void* avb_memset(void* dest, const int c, size_t n) {
41 return memset(dest, c, n);
42 }
43
avb_strcmp(const char * s1,const char * s2)44 int avb_strcmp(const char* s1, const char* s2) {
45 return strcmp(s1, s2);
46 }
47
avb_strncmp(const char * s1,const char * s2,size_t n)48 int avb_strncmp(const char* s1, const char* s2, size_t n) {
49 return strncmp(s1, s2, n);
50 }
51
avb_strlen(const char * str)52 size_t avb_strlen(const char* str) {
53 return strlen(str);
54 }
55
avb_abort(void)56 void avb_abort(void) {
57 abort();
58 }
59
get_log_stream()60 static FILE* get_log_stream() {
61 #ifdef USE_KMSG_AS_LOG_TARGET
62 static FILE* fp = NULL;
63
64 if (fp == NULL) {
65 fp = fopen("/dev/kmsg", "ae");
66 if (fp == NULL || setvbuf(fp, NULL, _IONBF, 0) != 0) {
67 fp = stderr;
68 }
69 }
70
71 return fp;
72 #else
73 return stderr;
74 #endif
75 }
76
avb_printf(const char * fmt,...)77 void avb_printf(const char* fmt, ...) {
78 va_list ap;
79 va_start(ap, fmt);
80 vfprintf(get_log_stream(), fmt, ap);
81 va_end(ap);
82 }
83
avb_print(const char * message)84 void avb_print(const char* message) {
85 fprintf(stderr, "%s", message);
86 }
87
avb_printv(const char * message,...)88 void avb_printv(const char* message, ...) {
89 va_list ap;
90 const char* m;
91
92 va_start(ap, message);
93 for (m = message; m != NULL; m = va_arg(ap, const char*)) {
94 fprintf(stderr, "%s", m);
95 }
96 va_end(ap);
97 }
98
avb_malloc_(size_t size)99 void* avb_malloc_(size_t size) {
100 return malloc(size);
101 }
102
avb_free(void * ptr)103 void avb_free(void* ptr) {
104 free(ptr);
105 }
106
avb_div_by_10(uint64_t * dividend)107 uint32_t avb_div_by_10(uint64_t* dividend) {
108 uint32_t rem = (uint32_t)(*dividend % 10);
109 *dividend /= 10;
110 return rem;
111 }
112