1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, 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
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON 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
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28/*=============================================================================
29VideoCore 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
43extern "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
59VCOS_INLINE_DECL
60int vcos_strcasecmp(const char *s1, const char *s2);
61
62VCOS_INLINE_DECL
63int vcos_strncasecmp(const char *s1, const char *s2, size_t n);
64
65VCOSPRE_ int VCOSPOST_ vcos_vsnprintf(char *buf, size_t buflen, const char *fmt, va_list ap);
66
67VCOSPRE_ 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 */
73VCOSPRE_ 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 */
82VCOSPRE_ 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 */
97VCOSPRE_ 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
102VCOS_STATIC_INLINE
103int vcos_strlen(const char *s) { return (int)strlen(s); }
104
105VCOS_STATIC_INLINE
106int vcos_strcmp(const char *s1, const char *s2) { return strcmp(s1,s2); }
107
108VCOS_STATIC_INLINE
109int vcos_strncmp(const char *cs, const char *ct, size_t count) { return strncmp(cs, ct, count); }
110
111VCOS_STATIC_INLINE
112char *vcos_strcpy(char *dst, const char *src) { return strcpy(dst, src); }
113
114VCOS_STATIC_INLINE
115char *vcos_strncpy(char *dst, const char *src, size_t count) { return strncpy(dst, src, count); }
116
117VCOS_STATIC_INLINE
118void *vcos_memcpy(void *dst, const void *src, size_t n) { memcpy(dst, src, n); return dst; }
119
120VCOS_STATIC_INLINE
121void *vcos_memset(void *p, int c, size_t n) { return memset(p, c, n); }
122
123VCOS_STATIC_INLINE
124int 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