c14n.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  1. /*
  2. * "Canonical XML" implementation
  3. * http://www.w3.org/TR/xml-c14n
  4. *
  5. * "Exclusive XML Canonicalization" implementation
  6. * http://www.w3.org/TR/xml-exc-c14n
  7. *
  8. * See Copyright for the status of this software.
  9. *
  10. * Author: Aleksey Sanin <aleksey@aleksey.com>
  11. */
  12. #define IN_LIBXML
  13. #include "libxml.h"
  14. #ifdef LIBXML_C14N_ENABLED
  15. #ifdef LIBXML_OUTPUT_ENABLED
  16. #ifdef HAVE_STDLIB_H
  17. #include <stdlib.h>
  18. #endif
  19. #include <string.h>
  20. #include <libxml/tree.h>
  21. #include <libxml/parser.h>
  22. #include <libxml/uri.h>
  23. #include <libxml/xmlerror.h>
  24. #include <libxml/globals.h>
  25. #include <libxml/xpathInternals.h>
  26. #include <libxml/c14n.h>
  27. /************************************************************************
  28. * *
  29. * Some declaration better left private ATM *
  30. * *
  31. ************************************************************************/
  32. typedef enum {
  33. XMLC14N_BEFORE_DOCUMENT_ELEMENT = 0,
  34. XMLC14N_INSIDE_DOCUMENT_ELEMENT = 1,
  35. XMLC14N_AFTER_DOCUMENT_ELEMENT = 2
  36. } xmlC14NPosition;
  37. typedef struct _xmlC14NVisibleNsStack {
  38. int nsCurEnd; /* number of nodes in the set */
  39. int nsPrevStart; /* the begginning of the stack for previous visible node */
  40. int nsPrevEnd; /* the end of the stack for previous visible node */
  41. int nsMax; /* size of the array as allocated */
  42. xmlNsPtr *nsTab; /* array of ns in no particular order */
  43. xmlNodePtr *nodeTab; /* array of nodes in no particular order */
  44. } xmlC14NVisibleNsStack, *xmlC14NVisibleNsStackPtr;
  45. typedef struct _xmlC14NCtx {
  46. /* input parameters */
  47. xmlDocPtr doc;
  48. xmlC14NIsVisibleCallback is_visible_callback;
  49. void* user_data;
  50. int with_comments;
  51. xmlOutputBufferPtr buf;
  52. /* position in the XML document */
  53. xmlC14NPosition pos;
  54. int parent_is_doc;
  55. xmlC14NVisibleNsStackPtr ns_rendered;
  56. /* C14N mode */
  57. xmlC14NMode mode;
  58. /* exclusive canonicalization */
  59. xmlChar **inclusive_ns_prefixes;
  60. /* error number */
  61. int error;
  62. } xmlC14NCtx, *xmlC14NCtxPtr;
  63. static xmlC14NVisibleNsStackPtr xmlC14NVisibleNsStackCreate (void);
  64. static void xmlC14NVisibleNsStackDestroy (xmlC14NVisibleNsStackPtr cur);
  65. static void xmlC14NVisibleNsStackAdd (xmlC14NVisibleNsStackPtr cur,
  66. xmlNsPtr ns,
  67. xmlNodePtr node);
  68. static void xmlC14NVisibleNsStackSave (xmlC14NVisibleNsStackPtr cur,
  69. xmlC14NVisibleNsStackPtr state);
  70. static void xmlC14NVisibleNsStackRestore (xmlC14NVisibleNsStackPtr cur,
  71. xmlC14NVisibleNsStackPtr state);
  72. static void xmlC14NVisibleNsStackShift (xmlC14NVisibleNsStackPtr cur);
  73. static int xmlC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur,
  74. xmlNsPtr ns);
  75. static int xmlExcC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur,
  76. xmlNsPtr ns,
  77. xmlC14NCtxPtr ctx);
  78. static int xmlC14NIsNodeInNodeset (xmlNodeSetPtr nodes,
  79. xmlNodePtr node,
  80. xmlNodePtr parent);
  81. static int xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur);
  82. static int xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur);
  83. typedef enum {
  84. XMLC14N_NORMALIZE_ATTR = 0,
  85. XMLC14N_NORMALIZE_COMMENT = 1,
  86. XMLC14N_NORMALIZE_PI = 2,
  87. XMLC14N_NORMALIZE_TEXT = 3
  88. } xmlC14NNormalizationMode;
  89. static xmlChar *xmlC11NNormalizeString(const xmlChar * input,
  90. xmlC14NNormalizationMode mode);
  91. #define xmlC11NNormalizeAttr( a ) \
  92. xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_ATTR)
  93. #define xmlC11NNormalizeComment( a ) \
  94. xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_COMMENT)
  95. #define xmlC11NNormalizePI( a ) \
  96. xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI)
  97. #define xmlC11NNormalizeText( a ) \
  98. xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT)
  99. #define xmlC14NIsVisible( ctx, node, parent ) \
  100. (((ctx)->is_visible_callback != NULL) ? \
  101. (ctx)->is_visible_callback((ctx)->user_data, \
  102. (xmlNodePtr)(node), (xmlNodePtr)(parent)) : 1)
  103. #define xmlC14NIsExclusive( ctx ) \
  104. ( (ctx)->mode == XML_C14N_EXCLUSIVE_1_0 )
  105. /************************************************************************
  106. * *
  107. * Some factorized error routines *
  108. * *
  109. ************************************************************************/
  110. /**
  111. * xmlC14NErrMemory:
  112. * @extra: extra informations
  113. *
  114. * Handle a redefinition of memory error
  115. */
  116. static void
  117. xmlC14NErrMemory(const char *extra)
  118. {
  119. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
  120. XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra,
  121. NULL, NULL, 0, 0,
  122. "Memory allocation failed : %s\n", extra);
  123. }
  124. /**
  125. * xmlC14NErrParam:
  126. * @extra: extra informations
  127. *
  128. * Handle a redefinition of param error
  129. */
  130. static void
  131. xmlC14NErrParam(const char *extra)
  132. {
  133. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
  134. XML_ERR_INTERNAL_ERROR, XML_ERR_ERROR, NULL, 0, extra,
  135. NULL, NULL, 0, 0,
  136. "Invalid parameter : %s\n", extra);
  137. }
  138. /**
  139. * xmlC14NErrInternal:
  140. * @extra: extra informations
  141. *
  142. * Handle a redefinition of internal error
  143. */
  144. static void
  145. xmlC14NErrInternal(const char *extra)
  146. {
  147. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
  148. XML_ERR_INTERNAL_ERROR, XML_ERR_ERROR, NULL, 0, extra,
  149. NULL, NULL, 0, 0,
  150. "Internal error : %s\n", extra);
  151. }
  152. /**
  153. * xmlC14NErrInvalidNode:
  154. * @extra: extra informations
  155. *
  156. * Handle a redefinition of invalid node error
  157. */
  158. static void
  159. xmlC14NErrInvalidNode(const char *node_type, const char *extra)
  160. {
  161. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
  162. XML_C14N_INVALID_NODE, XML_ERR_ERROR, NULL, 0, extra,
  163. NULL, NULL, 0, 0,
  164. "Node %s is invalid here : %s\n", node_type, extra);
  165. }
  166. /**
  167. * xmlC14NErrUnknownNode:
  168. * @extra: extra informations
  169. *
  170. * Handle a redefinition of unknown node error
  171. */
  172. static void
  173. xmlC14NErrUnknownNode(int node_type, const char *extra)
  174. {
  175. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
  176. XML_C14N_UNKNOW_NODE, XML_ERR_ERROR, NULL, 0, extra,
  177. NULL, NULL, 0, 0,
  178. "Unknown node type %d found : %s\n", node_type, extra);
  179. }
  180. /**
  181. * xmlC14NErrRelativeNamespace:
  182. * @extra: extra informations
  183. *
  184. * Handle a redefinition of relative namespace error
  185. */
  186. static void
  187. xmlC14NErrRelativeNamespace(const char *ns_uri)
  188. {
  189. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
  190. XML_C14N_RELATIVE_NAMESPACE, XML_ERR_ERROR, NULL, 0, NULL,
  191. NULL, NULL, 0, 0,
  192. "Relative namespace UR is invalid here : %s\n", ns_uri);
  193. }
  194. /**
  195. * xmlC14NErr:
  196. * @ctxt: a C14N evaluation context
  197. * @node: the context node
  198. * @error: the erorr code
  199. * @msg: the message
  200. * @extra: extra informations
  201. *
  202. * Handle a redefinition of attribute error
  203. */
  204. static void
  205. xmlC14NErr(xmlC14NCtxPtr ctxt, xmlNodePtr node, int error,
  206. const char * msg)
  207. {
  208. if (ctxt != NULL)
  209. ctxt->error = error;
  210. __xmlRaiseError(NULL, NULL, NULL,
  211. ctxt, node, XML_FROM_C14N, error,
  212. XML_ERR_ERROR, NULL, 0,
  213. NULL, NULL, NULL, 0, 0, "%s", msg);
  214. }
  215. /************************************************************************
  216. * *
  217. * The implementation internals *
  218. * *
  219. ************************************************************************/
  220. #define XML_NAMESPACES_DEFAULT 16
  221. static int
  222. xmlC14NIsNodeInNodeset(xmlNodeSetPtr nodes, xmlNodePtr node, xmlNodePtr parent) {
  223. if((nodes != NULL) && (node != NULL)) {
  224. if(node->type != XML_NAMESPACE_DECL) {
  225. return(xmlXPathNodeSetContains(nodes, node));
  226. } else {
  227. xmlNs ns;
  228. memcpy(&ns, node, sizeof(ns));
  229. /* this is a libxml hack! check xpath.c for details */
  230. if((parent != NULL) && (parent->type == XML_ATTRIBUTE_NODE)) {
  231. ns.next = (xmlNsPtr)parent->parent;
  232. } else {
  233. ns.next = (xmlNsPtr)parent;
  234. }
  235. /*
  236. * If the input is an XPath node-set, then the node-set must explicitly
  237. * contain every node to be rendered to the canonical form.
  238. */
  239. return(xmlXPathNodeSetContains(nodes, (xmlNodePtr)&ns));
  240. }
  241. }
  242. return(1);
  243. }
  244. static xmlC14NVisibleNsStackPtr
  245. xmlC14NVisibleNsStackCreate(void) {
  246. xmlC14NVisibleNsStackPtr ret;
  247. ret = (xmlC14NVisibleNsStackPtr) xmlMalloc(sizeof(xmlC14NVisibleNsStack));
  248. if (ret == NULL) {
  249. xmlC14NErrMemory("creating namespaces stack");
  250. return(NULL);
  251. }
  252. memset(ret, 0 , (size_t) sizeof(xmlC14NVisibleNsStack));
  253. return(ret);
  254. }
  255. static void
  256. xmlC14NVisibleNsStackDestroy(xmlC14NVisibleNsStackPtr cur) {
  257. if(cur == NULL) {
  258. xmlC14NErrParam("destroying namespaces stack");
  259. return;
  260. }
  261. if(cur->nsTab != NULL) {
  262. memset(cur->nsTab, 0, cur->nsMax * sizeof(xmlNsPtr));
  263. xmlFree(cur->nsTab);
  264. }
  265. if(cur->nodeTab != NULL) {
  266. memset(cur->nodeTab, 0, cur->nsMax * sizeof(xmlNodePtr));
  267. xmlFree(cur->nodeTab);
  268. }
  269. memset(cur, 0, sizeof(xmlC14NVisibleNsStack));
  270. xmlFree(cur);
  271. }
  272. static void
  273. xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlNodePtr node) {
  274. if((cur == NULL) ||
  275. ((cur->nsTab == NULL) && (cur->nodeTab != NULL)) ||
  276. ((cur->nsTab != NULL) && (cur->nodeTab == NULL))) {
  277. xmlC14NErrParam("adding namespace to stack");
  278. return;
  279. }
  280. if ((cur->nsTab == NULL) && (cur->nodeTab == NULL)) {
  281. cur->nsTab = (xmlNsPtr*) xmlMalloc(XML_NAMESPACES_DEFAULT * sizeof(xmlNsPtr));
  282. cur->nodeTab = (xmlNodePtr*) xmlMalloc(XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr));
  283. if ((cur->nsTab == NULL) || (cur->nodeTab == NULL)) {
  284. xmlC14NErrMemory("adding node to stack");
  285. return;
  286. }
  287. memset(cur->nsTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNsPtr));
  288. memset(cur->nodeTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr));
  289. cur->nsMax = XML_NAMESPACES_DEFAULT;
  290. } else if(cur->nsMax == cur->nsCurEnd) {
  291. void *tmp;
  292. int tmpSize;
  293. tmpSize = 2 * cur->nsMax;
  294. tmp = xmlRealloc(cur->nsTab, tmpSize * sizeof(xmlNsPtr));
  295. if (tmp == NULL) {
  296. xmlC14NErrMemory("adding node to stack");
  297. return;
  298. }
  299. cur->nsTab = (xmlNsPtr*)tmp;
  300. tmp = xmlRealloc(cur->nodeTab, tmpSize * sizeof(xmlNodePtr));
  301. if (tmp == NULL) {
  302. xmlC14NErrMemory("adding node to stack");
  303. return;
  304. }
  305. cur->nodeTab = (xmlNodePtr*)tmp;
  306. cur->nsMax = tmpSize;
  307. }
  308. cur->nsTab[cur->nsCurEnd] = ns;
  309. cur->nodeTab[cur->nsCurEnd] = node;
  310. ++cur->nsCurEnd;
  311. }
  312. static void
  313. xmlC14NVisibleNsStackSave(xmlC14NVisibleNsStackPtr cur, xmlC14NVisibleNsStackPtr state) {
  314. if((cur == NULL) || (state == NULL)) {
  315. xmlC14NErrParam("saving namespaces stack");
  316. return;
  317. }
  318. state->nsCurEnd = cur->nsCurEnd;
  319. state->nsPrevStart = cur->nsPrevStart;
  320. state->nsPrevEnd = cur->nsPrevEnd;
  321. }
  322. static void
  323. xmlC14NVisibleNsStackRestore(xmlC14NVisibleNsStackPtr cur, xmlC14NVisibleNsStackPtr state) {
  324. if((cur == NULL) || (state == NULL)) {
  325. xmlC14NErrParam("restoring namespaces stack");
  326. return;
  327. }
  328. cur->nsCurEnd = state->nsCurEnd;
  329. cur->nsPrevStart = state->nsPrevStart;
  330. cur->nsPrevEnd = state->nsPrevEnd;
  331. }
  332. static void
  333. xmlC14NVisibleNsStackShift(xmlC14NVisibleNsStackPtr cur) {
  334. if(cur == NULL) {
  335. xmlC14NErrParam("shifting namespaces stack");
  336. return;
  337. }
  338. cur->nsPrevStart = cur->nsPrevEnd;
  339. cur->nsPrevEnd = cur->nsCurEnd;
  340. }
  341. static int
  342. xmlC14NStrEqual(const xmlChar *str1, const xmlChar *str2) {
  343. if (str1 == str2) return(1);
  344. if (str1 == NULL) return((*str2) == '\0');
  345. if (str2 == NULL) return((*str1) == '\0');
  346. do {
  347. if (*str1++ != *str2) return(0);
  348. } while (*str2++);
  349. return(1);
  350. }
  351. /**
  352. * xmlC14NVisibleNsStackFind:
  353. * @ctx: the C14N context
  354. * @ns: the namespace to check
  355. *
  356. * Checks whether the given namespace was already rendered or not
  357. *
  358. * Returns 1 if we already wrote this namespace or 0 otherwise
  359. */
  360. static int
  361. xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns)
  362. {
  363. int i;
  364. const xmlChar *prefix;
  365. const xmlChar *href;
  366. int has_empty_ns;
  367. if(cur == NULL) {
  368. xmlC14NErrParam("searching namespaces stack (c14n)");
  369. return (0);
  370. }
  371. /*
  372. * if the default namespace xmlns="" is not defined yet then
  373. * we do not want to print it out
  374. */
  375. prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;
  376. href = ((ns == NULL) || (ns->href == NULL)) ? BAD_CAST "" : ns->href;
  377. has_empty_ns = (xmlC14NStrEqual(prefix, NULL) && xmlC14NStrEqual(href, NULL));
  378. if (cur->nsTab != NULL) {
  379. int start = (has_empty_ns) ? 0 : cur->nsPrevStart;
  380. for (i = cur->nsCurEnd - 1; i >= start; --i) {
  381. xmlNsPtr ns1 = cur->nsTab[i];
  382. if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {
  383. return(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL));
  384. }
  385. }
  386. }
  387. return(has_empty_ns);
  388. }
  389. static int
  390. xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlC14NCtxPtr ctx) {
  391. int i;
  392. const xmlChar *prefix;
  393. const xmlChar *href;
  394. int has_empty_ns;
  395. if(cur == NULL) {
  396. xmlC14NErrParam("searching namespaces stack (exc c14n)");
  397. return (0);
  398. }
  399. /*
  400. * if the default namespace xmlns="" is not defined yet then
  401. * we do not want to print it out
  402. */
  403. prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;
  404. href = ((ns == NULL) || (ns->href == NULL)) ? BAD_CAST "" : ns->href;
  405. has_empty_ns = (xmlC14NStrEqual(prefix, NULL) && xmlC14NStrEqual(href, NULL));
  406. if (cur->nsTab != NULL) {
  407. int start = 0;
  408. for (i = cur->nsCurEnd - 1; i >= start; --i) {
  409. xmlNsPtr ns1 = cur->nsTab[i];
  410. if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {
  411. if(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL)) {
  412. return(xmlC14NIsVisible(ctx, ns1, cur->nodeTab[i]));
  413. } else {
  414. return(0);
  415. }
  416. }
  417. }
  418. }
  419. return(has_empty_ns);
  420. }
  421. /**
  422. * xmlC14NIsXmlNs:
  423. * @ns: the namespace to check
  424. *
  425. * Checks whether the given namespace is a default "xml:" namespace
  426. * with href="http://www.w3.org/XML/1998/namespace"
  427. *
  428. * Returns 1 if the node is default or 0 otherwise
  429. */
  430. /* todo: make it a define? */
  431. static int
  432. xmlC14NIsXmlNs(xmlNsPtr ns)
  433. {
  434. return ((ns != NULL) &&
  435. (xmlStrEqual(ns->prefix, BAD_CAST "xml")) &&
  436. (xmlStrEqual(ns->href, XML_XML_NAMESPACE)));
  437. }
  438. /**
  439. * xmlC14NNsCompare:
  440. * @ns1: the pointer to first namespace
  441. * @ns2: the pointer to second namespace
  442. *
  443. * Compares the namespaces by names (prefixes).
  444. *
  445. * Returns -1 if ns1 < ns2, 0 if ns1 == ns2 or 1 if ns1 > ns2.
  446. */
  447. static int
  448. xmlC14NNsCompare(xmlNsPtr ns1, xmlNsPtr ns2)
  449. {
  450. if (ns1 == ns2)
  451. return (0);
  452. if (ns1 == NULL)
  453. return (-1);
  454. if (ns2 == NULL)
  455. return (1);
  456. return (xmlStrcmp(ns1->prefix, ns2->prefix));
  457. }
  458. /**
  459. * xmlC14NPrintNamespaces:
  460. * @ns: the pointer to namespace
  461. * @ctx: the C14N context
  462. *
  463. * Prints the given namespace to the output buffer from C14N context.
  464. *
  465. * Returns 1 on success or 0 on fail.
  466. */
  467. static int
  468. xmlC14NPrintNamespaces(const xmlNsPtr ns, xmlC14NCtxPtr ctx)
  469. {
  470. if ((ns == NULL) || (ctx == NULL)) {
  471. xmlC14NErrParam("writing namespaces");
  472. return 0;
  473. }
  474. if (ns->prefix != NULL) {
  475. xmlOutputBufferWriteString(ctx->buf, " xmlns:");
  476. xmlOutputBufferWriteString(ctx->buf, (const char *) ns->prefix);
  477. xmlOutputBufferWriteString(ctx->buf, "=\"");
  478. } else {
  479. xmlOutputBufferWriteString(ctx->buf, " xmlns=\"");
  480. }
  481. if(ns->href != NULL) {
  482. xmlOutputBufferWriteString(ctx->buf, (const char *) ns->href);
  483. }
  484. xmlOutputBufferWriteString(ctx->buf, "\"");
  485. return (1);
  486. }
  487. /**
  488. * xmlC14NProcessNamespacesAxis:
  489. * @ctx: the C14N context
  490. * @node: the current node
  491. *
  492. * Prints out canonical namespace axis of the current node to the
  493. * buffer from C14N context as follows
  494. *
  495. * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
  496. *
  497. * Namespace Axis
  498. * Consider a list L containing only namespace nodes in the
  499. * axis and in the node-set in lexicographic order (ascending). To begin
  500. * processing L, if the first node is not the default namespace node (a node
  501. * with no namespace URI and no local name), then generate a space followed
  502. * by xmlns="" if and only if the following conditions are met:
  503. * - the element E that owns the axis is in the node-set
  504. * - The nearest ancestor element of E in the node-set has a default
  505. * namespace node in the node-set (default namespace nodes always
  506. * have non-empty values in XPath)
  507. * The latter condition eliminates unnecessary occurrences of xmlns="" in
  508. * the canonical form since an element only receives an xmlns="" if its
  509. * default namespace is empty and if it has an immediate parent in the
  510. * canonical form that has a non-empty default namespace. To finish
  511. * processing L, simply process every namespace node in L, except omit
  512. * namespace node with local name xml, which defines the xml prefix,
  513. * if its string value is http://www.w3.org/XML/1998/namespace.
  514. *
  515. * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
  516. * Canonical XML applied to a document subset requires the search of the
  517. * ancestor nodes of each orphan element node for attributes in the xml
  518. * namespace, such as xml:lang and xml:space. These are copied into the
  519. * element node except if a declaration of the same attribute is already
  520. * in the attribute axis of the element (whether or not it is included in
  521. * the document subset). This search and copying are omitted from the
  522. * Exclusive XML Canonicalization method.
  523. *
  524. * Returns 0 on success or -1 on fail.
  525. */
  526. static int
  527. xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible)
  528. {
  529. xmlNodePtr n;
  530. xmlNsPtr ns, tmp;
  531. xmlListPtr list;
  532. int already_rendered;
  533. int has_empty_ns = 0;
  534. if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
  535. xmlC14NErrParam("processing namespaces axis (c14n)");
  536. return (-1);
  537. }
  538. /*
  539. * Create a sorted list to store element namespaces
  540. */
  541. list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare);
  542. if (list == NULL) {
  543. xmlC14NErrInternal("creating namespaces list (c14n)");
  544. return (-1);
  545. }
  546. /* check all namespaces */
  547. for(n = cur; n != NULL; n = n->parent) {
  548. for(ns = n->nsDef; ns != NULL; ns = ns->next) {
  549. tmp = xmlSearchNs(cur->doc, cur, ns->prefix);
  550. if((tmp == ns) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {
  551. already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);
  552. if(visible) {
  553. xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
  554. }
  555. if(!already_rendered) {
  556. xmlListInsert(list, ns);
  557. }
  558. if(xmlStrlen(ns->prefix) == 0) {
  559. has_empty_ns = 1;
  560. }
  561. }
  562. }
  563. }
  564. /**
  565. * if the first node is not the default namespace node (a node with no
  566. * namespace URI and no local name), then generate a space followed by
  567. * xmlns="" if and only if the following conditions are met:
  568. * - the element E that owns the axis is in the node-set
  569. * - the nearest ancestor element of E in the node-set has a default
  570. * namespace node in the node-set (default namespace nodes always
  571. * have non-empty values in XPath)
  572. */
  573. if(visible && !has_empty_ns) {
  574. static xmlNs ns_default;
  575. memset(&ns_default, 0, sizeof(ns_default));
  576. if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {
  577. xmlC14NPrintNamespaces(&ns_default, ctx);
  578. }
  579. }
  580. /*
  581. * print out all elements from list
  582. */
  583. xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);
  584. /*
  585. * Cleanup
  586. */
  587. xmlListDelete(list);
  588. return (0);
  589. }
  590. /**
  591. * xmlExcC14NProcessNamespacesAxis:
  592. * @ctx: the C14N context
  593. * @node: the current node
  594. *
  595. * Prints out exclusive canonical namespace axis of the current node to the
  596. * buffer from C14N context as follows
  597. *
  598. * Exclusive XML Canonicalization
  599. * http://www.w3.org/TR/xml-exc-c14n
  600. *
  601. * If the element node is in the XPath subset then output the node in
  602. * accordance with Canonical XML except for namespace nodes which are
  603. * rendered as follows:
  604. *
  605. * 1. Render each namespace node iff:
  606. * * it is visibly utilized by the immediate parent element or one of
  607. * its attributes, or is present in InclusiveNamespaces PrefixList, and
  608. * * its prefix and value do not appear in ns_rendered. ns_rendered is
  609. * obtained by popping the state stack in order to obtain a list of
  610. * prefixes and their values which have already been rendered by
  611. * an output ancestor of the namespace node's parent element.
  612. * 2. Append the rendered namespace node to the list ns_rendered of namespace
  613. * nodes rendered by output ancestors. Push ns_rendered on state stack and
  614. * recurse.
  615. * 3. After the recursion returns, pop thestate stack.
  616. *
  617. *
  618. * Returns 0 on success or -1 on fail.
  619. */
  620. static int
  621. xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible)
  622. {
  623. xmlNsPtr ns;
  624. xmlListPtr list;
  625. xmlAttrPtr attr;
  626. int already_rendered;
  627. int has_empty_ns = 0;
  628. int has_visibly_utilized_empty_ns = 0;
  629. int has_empty_ns_in_inclusive_list = 0;
  630. if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
  631. xmlC14NErrParam("processing namespaces axis (exc c14n)");
  632. return (-1);
  633. }
  634. if(!xmlC14NIsExclusive(ctx)) {
  635. xmlC14NErrParam("processing namespaces axis (exc c14n)");
  636. return (-1);
  637. }
  638. /*
  639. * Create a sorted list to store element namespaces
  640. */
  641. list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare);
  642. if (list == NULL) {
  643. xmlC14NErrInternal("creating namespaces list (exc c14n)");
  644. return (-1);
  645. }
  646. /*
  647. * process inclusive namespaces:
  648. * All namespace nodes appearing on inclusive ns list are
  649. * handled as provided in Canonical XML
  650. */
  651. if(ctx->inclusive_ns_prefixes != NULL) {
  652. xmlChar *prefix;
  653. int i;
  654. for (i = 0; ctx->inclusive_ns_prefixes[i] != NULL; ++i) {
  655. prefix = ctx->inclusive_ns_prefixes[i];
  656. /*
  657. * Special values for namespace with empty prefix
  658. */
  659. if (xmlStrEqual(prefix, BAD_CAST "#default")
  660. || xmlStrEqual(prefix, BAD_CAST "")) {
  661. prefix = NULL;
  662. has_empty_ns_in_inclusive_list = 1;
  663. }
  664. ns = xmlSearchNs(cur->doc, cur, prefix);
  665. if((ns != NULL) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {
  666. already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);
  667. if(visible) {
  668. xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
  669. }
  670. if(!already_rendered) {
  671. xmlListInsert(list, ns);
  672. }
  673. if(xmlStrlen(ns->prefix) == 0) {
  674. has_empty_ns = 1;
  675. }
  676. }
  677. }
  678. }
  679. /* add node namespace */
  680. if(cur->ns != NULL) {
  681. ns = cur->ns;
  682. } else {
  683. ns = xmlSearchNs(cur->doc, cur, NULL);
  684. has_visibly_utilized_empty_ns = 1;
  685. }
  686. if((ns != NULL) && !xmlC14NIsXmlNs(ns)) {
  687. if(visible && xmlC14NIsVisible(ctx, ns, cur)) {
  688. if(!xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, ns, ctx)) {
  689. xmlListInsert(list, ns);
  690. }
  691. }
  692. if(visible) {
  693. xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
  694. }
  695. if(xmlStrlen(ns->prefix) == 0) {
  696. has_empty_ns = 1;
  697. }
  698. }
  699. /* add attributes */
  700. for(attr = cur->properties; attr != NULL; attr = attr->next) {
  701. /*
  702. * we need to check that attribute is visible and has non
  703. * default namespace (XML Namespaces: "default namespaces
  704. * do not apply directly to attributes")
  705. */
  706. if((attr->ns != NULL) && !xmlC14NIsXmlNs(attr->ns) && xmlC14NIsVisible(ctx, attr, cur)) {
  707. already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, attr->ns, ctx);
  708. xmlC14NVisibleNsStackAdd(ctx->ns_rendered, attr->ns, cur);
  709. if(!already_rendered && visible) {
  710. xmlListInsert(list, attr->ns);
  711. }
  712. if(xmlStrlen(attr->ns->prefix) == 0) {
  713. has_empty_ns = 1;
  714. }
  715. } else if((attr->ns != NULL) && (xmlStrlen(attr->ns->prefix) == 0) && (xmlStrlen(attr->ns->href) == 0)) {
  716. has_visibly_utilized_empty_ns = 1;
  717. }
  718. }
  719. /*
  720. * Process xmlns=""
  721. */
  722. if(visible && has_visibly_utilized_empty_ns &&
  723. !has_empty_ns && !has_empty_ns_in_inclusive_list) {
  724. static xmlNs ns_default;
  725. memset(&ns_default, 0, sizeof(ns_default));
  726. already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default, ctx);
  727. if(!already_rendered) {
  728. xmlC14NPrintNamespaces(&ns_default, ctx);
  729. }
  730. } else if(visible && !has_empty_ns && has_empty_ns_in_inclusive_list) {
  731. static xmlNs ns_default;
  732. memset(&ns_default, 0, sizeof(ns_default));
  733. if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {
  734. xmlC14NPrintNamespaces(&ns_default, ctx);
  735. }
  736. }
  737. /*
  738. * print out all elements from list
  739. */
  740. xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);
  741. /*
  742. * Cleanup
  743. */
  744. xmlListDelete(list);
  745. return (0);
  746. }
  747. /**
  748. * xmlC14NIsXmlAttr:
  749. * @attr: the attr to check
  750. *
  751. * Checks whether the given attribute is a default "xml:" namespace
  752. * with href="http://www.w3.org/XML/1998/namespace"
  753. *
  754. * Returns 1 if the node is default or 0 otherwise
  755. */
  756. /* todo: make it a define? */
  757. static int
  758. xmlC14NIsXmlAttr(xmlAttrPtr attr)
  759. {
  760. return ((attr->ns != NULL) &&
  761. (xmlC14NIsXmlNs(attr->ns) != 0));
  762. }
  763. /**
  764. * xmlC14NAttrsCompare:
  765. * @attr1: the pointer tls o first attr
  766. * @attr2: the pointer to second attr
  767. *
  768. * Prints the given attribute to the output buffer from C14N context.
  769. *
  770. * Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2.
  771. */
  772. static int
  773. xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2)
  774. {
  775. int ret = 0;
  776. /*
  777. * Simple cases
  778. */
  779. if (attr1 == attr2)
  780. return (0);
  781. if (attr1 == NULL)
  782. return (-1);
  783. if (attr2 == NULL)
  784. return (1);
  785. if (attr1->ns == attr2->ns) {
  786. return (xmlStrcmp(attr1->name, attr2->name));
  787. }
  788. /*
  789. * Attributes in the default namespace are first
  790. * because the default namespace is not applied to
  791. * unqualified attributes
  792. */
  793. if (attr1->ns == NULL)
  794. return (-1);
  795. if (attr2->ns == NULL)
  796. return (1);
  797. if (attr1->ns->prefix == NULL)
  798. return (-1);
  799. if (attr2->ns->prefix == NULL)
  800. return (1);
  801. ret = xmlStrcmp(attr1->ns->href, attr2->ns->href);
  802. if (ret == 0) {
  803. ret = xmlStrcmp(attr1->name, attr2->name);
  804. }
  805. return (ret);
  806. }
  807. /**
  808. * xmlC14NPrintAttrs:
  809. * @attr: the pointer to attr
  810. * @ctx: the C14N context
  811. *
  812. * Prints out canonical attribute urrent node to the
  813. * buffer from C14N context as follows
  814. *
  815. * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
  816. *
  817. * Returns 1 on success or 0 on fail.
  818. */
  819. static int
  820. xmlC14NPrintAttrs(const xmlAttrPtr attr, xmlC14NCtxPtr ctx)
  821. {
  822. xmlChar *value;
  823. xmlChar *buffer;
  824. if ((attr == NULL) || (ctx == NULL)) {
  825. xmlC14NErrParam("writing attributes");
  826. return (0);
  827. }
  828. xmlOutputBufferWriteString(ctx->buf, " ");
  829. if (attr->ns != NULL && xmlStrlen(attr->ns->prefix) > 0) {
  830. xmlOutputBufferWriteString(ctx->buf,
  831. (const char *) attr->ns->prefix);
  832. xmlOutputBufferWriteString(ctx->buf, ":");
  833. }
  834. xmlOutputBufferWriteString(ctx->buf, (const char *) attr->name);
  835. xmlOutputBufferWriteString(ctx->buf, "=\"");
  836. value = xmlNodeListGetString(ctx->doc, attr->children, 1);
  837. /* todo: should we log an error if value==NULL ? */
  838. if (value != NULL) {
  839. buffer = xmlC11NNormalizeAttr(value);
  840. xmlFree(value);
  841. if (buffer != NULL) {
  842. xmlOutputBufferWriteString(ctx->buf, (const char *) buffer);
  843. xmlFree(buffer);
  844. } else {
  845. xmlC14NErrInternal("normalizing attributes axis");
  846. return (0);
  847. }
  848. }
  849. xmlOutputBufferWriteString(ctx->buf, "\"");
  850. return (1);
  851. }
  852. /**
  853. * xmlC14NFindHiddenParentAttr:
  854. *
  855. * Finds an attribute in a hidden parent node.
  856. *
  857. * Returns a pointer to the attribute node (if found) or NULL otherwise.
  858. */
  859. static xmlAttrPtr
  860. xmlC14NFindHiddenParentAttr(xmlC14NCtxPtr ctx, xmlNodePtr cur, const xmlChar * name, const xmlChar * ns)
  861. {
  862. xmlAttrPtr res;
  863. while((cur != NULL) && (!xmlC14NIsVisible(ctx, cur, cur->parent))) {
  864. res = xmlHasNsProp(cur, name, ns);
  865. if(res != NULL) {
  866. return res;
  867. }
  868. cur = cur->parent;
  869. }
  870. return NULL;
  871. }
  872. /**
  873. * xmlC14NFixupBaseAttr:
  874. *
  875. * Fixes up the xml:base attribute
  876. *
  877. * Returns the newly created attribute or NULL
  878. */
  879. static xmlAttrPtr
  880. xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml_base_attr)
  881. {
  882. xmlChar * res = NULL;
  883. xmlNodePtr cur;
  884. xmlAttrPtr attr;
  885. xmlChar * tmp_str;
  886. xmlChar * tmp_str2;
  887. int tmp_str_len;
  888. if ((ctx == NULL) || (xml_base_attr == NULL) || (xml_base_attr->parent == NULL)) {
  889. xmlC14NErrParam("processing xml:base attribute");
  890. return (NULL);
  891. }
  892. /* start from current value */
  893. res = xmlNodeListGetString(ctx->doc, xml_base_attr->children, 1);
  894. if(res == NULL) {
  895. xmlC14NErrInternal("processing xml:base attribute - can't get attr value");
  896. return (NULL);
  897. }
  898. /* go up the stack until we find a node that we rendered already */
  899. cur = xml_base_attr->parent->parent;
  900. while((cur != NULL) && (!xmlC14NIsVisible(ctx, cur, cur->parent))) {
  901. attr = xmlHasNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
  902. if(attr != NULL) {
  903. /* get attr value */
  904. tmp_str = xmlNodeListGetString(ctx->doc, attr->children, 1);
  905. if(tmp_str == NULL) {
  906. xmlFree(res);
  907. xmlC14NErrInternal("processing xml:base attribute - can't get attr value");
  908. return (NULL);
  909. }
  910. /* we need to add '/' if our current base uri ends with '..' or '.'
  911. to ensure that we are forced to go "up" all the time */
  912. tmp_str_len = xmlStrlen(tmp_str);
  913. if(tmp_str_len > 1 && tmp_str[tmp_str_len - 2] == '.') {
  914. tmp_str2 = xmlStrcat(tmp_str, BAD_CAST "/");
  915. if(tmp_str2 == NULL) {
  916. xmlFree(tmp_str);
  917. xmlFree(res);
  918. xmlC14NErrInternal("processing xml:base attribute - can't modify uri");
  919. return (NULL);
  920. }
  921. tmp_str = tmp_str2;
  922. }
  923. /* build uri */
  924. tmp_str2 = xmlBuildURI(res, tmp_str);
  925. if(tmp_str2 == NULL) {
  926. xmlFree(tmp_str);
  927. xmlFree(res);
  928. xmlC14NErrInternal("processing xml:base attribute - can't construct uri");
  929. return (NULL);
  930. }
  931. /* cleanup and set the new res */
  932. xmlFree(tmp_str);
  933. xmlFree(res);
  934. res = tmp_str2;
  935. }
  936. /* next */
  937. cur = cur->parent;
  938. }
  939. /* check if result uri is empty or not */
  940. if((res == NULL) || xmlStrEqual(res, BAD_CAST "")) {
  941. xmlFree(res);
  942. return (NULL);
  943. }
  944. /* create and return the new attribute node */
  945. attr = xmlNewNsProp(NULL, xml_base_attr->ns, BAD_CAST "base", res);
  946. if(attr == NULL) {
  947. xmlFree(res);
  948. xmlC14NErrInternal("processing xml:base attribute - can't construct attribute");
  949. return (NULL);
  950. }
  951. /* done */
  952. xmlFree(res);
  953. return (attr);
  954. }
  955. /**
  956. * xmlC14NProcessAttrsAxis:
  957. * @ctx: the C14N context
  958. * @cur: the current node
  959. * @parent_visible: the visibility of parent node
  960. * @all_parents_visible: the visibility of all parent nodes
  961. *
  962. * Prints out canonical attribute axis of the current node to the
  963. * buffer from C14N context as follows
  964. *
  965. * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
  966. *
  967. * Attribute Axis
  968. * In lexicographic order (ascending), process each node that
  969. * is in the element's attribute axis and in the node-set.
  970. *
  971. * The processing of an element node E MUST be modified slightly
  972. * when an XPath node-set is given as input and the element's
  973. * parent is omitted from the node-set.
  974. *
  975. *
  976. * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
  977. *
  978. * Canonical XML applied to a document subset requires the search of the
  979. * ancestor nodes of each orphan element node for attributes in the xml
  980. * namespace, such as xml:lang and xml:space. These are copied into the
  981. * element node except if a declaration of the same attribute is already
  982. * in the attribute axis of the element (whether or not it is included in
  983. * the document subset). This search and copying are omitted from the
  984. * Exclusive XML Canonicalization method.
  985. *
  986. * Returns 0 on success or -1 on fail.
  987. */
  988. static int
  989. xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int parent_visible)
  990. {
  991. xmlAttrPtr attr;
  992. xmlListPtr list;
  993. xmlAttrPtr attrs_to_delete = NULL;
  994. /* special processing for 1.1 spec */
  995. xmlAttrPtr xml_base_attr = NULL;
  996. xmlAttrPtr xml_lang_attr = NULL;
  997. xmlAttrPtr xml_space_attr = NULL;
  998. if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
  999. xmlC14NErrParam("processing attributes axis");
  1000. return (-1);
  1001. }
  1002. /*
  1003. * Create a sorted list to store element attributes
  1004. */
  1005. list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NAttrsCompare);
  1006. if (list == NULL) {
  1007. xmlC14NErrInternal("creating attributes list");
  1008. return (-1);
  1009. }
  1010. switch(ctx->mode) {
  1011. case XML_C14N_1_0:
  1012. /* The processing of an element node E MUST be modified slightly when an XPath node-set is
  1013. * given as input and the element's parent is omitted from the node-set. The method for processing
  1014. * the attribute axis of an element E in the node-set is enhanced. All element nodes along E's
  1015. * ancestor axis are examined for nearest occurrences of attributes in the xml namespace, such
  1016. * as xml:lang and xml:space (whether or not they are in the node-set). From this list of attributes,
  1017. * remove any that are in E's attribute axis (whether or not they are in the node-set). Then,
  1018. * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
  1019. * the node-set. The result of visiting the attribute axis is computed by processing the attribute
  1020. * nodes in this merged attribute list.
  1021. */
  1022. /*
  1023. * Add all visible attributes from current node.
  1024. */
  1025. attr = cur->properties;
  1026. while (attr != NULL) {
  1027. /* check that attribute is visible */
  1028. if (xmlC14NIsVisible(ctx, attr, cur)) {
  1029. xmlListInsert(list, attr);
  1030. }
  1031. attr = attr->next;
  1032. }
  1033. /*
  1034. * Handle xml attributes
  1035. */
  1036. if (parent_visible && (cur->parent != NULL) &&
  1037. (!xmlC14NIsVisible(ctx, cur->parent, cur->parent->parent)))
  1038. {
  1039. xmlNodePtr tmp;
  1040. /*
  1041. * If XPath node-set is not specified then the parent is always
  1042. * visible!
  1043. */
  1044. tmp = cur->parent;
  1045. while (tmp != NULL) {
  1046. attr = tmp->properties;
  1047. while (attr != NULL) {
  1048. if (xmlC14NIsXmlAttr(attr) != 0) {
  1049. if (xmlListSearch(list, attr) == NULL) {
  1050. xmlListInsert(list, attr);
  1051. }
  1052. }
  1053. attr = attr->next;
  1054. }
  1055. tmp = tmp->parent;
  1056. }
  1057. }
  1058. /* done */
  1059. break;
  1060. case XML_C14N_EXCLUSIVE_1_0:
  1061. /* attributes in the XML namespace, such as xml:lang and xml:space
  1062. * are not imported into orphan nodes of the document subset
  1063. */
  1064. /*
  1065. * Add all visible attributes from current node.
  1066. */
  1067. attr = cur->properties;
  1068. while (attr != NULL) {
  1069. /* check that attribute is visible */
  1070. if (xmlC14NIsVisible(ctx, attr, cur)) {
  1071. xmlListInsert(list, attr);
  1072. }
  1073. attr = attr->next;
  1074. }
  1075. /* do nothing special for xml attributes */
  1076. break;
  1077. case XML_C14N_1_1:
  1078. /* The processing of an element node E MUST be modified slightly when an XPath node-set is
  1079. * given as input and some of the element's ancestors are omitted from the node-set.
  1080. *
  1081. * Simple inheritable attributes are attributes that have a value that requires at most a simple
  1082. * redeclaration. This redeclaration is done by supplying a new value in the child axis. The
  1083. * redeclaration of a simple inheritable attribute A contained in one of E's ancestors is done
  1084. * by supplying a value to an attribute Ae inside E with the same name. Simple inheritable attributes
  1085. * are xml:lang and xml:space.
  1086. *
  1087. * The method for processing the attribute axis of an element E in the node-set is hence enhanced.
  1088. * All element nodes along E's ancestor axis are examined for the nearest occurrences of simple
  1089. * inheritable attributes in the xml namespace, such as xml:lang and xml:space (whether or not they
  1090. * are in the node-set). From this list of attributes, any simple inheritable attributes that are
  1091. * already in E's attribute axis (whether or not they are in the node-set) are removed. Then,
  1092. * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
  1093. * the node-set. The result of visiting the attribute axis is computed by processing the attribute
  1094. * nodes in this merged attribute list.
  1095. *
  1096. * The xml:id attribute is not a simple inheritable attribute and no processing of these attributes is
  1097. * performed.
  1098. *
  1099. * The xml:base attribute is not a simple inheritable attribute and requires special processing beyond
  1100. * a simple redeclaration.
  1101. *
  1102. * Attributes in the XML namespace other than xml:base, xml:id, xml:lang, and xml:space MUST be processed
  1103. * as ordinary attributes.
  1104. */
  1105. /*
  1106. * Add all visible attributes from current node.
  1107. */
  1108. attr = cur->properties;
  1109. while (attr != NULL) {
  1110. /* special processing for XML attribute kiks in only when we have invisible parents */
  1111. if ((!parent_visible) || (xmlC14NIsXmlAttr(attr) == 0)) {
  1112. /* check that attribute is visible */
  1113. if (xmlC14NIsVisible(ctx, attr, cur)) {
  1114. xmlListInsert(list, attr);
  1115. }
  1116. } else {
  1117. int matched = 0;
  1118. /* check for simple inheritance attributes */
  1119. if((!matched) && (xml_lang_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "lang")) {
  1120. xml_lang_attr = attr;
  1121. matched = 1;
  1122. }
  1123. if((!matched) && (xml_space_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "space")) {
  1124. xml_space_attr = attr;
  1125. matched = 1;
  1126. }
  1127. /* check for base attr */
  1128. if((!matched) && (xml_base_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "base")) {
  1129. xml_base_attr = attr;
  1130. matched = 1;
  1131. }
  1132. /* otherwise, it is a normal attribute, so just check if it is visible */
  1133. if((!matched) && xmlC14NIsVisible(ctx, attr, cur)) {
  1134. xmlListInsert(list, attr);
  1135. }
  1136. }
  1137. /* move to the next one */
  1138. attr = attr->next;
  1139. }
  1140. /* special processing for XML attribute kiks in only when we have invisible parents */
  1141. if ((parent_visible)) {
  1142. /* simple inheritance attributes - copy */
  1143. if(xml_lang_attr == NULL) {
  1144. xml_lang_attr = xmlC14NFindHiddenParentAttr(ctx, cur->parent, BAD_CAST "lang", XML_XML_NAMESPACE);
  1145. }
  1146. if(xml_lang_attr != NULL) {
  1147. xmlListInsert(list, xml_lang_attr);
  1148. }
  1149. if(xml_space_attr == NULL) {
  1150. xml_space_attr = xmlC14NFindHiddenParentAttr(ctx, cur->parent, BAD_CAST "space", XML_XML_NAMESPACE);
  1151. }
  1152. if(xml_space_attr != NULL) {
  1153. xmlListInsert(list, xml_space_attr);
  1154. }
  1155. /* base uri attribute - fix up */
  1156. if(xml_base_attr == NULL) {
  1157. /* if we don't have base uri attribute, check if we have a "hidden" one above */
  1158. xml_base_attr = xmlC14NFindHiddenParentAttr(ctx, cur->parent, BAD_CAST "base", XML_XML_NAMESPACE);
  1159. }
  1160. if(xml_base_attr != NULL) {
  1161. xml_base_attr = xmlC14NFixupBaseAttr(ctx, xml_base_attr);
  1162. if(xml_base_attr != NULL) {
  1163. xmlListInsert(list, xml_base_attr);
  1164. /* note that we MUST delete returned attr node ourselves! */
  1165. xml_base_attr->next = attrs_to_delete;
  1166. attrs_to_delete = xml_base_attr;
  1167. }
  1168. }
  1169. }
  1170. /* done */
  1171. break;
  1172. }
  1173. /*
  1174. * print out all elements from list
  1175. */
  1176. xmlListWalk(list, (xmlListWalker) xmlC14NPrintAttrs, (const void *) ctx);
  1177. /*
  1178. * Cleanup
  1179. */
  1180. xmlFreePropList(attrs_to_delete);
  1181. xmlListDelete(list);
  1182. return (0);
  1183. }
  1184. /**
  1185. * xmlC14NCheckForRelativeNamespaces:
  1186. * @ctx: the C14N context
  1187. * @cur: the current element node
  1188. *
  1189. * Checks that current element node has no relative namespaces defined
  1190. *
  1191. * Returns 0 if the node has no relative namespaces or -1 otherwise.
  1192. */
  1193. static int
  1194. xmlC14NCheckForRelativeNamespaces(xmlC14NCtxPtr ctx, xmlNodePtr cur)
  1195. {
  1196. xmlNsPtr ns;
  1197. if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
  1198. xmlC14NErrParam("checking for relative namespaces");
  1199. return (-1);
  1200. }
  1201. ns = cur->nsDef;
  1202. while (ns != NULL) {
  1203. if (xmlStrlen(ns->href) > 0) {
  1204. xmlURIPtr uri;
  1205. uri = xmlParseURI((const char *) ns->href);
  1206. if (uri == NULL) {
  1207. xmlC14NErrInternal("parsing namespace uri");
  1208. return (-1);
  1209. }
  1210. if (xmlStrlen((const xmlChar *) uri->scheme) == 0) {
  1211. xmlC14NErrRelativeNamespace(uri->scheme);
  1212. xmlFreeURI(uri);
  1213. return (-1);
  1214. }
  1215. if ((xmlStrcasecmp((const xmlChar *) uri->scheme, BAD_CAST "urn") != 0)
  1216. && (xmlStrcasecmp((const xmlChar *) uri->scheme, BAD_CAST "dav") !=0)
  1217. && (xmlStrlen((const xmlChar *) uri->server) == 0)) {
  1218. xmlC14NErrRelativeNamespace(uri->scheme);
  1219. xmlFreeURI(uri);
  1220. return (-1);
  1221. }
  1222. xmlFreeURI(uri);
  1223. }
  1224. ns = ns->next;
  1225. }
  1226. return (0);
  1227. }
  1228. /**
  1229. * xmlC14NProcessElementNode:
  1230. * @ctx: the pointer to C14N context object
  1231. * @cur: the node to process
  1232. * @visible: this node is visible
  1233. * @all_parents_visible: whether all the parents of this node are visible
  1234. *
  1235. * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
  1236. *
  1237. * Element Nodes
  1238. * If the element is not in the node-set, then the result is obtained
  1239. * by processing the namespace axis, then the attribute axis, then
  1240. * processing the child nodes of the element that are in the node-set
  1241. * (in document order). If the element is in the node-set, then the result
  1242. * is an open angle bracket (<), the element QName, the result of
  1243. * processing the namespace axis, the result of processing the attribute
  1244. * axis, a close angle bracket (>), the result of processing the child
  1245. * nodes of the element that are in the node-set (in document order), an
  1246. * open angle bracket, a forward slash (/), the element QName, and a close
  1247. * angle bracket.
  1248. *
  1249. * Returns non-negative value on success or negative value on fail
  1250. */
  1251. static int
  1252. xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible)
  1253. {
  1254. int ret;
  1255. xmlC14NVisibleNsStack state;
  1256. int parent_is_doc = 0;
  1257. if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
  1258. xmlC14NErrParam("processing element node");
  1259. return (-1);
  1260. }
  1261. /*
  1262. * Check relative relative namespaces:
  1263. * implementations of XML canonicalization MUST report an operation
  1264. * failure on documents containing relative namespace URIs.
  1265. */
  1266. if (xmlC14NCheckForRelativeNamespaces(ctx, cur) < 0) {
  1267. xmlC14NErrInternal("checking for relative namespaces");
  1268. return (-1);
  1269. }
  1270. /*
  1271. * Save ns_rendered stack position
  1272. */
  1273. memset(&state, 0, sizeof(state));
  1274. xmlC14NVisibleNsStackSave(ctx->ns_rendered, &state);
  1275. if (visible) {
  1276. if (ctx->parent_is_doc) {
  1277. /* save this flag into the stack */
  1278. parent_is_doc = ctx->parent_is_doc;
  1279. ctx->parent_is_doc = 0;
  1280. ctx->pos = XMLC14N_INSIDE_DOCUMENT_ELEMENT;
  1281. }
  1282. xmlOutputBufferWriteString(ctx->buf, "<");
  1283. if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) {
  1284. xmlOutputBufferWriteString(ctx->buf,
  1285. (const char *) cur->ns->prefix);
  1286. xmlOutputBufferWriteString(ctx->buf, ":");
  1287. }
  1288. xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name);
  1289. }
  1290. if (!xmlC14NIsExclusive(ctx)) {
  1291. ret = xmlC14NProcessNamespacesAxis(ctx, cur, visible);
  1292. } else {
  1293. ret = xmlExcC14NProcessNamespacesAxis(ctx, cur, visible);
  1294. }
  1295. if (ret < 0) {
  1296. xmlC14NErrInternal("processing namespaces axis");
  1297. return (-1);
  1298. }
  1299. /* todo: shouldn't this go to "visible only"? */
  1300. if(visible) {
  1301. xmlC14NVisibleNsStackShift(ctx->ns_rendered);
  1302. }
  1303. ret = xmlC14NProcessAttrsAxis(ctx, cur, visible);
  1304. if (ret < 0) {
  1305. xmlC14NErrInternal("processing attributes axis");
  1306. return (-1);
  1307. }
  1308. if (visible) {
  1309. xmlOutputBufferWriteString(ctx->buf, ">");
  1310. }
  1311. if (cur->children != NULL) {
  1312. ret = xmlC14NProcessNodeList(ctx, cur->children);
  1313. if (ret < 0) {
  1314. xmlC14NErrInternal("processing childrens list");
  1315. return (-1);
  1316. }
  1317. }
  1318. if (visible) {
  1319. xmlOutputBufferWriteString(ctx->buf, "</");
  1320. if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) {
  1321. xmlOutputBufferWriteString(ctx->buf,
  1322. (const char *) cur->ns->prefix);
  1323. xmlOutputBufferWriteString(ctx->buf, ":");
  1324. }
  1325. xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name);
  1326. xmlOutputBufferWriteString(ctx->buf, ">");
  1327. if (parent_is_doc) {
  1328. /* restore this flag from the stack for next node */
  1329. ctx->parent_is_doc = parent_is_doc;
  1330. ctx->pos = XMLC14N_AFTER_DOCUMENT_ELEMENT;
  1331. }
  1332. }
  1333. /*
  1334. * Restore ns_rendered stack position
  1335. */
  1336. xmlC14NVisibleNsStackRestore(ctx->ns_rendered, &state);
  1337. return (0);
  1338. }
  1339. /**
  1340. * xmlC14NProcessNode:
  1341. * @ctx: the pointer to C14N context object
  1342. * @cur: the node to process
  1343. *
  1344. * Processes the given node
  1345. *
  1346. * Returns non-negative value on success or negative value on fail
  1347. */
  1348. static int
  1349. xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur)
  1350. {
  1351. int ret = 0;
  1352. int visible;
  1353. if ((ctx == NULL) || (cur == NULL)) {
  1354. xmlC14NErrParam("processing node");
  1355. return (-1);
  1356. }
  1357. visible = xmlC14NIsVisible(ctx, cur, cur->parent);
  1358. switch (cur->type) {
  1359. case XML_ELEMENT_NODE:
  1360. ret = xmlC14NProcessElementNode(ctx, cur, visible);
  1361. break;
  1362. case XML_CDATA_SECTION_NODE:
  1363. case XML_TEXT_NODE:
  1364. /*
  1365. * Text Nodes
  1366. * the string value, except all ampersands are replaced
  1367. * by &amp;, all open angle brackets (<) are replaced by &lt;, all closing
  1368. * angle brackets (>) are replaced by &gt;, and all #xD characters are
  1369. * replaced by &#xD;.
  1370. */
  1371. /* cdata sections are processed as text nodes */
  1372. /* todo: verify that cdata sections are included in XPath nodes set */
  1373. if ((visible) && (cur->content != NULL)) {
  1374. xmlChar *buffer;
  1375. buffer = xmlC11NNormalizeText(cur->content);
  1376. if (buffer != NULL) {
  1377. xmlOutputBufferWriteString(ctx->buf,
  1378. (const char *) buffer);
  1379. xmlFree(buffer);
  1380. } else {
  1381. xmlC14NErrInternal("normalizing text node");
  1382. return (-1);
  1383. }
  1384. }
  1385. break;
  1386. case XML_PI_NODE:
  1387. /*
  1388. * Processing Instruction (PI) Nodes-
  1389. * The opening PI symbol (<?), the PI target name of the node,
  1390. * a leading space and the string value if it is not empty, and
  1391. * the closing PI symbol (?>). If the string value is empty,
  1392. * then the leading space is not added. Also, a trailing #xA is
  1393. * rendered after the closing PI symbol for PI children of the
  1394. * root node with a lesser document order than the document
  1395. * element, and a leading #xA is rendered before the opening PI
  1396. * symbol of PI children of the root node with a greater document
  1397. * order than the document element.
  1398. */
  1399. if (visible) {
  1400. if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) {
  1401. xmlOutputBufferWriteString(ctx->buf, "\x0A<?");
  1402. } else {
  1403. xmlOutputBufferWriteString(ctx->buf, "<?");
  1404. }
  1405. xmlOutputBufferWriteString(ctx->buf,
  1406. (const char *) cur->name);
  1407. if ((cur->content != NULL) && (*(cur->content) != '\0')) {
  1408. xmlChar *buffer;
  1409. xmlOutputBufferWriteString(ctx->buf, " ");
  1410. /* todo: do we need to normalize pi? */
  1411. buffer = xmlC11NNormalizePI(cur->content);
  1412. if (buffer != NULL) {
  1413. xmlOutputBufferWriteString(ctx->buf,
  1414. (const char *) buffer);
  1415. xmlFree(buffer);
  1416. } else {
  1417. xmlC14NErrInternal("normalizing pi node");
  1418. return (-1);
  1419. }
  1420. }
  1421. if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) {
  1422. xmlOutputBufferWriteString(ctx->buf, "?>\x0A");
  1423. } else {
  1424. xmlOutputBufferWriteString(ctx->buf, "?>");
  1425. }
  1426. }
  1427. break;
  1428. case XML_COMMENT_NODE:
  1429. /*
  1430. * Comment Nodes
  1431. * Nothing if generating canonical XML without comments. For
  1432. * canonical XML with comments, generate the opening comment
  1433. * symbol (<!--), the string value of the node, and the
  1434. * closing comment symbol (-->). Also, a trailing #xA is rendered
  1435. * after the closing comment symbol for comment children of the
  1436. * root node with a lesser document order than the document
  1437. * element, and a leading #xA is rendered before the opening
  1438. * comment symbol of comment children of the root node with a
  1439. * greater document order than the document element. (Comment
  1440. * children of the root node represent comments outside of the
  1441. * top-level document element and outside of the document type
  1442. * declaration).
  1443. */
  1444. if (visible && ctx->with_comments) {
  1445. if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) {
  1446. xmlOutputBufferWriteString(ctx->buf, "\x0A<!--");
  1447. } else {
  1448. xmlOutputBufferWriteString(ctx->buf, "<!--");
  1449. }
  1450. if (cur->content != NULL) {
  1451. xmlChar *buffer;
  1452. /* todo: do we need to normalize comment? */
  1453. buffer = xmlC11NNormalizeComment(cur->content);
  1454. if (buffer != NULL) {
  1455. xmlOutputBufferWriteString(ctx->buf,
  1456. (const char *) buffer);
  1457. xmlFree(buffer);
  1458. } else {
  1459. xmlC14NErrInternal("normalizing comment node");
  1460. return (-1);
  1461. }
  1462. }
  1463. if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) {
  1464. xmlOutputBufferWriteString(ctx->buf, "-->\x0A");
  1465. } else {
  1466. xmlOutputBufferWriteString(ctx->buf, "-->");
  1467. }
  1468. }
  1469. break;
  1470. case XML_DOCUMENT_NODE:
  1471. case XML_DOCUMENT_FRAG_NODE: /* should be processed as document? */
  1472. #ifdef LIBXML_DOCB_ENABLED
  1473. case XML_DOCB_DOCUMENT_NODE: /* should be processed as document? */
  1474. #endif
  1475. #ifdef LIBXML_HTML_ENABLED
  1476. case XML_HTML_DOCUMENT_NODE: /* should be processed as document? */
  1477. #endif
  1478. if (cur->children != NULL) {
  1479. ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT;
  1480. ctx->parent_is_doc = 1;
  1481. ret = xmlC14NProcessNodeList(ctx, cur->children);
  1482. }
  1483. break;
  1484. case XML_ATTRIBUTE_NODE:
  1485. xmlC14NErrInvalidNode("XML_ATTRIBUTE_NODE", "processing node");
  1486. return (-1);
  1487. case XML_NAMESPACE_DECL:
  1488. xmlC14NErrInvalidNode("XML_NAMESPACE_DECL", "processing node");
  1489. return (-1);
  1490. case XML_ENTITY_REF_NODE:
  1491. xmlC14NErrInvalidNode("XML_ENTITY_REF_NODE", "processing node");
  1492. return (-1);
  1493. case XML_ENTITY_NODE:
  1494. xmlC14NErrInvalidNode("XML_ENTITY_NODE", "processing node");
  1495. return (-1);
  1496. case XML_DOCUMENT_TYPE_NODE:
  1497. case XML_NOTATION_NODE:
  1498. case XML_DTD_NODE:
  1499. case XML_ELEMENT_DECL:
  1500. case XML_ATTRIBUTE_DECL:
  1501. case XML_ENTITY_DECL:
  1502. #ifdef LIBXML_XINCLUDE_ENABLED
  1503. case XML_XINCLUDE_START:
  1504. case XML_XINCLUDE_END:
  1505. #endif
  1506. /*
  1507. * should be ignored according to "W3C Canonical XML"
  1508. */
  1509. break;
  1510. default:
  1511. xmlC14NErrUnknownNode(cur->type, "processing node");
  1512. return (-1);
  1513. }
  1514. return (ret);
  1515. }
  1516. /**
  1517. * xmlC14NProcessNodeList:
  1518. * @ctx: the pointer to C14N context object
  1519. * @cur: the node to start from
  1520. *
  1521. * Processes all nodes in the row starting from cur.
  1522. *
  1523. * Returns non-negative value on success or negative value on fail
  1524. */
  1525. static int
  1526. xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur)
  1527. {
  1528. int ret;
  1529. if (ctx == NULL) {
  1530. xmlC14NErrParam("processing node list");
  1531. return (-1);
  1532. }
  1533. for (ret = 0; cur != NULL && ret >= 0; cur = cur->next) {
  1534. ret = xmlC14NProcessNode(ctx, cur);
  1535. }
  1536. return (ret);
  1537. }
  1538. /**
  1539. * xmlC14NFreeCtx:
  1540. * @ctx: the pointer to C14N context object
  1541. *
  1542. * Cleanups the C14N context object.
  1543. */
  1544. static void
  1545. xmlC14NFreeCtx(xmlC14NCtxPtr ctx)
  1546. {
  1547. if (ctx == NULL) {
  1548. xmlC14NErrParam("freeing context");
  1549. return;
  1550. }
  1551. if (ctx->ns_rendered != NULL) {
  1552. xmlC14NVisibleNsStackDestroy(ctx->ns_rendered);
  1553. }
  1554. xmlFree(ctx);
  1555. }
  1556. /**
  1557. * xmlC14NNewCtx:
  1558. * @doc: the XML document for canonization
  1559. * @is_visible_callback:the function to use to determine is node visible
  1560. * or not
  1561. * @user_data: the first parameter for @is_visible_callback function
  1562. * (in most cases, it is nodes set)
  1563. * @mode: the c14n mode (see @xmlC14NMode)
  1564. * @inclusive_ns_prefixe the list of inclusive namespace prefixes
  1565. * ended with a NULL or NULL if there is no
  1566. * inclusive namespaces (only for `
  1567. * canonicalization)
  1568. * @with_comments: include comments in the result (!=0) or not (==0)
  1569. * @buf: the output buffer to store canonical XML; this
  1570. * buffer MUST have encoder==NULL because C14N requires
  1571. * UTF-8 output
  1572. *
  1573. * Creates new C14N context object to store C14N parameters.
  1574. *
  1575. * Returns pointer to newly created object (success) or NULL (fail)
  1576. */
  1577. static xmlC14NCtxPtr
  1578. xmlC14NNewCtx(xmlDocPtr doc,
  1579. xmlC14NIsVisibleCallback is_visible_callback, void* user_data,
  1580. xmlC14NMode mode, xmlChar ** inclusive_ns_prefixes,
  1581. int with_comments, xmlOutputBufferPtr buf)
  1582. {
  1583. xmlC14NCtxPtr ctx = NULL;
  1584. if ((doc == NULL) || (buf == NULL)) {
  1585. xmlC14NErrParam("creating new context");
  1586. return (NULL);
  1587. }
  1588. /*
  1589. * Validate the encoding output buffer encoding
  1590. */
  1591. if (buf->encoder != NULL) {
  1592. xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8,
  1593. "xmlC14NNewCtx: output buffer encoder != NULL but C14N requires UTF8 output\n");
  1594. return (NULL);
  1595. }
  1596. /*
  1597. * Validate the XML document encoding value, if provided.
  1598. */
  1599. if (doc->charset != XML_CHAR_ENCODING_UTF8) {
  1600. xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8,
  1601. "xmlC14NNewCtx: source document not in UTF8\n");
  1602. return (NULL);
  1603. }
  1604. /*
  1605. * Allocate a new xmlC14NCtxPtr and fill the fields.
  1606. */
  1607. ctx = (xmlC14NCtxPtr) xmlMalloc(sizeof(xmlC14NCtx));
  1608. if (ctx == NULL) {
  1609. xmlC14NErrMemory("creating context");
  1610. return (NULL);
  1611. }
  1612. memset(ctx, 0, sizeof(xmlC14NCtx));
  1613. /*
  1614. * initialize C14N context
  1615. */
  1616. ctx->doc = doc;
  1617. ctx->with_comments = with_comments;
  1618. ctx->is_visible_callback = is_visible_callback;
  1619. ctx->user_data = user_data;
  1620. ctx->buf = buf;
  1621. ctx->parent_is_doc = 1;
  1622. ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT;
  1623. ctx->ns_rendered = xmlC14NVisibleNsStackCreate();
  1624. if(ctx->ns_rendered == NULL) {
  1625. xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_CREATE_STACK,
  1626. "xmlC14NNewCtx: xmlC14NVisibleNsStackCreate failed\n");
  1627. xmlC14NFreeCtx(ctx);
  1628. return (NULL);
  1629. }
  1630. /*
  1631. * Set "mode" flag and remember list of incluseve prefixes
  1632. * for exclusive c14n
  1633. */
  1634. ctx->mode = mode;
  1635. if(xmlC14NIsExclusive(ctx)) {
  1636. ctx->inclusive_ns_prefixes = inclusive_ns_prefixes;
  1637. }
  1638. return (ctx);
  1639. }
  1640. /**
  1641. * xmlC14NExecute:
  1642. * @doc: the XML document for canonization
  1643. * @is_visible_callback:the function to use to determine is node visible
  1644. * or not
  1645. * @user_data: the first parameter for @is_visible_callback function
  1646. * (in most cases, it is nodes set)
  1647. * @mode: the c14n mode (see @xmlC14NMode)
  1648. * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
  1649. * ended with a NULL or NULL if there is no
  1650. * inclusive namespaces (only for exclusive
  1651. * canonicalization, ignored otherwise)
  1652. * @with_comments: include comments in the result (!=0) or not (==0)
  1653. * @buf: the output buffer to store canonical XML; this
  1654. * buffer MUST have encoder==NULL because C14N requires
  1655. * UTF-8 output
  1656. *
  1657. * Dumps the canonized image of given XML document into the provided buffer.
  1658. * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
  1659. * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
  1660. *
  1661. * Returns non-negative value on success or a negative value on fail
  1662. */
  1663. int
  1664. xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback is_visible_callback,
  1665. void* user_data, int mode, xmlChar **inclusive_ns_prefixes,
  1666. int with_comments, xmlOutputBufferPtr buf) {
  1667. xmlC14NCtxPtr ctx;
  1668. xmlC14NMode c14n_mode = XML_C14N_1_0;
  1669. int ret;
  1670. if ((buf == NULL) || (doc == NULL)) {
  1671. xmlC14NErrParam("executing c14n");
  1672. return (-1);
  1673. }
  1674. /* for backward compatibility, we have to have "mode" as "int"
  1675. and here we check that user gives valid value */
  1676. switch(mode) {
  1677. case XML_C14N_1_0:
  1678. case XML_C14N_EXCLUSIVE_1_0:
  1679. case XML_C14N_1_1:
  1680. c14n_mode = (xmlC14NMode)mode;
  1681. break;
  1682. default:
  1683. xmlC14NErrParam("invalid mode for executing c14n");
  1684. return (-1);
  1685. }
  1686. /*
  1687. * Validate the encoding output buffer encoding
  1688. */
  1689. if (buf->encoder != NULL) {
  1690. xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8,
  1691. "xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n");
  1692. return (-1);
  1693. }
  1694. ctx = xmlC14NNewCtx(doc, is_visible_callback, user_data,
  1695. c14n_mode, inclusive_ns_prefixes,
  1696. with_comments, buf);
  1697. if (ctx == NULL) {
  1698. xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_CREATE_CTXT,
  1699. "xmlC14NExecute: unable to create C14N context\n");
  1700. return (-1);
  1701. }
  1702. /*
  1703. * Root Node
  1704. * The root node is the parent of the top-level document element. The
  1705. * result of processing each of its child nodes that is in the node-set
  1706. * in document order. The root node does not generate a byte order mark,
  1707. * XML declaration, nor anything from within the document type
  1708. * declaration.
  1709. */
  1710. if (doc->children != NULL) {
  1711. ret = xmlC14NProcessNodeList(ctx, doc->children);
  1712. if (ret < 0) {
  1713. xmlC14NErrInternal("processing docs children list");
  1714. xmlC14NFreeCtx(ctx);
  1715. return (-1);
  1716. }
  1717. }
  1718. /*
  1719. * Flush buffer to get number of bytes written
  1720. */
  1721. ret = xmlOutputBufferFlush(buf);
  1722. if (ret < 0) {
  1723. xmlC14NErrInternal("flushing output buffer");
  1724. xmlC14NFreeCtx(ctx);
  1725. return (-1);
  1726. }
  1727. /*
  1728. * Cleanup
  1729. */
  1730. xmlC14NFreeCtx(ctx);
  1731. return (ret);
  1732. }
  1733. /**
  1734. * xmlC14NDocSaveTo:
  1735. * @doc: the XML document for canonization
  1736. * @nodes: the nodes set to be included in the canonized image
  1737. * or NULL if all document nodes should be included
  1738. * @mode: the c14n mode (see @xmlC14NMode)
  1739. * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
  1740. * ended with a NULL or NULL if there is no
  1741. * inclusive namespaces (only for exclusive
  1742. * canonicalization, ignored otherwise)
  1743. * @with_comments: include comments in the result (!=0) or not (==0)
  1744. * @buf: the output buffer to store canonical XML; this
  1745. * buffer MUST have encoder==NULL because C14N requires
  1746. * UTF-8 output
  1747. *
  1748. * Dumps the canonized image of given XML document into the provided buffer.
  1749. * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
  1750. * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
  1751. *
  1752. * Returns non-negative value on success or a negative value on fail
  1753. */
  1754. int
  1755. xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes,
  1756. int mode, xmlChar ** inclusive_ns_prefixes,
  1757. int with_comments, xmlOutputBufferPtr buf) {
  1758. return(xmlC14NExecute(doc,
  1759. (xmlC14NIsVisibleCallback)xmlC14NIsNodeInNodeset,
  1760. nodes,
  1761. mode,
  1762. inclusive_ns_prefixes,
  1763. with_comments,
  1764. buf));
  1765. }
  1766. /**
  1767. * xmlC14NDocDumpMemory:
  1768. * @doc: the XML document for canonization
  1769. * @nodes: the nodes set to be included in the canonized image
  1770. * or NULL if all document nodes should be included
  1771. * @mode: the c14n mode (see @xmlC14NMode)
  1772. * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
  1773. * ended with a NULL or NULL if there is no
  1774. * inclusive namespaces (only for exclusive
  1775. * canonicalization, ignored otherwise)
  1776. * @with_comments: include comments in the result (!=0) or not (==0)
  1777. * @doc_txt_ptr: the memory pointer for allocated canonical XML text;
  1778. * the caller of this functions is responsible for calling
  1779. * xmlFree() to free allocated memory
  1780. *
  1781. * Dumps the canonized image of given XML document into memory.
  1782. * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
  1783. * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
  1784. *
  1785. * Returns the number of bytes written on success or a negative value on fail
  1786. */
  1787. int
  1788. xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr nodes,
  1789. int mode, xmlChar ** inclusive_ns_prefixes,
  1790. int with_comments, xmlChar ** doc_txt_ptr)
  1791. {
  1792. int ret;
  1793. xmlOutputBufferPtr buf;
  1794. if (doc_txt_ptr == NULL) {
  1795. xmlC14NErrParam("dumping doc to memory");
  1796. return (-1);
  1797. }
  1798. *doc_txt_ptr = NULL;
  1799. /*
  1800. * create memory buffer with UTF8 (default) encoding
  1801. */
  1802. buf = xmlAllocOutputBuffer(NULL);
  1803. if (buf == NULL) {
  1804. xmlC14NErrMemory("creating output buffer");
  1805. return (-1);
  1806. }
  1807. /*
  1808. * canonize document and write to buffer
  1809. */
  1810. ret = xmlC14NDocSaveTo(doc, nodes, mode, inclusive_ns_prefixes,
  1811. with_comments, buf);
  1812. if (ret < 0) {
  1813. xmlC14NErrInternal("saving doc to output buffer");
  1814. (void) xmlOutputBufferClose(buf);
  1815. return (-1);
  1816. }
  1817. ret = buf->buffer->use;
  1818. if (ret > 0) {
  1819. *doc_txt_ptr = xmlStrndup(buf->buffer->content, ret);
  1820. }
  1821. (void) xmlOutputBufferClose(buf);
  1822. if ((*doc_txt_ptr == NULL) && (ret > 0)) {
  1823. xmlC14NErrMemory("coping canonicanized document");
  1824. return (-1);
  1825. }
  1826. return (ret);
  1827. }
  1828. /**
  1829. * xmlC14NDocSave:
  1830. * @doc: the XML document for canonization
  1831. * @nodes: the nodes set to be included in the canonized image
  1832. * or NULL if all document nodes should be included
  1833. * @mode: the c14n mode (see @xmlC14NMode)
  1834. * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
  1835. * ended with a NULL or NULL if there is no
  1836. * inclusive namespaces (only for exclusive
  1837. * canonicalization, ignored otherwise)
  1838. * @with_comments: include comments in the result (!=0) or not (==0)
  1839. * @filename: the filename to store canonical XML image
  1840. * @compression: the compression level (zlib requred):
  1841. * -1 - libxml default,
  1842. * 0 - uncompressed,
  1843. * >0 - compression level
  1844. *
  1845. * Dumps the canonized image of given XML document into the file.
  1846. * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
  1847. * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
  1848. *
  1849. * Returns the number of bytes written success or a negative value on fail
  1850. */
  1851. int
  1852. xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes,
  1853. int mode, xmlChar ** inclusive_ns_prefixes,
  1854. int with_comments, const char *filename, int compression)
  1855. {
  1856. xmlOutputBufferPtr buf;
  1857. int ret;
  1858. if (filename == NULL) {
  1859. xmlC14NErrParam("saving doc");
  1860. return (-1);
  1861. }
  1862. #ifdef HAVE_ZLIB_H
  1863. if (compression < 0)
  1864. compression = xmlGetCompressMode();
  1865. #endif
  1866. /*
  1867. * save the content to a temp buffer, use default UTF8 encoding.
  1868. */
  1869. buf = xmlOutputBufferCreateFilename(filename, NULL, compression);
  1870. if (buf == NULL) {
  1871. xmlC14NErrInternal("creating temporary filename");
  1872. return (-1);
  1873. }
  1874. /*
  1875. * canonize document and write to buffer
  1876. */
  1877. ret = xmlC14NDocSaveTo(doc, nodes, mode, inclusive_ns_prefixes,
  1878. with_comments, buf);
  1879. if (ret < 0) {
  1880. xmlC14NErrInternal("cannicanize document to buffer");
  1881. (void) xmlOutputBufferClose(buf);
  1882. return (-1);
  1883. }
  1884. /*
  1885. * get the numbers of bytes written
  1886. */
  1887. ret = xmlOutputBufferClose(buf);
  1888. return (ret);
  1889. }
  1890. /*
  1891. * Macro used to grow the current buffer.
  1892. */
  1893. #define growBufferReentrant() { \
  1894. buffer_size *= 2; \
  1895. buffer = (xmlChar *) \
  1896. xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \
  1897. if (buffer == NULL) { \
  1898. xmlC14NErrMemory("growing buffer"); \
  1899. return(NULL); \
  1900. } \
  1901. }
  1902. /**
  1903. * xmlC11NNormalizeString:
  1904. * @input: the input string
  1905. * @mode: the normalization mode (attribute, comment, PI or text)
  1906. *
  1907. * Converts a string to a canonical (normalized) format. The code is stolen
  1908. * from xmlEncodeEntitiesReentrant(). Added normalization of \x09, \x0a, \x0A
  1909. * and the @mode parameter
  1910. *
  1911. * Returns a normalized string (caller is responsible for calling xmlFree())
  1912. * or NULL if an error occurs
  1913. */
  1914. static xmlChar *
  1915. xmlC11NNormalizeString(const xmlChar * input,
  1916. xmlC14NNormalizationMode mode)
  1917. {
  1918. const xmlChar *cur = input;
  1919. xmlChar *buffer = NULL;
  1920. xmlChar *out = NULL;
  1921. int buffer_size = 0;
  1922. if (input == NULL)
  1923. return (NULL);
  1924. /*
  1925. * allocate an translation buffer.
  1926. */
  1927. buffer_size = 1000;
  1928. buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
  1929. if (buffer == NULL) {
  1930. xmlC14NErrMemory("allocating buffer");
  1931. return (NULL);
  1932. }
  1933. out = buffer;
  1934. while (*cur != '\0') {
  1935. if ((out - buffer) > (buffer_size - 10)) {
  1936. int indx = out - buffer;
  1937. growBufferReentrant();
  1938. out = &buffer[indx];
  1939. }
  1940. if ((*cur == '<') && ((mode == XMLC14N_NORMALIZE_ATTR) ||
  1941. (mode == XMLC14N_NORMALIZE_TEXT))) {
  1942. *out++ = '&';
  1943. *out++ = 'l';
  1944. *out++ = 't';
  1945. *out++ = ';';
  1946. } else if ((*cur == '>') && (mode == XMLC14N_NORMALIZE_TEXT)) {
  1947. *out++ = '&';
  1948. *out++ = 'g';
  1949. *out++ = 't';
  1950. *out++ = ';';
  1951. } else if ((*cur == '&') && ((mode == XMLC14N_NORMALIZE_ATTR) ||
  1952. (mode == XMLC14N_NORMALIZE_TEXT))) {
  1953. *out++ = '&';
  1954. *out++ = 'a';
  1955. *out++ = 'm';
  1956. *out++ = 'p';
  1957. *out++ = ';';
  1958. } else if ((*cur == '"') && (mode == XMLC14N_NORMALIZE_ATTR)) {
  1959. *out++ = '&';
  1960. *out++ = 'q';
  1961. *out++ = 'u';
  1962. *out++ = 'o';
  1963. *out++ = 't';
  1964. *out++ = ';';
  1965. } else if ((*cur == '\x09') && (mode == XMLC14N_NORMALIZE_ATTR)) {
  1966. *out++ = '&';
  1967. *out++ = '#';
  1968. *out++ = 'x';
  1969. *out++ = '9';
  1970. *out++ = ';';
  1971. } else if ((*cur == '\x0A') && (mode == XMLC14N_NORMALIZE_ATTR)) {
  1972. *out++ = '&';
  1973. *out++ = '#';
  1974. *out++ = 'x';
  1975. *out++ = 'A';
  1976. *out++ = ';';
  1977. } else if ((*cur == '\x0D') && ((mode == XMLC14N_NORMALIZE_ATTR) ||
  1978. (mode == XMLC14N_NORMALIZE_TEXT) ||
  1979. (mode == XMLC14N_NORMALIZE_COMMENT) ||
  1980. (mode == XMLC14N_NORMALIZE_PI))) {
  1981. *out++ = '&';
  1982. *out++ = '#';
  1983. *out++ = 'x';
  1984. *out++ = 'D';
  1985. *out++ = ';';
  1986. } else {
  1987. /*
  1988. * Works because on UTF-8, all extended sequences cannot
  1989. * result in bytes in the ASCII range.
  1990. */
  1991. *out++ = *cur;
  1992. }
  1993. cur++;
  1994. }
  1995. *out = 0;
  1996. return (buffer);
  1997. }
  1998. #endif /* LIBXML_OUTPUT_ENABLED */
  1999. #define bottom_c14n
  2000. #include "elfgcchack.h"
  2001. #endif /* LIBXML_C14N_ENABLED */