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

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • utils
hex.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  utils/hex.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "hex.h"
36 
37 #include <kleo/exception.h>
38 
39 #include <KLocale>
40 
41 #include <QString>
42 #include <QByteArray>
43 
44 using namespace Kleo;
45 
46 static unsigned char unhex( unsigned char ch ) {
47  if ( ch >= '0' && ch <= '9' )
48  return ch - '0';
49  if ( ch >= 'A' && ch <= 'F' )
50  return ch - 'A' + 10;
51  if ( ch >= 'a' && ch <= 'f' )
52  return ch - 'a' + 10;
53  const char cch = ch;
54  throw Exception( gpg_error( GPG_ERR_ASS_SYNTAX ),
55  i18n("Invalid hex char '%1' in input stream.",
56  QString::fromLatin1( &cch, 1 ) ) );
57 }
58 
59 std::string Kleo::hexdecode( const std::string & in ) {
60  std::string result;
61  result.reserve( in.size() );
62  for ( std::string::const_iterator it = in.begin(), end = in.end() ; it != end ; ++it )
63  if ( *it == '%' ) {
64  ++it;
65  unsigned char ch = '\0';
66  if ( it == end )
67  throw Exception( gpg_error( GPG_ERR_ASS_SYNTAX ),
68  i18n("Premature end of hex-encoded char in input stream") );
69  ch |= unhex( *it ) << 4;
70  ++it;
71  if ( it == end )
72  throw Exception( gpg_error( GPG_ERR_ASS_SYNTAX ),
73  i18n("Premature end of hex-encoded char in input stream") );
74  ch |= unhex( *it );
75  result.push_back( ch );
76  } else if ( *it == '+' ) {
77  result += ' ';
78  } else {
79  result.push_back( *it );
80  }
81  return result;
82 }
83 
84 std::string Kleo::hexencode( const std::string & in ) {
85  std::string result;
86  result.reserve( 3 * in.size() );
87 
88  static const char hex[] = "0123456789ABCDEF";
89 
90  for ( std::string::const_iterator it = in.begin(), end = in.end() ; it != end ; ++it )
91  switch ( const unsigned char ch = *it ) {
92  default:
93  if ( ( ch >= '!' && ch <= '~' ) || ch > 0xA0 ) {
94  result += ch;
95  break;
96  }
97  // else fall through
98  case ' ':
99  result += '+';
100  break;
101  case '"':
102  case '#':
103  case '$':
104  case '%':
105  case '\'':
106  case '+':
107  case '=':
108  result += '%';
109  result += hex[ (ch & 0xF0) >> 4 ];
110  result += hex[ (ch & 0x0F) ];
111  break;
112  }
113 
114  return result;
115 }
116 
117 std::string Kleo::hexdecode( const char * in ) {
118  if ( !in )
119  return std::string();
120  return hexdecode( std::string( in ) );
121 }
122 
123 std::string Kleo::hexencode( const char * in ) {
124  if ( !in )
125  return std::string();
126  return hexencode( std::string( in ) );
127 }
128 
129 QByteArray Kleo::hexdecode( const QByteArray & in ) {
130  if ( in.isNull() )
131  return QByteArray();
132  const std::string result = hexdecode( std::string( in.constData() ) );
133  return QByteArray( result.data(), result.size() );
134 }
135 
136 QByteArray Kleo::hexencode( const QByteArray & in ) {
137  if ( in.isNull() )
138  return QByteArray();
139  const std::string result = hexencode( std::string( in.constData() ) );
140  return QByteArray( result.data(), result.size() );
141 }
Kleo::hexencode
std::string hexencode(const char *s)
Definition: hex.cpp:123
hex.h
string
const char * string
Definition: verifychecksumscontroller.cpp:511
unhex
static unsigned char unhex(unsigned char ch)
Definition: hex.cpp:46
Kleo::hexdecode
std::string hexdecode(const char *s)
Definition: hex.cpp:117
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

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