aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/src/HexBinaryDecoder.cpp
blob: 15cdbeb43b9d80dd6b85992072d10179eb254d6f (plain) (blame)
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
// 
// HexBinaryDecoder.cpp 
// 
// Library: Foundation 
// Package: Streams 
// Module:  HexBinary 
// 
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 
// and Contributors. 
// 
// SPDX-License-Identifier:	BSL-1.0 
// 
 
 
#include "Poco/HexBinaryDecoder.h" 
#include "Poco/Exception.h" 
 
 
namespace Poco { 
 
 
HexBinaryDecoderBuf::HexBinaryDecoderBuf(std::istream& istr):  
	_buf(*istr.rdbuf()) 
{ 
} 
 
 
HexBinaryDecoderBuf::~HexBinaryDecoderBuf() 
{ 
} 
 
 
int HexBinaryDecoderBuf::readFromDevice() 
{ 
	int c; 
	int n; 
	if ((n = readOne()) == -1) return -1; 
	if (n >= '0' && n <= '9') 
		c = n - '0'; 
	else if (n >= 'A' && n <= 'F') 
		c = n - 'A' + 10; 
	else if (n >= 'a' && n <= 'f') 
		c = n - 'a' + 10; 
	else throw DataFormatException(); 
	c <<= 4; 
	if ((n = readOne()) == -1) throw DataFormatException(); 
	if (n >= '0' && n <= '9') 
		c |= n - '0'; 
	else if (n >= 'A' && n <= 'F') 
		c |= n - 'A' + 10; 
	else if (n >= 'a' && n <= 'f') 
		c |= n - 'a' + 10; 
	else throw DataFormatException(); 
	return c; 
} 
 
 
int HexBinaryDecoderBuf::readOne() 
{ 
	int ch = _buf.sbumpc(); 
	while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n') 
		ch = _buf.sbumpc(); 
	return ch; 
} 
 
 
HexBinaryDecoderIOS::HexBinaryDecoderIOS(std::istream& istr): _buf(istr) 
{ 
	poco_ios_init(&_buf); 
} 
 
 
HexBinaryDecoderIOS::~HexBinaryDecoderIOS() 
{ 
} 
 
 
HexBinaryDecoderBuf* HexBinaryDecoderIOS::rdbuf() 
{ 
	return &_buf; 
} 
 
 
HexBinaryDecoder::HexBinaryDecoder(std::istream& istr): HexBinaryDecoderIOS(istr), std::istream(&_buf) 
{ 
} 
 
 
HexBinaryDecoder::~HexBinaryDecoder() 
{ 
} 
 
 
} // namespace Poco