File: ByteArray.h

package info (click to toggle)
beid 3.5.2.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 147,240 kB
  • ctags: 34,507
  • sloc: cpp: 149,944; ansic: 41,577; java: 8,927; cs: 6,528; sh: 2,426; perl: 1,866; xml: 805; python: 463; makefile: 263; lex: 92
file content (106 lines) | stat: -rw-r--r-- 4,002 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* ****************************************************************************

 * eID Middleware Project.
 * Copyright (C) 2008-2009 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.

**************************************************************************** */
#pragma once

#include "Export.h"

#include <string>

namespace eIDMW
{

class EIDMW_CMN_API CByteArray  
{
public:
    CByteArray(unsigned long ulCapacity = 0);
    CByteArray(const unsigned char * pucData, unsigned long ulSize, unsigned long ulCapacity = 0);
    CByteArray(const CByteArray & oByteArray);
	CByteArray(const std::string & csData, bool bIsHex = false); 
    ~CByteArray();

    CByteArray & operator = (const CByteArray & oByteArray);

    unsigned long Size() const;

    unsigned char GetByte(unsigned long ulIndex) const;
    unsigned long GetLong(unsigned long ulIndex) const;
    void SetByte(unsigned char ucByte, unsigned long ulIndex);

	/** If Size() == 0, then NULL is returned */
    unsigned char *GetBytes();
    const unsigned char *GetBytes() const;
 	/** Create a new CByteArray with part of this */
	CByteArray GetBytes(unsigned long ulOffset, unsigned long ulLen = 0xFFFFFFFF) const;

    void Append(unsigned char ucByte);
    CByteArray & operator += (const unsigned char ucByte);
    void AppendLong(unsigned long ulLong);
    void Append(const CByteArray & oByteArray);
    CByteArray & operator += (const CByteArray & oByteArray);
    void Append(const unsigned char * pucData, unsigned long ulSize);
	void Append(const std::string scData);
    CByteArray & operator += (const std::string scData);
	void AppendString(const std::string scData);

	/** Remove the ulSize last bytes; if less bytes are present
	 * then everything is removed. */
    void Chop(unsigned long ulSize);

	/** Remove all the bytes at the end (right) of the array
	*   if they are equal to ucByte
	*/
    void TrimRight(unsigned char ucByte=0);

	/** 
	* Replace all ucByteSrc bytes by ucByteDest
	*/
    void Replace(unsigned char ucByteSrc,unsigned char ucByteDest);

	void ClearContents();

    bool Equals(const CByteArray & oByteArray) const;

    /** Returns a hex string, either on 1 line and truncated if needed
      * (e.g. "A5 35 51 00 67 .. 2C 93") or in lines of 16 hex chars
      * preceeded by a tab.
      * A value of 0xFFFFFFFF for ulLen means: till the end of the bytearray. */
    std::wstring ToWString(bool bAddSpace = true, bool bOneLine = true,
        unsigned long ulOffset = 0, unsigned long ulLen = 0xFFFFFFFF) const;
    std::string ToString(bool bAddSpace = true, bool bOneLine = true,
        unsigned long ulOffset = 0, unsigned long ulLen = 0xFFFFFFFF) const;

	// Following methods are used when sending data using socket communication to
	// allow for embedded new line characters without corrupting the data being send.
	// Any embedded new line character is replaced with an escape character and an 
	// encoded newline character. So, '\n' becomes '0x271a'.
	// To allow for embedded escape characters, '0x27' becomes '0x2737'.
	void HideNewLineCharsA();
	void RevealNewLineCharsA();

private:
    void MakeArray(const unsigned char * pucData, unsigned long ulSize, unsigned long ulCapacity = 0);

    unsigned char	*m_pucData;
    unsigned long	m_ulSize;
    unsigned long	m_ulCapacity;
    bool			m_bMallocError;
};

}