OLD | NEW |
1 /* | 1 /* |
2 * "Canonical XML" implementation | 2 * "Canonical XML" implementation |
3 * http://www.w3.org/TR/xml-c14n | 3 * http://www.w3.org/TR/xml-c14n |
4 * | 4 * |
5 * "Exclusive XML Canonicalization" implementation | 5 * "Exclusive XML Canonicalization" implementation |
6 * http://www.w3.org/TR/xml-exc-c14n | 6 * http://www.w3.org/TR/xml-exc-c14n |
7 * | 7 * |
8 * See Copyright for the status of this software. | 8 * See Copyright for the status of this software. |
9 * | 9 * |
10 * Author: Aleksey Sanin <aleksey@aleksey.com> | 10 * Author: Aleksey Sanin <aleksey@aleksey.com> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 xmlDocPtr doc; | 53 xmlDocPtr doc; |
54 xmlC14NIsVisibleCallback is_visible_callback; | 54 xmlC14NIsVisibleCallback is_visible_callback; |
55 void* user_data; | 55 void* user_data; |
56 int with_comments; | 56 int with_comments; |
57 xmlOutputBufferPtr buf; | 57 xmlOutputBufferPtr buf; |
58 | 58 |
59 /* position in the XML document */ | 59 /* position in the XML document */ |
60 xmlC14NPosition pos; | 60 xmlC14NPosition pos; |
61 int parent_is_doc; | 61 int parent_is_doc; |
62 xmlC14NVisibleNsStackPtr ns_rendered; | 62 xmlC14NVisibleNsStackPtr ns_rendered; |
| 63 |
| 64 /* C14N mode */ |
| 65 xmlC14NMode mode; |
63 | 66 |
64 /* exclusive canonicalization */ | 67 /* exclusive canonicalization */ |
65 int exclusive; | |
66 xmlChar **inclusive_ns_prefixes; | 68 xmlChar **inclusive_ns_prefixes; |
67 | 69 |
68 /* error number */ | 70 /* error number */ |
69 int error; | 71 int error; |
70 } xmlC14NCtx, *xmlC14NCtxPtr; | 72 } xmlC14NCtx, *xmlC14NCtxPtr; |
71 | 73 |
72 static xmlC14NVisibleNsStackPtr xmlC14NVisibleNsStackCreate (void); | 74 static xmlC14NVisibleNsStackPtr xmlC14NVisibleNsStackCreate (void); |
73 static void xmlC14NVisibleNsStackDestroy (xmlC14NVisibleNsStackPtr cur); | 75 static void xmlC14NVisibleNsStackDestroy (xmlC14NVisibleNsStackPtr cur); |
74 static void xmlC14NVisibleNsStackAdd (xmlC14NVisibleNsStackPtr cu
r, | 76 static void xmlC14NVisibleNsStackAdd (xmlC14NVisibleNsStackPtr cu
r, |
75 xmlNsPtr ns, | 77 xmlNsPtr ns, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 #define xmlC11NNormalizePI( a ) \ | 112 #define xmlC11NNormalizePI( a ) \ |
111 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI) | 113 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI) |
112 #define xmlC11NNormalizeText( a ) \ | 114 #define xmlC11NNormalizeText( a ) \ |
113 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT) | 115 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT) |
114 | 116 |
115 #define xmlC14NIsVisible( ctx, node, parent ) \ | 117 #define xmlC14NIsVisible( ctx, node, parent ) \ |
116 (((ctx)->is_visible_callback != NULL) ? \ | 118 (((ctx)->is_visible_callback != NULL) ? \ |
117 (ctx)->is_visible_callback((ctx)->user_data, \ | 119 (ctx)->is_visible_callback((ctx)->user_data, \ |
118 (xmlNodePtr)(node), (xmlNodePtr)(parent)) : 1) | 120 (xmlNodePtr)(node), (xmlNodePtr)(parent)) : 1) |
119 | 121 |
| 122 #define xmlC14NIsExclusive( ctx ) \ |
| 123 ( (ctx)->mode == XML_C14N_EXCLUSIVE_1_0 ) |
| 124 |
120 /************************************************************************ | 125 /************************************************************************ |
121 * * | 126 * * |
122 * Some factorized error routines * | 127 * Some factorized error routines * |
123 * * | 128 * * |
124 ************************************************************************/ | 129 ************************************************************************/ |
125 | 130 |
126 /** | 131 /** |
127 * xmlC14NErrMemory: | 132 * xmlC14NErrMemory: |
128 * @extra: extra informations | 133 * @extra: extra informations |
129 * | 134 * |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 */ | 232 */ |
228 static void | 233 static void |
229 xmlC14NErr(xmlC14NCtxPtr ctxt, xmlNodePtr node, int error, | 234 xmlC14NErr(xmlC14NCtxPtr ctxt, xmlNodePtr node, int error, |
230 const char * msg) | 235 const char * msg) |
231 { | 236 { |
232 if (ctxt != NULL) | 237 if (ctxt != NULL) |
233 ctxt->error = error; | 238 ctxt->error = error; |
234 __xmlRaiseError(NULL, NULL, NULL, | 239 __xmlRaiseError(NULL, NULL, NULL, |
235 ctxt, node, XML_FROM_C14N, error, | 240 ctxt, node, XML_FROM_C14N, error, |
236 XML_ERR_ERROR, NULL, 0, | 241 XML_ERR_ERROR, NULL, 0, |
237 » » NULL, NULL, NULL, 0, 0, msg); | 242 » » NULL, NULL, NULL, 0, 0, "%s", msg); |
238 } | 243 } |
239 | 244 |
240 /************************************************************************ | 245 /************************************************************************ |
241 * * | 246 * * |
242 * The implementation internals * | 247 * The implementation internals * |
243 * * | 248 * * |
244 ************************************************************************/ | 249 ************************************************************************/ |
245 #define XML_NAMESPACES_DEFAULT 16 | 250 #define XML_NAMESPACES_DEFAULT 16 |
246 | 251 |
247 static int | 252 static int |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 * | 490 * |
486 * Returns 1 if the node is default or 0 otherwise | 491 * Returns 1 if the node is default or 0 otherwise |
487 */ | 492 */ |
488 | 493 |
489 /* todo: make it a define? */ | 494 /* todo: make it a define? */ |
490 static int | 495 static int |
491 xmlC14NIsXmlNs(xmlNsPtr ns) | 496 xmlC14NIsXmlNs(xmlNsPtr ns) |
492 { | 497 { |
493 return ((ns != NULL) && | 498 return ((ns != NULL) && |
494 (xmlStrEqual(ns->prefix, BAD_CAST "xml")) && | 499 (xmlStrEqual(ns->prefix, BAD_CAST "xml")) && |
495 (xmlStrEqual(ns->href, | 500 (xmlStrEqual(ns->href, XML_XML_NAMESPACE))); |
496 BAD_CAST | |
497 "http://www.w3.org/XML/1998/namespace"))); | |
498 } | 501 } |
499 | 502 |
500 | 503 |
501 /** | 504 /** |
502 * xmlC14NNsCompare: | 505 * xmlC14NNsCompare: |
503 * @ns1: the pointer to first namespace | 506 * @ns1: the pointer to first namespace |
504 * @ns2: the pointer to second namespace | 507 * @ns2: the pointer to second namespace |
505 * | 508 * |
506 * Compares the namespaces by names (prefixes). | 509 * Compares the namespaces by names (prefixes). |
507 * | 510 * |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 int already_rendered; | 709 int already_rendered; |
707 int has_empty_ns = 0; | 710 int has_empty_ns = 0; |
708 int has_visibly_utilized_empty_ns = 0; | 711 int has_visibly_utilized_empty_ns = 0; |
709 int has_empty_ns_in_inclusive_list = 0; | 712 int has_empty_ns_in_inclusive_list = 0; |
710 | 713 |
711 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { | 714 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
712 xmlC14NErrParam("processing namespaces axis (exc c14n)"); | 715 xmlC14NErrParam("processing namespaces axis (exc c14n)"); |
713 return (-1); | 716 return (-1); |
714 } | 717 } |
715 | 718 |
716 if(!ctx->exclusive) { | 719 if(!xmlC14NIsExclusive(ctx)) { |
717 xmlC14NErrParam("processing namespaces axis (exc c14n)"); | 720 xmlC14NErrParam("processing namespaces axis (exc c14n)"); |
718 return (-1); | 721 return (-1); |
719 | 722 |
720 } | 723 } |
721 | 724 |
722 /* | 725 /* |
723 * Create a sorted list to store element namespaces | 726 * Create a sorted list to store element namespaces |
724 */ | 727 */ |
725 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare); | 728 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare); |
726 if (list == NULL) { | 729 if (list == NULL) { |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 | 841 |
839 /* | 842 /* |
840 * Cleanup | 843 * Cleanup |
841 */ | 844 */ |
842 xmlListDelete(list); | 845 xmlListDelete(list); |
843 return (0); | 846 return (0); |
844 } | 847 } |
845 | 848 |
846 | 849 |
847 /** | 850 /** |
| 851 * xmlC14NIsXmlAttr: |
| 852 * @attr: the attr to check |
| 853 * |
| 854 * Checks whether the given attribute is a default "xml:" namespace |
| 855 * with href="http://www.w3.org/XML/1998/namespace" |
| 856 * |
| 857 * Returns 1 if the node is default or 0 otherwise |
| 858 */ |
| 859 |
| 860 /* todo: make it a define? */ |
| 861 static int |
| 862 xmlC14NIsXmlAttr(xmlAttrPtr attr) |
| 863 { |
| 864 return ((attr->ns != NULL) && |
| 865 (xmlC14NIsXmlNs(attr->ns) != 0)); |
| 866 } |
| 867 |
| 868 |
| 869 /** |
848 * xmlC14NAttrsCompare: | 870 * xmlC14NAttrsCompare: |
849 * @attr1: the pointer tls o first attr | 871 * @attr1: the pointer tls o first attr |
850 * @attr2: the pointer to second attr | 872 * @attr2: the pointer to second attr |
851 * | 873 * |
852 * Prints the given attribute to the output buffer from C14N context. | 874 * Prints the given attribute to the output buffer from C14N context. |
853 * | 875 * |
854 * Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2. | 876 * Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2. |
855 */ | 877 */ |
856 static int | 878 static int |
857 xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2) | 879 xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 | 940 |
919 xmlOutputBufferWriteString(ctx->buf, " "); | 941 xmlOutputBufferWriteString(ctx->buf, " "); |
920 if (attr->ns != NULL && xmlStrlen(attr->ns->prefix) > 0) { | 942 if (attr->ns != NULL && xmlStrlen(attr->ns->prefix) > 0) { |
921 xmlOutputBufferWriteString(ctx->buf, | 943 xmlOutputBufferWriteString(ctx->buf, |
922 (const char *) attr->ns->prefix); | 944 (const char *) attr->ns->prefix); |
923 xmlOutputBufferWriteString(ctx->buf, ":"); | 945 xmlOutputBufferWriteString(ctx->buf, ":"); |
924 } | 946 } |
925 xmlOutputBufferWriteString(ctx->buf, (const char *) attr->name); | 947 xmlOutputBufferWriteString(ctx->buf, (const char *) attr->name); |
926 xmlOutputBufferWriteString(ctx->buf, "=\""); | 948 xmlOutputBufferWriteString(ctx->buf, "=\""); |
927 | 949 |
928 value = xmlNodeListGetString(attr->doc, attr->children, 1); | 950 value = xmlNodeListGetString(ctx->doc, attr->children, 1); |
929 /* todo: should we log an error if value==NULL ? */ | 951 /* todo: should we log an error if value==NULL ? */ |
930 if (value != NULL) { | 952 if (value != NULL) { |
931 buffer = xmlC11NNormalizeAttr(value); | 953 buffer = xmlC11NNormalizeAttr(value); |
932 xmlFree(value); | 954 xmlFree(value); |
933 if (buffer != NULL) { | 955 if (buffer != NULL) { |
934 xmlOutputBufferWriteString(ctx->buf, (const char *) buffer); | 956 xmlOutputBufferWriteString(ctx->buf, (const char *) buffer); |
935 xmlFree(buffer); | 957 xmlFree(buffer); |
936 } else { | 958 } else { |
937 xmlC14NErrInternal("normalizing attributes axis"); | 959 xmlC14NErrInternal("normalizing attributes axis"); |
938 return (0); | 960 return (0); |
939 } | 961 } |
940 } | 962 } |
941 xmlOutputBufferWriteString(ctx->buf, "\""); | 963 xmlOutputBufferWriteString(ctx->buf, "\""); |
942 return (1); | 964 return (1); |
943 } | 965 } |
944 | 966 |
945 /** | 967 /** |
| 968 * xmlC14NFindHiddenParentAttr: |
| 969 * |
| 970 * Finds an attribute in a hidden parent node. |
| 971 * |
| 972 * Returns a pointer to the attribute node (if found) or NULL otherwise. |
| 973 */ |
| 974 static xmlAttrPtr |
| 975 xmlC14NFindHiddenParentAttr(xmlC14NCtxPtr ctx, xmlNodePtr cur, const xmlChar * n
ame, const xmlChar * ns) |
| 976 { |
| 977 xmlAttrPtr res; |
| 978 while((cur != NULL) && (!xmlC14NIsVisible(ctx, cur, cur->parent))) { |
| 979 res = xmlHasNsProp(cur, name, ns); |
| 980 if(res != NULL) { |
| 981 return res; |
| 982 } |
| 983 |
| 984 cur = cur->parent; |
| 985 } |
| 986 |
| 987 return NULL; |
| 988 } |
| 989 |
| 990 /** |
| 991 * xmlC14NFixupBaseAttr: |
| 992 * |
| 993 * Fixes up the xml:base attribute |
| 994 * |
| 995 * Returns the newly created attribute or NULL |
| 996 */ |
| 997 static xmlAttrPtr |
| 998 xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml_base_attr) |
| 999 { |
| 1000 xmlChar * res = NULL; |
| 1001 xmlNodePtr cur; |
| 1002 xmlAttrPtr attr; |
| 1003 xmlChar * tmp_str; |
| 1004 xmlChar * tmp_str2; |
| 1005 int tmp_str_len; |
| 1006 |
| 1007 if ((ctx == NULL) || (xml_base_attr == NULL) || (xml_base_attr->parent == NU
LL)) { |
| 1008 xmlC14NErrParam("processing xml:base attribute"); |
| 1009 return (NULL); |
| 1010 } |
| 1011 |
| 1012 /* start from current value */ |
| 1013 res = xmlNodeListGetString(ctx->doc, xml_base_attr->children, 1); |
| 1014 if(res == NULL) { |
| 1015 xmlC14NErrInternal("processing xml:base attribute - can't get attr value
"); |
| 1016 return (NULL); |
| 1017 } |
| 1018 |
| 1019 /* go up the stack until we find a node that we rendered already */ |
| 1020 cur = xml_base_attr->parent->parent; |
| 1021 while((cur != NULL) && (!xmlC14NIsVisible(ctx, cur, cur->parent))) { |
| 1022 attr = xmlHasNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
| 1023 if(attr != NULL) { |
| 1024 /* get attr value */ |
| 1025 tmp_str = xmlNodeListGetString(ctx->doc, attr->children, 1); |
| 1026 if(tmp_str == NULL) { |
| 1027 xmlFree(res); |
| 1028 |
| 1029 xmlC14NErrInternal("processing xml:base attribute - can't get at
tr value"); |
| 1030 return (NULL); |
| 1031 } |
| 1032 |
| 1033 /* we need to add '/' if our current base uri ends with '..' or '.' |
| 1034 to ensure that we are forced to go "up" all the time */ |
| 1035 tmp_str_len = xmlStrlen(tmp_str); |
| 1036 if(tmp_str_len > 1 && tmp_str[tmp_str_len - 2] == '.') { |
| 1037 tmp_str2 = xmlStrcat(tmp_str, BAD_CAST "/"); |
| 1038 if(tmp_str2 == NULL) { |
| 1039 xmlFree(tmp_str); |
| 1040 xmlFree(res); |
| 1041 |
| 1042 xmlC14NErrInternal("processing xml:base attribute - can't mo
dify uri"); |
| 1043 return (NULL); |
| 1044 } |
| 1045 |
| 1046 tmp_str = tmp_str2; |
| 1047 } |
| 1048 |
| 1049 /* build uri */ |
| 1050 tmp_str2 = xmlBuildURI(res, tmp_str); |
| 1051 if(tmp_str2 == NULL) { |
| 1052 xmlFree(tmp_str); |
| 1053 xmlFree(res); |
| 1054 |
| 1055 xmlC14NErrInternal("processing xml:base attribute - can't constr
uct uri"); |
| 1056 return (NULL); |
| 1057 } |
| 1058 |
| 1059 /* cleanup and set the new res */ |
| 1060 xmlFree(tmp_str); |
| 1061 xmlFree(res); |
| 1062 res = tmp_str2; |
| 1063 } |
| 1064 |
| 1065 /* next */ |
| 1066 cur = cur->parent; |
| 1067 } |
| 1068 |
| 1069 /* check if result uri is empty or not */ |
| 1070 if((res == NULL) || xmlStrEqual(res, BAD_CAST "")) { |
| 1071 xmlFree(res); |
| 1072 return (NULL); |
| 1073 } |
| 1074 |
| 1075 /* create and return the new attribute node */ |
| 1076 attr = xmlNewNsProp(NULL, xml_base_attr->ns, BAD_CAST "base", res); |
| 1077 if(attr == NULL) { |
| 1078 xmlFree(res); |
| 1079 |
| 1080 xmlC14NErrInternal("processing xml:base attribute - can't construct attr
ibute"); |
| 1081 return (NULL); |
| 1082 } |
| 1083 |
| 1084 /* done */ |
| 1085 xmlFree(res); |
| 1086 return (attr); |
| 1087 } |
| 1088 |
| 1089 /** |
946 * xmlC14NProcessAttrsAxis: | 1090 * xmlC14NProcessAttrsAxis: |
947 * @ctx: the C14N context | 1091 * @ctx: the C14N context |
948 * @cur: the current node | 1092 * @cur: the current node |
949 * @parent_visible: the visibility of parent node | 1093 * @parent_visible: the visibility of parent node |
| 1094 * @all_parents_visible: the visibility of all parent nodes |
950 * | 1095 * |
951 * Prints out canonical attribute axis of the current node to the | 1096 * Prints out canonical attribute axis of the current node to the |
952 * buffer from C14N context as follows | 1097 * buffer from C14N context as follows |
953 * | 1098 * |
954 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) | 1099 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
955 * | 1100 * |
956 * Attribute Axis | 1101 * Attribute Axis |
957 * In lexicographic order (ascending), process each node that | 1102 * In lexicographic order (ascending), process each node that |
958 * is in the element's attribute axis and in the node-set. | 1103 * is in the element's attribute axis and in the node-set. |
959 * | 1104 * |
(...skipping 11 matching lines...) Expand all Loading... |
971 * in the attribute axis of the element (whether or not it is included in | 1116 * in the attribute axis of the element (whether or not it is included in |
972 * the document subset). This search and copying are omitted from the | 1117 * the document subset). This search and copying are omitted from the |
973 * Exclusive XML Canonicalization method. | 1118 * Exclusive XML Canonicalization method. |
974 * | 1119 * |
975 * Returns 0 on success or -1 on fail. | 1120 * Returns 0 on success or -1 on fail. |
976 */ | 1121 */ |
977 static int | 1122 static int |
978 xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int parent_visible) | 1123 xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int parent_visible) |
979 { | 1124 { |
980 xmlAttrPtr attr; | 1125 xmlAttrPtr attr; |
981 xmlListPtr list; | 1126 xmlListPtr list; |
| 1127 xmlAttrPtr attrs_to_delete = NULL; |
| 1128 |
| 1129 /* special processing for 1.1 spec */ |
| 1130 xmlAttrPtr xml_base_attr = NULL; |
| 1131 xmlAttrPtr xml_lang_attr = NULL; |
| 1132 xmlAttrPtr xml_space_attr = NULL; |
982 | 1133 |
983 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { | 1134 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
984 xmlC14NErrParam("processing attributes axis"); | 1135 xmlC14NErrParam("processing attributes axis"); |
985 return (-1); | 1136 return (-1); |
986 } | 1137 } |
987 | 1138 |
988 /* | 1139 /* |
989 * Create a sorted list to store element attributes | 1140 * Create a sorted list to store element attributes |
990 */ | 1141 */ |
991 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NAttrsCompare); | 1142 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NAttrsCompare); |
992 if (list == NULL) { | 1143 if (list == NULL) { |
993 xmlC14NErrInternal("creating attributes list"); | 1144 xmlC14NErrInternal("creating attributes list"); |
994 return (-1); | 1145 return (-1); |
995 } | 1146 } |
996 | 1147 |
997 /* | 1148 switch(ctx->mode) { |
998 * Add all visible attributes from current node. | 1149 case XML_C14N_1_0: |
999 */ | 1150 /* The processing of an element node E MUST be modified slightly when an
XPath node-set is |
1000 attr = cur->properties; | 1151 * given as input and the element's parent is omitted from the node-set.
The method for processing |
1001 while (attr != NULL) { | 1152 * the attribute axis of an element E in the node-set is enhanced. All e
lement nodes along E's |
1002 /* check that attribute is visible */ | 1153 * ancestor axis are examined for nearest occurrences of attributes in t
he xml namespace, such |
1003 if (xmlC14NIsVisible(ctx, attr, cur)) { | 1154 * as xml:lang and xml:space (whether or not they are in the node-set).
From this list of attributes, |
1004 xmlListInsert(list, attr); | 1155 * remove any that are in E's attribute axis (whether or not they are in
the node-set). Then, |
| 1156 * lexicographically merge this attribute list with the nodes of E's att
ribute axis that are in |
| 1157 * the node-set. The result of visiting the attribute axis is computed b
y processing the attribute |
| 1158 * nodes in this merged attribute list. |
| 1159 */ |
| 1160 |
| 1161 /* |
| 1162 * Add all visible attributes from current node. |
| 1163 */ |
| 1164 attr = cur->properties; |
| 1165 while (attr != NULL) { |
| 1166 /* check that attribute is visible */ |
| 1167 if (xmlC14NIsVisible(ctx, attr, cur)) { |
| 1168 xmlListInsert(list, attr); |
| 1169 } |
| 1170 attr = attr->next; |
1005 } | 1171 } |
1006 attr = attr->next; | 1172 |
| 1173 /* |
| 1174 * Handle xml attributes |
| 1175 */ |
| 1176 if (parent_visible && (cur->parent != NULL) && |
| 1177 (!xmlC14NIsVisible(ctx, cur->parent, cur->parent->parent))) |
| 1178 { |
| 1179 xmlNodePtr tmp; |
| 1180 |
| 1181 /* |
| 1182 * If XPath node-set is not specified then the parent is always |
| 1183 * visible! |
| 1184 */ |
| 1185 tmp = cur->parent; |
| 1186 while (tmp != NULL) { |
| 1187 attr = tmp->properties; |
| 1188 while (attr != NULL) { |
| 1189 if (xmlC14NIsXmlAttr(attr) != 0) { |
| 1190 if (xmlListSearch(list, attr) == NULL) { |
| 1191 xmlListInsert(list, attr); |
| 1192 } |
| 1193 } |
| 1194 attr = attr->next; |
| 1195 } |
| 1196 tmp = tmp->parent; |
| 1197 } |
| 1198 } |
| 1199 |
| 1200 /* done */ |
| 1201 break; |
| 1202 case XML_C14N_EXCLUSIVE_1_0: |
| 1203 /* attributes in the XML namespace, such as xml:lang and xml:space |
| 1204 * are not imported into orphan nodes of the document subset |
| 1205 */ |
| 1206 |
| 1207 /* |
| 1208 * Add all visible attributes from current node. |
| 1209 */ |
| 1210 attr = cur->properties; |
| 1211 while (attr != NULL) { |
| 1212 /* check that attribute is visible */ |
| 1213 if (xmlC14NIsVisible(ctx, attr, cur)) { |
| 1214 xmlListInsert(list, attr); |
| 1215 } |
| 1216 attr = attr->next; |
| 1217 } |
| 1218 |
| 1219 /* do nothing special for xml attributes */ |
| 1220 break; |
| 1221 case XML_C14N_1_1: |
| 1222 /* The processing of an element node E MUST be modified slightly when an
XPath node-set is |
| 1223 * given as input and some of the element's ancestors are omitted from t
he node-set. |
| 1224 * |
| 1225 * Simple inheritable attributes are attributes that have a value that r
equires at most a simple |
| 1226 * redeclaration. This redeclaration is done by supplying a new value in
the child axis. The |
| 1227 * redeclaration of a simple inheritable attribute A contained in one of
E's ancestors is done |
| 1228 * by supplying a value to an attribute Ae inside E with the same name.
Simple inheritable attributes |
| 1229 * are xml:lang and xml:space. |
| 1230 * |
| 1231 * The method for processing the attribute axis of an element E in the n
ode-set is hence enhanced. |
| 1232 * All element nodes along E's ancestor axis are examined for the neares
t occurrences of simple |
| 1233 * inheritable attributes in the xml namespace, such as xml:lang and xml
:space (whether or not they |
| 1234 * are in the node-set). From this list of attributes, any simple inheri
table attributes that are |
| 1235 * already in E's attribute axis (whether or not they are in the node-se
t) are removed. Then, |
| 1236 * lexicographically merge this attribute list with the nodes of E's att
ribute axis that are in |
| 1237 * the node-set. The result of visiting the attribute axis is computed b
y processing the attribute |
| 1238 * nodes in this merged attribute list. |
| 1239 * |
| 1240 * The xml:id attribute is not a simple inheritable attribute and no pro
cessing of these attributes is |
| 1241 * performed. |
| 1242 * |
| 1243 * The xml:base attribute is not a simple inheritable attribute and requ
ires special processing beyond |
| 1244 * a simple redeclaration. |
| 1245 * |
| 1246 * Attributes in the XML namespace other than xml:base, xml:id, xml:lang
, and xml:space MUST be processed |
| 1247 * as ordinary attributes. |
| 1248 */ |
| 1249 |
| 1250 /* |
| 1251 * Add all visible attributes from current node. |
| 1252 */ |
| 1253 attr = cur->properties; |
| 1254 while (attr != NULL) { |
| 1255 /* special processing for XML attribute kiks in only when we have in
visible parents */ |
| 1256 if ((!parent_visible) || (xmlC14NIsXmlAttr(attr) == 0)) { |
| 1257 /* check that attribute is visible */ |
| 1258 if (xmlC14NIsVisible(ctx, attr, cur)) { |
| 1259 xmlListInsert(list, attr); |
| 1260 } |
| 1261 } else { |
| 1262 int matched = 0; |
| 1263 |
| 1264 /* check for simple inheritance attributes */ |
| 1265 if((!matched) && (xml_lang_attr == NULL) && xmlStrEqual(attr->na
me, BAD_CAST "lang")) { |
| 1266 xml_lang_attr = attr; |
| 1267 matched = 1; |
| 1268 } |
| 1269 if((!matched) && (xml_space_attr == NULL) && xmlStrEqual(attr->n
ame, BAD_CAST "space")) { |
| 1270 xml_space_attr = attr; |
| 1271 matched = 1; |
| 1272 } |
| 1273 |
| 1274 /* check for base attr */ |
| 1275 if((!matched) && (xml_base_attr == NULL) && xmlStrEqual(attr->na
me, BAD_CAST "base")) { |
| 1276 xml_base_attr = attr; |
| 1277 matched = 1; |
| 1278 } |
| 1279 |
| 1280 /* otherwise, it is a normal attribute, so just check if it is v
isible */ |
| 1281 if((!matched) && xmlC14NIsVisible(ctx, attr, cur)) { |
| 1282 xmlListInsert(list, attr); |
| 1283 } |
| 1284 } |
| 1285 |
| 1286 /* move to the next one */ |
| 1287 attr = attr->next; |
| 1288 } |
| 1289 |
| 1290 /* special processing for XML attribute kiks in only when we have invisi
ble parents */ |
| 1291 if ((parent_visible)) { |
| 1292 |
| 1293 /* simple inheritance attributes - copy */ |
| 1294 if(xml_lang_attr == NULL) { |
| 1295 xml_lang_attr = xmlC14NFindHiddenParentAttr(ctx, cur->parent, BA
D_CAST "lang", XML_XML_NAMESPACE); |
| 1296 } |
| 1297 if(xml_lang_attr != NULL) { |
| 1298 xmlListInsert(list, xml_lang_attr); |
| 1299 } |
| 1300 if(xml_space_attr == NULL) { |
| 1301 xml_space_attr = xmlC14NFindHiddenParentAttr(ctx, cur->parent, B
AD_CAST "space", XML_XML_NAMESPACE); |
| 1302 } |
| 1303 if(xml_space_attr != NULL) { |
| 1304 xmlListInsert(list, xml_space_attr); |
| 1305 } |
| 1306 |
| 1307 /* base uri attribute - fix up */ |
| 1308 if(xml_base_attr == NULL) { |
| 1309 /* if we don't have base uri attribute, check if we have a "hidd
en" one above */ |
| 1310 xml_base_attr = xmlC14NFindHiddenParentAttr(ctx, cur->parent, BA
D_CAST "base", XML_XML_NAMESPACE); |
| 1311 } |
| 1312 if(xml_base_attr != NULL) { |
| 1313 xml_base_attr = xmlC14NFixupBaseAttr(ctx, xml_base_attr); |
| 1314 if(xml_base_attr != NULL) { |
| 1315 xmlListInsert(list, xml_base_attr); |
| 1316 |
| 1317 /* note that we MUST delete returned attr node ourselves! */ |
| 1318 xml_base_attr->next = attrs_to_delete; |
| 1319 attrs_to_delete = xml_base_attr; |
| 1320 } |
| 1321 } |
| 1322 } |
| 1323 |
| 1324 /* done */ |
| 1325 break; |
1007 } | 1326 } |
1008 | 1327 |
1009 /* | 1328 /* |
1010 * include attributes in "xml" namespace defined in ancestors | |
1011 * (only for non-exclusive XML Canonicalization) | |
1012 */ | |
1013 if (parent_visible && (!ctx->exclusive) && (cur->parent != NULL) | |
1014 && (!xmlC14NIsVisible(ctx, cur->parent, cur->parent->parent))) { | |
1015 /* | |
1016 * If XPath node-set is not specified then the parent is always | |
1017 * visible! | |
1018 */ | |
1019 cur = cur->parent; | |
1020 while (cur != NULL) { | |
1021 attr = cur->properties; | |
1022 while (attr != NULL) { | |
1023 if ((attr->ns != NULL) | |
1024 && (xmlStrEqual(attr->ns->prefix, BAD_CAST "xml"))) { | |
1025 if (xmlListSearch(list, attr) == NULL) { | |
1026 xmlListInsert(list, attr); | |
1027 } | |
1028 } | |
1029 attr = attr->next; | |
1030 } | |
1031 cur = cur->parent; | |
1032 } | |
1033 } | |
1034 | |
1035 /* | |
1036 * print out all elements from list | 1329 * print out all elements from list |
1037 */ | 1330 */ |
1038 xmlListWalk(list, (xmlListWalker) xmlC14NPrintAttrs, (const void *) ctx); | 1331 xmlListWalk(list, (xmlListWalker) xmlC14NPrintAttrs, (const void *) ctx); |
1039 | 1332 |
1040 /* | 1333 /* |
1041 * Cleanup | 1334 * Cleanup |
1042 */ | 1335 */ |
| 1336 xmlFreePropList(attrs_to_delete); |
1043 xmlListDelete(list); | 1337 xmlListDelete(list); |
1044 return (0); | 1338 return (0); |
1045 } | 1339 } |
1046 | 1340 |
1047 /** | 1341 /** |
1048 * xmlC14NCheckForRelativeNamespaces: | 1342 * xmlC14NCheckForRelativeNamespaces: |
1049 * @ctx: the C14N context | 1343 * @ctx: the C14N context |
1050 * @cur: the current element node | 1344 * @cur: the current element node |
1051 * | 1345 * |
1052 * Checks that current element node has no relative namespaces defined | 1346 * Checks that current element node has no relative namespaces defined |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1089 } | 1383 } |
1090 ns = ns->next; | 1384 ns = ns->next; |
1091 } | 1385 } |
1092 return (0); | 1386 return (0); |
1093 } | 1387 } |
1094 | 1388 |
1095 /** | 1389 /** |
1096 * xmlC14NProcessElementNode: | 1390 * xmlC14NProcessElementNode: |
1097 * @ctx: the pointer to C14N context object | 1391 * @ctx: the pointer to C14N context object |
1098 * @cur: the node to process | 1392 * @cur: the node to process |
| 1393 * @visible: this node is visible |
| 1394 * @all_parents_visible: whether all the parents of this node are visible |
1099 * | 1395 * |
1100 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) | 1396 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
1101 * | 1397 * |
1102 * Element Nodes | 1398 * Element Nodes |
1103 * If the element is not in the node-set, then the result is obtained | 1399 * If the element is not in the node-set, then the result is obtained |
1104 * by processing the namespace axis, then the attribute axis, then | 1400 * by processing the namespace axis, then the attribute axis, then |
1105 * processing the child nodes of the element that are in the node-set | 1401 * processing the child nodes of the element that are in the node-set |
1106 * (in document order). If the element is in the node-set, then the result | 1402 * (in document order). If the element is in the node-set, then the result |
1107 * is an open angle bracket (<), the element QName, the result of | 1403 * is an open angle bracket (<), the element QName, the result of |
1108 * processing the namespace axis, the result of processing the attribute | 1404 * processing the namespace axis, the result of processing the attribute |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1152 xmlOutputBufferWriteString(ctx->buf, "<"); | 1448 xmlOutputBufferWriteString(ctx->buf, "<"); |
1153 | 1449 |
1154 if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) { | 1450 if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) { |
1155 xmlOutputBufferWriteString(ctx->buf, | 1451 xmlOutputBufferWriteString(ctx->buf, |
1156 (const char *) cur->ns->prefix); | 1452 (const char *) cur->ns->prefix); |
1157 xmlOutputBufferWriteString(ctx->buf, ":"); | 1453 xmlOutputBufferWriteString(ctx->buf, ":"); |
1158 } | 1454 } |
1159 xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name); | 1455 xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name); |
1160 } | 1456 } |
1161 | 1457 |
1162 if (!ctx->exclusive) { | 1458 if (!xmlC14NIsExclusive(ctx)) { |
1163 ret = xmlC14NProcessNamespacesAxis(ctx, cur, visible); | 1459 ret = xmlC14NProcessNamespacesAxis(ctx, cur, visible); |
1164 } else { | 1460 } else { |
1165 ret = xmlExcC14NProcessNamespacesAxis(ctx, cur, visible); | 1461 ret = xmlExcC14NProcessNamespacesAxis(ctx, cur, visible); |
1166 } | 1462 } |
1167 if (ret < 0) { | 1463 if (ret < 0) { |
1168 xmlC14NErrInternal("processing namespaces axis"); | 1464 xmlC14NErrInternal("processing namespaces axis"); |
1169 return (-1); | 1465 return (-1); |
1170 } | 1466 } |
1171 /* todo: shouldn't this go to "visible only"? */ | 1467 /* todo: shouldn't this go to "visible only"? */ |
1172 if(visible) { | 1468 if(visible) { |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1450 xmlFree(ctx); | 1746 xmlFree(ctx); |
1451 } | 1747 } |
1452 | 1748 |
1453 /** | 1749 /** |
1454 * xmlC14NNewCtx: | 1750 * xmlC14NNewCtx: |
1455 * @doc: the XML document for canonization | 1751 * @doc: the XML document for canonization |
1456 * @is_visible_callback:the function to use to determine is node visible | 1752 * @is_visible_callback:the function to use to determine is node visible |
1457 * or not | 1753 * or not |
1458 * @user_data: the first parameter for @is_visible_callback function | 1754 * @user_data: the first parameter for @is_visible_callback function |
1459 * (in most cases, it is nodes set) | 1755 * (in most cases, it is nodes set) |
| 1756 * @mode: the c14n mode (see @xmlC14NMode) |
1460 * @inclusive_ns_prefixe the list of inclusive namespace prefixes | 1757 * @inclusive_ns_prefixe the list of inclusive namespace prefixes |
1461 * ended with a NULL or NULL if there is no | 1758 * ended with a NULL or NULL if there is no |
1462 *» » » inclusive namespaces (only for exclusive | 1759 *» » » inclusive namespaces (only for ` |
1463 * canonicalization) | 1760 * canonicalization) |
1464 * @with_comments: include comments in the result (!=0) or not (==0) | 1761 * @with_comments: include comments in the result (!=0) or not (==0) |
1465 * @buf: the output buffer to store canonical XML; this | 1762 * @buf: the output buffer to store canonical XML; this |
1466 * buffer MUST have encoder==NULL because C14N requires | 1763 * buffer MUST have encoder==NULL because C14N requires |
1467 * UTF-8 output | 1764 * UTF-8 output |
1468 * | 1765 * |
1469 * Creates new C14N context object to store C14N parameters. | 1766 * Creates new C14N context object to store C14N parameters. |
1470 * | 1767 * |
1471 * Returns pointer to newly created object (success) or NULL (fail) | 1768 * Returns pointer to newly created object (success) or NULL (fail) |
1472 */ | 1769 */ |
1473 static xmlC14NCtxPtr | 1770 static xmlC14NCtxPtr |
1474 xmlC14NNewCtx(xmlDocPtr doc, | 1771 xmlC14NNewCtx(xmlDocPtr doc, |
1475 xmlC14NIsVisibleCallback is_visible_callback, void* user_data, | 1772 xmlC14NIsVisibleCallback is_visible_callback, void* user_data, |
1476 int exclusive, xmlChar ** inclusive_ns_prefixes, | 1773 xmlC14NMode mode, xmlChar ** inclusive_ns_prefixes, |
1477 int with_comments, xmlOutputBufferPtr buf) | 1774 int with_comments, xmlOutputBufferPtr buf) |
1478 { | 1775 { |
1479 xmlC14NCtxPtr ctx = NULL; | 1776 xmlC14NCtxPtr ctx = NULL; |
1480 | 1777 |
1481 if ((doc == NULL) || (buf == NULL)) { | 1778 if ((doc == NULL) || (buf == NULL)) { |
1482 xmlC14NErrParam("creating new context"); | 1779 xmlC14NErrParam("creating new context"); |
1483 return (NULL); | 1780 return (NULL); |
1484 } | 1781 } |
1485 | 1782 |
1486 /* | 1783 /* |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1524 ctx->ns_rendered = xmlC14NVisibleNsStackCreate(); | 1821 ctx->ns_rendered = xmlC14NVisibleNsStackCreate(); |
1525 | 1822 |
1526 if(ctx->ns_rendered == NULL) { | 1823 if(ctx->ns_rendered == NULL) { |
1527 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_CREATE_STACK, | 1824 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_CREATE_STACK, |
1528 "xmlC14NNewCtx: xmlC14NVisibleNsStackCreate failed\n"); | 1825 "xmlC14NNewCtx: xmlC14NVisibleNsStackCreate failed\n"); |
1529 xmlC14NFreeCtx(ctx); | 1826 xmlC14NFreeCtx(ctx); |
1530 return (NULL); | 1827 return (NULL); |
1531 } | 1828 } |
1532 | 1829 |
1533 /* | 1830 /* |
1534 * Set "exclusive" flag, create a nodes set for namespaces | 1831 * Set "mode" flag and remember list of incluseve prefixes |
1535 * stack and remember list of incluseve prefixes | 1832 * for exclusive c14n |
1536 */ | 1833 */ |
1537 if (exclusive) { | 1834 ctx->mode = mode; |
1538 ctx->exclusive = 1; | 1835 if(xmlC14NIsExclusive(ctx)) { |
1539 ctx->inclusive_ns_prefixes = inclusive_ns_prefixes; | 1836 ctx->inclusive_ns_prefixes = inclusive_ns_prefixes; |
1540 } | 1837 } |
1541 return (ctx); | 1838 return (ctx); |
1542 } | 1839 } |
1543 | 1840 |
1544 /** | 1841 /** |
1545 * xmlC14NExecute: | 1842 * xmlC14NExecute: |
1546 * @doc: the XML document for canonization | 1843 * @doc: the XML document for canonization |
1547 * @is_visible_callback:the function to use to determine is node visible | 1844 * @is_visible_callback:the function to use to determine is node visible |
1548 * or not | 1845 * or not |
1549 * @user_data: the first parameter for @is_visible_callback function | 1846 * @user_data: the first parameter for @is_visible_callback function |
1550 * (in most cases, it is nodes set) | 1847 * (in most cases, it is nodes set) |
1551 * @exclusive:» » the exclusive flag (0 - non-exclusive canonicalization; | 1848 * @mode:» the c14n mode (see @xmlC14NMode) |
1552 *» » » otherwise - exclusive canonicalization) | |
1553 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes | 1849 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
1554 * ended with a NULL or NULL if there is no | 1850 * ended with a NULL or NULL if there is no |
1555 * inclusive namespaces (only for exclusive | 1851 * inclusive namespaces (only for exclusive |
1556 * canonicalization, ignored otherwise) | 1852 * canonicalization, ignored otherwise) |
1557 * @with_comments: include comments in the result (!=0) or not (==0) | 1853 * @with_comments: include comments in the result (!=0) or not (==0) |
1558 * @buf: the output buffer to store canonical XML; this | 1854 * @buf: the output buffer to store canonical XML; this |
1559 * buffer MUST have encoder==NULL because C14N requires | 1855 * buffer MUST have encoder==NULL because C14N requires |
1560 * UTF-8 output | 1856 * UTF-8 output |
1561 * | 1857 * |
1562 * Dumps the canonized image of given XML document into the provided buffer. | 1858 * Dumps the canonized image of given XML document into the provided buffer. |
1563 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or | 1859 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
1564 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) | 1860 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
1565 * | 1861 * |
1566 * Returns non-negative value on success or a negative value on fail | 1862 * Returns non-negative value on success or a negative value on fail |
1567 */ | 1863 */ |
1568 int | 1864 int |
1569 xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback is_visible_callback, | 1865 xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback is_visible_callback, |
1570 » void* user_data, int exclusive, xmlChar **inclusive_ns_prefixes, | 1866 » void* user_data, int mode, xmlChar **inclusive_ns_prefixes, |
1571 int with_comments, xmlOutputBufferPtr buf) { | 1867 int with_comments, xmlOutputBufferPtr buf) { |
1572 | 1868 |
1573 xmlC14NCtxPtr ctx; | 1869 xmlC14NCtxPtr ctx; |
| 1870 xmlC14NMode c14n_mode = XML_C14N_1_0; |
1574 int ret; | 1871 int ret; |
1575 | 1872 |
1576 if ((buf == NULL) || (doc == NULL)) { | 1873 if ((buf == NULL) || (doc == NULL)) { |
1577 xmlC14NErrParam("executing c14n"); | 1874 xmlC14NErrParam("executing c14n"); |
1578 return (-1); | 1875 return (-1); |
1579 } | 1876 } |
1580 | 1877 |
| 1878 /* for backward compatibility, we have to have "mode" as "int" |
| 1879 and here we check that user gives valid value */ |
| 1880 switch(mode) { |
| 1881 case XML_C14N_1_0: |
| 1882 case XML_C14N_EXCLUSIVE_1_0: |
| 1883 case XML_C14N_1_1: |
| 1884 c14n_mode = (xmlC14NMode)mode; |
| 1885 break; |
| 1886 default: |
| 1887 xmlC14NErrParam("invalid mode for executing c14n"); |
| 1888 return (-1); |
| 1889 } |
| 1890 |
1581 /* | 1891 /* |
1582 * Validate the encoding output buffer encoding | 1892 * Validate the encoding output buffer encoding |
1583 */ | 1893 */ |
1584 if (buf->encoder != NULL) { | 1894 if (buf->encoder != NULL) { |
1585 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8, | 1895 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8, |
1586 "xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n")
; | 1896 "xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n")
; |
1587 return (-1); | 1897 return (-1); |
1588 } | 1898 } |
1589 | 1899 |
1590 ctx = xmlC14NNewCtx(doc, is_visible_callback, user_data, | 1900 ctx = xmlC14NNewCtx(doc, is_visible_callback, user_data, |
1591 » » » exclusive, inclusive_ns_prefixes, | 1901 » c14n_mode, inclusive_ns_prefixes, |
1592 with_comments, buf); | 1902 with_comments, buf); |
1593 if (ctx == NULL) { | 1903 if (ctx == NULL) { |
1594 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_CREATE_CTXT, | 1904 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_CREATE_CTXT, |
1595 "xmlC14NExecute: unable to create C14N context\n"); | 1905 "xmlC14NExecute: unable to create C14N context\n"); |
1596 return (-1); | 1906 return (-1); |
1597 } | 1907 } |
1598 | 1908 |
1599 | 1909 |
1600 | 1910 |
1601 /* | 1911 /* |
1602 * Root Node | 1912 * Root Node |
(...skipping 27 matching lines...) Expand all Loading... |
1630 */ | 1940 */ |
1631 xmlC14NFreeCtx(ctx); | 1941 xmlC14NFreeCtx(ctx); |
1632 return (ret); | 1942 return (ret); |
1633 } | 1943 } |
1634 | 1944 |
1635 /** | 1945 /** |
1636 * xmlC14NDocSaveTo: | 1946 * xmlC14NDocSaveTo: |
1637 * @doc: the XML document for canonization | 1947 * @doc: the XML document for canonization |
1638 * @nodes: the nodes set to be included in the canonized image | 1948 * @nodes: the nodes set to be included in the canonized image |
1639 * or NULL if all document nodes should be included | 1949 * or NULL if all document nodes should be included |
1640 * @exclusive:» » the exclusive flag (0 - non-exclusive canonicalization; | 1950 * @mode:» » the c14n mode (see @xmlC14NMode) |
1641 *» » » otherwise - exclusive canonicalization) | |
1642 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes | 1951 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
1643 * ended with a NULL or NULL if there is no | 1952 * ended with a NULL or NULL if there is no |
1644 * inclusive namespaces (only for exclusive | 1953 * inclusive namespaces (only for exclusive |
1645 * canonicalization, ignored otherwise) | 1954 * canonicalization, ignored otherwise) |
1646 * @with_comments: include comments in the result (!=0) or not (==0) | 1955 * @with_comments: include comments in the result (!=0) or not (==0) |
1647 * @buf: the output buffer to store canonical XML; this | 1956 * @buf: the output buffer to store canonical XML; this |
1648 * buffer MUST have encoder==NULL because C14N requires | 1957 * buffer MUST have encoder==NULL because C14N requires |
1649 * UTF-8 output | 1958 * UTF-8 output |
1650 * | 1959 * |
1651 * Dumps the canonized image of given XML document into the provided buffer. | 1960 * Dumps the canonized image of given XML document into the provided buffer. |
1652 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or | 1961 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
1653 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) | 1962 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
1654 * | 1963 * |
1655 * Returns non-negative value on success or a negative value on fail | 1964 * Returns non-negative value on success or a negative value on fail |
1656 */ | 1965 */ |
1657 int | 1966 int |
1658 xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes, | 1967 xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes, |
1659 int exclusive, xmlChar ** inclusive_ns_prefixes, | 1968 int mode, xmlChar ** inclusive_ns_prefixes, |
1660 int with_comments, xmlOutputBufferPtr buf) { | 1969 int with_comments, xmlOutputBufferPtr buf) { |
1661 return(xmlC14NExecute(doc, | 1970 return(xmlC14NExecute(doc, |
1662 (xmlC14NIsVisibleCallback)xmlC14NIsNodeInNodeset, | 1971 (xmlC14NIsVisibleCallback)xmlC14NIsNodeInNodeset, |
1663 nodes, | 1972 nodes, |
1664 » » » exclusive, | 1973 » » » mode, |
1665 inclusive_ns_prefixes, | 1974 inclusive_ns_prefixes, |
1666 with_comments, | 1975 with_comments, |
1667 buf)); | 1976 buf)); |
1668 } | 1977 } |
1669 | 1978 |
1670 | 1979 |
1671 /** | 1980 /** |
1672 * xmlC14NDocDumpMemory: | 1981 * xmlC14NDocDumpMemory: |
1673 * @doc: the XML document for canonization | 1982 * @doc: the XML document for canonization |
1674 * @nodes: the nodes set to be included in the canonized image | 1983 * @nodes: the nodes set to be included in the canonized image |
1675 * or NULL if all document nodes should be included | 1984 * or NULL if all document nodes should be included |
1676 * @exclusive:» » the exclusive flag (0 - non-exclusive canonicalization; | 1985 * @mode:» » the c14n mode (see @xmlC14NMode) |
1677 *» » » otherwise - exclusive canonicalization) | |
1678 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes | 1986 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
1679 * ended with a NULL or NULL if there is no | 1987 * ended with a NULL or NULL if there is no |
1680 * inclusive namespaces (only for exclusive | 1988 * inclusive namespaces (only for exclusive |
1681 * canonicalization, ignored otherwise) | 1989 * canonicalization, ignored otherwise) |
1682 * @with_comments: include comments in the result (!=0) or not (==0) | 1990 * @with_comments: include comments in the result (!=0) or not (==0) |
1683 * @doc_txt_ptr: the memory pointer for allocated canonical XML text; | 1991 * @doc_txt_ptr: the memory pointer for allocated canonical XML text; |
1684 * the caller of this functions is responsible for calling | 1992 * the caller of this functions is responsible for calling |
1685 * xmlFree() to free allocated memory | 1993 * xmlFree() to free allocated memory |
1686 * | 1994 * |
1687 * Dumps the canonized image of given XML document into memory. | 1995 * Dumps the canonized image of given XML document into memory. |
1688 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or | 1996 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
1689 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) | 1997 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
1690 * | 1998 * |
1691 * Returns the number of bytes written on success or a negative value on fail | 1999 * Returns the number of bytes written on success or a negative value on fail |
1692 */ | 2000 */ |
1693 int | 2001 int |
1694 xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr nodes, | 2002 xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr nodes, |
1695 int exclusive, xmlChar ** inclusive_ns_prefixes, | 2003 int mode, xmlChar ** inclusive_ns_prefixes, |
1696 int with_comments, xmlChar ** doc_txt_ptr) | 2004 int with_comments, xmlChar ** doc_txt_ptr) |
1697 { | 2005 { |
1698 int ret; | 2006 int ret; |
1699 xmlOutputBufferPtr buf; | 2007 xmlOutputBufferPtr buf; |
1700 | 2008 |
1701 if (doc_txt_ptr == NULL) { | 2009 if (doc_txt_ptr == NULL) { |
1702 xmlC14NErrParam("dumping doc to memory"); | 2010 xmlC14NErrParam("dumping doc to memory"); |
1703 return (-1); | 2011 return (-1); |
1704 } | 2012 } |
1705 | 2013 |
1706 *doc_txt_ptr = NULL; | 2014 *doc_txt_ptr = NULL; |
1707 | 2015 |
1708 /* | 2016 /* |
1709 * create memory buffer with UTF8 (default) encoding | 2017 * create memory buffer with UTF8 (default) encoding |
1710 */ | 2018 */ |
1711 buf = xmlAllocOutputBuffer(NULL); | 2019 buf = xmlAllocOutputBuffer(NULL); |
1712 if (buf == NULL) { | 2020 if (buf == NULL) { |
1713 xmlC14NErrMemory("creating output buffer"); | 2021 xmlC14NErrMemory("creating output buffer"); |
1714 return (-1); | 2022 return (-1); |
1715 } | 2023 } |
1716 | 2024 |
1717 /* | 2025 /* |
1718 * canonize document and write to buffer | 2026 * canonize document and write to buffer |
1719 */ | 2027 */ |
1720 ret = xmlC14NDocSaveTo(doc, nodes, exclusive, inclusive_ns_prefixes, | 2028 ret = xmlC14NDocSaveTo(doc, nodes, mode, inclusive_ns_prefixes, |
1721 with_comments, buf); | 2029 with_comments, buf); |
1722 if (ret < 0) { | 2030 if (ret < 0) { |
1723 xmlC14NErrInternal("saving doc to output buffer"); | 2031 xmlC14NErrInternal("saving doc to output buffer"); |
1724 (void) xmlOutputBufferClose(buf); | 2032 (void) xmlOutputBufferClose(buf); |
1725 return (-1); | 2033 return (-1); |
1726 } | 2034 } |
1727 | 2035 |
1728 ret = buf->buffer->use; | 2036 ret = buf->buffer->use; |
1729 if (ret > 0) { | 2037 if (ret > 0) { |
1730 *doc_txt_ptr = xmlStrndup(buf->buffer->content, ret); | 2038 *doc_txt_ptr = xmlStrndup(buf->buffer->content, ret); |
1731 } | 2039 } |
1732 (void) xmlOutputBufferClose(buf); | 2040 (void) xmlOutputBufferClose(buf); |
1733 | 2041 |
1734 if ((*doc_txt_ptr == NULL) && (ret > 0)) { | 2042 if ((*doc_txt_ptr == NULL) && (ret > 0)) { |
1735 xmlC14NErrMemory("coping canonicanized document"); | 2043 xmlC14NErrMemory("coping canonicanized document"); |
1736 return (-1); | 2044 return (-1); |
1737 } | 2045 } |
1738 return (ret); | 2046 return (ret); |
1739 } | 2047 } |
1740 | 2048 |
1741 /** | 2049 /** |
1742 * xmlC14NDocSave: | 2050 * xmlC14NDocSave: |
1743 * @doc: the XML document for canonization | 2051 * @doc: the XML document for canonization |
1744 * @nodes: the nodes set to be included in the canonized image | 2052 * @nodes: the nodes set to be included in the canonized image |
1745 * or NULL if all document nodes should be included | 2053 * or NULL if all document nodes should be included |
1746 * @exclusive:» » the exclusive flag (0 - non-exclusive canonicalization; | 2054 * @mode:» » the c14n mode (see @xmlC14NMode) |
1747 *» » » otherwise - exclusive canonicalization) | |
1748 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes | 2055 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
1749 * ended with a NULL or NULL if there is no | 2056 * ended with a NULL or NULL if there is no |
1750 * inclusive namespaces (only for exclusive | 2057 * inclusive namespaces (only for exclusive |
1751 * canonicalization, ignored otherwise) | 2058 * canonicalization, ignored otherwise) |
1752 * @with_comments: include comments in the result (!=0) or not (==0) | 2059 * @with_comments: include comments in the result (!=0) or not (==0) |
1753 * @filename: the filename to store canonical XML image | 2060 * @filename: the filename to store canonical XML image |
1754 * @compression: the compression level (zlib requred): | 2061 * @compression: the compression level (zlib requred): |
1755 * -1 - libxml default, | 2062 * -1 - libxml default, |
1756 * 0 - uncompressed, | 2063 * 0 - uncompressed, |
1757 * >0 - compression level | 2064 * >0 - compression level |
1758 * | 2065 * |
1759 * Dumps the canonized image of given XML document into the file. | 2066 * Dumps the canonized image of given XML document into the file. |
1760 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or | 2067 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
1761 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) | 2068 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
1762 * | 2069 * |
1763 * Returns the number of bytes written success or a negative value on fail | 2070 * Returns the number of bytes written success or a negative value on fail |
1764 */ | 2071 */ |
1765 int | 2072 int |
1766 xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes, | 2073 xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes, |
1767 int exclusive, xmlChar ** inclusive_ns_prefixes, | 2074 int mode, xmlChar ** inclusive_ns_prefixes, |
1768 int with_comments, const char *filename, int compression) | 2075 int with_comments, const char *filename, int compression) |
1769 { | 2076 { |
1770 xmlOutputBufferPtr buf; | 2077 xmlOutputBufferPtr buf; |
1771 int ret; | 2078 int ret; |
1772 | 2079 |
1773 if (filename == NULL) { | 2080 if (filename == NULL) { |
1774 xmlC14NErrParam("saving doc"); | 2081 xmlC14NErrParam("saving doc"); |
1775 return (-1); | 2082 return (-1); |
1776 } | 2083 } |
1777 #ifdef HAVE_ZLIB_H | 2084 #ifdef HAVE_ZLIB_H |
1778 if (compression < 0) | 2085 if (compression < 0) |
1779 compression = xmlGetCompressMode(); | 2086 compression = xmlGetCompressMode(); |
1780 #endif | 2087 #endif |
1781 | 2088 |
1782 /* | 2089 /* |
1783 * save the content to a temp buffer, use default UTF8 encoding. | 2090 * save the content to a temp buffer, use default UTF8 encoding. |
1784 */ | 2091 */ |
1785 buf = xmlOutputBufferCreateFilename(filename, NULL, compression); | 2092 buf = xmlOutputBufferCreateFilename(filename, NULL, compression); |
1786 if (buf == NULL) { | 2093 if (buf == NULL) { |
1787 xmlC14NErrInternal("creating temporary filename"); | 2094 xmlC14NErrInternal("creating temporary filename"); |
1788 return (-1); | 2095 return (-1); |
1789 } | 2096 } |
1790 | 2097 |
1791 /* | 2098 /* |
1792 * canonize document and write to buffer | 2099 * canonize document and write to buffer |
1793 */ | 2100 */ |
1794 ret = xmlC14NDocSaveTo(doc, nodes, exclusive, inclusive_ns_prefixes, | 2101 ret = xmlC14NDocSaveTo(doc, nodes, mode, inclusive_ns_prefixes, |
1795 with_comments, buf); | 2102 with_comments, buf); |
1796 if (ret < 0) { | 2103 if (ret < 0) { |
1797 xmlC14NErrInternal("cannicanize document to buffer"); | 2104 xmlC14NErrInternal("cannicanize document to buffer"); |
1798 (void) xmlOutputBufferClose(buf); | 2105 (void) xmlOutputBufferClose(buf); |
1799 return (-1); | 2106 return (-1); |
1800 } | 2107 } |
1801 | 2108 |
1802 /* | 2109 /* |
1803 * get the numbers of bytes written | 2110 * get the numbers of bytes written |
1804 */ | 2111 */ |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1912 *out++ = ';'; | 2219 *out++ = ';'; |
1913 } else { | 2220 } else { |
1914 /* | 2221 /* |
1915 * Works because on UTF-8, all extended sequences cannot | 2222 * Works because on UTF-8, all extended sequences cannot |
1916 * result in bytes in the ASCII range. | 2223 * result in bytes in the ASCII range. |
1917 */ | 2224 */ |
1918 *out++ = *cur; | 2225 *out++ = *cur; |
1919 } | 2226 } |
1920 cur++; | 2227 cur++; |
1921 } | 2228 } |
1922 *out++ = 0; | 2229 *out = 0; |
1923 return (buffer); | 2230 return (buffer); |
1924 } | 2231 } |
1925 #endif /* LIBXML_OUTPUT_ENABLED */ | 2232 #endif /* LIBXML_OUTPUT_ENABLED */ |
1926 #define bottom_c14n | 2233 #define bottom_c14n |
1927 #include "elfgcchack.h" | 2234 #include "elfgcchack.h" |
1928 #endif /* LIBXML_C14N_ENABLED */ | 2235 #endif /* LIBXML_C14N_ENABLED */ |
OLD | NEW |