• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Beken Corporation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <common/bk_include.h>
17 #include "bk_ef.h"
18 #include "base_64.h"
19 #include <os/mem.h>
20 #include <os/str.h>
21 
22 #if CONFIG_EASY_FLASH
bk_get_env(const char * key)23 char *bk_get_env(const char *key)
24 {
25 	return ef_get_env(key);
26 }
27 
bk_set_env(const char * key,const char * value)28 EfErrCode bk_set_env(const char *key, const char *value)
29 {
30 	return ef_set_env(key, value);
31 }
32 
bk_save_env(void)33 EfErrCode bk_save_env(void)
34 {
35 	return ef_save_env();
36 }
37 
bk_set_buf_env(const char * key,const char * buf,int len)38 EfErrCode bk_set_buf_env(const char *key, const char *buf, int len)
39 {
40 	EfErrCode result;
41 	unsigned char ret;
42 	unsigned char *ef_value;
43 	uint32_t buf_len;
44 	int out_len;
45 
46 	if ((0 == len) || (NULL == key)
47 		|| (NULL == buf))
48 		return EF_ENV_INIT_FAILED;
49 
50 	buf_len = base64_calc_encode_length(len) + 4;
51 	ef_value = os_zalloc(buf_len);
52 	if (NULL == ef_value)
53 		return EF_ENV_INIT_FAILED;
54 
55 	ret = base64_encode((unsigned char *)buf, len, &out_len, ef_value);
56 	if (0 == ret) {
57 		os_free(ef_value);
58 		ef_value = NULL;
59 		return EF_ENV_INIT_FAILED;
60 	}
61 
62 	result = ef_set_env(key, (char *)ef_value);
63 
64 	os_free(ef_value);
65 
66 	return result;
67 }
68 
bk_get_buf_env(const char * key,const char * buf,int len)69 EfErrCode bk_get_buf_env(const char *key, const char *buf, int len)
70 {
71 	unsigned char *out_ptr;
72 	unsigned char ret;
73 	char *ef_value;
74 	int out_buf_len, count;
75 	int out_len, value_len;
76 
77 	ef_value = ef_get_env(key);
78 	if (NULL == ef_value)
79 		return EF_ENV_NAME_ERR;
80 
81 	value_len = os_strlen(ef_value);
82 	if (0 == value_len)
83 		return EF_ENV_INIT_FAILED;
84 
85 	out_buf_len = base64_calc_decode_length((unsigned char *)ef_value, value_len);
86 	out_ptr = (unsigned char *)os_zalloc(out_buf_len + 4);
87 	if (NULL == out_ptr)
88 		return EF_ENV_INIT_FAILED;
89 
90 	ret = base64_decode((unsigned char *)ef_value, value_len, &out_len, out_ptr);
91 	if (0 == ret) {
92 		os_free(out_ptr);
93 		out_ptr = NULL;
94 		return EF_ENV_INIT_FAILED;
95 	}
96 
97 	if (out_len) {
98 		count = min(len, out_len);
99 		BK_ASSERT(len == out_len);
100 
101 		os_memcpy((void *)buf, out_ptr, count);
102 	}
103 
104 	os_free(out_ptr);
105 
106 	return EF_NO_ERR;
107 }
108 #endif // CONFIG_EASY_FLASH
109 
110 // eof
111 
112