uri.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  1. /**
  2. * uri.c: set of generic URI related routines
  3. *
  4. * Reference: RFCs 3986, 2732 and 2373
  5. *
  6. * See Copyright for the status of this software.
  7. *
  8. * daniel@veillard.com
  9. */
  10. #define IN_LIBXML
  11. #include "libxml.h"
  12. #include <limits.h>
  13. #include <string.h>
  14. #include <libxml/xmlmemory.h>
  15. #include <libxml/uri.h>
  16. #include <libxml/globals.h>
  17. #include <libxml/xmlerror.h>
  18. /**
  19. * MAX_URI_LENGTH:
  20. *
  21. * The definition of the URI regexp in the above RFC has no size limit
  22. * In practice they are usually relatively short except for the
  23. * data URI scheme as defined in RFC 2397. Even for data URI the usual
  24. * maximum size before hitting random practical limits is around 64 KB
  25. * and 4KB is usually a maximum admitted limit for proper operations.
  26. * The value below is more a security limit than anything else and
  27. * really should never be hit by 'normal' operations
  28. * Set to 1 MByte in 2012, this is only enforced on output
  29. */
  30. #define MAX_URI_LENGTH 1024 * 1024
  31. static void
  32. xmlURIErrMemory(const char *extra)
  33. {
  34. if (extra)
  35. __xmlRaiseError(NULL, NULL, NULL,
  36. NULL, NULL, XML_FROM_URI,
  37. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
  38. extra, NULL, NULL, 0, 0,
  39. "Memory allocation failed : %s\n", extra);
  40. else
  41. __xmlRaiseError(NULL, NULL, NULL,
  42. NULL, NULL, XML_FROM_URI,
  43. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
  44. NULL, NULL, NULL, 0, 0,
  45. "Memory allocation failed\n");
  46. }
  47. static void xmlCleanURI(xmlURIPtr uri);
  48. /*
  49. * Old rule from 2396 used in legacy handling code
  50. * alpha = lowalpha | upalpha
  51. */
  52. #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
  53. /*
  54. * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
  55. * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
  56. * "u" | "v" | "w" | "x" | "y" | "z"
  57. */
  58. #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z'))
  59. /*
  60. * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" |
  61. * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" |
  62. * "U" | "V" | "W" | "X" | "Y" | "Z"
  63. */
  64. #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z'))
  65. #ifdef IS_DIGIT
  66. #undef IS_DIGIT
  67. #endif
  68. /*
  69. * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
  70. */
  71. #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
  72. /*
  73. * alphanum = alpha | digit
  74. */
  75. #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
  76. /*
  77. * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
  78. */
  79. #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
  80. ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
  81. ((x) == '(') || ((x) == ')'))
  82. /*
  83. * unwise = "{" | "}" | "|" | "\" | "^" | "`"
  84. */
  85. #define IS_UNWISE(p) \
  86. (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \
  87. ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \
  88. ((*(p) == ']')) || ((*(p) == '`')))
  89. /*
  90. * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
  91. * "[" | "]"
  92. */
  93. #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
  94. ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
  95. ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \
  96. ((x) == ']'))
  97. /*
  98. * unreserved = alphanum | mark
  99. */
  100. #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
  101. /*
  102. * Skip to next pointer char, handle escaped sequences
  103. */
  104. #define NEXT(p) ((*p == '%')? p += 3 : p++)
  105. /*
  106. * Productions from the spec.
  107. *
  108. * authority = server | reg_name
  109. * reg_name = 1*( unreserved | escaped | "$" | "," |
  110. * ";" | ":" | "@" | "&" | "=" | "+" )
  111. *
  112. * path = [ abs_path | opaque_part ]
  113. */
  114. #define STRNDUP(s, n) (char *) xmlStrndup((const xmlChar *)(s), (n))
  115. /************************************************************************
  116. * *
  117. * RFC 3986 parser *
  118. * *
  119. ************************************************************************/
  120. #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9'))
  121. #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \
  122. ((*(p) >= 'A') && (*(p) <= 'Z')))
  123. #define ISA_HEXDIG(p) \
  124. (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \
  125. ((*(p) >= 'A') && (*(p) <= 'F')))
  126. /*
  127. * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  128. * / "*" / "+" / "," / ";" / "="
  129. */
  130. #define ISA_SUB_DELIM(p) \
  131. (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \
  132. ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \
  133. ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \
  134. ((*(p) == '=')) || ((*(p) == '\'')))
  135. /*
  136. * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
  137. */
  138. #define ISA_GEN_DELIM(p) \
  139. (((*(p) == ':')) || ((*(p) == '/')) || ((*(p) == '?')) || \
  140. ((*(p) == '#')) || ((*(p) == '[')) || ((*(p) == ']')) || \
  141. ((*(p) == '@')))
  142. /*
  143. * reserved = gen-delims / sub-delims
  144. */
  145. #define ISA_RESERVED(p) (ISA_GEN_DELIM(p) || (ISA_SUB_DELIM(p)))
  146. /*
  147. * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  148. */
  149. #define ISA_UNRESERVED(p) \
  150. ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \
  151. ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
  152. /*
  153. * pct-encoded = "%" HEXDIG HEXDIG
  154. */
  155. #define ISA_PCT_ENCODED(p) \
  156. ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2)))
  157. /*
  158. * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  159. */
  160. #define ISA_PCHAR(p) \
  161. (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \
  162. ((*(p) == ':')) || ((*(p) == '@')))
  163. /**
  164. * xmlParse3986Scheme:
  165. * @uri: pointer to an URI structure
  166. * @str: pointer to the string to analyze
  167. *
  168. * Parse an URI scheme
  169. *
  170. * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  171. *
  172. * Returns 0 or the error code
  173. */
  174. static int
  175. xmlParse3986Scheme(xmlURIPtr uri, const char **str) {
  176. const char *cur;
  177. if (str == NULL)
  178. return(-1);
  179. cur = *str;
  180. if (!ISA_ALPHA(cur))
  181. return(2);
  182. cur++;
  183. while (ISA_ALPHA(cur) || ISA_DIGIT(cur) ||
  184. (*cur == '+') || (*cur == '-') || (*cur == '.')) cur++;
  185. if (uri != NULL) {
  186. if (uri->scheme != NULL) xmlFree(uri->scheme);
  187. uri->scheme = STRNDUP(*str, cur - *str);
  188. }
  189. *str = cur;
  190. return(0);
  191. }
  192. /**
  193. * xmlParse3986Fragment:
  194. * @uri: pointer to an URI structure
  195. * @str: pointer to the string to analyze
  196. *
  197. * Parse the query part of an URI
  198. *
  199. * fragment = *( pchar / "/" / "?" )
  200. * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']'
  201. * in the fragment identifier but this is used very broadly for
  202. * xpointer scheme selection, so we are allowing it here to not break
  203. * for example all the DocBook processing chains.
  204. *
  205. * Returns 0 or the error code
  206. */
  207. static int
  208. xmlParse3986Fragment(xmlURIPtr uri, const char **str)
  209. {
  210. const char *cur;
  211. if (str == NULL)
  212. return (-1);
  213. cur = *str;
  214. while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') ||
  215. (*cur == '[') || (*cur == ']') ||
  216. ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))
  217. NEXT(cur);
  218. if (uri != NULL) {
  219. if (uri->fragment != NULL)
  220. xmlFree(uri->fragment);
  221. if (uri->cleanup & 2)
  222. uri->fragment = STRNDUP(*str, cur - *str);
  223. else
  224. uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL);
  225. }
  226. *str = cur;
  227. return (0);
  228. }
  229. /**
  230. * xmlParse3986Query:
  231. * @uri: pointer to an URI structure
  232. * @str: pointer to the string to analyze
  233. *
  234. * Parse the query part of an URI
  235. *
  236. * query = *uric
  237. *
  238. * Returns 0 or the error code
  239. */
  240. static int
  241. xmlParse3986Query(xmlURIPtr uri, const char **str)
  242. {
  243. const char *cur;
  244. if (str == NULL)
  245. return (-1);
  246. cur = *str;
  247. while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') ||
  248. ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))
  249. NEXT(cur);
  250. if (uri != NULL) {
  251. if (uri->query != NULL)
  252. xmlFree(uri->query);
  253. if (uri->cleanup & 2)
  254. uri->query = STRNDUP(*str, cur - *str);
  255. else
  256. uri->query = xmlURIUnescapeString(*str, cur - *str, NULL);
  257. /* Save the raw bytes of the query as well.
  258. * See: http://mail.gnome.org/archives/xml/2007-April/thread.html#00114
  259. */
  260. if (uri->query_raw != NULL)
  261. xmlFree (uri->query_raw);
  262. uri->query_raw = STRNDUP (*str, cur - *str);
  263. }
  264. *str = cur;
  265. return (0);
  266. }
  267. /**
  268. * xmlParse3986Port:
  269. * @uri: pointer to an URI structure
  270. * @str: the string to analyze
  271. *
  272. * Parse a port part and fills in the appropriate fields
  273. * of the @uri structure
  274. *
  275. * port = *DIGIT
  276. *
  277. * Returns 0 or the error code
  278. */
  279. static int
  280. xmlParse3986Port(xmlURIPtr uri, const char **str)
  281. {
  282. const char *cur = *str;
  283. int port = 0;
  284. if (ISA_DIGIT(cur)) {
  285. while (ISA_DIGIT(cur)) {
  286. int digit = *cur - '0';
  287. if (port > INT_MAX / 10)
  288. return(1);
  289. port *= 10;
  290. if (port > INT_MAX - digit)
  291. return(1);
  292. port += digit;
  293. cur++;
  294. }
  295. if (uri != NULL)
  296. uri->port = port;
  297. *str = cur;
  298. return(0);
  299. }
  300. return(1);
  301. }
  302. /**
  303. * xmlParse3986Userinfo:
  304. * @uri: pointer to an URI structure
  305. * @str: the string to analyze
  306. *
  307. * Parse an user information part and fills in the appropriate fields
  308. * of the @uri structure
  309. *
  310. * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
  311. *
  312. * Returns 0 or the error code
  313. */
  314. static int
  315. xmlParse3986Userinfo(xmlURIPtr uri, const char **str)
  316. {
  317. const char *cur;
  318. cur = *str;
  319. while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) ||
  320. ISA_SUB_DELIM(cur) || (*cur == ':'))
  321. NEXT(cur);
  322. if (*cur == '@') {
  323. if (uri != NULL) {
  324. if (uri->user != NULL) xmlFree(uri->user);
  325. if (uri->cleanup & 2)
  326. uri->user = STRNDUP(*str, cur - *str);
  327. else
  328. uri->user = xmlURIUnescapeString(*str, cur - *str, NULL);
  329. }
  330. *str = cur;
  331. return(0);
  332. }
  333. return(1);
  334. }
  335. /**
  336. * xmlParse3986DecOctet:
  337. * @str: the string to analyze
  338. *
  339. * dec-octet = DIGIT ; 0-9
  340. * / %x31-39 DIGIT ; 10-99
  341. * / "1" 2DIGIT ; 100-199
  342. * / "2" %x30-34 DIGIT ; 200-249
  343. * / "25" %x30-35 ; 250-255
  344. *
  345. * Skip a dec-octet.
  346. *
  347. * Returns 0 if found and skipped, 1 otherwise
  348. */
  349. static int
  350. xmlParse3986DecOctet(const char **str) {
  351. const char *cur = *str;
  352. if (!(ISA_DIGIT(cur)))
  353. return(1);
  354. if (!ISA_DIGIT(cur+1))
  355. cur++;
  356. else if ((*cur != '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur+2)))
  357. cur += 2;
  358. else if ((*cur == '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur + 2)))
  359. cur += 3;
  360. else if ((*cur == '2') && (*(cur + 1) >= '0') &&
  361. (*(cur + 1) <= '4') && (ISA_DIGIT(cur + 2)))
  362. cur += 3;
  363. else if ((*cur == '2') && (*(cur + 1) == '5') &&
  364. (*(cur + 2) >= '0') && (*(cur + 1) <= '5'))
  365. cur += 3;
  366. else
  367. return(1);
  368. *str = cur;
  369. return(0);
  370. }
  371. /**
  372. * xmlParse3986Host:
  373. * @uri: pointer to an URI structure
  374. * @str: the string to analyze
  375. *
  376. * Parse an host part and fills in the appropriate fields
  377. * of the @uri structure
  378. *
  379. * host = IP-literal / IPv4address / reg-name
  380. * IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  381. * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
  382. * reg-name = *( unreserved / pct-encoded / sub-delims )
  383. *
  384. * Returns 0 or the error code
  385. */
  386. static int
  387. xmlParse3986Host(xmlURIPtr uri, const char **str)
  388. {
  389. const char *cur = *str;
  390. const char *host;
  391. host = cur;
  392. /*
  393. * IPv6 and future addressing scheme are enclosed between brackets
  394. */
  395. if (*cur == '[') {
  396. cur++;
  397. while ((*cur != ']') && (*cur != 0))
  398. cur++;
  399. if (*cur != ']')
  400. return(1);
  401. cur++;
  402. goto found;
  403. }
  404. /*
  405. * try to parse an IPv4
  406. */
  407. if (ISA_DIGIT(cur)) {
  408. if (xmlParse3986DecOctet(&cur) != 0)
  409. goto not_ipv4;
  410. if (*cur != '.')
  411. goto not_ipv4;
  412. cur++;
  413. if (xmlParse3986DecOctet(&cur) != 0)
  414. goto not_ipv4;
  415. if (*cur != '.')
  416. goto not_ipv4;
  417. if (xmlParse3986DecOctet(&cur) != 0)
  418. goto not_ipv4;
  419. if (*cur != '.')
  420. goto not_ipv4;
  421. if (xmlParse3986DecOctet(&cur) != 0)
  422. goto not_ipv4;
  423. goto found;
  424. not_ipv4:
  425. cur = *str;
  426. }
  427. /*
  428. * then this should be a hostname which can be empty
  429. */
  430. while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur))
  431. NEXT(cur);
  432. found:
  433. if (uri != NULL) {
  434. if (uri->authority != NULL) xmlFree(uri->authority);
  435. uri->authority = NULL;
  436. if (uri->server != NULL) xmlFree(uri->server);
  437. if (cur != host) {
  438. if (uri->cleanup & 2)
  439. uri->server = STRNDUP(host, cur - host);
  440. else
  441. uri->server = xmlURIUnescapeString(host, cur - host, NULL);
  442. } else
  443. uri->server = NULL;
  444. }
  445. *str = cur;
  446. return(0);
  447. }
  448. /**
  449. * xmlParse3986Authority:
  450. * @uri: pointer to an URI structure
  451. * @str: the string to analyze
  452. *
  453. * Parse an authority part and fills in the appropriate fields
  454. * of the @uri structure
  455. *
  456. * authority = [ userinfo "@" ] host [ ":" port ]
  457. *
  458. * Returns 0 or the error code
  459. */
  460. static int
  461. xmlParse3986Authority(xmlURIPtr uri, const char **str)
  462. {
  463. const char *cur;
  464. int ret;
  465. cur = *str;
  466. /*
  467. * try to parse an userinfo and check for the trailing @
  468. */
  469. ret = xmlParse3986Userinfo(uri, &cur);
  470. if ((ret != 0) || (*cur != '@'))
  471. cur = *str;
  472. else
  473. cur++;
  474. ret = xmlParse3986Host(uri, &cur);
  475. if (ret != 0) return(ret);
  476. if (*cur == ':') {
  477. cur++;
  478. ret = xmlParse3986Port(uri, &cur);
  479. if (ret != 0) return(ret);
  480. }
  481. *str = cur;
  482. return(0);
  483. }
  484. /**
  485. * xmlParse3986Segment:
  486. * @str: the string to analyze
  487. * @forbid: an optional forbidden character
  488. * @empty: allow an empty segment
  489. *
  490. * Parse a segment and fills in the appropriate fields
  491. * of the @uri structure
  492. *
  493. * segment = *pchar
  494. * segment-nz = 1*pchar
  495. * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
  496. * ; non-zero-length segment without any colon ":"
  497. *
  498. * Returns 0 or the error code
  499. */
  500. static int
  501. xmlParse3986Segment(const char **str, char forbid, int empty)
  502. {
  503. const char *cur;
  504. cur = *str;
  505. if (!ISA_PCHAR(cur)) {
  506. if (empty)
  507. return(0);
  508. return(1);
  509. }
  510. while (ISA_PCHAR(cur) && (*cur != forbid))
  511. NEXT(cur);
  512. *str = cur;
  513. return (0);
  514. }
  515. /**
  516. * xmlParse3986PathAbEmpty:
  517. * @uri: pointer to an URI structure
  518. * @str: the string to analyze
  519. *
  520. * Parse an path absolute or empty and fills in the appropriate fields
  521. * of the @uri structure
  522. *
  523. * path-abempty = *( "/" segment )
  524. *
  525. * Returns 0 or the error code
  526. */
  527. static int
  528. xmlParse3986PathAbEmpty(xmlURIPtr uri, const char **str)
  529. {
  530. const char *cur;
  531. int ret;
  532. cur = *str;
  533. while (*cur == '/') {
  534. cur++;
  535. ret = xmlParse3986Segment(&cur, 0, 1);
  536. if (ret != 0) return(ret);
  537. }
  538. if (uri != NULL) {
  539. if (uri->path != NULL) xmlFree(uri->path);
  540. if (*str != cur) {
  541. if (uri->cleanup & 2)
  542. uri->path = STRNDUP(*str, cur - *str);
  543. else
  544. uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
  545. } else {
  546. uri->path = NULL;
  547. }
  548. }
  549. *str = cur;
  550. return (0);
  551. }
  552. /**
  553. * xmlParse3986PathAbsolute:
  554. * @uri: pointer to an URI structure
  555. * @str: the string to analyze
  556. *
  557. * Parse an path absolute and fills in the appropriate fields
  558. * of the @uri structure
  559. *
  560. * path-absolute = "/" [ segment-nz *( "/" segment ) ]
  561. *
  562. * Returns 0 or the error code
  563. */
  564. static int
  565. xmlParse3986PathAbsolute(xmlURIPtr uri, const char **str)
  566. {
  567. const char *cur;
  568. int ret;
  569. cur = *str;
  570. if (*cur != '/')
  571. return(1);
  572. cur++;
  573. ret = xmlParse3986Segment(&cur, 0, 0);
  574. if (ret == 0) {
  575. while (*cur == '/') {
  576. cur++;
  577. ret = xmlParse3986Segment(&cur, 0, 1);
  578. if (ret != 0) return(ret);
  579. }
  580. }
  581. if (uri != NULL) {
  582. if (uri->path != NULL) xmlFree(uri->path);
  583. if (cur != *str) {
  584. if (uri->cleanup & 2)
  585. uri->path = STRNDUP(*str, cur - *str);
  586. else
  587. uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
  588. } else {
  589. uri->path = NULL;
  590. }
  591. }
  592. *str = cur;
  593. return (0);
  594. }
  595. /**
  596. * xmlParse3986PathRootless:
  597. * @uri: pointer to an URI structure
  598. * @str: the string to analyze
  599. *
  600. * Parse an path without root and fills in the appropriate fields
  601. * of the @uri structure
  602. *
  603. * path-rootless = segment-nz *( "/" segment )
  604. *
  605. * Returns 0 or the error code
  606. */
  607. static int
  608. xmlParse3986PathRootless(xmlURIPtr uri, const char **str)
  609. {
  610. const char *cur;
  611. int ret;
  612. cur = *str;
  613. ret = xmlParse3986Segment(&cur, 0, 0);
  614. if (ret != 0) return(ret);
  615. while (*cur == '/') {
  616. cur++;
  617. ret = xmlParse3986Segment(&cur, 0, 1);
  618. if (ret != 0) return(ret);
  619. }
  620. if (uri != NULL) {
  621. if (uri->path != NULL) xmlFree(uri->path);
  622. if (cur != *str) {
  623. if (uri->cleanup & 2)
  624. uri->path = STRNDUP(*str, cur - *str);
  625. else
  626. uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
  627. } else {
  628. uri->path = NULL;
  629. }
  630. }
  631. *str = cur;
  632. return (0);
  633. }
  634. /**
  635. * xmlParse3986PathNoScheme:
  636. * @uri: pointer to an URI structure
  637. * @str: the string to analyze
  638. *
  639. * Parse an path which is not a scheme and fills in the appropriate fields
  640. * of the @uri structure
  641. *
  642. * path-noscheme = segment-nz-nc *( "/" segment )
  643. *
  644. * Returns 0 or the error code
  645. */
  646. static int
  647. xmlParse3986PathNoScheme(xmlURIPtr uri, const char **str)
  648. {
  649. const char *cur;
  650. int ret;
  651. cur = *str;
  652. ret = xmlParse3986Segment(&cur, ':', 0);
  653. if (ret != 0) return(ret);
  654. while (*cur == '/') {
  655. cur++;
  656. ret = xmlParse3986Segment(&cur, 0, 1);
  657. if (ret != 0) return(ret);
  658. }
  659. if (uri != NULL) {
  660. if (uri->path != NULL) xmlFree(uri->path);
  661. if (cur != *str) {
  662. if (uri->cleanup & 2)
  663. uri->path = STRNDUP(*str, cur - *str);
  664. else
  665. uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
  666. } else {
  667. uri->path = NULL;
  668. }
  669. }
  670. *str = cur;
  671. return (0);
  672. }
  673. /**
  674. * xmlParse3986HierPart:
  675. * @uri: pointer to an URI structure
  676. * @str: the string to analyze
  677. *
  678. * Parse an hierarchical part and fills in the appropriate fields
  679. * of the @uri structure
  680. *
  681. * hier-part = "//" authority path-abempty
  682. * / path-absolute
  683. * / path-rootless
  684. * / path-empty
  685. *
  686. * Returns 0 or the error code
  687. */
  688. static int
  689. xmlParse3986HierPart(xmlURIPtr uri, const char **str)
  690. {
  691. const char *cur;
  692. int ret;
  693. cur = *str;
  694. if ((*cur == '/') && (*(cur + 1) == '/')) {
  695. cur += 2;
  696. ret = xmlParse3986Authority(uri, &cur);
  697. if (ret != 0) return(ret);
  698. if (uri->server == NULL)
  699. uri->port = -1;
  700. ret = xmlParse3986PathAbEmpty(uri, &cur);
  701. if (ret != 0) return(ret);
  702. *str = cur;
  703. return(0);
  704. } else if (*cur == '/') {
  705. ret = xmlParse3986PathAbsolute(uri, &cur);
  706. if (ret != 0) return(ret);
  707. } else if (ISA_PCHAR(cur)) {
  708. ret = xmlParse3986PathRootless(uri, &cur);
  709. if (ret != 0) return(ret);
  710. } else {
  711. /* path-empty is effectively empty */
  712. if (uri != NULL) {
  713. if (uri->path != NULL) xmlFree(uri->path);
  714. uri->path = NULL;
  715. }
  716. }
  717. *str = cur;
  718. return (0);
  719. }
  720. /**
  721. * xmlParse3986RelativeRef:
  722. * @uri: pointer to an URI structure
  723. * @str: the string to analyze
  724. *
  725. * Parse an URI string and fills in the appropriate fields
  726. * of the @uri structure
  727. *
  728. * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
  729. * relative-part = "//" authority path-abempty
  730. * / path-absolute
  731. * / path-noscheme
  732. * / path-empty
  733. *
  734. * Returns 0 or the error code
  735. */
  736. static int
  737. xmlParse3986RelativeRef(xmlURIPtr uri, const char *str) {
  738. int ret;
  739. if ((*str == '/') && (*(str + 1) == '/')) {
  740. str += 2;
  741. ret = xmlParse3986Authority(uri, &str);
  742. if (ret != 0) return(ret);
  743. ret = xmlParse3986PathAbEmpty(uri, &str);
  744. if (ret != 0) return(ret);
  745. } else if (*str == '/') {
  746. ret = xmlParse3986PathAbsolute(uri, &str);
  747. if (ret != 0) return(ret);
  748. } else if (ISA_PCHAR(str)) {
  749. ret = xmlParse3986PathNoScheme(uri, &str);
  750. if (ret != 0) return(ret);
  751. } else {
  752. /* path-empty is effectively empty */
  753. if (uri != NULL) {
  754. if (uri->path != NULL) xmlFree(uri->path);
  755. uri->path = NULL;
  756. }
  757. }
  758. if (*str == '?') {
  759. str++;
  760. ret = xmlParse3986Query(uri, &str);
  761. if (ret != 0) return(ret);
  762. }
  763. if (*str == '#') {
  764. str++;
  765. ret = xmlParse3986Fragment(uri, &str);
  766. if (ret != 0) return(ret);
  767. }
  768. if (*str != 0) {
  769. xmlCleanURI(uri);
  770. return(1);
  771. }
  772. return(0);
  773. }
  774. /**
  775. * xmlParse3986URI:
  776. * @uri: pointer to an URI structure
  777. * @str: the string to analyze
  778. *
  779. * Parse an URI string and fills in the appropriate fields
  780. * of the @uri structure
  781. *
  782. * scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  783. *
  784. * Returns 0 or the error code
  785. */
  786. static int
  787. xmlParse3986URI(xmlURIPtr uri, const char *str) {
  788. int ret;
  789. ret = xmlParse3986Scheme(uri, &str);
  790. if (ret != 0) return(ret);
  791. if (*str != ':') {
  792. return(1);
  793. }
  794. str++;
  795. ret = xmlParse3986HierPart(uri, &str);
  796. if (ret != 0) return(ret);
  797. if (*str == '?') {
  798. str++;
  799. ret = xmlParse3986Query(uri, &str);
  800. if (ret != 0) return(ret);
  801. }
  802. if (*str == '#') {
  803. str++;
  804. ret = xmlParse3986Fragment(uri, &str);
  805. if (ret != 0) return(ret);
  806. }
  807. if (*str != 0) {
  808. xmlCleanURI(uri);
  809. return(1);
  810. }
  811. return(0);
  812. }
  813. /**
  814. * xmlParse3986URIReference:
  815. * @uri: pointer to an URI structure
  816. * @str: the string to analyze
  817. *
  818. * Parse an URI reference string and fills in the appropriate fields
  819. * of the @uri structure
  820. *
  821. * URI-reference = URI / relative-ref
  822. *
  823. * Returns 0 or the error code
  824. */
  825. static int
  826. xmlParse3986URIReference(xmlURIPtr uri, const char *str) {
  827. int ret;
  828. if (str == NULL)
  829. return(-1);
  830. xmlCleanURI(uri);
  831. /*
  832. * Try first to parse absolute refs, then fallback to relative if
  833. * it fails.
  834. */
  835. ret = xmlParse3986URI(uri, str);
  836. if (ret != 0) {
  837. xmlCleanURI(uri);
  838. ret = xmlParse3986RelativeRef(uri, str);
  839. if (ret != 0) {
  840. xmlCleanURI(uri);
  841. return(ret);
  842. }
  843. }
  844. return(0);
  845. }
  846. /**
  847. * xmlParseURI:
  848. * @str: the URI string to analyze
  849. *
  850. * Parse an URI based on RFC 3986
  851. *
  852. * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
  853. *
  854. * Returns a newly built xmlURIPtr or NULL in case of error
  855. */
  856. xmlURIPtr
  857. xmlParseURI(const char *str) {
  858. xmlURIPtr uri;
  859. int ret;
  860. if (str == NULL)
  861. return(NULL);
  862. uri = xmlCreateURI();
  863. if (uri != NULL) {
  864. ret = xmlParse3986URIReference(uri, str);
  865. if (ret) {
  866. xmlFreeURI(uri);
  867. return(NULL);
  868. }
  869. }
  870. return(uri);
  871. }
  872. /**
  873. * xmlParseURIReference:
  874. * @uri: pointer to an URI structure
  875. * @str: the string to analyze
  876. *
  877. * Parse an URI reference string based on RFC 3986 and fills in the
  878. * appropriate fields of the @uri structure
  879. *
  880. * URI-reference = URI / relative-ref
  881. *
  882. * Returns 0 or the error code
  883. */
  884. int
  885. xmlParseURIReference(xmlURIPtr uri, const char *str) {
  886. return(xmlParse3986URIReference(uri, str));
  887. }
  888. /**
  889. * xmlParseURIRaw:
  890. * @str: the URI string to analyze
  891. * @raw: if 1 unescaping of URI pieces are disabled
  892. *
  893. * Parse an URI but allows to keep intact the original fragments.
  894. *
  895. * URI-reference = URI / relative-ref
  896. *
  897. * Returns a newly built xmlURIPtr or NULL in case of error
  898. */
  899. xmlURIPtr
  900. xmlParseURIRaw(const char *str, int raw) {
  901. xmlURIPtr uri;
  902. int ret;
  903. if (str == NULL)
  904. return(NULL);
  905. uri = xmlCreateURI();
  906. if (uri != NULL) {
  907. if (raw) {
  908. uri->cleanup |= 2;
  909. }
  910. ret = xmlParseURIReference(uri, str);
  911. if (ret) {
  912. xmlFreeURI(uri);
  913. return(NULL);
  914. }
  915. }
  916. return(uri);
  917. }
  918. /************************************************************************
  919. * *
  920. * Generic URI structure functions *
  921. * *
  922. ************************************************************************/
  923. /**
  924. * xmlCreateURI:
  925. *
  926. * Simply creates an empty xmlURI
  927. *
  928. * Returns the new structure or NULL in case of error
  929. */
  930. xmlURIPtr
  931. xmlCreateURI(void) {
  932. xmlURIPtr ret;
  933. ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI));
  934. if (ret == NULL) {
  935. xmlURIErrMemory("creating URI structure\n");
  936. return(NULL);
  937. }
  938. memset(ret, 0, sizeof(xmlURI));
  939. return(ret);
  940. }
  941. /**
  942. * xmlSaveUriRealloc:
  943. *
  944. * Function to handle properly a reallocation when saving an URI
  945. * Also imposes some limit on the length of an URI string output
  946. */
  947. static xmlChar *
  948. xmlSaveUriRealloc(xmlChar *ret, int *max) {
  949. xmlChar *temp;
  950. int tmp;
  951. if (*max > MAX_URI_LENGTH) {
  952. xmlURIErrMemory("reaching arbitrary MAX_URI_LENGTH limit\n");
  953. return(NULL);
  954. }
  955. tmp = *max * 2;
  956. temp = (xmlChar *) xmlRealloc(ret, (tmp + 1));
  957. if (temp == NULL) {
  958. xmlURIErrMemory("saving URI\n");
  959. return(NULL);
  960. }
  961. *max = tmp;
  962. return(temp);
  963. }
  964. /**
  965. * xmlSaveUri:
  966. * @uri: pointer to an xmlURI
  967. *
  968. * Save the URI as an escaped string
  969. *
  970. * Returns a new string (to be deallocated by caller)
  971. */
  972. xmlChar *
  973. xmlSaveUri(xmlURIPtr uri) {
  974. xmlChar *ret = NULL;
  975. xmlChar *temp;
  976. const char *p;
  977. int len;
  978. int max;
  979. if (uri == NULL) return(NULL);
  980. max = 80;
  981. ret = (xmlChar *) xmlMallocAtomic((max + 1) * sizeof(xmlChar));
  982. if (ret == NULL) {
  983. xmlURIErrMemory("saving URI\n");
  984. return(NULL);
  985. }
  986. len = 0;
  987. if (uri->scheme != NULL) {
  988. p = uri->scheme;
  989. while (*p != 0) {
  990. if (len >= max) {
  991. temp = xmlSaveUriRealloc(ret, &max);
  992. if (temp == NULL) goto mem_error;
  993. ret = temp;
  994. }
  995. ret[len++] = *p++;
  996. }
  997. if (len >= max) {
  998. temp = xmlSaveUriRealloc(ret, &max);
  999. if (temp == NULL) goto mem_error;
  1000. ret = temp;
  1001. }
  1002. ret[len++] = ':';
  1003. }
  1004. if (uri->opaque != NULL) {
  1005. p = uri->opaque;
  1006. while (*p != 0) {
  1007. if (len + 3 >= max) {
  1008. temp = xmlSaveUriRealloc(ret, &max);
  1009. if (temp == NULL) goto mem_error;
  1010. ret = temp;
  1011. }
  1012. if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p)))
  1013. ret[len++] = *p++;
  1014. else {
  1015. int val = *(unsigned char *)p++;
  1016. int hi = val / 0x10, lo = val % 0x10;
  1017. ret[len++] = '%';
  1018. ret[len++] = hi + (hi > 9? 'A'-10 : '0');
  1019. ret[len++] = lo + (lo > 9? 'A'-10 : '0');
  1020. }
  1021. }
  1022. } else {
  1023. if ((uri->server != NULL) || (uri->port == -1)) {
  1024. if (len + 3 >= max) {
  1025. temp = xmlSaveUriRealloc(ret, &max);
  1026. if (temp == NULL) goto mem_error;
  1027. ret = temp;
  1028. }
  1029. ret[len++] = '/';
  1030. ret[len++] = '/';
  1031. if (uri->user != NULL) {
  1032. p = uri->user;
  1033. while (*p != 0) {
  1034. if (len + 3 >= max) {
  1035. temp = xmlSaveUriRealloc(ret, &max);
  1036. if (temp == NULL) goto mem_error;
  1037. ret = temp;
  1038. }
  1039. if ((IS_UNRESERVED(*(p))) ||
  1040. ((*(p) == ';')) || ((*(p) == ':')) ||
  1041. ((*(p) == '&')) || ((*(p) == '=')) ||
  1042. ((*(p) == '+')) || ((*(p) == '$')) ||
  1043. ((*(p) == ',')))
  1044. ret[len++] = *p++;
  1045. else {
  1046. int val = *(unsigned char *)p++;
  1047. int hi = val / 0x10, lo = val % 0x10;
  1048. ret[len++] = '%';
  1049. ret[len++] = hi + (hi > 9? 'A'-10 : '0');
  1050. ret[len++] = lo + (lo > 9? 'A'-10 : '0');
  1051. }
  1052. }
  1053. if (len + 3 >= max) {
  1054. temp = xmlSaveUriRealloc(ret, &max);
  1055. if (temp == NULL) goto mem_error;
  1056. ret = temp;
  1057. }
  1058. ret[len++] = '@';
  1059. }
  1060. if (uri->server != NULL) {
  1061. p = uri->server;
  1062. while (*p != 0) {
  1063. if (len >= max) {
  1064. temp = xmlSaveUriRealloc(ret, &max);
  1065. if (temp == NULL) goto mem_error;
  1066. ret = temp;
  1067. }
  1068. ret[len++] = *p++;
  1069. }
  1070. if (uri->port > 0) {
  1071. if (len + 10 >= max) {
  1072. temp = xmlSaveUriRealloc(ret, &max);
  1073. if (temp == NULL) goto mem_error;
  1074. ret = temp;
  1075. }
  1076. len += snprintf((char *) &ret[len], max - len, ":%d", uri->port);
  1077. }
  1078. }
  1079. } else if (uri->authority != NULL) {
  1080. if (len + 3 >= max) {
  1081. temp = xmlSaveUriRealloc(ret, &max);
  1082. if (temp == NULL) goto mem_error;
  1083. ret = temp;
  1084. }
  1085. ret[len++] = '/';
  1086. ret[len++] = '/';
  1087. p = uri->authority;
  1088. while (*p != 0) {
  1089. if (len + 3 >= max) {
  1090. temp = xmlSaveUriRealloc(ret, &max);
  1091. if (temp == NULL) goto mem_error;
  1092. ret = temp;
  1093. }
  1094. if ((IS_UNRESERVED(*(p))) ||
  1095. ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) ||
  1096. ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) ||
  1097. ((*(p) == '=')) || ((*(p) == '+')))
  1098. ret[len++] = *p++;
  1099. else {
  1100. int val = *(unsigned char *)p++;
  1101. int hi = val / 0x10, lo = val % 0x10;
  1102. ret[len++] = '%';
  1103. ret[len++] = hi + (hi > 9? 'A'-10 : '0');
  1104. ret[len++] = lo + (lo > 9? 'A'-10 : '0');
  1105. }
  1106. }
  1107. } else if (uri->scheme != NULL) {
  1108. if (len + 3 >= max) {
  1109. temp = xmlSaveUriRealloc(ret, &max);
  1110. if (temp == NULL) goto mem_error;
  1111. ret = temp;
  1112. }
  1113. }
  1114. if (uri->path != NULL) {
  1115. p = uri->path;
  1116. /*
  1117. * the colon in file:///d: should not be escaped or
  1118. * Windows accesses fail later.
  1119. */
  1120. if ((uri->scheme != NULL) &&
  1121. (p[0] == '/') &&
  1122. (((p[1] >= 'a') && (p[1] <= 'z')) ||
  1123. ((p[1] >= 'A') && (p[1] <= 'Z'))) &&
  1124. (p[2] == ':') &&
  1125. (xmlStrEqual(BAD_CAST uri->scheme, BAD_CAST "file"))) {
  1126. if (len + 3 >= max) {
  1127. temp = xmlSaveUriRealloc(ret, &max);
  1128. if (temp == NULL) goto mem_error;
  1129. ret = temp;
  1130. }
  1131. ret[len++] = *p++;
  1132. ret[len++] = *p++;
  1133. ret[len++] = *p++;
  1134. }
  1135. while (*p != 0) {
  1136. if (len + 3 >= max) {
  1137. temp = xmlSaveUriRealloc(ret, &max);
  1138. if (temp == NULL) goto mem_error;
  1139. ret = temp;
  1140. }
  1141. if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) ||
  1142. ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) ||
  1143. ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||
  1144. ((*(p) == ',')))
  1145. ret[len++] = *p++;
  1146. else {
  1147. int val = *(unsigned char *)p++;
  1148. int hi = val / 0x10, lo = val % 0x10;
  1149. ret[len++] = '%';
  1150. ret[len++] = hi + (hi > 9? 'A'-10 : '0');
  1151. ret[len++] = lo + (lo > 9? 'A'-10 : '0');
  1152. }
  1153. }
  1154. }
  1155. if (uri->query_raw != NULL) {
  1156. if (len + 1 >= max) {
  1157. temp = xmlSaveUriRealloc(ret, &max);
  1158. if (temp == NULL) goto mem_error;
  1159. ret = temp;
  1160. }
  1161. ret[len++] = '?';
  1162. p = uri->query_raw;
  1163. while (*p != 0) {
  1164. if (len + 1 >= max) {
  1165. temp = xmlSaveUriRealloc(ret, &max);
  1166. if (temp == NULL) goto mem_error;
  1167. ret = temp;
  1168. }
  1169. ret[len++] = *p++;
  1170. }
  1171. } else if (uri->query != NULL) {
  1172. if (len + 3 >= max) {
  1173. temp = xmlSaveUriRealloc(ret, &max);
  1174. if (temp == NULL) goto mem_error;
  1175. ret = temp;
  1176. }
  1177. ret[len++] = '?';
  1178. p = uri->query;
  1179. while (*p != 0) {
  1180. if (len + 3 >= max) {
  1181. temp = xmlSaveUriRealloc(ret, &max);
  1182. if (temp == NULL) goto mem_error;
  1183. ret = temp;
  1184. }
  1185. if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
  1186. ret[len++] = *p++;
  1187. else {
  1188. int val = *(unsigned char *)p++;
  1189. int hi = val / 0x10, lo = val % 0x10;
  1190. ret[len++] = '%';
  1191. ret[len++] = hi + (hi > 9? 'A'-10 : '0');
  1192. ret[len++] = lo + (lo > 9? 'A'-10 : '0');
  1193. }
  1194. }
  1195. }
  1196. }
  1197. if (uri->fragment != NULL) {
  1198. if (len + 3 >= max) {
  1199. temp = xmlSaveUriRealloc(ret, &max);
  1200. if (temp == NULL) goto mem_error;
  1201. ret = temp;
  1202. }
  1203. ret[len++] = '#';
  1204. p = uri->fragment;
  1205. while (*p != 0) {
  1206. if (len + 3 >= max) {
  1207. temp = xmlSaveUriRealloc(ret, &max);
  1208. if (temp == NULL) goto mem_error;
  1209. ret = temp;
  1210. }
  1211. if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
  1212. ret[len++] = *p++;
  1213. else {
  1214. int val = *(unsigned char *)p++;
  1215. int hi = val / 0x10, lo = val % 0x10;
  1216. ret[len++] = '%';
  1217. ret[len++] = hi + (hi > 9? 'A'-10 : '0');
  1218. ret[len++] = lo + (lo > 9? 'A'-10 : '0');
  1219. }
  1220. }
  1221. }
  1222. if (len >= max) {
  1223. temp = xmlSaveUriRealloc(ret, &max);
  1224. if (temp == NULL) goto mem_error;
  1225. ret = temp;
  1226. }
  1227. ret[len] = 0;
  1228. return(ret);
  1229. mem_error:
  1230. xmlFree(ret);
  1231. return(NULL);
  1232. }
  1233. /**
  1234. * xmlPrintURI:
  1235. * @stream: a FILE* for the output
  1236. * @uri: pointer to an xmlURI
  1237. *
  1238. * Prints the URI in the stream @stream.
  1239. */
  1240. void
  1241. xmlPrintURI(FILE *stream, xmlURIPtr uri) {
  1242. xmlChar *out;
  1243. out = xmlSaveUri(uri);
  1244. if (out != NULL) {
  1245. fprintf(stream, "%s", (char *) out);
  1246. xmlFree(out);
  1247. }
  1248. }
  1249. /**
  1250. * xmlCleanURI:
  1251. * @uri: pointer to an xmlURI
  1252. *
  1253. * Make sure the xmlURI struct is free of content
  1254. */
  1255. static void
  1256. xmlCleanURI(xmlURIPtr uri) {
  1257. if (uri == NULL) return;
  1258. if (uri->scheme != NULL) xmlFree(uri->scheme);
  1259. uri->scheme = NULL;
  1260. if (uri->server != NULL) xmlFree(uri->server);
  1261. uri->server = NULL;
  1262. if (uri->user != NULL) xmlFree(uri->user);
  1263. uri->user = NULL;
  1264. if (uri->path != NULL) xmlFree(uri->path);
  1265. uri->path = NULL;
  1266. if (uri->fragment != NULL) xmlFree(uri->fragment);
  1267. uri->fragment = NULL;
  1268. if (uri->opaque != NULL) xmlFree(uri->opaque);
  1269. uri->opaque = NULL;
  1270. if (uri->authority != NULL) xmlFree(uri->authority);
  1271. uri->authority = NULL;
  1272. if (uri->query != NULL) xmlFree(uri->query);
  1273. uri->query = NULL;
  1274. if (uri->query_raw != NULL) xmlFree(uri->query_raw);
  1275. uri->query_raw = NULL;
  1276. }
  1277. /**
  1278. * xmlFreeURI:
  1279. * @uri: pointer to an xmlURI
  1280. *
  1281. * Free up the xmlURI struct
  1282. */
  1283. void
  1284. xmlFreeURI(xmlURIPtr uri) {
  1285. if (uri == NULL) return;
  1286. if (uri->scheme != NULL) xmlFree(uri->scheme);
  1287. if (uri->server != NULL) xmlFree(uri->server);
  1288. if (uri->user != NULL) xmlFree(uri->user);
  1289. if (uri->path != NULL) xmlFree(uri->path);
  1290. if (uri->fragment != NULL) xmlFree(uri->fragment);
  1291. if (uri->opaque != NULL) xmlFree(uri->opaque);
  1292. if (uri->authority != NULL) xmlFree(uri->authority);
  1293. if (uri->query != NULL) xmlFree(uri->query);
  1294. if (uri->query_raw != NULL) xmlFree(uri->query_raw);
  1295. xmlFree(uri);
  1296. }
  1297. /************************************************************************
  1298. * *
  1299. * Helper functions *
  1300. * *
  1301. ************************************************************************/
  1302. /**
  1303. * xmlNormalizeURIPath:
  1304. * @path: pointer to the path string
  1305. *
  1306. * Applies the 5 normalization steps to a path string--that is, RFC 2396
  1307. * Section 5.2, steps 6.c through 6.g.
  1308. *
  1309. * Normalization occurs directly on the string, no new allocation is done
  1310. *
  1311. * Returns 0 or an error code
  1312. */
  1313. int
  1314. xmlNormalizeURIPath(char *path) {
  1315. char *cur, *out;
  1316. if (path == NULL)
  1317. return(-1);
  1318. /* Skip all initial "/" chars. We want to get to the beginning of the
  1319. * first non-empty segment.
  1320. */
  1321. cur = path;
  1322. while (cur[0] == '/')
  1323. ++cur;
  1324. if (cur[0] == '\0')
  1325. return(0);
  1326. /* Keep everything we've seen so far. */
  1327. out = cur;
  1328. /*
  1329. * Analyze each segment in sequence for cases (c) and (d).
  1330. */
  1331. while (cur[0] != '\0') {
  1332. /*
  1333. * c) All occurrences of "./", where "." is a complete path segment,
  1334. * are removed from the buffer string.
  1335. */
  1336. if ((cur[0] == '.') && (cur[1] == '/')) {
  1337. cur += 2;
  1338. /* '//' normalization should be done at this point too */
  1339. while (cur[0] == '/')
  1340. cur++;
  1341. continue;
  1342. }
  1343. /*
  1344. * d) If the buffer string ends with "." as a complete path segment,
  1345. * that "." is removed.
  1346. */
  1347. if ((cur[0] == '.') && (cur[1] == '\0'))
  1348. break;
  1349. /* Otherwise keep the segment. */
  1350. while (cur[0] != '/') {
  1351. if (cur[0] == '\0')
  1352. goto done_cd;
  1353. (out++)[0] = (cur++)[0];
  1354. }
  1355. /* normalize // */
  1356. while ((cur[0] == '/') && (cur[1] == '/'))
  1357. cur++;
  1358. (out++)[0] = (cur++)[0];
  1359. }
  1360. done_cd:
  1361. out[0] = '\0';
  1362. /* Reset to the beginning of the first segment for the next sequence. */
  1363. cur = path;
  1364. while (cur[0] == '/')
  1365. ++cur;
  1366. if (cur[0] == '\0')
  1367. return(0);
  1368. /*
  1369. * Analyze each segment in sequence for cases (e) and (f).
  1370. *
  1371. * e) All occurrences of "<segment>/../", where <segment> is a
  1372. * complete path segment not equal to "..", are removed from the
  1373. * buffer string. Removal of these path segments is performed
  1374. * iteratively, removing the leftmost matching pattern on each
  1375. * iteration, until no matching pattern remains.
  1376. *
  1377. * f) If the buffer string ends with "<segment>/..", where <segment>
  1378. * is a complete path segment not equal to "..", that
  1379. * "<segment>/.." is removed.
  1380. *
  1381. * To satisfy the "iterative" clause in (e), we need to collapse the
  1382. * string every time we find something that needs to be removed. Thus,
  1383. * we don't need to keep two pointers into the string: we only need a
  1384. * "current position" pointer.
  1385. */
  1386. while (1) {
  1387. char *segp, *tmp;
  1388. /* At the beginning of each iteration of this loop, "cur" points to
  1389. * the first character of the segment we want to examine.
  1390. */
  1391. /* Find the end of the current segment. */
  1392. segp = cur;
  1393. while ((segp[0] != '/') && (segp[0] != '\0'))
  1394. ++segp;
  1395. /* If this is the last segment, we're done (we need at least two
  1396. * segments to meet the criteria for the (e) and (f) cases).
  1397. */
  1398. if (segp[0] == '\0')
  1399. break;
  1400. /* If the first segment is "..", or if the next segment _isn't_ "..",
  1401. * keep this segment and try the next one.
  1402. */
  1403. ++segp;
  1404. if (((cur[0] == '.') && (cur[1] == '.') && (segp == cur+3))
  1405. || ((segp[0] != '.') || (segp[1] != '.')
  1406. || ((segp[2] != '/') && (segp[2] != '\0')))) {
  1407. cur = segp;
  1408. continue;
  1409. }
  1410. /* If we get here, remove this segment and the next one and back up
  1411. * to the previous segment (if there is one), to implement the
  1412. * "iteratively" clause. It's pretty much impossible to back up
  1413. * while maintaining two pointers into the buffer, so just compact
  1414. * the whole buffer now.
  1415. */
  1416. /* If this is the end of the buffer, we're done. */
  1417. if (segp[2] == '\0') {
  1418. cur[0] = '\0';
  1419. break;
  1420. }
  1421. /* Valgrind complained, strcpy(cur, segp + 3); */
  1422. /* string will overlap, do not use strcpy */
  1423. tmp = cur;
  1424. segp += 3;
  1425. while ((*tmp++ = *segp++) != 0)
  1426. ;
  1427. /* If there are no previous segments, then keep going from here. */
  1428. segp = cur;
  1429. while ((segp > path) && ((--segp)[0] == '/'))
  1430. ;
  1431. if (segp == path)
  1432. continue;
  1433. /* "segp" is pointing to the end of a previous segment; find it's
  1434. * start. We need to back up to the previous segment and start
  1435. * over with that to handle things like "foo/bar/../..". If we
  1436. * don't do this, then on the first pass we'll remove the "bar/..",
  1437. * but be pointing at the second ".." so we won't realize we can also
  1438. * remove the "foo/..".
  1439. */
  1440. cur = segp;
  1441. while ((cur > path) && (cur[-1] != '/'))
  1442. --cur;
  1443. }
  1444. out[0] = '\0';
  1445. /*
  1446. * g) If the resulting buffer string still begins with one or more
  1447. * complete path segments of "..", then the reference is
  1448. * considered to be in error. Implementations may handle this
  1449. * error by retaining these components in the resolved path (i.e.,
  1450. * treating them as part of the final URI), by removing them from
  1451. * the resolved path (i.e., discarding relative levels above the
  1452. * root), or by avoiding traversal of the reference.
  1453. *
  1454. * We discard them from the final path.
  1455. */
  1456. if (path[0] == '/') {
  1457. cur = path;
  1458. while ((cur[0] == '/') && (cur[1] == '.') && (cur[2] == '.')
  1459. && ((cur[3] == '/') || (cur[3] == '\0')))
  1460. cur += 3;
  1461. if (cur != path) {
  1462. out = path;
  1463. while (cur[0] != '\0')
  1464. (out++)[0] = (cur++)[0];
  1465. out[0] = 0;
  1466. }
  1467. }
  1468. return(0);
  1469. }
  1470. static int is_hex(char c) {
  1471. if (((c >= '0') && (c <= '9')) ||
  1472. ((c >= 'a') && (c <= 'f')) ||
  1473. ((c >= 'A') && (c <= 'F')))
  1474. return(1);
  1475. return(0);
  1476. }
  1477. /**
  1478. * xmlURIUnescapeString:
  1479. * @str: the string to unescape
  1480. * @len: the length in bytes to unescape (or <= 0 to indicate full string)
  1481. * @target: optional destination buffer
  1482. *
  1483. * Unescaping routine, but does not check that the string is an URI. The
  1484. * output is a direct unsigned char translation of %XX values (no encoding)
  1485. * Note that the length of the result can only be smaller or same size as
  1486. * the input string.
  1487. *
  1488. * Returns a copy of the string, but unescaped, will return NULL only in case
  1489. * of error
  1490. */
  1491. char *
  1492. xmlURIUnescapeString(const char *str, int len, char *target) {
  1493. char *ret, *out;
  1494. const char *in;
  1495. if (str == NULL)
  1496. return(NULL);
  1497. if (len <= 0) len = strlen(str);
  1498. if (len < 0) return(NULL);
  1499. if (target == NULL) {
  1500. ret = (char *) xmlMallocAtomic(len + 1);
  1501. if (ret == NULL) {
  1502. xmlURIErrMemory("unescaping URI value\n");
  1503. return(NULL);
  1504. }
  1505. } else
  1506. ret = target;
  1507. in = str;
  1508. out = ret;
  1509. while(len > 0) {
  1510. if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) {
  1511. in++;
  1512. if ((*in >= '0') && (*in <= '9'))
  1513. *out = (*in - '0');
  1514. else if ((*in >= 'a') && (*in <= 'f'))
  1515. *out = (*in - 'a') + 10;
  1516. else if ((*in >= 'A') && (*in <= 'F'))
  1517. *out = (*in - 'A') + 10;
  1518. in++;
  1519. if ((*in >= '0') && (*in <= '9'))
  1520. *out = *out * 16 + (*in - '0');
  1521. else if ((*in >= 'a') && (*in <= 'f'))
  1522. *out = *out * 16 + (*in - 'a') + 10;
  1523. else if ((*in >= 'A') && (*in <= 'F'))
  1524. *out = *out * 16 + (*in - 'A') + 10;
  1525. in++;
  1526. len -= 3;
  1527. out++;
  1528. } else {
  1529. *out++ = *in++;
  1530. len--;
  1531. }
  1532. }
  1533. *out = 0;
  1534. return(ret);
  1535. }
  1536. /**
  1537. * xmlURIEscapeStr:
  1538. * @str: string to escape
  1539. * @list: exception list string of chars not to escape
  1540. *
  1541. * This routine escapes a string to hex, ignoring reserved characters (a-z)
  1542. * and the characters in the exception list.
  1543. *
  1544. * Returns a new escaped string or NULL in case of error.
  1545. */
  1546. xmlChar *
  1547. xmlURIEscapeStr(const xmlChar *str, const xmlChar *list) {
  1548. xmlChar *ret, ch;
  1549. xmlChar *temp;
  1550. const xmlChar *in;
  1551. int len, out;
  1552. if (str == NULL)
  1553. return(NULL);
  1554. if (str[0] == 0)
  1555. return(xmlStrdup(str));
  1556. len = xmlStrlen(str);
  1557. if (!(len > 0)) return(NULL);
  1558. len += 20;
  1559. ret = (xmlChar *) xmlMallocAtomic(len);
  1560. if (ret == NULL) {
  1561. xmlURIErrMemory("escaping URI value\n");
  1562. return(NULL);
  1563. }
  1564. in = (const xmlChar *) str;
  1565. out = 0;
  1566. while(*in != 0) {
  1567. if (len - out <= 3) {
  1568. temp = xmlSaveUriRealloc(ret, &len);
  1569. if (temp == NULL) {
  1570. xmlURIErrMemory("escaping URI value\n");
  1571. xmlFree(ret);
  1572. return(NULL);
  1573. }
  1574. ret = temp;
  1575. }
  1576. ch = *in;
  1577. if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!xmlStrchr(list, ch))) {
  1578. unsigned char val;
  1579. ret[out++] = '%';
  1580. val = ch >> 4;
  1581. if (val <= 9)
  1582. ret[out++] = '0' + val;
  1583. else
  1584. ret[out++] = 'A' + val - 0xA;
  1585. val = ch & 0xF;
  1586. if (val <= 9)
  1587. ret[out++] = '0' + val;
  1588. else
  1589. ret[out++] = 'A' + val - 0xA;
  1590. in++;
  1591. } else {
  1592. ret[out++] = *in++;
  1593. }
  1594. }
  1595. ret[out] = 0;
  1596. return(ret);
  1597. }
  1598. /**
  1599. * xmlURIEscape:
  1600. * @str: the string of the URI to escape
  1601. *
  1602. * Escaping routine, does not do validity checks !
  1603. * It will try to escape the chars needing this, but this is heuristic
  1604. * based it's impossible to be sure.
  1605. *
  1606. * Returns an copy of the string, but escaped
  1607. *
  1608. * 25 May 2001
  1609. * Uses xmlParseURI and xmlURIEscapeStr to try to escape correctly
  1610. * according to RFC2396.
  1611. * - Carl Douglas
  1612. */
  1613. xmlChar *
  1614. xmlURIEscape(const xmlChar * str)
  1615. {
  1616. xmlChar *ret, *segment = NULL;
  1617. xmlURIPtr uri;
  1618. int ret2;
  1619. if (str == NULL)
  1620. return (NULL);
  1621. uri = xmlCreateURI();
  1622. if (uri != NULL) {
  1623. /*
  1624. * Allow escaping errors in the unescaped form
  1625. */
  1626. uri->cleanup = 1;
  1627. ret2 = xmlParseURIReference(uri, (const char *)str);
  1628. if (ret2) {
  1629. xmlFreeURI(uri);
  1630. return (NULL);
  1631. }
  1632. }
  1633. if (!uri)
  1634. return NULL;
  1635. ret = NULL;
  1636. #define NULLCHK(p) if(!p) { \
  1637. xmlURIErrMemory("escaping URI value\n"); \
  1638. xmlFreeURI(uri); \
  1639. xmlFree(ret); \
  1640. return NULL; } \
  1641. if (uri->scheme) {
  1642. segment = xmlURIEscapeStr(BAD_CAST uri->scheme, BAD_CAST "+-.");
  1643. NULLCHK(segment)
  1644. ret = xmlStrcat(ret, segment);
  1645. ret = xmlStrcat(ret, BAD_CAST ":");
  1646. xmlFree(segment);
  1647. }
  1648. if (uri->authority) {
  1649. segment =
  1650. xmlURIEscapeStr(BAD_CAST uri->authority, BAD_CAST "/?;:@");
  1651. NULLCHK(segment)
  1652. ret = xmlStrcat(ret, BAD_CAST "//");
  1653. ret = xmlStrcat(ret, segment);
  1654. xmlFree(segment);
  1655. }
  1656. if (uri->user) {
  1657. segment = xmlURIEscapeStr(BAD_CAST uri->user, BAD_CAST ";:&=+$,");
  1658. NULLCHK(segment)
  1659. ret = xmlStrcat(ret,BAD_CAST "//");
  1660. ret = xmlStrcat(ret, segment);
  1661. ret = xmlStrcat(ret, BAD_CAST "@");
  1662. xmlFree(segment);
  1663. }
  1664. if (uri->server) {
  1665. segment = xmlURIEscapeStr(BAD_CAST uri->server, BAD_CAST "/?;:@");
  1666. NULLCHK(segment)
  1667. if (uri->user == NULL)
  1668. ret = xmlStrcat(ret, BAD_CAST "//");
  1669. ret = xmlStrcat(ret, segment);
  1670. xmlFree(segment);
  1671. }
  1672. if (uri->port) {
  1673. xmlChar port[10];
  1674. snprintf((char *) port, 10, "%d", uri->port);
  1675. ret = xmlStrcat(ret, BAD_CAST ":");
  1676. ret = xmlStrcat(ret, port);
  1677. }
  1678. if (uri->path) {
  1679. segment =
  1680. xmlURIEscapeStr(BAD_CAST uri->path, BAD_CAST ":@&=+$,/?;");
  1681. NULLCHK(segment)
  1682. ret = xmlStrcat(ret, segment);
  1683. xmlFree(segment);
  1684. }
  1685. if (uri->query_raw) {
  1686. ret = xmlStrcat(ret, BAD_CAST "?");
  1687. ret = xmlStrcat(ret, BAD_CAST uri->query_raw);
  1688. }
  1689. else if (uri->query) {
  1690. segment =
  1691. xmlURIEscapeStr(BAD_CAST uri->query, BAD_CAST ";/?:@&=+,$");
  1692. NULLCHK(segment)
  1693. ret = xmlStrcat(ret, BAD_CAST "?");
  1694. ret = xmlStrcat(ret, segment);
  1695. xmlFree(segment);
  1696. }
  1697. if (uri->opaque) {
  1698. segment = xmlURIEscapeStr(BAD_CAST uri->opaque, BAD_CAST "");
  1699. NULLCHK(segment)
  1700. ret = xmlStrcat(ret, segment);
  1701. xmlFree(segment);
  1702. }
  1703. if (uri->fragment) {
  1704. segment = xmlURIEscapeStr(BAD_CAST uri->fragment, BAD_CAST "#");
  1705. NULLCHK(segment)
  1706. ret = xmlStrcat(ret, BAD_CAST "#");
  1707. ret = xmlStrcat(ret, segment);
  1708. xmlFree(segment);
  1709. }
  1710. xmlFreeURI(uri);
  1711. #undef NULLCHK
  1712. return (ret);
  1713. }
  1714. /************************************************************************
  1715. * *
  1716. * Public functions *
  1717. * *
  1718. ************************************************************************/
  1719. /**
  1720. * xmlBuildURI:
  1721. * @URI: the URI instance found in the document
  1722. * @base: the base value
  1723. *
  1724. * Computes he final URI of the reference done by checking that
  1725. * the given URI is valid, and building the final URI using the
  1726. * base URI. This is processed according to section 5.2 of the
  1727. * RFC 2396
  1728. *
  1729. * 5.2. Resolving Relative References to Absolute Form
  1730. *
  1731. * Returns a new URI string (to be freed by the caller) or NULL in case
  1732. * of error.
  1733. */
  1734. xmlChar *
  1735. xmlBuildURI(const xmlChar *URI, const xmlChar *base) {
  1736. xmlChar *val = NULL;
  1737. int ret, len, indx, cur, out;
  1738. xmlURIPtr ref = NULL;
  1739. xmlURIPtr bas = NULL;
  1740. xmlURIPtr res = NULL;
  1741. /*
  1742. * 1) The URI reference is parsed into the potential four components and
  1743. * fragment identifier, as described in Section 4.3.
  1744. *
  1745. * NOTE that a completely empty URI is treated by modern browsers
  1746. * as a reference to "." rather than as a synonym for the current
  1747. * URI. Should we do that here?
  1748. */
  1749. if (URI == NULL)
  1750. ret = -1;
  1751. else {
  1752. if (*URI) {
  1753. ref = xmlCreateURI();
  1754. if (ref == NULL)
  1755. goto done;
  1756. ret = xmlParseURIReference(ref, (const char *) URI);
  1757. }
  1758. else
  1759. ret = 0;
  1760. }
  1761. if (ret != 0)
  1762. goto done;
  1763. if ((ref != NULL) && (ref->scheme != NULL)) {
  1764. /*
  1765. * The URI is absolute don't modify.
  1766. */
  1767. val = xmlStrdup(URI);
  1768. goto done;
  1769. }
  1770. if (base == NULL)
  1771. ret = -1;
  1772. else {
  1773. bas = xmlCreateURI();
  1774. if (bas == NULL)
  1775. goto done;
  1776. ret = xmlParseURIReference(bas, (const char *) base);
  1777. }
  1778. if (ret != 0) {
  1779. if (ref)
  1780. val = xmlSaveUri(ref);
  1781. goto done;
  1782. }
  1783. if (ref == NULL) {
  1784. /*
  1785. * the base fragment must be ignored
  1786. */
  1787. if (bas->fragment != NULL) {
  1788. xmlFree(bas->fragment);
  1789. bas->fragment = NULL;
  1790. }
  1791. val = xmlSaveUri(bas);
  1792. goto done;
  1793. }
  1794. /*
  1795. * 2) If the path component is empty and the scheme, authority, and
  1796. * query components are undefined, then it is a reference to the
  1797. * current document and we are done. Otherwise, the reference URI's
  1798. * query and fragment components are defined as found (or not found)
  1799. * within the URI reference and not inherited from the base URI.
  1800. *
  1801. * NOTE that in modern browsers, the parsing differs from the above
  1802. * in the following aspect: the query component is allowed to be
  1803. * defined while still treating this as a reference to the current
  1804. * document.
  1805. */
  1806. res = xmlCreateURI();
  1807. if (res == NULL)
  1808. goto done;
  1809. if ((ref->scheme == NULL) && (ref->path == NULL) &&
  1810. ((ref->authority == NULL) && (ref->server == NULL))) {
  1811. if (bas->scheme != NULL)
  1812. res->scheme = xmlMemStrdup(bas->scheme);
  1813. if (bas->authority != NULL)
  1814. res->authority = xmlMemStrdup(bas->authority);
  1815. else if ((bas->server != NULL) || (bas->port == -1)) {
  1816. if (bas->server != NULL)
  1817. res->server = xmlMemStrdup(bas->server);
  1818. if (bas->user != NULL)
  1819. res->user = xmlMemStrdup(bas->user);
  1820. res->port = bas->port;
  1821. }
  1822. if (bas->path != NULL)
  1823. res->path = xmlMemStrdup(bas->path);
  1824. if (ref->query_raw != NULL)
  1825. res->query_raw = xmlMemStrdup (ref->query_raw);
  1826. else if (ref->query != NULL)
  1827. res->query = xmlMemStrdup(ref->query);
  1828. else if (bas->query_raw != NULL)
  1829. res->query_raw = xmlMemStrdup(bas->query_raw);
  1830. else if (bas->query != NULL)
  1831. res->query = xmlMemStrdup(bas->query);
  1832. if (ref->fragment != NULL)
  1833. res->fragment = xmlMemStrdup(ref->fragment);
  1834. goto step_7;
  1835. }
  1836. /*
  1837. * 3) If the scheme component is defined, indicating that the reference
  1838. * starts with a scheme name, then the reference is interpreted as an
  1839. * absolute URI and we are done. Otherwise, the reference URI's
  1840. * scheme is inherited from the base URI's scheme component.
  1841. */
  1842. if (ref->scheme != NULL) {
  1843. val = xmlSaveUri(ref);
  1844. goto done;
  1845. }
  1846. if (bas->scheme != NULL)
  1847. res->scheme = xmlMemStrdup(bas->scheme);
  1848. if (ref->query_raw != NULL)
  1849. res->query_raw = xmlMemStrdup(ref->query_raw);
  1850. else if (ref->query != NULL)
  1851. res->query = xmlMemStrdup(ref->query);
  1852. if (ref->fragment != NULL)
  1853. res->fragment = xmlMemStrdup(ref->fragment);
  1854. /*
  1855. * 4) If the authority component is defined, then the reference is a
  1856. * network-path and we skip to step 7. Otherwise, the reference
  1857. * URI's authority is inherited from the base URI's authority
  1858. * component, which will also be undefined if the URI scheme does not
  1859. * use an authority component.
  1860. */
  1861. if ((ref->authority != NULL) || (ref->server != NULL)) {
  1862. if (ref->authority != NULL)
  1863. res->authority = xmlMemStrdup(ref->authority);
  1864. else {
  1865. res->server = xmlMemStrdup(ref->server);
  1866. if (ref->user != NULL)
  1867. res->user = xmlMemStrdup(ref->user);
  1868. res->port = ref->port;
  1869. }
  1870. if (ref->path != NULL)
  1871. res->path = xmlMemStrdup(ref->path);
  1872. goto step_7;
  1873. }
  1874. if (bas->authority != NULL)
  1875. res->authority = xmlMemStrdup(bas->authority);
  1876. else if ((bas->server != NULL) || (bas->port == -1)) {
  1877. if (bas->server != NULL)
  1878. res->server = xmlMemStrdup(bas->server);
  1879. if (bas->user != NULL)
  1880. res->user = xmlMemStrdup(bas->user);
  1881. res->port = bas->port;
  1882. }
  1883. /*
  1884. * 5) If the path component begins with a slash character ("/"), then
  1885. * the reference is an absolute-path and we skip to step 7.
  1886. */
  1887. if ((ref->path != NULL) && (ref->path[0] == '/')) {
  1888. res->path = xmlMemStrdup(ref->path);
  1889. goto step_7;
  1890. }
  1891. /*
  1892. * 6) If this step is reached, then we are resolving a relative-path
  1893. * reference. The relative path needs to be merged with the base
  1894. * URI's path. Although there are many ways to do this, we will
  1895. * describe a simple method using a separate string buffer.
  1896. *
  1897. * Allocate a buffer large enough for the result string.
  1898. */
  1899. len = 2; /* extra / and 0 */
  1900. if (ref->path != NULL)
  1901. len += strlen(ref->path);
  1902. if (bas->path != NULL)
  1903. len += strlen(bas->path);
  1904. res->path = (char *) xmlMallocAtomic(len);
  1905. if (res->path == NULL) {
  1906. xmlURIErrMemory("resolving URI against base\n");
  1907. goto done;
  1908. }
  1909. res->path[0] = 0;
  1910. /*
  1911. * a) All but the last segment of the base URI's path component is
  1912. * copied to the buffer. In other words, any characters after the
  1913. * last (right-most) slash character, if any, are excluded.
  1914. */
  1915. cur = 0;
  1916. out = 0;
  1917. if (bas->path != NULL) {
  1918. while (bas->path[cur] != 0) {
  1919. while ((bas->path[cur] != 0) && (bas->path[cur] != '/'))
  1920. cur++;
  1921. if (bas->path[cur] == 0)
  1922. break;
  1923. cur++;
  1924. while (out < cur) {
  1925. res->path[out] = bas->path[out];
  1926. out++;
  1927. }
  1928. }
  1929. }
  1930. res->path[out] = 0;
  1931. /*
  1932. * b) The reference's path component is appended to the buffer
  1933. * string.
  1934. */
  1935. if (ref->path != NULL && ref->path[0] != 0) {
  1936. indx = 0;
  1937. /*
  1938. * Ensure the path includes a '/'
  1939. */
  1940. if ((out == 0) && (bas->server != NULL))
  1941. res->path[out++] = '/';
  1942. while (ref->path[indx] != 0) {
  1943. res->path[out++] = ref->path[indx++];
  1944. }
  1945. }
  1946. res->path[out] = 0;
  1947. /*
  1948. * Steps c) to h) are really path normalization steps
  1949. */
  1950. xmlNormalizeURIPath(res->path);
  1951. step_7:
  1952. /*
  1953. * 7) The resulting URI components, including any inherited from the
  1954. * base URI, are recombined to give the absolute form of the URI
  1955. * reference.
  1956. */
  1957. val = xmlSaveUri(res);
  1958. done:
  1959. if (ref != NULL)
  1960. xmlFreeURI(ref);
  1961. if (bas != NULL)
  1962. xmlFreeURI(bas);
  1963. if (res != NULL)
  1964. xmlFreeURI(res);
  1965. return(val);
  1966. }
  1967. /**
  1968. * xmlBuildRelativeURI:
  1969. * @URI: the URI reference under consideration
  1970. * @base: the base value
  1971. *
  1972. * Expresses the URI of the reference in terms relative to the
  1973. * base. Some examples of this operation include:
  1974. * base = "http://site1.com/docs/book1.html"
  1975. * URI input URI returned
  1976. * docs/pic1.gif pic1.gif
  1977. * docs/img/pic1.gif img/pic1.gif
  1978. * img/pic1.gif ../img/pic1.gif
  1979. * http://site1.com/docs/pic1.gif pic1.gif
  1980. * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif
  1981. *
  1982. * base = "docs/book1.html"
  1983. * URI input URI returned
  1984. * docs/pic1.gif pic1.gif
  1985. * docs/img/pic1.gif img/pic1.gif
  1986. * img/pic1.gif ../img/pic1.gif
  1987. * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif
  1988. *
  1989. *
  1990. * Note: if the URI reference is really weird or complicated, it may be
  1991. * worthwhile to first convert it into a "nice" one by calling
  1992. * xmlBuildURI (using 'base') before calling this routine,
  1993. * since this routine (for reasonable efficiency) assumes URI has
  1994. * already been through some validation.
  1995. *
  1996. * Returns a new URI string (to be freed by the caller) or NULL in case
  1997. * error.
  1998. */
  1999. xmlChar *
  2000. xmlBuildRelativeURI (const xmlChar * URI, const xmlChar * base)
  2001. {
  2002. xmlChar *val = NULL;
  2003. int ret;
  2004. int ix;
  2005. int nbslash = 0;
  2006. int len;
  2007. xmlURIPtr ref = NULL;
  2008. xmlURIPtr bas = NULL;
  2009. xmlChar *bptr, *uptr, *vptr;
  2010. int remove_path = 0;
  2011. if ((URI == NULL) || (*URI == 0))
  2012. return NULL;
  2013. /*
  2014. * First parse URI into a standard form
  2015. */
  2016. ref = xmlCreateURI ();
  2017. if (ref == NULL)
  2018. return NULL;
  2019. /* If URI not already in "relative" form */
  2020. if (URI[0] != '.') {
  2021. ret = xmlParseURIReference (ref, (const char *) URI);
  2022. if (ret != 0)
  2023. goto done; /* Error in URI, return NULL */
  2024. } else
  2025. ref->path = (char *)xmlStrdup(URI);
  2026. /*
  2027. * Next parse base into the same standard form
  2028. */
  2029. if ((base == NULL) || (*base == 0)) {
  2030. val = xmlStrdup (URI);
  2031. goto done;
  2032. }
  2033. bas = xmlCreateURI ();
  2034. if (bas == NULL)
  2035. goto done;
  2036. if (base[0] != '.') {
  2037. ret = xmlParseURIReference (bas, (const char *) base);
  2038. if (ret != 0)
  2039. goto done; /* Error in base, return NULL */
  2040. } else
  2041. bas->path = (char *)xmlStrdup(base);
  2042. /*
  2043. * If the scheme / server on the URI differs from the base,
  2044. * just return the URI
  2045. */
  2046. if ((ref->scheme != NULL) &&
  2047. ((bas->scheme == NULL) ||
  2048. (xmlStrcmp ((xmlChar *)bas->scheme, (xmlChar *)ref->scheme)) ||
  2049. (xmlStrcmp ((xmlChar *)bas->server, (xmlChar *)ref->server)))) {
  2050. val = xmlStrdup (URI);
  2051. goto done;
  2052. }
  2053. if (xmlStrEqual((xmlChar *)bas->path, (xmlChar *)ref->path)) {
  2054. val = xmlStrdup(BAD_CAST "");
  2055. goto done;
  2056. }
  2057. if (bas->path == NULL) {
  2058. val = xmlStrdup((xmlChar *)ref->path);
  2059. goto done;
  2060. }
  2061. if (ref->path == NULL) {
  2062. ref->path = (char *) "/";
  2063. remove_path = 1;
  2064. }
  2065. /*
  2066. * At this point (at last!) we can compare the two paths
  2067. *
  2068. * First we take care of the special case where either of the
  2069. * two path components may be missing (bug 316224)
  2070. */
  2071. bptr = (xmlChar *)bas->path;
  2072. {
  2073. xmlChar *rptr = (xmlChar *) ref->path;
  2074. int pos = 0;
  2075. /*
  2076. * Next we compare the two strings and find where they first differ
  2077. */
  2078. if ((*rptr == '.') && (rptr[1] == '/'))
  2079. rptr += 2;
  2080. if ((*bptr == '.') && (bptr[1] == '/'))
  2081. bptr += 2;
  2082. else if ((*bptr == '/') && (*rptr != '/'))
  2083. bptr++;
  2084. while ((bptr[pos] == rptr[pos]) && (bptr[pos] != 0))
  2085. pos++;
  2086. if (bptr[pos] == rptr[pos]) {
  2087. val = xmlStrdup(BAD_CAST "");
  2088. goto done; /* (I can't imagine why anyone would do this) */
  2089. }
  2090. /*
  2091. * In URI, "back up" to the last '/' encountered. This will be the
  2092. * beginning of the "unique" suffix of URI
  2093. */
  2094. ix = pos;
  2095. for (; ix > 0; ix--) {
  2096. if (rptr[ix - 1] == '/')
  2097. break;
  2098. }
  2099. uptr = (xmlChar *)&rptr[ix];
  2100. /*
  2101. * In base, count the number of '/' from the differing point
  2102. */
  2103. for (; bptr[ix] != 0; ix++) {
  2104. if (bptr[ix] == '/')
  2105. nbslash++;
  2106. }
  2107. /*
  2108. * e.g: URI="foo/" base="foo/bar" -> "./"
  2109. */
  2110. if (nbslash == 0 && !uptr[0]) {
  2111. val = xmlStrdup(BAD_CAST "./");
  2112. goto done;
  2113. }
  2114. len = xmlStrlen (uptr) + 1;
  2115. }
  2116. if (nbslash == 0) {
  2117. if (uptr != NULL)
  2118. /* exception characters from xmlSaveUri */
  2119. val = xmlURIEscapeStr(uptr, BAD_CAST "/;&=+$,");
  2120. goto done;
  2121. }
  2122. /*
  2123. * Allocate just enough space for the returned string -
  2124. * length of the remainder of the URI, plus enough space
  2125. * for the "../" groups, plus one for the terminator
  2126. */
  2127. val = (xmlChar *) xmlMalloc (len + 3 * nbslash);
  2128. if (val == NULL) {
  2129. xmlURIErrMemory("building relative URI\n");
  2130. goto done;
  2131. }
  2132. vptr = val;
  2133. /*
  2134. * Put in as many "../" as needed
  2135. */
  2136. for (; nbslash>0; nbslash--) {
  2137. *vptr++ = '.';
  2138. *vptr++ = '.';
  2139. *vptr++ = '/';
  2140. }
  2141. /*
  2142. * Finish up with the end of the URI
  2143. */
  2144. if (uptr != NULL) {
  2145. if ((vptr > val) && (len > 0) &&
  2146. (uptr[0] == '/') && (vptr[-1] == '/')) {
  2147. memcpy (vptr, uptr + 1, len - 1);
  2148. vptr[len - 2] = 0;
  2149. } else {
  2150. memcpy (vptr, uptr, len);
  2151. vptr[len - 1] = 0;
  2152. }
  2153. } else {
  2154. vptr[len - 1] = 0;
  2155. }
  2156. /* escape the freshly-built path */
  2157. vptr = val;
  2158. /* exception characters from xmlSaveUri */
  2159. val = xmlURIEscapeStr(vptr, BAD_CAST "/;&=+$,");
  2160. xmlFree(vptr);
  2161. done:
  2162. /*
  2163. * Free the working variables
  2164. */
  2165. if (remove_path != 0)
  2166. ref->path = NULL;
  2167. if (ref != NULL)
  2168. xmlFreeURI (ref);
  2169. if (bas != NULL)
  2170. xmlFreeURI (bas);
  2171. return val;
  2172. }
  2173. /**
  2174. * xmlCanonicPath:
  2175. * @path: the resource locator in a filesystem notation
  2176. *
  2177. * Constructs a canonic path from the specified path.
  2178. *
  2179. * Returns a new canonic path, or a duplicate of the path parameter if the
  2180. * construction fails. The caller is responsible for freeing the memory occupied
  2181. * by the returned string. If there is insufficient memory available, or the
  2182. * argument is NULL, the function returns NULL.
  2183. */
  2184. #define IS_WINDOWS_PATH(p) \
  2185. ((p != NULL) && \
  2186. (((p[0] >= 'a') && (p[0] <= 'z')) || \
  2187. ((p[0] >= 'A') && (p[0] <= 'Z'))) && \
  2188. (p[1] == ':') && ((p[2] == '/') || (p[2] == '\\')))
  2189. xmlChar *
  2190. xmlCanonicPath(const xmlChar *path)
  2191. {
  2192. /*
  2193. * For Windows implementations, additional work needs to be done to
  2194. * replace backslashes in pathnames with "forward slashes"
  2195. */
  2196. #if defined(_WIN32) && !defined(__CYGWIN__)
  2197. int len = 0;
  2198. char *p = NULL;
  2199. #endif
  2200. xmlURIPtr uri;
  2201. xmlChar *ret;
  2202. const xmlChar *absuri;
  2203. if (path == NULL)
  2204. return(NULL);
  2205. #if defined(_WIN32)
  2206. /*
  2207. * We must not change the backslashes to slashes if the the path
  2208. * starts with \\?\
  2209. * Those paths can be up to 32k characters long.
  2210. * Was added specifically for OpenOffice, those paths can't be converted
  2211. * to URIs anyway.
  2212. */
  2213. if ((path[0] == '\\') && (path[1] == '\\') && (path[2] == '?') &&
  2214. (path[3] == '\\') )
  2215. return xmlStrdup((const xmlChar *) path);
  2216. #endif
  2217. /* sanitize filename starting with // so it can be used as URI */
  2218. if ((path[0] == '/') && (path[1] == '/') && (path[2] != '/'))
  2219. path++;
  2220. if ((uri = xmlParseURI((const char *) path)) != NULL) {
  2221. xmlFreeURI(uri);
  2222. return xmlStrdup(path);
  2223. }
  2224. /* Check if this is an "absolute uri" */
  2225. absuri = xmlStrstr(path, BAD_CAST "://");
  2226. if (absuri != NULL) {
  2227. int l, j;
  2228. unsigned char c;
  2229. xmlChar *escURI;
  2230. /*
  2231. * this looks like an URI where some parts have not been
  2232. * escaped leading to a parsing problem. Check that the first
  2233. * part matches a protocol.
  2234. */
  2235. l = absuri - path;
  2236. /* Bypass if first part (part before the '://') is > 20 chars */
  2237. if ((l <= 0) || (l > 20))
  2238. goto path_processing;
  2239. /* Bypass if any non-alpha characters are present in first part */
  2240. for (j = 0;j < l;j++) {
  2241. c = path[j];
  2242. if (!(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))))
  2243. goto path_processing;
  2244. }
  2245. /* Escape all except the characters specified in the supplied path */
  2246. escURI = xmlURIEscapeStr(path, BAD_CAST ":/?_.#&;=");
  2247. if (escURI != NULL) {
  2248. /* Try parsing the escaped path */
  2249. uri = xmlParseURI((const char *) escURI);
  2250. /* If successful, return the escaped string */
  2251. if (uri != NULL) {
  2252. xmlFreeURI(uri);
  2253. return escURI;
  2254. }
  2255. xmlFree(escURI);
  2256. }
  2257. }
  2258. path_processing:
  2259. /* For Windows implementations, replace backslashes with 'forward slashes' */
  2260. #if defined(_WIN32) && !defined(__CYGWIN__)
  2261. /*
  2262. * Create a URI structure
  2263. */
  2264. uri = xmlCreateURI();
  2265. if (uri == NULL) { /* Guard against 'out of memory' */
  2266. return(NULL);
  2267. }
  2268. len = xmlStrlen(path);
  2269. if ((len > 2) && IS_WINDOWS_PATH(path)) {
  2270. /* make the scheme 'file' */
  2271. uri->scheme = (char *) xmlStrdup(BAD_CAST "file");
  2272. /* allocate space for leading '/' + path + string terminator */
  2273. uri->path = xmlMallocAtomic(len + 2);
  2274. if (uri->path == NULL) {
  2275. xmlFreeURI(uri); /* Guard against 'out of memory' */
  2276. return(NULL);
  2277. }
  2278. /* Put in leading '/' plus path */
  2279. uri->path[0] = '/';
  2280. p = uri->path + 1;
  2281. strncpy(p, (char *) path, len + 1);
  2282. } else {
  2283. uri->path = (char *) xmlStrdup(path);
  2284. if (uri->path == NULL) {
  2285. xmlFreeURI(uri);
  2286. return(NULL);
  2287. }
  2288. p = uri->path;
  2289. }
  2290. /* Now change all occurrences of '\' to '/' */
  2291. while (*p != '\0') {
  2292. if (*p == '\\')
  2293. *p = '/';
  2294. p++;
  2295. }
  2296. if (uri->scheme == NULL) {
  2297. ret = xmlStrdup((const xmlChar *) uri->path);
  2298. } else {
  2299. ret = xmlSaveUri(uri);
  2300. }
  2301. xmlFreeURI(uri);
  2302. #else
  2303. ret = xmlStrdup((const xmlChar *) path);
  2304. #endif
  2305. return(ret);
  2306. }
  2307. /**
  2308. * xmlPathToURI:
  2309. * @path: the resource locator in a filesystem notation
  2310. *
  2311. * Constructs an URI expressing the existing path
  2312. *
  2313. * Returns a new URI, or a duplicate of the path parameter if the
  2314. * construction fails. The caller is responsible for freeing the memory
  2315. * occupied by the returned string. If there is insufficient memory available,
  2316. * or the argument is NULL, the function returns NULL.
  2317. */
  2318. xmlChar *
  2319. xmlPathToURI(const xmlChar *path)
  2320. {
  2321. xmlURIPtr uri;
  2322. xmlURI temp;
  2323. xmlChar *ret, *cal;
  2324. if (path == NULL)
  2325. return(NULL);
  2326. if ((uri = xmlParseURI((const char *) path)) != NULL) {
  2327. xmlFreeURI(uri);
  2328. return xmlStrdup(path);
  2329. }
  2330. cal = xmlCanonicPath(path);
  2331. if (cal == NULL)
  2332. return(NULL);
  2333. #if defined(_WIN32) && !defined(__CYGWIN__)
  2334. /* xmlCanonicPath can return an URI on Windows (is that the intended behaviour?)
  2335. If 'cal' is a valid URI already then we are done here, as continuing would make
  2336. it invalid. */
  2337. if ((uri = xmlParseURI((const char *) cal)) != NULL) {
  2338. xmlFreeURI(uri);
  2339. return cal;
  2340. }
  2341. /* 'cal' can contain a relative path with backslashes. If that is processed
  2342. by xmlSaveURI, they will be escaped and the external entity loader machinery
  2343. will fail. So convert them to slashes. Misuse 'ret' for walking. */
  2344. ret = cal;
  2345. while (*ret != '\0') {
  2346. if (*ret == '\\')
  2347. *ret = '/';
  2348. ret++;
  2349. }
  2350. #endif
  2351. memset(&temp, 0, sizeof(temp));
  2352. temp.path = (char *) cal;
  2353. ret = xmlSaveUri(&temp);
  2354. xmlFree(cal);
  2355. return(ret);
  2356. }
  2357. #define bottom_uri
  2358. #include "elfgcchack.h"