GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / media / atomisp / pci / atomisp2 / css2400 / hive_isp_css_include / string_support.h
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>
19 #include <storage_class.h>
20
21 #if !defined(_MSC_VER)
22 /*
23  * For all non microsoft cases, we need the following functions
24  */
25
26
27 /** @brief Copy from src_buf to dest_buf.
28  *
29  * @param[out] dest_buf. Destination buffer to copy to
30  * @param[in]  dest_size. The size of the destination buffer in bytes
31  * @param[in]  src_buf. The source buffer
32  * @param[in]  src_size. The size of the source buffer in bytes
33  * @return     0 on success, error code on failure
34  * @return     EINVAL on Invalid arguments
35  * @return     ERANGE on Destination size too small
36  */
37 STORAGE_CLASS_INLINE int memcpy_s(
38         void* dest_buf,
39         size_t dest_size,
40         const void* src_buf,
41         size_t src_size)
42 {
43         if ((src_buf == NULL) || (dest_buf == NULL)) {
44                 /* Invalid arguments*/
45                 return EINVAL;
46         }
47
48         if ((dest_size < src_size) || (src_size == 0)) {
49                 /* Destination too small*/
50                 return ERANGE;
51         }
52
53         memcpy(dest_buf, src_buf, src_size);
54         return 0;
55 }
56
57 /** @brief Get the length of the string, excluding the null terminator
58  *
59  * @param[in]  src_str. The source string
60  * @param[in]  max_len. Look only for max_len bytes in the string
61  * @return     Return the string length excluding null character
62  * @return     Return max_len if no null character in the first max_len bytes
63  * @return     Returns 0 if src_str is NULL
64  */
65 static size_t strnlen_s(
66         const char* src_str,
67         size_t max_len)
68 {
69         size_t ix;
70         if (src_str == NULL) {
71                 /* Invalid arguments*/
72                 return 0;
73         }
74
75         for (ix = 0; ix < max_len && src_str[ix] != '\0'; ix++)
76                 ;
77
78         /* On Error, it will return src_size == max_len*/
79         return ix;
80 }
81
82 /** @brief Copy string from src_str to dest_str
83  *
84  * @param[out] dest_str. Destination buffer to copy to
85  * @param[in]  dest_size. The size of the destination buffer in bytes
86  * @param[in]  src_str. The source buffer
87  * @param[in]  src_size. The size of the source buffer in bytes
88  * @return     Returns 0 on success
89  * @return     Returns EINVAL on invalid arguments
90  * @return     Returns ERANGE on destination size too small
91  */
92 STORAGE_CLASS_INLINE int strncpy_s(
93         char* dest_str,
94         size_t dest_size,
95         const char* src_str,
96         size_t src_size)
97 {
98         size_t len;
99         if (dest_str == NULL) {
100                 /* Invalid arguments*/
101                 return EINVAL;
102         }
103
104         if ((src_str == NULL) || (dest_size == 0)) {
105                 /* Invalid arguments*/
106                 dest_str[0] = '\0';
107                 return EINVAL;
108         }
109
110         len = strnlen_s(src_str, src_size);
111
112         if (len >= dest_size) {
113                 /* Destination too small*/
114                 dest_str[0] = '\0';
115                 return ERANGE;
116         }
117
118         /* dest_str is big enough for the len */
119         strncpy(dest_str, src_str, len);
120         dest_str[len] = '\0';
121         return 0;
122 }
123
124 /** @brief Copy string from src_str to dest_str
125  *
126  * @param[out] dest_str. Destination buffer to copy to
127  * @param[in]  dest_size. The size of the destination buffer in bytes
128  * @param[in]  src_str. The source buffer
129  * @return     Returns 0 on success
130  * @return     Returns EINVAL on invalid arguments
131  * @return     Returns ERANGE on destination size too small
132  */
133 STORAGE_CLASS_INLINE int strcpy_s(
134         char* dest_str,
135         size_t dest_size,
136         const char* src_str)
137 {
138         size_t len;
139         if (dest_str == NULL) {
140                 /* Invalid arguments*/
141                 return EINVAL;
142         }
143
144         if ((src_str == NULL) || (dest_size == 0)) {
145                 /* Invalid arguments*/
146                 dest_str[0] = '\0';
147                 return EINVAL;
148         }
149
150         len = strnlen_s(src_str, dest_size);
151
152         if (len >= dest_size) {
153                 /* Destination too small*/
154                 dest_str[0] = '\0';
155                 return ERANGE;
156         }
157
158         /* dest_str is big enough for the len */
159         strncpy(dest_str, src_str, len);
160         dest_str[len] = '\0';
161         return 0;
162 }
163
164 #endif /*!defined(_MSC_VER)*/
165
166 #endif /* __STRING_SUPPORT_H_INCLUDED__ */