| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Canonical XML implementation test program | 2 * Canonical XML implementation test program |
| 3 * (http://www.w3.org/TR/2001/REC-xml-c14n-20010315) | 3 * (http://www.w3.org/TR/2001/REC-xml-c14n-20010315) |
| 4 * | 4 * |
| 5 * See Copyright for the status of this software. | 5 * See Copyright for the status of this software. |
| 6 * | 6 * |
| 7 * Author: Aleksey Sanin <aleksey@aleksey.com> | 7 * Author: Aleksey Sanin <aleksey@aleksey.com> |
| 8 */ | 8 */ |
| 9 #include "libxml.h" | 9 #include "libxml.h" |
| 10 #if defined(LIBXML_C14N_ENABLED) && defined(LIBXML_OUTPUT_ENABLED) | 10 #if defined(LIBXML_C14N_ENABLED) && defined(LIBXML_OUTPUT_ENABLED) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 #include <libxml/c14n.h> | 26 #include <libxml/c14n.h> |
| 27 | 27 |
| 28 | 28 |
| 29 static void usage(const char *name) { | 29 static void usage(const char *name) { |
| 30 fprintf(stderr, | 30 fprintf(stderr, |
| 31 "Usage: %s <mode> <xml-file> [<xpath-expr>] [<inclusive-ns-list>]\n", | 31 "Usage: %s <mode> <xml-file> [<xpath-expr>] [<inclusive-ns-list>]\n", |
| 32 name); | 32 name); |
| 33 fprintf(stderr, "where <mode> is one of following:\n"); | 33 fprintf(stderr, "where <mode> is one of following:\n"); |
| 34 fprintf(stderr, | 34 fprintf(stderr, |
| 35 » "--with-comments \t XML file canonization w comments\n"); | 35 » "--with-comments \t XML file canonicalization v1.0 w comments \n")
; |
| 36 fprintf(stderr, | 36 fprintf(stderr, |
| 37 » "--without-comments \t XML file canonization w/o comments\n"); | 37 » "--without-comments \t XML file canonicalization v1.0 w/o comments\n"
); |
| 38 fprintf(stderr, | 38 fprintf(stderr, |
| 39 "--exc-with-comments \t Exclusive XML file canonization w comments\n"); | 39 » "--1-1-with-comments \t XML file canonicalization v1.1 w comments\
n"); |
| 40 fprintf(stderr, | 40 fprintf(stderr, |
| 41 "--exc-without-comments\t Exclusive XML file canonization w/o comments\n"); | 41 » "--1-1-without-comments \t XML file canonicalization v1.1 w/o comment
s\n"); |
| 42 fprintf(stderr, |
| 43 "--exc-with-comments \t Exclusive XML file canonicalization v1.0 w comment
s\n"); |
| 44 fprintf(stderr, |
| 45 "--exc-without-comments\t Exclusive XML file canonicalization v1.0 w/o comme
nts\n"); |
| 42 } | 46 } |
| 43 | 47 |
| 44 static xmlXPathObjectPtr | 48 static xmlXPathObjectPtr |
| 45 load_xpath_expr (xmlDocPtr parent_doc, const char* filename); | 49 load_xpath_expr (xmlDocPtr parent_doc, const char* filename); |
| 46 | 50 |
| 47 static xmlChar **parse_list(xmlChar *str); | 51 static xmlChar **parse_list(xmlChar *str); |
| 48 | 52 |
| 49 /* static void print_xpath_nodes(xmlNodeSetPtr nodes); */ | 53 /* static void print_xpath_nodes(xmlNodeSetPtr nodes); */ |
| 50 | 54 |
| 51 static int | 55 static int |
| 52 test_c14n(const char* xml_filename, int with_comments, int exclusive, | 56 test_c14n(const char* xml_filename, int with_comments, int mode, |
| 53 const char* xpath_filename, xmlChar **inclusive_namespaces) { | 57 const char* xpath_filename, xmlChar **inclusive_namespaces) { |
| 54 xmlDocPtr doc; | 58 xmlDocPtr doc; |
| 55 xmlXPathObjectPtr xpath = NULL; | 59 xmlXPathObjectPtr xpath = NULL; |
| 56 xmlChar *result = NULL; | 60 xmlChar *result = NULL; |
| 57 int ret; | 61 int ret; |
| 58 | 62 |
| 59 /* | 63 /* |
| 60 * build an XML tree from a the file; we need to add default | 64 * build an XML tree from a the file; we need to add default |
| 61 * attributes and resolve all character and entities references | 65 * attributes and resolve all character and entities references |
| 62 */ | 66 */ |
| (...skipping 26 matching lines...) Expand all Loading... |
| 89 return(-1); | 93 return(-1); |
| 90 } | 94 } |
| 91 } | 95 } |
| 92 | 96 |
| 93 /* | 97 /* |
| 94 * Canonical form | 98 * Canonical form |
| 95 */ | 99 */ |
| 96 /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename);
*/ | 100 /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename);
*/ |
| 97 ret = xmlC14NDocDumpMemory(doc, | 101 ret = xmlC14NDocDumpMemory(doc, |
| 98 (xpath) ? xpath->nodesetval : NULL, | 102 (xpath) ? xpath->nodesetval : NULL, |
| 99 » exclusive, inclusive_namespaces, | 103 » mode, inclusive_namespaces, |
| 100 with_comments, &result); | 104 with_comments, &result); |
| 101 if(ret >= 0) { | 105 if(ret >= 0) { |
| 102 if(result != NULL) { | 106 if(result != NULL) { |
| 103 write(1, result, ret); | 107 write(1, result, ret); |
| 104 xmlFree(result); | 108 xmlFree(result); |
| 105 } | 109 } |
| 106 } else { | 110 } else { |
| 107 fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n
", xml_filename, ret); | 111 fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n
", xml_filename, ret); |
| 108 if(result != NULL) xmlFree(result); | 112 if(result != NULL) xmlFree(result); |
| 109 xmlFreeDoc(doc); | 113 xmlFreeDoc(doc); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 128 xmlInitParser(); | 132 xmlInitParser(); |
| 129 LIBXML_TEST_VERSION | 133 LIBXML_TEST_VERSION |
| 130 | 134 |
| 131 /* | 135 /* |
| 132 * Parse command line and process file | 136 * Parse command line and process file |
| 133 */ | 137 */ |
| 134 if( argc < 3 ) { | 138 if( argc < 3 ) { |
| 135 fprintf(stderr, "Error: wrong number of arguments.\n"); | 139 fprintf(stderr, "Error: wrong number of arguments.\n"); |
| 136 usage(argv[0]); | 140 usage(argv[0]); |
| 137 } else if(strcmp(argv[1], "--with-comments") == 0) { | 141 } else if(strcmp(argv[1], "--with-comments") == 0) { |
| 138 » ret = test_c14n(argv[2], 1, 0, (argc > 3) ? argv[3] : NULL, NULL); | 142 » ret = test_c14n(argv[2], 1, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, N
ULL); |
| 139 } else if(strcmp(argv[1], "--without-comments") == 0) { | 143 } else if(strcmp(argv[1], "--without-comments") == 0) { |
| 140 » ret = test_c14n(argv[2], 0, 0, (argc > 3) ? argv[3] : NULL, NULL); | 144 » ret = test_c14n(argv[2], 0, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, N
ULL); |
| 145 } else if(strcmp(argv[1], "--1-1-with-comments") == 0) { |
| 146 » ret = test_c14n(argv[2], 1, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, N
ULL); |
| 147 } else if(strcmp(argv[1], "--1-1-without-comments") == 0) { |
| 148 » ret = test_c14n(argv[2], 0, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, N
ULL); |
| 141 } else if(strcmp(argv[1], "--exc-with-comments") == 0) { | 149 } else if(strcmp(argv[1], "--exc-with-comments") == 0) { |
| 142 xmlChar **list; | 150 xmlChar **list; |
| 143 | 151 |
| 144 /* load exclusive namespace from command line */ | 152 /* load exclusive namespace from command line */ |
| 145 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL; | 153 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL; |
| 146 » ret = test_c14n(argv[2], 1, 1, (argc > 3) ? argv[3] : NULL, list); | 154 » ret = test_c14n(argv[2], 1, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3]
: NULL, list); |
| 147 if(list != NULL) xmlFree(list); | 155 if(list != NULL) xmlFree(list); |
| 148 } else if(strcmp(argv[1], "--exc-without-comments") == 0) { | 156 } else if(strcmp(argv[1], "--exc-without-comments") == 0) { |
| 149 xmlChar **list; | 157 xmlChar **list; |
| 150 | 158 |
| 151 /* load exclusive namespace from command line */ | 159 /* load exclusive namespace from command line */ |
| 152 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL; | 160 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL; |
| 153 » ret = test_c14n(argv[2], 0, 1, (argc > 3) ? argv[3] : NULL, list); | 161 » ret = test_c14n(argv[2], 0, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3]
: NULL, list); |
| 154 if(list != NULL) xmlFree(list); | 162 if(list != NULL) xmlFree(list); |
| 155 } else { | 163 } else { |
| 156 fprintf(stderr, "Error: bad option.\n"); | 164 fprintf(stderr, "Error: bad option.\n"); |
| 157 usage(argv[0]); | 165 usage(argv[0]); |
| 158 } | 166 } |
| 159 | 167 |
| 160 /* | 168 /* |
| 161 * Shutdown libxml | 169 * Shutdown libxml |
| 162 */ | 170 */ |
| 163 xmlCleanupParser(); | 171 xmlCleanupParser(); |
| 164 xmlMemoryDump(); | 172 xmlMemoryDump(); |
| 165 | 173 |
| 166 return((ret >= 0) ? 0 : 1); | 174 return((ret >= 0) ? 0 : 1); |
| 167 } | 175 } |
| 168 | 176 |
| 169 /* | 177 /* |
| 170 * Macro used to grow the current buffer. | 178 * Macro used to grow the current buffer. |
| 171 */ | 179 */ |
| 172 #define growBufferReentrant() { \ | 180 #define growBufferReentrant() { \ |
| 173 buffer_size *= 2; \ | 181 buffer_size *= 2; \ |
| 174 buffer = (xmlChar **) \ | 182 buffer = (xmlChar **) \ |
| 175 » » xmlRealloc(buffer, buffer_size * sizeof(xmlChar*));» \ | 183 » » xmlRealloc(buffer, buffer_size * sizeof(xmlChar*));» \ |
| 176 if (buffer == NULL) { \ | 184 if (buffer == NULL) { \ |
| 177 perror("realloc failed"); \ | 185 perror("realloc failed"); \ |
| 178 return(NULL); \ | 186 return(NULL); \ |
| 179 } \ | 187 } \ |
| 180 } | 188 } |
| 181 | 189 |
| 182 static xmlChar ** | 190 static xmlChar ** |
| 183 parse_list(xmlChar *str) { | 191 parse_list(xmlChar *str) { |
| 184 xmlChar **buffer; | 192 xmlChar **buffer; |
| 185 xmlChar **out = NULL; | 193 xmlChar **out = NULL; |
| 186 int buffer_size = 0; | 194 int buffer_size = 0; |
| 187 int len; | 195 int len; |
| 188 | 196 |
| 189 if(str == NULL) { | 197 if(str == NULL) { |
| 190 return(NULL); | 198 return(NULL); |
| 191 } | 199 } |
| 192 | 200 |
| 193 len = xmlStrlen(str); | 201 len = xmlStrlen(str); |
| 194 if((str[0] == '\'') && (str[len - 1] == '\'')) { | 202 if((str[0] == '\'') && (str[len - 1] == '\'')) { |
| 195 str[len - 1] = '\0'; | 203 str[len - 1] = '\0'; |
| 196 str++; | 204 str++; |
| 197 len -= 2; | |
| 198 } | 205 } |
| 199 /* | 206 /* |
| 200 * allocate an translation buffer. | 207 * allocate an translation buffer. |
| 201 */ | 208 */ |
| 202 buffer_size = 1000; | 209 buffer_size = 1000; |
| 203 buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*)); | 210 buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*)); |
| 204 if (buffer == NULL) { | 211 if (buffer == NULL) { |
| 205 perror("malloc failed"); | 212 perror("malloc failed"); |
| 206 return(NULL); | 213 return(NULL); |
| 207 } | 214 } |
| 208 out = buffer; | 215 out = buffer; |
| 209 | 216 |
| 210 while(*str != '\0') { | 217 while(*str != '\0') { |
| 211 if (out - buffer > buffer_size - 10) { | 218 if (out - buffer > buffer_size - 10) { |
| 212 int indx = out - buffer; | 219 int indx = out - buffer; |
| 213 | 220 |
| 214 growBufferReentrant(); | 221 growBufferReentrant(); |
| 215 out = &buffer[indx]; | 222 out = &buffer[indx]; |
| 216 } | 223 } |
| 217 (*out++) = str; | 224 (*out++) = str; |
| 218 while(*str != ',' && *str != '\0') ++str; | 225 while(*str != ',' && *str != '\0') ++str; |
| 219 if(*str == ',') *(str++) = '\0'; | 226 if(*str == ',') *(str++) = '\0'; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 | 355 |
| 349 #else | 356 #else |
| 350 #include <stdio.h> | 357 #include <stdio.h> |
| 351 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { | 358 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { |
| 352 printf("%s : XPath/Canonicalization and output support not compiled in\n", a
rgv[0]); | 359 printf("%s : XPath/Canonicalization and output support not compiled in\n", a
rgv[0]); |
| 353 return(0); | 360 return(0); |
| 354 } | 361 } |
| 355 #endif /* LIBXML_C14N_ENABLED */ | 362 #endif /* LIBXML_C14N_ENABLED */ |
| 356 | 363 |
| 357 | 364 |
| OLD | NEW |