• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 #ifndef ANDROID_AUDIO_STRING_H
18 #define ANDROID_AUDIO_STRING_H
19 
20 #include <string.h>
21 #ifndef __ANDROID__
22 #include <cutils/memory.h>
23 #endif
24 
25 /** similar to strlcpy but also zero fills to end of string buffer, ensures no data leak
26     in parceled data sent over binder.*/
audio_utils_strlcpy_zerofill(char * dst,const char * src,size_t dst_size)27 inline size_t audio_utils_strlcpy_zerofill(char *dst, const char *src, size_t dst_size) {
28     const size_t srclen = strlcpy(dst, src, dst_size);
29     const size_t srclen_with_zero = srclen + 1; /* include zero termination in length. */
30     if (srclen_with_zero < dst_size) {
31         const size_t num_zeroes = dst_size - srclen_with_zero;
32         memset(dst + srclen_with_zero, 0 /* value */, num_zeroes); /* clear remaining buffer */
33     }
34     return srclen;
35 }
36 
37 #ifdef __cplusplus
38 
39 /** similar to audio_utils_strlcpy_zerofill for fixed size destination string. */
40 template <size_t size>
audio_utils_strlcpy_zerofill(char (& dst)[size],const char * src)41 inline size_t audio_utils_strlcpy_zerofill(char (&dst)[size], const char *src) {
42     return audio_utils_strlcpy_zerofill(dst, src, size);
43 }
44 
45 /** similar to strlcpy for fixed size destination string. */
46 template <size_t size>
audio_utils_strlcpy(char (& dst)[size],const char * src)47 inline size_t audio_utils_strlcpy(char (&dst)[size], const char *src) {
48     return strlcpy(dst, src, size);
49 }
50 
51 #endif // __cplusplus
52 
53 #endif // !ANDROID_AUDIO_STRING_H
54