| 1 | /* | 
|---|
| 2 | Copyright (c) 2012, Broadcom Europe Ltd | 
|---|
| 3 | All rights reserved. | 
|---|
| 4 |  | 
|---|
| 5 | Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | modification, are permitted provided that the following conditions are met: | 
|---|
| 7 | * Redistributions of source code must retain the above copyright | 
|---|
| 8 | notice, this list of conditions and the following disclaimer. | 
|---|
| 9 | * Redistributions in binary form must reproduce the above copyright | 
|---|
| 10 | notice, this list of conditions and the following disclaimer in the | 
|---|
| 11 | documentation and/or other materials provided with the distribution. | 
|---|
| 12 | * Neither the name of the copyright holder nor the | 
|---|
| 13 | names of its contributors may be used to endorse or promote products | 
|---|
| 14 | derived from this software without specific prior written permission. | 
|---|
| 15 |  | 
|---|
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | 
|---|
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 
|---|
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
|---|
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY | 
|---|
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 
|---|
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 
|---|
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 
|---|
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
|---|
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 26 | */ | 
|---|
| 27 |  | 
|---|
| 28 | /*============================================================================= | 
|---|
| 29 | VideoCore OS Abstraction Layer - public header file | 
|---|
| 30 | =============================================================================*/ | 
|---|
| 31 |  | 
|---|
| 32 | #ifndef VCOS_STRING_H | 
|---|
| 33 | #define VCOS_STRING_H | 
|---|
| 34 |  | 
|---|
| 35 | /** | 
|---|
| 36 | * \file | 
|---|
| 37 | * | 
|---|
| 38 | * String functions. | 
|---|
| 39 | * | 
|---|
| 40 | */ | 
|---|
| 41 |  | 
|---|
| 42 | #ifdef __cplusplus | 
|---|
| 43 | extern "C"{ | 
|---|
| 44 | #endif | 
|---|
| 45 |  | 
|---|
| 46 | #include "interface/vcos/vcos_types.h" | 
|---|
| 47 | #include "vcos.h" | 
|---|
| 48 |  | 
|---|
| 49 | #ifdef __KERNEL__ | 
|---|
| 50 | #include <linux/string.h> | 
|---|
| 51 | #else | 
|---|
| 52 | #include <string.h> | 
|---|
| 53 | #endif | 
|---|
| 54 |  | 
|---|
| 55 | /** Case insensitive string comparison. | 
|---|
| 56 | * | 
|---|
| 57 | */ | 
|---|
| 58 |  | 
|---|
| 59 | VCOS_INLINE_DECL | 
|---|
| 60 | int vcos_strcasecmp(const char *s1, const char *s2); | 
|---|
| 61 |  | 
|---|
| 62 | VCOS_INLINE_DECL | 
|---|
| 63 | int vcos_strncasecmp(const char *s1, const char *s2, size_t n); | 
|---|
| 64 |  | 
|---|
| 65 | VCOSPRE_ int VCOSPOST_ vcos_vsnprintf(char *buf, size_t buflen, const char *fmt, va_list ap); | 
|---|
| 66 |  | 
|---|
| 67 | VCOSPRE_ int VCOSPOST_ vcos_snprintf(char *buf, size_t buflen, const char *fmt, ...); | 
|---|
| 68 |  | 
|---|
| 69 | /** Like vsnprintf, except it places the output at the specified offset. | 
|---|
| 70 | * Output is truncated to fit in buflen bytes, and is guaranteed to be NUL-terminated. | 
|---|
| 71 | * Returns the string length before/without truncation. | 
|---|
| 72 | */ | 
|---|
| 73 | VCOSPRE_ size_t VCOSPOST_ vcos_safe_vsprintf(char *buf, size_t buflen, size_t offset, const char *fmt, va_list ap); | 
|---|
| 74 |  | 
|---|
| 75 | #define VCOS_SAFE_VSPRINTF(buf, offset, fmt, ap) \ | 
|---|
| 76 | vcos_safe_vsprintf(buf, sizeof(buf) + ((char (*)[sizeof(buf)])buf - &(buf)), offset, fmt, ap) | 
|---|
| 77 |  | 
|---|
| 78 | /** Like snprintf, except it places the output at the specified offset. | 
|---|
| 79 | * Output is truncated to fit in buflen bytes, and is guaranteed to be NUL-terminated. | 
|---|
| 80 | * Returns the string length before/without truncation. | 
|---|
| 81 | */ | 
|---|
| 82 | VCOSPRE_ size_t VCOSPOST_ vcos_safe_sprintf(char *buf, size_t buflen, size_t offset, const char *fmt, ...); | 
|---|
| 83 |  | 
|---|
| 84 | /* The Metaware compiler currently has a bug in its variadic macro handling which | 
|---|
| 85 | causes it to append a spurious command to the end of its __VA_ARGS__ data. | 
|---|
| 86 | Do not use until this has been fixed (and this comment has been deleted). */ | 
|---|
| 87 |  | 
|---|
| 88 | #define VCOS_SAFE_SPRINTF(buf, offset, ...) \ | 
|---|
| 89 | vcos_safe_sprintf(buf, sizeof(buf) + ((char (*)[sizeof(buf)])buf - &(buf)), offset, __VA_ARGS__) | 
|---|
| 90 |  | 
|---|
| 91 | /** Copies string src to dst at the specified offset. | 
|---|
| 92 | * Output is truncated to fit in dstlen bytes, i.e. the string is at most | 
|---|
| 93 | * (buflen - 1) characters long. Unlike strncpy, exactly one NUL is written | 
|---|
| 94 | * to dst, which is always NUL-terminated. | 
|---|
| 95 | * Returns the string length before/without truncation. | 
|---|
| 96 | */ | 
|---|
| 97 | VCOSPRE_ size_t VCOSPOST_ vcos_safe_strcpy(char *dst, const char *src, size_t dstlen, size_t offset); | 
|---|
| 98 |  | 
|---|
| 99 | #define VCOS_SAFE_STRCPY(dst, src, offset) \ | 
|---|
| 100 | vcos_safe_strcpy(dst, src, sizeof(dst) + ((char (*)[sizeof(dst)])dst - &(dst)), offset) | 
|---|
| 101 |  | 
|---|
| 102 | VCOS_STATIC_INLINE | 
|---|
| 103 | int vcos_strlen(const char *s) { return (int)strlen(s); } | 
|---|
| 104 |  | 
|---|
| 105 | VCOS_STATIC_INLINE | 
|---|
| 106 | int vcos_strcmp(const char *s1, const char *s2) { return strcmp(s1,s2); } | 
|---|
| 107 |  | 
|---|
| 108 | VCOS_STATIC_INLINE | 
|---|
| 109 | int vcos_strncmp(const char *cs, const char *ct, size_t count) { return strncmp(cs, ct, count); } | 
|---|
| 110 |  | 
|---|
| 111 | VCOS_STATIC_INLINE | 
|---|
| 112 | char *vcos_strcpy(char *dst, const char *src) { return strcpy(dst, src); } | 
|---|
| 113 |  | 
|---|
| 114 | VCOS_STATIC_INLINE | 
|---|
| 115 | char *vcos_strncpy(char *dst, const char *src, size_t count) { return strncpy(dst, src, count); } | 
|---|
| 116 |  | 
|---|
| 117 | VCOS_STATIC_INLINE | 
|---|
| 118 | void *vcos_memcpy(void *dst, const void *src, size_t n) {  memcpy(dst, src, n);  return dst;  } | 
|---|
| 119 |  | 
|---|
| 120 | VCOS_STATIC_INLINE | 
|---|
| 121 | void *vcos_memset(void *p, int c, size_t n) { return memset(p, c, n); } | 
|---|
| 122 |  | 
|---|
| 123 | VCOS_STATIC_INLINE | 
|---|
| 124 | int vcos_memcmp(const void *ptr1, const void *ptr2, size_t count) { return memcmp(ptr1, ptr2, count); } | 
|---|
| 125 |  | 
|---|
| 126 | #ifdef __cplusplus | 
|---|
| 127 | } | 
|---|
| 128 | #endif | 
|---|
| 129 | #endif | 
|---|
| 130 |  | 
|---|