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