1//
2// HexBinaryDecoder.cpp
3//
4// Library: Foundation
5// Package: Streams
6// Module: HexBinary
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/HexBinaryDecoder.h"
16#include "Poco/Exception.h"
17
18
19namespace Poco {
20
21
22HexBinaryDecoderBuf::HexBinaryDecoderBuf(std::istream& istr):
23 _buf(*istr.rdbuf())
24{
25}
26
27
28HexBinaryDecoderBuf::~HexBinaryDecoderBuf()
29{
30}
31
32
33int HexBinaryDecoderBuf::readFromDevice()
34{
35 int c;
36 int n;
37 if ((n = readOne()) == -1) return -1;
38 if (n >= '0' && n <= '9')
39 c = n - '0';
40 else if (n >= 'A' && n <= 'F')
41 c = n - 'A' + 10;
42 else if (n >= 'a' && n <= 'f')
43 c = n - 'a' + 10;
44 else throw DataFormatException();
45 c <<= 4;
46 if ((n = readOne()) == -1) throw DataFormatException();
47 if (n >= '0' && n <= '9')
48 c |= n - '0';
49 else if (n >= 'A' && n <= 'F')
50 c |= n - 'A' + 10;
51 else if (n >= 'a' && n <= 'f')
52 c |= n - 'a' + 10;
53 else throw DataFormatException();
54 return c;
55}
56
57
58int HexBinaryDecoderBuf::readOne()
59{
60 int ch = _buf.sbumpc();
61 while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n')
62 ch = _buf.sbumpc();
63 return ch;
64}
65
66
67HexBinaryDecoderIOS::HexBinaryDecoderIOS(std::istream& istr): _buf(istr)
68{
69 poco_ios_init(&_buf);
70}
71
72
73HexBinaryDecoderIOS::~HexBinaryDecoderIOS()
74{
75}
76
77
78HexBinaryDecoderBuf* HexBinaryDecoderIOS::rdbuf()
79{
80 return &_buf;
81}
82
83
84HexBinaryDecoder::HexBinaryDecoder(std::istream& istr): HexBinaryDecoderIOS(istr), std::istream(&_buf)
85{
86}
87
88
89HexBinaryDecoder::~HexBinaryDecoder()
90{
91}
92
93
94} // namespace Poco
95