• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeabc
 

tdeabc

  • tdeabc
ldif.cpp
1/*
2 This file is part of libtdeabc.
3 Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include <kdebug.h>
22#include <kmdcodec.h>
23
24#include "ldif.h"
25
26using namespace TDEABC;
27
28LDIF::LDIF()
29{
30 startParsing();
31}
32
33LDIF::~LDIF()
34{
35}
36
37TQCString LDIF::assembleLine( const TQString &fieldname, const TQByteArray &value,
38 uint linelen, bool url )
39{
40 bool safe = false;
41 bool isDn;
42 TQCString result;
43 uint i;
44
45 if ( url ) {
46 result = fieldname.utf8() + ":< " + TQCString( value.data(), value.size()+1 );
47 } else {
48 isDn = fieldname.lower() == "dn";
49 //SAFE-INIT-CHAR
50 if ( value.size() > 0 && value[0] > 0 && value[0] != '\n' &&
51 value[0] != '\r' && value[0] != ':' && value[0] != '<' ) safe = true;
52
53 //SAFE-CHAR
54 if ( safe ) {
55 for ( i=1; i < value.size(); i++ ) {
56 //allow utf-8 in Distinguished Names
57 if ( ( isDn && value[i] == 0 ) ||
58 ( !isDn && value[i] <= 0 ) ||
59 value[i] == '\r' || value[i] == '\n' ) {
60 safe = false;
61 break;
62 }
63 }
64 }
65
66 if ( value.size() == 0 ) safe = true;
67
68 if( safe ) {
69 result = fieldname.utf8() + ": " + TQCString( value.data(), value.size()+1 );
70 } else {
71 result = fieldname.utf8() + ":: " + KCodecs::base64Encode( value, false );
72 }
73
74 if ( linelen > 0 ) {
75 i = (fieldname.length()+2) > linelen ? fieldname.length()+2 : linelen;
76 while ( i < result.length() ) {
77 result.insert( i, "\n " );
78 i += linelen+2;
79 }
80 }
81 }
82 return result;
83}
84
85TQCString LDIF::assembleLine( const TQString &fieldname, const TQCString &value,
86 uint linelen, bool url )
87{
88 TQCString ret;
89 TQByteArray tmp;
90 uint valuelen = value.length();
91 const char *data = value.data();
92
93 tmp.setRawData( data, valuelen );
94 ret = assembleLine( fieldname, tmp, linelen, url );
95 tmp.resetRawData( data, valuelen );
96 return ret;
97
98}
99
100TQCString LDIF::assembleLine( const TQString &fieldname, const TQString &value,
101 uint linelen, bool url )
102{
103 return assembleLine( fieldname, value.utf8(), linelen, url );
104}
105
106bool LDIF::splitLine( const TQCString &line, TQString &fieldname, TQByteArray &value )
107{
108 int position;
109 TQByteArray tmp;
110 int linelen;
111 const char *data;
112
113// kdDebug(5700) << "splitLine line: " << TQString::fromUtf8(line) << endl;
114
115 position = line.find( ":" );
116 if ( position == -1 ) {
117 // strange: we did not find a fieldname
118 fieldname = "";
119 TQCString str;
120 str = line.stripWhiteSpace();
121 linelen = str.length();
122 data = str.data();
123 tmp.setRawData( data, linelen );
124 value = tmp.copy();
125 tmp.resetRawData( data, linelen );
126// kdDebug(5700) << "value : " << value[0] << endl;
127 return false;
128 }
129
130 linelen = line.length();
131
132 if ( linelen > ( position + 1 ) && line[ position + 1 ] == ':' ) {
133 // String is BASE64 encoded -> decode it now.
134 fieldname = TQString::fromUtf8(
135 line.left( position ).stripWhiteSpace() );
136 if ( linelen <= ( position + 3 ) ) {
137 value.resize( 0 );
138 return false;
139 }
140 data = &line.data()[ position + 3 ];
141 tmp.setRawData( data, linelen - position - 3 );
142 KCodecs::base64Decode( tmp, value );
143 tmp.resetRawData( data, linelen - position - 3 );
144 return false;
145 }
146
147 if ( linelen > ( position + 1 ) && line[ position + 1 ] == '<' ) {
148 // String is an URL.
149 fieldname = TQString::fromUtf8(
150 line.left( position ).stripWhiteSpace() );
151 if ( linelen <= ( position + 3 ) ) {
152 value.resize( 0 );
153 return false;
154 }
155 data = &line.data()[ position + 3];
156 tmp.setRawData( data, linelen - position - 3 );
157 value = tmp.copy();
158 tmp.resetRawData( data, linelen - position - 3 );
159 return true;
160 }
161
162 fieldname = TQString::fromUtf8(line.left( position ).stripWhiteSpace());
163 if ( linelen <= ( position + 2 ) ) {
164 value.resize( 0 );
165 return false;
166 }
167 data = &line.data()[ position + 2 ];
168 tmp.setRawData( data, linelen - position - 2 );
169 value = tmp.copy();
170 tmp.resetRawData( data, linelen - position - 2 );
171 return false;
172}
173
174bool LDIF::splitControl( const TQCString &line, TQString &oid, bool &critical,
175 TQByteArray &value )
176{
177 TQString tmp;
178 critical = false;
179 bool url = splitLine( line, tmp, value );
180
181 kdDebug(5700) << "splitControl: value: " << TQString(TQString::fromUtf8(value, value.size())) << endl;
182 if ( tmp.isEmpty() ) {
183 tmp = TQString::fromUtf8( value, value.size() );
184 value.resize( 0 );
185 }
186 if ( tmp.right( 4 ) == "true" ) {
187 critical = true;
188 tmp.truncate( tmp.length() - 5 );
189 } else if ( tmp.right( 5 ) == "false" ) {
190 critical = false;
191 tmp.truncate( tmp.length() - 6 );
192 }
193 oid = tmp;
194 return url;
195}
196
197LDIF::ParseVal LDIF::processLine()
198{
199
200 if ( mIsComment ) return None;
201
202 ParseVal retval = None;
203 if ( mLastParseVal == EndEntry ) mEntryType = Entry_None;
204
205 mUrl = splitLine( line, mAttr, mVal );
206
207 TQString attrLower = mAttr.lower();
208
209 switch ( mEntryType ) {
210 case Entry_None:
211 if ( attrLower == "version" ) {
212 if ( !mDn.isEmpty() ) retval = Err;
213 } else if ( attrLower == "dn" ) {
214 kdDebug(5700) << "ldapentry dn: " << TQString(TQString::fromUtf8( mVal, mVal.size() )) << endl;
215 mDn = TQString::fromUtf8( mVal, mVal.size() );
216 mModType = Mod_None;
217 retval = NewEntry;
218 } else if ( attrLower == "changetype" ) {
219 if ( mDn.isEmpty() )
220 retval = Err;
221 else {
222 TQString tmpval = TQString::fromUtf8( mVal, mVal.size() );
223 kdDebug(5700) << "changetype: " << tmpval << endl;
224 if ( tmpval == "add" ) mEntryType = Entry_Add;
225 else if ( tmpval == "delete" ) mEntryType = Entry_Del;
226 else if ( tmpval == "modrdn" || tmpval == "moddn" ) {
227 mNewRdn = "";
228 mNewSuperior = "";
229 mDelOldRdn = true;
230 mEntryType = Entry_Modrdn;
231 }
232 else if ( tmpval == "modify" ) mEntryType = Entry_Mod;
233 else retval = Err;
234 }
235 } else if ( attrLower == "control" ) {
236 mUrl = splitControl( TQCString( mVal, mVal.size() + 1 ), mOid, mCritical, mVal );
237 retval = Control;
238 } else if ( !mAttr.isEmpty() && mVal.size() > 0 ) {
239 mEntryType = Entry_Add;
240 retval = Item;
241 }
242 break;
243 case Entry_Add:
244 if ( mAttr.isEmpty() && mVal.size() == 0 )
245 retval = EndEntry;
246 else
247 retval = Item;
248 break;
249 case Entry_Del:
250 if ( mAttr.isEmpty() && mVal.size() == 0 )
251 retval = EndEntry;
252 else
253 retval = Err;
254 break;
255 case Entry_Mod:
256 if ( mModType == Mod_None ) {
257 kdDebug(5700) << "tdeio_ldap: new modtype " << mAttr << endl;
258 if ( mAttr.isEmpty() && mVal.size() == 0 ) {
259 retval = EndEntry;
260 } else if ( attrLower == "add" ) {
261 mModType = Mod_Add;
262 } else if ( attrLower == "replace" ) {
263 mModType = Mod_Replace;
264 mAttr = TQString::fromUtf8( mVal, mVal.size() );
265 mVal.resize( 0 );
266 retval = Item;
267 } else if ( attrLower == "delete" ) {
268 mModType = Mod_Del;
269 mAttr = TQString::fromUtf8( mVal, mVal.size() );
270 mVal.resize( 0 );
271 retval = Item;
272 } else {
273 retval = Err;
274 }
275 } else {
276 if ( mAttr.isEmpty() ) {
277 if ( TQString::fromUtf8( mVal, mVal.size() ) == "-" ) {
278 mModType = Mod_None;
279 } else if ( mVal.size() == 0 ) {
280 retval = EndEntry;
281 } else
282 retval = Err;
283 } else
284 retval = Item;
285 }
286 break;
287 case Entry_Modrdn:
288 if ( mAttr.isEmpty() && mVal.size() == 0 )
289 retval = EndEntry;
290 else if ( attrLower == "newrdn" )
291 mNewRdn = TQString::fromUtf8( mVal, mVal.size() );
292 else if ( attrLower == "newsuperior" )
293 mNewSuperior = TQString::fromUtf8( mVal, mVal.size() );
294 else if ( attrLower == "deleteoldrdn" ) {
295 if ( mVal.size() > 0 && mVal[0] == '0' )
296 mDelOldRdn = false;
297 else if ( mVal.size() > 0 && mVal[0] == '1' )
298 mDelOldRdn = true;
299 else
300 retval = Err;
301 } else
302 retval = Err;
303 break;
304 }
305 return retval;
306}
307
308LDIF::ParseVal LDIF::nextItem()
309{
310 ParseVal retval = None;
311 char c=0;
312
313 while( retval == None ) {
314 if ( mPos < mLdif.size() ) {
315 c = mLdif[mPos];
316 mPos++;
317 if ( mIsNewLine && c == '\r' ) continue; //handle \n\r line end
318 if ( mIsNewLine && ( c == ' ' || c == '\t' ) ) { //line folding
319 mIsNewLine = false;
320 continue;
321 }
322 if ( mIsNewLine ) {
323 mIsNewLine = false;
324 retval = processLine();
325 mLastParseVal = retval;
326 line.resize( 0 );
327 mIsComment = ( c == '#' );
328 }
329 if ( c == '\n' || c == '\r' ) {
330 mLineNo++;
331 mIsNewLine = true;
332 continue;
333 }
334 } else {
335 retval = MoreData;
336 break;
337 }
338
339 if ( !mIsComment ) line += c;
340 }
341 return retval;
342}
343
344void LDIF::endLDIF()
345{
346 TQByteArray tmp( 3 );
347 tmp[ 0 ] = '\n';
348 tmp[ 1 ] = '\n';
349 tmp[ 2 ] = '\n';
350 mLdif = tmp;
351 mPos = 0;
352}
353
354void LDIF::startParsing()
355{
356 mPos = mLineNo = 0;
357 mDelOldRdn = false;
358 mEntryType = Entry_None;
359 mModType = Mod_None;
360 mDn = mNewRdn = mNewSuperior = "";
361 line = "";
362 mIsNewLine = false;
363 mIsComment = false;
364 mLastParseVal = None;
365}
KCodecs::base64Decode
static TQCString base64Decode(const TQByteArray &in)
KCodecs::base64Encode
static TQCString base64Encode(const TQByteArray &in, bool insertLFs=false)
TDEABC::LDIF::critical
bool critical() const
Returns the criticality level when modType() returned Control.
Definition: ldif.h:156
TDEABC::LDIF::splitLine
static bool splitLine(const TQCString &line, TQString &fieldname, TQByteArray &value)
Splits one line from an LDIF file to attribute and value components.
Definition: ldif.cpp:106
TDEABC::LDIF::assembleLine
static TQCString assembleLine(const TQString &fieldname, const TQByteArray &value, uint linelen=0, bool url=false)
Assembles fieldname and value into a valid LDIF line, BASE64 encodes the value if neccessary and opti...
Definition: ldif.cpp:37
TDEABC::LDIF::oid
const TQString & oid() const
Returns the OID when modType() returned Control.
Definition: ldif.h:160
TDEABC::LDIF::startParsing
void startParsing()
Starts the parsing of a new LDIF.
Definition: ldif.cpp:354
TDEABC::LDIF::processLine
ParseVal processLine()
Process one LDIF line.
Definition: ldif.cpp:197
TDEABC::LDIF::endLDIF
void endLDIF()
Indicates the end of the LDIF file/stream.
Definition: ldif.cpp:344
TDEABC::LDIF::nextItem
ParseVal nextItem()
Process the LDIF until a complete item can be returned.
Definition: ldif.cpp:308
TDEABC::LDIF::splitControl
static bool splitControl(const TQCString &line, TQString &oid, bool &critical, TQByteArray &value)
Splits a control specification (without the "control:" directive)
Definition: ldif.cpp:174
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
TDEABC
static data, shared by ALL addressee objects
Definition: address.h:48

tdeabc

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

tdeabc

Skip menu "tdeabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeabc by doxygen 1.9.4
This website is maintained by Timothy Pearson.