/src/icu/source/common/bytestream.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | // © 2016 and later: Unicode, Inc. and others.  | 
2  |  | // License & terms of use: http://www.unicode.org/copyright.html  | 
3  |  | // Copyright (C) 2009-2011, International Business Machines  | 
4  |  | // Corporation and others. All Rights Reserved.  | 
5  |  | //  | 
6  |  | // Copyright 2007 Google Inc. All Rights Reserved.  | 
7  |  | // Author: sanjay@google.com (Sanjay Ghemawat)  | 
8  |  |  | 
9  |  | #include "unicode/utypes.h"  | 
10  |  | #include "unicode/bytestream.h"  | 
11  |  | #include "cmemory.h"  | 
12  |  |  | 
13  |  | U_NAMESPACE_BEGIN  | 
14  |  |  | 
15  | 0  | ByteSink::~ByteSink() {} | 
16  |  |  | 
17  |  | char* ByteSink::GetAppendBuffer(int32_t min_capacity,  | 
18  |  |                                 int32_t /*desired_capacity_hint*/,  | 
19  |  |                                 char* scratch, int32_t scratch_capacity,  | 
20  | 0  |                                 int32_t* result_capacity) { | 
21  | 0  |   if (min_capacity < 1 || scratch_capacity < min_capacity) { | 
22  | 0  |     *result_capacity = 0;  | 
23  | 0  |     return NULL;  | 
24  | 0  |   }  | 
25  | 0  |   *result_capacity = scratch_capacity;  | 
26  | 0  |   return scratch;  | 
27  | 0  | }  | 
28  |  |  | 
29  | 0  | void ByteSink::Flush() {} | 
30  |  |  | 
31  |  | CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity)  | 
32  | 0  |     : outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity),  | 
33  | 0  |       size_(0), appended_(0), overflowed_(FALSE) { | 
34  | 0  | }  | 
35  |  |  | 
36  |  | CheckedArrayByteSink::~CheckedArrayByteSink() {} | 
37  |  |  | 
38  | 0  | CheckedArrayByteSink& CheckedArrayByteSink::Reset() { | 
39  | 0  |   size_ = appended_ = 0;  | 
40  | 0  |   overflowed_ = FALSE;  | 
41  | 0  |   return *this;  | 
42  | 0  | }  | 
43  |  |  | 
44  | 0  | void CheckedArrayByteSink::Append(const char* bytes, int32_t n) { | 
45  | 0  |   if (n <= 0) { | 
46  | 0  |     return;  | 
47  | 0  |   }  | 
48  | 0  |   if (n > (INT32_MAX - appended_)) { | 
49  |  |     // TODO: Report as integer overflow, not merely buffer overflow.  | 
50  | 0  |     appended_ = INT32_MAX;  | 
51  | 0  |     overflowed_ = TRUE;  | 
52  | 0  |     return;  | 
53  | 0  |   }  | 
54  | 0  |   appended_ += n;  | 
55  | 0  |   int32_t available = capacity_ - size_;  | 
56  | 0  |   if (n > available) { | 
57  | 0  |     n = available;  | 
58  | 0  |     overflowed_ = TRUE;  | 
59  | 0  |   }  | 
60  | 0  |   if (n > 0 && bytes != (outbuf_ + size_)) { | 
61  | 0  |     uprv_memcpy(outbuf_ + size_, bytes, n);  | 
62  | 0  |   }  | 
63  | 0  |   size_ += n;  | 
64  | 0  | }  | 
65  |  |  | 
66  |  | char* CheckedArrayByteSink::GetAppendBuffer(int32_t min_capacity,  | 
67  |  |                                             int32_t /*desired_capacity_hint*/,  | 
68  |  |                                             char* scratch,  | 
69  |  |                                             int32_t scratch_capacity,  | 
70  | 0  |                                             int32_t* result_capacity) { | 
71  | 0  |   if (min_capacity < 1 || scratch_capacity < min_capacity) { | 
72  | 0  |     *result_capacity = 0;  | 
73  | 0  |     return NULL;  | 
74  | 0  |   }  | 
75  | 0  |   int32_t available = capacity_ - size_;  | 
76  | 0  |   if (available >= min_capacity) { | 
77  | 0  |     *result_capacity = available;  | 
78  | 0  |     return outbuf_ + size_;  | 
79  | 0  |   } else { | 
80  | 0  |     *result_capacity = scratch_capacity;  | 
81  | 0  |     return scratch;  | 
82  | 0  |   }  | 
83  | 0  | }  | 
84  |  |  | 
85  |  | U_NAMESPACE_END  |