~ubuntu-branches/ubuntu/lucid/xml-security-c/lucid

« back to all changes in this revision

Viewing changes to src/tools/c14n/c14n.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Russ Allbery
  • Date: 2009-08-05 14:11:52 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090805141152-a7xyrgur2sq3exkr
Tags: 1.5.1-1
* New upstream release.
  - Rename library package for upstream SONAME bump.
* Upstream now ships an older version of libtool, so run libtoolize and
  aclocal before the build.  Add build dependencies on automake and
  libtool.
* Build against Xerces-C 3.0.
* Stop building against Xalan.  The Xalan packages for Debian have been
  orphaned, the current Xalan release does not support Xerces-C 3.0, and
  porting it is not trivial.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright 2002-2005 The Apache Software Foundation.
 
2
 * Copyright 2002-2009 The Apache Software Foundation.
3
3
 *
4
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
5
 * you may not use this file except in compliance with the License.
20
20
 * c14n := tool to dump a XML file to the console after canonacalising it thru
21
21
 *                      c14n
22
22
 *
23
 
 * $Id: c14n.cpp 351394 2005-07-22 09:55:31Z blautenb $
 
23
 * $Id: c14n.cpp 783065 2009-06-09 16:58:45Z scantor $
24
24
 *
25
25
 */
26
26
 
56
56
 
57
57
void printUsage(void) {
58
58
 
59
 
        cerr << "\nUsage: c14n [-n] <input file name>\n";
60
 
        cerr << "       -n = No comments\n\n";
 
59
        cerr << "\nUsage: c14n [-n] [-x] [-1.1] [-id ID] <input file name>\n";
 
60
        cerr << "       -n = No comments\n";
 
61
    cerr << "       -1.1 = Use c14n 1.1\n";
 
62
    cerr << "       -x = Use exclusive c14n\n";
 
63
        cerr << "       -id ID = References node to canonicalize by ID\n\n";
61
64
 
62
65
}
63
66
 
64
67
int main(int argc, char **argv) {
65
68
 
66
 
        bool printComments = true;              // By default print comments
 
69
    const char* id = NULL;
 
70
        bool printComments = true;
 
71
    bool exclusive = false;
 
72
    bool inclusive11 = false;
67
73
 
68
74
        // Check arguments
69
75
 
70
76
        if (argc < 2) {
71
 
 
72
77
                printUsage();
73
78
                exit (1);
74
79
        }
75
80
 
76
81
        if (argc > 2) {
77
 
 
78
82
                for (int i = 1; i < argc - 1; ++i) {
79
83
 
80
84
                        if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "-N"))
81
85
                                printComments = false;
 
86
            else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "-X"))
 
87
                exclusive = true;
 
88
            else if (!strcmp(argv[i], "-1.1"))
 
89
                inclusive11 = true;
 
90
            else if (!strcmp(argv[i], "-id") && argc > i + 1)
 
91
                id = argv[++i];
82
92
                        else {
83
93
                                cerr << "Unknown option %s\n\n";
84
94
                                printUsage();
109
119
 
110
120
        XercesDOMParser * parser = new XercesDOMParser;
111
121
        parser->setDoNamespaces(true);
112
 
        parser->setValidationScheme(XercesDOMParser::Val_Never);
 
122
    parser->setValidationScheme(XercesDOMParser::Val_Never);
113
123
        parser->setDoSchema(false);
114
124
        parser->setCreateEntityReferenceNodes(false);
115
125
 
116
126
        // Now parse out file
117
127
 
118
128
        bool errorsOccured = false;
119
 
        int errorCount = 0;
 
129
        xsecsize_t errorCount = 0;
120
130
    try
121
131
    {
122
132
        parser->parse(argv[argc-1]);
153
163
 
154
164
        */
155
165
        
156
 
        DOMNode *doc;           // The document that we parsed
157
 
 
158
 
        doc = parser->getDocument();
 
166
        DOMNode *subtree = NULL;
159
167
        DOMDocument *theDOM = parser->getDocument();
160
 
 
161
 
        // Creat the canonicalizer
162
 
 
163
 
        XSECC14n20010315 canon(theDOM);
164
 
        canon.setCommentsProcessing(printComments);
165
 
        canon.setUseNamespaceStack(true);
166
 
 
167
 
        // canon.XPathSelectNodes("(/descendant-or-self::node() | /descendant-or-self::node()/attribute::* | /descendant-or-self::node()/namespace::*)[ self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2)) or count (id(\"E3\") | ancestor-or-self::node()) = count (ancestor-or-self::node())]");
 
168
    if (id) {
 
169
        XMLCh* temp = XMLString::transcode(id);
 
170
        subtree = theDOM->getElementById(temp);
 
171
        XMLString::release(&temp);
 
172
        if (!subtree) {
 
173
            cerr << "ID reference did not resolve" << endl;
 
174
            exit(1);
 
175
        }
 
176
    }
 
177
 
 
178
        // Create the canonicalizer
 
179
    XSECC14n20010315* canon=NULL;
 
180
    if (subtree)
 
181
            canon = new XSECC14n20010315(theDOM, subtree);
 
182
    else
 
183
        canon = new XSECC14n20010315(theDOM);
 
184
        canon->setCommentsProcessing(printComments);
 
185
        canon->setUseNamespaceStack(true);
 
186
    if (inclusive11)
 
187
        canon->setInclusive11();
 
188
    else if (exclusive)
 
189
        canon->setExclusive();
 
190
 
 
191
        // canon->XPathSelectNodes("(/descendant-or-self::node() | /descendant-or-self::node()/attribute::* | /descendant-or-self::node()/namespace::*)[ self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2)) or count (id(\"E3\") | ancestor-or-self::node()) = count (ancestor-or-self::node())]");
168
192
 
169
193
        char buffer[512];
170
 
        int res = canon.outputBuffer((unsigned char *) buffer, 128);
 
194
        xsecsize_t res = canon->outputBuffer((unsigned char *) buffer, 128);
171
195
 
172
196
 
173
197
        while (res != 0) {
174
198
                buffer[res] = '\0';
175
199
                cout << buffer;
176
 
                res = canon.outputBuffer((unsigned char *) buffer, 128);
 
200
                res = canon->outputBuffer((unsigned char *) buffer, 128);
177
201
        }
178
202
 
179
203
        cout << endl;
180
 
        
 
204
 
 
205
    delete canon;
 
206
    delete parser;
 
207
 
 
208
    XSECPlatformUtils::Terminate();
 
209
        XMLPlatformUtils::Terminate();
 
210
 
181
211
        return 0;
182
212
}