1 /*
2  * Copyright (c) 2019, The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * 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
10  *      copyright notice, this list of conditions and the following
11  *      disclaimer in the documentation and/or other materials provided
12  *      with the distribution.
13  *    * Neither the name of The Linux Foundation nor the names of its
14  *      contributors may be used to endorse or promote products derived
15  *      from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 /*==============================================================================
30 
31 FILE:  AEEBufBound.c
32 
33 SERVICES:
34         AEEBufBound APIs
35 
36 GENERAL DESCRIPTION:
37         AEEBufBound provides a "bounded buffer" API that facilitates
38           measuring strings or character output.  It's design accomodates
39           the implementation of functions that can have the same exact logic
40           for measuring and outputting char buffer content.
41 
42 REVISION HISTORY:
43         Sun Mar 06 11:23:10 2005 Created
44 
45 ==============================================================================*/
46 #include <limits.h>
47 #include "AEEBufBound.h"
48 #include "AEEstd.h"
49 
50 // Note on bounds-checking logic and saturation:
51 //
52 // Simple pointer comparisons are not adequate for bounds checking.  pcBuf
53 // and pcEnd are assumed to be valid pointers in the address space.  But
54 // pcWrite is not ... it is a theoretical value that can exceed pcEnd, and
55 // may in fact wrap around the end of the address space.  In that case the
56 // test for (pcWrite < pcEnd) will yield TRUE, although pcWrite is outside
57 // the buffer.  Use (pcEnd-pcWrite) > 0 to be accurate.
58 //
59 // In order to ensure this works in all cases, we need to avoid integer
60 // overflows.  We do this by restricting pcWrite to the range
61 // [pcBuf..pcBuf+INT_MAX].  The ensures that pcWrite-pcBuf and pcWrite-pcBuf
62 // will always be valid integers.  It also allows us to ensure that
63 // BufBound_Wrote() will not return wildly misleading results.
64 //
65 //                                            PCSAT
66 //    pcBuf               pcEnd            pcBuf+MAXINT
67 //      |-------------------| . . . . . . . . . |
68 //                   ^            ^
69 //    pcWrite:      (a)          (b)
70 //
71 
72 #define PCSAT(me)   ((me)->pcBuf + INT_MAX)
73 
74 
75 // Advance me->pcWrite, saturating.
76 //
77 // On entry:
78 //    *pnLen = number of bytes to be written (non-negative)
79 // On exit:
80 //    return value = where to write (pointer into the buffer)
81 //    *pnLen       = number of bytes to write
82 //
83 static char *
BufBound_ValidateWrite(BufBound * me,int * pnLen)84 BufBound_ValidateWrite(BufBound *me, int *pnLen)
85 {
86    int nLen = *pnLen;
87    char *pcWrite = me->pcWrite;
88    int nMaxCopy = me->pcEnd - pcWrite;        // could be negative!
89 
90    if ( nMaxCopy < nLen ) {
91       // Must check PCSAT to validate advance
92       int nMaxAdvance = PCSAT(me) - pcWrite;      // max amount to advance
93 
94       if (nLen > nMaxAdvance) {
95          nLen = nMaxAdvance;
96       }
97       if (nMaxCopy < 0) {
98          nMaxCopy = 0;
99       }
100    } else {
101       // Simple case: all fits in the buffer
102       nMaxCopy = nLen;
103    }
104 
105    *pnLen = nMaxCopy;
106    me->pcWrite = pcWrite + nLen;
107    return pcWrite;
108 }
109 
BufBound_Write(BufBound * me,const char * pc,int nLen)110 void BufBound_Write(BufBound *me, const char *pc, int nLen)
111 {
112    if (nLen > 0) {
113       char *pcDest = BufBound_ValidateWrite(me, &nLen);
114 
115       while (--nLen >= 0) {
116          pcDest[nLen] = pc[nLen];
117       }
118    }
119 }
120 
BufBound_Putnc(BufBound * me,char c,int nLen)121 void BufBound_Putnc(BufBound *me, char c, int nLen)
122 {
123    if (nLen > 0) {
124       char *pcDest = BufBound_ValidateWrite(me, &nLen);
125 
126       while (--nLen >= 0) {
127          pcDest[nLen] = c;
128       }
129    }
130 }
131 
BufBound_Advance(BufBound * me,int nLen)132 void BufBound_Advance(BufBound *me, int nLen)
133 {
134    uint32 uOffset = (uint32)((me->pcWrite - me->pcBuf) + nLen);
135 
136    if (uOffset > INT_MAX) {
137       uOffset = INT_MAX;
138       if (nLen < 0) {
139          uOffset = 0;
140       }
141    }
142    me->pcWrite = me->pcBuf + uOffset;
143 }
144 
BufBound_Init(BufBound * me,char * pBuf,int nLen)145 void BufBound_Init(BufBound *me, char *pBuf, int nLen)
146 {
147    if (nLen < 0) {
148       nLen = 0;
149    }
150    me->pcWrite = me->pcBuf = pBuf;
151    me->pcEnd   = pBuf + nLen;
152 }
153 
BufBound_Putc(BufBound * me,char c)154 void BufBound_Putc(BufBound *me, char c)
155 {
156    if ( (me->pcEnd - me->pcWrite) > 0) {
157       *me->pcWrite++ = c;
158    } else if (me->pcWrite != PCSAT(me)) {
159       ++me->pcWrite;
160    }
161 }
162 
BufBound_ForceNullTerm(BufBound * me)163 void BufBound_ForceNullTerm(BufBound *me)
164 {
165    if ( (me->pcEnd - me->pcWrite) > 0) {
166       *me->pcWrite++ = '\0';
167    } else {
168       if (me->pcWrite != PCSAT(me)) {
169          ++me->pcWrite;
170       }
171       // ensure null termination if non-empty buffer
172       if (me->pcEnd != me->pcBuf) {
173          me->pcEnd[-1] = '\0';
174       }
175    }
176 }
177 
BufBound_Puts(BufBound * me,const char * cpsz)178 void BufBound_Puts(BufBound *me, const char* cpsz)
179 {
180    BufBound_Write(me, cpsz, std_strlen(cpsz));
181 }
182 
BufBound_BufSize(BufBound * me)183 int BufBound_BufSize(BufBound* me)
184 {
185    return me->pcEnd - me->pcBuf;
186 }
187 
BufBound_Left(BufBound * me)188 int BufBound_Left(BufBound* me)
189 {
190    return (me->pcEnd - me->pcWrite);
191 }
192 
BufBound_ReallyWrote(BufBound * me)193 int BufBound_ReallyWrote(BufBound* me)
194 {
195    return STD_MIN(me->pcEnd - me->pcBuf, me->pcWrite - me->pcBuf);
196 }
197 
BufBound_Wrote(BufBound * me)198 int BufBound_Wrote(BufBound* me)
199 {
200    return (me->pcWrite - me->pcBuf);
201 }
202 
BufBound_WriteLE(BufBound * me,const void * pvSrc,int nSrcSize,const char * pszFields)203 void BufBound_WriteLE(BufBound *me,
204                       const void *pvSrc, int nSrcSize,
205                       const char *pszFields)
206 {
207    if (nSrcSize > 0) {
208       int nLen = nSrcSize;
209       char *pcDest = BufBound_ValidateWrite(me, &nLen);
210 
211       (void)std_CopyLE(pcDest, nLen, pvSrc, nSrcSize, pszFields);
212    }
213 }
214 
BufBound_WriteBE(BufBound * me,const void * pvSrc,int nSrcSize,const char * pszFields)215 void BufBound_WriteBE(BufBound *me,
216                       const void *pvSrc, int nSrcSize,
217                       const char *pszFields)
218 {
219    if (nSrcSize > 0) {
220       int nLen = nSrcSize;
221       char *pcDest = BufBound_ValidateWrite(me, &nLen);
222 
223       (void)std_CopyBE(pcDest, nLen, pvSrc, nSrcSize, pszFields);
224    }
225 }
226