]>
Commit | Line | Data |
---|---|---|
a49d2536 AC |
1 | /* |
2 | * Support for Intel Camera Imaging ISP subsystem. | |
3 | * Copyright (c) 2015, Intel Corporation. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms and conditions of the GNU General Public License, | |
7 | * version 2, as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | */ | |
14 | ||
15 | #ifndef __STRING_SUPPORT_H_INCLUDED__ | |
16 | #define __STRING_SUPPORT_H_INCLUDED__ | |
17 | #include <platform_support.h> | |
18 | #include <type_support.h> | |
a49d2536 AC |
19 | |
20 | #if !defined(_MSC_VER) | |
21 | /* | |
22 | * For all non microsoft cases, we need the following functions | |
23 | */ | |
24 | ||
25 | ||
d929fb4e | 26 | /* @brief Copy from src_buf to dest_buf. |
a49d2536 AC |
27 | * |
28 | * @param[out] dest_buf. Destination buffer to copy to | |
29 | * @param[in] dest_size. The size of the destination buffer in bytes | |
30 | * @param[in] src_buf. The source buffer | |
31 | * @param[in] src_size. The size of the source buffer in bytes | |
32 | * @return 0 on success, error code on failure | |
33 | * @return EINVAL on Invalid arguments | |
34 | * @return ERANGE on Destination size too small | |
35 | */ | |
4c5133f5 | 36 | static inline int memcpy_s( |
a49d2536 AC |
37 | void* dest_buf, |
38 | size_t dest_size, | |
39 | const void* src_buf, | |
40 | size_t src_size) | |
41 | { | |
42 | if ((src_buf == NULL) || (dest_buf == NULL)) { | |
43 | /* Invalid arguments*/ | |
44 | return EINVAL; | |
45 | } | |
46 | ||
47 | if ((dest_size < src_size) || (src_size == 0)) { | |
48 | /* Destination too small*/ | |
49 | return ERANGE; | |
50 | } | |
51 | ||
52 | memcpy(dest_buf, src_buf, src_size); | |
53 | return 0; | |
54 | } | |
55 | ||
d929fb4e | 56 | /* @brief Get the length of the string, excluding the null terminator |
a49d2536 AC |
57 | * |
58 | * @param[in] src_str. The source string | |
59 | * @param[in] max_len. Look only for max_len bytes in the string | |
60 | * @return Return the string length excluding null character | |
61 | * @return Return max_len if no null character in the first max_len bytes | |
62 | * @return Returns 0 if src_str is NULL | |
63 | */ | |
64 | static size_t strnlen_s( | |
65 | const char* src_str, | |
66 | size_t max_len) | |
67 | { | |
68 | size_t ix; | |
69 | if (src_str == NULL) { | |
70 | /* Invalid arguments*/ | |
71 | return 0; | |
72 | } | |
73 | ||
d1fec5bd DC |
74 | for (ix = 0; ix < max_len && src_str[ix] != '\0'; ix++) |
75 | ; | |
a49d2536 AC |
76 | |
77 | /* On Error, it will return src_size == max_len*/ | |
78 | return ix; | |
79 | } | |
80 | ||
d929fb4e | 81 | /* @brief Copy string from src_str to dest_str |
a49d2536 AC |
82 | * |
83 | * @param[out] dest_str. Destination buffer to copy to | |
84 | * @param[in] dest_size. The size of the destination buffer in bytes | |
85 | * @param[in] src_str. The source buffer | |
86 | * @param[in] src_size. The size of the source buffer in bytes | |
87 | * @return Returns 0 on success | |
88 | * @return Returns EINVAL on invalid arguments | |
89 | * @return Returns ERANGE on destination size too small | |
90 | */ | |
4c5133f5 | 91 | static inline int strncpy_s( |
a49d2536 AC |
92 | char* dest_str, |
93 | size_t dest_size, | |
94 | const char* src_str, | |
95 | size_t src_size) | |
96 | { | |
97 | size_t len; | |
98 | if (dest_str == NULL) { | |
99 | /* Invalid arguments*/ | |
100 | return EINVAL; | |
101 | } | |
102 | ||
103 | if ((src_str == NULL) || (dest_size == 0)) { | |
104 | /* Invalid arguments*/ | |
105 | dest_str[0] = '\0'; | |
106 | return EINVAL; | |
107 | } | |
108 | ||
109 | len = strnlen_s(src_str, src_size); | |
110 | ||
111 | if (len >= dest_size) { | |
112 | /* Destination too small*/ | |
113 | dest_str[0] = '\0'; | |
114 | return ERANGE; | |
115 | } | |
116 | ||
117 | /* dest_str is big enough for the len */ | |
118 | strncpy(dest_str, src_str, len); | |
c32e3d1b | 119 | dest_str[len] = '\0'; |
a49d2536 AC |
120 | return 0; |
121 | } | |
122 | ||
d929fb4e | 123 | /* @brief Copy string from src_str to dest_str |
a49d2536 AC |
124 | * |
125 | * @param[out] dest_str. Destination buffer to copy to | |
126 | * @param[in] dest_size. The size of the destination buffer in bytes | |
127 | * @param[in] src_str. The source buffer | |
128 | * @return Returns 0 on success | |
129 | * @return Returns EINVAL on invalid arguments | |
130 | * @return Returns ERANGE on destination size too small | |
131 | */ | |
4c5133f5 | 132 | static inline int strcpy_s( |
a49d2536 AC |
133 | char* dest_str, |
134 | size_t dest_size, | |
135 | const char* src_str) | |
136 | { | |
137 | size_t len; | |
138 | if (dest_str == NULL) { | |
139 | /* Invalid arguments*/ | |
140 | return EINVAL; | |
141 | } | |
142 | ||
143 | if ((src_str == NULL) || (dest_size == 0)) { | |
144 | /* Invalid arguments*/ | |
145 | dest_str[0] = '\0'; | |
146 | return EINVAL; | |
147 | } | |
148 | ||
149 | len = strnlen_s(src_str, dest_size); | |
150 | ||
151 | if (len >= dest_size) { | |
152 | /* Destination too small*/ | |
153 | dest_str[0] = '\0'; | |
154 | return ERANGE; | |
155 | } | |
156 | ||
157 | /* dest_str is big enough for the len */ | |
158 | strncpy(dest_str, src_str, len); | |
c32e3d1b | 159 | dest_str[len] = '\0'; |
a49d2536 AC |
160 | return 0; |
161 | } | |
162 | ||
163 | #endif /*!defined(_MSC_VER)*/ | |
164 | ||
165 | #endif /* __STRING_SUPPORT_H_INCLUDED__ */ |