1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 /**************************************************************************/
5 /* a binary string (blob) class */
10 #include <string.h> // for memmove, memcpy ...
14 #pragma warning(disable:22008) // "Suppress PREfast warnings about integer overflow"
19 BinStr() { len = 0L; max = 8L; ptr_ = buff; }
20 BinStr(BYTE* pb, DWORD cb) { len = cb; max = cb+8; ptr_ = pb; }
21 ~BinStr() { if (ptr_ != buff) delete [] ptr_; }
23 void insertInt8(int val) { if (len >= max) Realloc(); memmove(ptr_+1, ptr_, len); *ptr_ = val; len++; }
24 void insertInt32(int val) { if (len + 4 > max) Realloc(); memmove(ptr_+4, ptr_, len); SET_UNALIGNED_32(&ptr_[0], val); len+=4; }
25 void appendInt8(int val) { if (len >= max) Realloc(); ptr_[len++] = val; }
26 void appendInt16(int val) { if (len + 2 > max) Realloc(); SET_UNALIGNED_16(&ptr_[len], val); len += 2; }
27 void appendInt32(int val) { if (len + 4 > max) Realloc(); SET_UNALIGNED_32(&ptr_[len], val); len += 4; }
28 void appendInt64(__int64 *pval) { if (len + 8 > max) Realloc(8); SET_UNALIGNED_64(&ptr_[len],(*pval)); len += 8; }
29 unsigned __int8* getBuff(unsigned size) {
30 if (len + size > max) Realloc(size);
31 _ASSERTE(len + size <= max);
32 unsigned __int8* ret = &ptr_[len];
36 void append(BinStr* str) {
37 memcpy(getBuff(str->length()), str->ptr(), str->length());
40 void appendFrom(BinStr* str, unsigned ix) {
41 _ASSERTE(str->length() >= ix);
42 if (str->length() >= ix)
44 memcpy(getBuff(str->length()-ix), str->ptr()+ix, str->length()-ix);
48 void remove(unsigned size) { _ASSERTE(len >= size); len -= size; }
50 unsigned __int8* ptr() { return(ptr_); }
51 unsigned length() { return(len); }
54 void Realloc(unsigned atLeast = 4) {
56 if (max < atLeast + len)
58 _ASSERTE(max >= len + atLeast);
59 unsigned __int8* newPtr = new unsigned __int8[max];
60 memcpy(newPtr, ptr_, len);
61 if (ptr_ != buff) delete [] ptr_;
68 unsigned __int8 *ptr_;
69 unsigned __int8 buff[8];
71 BinStr* BinStrToUnicode(BinStr* pSource, bool Swap = false);