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 */
6
7#ifndef BINSTR_H
8#define BINSTR_H
9
10#include <string.h> // for memmove, memcpy ...
11
12#ifdef _PREFAST_
13#pragma warning(push)
14#pragma warning(disable:22008) // "Suppress PREfast warnings about integer overflow"
15#endif
16
17class BinStr {
18public:
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_; }
22
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];
33 len += size;
34 return(ret);
35 }
36 void append(BinStr* str) {
37 memcpy(getBuff(str->length()), str->ptr(), str->length());
38 }
39
40 void appendFrom(BinStr* str, unsigned ix) {
41 _ASSERTE(str->length() >= ix);
42 if (str->length() >= ix)
43 {
44 memcpy(getBuff(str->length()-ix), str->ptr()+ix, str->length()-ix);
45 }
46 }
47
48 void remove(unsigned size) { _ASSERTE(len >= size); len -= size; }
49
50 unsigned __int8* ptr() { return(ptr_); }
51 unsigned length() { return(len); }
52
53private:
54 void Realloc(unsigned atLeast = 4) {
55 max = max * 2;
56 if (max < atLeast + len)
57 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_;
62 ptr_ = newPtr;
63 }
64
65private:
66 unsigned len;
67 unsigned max;
68 unsigned __int8 *ptr_;
69 unsigned __int8 buff[8];
70};
71BinStr* BinStrToUnicode(BinStr* pSource, bool Swap = false);
72#ifdef _PREFAST_
73#pragma warning(pop)
74#endif
75
76#endif
77
78