summaryrefslogtreecommitdiff
path: root/package/source/manifest/ManifestImport.cxx
blob: 916db4c3c6948c6fecb9a1bb5082151650dd22ae (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "ManifestImport.hxx"
#include "ManifestDefines.hxx"
#include <sax/tools/converter.hxx>
#include <osl/diagnose.h>
#include <com/sun/star/xml/sax/XAttributeList.hpp>
#include <com/sun/star/xml/crypto/DigestID.hpp>
#include <com/sun/star/xml/crypto/CipherID.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <comphelper/base64.hxx>
#include <comphelper/sequence.hxx>

using namespace com::sun::star::uno;
using namespace com::sun::star::beans;
using namespace com::sun::star;
using namespace std;

ManifestImport::ManifestImport( vector < Sequence < PropertyValue > > & rNewManVector )
    : bIgnoreEncryptData    ( false )
    , bPgpEncryption ( false )
    , nDerivedKeySize( 0 )
    , rManVector ( rNewManVector )

    , sFileEntryElement     ( ELEMENT_FILE_ENTRY )
    , sEncryptionDataElement( ELEMENT_ENCRYPTION_DATA )
    , sAlgorithmElement ( ELEMENT_ALGORITHM )
    , sStartKeyAlgElement   ( ELEMENT_START_KEY_GENERATION )
    , sKeyDerivationElement( ELEMENT_KEY_DERIVATION )

    , sMediaTypeAttribute           ( ATTRIBUTE_MEDIA_TYPE )
    , sVersionAttribute             ( ATTRIBUTE_VERSION )
    , sFullPathAttribute            ( ATTRIBUTE_FULL_PATH )
    , sSizeAttribute                ( ATTRIBUTE_SIZE )
    , sSaltAttribute                ( ATTRIBUTE_SALT )
    , sInitialisationVectorAttribute ( ATTRIBUTE_INITIALISATION_VECTOR )
    , sIterationCountAttribute      ( ATTRIBUTE_ITERATION_COUNT )
    , sKeySizeAttribute             ( ATTRIBUTE_KEY_SIZE )
    , sAlgorithmNameAttribute       ( ATTRIBUTE_ALGORITHM_NAME )
    , sStartKeyAlgNameAttribute     ( ATTRIBUTE_START_KEY_GENERATION_NAME )
    , sKeyDerivationNameAttribute   ( ATTRIBUTE_KEY_DERIVATION_NAME )
    , sChecksumAttribute            ( ATTRIBUTE_CHECKSUM )
    , sChecksumTypeAttribute        ( ATTRIBUTE_CHECKSUM_TYPE )

    , sKeyInfoElement               ( ELEMENT_ENCRYPTED_KEYINFO )
    , sManifestKeyInfoElement       ( ELEMENT_MANIFEST_KEYINFO )
    , sEncryptedKeyElement          ( ELEMENT_ENCRYPTEDKEY )
    , sEncryptionMethodElement      ( ELEMENT_ENCRYPTIONMETHOD )
    , sPgpDataElement               ( ELEMENT_PGPDATA )
    , sPgpKeyIDElement              ( ELEMENT_PGPKEYID )
    , sPGPKeyPacketElement          ( ELEMENT_PGPKEYPACKET )
    , sAlgorithmAttribute           ( ATTRIBUTE_ALGORITHM )
    , sCipherDataElement            ( ELEMENT_CIPHERDATA )
    , sCipherValueElement           ( ELEMENT_CIPHERVALUE )

    , sFullPathProperty             ( "FullPath" )
    , sMediaTypeProperty            ( "MediaType" )
    , sVersionProperty              ( "Version" )
    , sIterationCountProperty       ( "IterationCount" )
    , sDerivedKeySizeProperty       ( "DerivedKeySize" )
    , sSaltProperty                 ( "Salt" )
    , sInitialisationVectorProperty ( "InitialisationVector" )
    , sSizeProperty                 ( "Size" )
    , sDigestProperty               ( "Digest" )
    , sEncryptionAlgProperty        ( "EncryptionAlgorithm" )
    , sStartKeyAlgProperty          ( "StartKeyAlgorithm" )
    , sDigestAlgProperty            ( "DigestAlgorithm" )

    , sSHA256_URL_ODF12             ( SHA256_URL_ODF12 )
    , sSHA256_URL                   ( SHA256_URL )
    , sSHA1_Name                    ( SHA1_NAME )
    , sSHA1_URL                     ( SHA1_URL )

    , sSHA256_1k_URL                ( SHA256_1K_URL )
    , sSHA1_1k_Name                 ( SHA1_1K_NAME )
    , sSHA1_1k_URL                  ( SHA1_1K_URL )

    , sBlowfish_Name                ( BLOWFISH_NAME )
    , sBlowfish_URL                 ( BLOWFISH_URL )
    , sAES128_URL                   ( AES128_URL )
    , sAES192_URL                   ( AES192_URL )
    , sAES256_URL                   ( AES256_URL )

    , sPBKDF2_Name                  ( PBKDF2_NAME )
    , sPBKDF2_URL                   ( PBKDF2_URL )
{
    aStack.reserve( 10 );
}

ManifestImport::~ManifestImport()
{
}

void SAL_CALL ManifestImport::startDocument(  )
{
}

void SAL_CALL ManifestImport::endDocument(  )
{
}

void ManifestImport::doFileEntry(StringHashMap &rConvertedAttribs)
{
    aSequence.resize(PKG_SIZE_ENCR_MNFST);

    aSequence[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
    aSequence[PKG_MNFST_FULLPATH].Value <<= rConvertedAttribs[sFullPathAttribute];
    aSequence[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
    aSequence[PKG_MNFST_MEDIATYPE].Value <<= rConvertedAttribs[sMediaTypeAttribute];

    OUString sVersion = rConvertedAttribs[sVersionAttribute];
    if ( sVersion.getLength() ) {
        aSequence[PKG_MNFST_VERSION].Name = sVersionProperty;
        aSequence[PKG_MNFST_VERSION].Value <<= sVersion;
    }

    OUString sSize = rConvertedAttribs[sSizeAttribute];
    if ( sSize.getLength() ) {
        sal_Int64 nSize = sSize.toInt64();
        aSequence[PKG_MNFST_UCOMPSIZE].Name = sSizeProperty;
        aSequence[PKG_MNFST_UCOMPSIZE].Value <<= nSize;
    }
}

void ManifestImport::doKeyInfoEntry(StringHashMap &)
{
}

void ManifestImport::doEncryptedKey(StringHashMap &)
{
    aKeyInfoSequence.clear();
    aKeyInfoSequence.resize(3);
}

void ManifestImport::doEncryptionMethod(StringHashMap &rConvertedAttribs)
{
    OUString aString = rConvertedAttribs[sAlgorithmAttribute];
    if ( aKeyInfoSequence.size() != 3
         || aString != "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" )
    {
        bIgnoreEncryptData = true;
    }
}

void ManifestImport::doEncryptedKeyInfo(StringHashMap &)
{
}

void ManifestImport::doEncryptedCipherData(StringHashMap &)
{
}

void ManifestImport::doEncryptedPgpData(StringHashMap &)
{
}

void ManifestImport::doEncryptedCipherValue()
{
    if ( aKeyInfoSequence.size() == 3 )
    {
        aKeyInfoSequence[2].Name = "CipherValue";
        uno::Sequence < sal_Int8 > aDecodeBuffer;
        ::comphelper::Base64::decode(aDecodeBuffer, aCurrentCharacters);
        aKeyInfoSequence[2].Value <<= aDecodeBuffer;
        aCurrentCharacters = ""; // consumed
    }
    else
        bIgnoreEncryptData = true;
}

void ManifestImport::doEncryptedKeyId()
{
    if ( aKeyInfoSequence.size() == 3 )
    {
        aKeyInfoSequence[0].Name = "KeyId";
        uno::Sequence < sal_Int8 > aDecodeBuffer;
        ::comphelper::Base64::decode(aDecodeBuffer, aCurrentCharacters);
        aKeyInfoSequence[0].Value <<= aDecodeBuffer;
        aCurrentCharacters = ""; // consumed
    }
    else
        bIgnoreEncryptData = true;
}

void ManifestImport::doEncryptedKeyPacket()
{
    if ( aKeyInfoSequence.size() == 3 )
    {
        aKeyInfoSequence[1].Name = "KeyPacket";
        uno::Sequence < sal_Int8 > aDecodeBuffer;
        ::comphelper::Base64::decode(aDecodeBuffer, aCurrentCharacters);
        aKeyInfoSequence[1].Value <<= aDecodeBuffer;
        aCurrentCharacters = ""; // consumed
    }
    else
        bIgnoreEncryptData = true;
}

void ManifestImport::doEncryptionData(StringHashMap &rConvertedAttribs)
{
    // If this element exists, then this stream is encrypted and we need
    // to import the initialisation vector, salt and iteration count used
    nDerivedKeySize = 0;
    OUString aString = rConvertedAttribs[sChecksumTypeAttribute];
    if ( !bIgnoreEncryptData ) {
        if ( aString == sSHA1_1k_Name || aString == sSHA1_1k_URL ) {
            aSequence[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty;
            aSequence[PKG_MNFST_DIGESTALG].Value <<= xml::crypto::DigestID::SHA1_1K;
        } else if ( aString == sSHA256_1k_URL ) {
            aSequence[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty;
            aSequence[PKG_MNFST_DIGESTALG].Value <<= xml::crypto::DigestID::SHA256_1K;
        } else
            bIgnoreEncryptData = true;

        if ( !bIgnoreEncryptData ) {
            aString = rConvertedAttribs[sChecksumAttribute];
            uno::Sequence < sal_Int8 > aDecodeBuffer;
            ::comphelper::Base64::decode(aDecodeBuffer, aString);
            aSequence[PKG_MNFST_DIGEST].Name = sDigestProperty;
            aSequence[PKG_MNFST_DIGEST].Value <<= aDecodeBuffer;
        }
    }
}

void ManifestImport::doAlgorithm(StringHashMap &rConvertedAttribs)
{
    if ( !bIgnoreEncryptData ) {
        OUString aString = rConvertedAttribs[sAlgorithmNameAttribute];
        if ( aString == sBlowfish_Name || aString == sBlowfish_URL ) {
            aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
            aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::BLOWFISH_CFB_8;
        } else if ( aString == sAES256_URL ) {
            aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
            aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING;
            OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 32, "Unexpected derived key length!" );
            nDerivedKeySize = 32;
        } else if ( aString == sAES192_URL ) {
            aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
            aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING;
            OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 24, "Unexpected derived key length!" );
            nDerivedKeySize = 24;
        } else if ( aString == sAES128_URL ) {
            aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
            aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING;
            OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 16, "Unexpected derived key length!" );
            nDerivedKeySize = 16;
        } else
            bIgnoreEncryptData = true;

        if ( !bIgnoreEncryptData ) {
            aString = rConvertedAttribs[sInitialisationVectorAttribute];
            uno::Sequence < sal_Int8 > aDecodeBuffer;
            ::comphelper::Base64::decode(aDecodeBuffer, aString);
            aSequence[PKG_MNFST_INIVECTOR].Name = sInitialisationVectorProperty;
            aSequence[PKG_MNFST_INIVECTOR].Value <<= aDecodeBuffer;
        }
    }
}

void ManifestImport::doKeyDerivation(StringHashMap &rConvertedAttribs)
{
    if ( !bIgnoreEncryptData ) {
        OUString aString = rConvertedAttribs[sKeyDerivationNameAttribute];
        if ( aString == sPBKDF2_Name || aString == sPBKDF2_URL ) {
            aString = rConvertedAttribs[sSaltAttribute];
            uno::Sequence < sal_Int8 > aDecodeBuffer;
            ::comphelper::Base64::decode(aDecodeBuffer, aString);
            aSequence[PKG_MNFST_SALT].Name = sSaltProperty;
            aSequence[PKG_MNFST_SALT].Value <<= aDecodeBuffer;

            aString = rConvertedAttribs[sIterationCountAttribute];
            aSequence[PKG_MNFST_ITERATION].Name = sIterationCountProperty;
            aSequence[PKG_MNFST_ITERATION].Value <<= aString.toInt32();

            aString = rConvertedAttribs[sKeySizeAttribute];
            if ( aString.getLength() ) {
                sal_Int32 nKey = aString.toInt32();
                OSL_ENSURE( !nDerivedKeySize || nKey == nDerivedKeySize , "Provided derived key length differs from the expected one!" );
                nDerivedKeySize = nKey;
            } else if ( !nDerivedKeySize )
                nDerivedKeySize = 16;
            else if ( nDerivedKeySize != 16 )
                OSL_ENSURE( false, "Default derived key length differs from the expected one!" );

            aSequence[PKG_MNFST_DERKEYSIZE].Name = sDerivedKeySizeProperty;
            aSequence[PKG_MNFST_DERKEYSIZE].Value <<= nDerivedKeySize;
        } else if ( bPgpEncryption ) {
            if ( aString != "PGP" )
                bIgnoreEncryptData = true;
        } else
            bIgnoreEncryptData = true;
    }
}

void ManifestImport::doStartKeyAlg(StringHashMap &rConvertedAttribs)
{
    OUString aString = rConvertedAttribs[sStartKeyAlgNameAttribute];
    if (aString == sSHA256_URL || aString == sSHA256_URL_ODF12) {
        aSequence[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty;
        aSequence[PKG_MNFST_STARTALG].Value <<= xml::crypto::DigestID::SHA256;
    } else if ( aString == sSHA1_Name || aString == sSHA1_URL ) {
        aSequence[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty;
        aSequence[PKG_MNFST_STARTALG].Value <<= xml::crypto::DigestID::SHA1;
    } else
        bIgnoreEncryptData = true;
}

void SAL_CALL ManifestImport::startElement( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
{
    StringHashMap aConvertedAttribs;
    OUString aConvertedName = PushNameAndNamespaces( aName, xAttribs, aConvertedAttribs );

    size_t nLevel = aStack.size();

    assert(nLevel >= 1);

    switch (nLevel) {
    case 1: {
        if (aConvertedName != ELEMENT_MANIFEST) //manifest:manifest
            aStack.back().m_bValid = false;
        break;
    }
    case 2: {
        if (aConvertedName == sFileEntryElement) //manifest:file-entry
            doFileEntry(aConvertedAttribs);
        else if (aConvertedName == sManifestKeyInfoElement) //loext:KeyInfo
            doKeyInfoEntry(aConvertedAttribs);
        else
            aStack.back().m_bValid = false;
        break;
    }
    case 3: {
        ManifestStack::reverse_iterator aIter = aStack.rbegin();
        ++aIter;

        if (!aIter->m_bValid)
            aStack.back().m_bValid = false;
        else if (aConvertedName == sEncryptionDataElement)   //manifest:encryption-data
            doEncryptionData(aConvertedAttribs);
        else if (aConvertedName == sEncryptedKeyElement)   //loext:encrypted-key
            doEncryptedKey(aConvertedAttribs);
        else
            aStack.back().m_bValid = false;
        break;
    }
    case 4: {
        ManifestStack::reverse_iterator aIter = aStack.rbegin();
        ++aIter;

        if (!aIter->m_bValid)
            aStack.back().m_bValid = false;
        else if (aConvertedName == sAlgorithmElement)   //manifest:algorithm,
            doAlgorithm(aConvertedAttribs);
        else if (aConvertedName == sKeyDerivationElement) //manifest:key-derivation,
            doKeyDerivation(aConvertedAttribs);
        else if (aConvertedName == sStartKeyAlgElement)   //manifest:start-key-generation
            doStartKeyAlg(aConvertedAttribs);
        else if (aConvertedName == sEncryptionMethodElement)   //loext:encryption-method
            doEncryptionMethod(aConvertedAttribs);
        else if (aConvertedName == sKeyInfoElement)            //loext:KeyInfo
            doEncryptedKeyInfo(aConvertedAttribs);
        else if (aConvertedName == sCipherDataElement)            //loext:CipherData
            doEncryptedCipherData(aConvertedAttribs);
        else
            aStack.back().m_bValid = false;
        break;
    }
    case 5: {
        ManifestStack::reverse_iterator aIter = aStack.rbegin();
        ++aIter;

        if (!aIter->m_bValid)
            aStack.back().m_bValid = false;
        else if (aConvertedName == sPgpDataElement)   //loext:PGPData
            doEncryptedPgpData(aConvertedAttribs);
        else if (aConvertedName == sCipherValueElement) //loext:CipherValue
            // ciphervalue action happens on endElement
            aCurrentCharacters = "";
        else
            aStack.back().m_bValid = false;
        break;
    }
    case 6: {
        ManifestStack::reverse_iterator aIter = aStack.rbegin();
        ++aIter;

        if (!aIter->m_bValid)
            aStack.back().m_bValid = false;
        else if (aConvertedName == sPgpKeyIDElement)   //loext:PGPKeyID
            // ciphervalue action happens on endElement
            aCurrentCharacters = "";
        else if (aConvertedName == sPGPKeyPacketElement) //loext:PGPKeyPacket
            // ciphervalue action happens on endElement
            aCurrentCharacters = "";
        else
            aStack.back().m_bValid = false;
        break;
    }
    default:
        aStack.back().m_bValid = false;
        break;
    }
}

namespace
{
bool isEmpty(const css::beans::PropertyValue &rProp)
{
    return rProp.Name.isEmpty();
}
}

void SAL_CALL ManifestImport::endElement( const OUString& aName )
{
    size_t nLevel = aStack.size();

    assert(nLevel >= 1);

    OUString aConvertedName = ConvertName( aName );
    if ( !aStack.empty() && aStack.rbegin()->m_aConvertedName == aConvertedName ) {
        if ( aConvertedName == sFileEntryElement && aStack.back().m_bValid ) {
            // root folder gets KeyInfo entry if any, for PGP encryption
            if (!bIgnoreEncryptData && !aKeys.empty() && aSequence[PKG_MNFST_FULLPATH].Value.get<OUString>() == "/" )
            {
                aSequence[PKG_SIZE_NOENCR_MNFST].Name = "KeyInfo";
                aSequence[PKG_SIZE_NOENCR_MNFST].Value <<= comphelper::containerToSequence(aKeys);
            }
            css::beans::PropertyValue aEmpty;
            aSequence.erase(std::remove_if(aSequence.begin(), aSequence.end(),
                                           isEmpty), aSequence.end());

            bIgnoreEncryptData = false;
            rManVector.push_back ( comphelper::containerToSequence(aSequence) );

            aSequence.clear();
        }
        else if ( aConvertedName == sEncryptedKeyElement && aStack.back().m_bValid ) {
            if ( !bIgnoreEncryptData )
            {
                aKeys.push_back( comphelper::containerToSequence(aKeyInfoSequence) );
                bPgpEncryption = true;
            }
            aKeyInfoSequence.clear();
        }

        // end element handling for elements with cdata
        switch (nLevel) {
            case 5: {
                if (aConvertedName == sCipherValueElement) //loext:CipherValue
                    doEncryptedCipherValue();
                else
                    aStack.back().m_bValid = false;
                break;
            }
            case 6: {
                if (aConvertedName == sPgpKeyIDElement)   //loext:PGPKeyID
                    doEncryptedKeyId();
                else if (aConvertedName == sPGPKeyPacketElement) //loext:PGPKeyPacket
                    doEncryptedKeyPacket();
                else
                    aStack.back().m_bValid = false;
                break;
            }
        }

        aStack.pop_back();
        return;
    }
}

void SAL_CALL ManifestImport::characters( const OUString& aChars )
{
    aCurrentCharacters += aChars;
}

void SAL_CALL ManifestImport::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
{
}

void SAL_CALL ManifestImport::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ )
{
}

void SAL_CALL ManifestImport::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
{
}

OUString ManifestImport::PushNameAndNamespaces( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs, StringHashMap& o_aConvertedAttribs )
{
    StringHashMap aNamespaces;
    ::std::vector< ::std::pair< OUString, OUString > > aAttribsStrs;

    if ( xAttribs.is() ) {
        sal_Int16 nAttrCount = xAttribs.is() ? xAttribs->getLength() : 0;
        aAttribsStrs.reserve( nAttrCount );

        for( sal_Int16 nInd = 0; nInd < nAttrCount; nInd++ ) {
            OUString aAttrName = xAttribs->getNameByIndex( nInd );
            OUString aAttrValue = xAttribs->getValueByIndex( nInd );
            if ( aAttrName.getLength() >= 5
                 && aAttrName.startsWith("xmlns")
                 && ( aAttrName.getLength() == 5 || aAttrName[5] == ':' ) ) {
                // this is a namespace declaration
                OUString aNsName( ( aAttrName.getLength() == 5 ) ? OUString() : aAttrName.copy( 6 ) );
                aNamespaces[aNsName] = aAttrValue;
            } else {
                // this is no namespace declaration
                aAttribsStrs.emplace_back( aAttrName, aAttrValue );
            }
        }
    }

    OUString aConvertedName = ConvertNameWithNamespace( aName, aNamespaces );
    if ( !aConvertedName.getLength() )
        aConvertedName = ConvertName( aName );

    aStack.emplace_back( aConvertedName, aNamespaces );

    for (const std::pair<OUString,OUString> & rAttribsStr : aAttribsStrs) {
        // convert the attribute names on filling
        o_aConvertedAttribs[ConvertName( rAttribsStr.first )] = rAttribsStr.second;
    }

    return aConvertedName;
}

OUString ManifestImport::ConvertNameWithNamespace( const OUString& aName, const StringHashMap& aNamespaces )
{
    OUString aNsAlias;
    OUString aPureName = aName;

    sal_Int32 nInd = aName.indexOf( ':' );
    if ( nInd != -1 && nInd < aName.getLength() ) {
        aNsAlias = aName.copy( 0, nInd );
        aPureName = aName.copy( nInd + 1 );
    }

    OUString aResult;

    StringHashMap::const_iterator aIter = aNamespaces.find( aNsAlias );
    if ( aIter != aNamespaces.end()
         && ( aIter->second == MANIFEST_NAMESPACE || aIter->second == MANIFEST_OASIS_NAMESPACE ) ) {
        // no check for manifest.xml consistency currently since the old versions have supported inconsistent documents as well
        aResult = MANIFEST_NSPREFIX;
        aResult += aPureName;
    }

    return aResult;
}

OUString ManifestImport::ConvertName( const OUString& aName )
{
    OUString aConvertedName;
    for ( ManifestStack::reverse_iterator aIter = aStack.rbegin(); !aConvertedName.getLength() && aIter != aStack.rend(); ++aIter ) {
        if ( !aIter->m_aNamespaces.empty() )
            aConvertedName = ConvertNameWithNamespace( aName, aIter->m_aNamespaces );
    }

    if ( !aConvertedName.getLength() )
        aConvertedName = aName;

    return aConvertedName;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */