• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • core
  • codecs
hexadecimalbytecodec.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Core library, made within the KDE community.
3 
4  Copyright 2004,2011 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "hexadecimalbytecodec.h"
24 
25 // Qt
26 #include <QtCore/QString>
27 
28 
29 namespace Okteta
30 {
31 
32 static const QChar upperCaseDigits[16] =
33 { // '0', '1', '2', '3', '4', '5', '6', '7' ,'8', '9',
34  0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0039,
35  // 'A', 'B', 'C', 'D', 'E', 'F'
36  0x0041,0x0042,0x0043,0x0044,0x0045,0x0046 };
37 static const QChar lowerCaseDigits[16] =
38 { // '0', '1', '2', '3', '4', '5', '6', '7' ,'8', '9',
39  0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0039,
40  // 'a', 'b', 'c', 'd', 'e', 'f'
41  0x0061,0x0062,0x0063,0x0064,0x0065,0x0066 };
42 static const Byte hexadecimalDigitsFilledLimit = 16;
43 
44 
45 HexadecimalByteCodec::HexadecimalByteCodec( bool useLowerCaseDigits )
46  : mDigits( useLowerCaseDigits ? lowerCaseDigits : upperCaseDigits )
47 {
48 }
49 
50 bool HexadecimalByteCodec::setLowerCaseDigits( bool useLowerCaseDigits )
51 {
52  const QChar* const digits = useLowerCaseDigits ? lowerCaseDigits : upperCaseDigits;
53 
54  if( digits == mDigits )
55  return false;
56 
57  mDigits = digits;
58  return true;
59 }
60 
61 bool HexadecimalByteCodec::isLowerCaseDigits() const { return mDigits == lowerCaseDigits; }
62 
63 
64 unsigned int HexadecimalByteCodec::encodingWidth() const { return 2; }
65 Byte HexadecimalByteCodec::digitsFilledLimit() const { return hexadecimalDigitsFilledLimit; }
66 
67 void HexadecimalByteCodec::encode( QString& digits, unsigned int pos, Byte byte ) const
68 {
69  digits[pos++] = mDigits[byte>>4];
70  digits[pos] = mDigits[byte&0x0F];
71 }
72 
73 void HexadecimalByteCodec::encodeShort( QString& digits, unsigned int pos, Byte byte ) const
74 {
75  unsigned char digitValue = byte>>4;
76  if( digitValue > 0 )
77  digits[pos++] = mDigits[digitValue];
78  digits[pos] = mDigits[byte&0x0F];
79 }
80 
81 
82 static inline bool isValidBigDigit( unsigned char digit )
83 {
84  return ('A' <= digit && digit <= 'F');
85 }
86 
87 static inline bool isValidSmallDigit( unsigned char digit )
88 {
89  return ('a' <= digit && digit <= 'f');
90 }
91 
92 static inline bool isValidDecimalDigit( unsigned char digit )
93 {
94  return ('0' <= digit && digit <= '9');
95 }
96 
97 
98 bool HexadecimalByteCodec::isValidDigit( unsigned char digit ) const
99 {
100  return isValidDecimalDigit(digit) || isValidBigDigit(digit) || isValidSmallDigit(digit);
101 }
102 
103 bool HexadecimalByteCodec::turnToValue( unsigned char* digit ) const
104 {
105  if( isValidDecimalDigit(*digit) )
106  *digit -= '0';
107  else if( isValidBigDigit(*digit) )
108  *digit -= 'A' - 10;
109  else if( isValidSmallDigit(*digit) )
110  *digit -= 'a' - 10;
111  else
112  return false;
113 
114  return true;
115 }
116 
117 bool HexadecimalByteCodec::appendDigit( Byte* byte, unsigned char digit ) const
118 {
119  if( turnToValue(&digit) )
120  {
121  Byte _byte = *byte;
122  if( _byte < hexadecimalDigitsFilledLimit )
123  {
124  _byte <<= 4;
125  _byte += digit;
126  *byte = _byte;
127  return true;
128  }
129  }
130  return false;
131 }
132 
133 
134 
135 void HexadecimalByteCodec::removeLastDigit( Byte* byte ) const
136 {
137  *byte >>= 4;
138 }
139 
140 }
Okteta::HexadecimalByteCodec::removeLastDigit
virtual void removeLastDigit(Byte *byte) const
Tries to remove the last (least significant) digit from byte.
Definition: hexadecimalbytecodec.cpp:135
Okteta::upperCaseDigits
static const QChar upperCaseDigits[16]
Definition: hexadecimalbytecodec.cpp:32
Okteta::hexadecimalDigitsFilledLimit
static const Byte hexadecimalDigitsFilledLimit
Definition: hexadecimalbytecodec.cpp:42
Okteta::HexadecimalByteCodec::encode
virtual void encode(QString &digits, unsigned int pos, Byte byte) const
Encodes the byte using full coding width, prefixing with 0s if needed, and writes the result to digit...
Definition: hexadecimalbytecodec.cpp:67
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Okteta::lowerCaseDigits
static const QChar lowerCaseDigits[16]
Definition: hexadecimalbytecodec.cpp:37
Okteta::HexadecimalByteCodec::encodingWidth
virtual unsigned int encodingWidth() const
Definition: hexadecimalbytecodec.cpp:64
Okteta::HexadecimalByteCodec::HexadecimalByteCodec
HexadecimalByteCodec(bool lowerCaseDigits=false)
Definition: hexadecimalbytecodec.cpp:45
Okteta::HexadecimalByteCodec::mDigits
const QChar * mDigits
Definition: hexadecimalbytecodec.h:56
Okteta::HexadecimalByteCodec::isLowerCaseDigits
bool isLowerCaseDigits() const
Definition: hexadecimalbytecodec.cpp:61
Okteta::isValidDecimalDigit
static bool isValidDecimalDigit(unsigned char digit)
Definition: hexadecimalbytecodec.cpp:92
Okteta::isValidSmallDigit
static bool isValidSmallDigit(unsigned char digit)
Definition: hexadecimalbytecodec.cpp:87
Okteta::HexadecimalByteCodec::appendDigit
virtual bool appendDigit(Byte *byte, unsigned char digit) const
Tries to increase the byte value by appending a digit to the digits of the current encoding...
Definition: hexadecimalbytecodec.cpp:117
Okteta::isValidBigDigit
static bool isValidBigDigit(unsigned char digit)
Definition: hexadecimalbytecodec.cpp:82
Okteta::HexadecimalByteCodec::setLowerCaseDigits
bool setLowerCaseDigits(bool lowerCaseDigits)
Definition: hexadecimalbytecodec.cpp:50
hexadecimalbytecodec.h
Okteta::HexadecimalByteCodec::digitsFilledLimit
virtual Byte digitsFilledLimit() const
Definition: hexadecimalbytecodec.cpp:65
Okteta::HexadecimalByteCodec::isValidDigit
virtual bool isValidDigit(unsigned char digit) const
Checks if the given digit is used in the encoding.
Definition: hexadecimalbytecodec.cpp:98
Okteta::HexadecimalByteCodec::turnToValue
virtual bool turnToValue(unsigned char *digit) const
Turns the digit into a byte with the value of the digit.
Definition: hexadecimalbytecodec.cpp:103
Okteta::HexadecimalByteCodec::encodeShort
virtual void encodeShort(QString &digits, unsigned int pos, Byte byte) const
Encodes the byte and writes the result to digits, no leading 0s.
Definition: hexadecimalbytecodec.cpp:73
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal