blob: e833820ab33f29848fe8ccecc4e83df44170c1de [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -08001/*
2 * Copyright (C) 2008 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#ifndef _ADB_UTILS_H
17#define _ADB_UTILS_H
18
Dan Albertd808d212015-02-18 10:21:54 -080019#ifdef __cplusplus
20extern "C" {
21#endif
22
Doug Zongker9270a202012-01-09 15:16:13 -080023/* bounded buffer functions */
24
25/* all these functions are used to append data to a bounded buffer.
26 *
27 * after each operation, the buffer is guaranteed to be zero-terminated,
28 * even in the case of an overflow. they all return the new buffer position
29 * which allows one to use them in succession, only checking for overflows
30 * at the end. For example:
31 *
32 * BUFF_DECL(temp,p,end,1024);
33 * char* p;
34 *
35 * p = buff_addc(temp, end, '"');
36 * p = buff_adds(temp, end, string);
37 * p = buff_addc(temp, end, '"');
38 *
39 * if (p >= end) {
40 * overflow detected. note that 'temp' is
41 * zero-terminated for safety.
42 * }
43 * return strdup(temp);
44 */
45
46/* tries to add a character to the buffer, in case of overflow
47 * this will only write a terminating zero and return buffEnd.
48 */
49char* buff_addc (char* buff, char* buffEnd, int c);
50
51/* tries to add a string to the buffer */
52char* buff_adds (char* buff, char* buffEnd, const char* s);
53
54/* tries to add a bytes to the buffer. the input can contain zero bytes,
55 * but a terminating zero will always be appended at the end anyway
56 */
57char* buff_addb (char* buff, char* buffEnd, const void* data, int len);
58
59/* tries to add a formatted string to a bounded buffer */
60char* buff_add (char* buff, char* buffEnd, const char* format, ... );
61
62/* convenience macro used to define a bounded buffer, as well as
63 * a 'cursor' and 'end' variables all in one go.
64 *
65 * note: this doesn't place an initial terminating zero in the buffer,
66 * you need to use one of the buff_ functions for this. or simply
67 * do _cursor[0] = 0 manually.
68 */
69#define BUFF_DECL(_buff,_cursor,_end,_size) \
70 char _buff[_size], *_cursor=_buff, *_end = _cursor + (_size)
71
Dan Albertd808d212015-02-18 10:21:54 -080072#ifdef __cplusplus
73}
74#endif
75
Doug Zongker9270a202012-01-09 15:16:13 -080076#endif /* _ADB_UTILS_H */