buf.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /*
  2. * buf.c: memory buffers for libxml2
  3. *
  4. * new buffer structures and entry points to simplify the maintenance
  5. * of libxml2 and ensure we keep good control over memory allocations
  6. * and stay 64 bits clean.
  7. * The new entry point use the xmlBufPtr opaque structure and
  8. * xmlBuf...() counterparts to the old xmlBuf...() functions
  9. *
  10. * See Copyright for the status of this software.
  11. *
  12. * daniel@veillard.com
  13. */
  14. #define IN_LIBXML
  15. #include "libxml.h"
  16. #include <string.h> /* for memset() only ! */
  17. #include <limits.h>
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20. #include <libxml/tree.h>
  21. #include <libxml/parserInternals.h> /* for XML_MAX_TEXT_LENGTH */
  22. #include "private/buf.h"
  23. #include "private/error.h"
  24. #ifndef SIZE_MAX
  25. #define SIZE_MAX ((size_t) -1)
  26. #endif
  27. #define WITH_BUFFER_COMPAT
  28. /**
  29. * xmlBuf:
  30. *
  31. * A buffer structure. The base of the structure is somehow compatible
  32. * with struct _xmlBuffer to limit risks on application which accessed
  33. * directly the input->buf->buffer structures.
  34. */
  35. struct _xmlBuf {
  36. xmlChar *content; /* The buffer content UTF8 */
  37. unsigned int compat_use; /* for binary compatibility */
  38. unsigned int compat_size; /* for binary compatibility */
  39. xmlBufferAllocationScheme alloc; /* The realloc method */
  40. xmlChar *contentIO; /* in IO mode we may have a different base */
  41. size_t use; /* The buffer size used */
  42. size_t size; /* The buffer size */
  43. xmlBufferPtr buffer; /* wrapper for an old buffer */
  44. int error; /* an error code if a failure occurred */
  45. };
  46. #ifdef WITH_BUFFER_COMPAT
  47. /*
  48. * Macro for compatibility with xmlBuffer to be used after an xmlBuf
  49. * is updated. This makes sure the compat fields are updated too.
  50. */
  51. #define UPDATE_COMPAT(buf) \
  52. if (buf->size < INT_MAX) buf->compat_size = buf->size; \
  53. else buf->compat_size = INT_MAX; \
  54. if (buf->use < INT_MAX) buf->compat_use = buf->use; \
  55. else buf->compat_use = INT_MAX;
  56. /*
  57. * Macro for compatibility with xmlBuffer to be used in all the xmlBuf
  58. * entry points, it checks that the compat fields have not been modified
  59. * by direct call to xmlBuffer function from code compiled before 2.9.0 .
  60. */
  61. #define CHECK_COMPAT(buf) \
  62. if (buf->size != (size_t) buf->compat_size) \
  63. if (buf->compat_size < INT_MAX) \
  64. buf->size = buf->compat_size; \
  65. if (buf->use != (size_t) buf->compat_use) \
  66. if (buf->compat_use < INT_MAX) \
  67. buf->use = buf->compat_use;
  68. #else /* ! WITH_BUFFER_COMPAT */
  69. #define UPDATE_COMPAT(buf)
  70. #define CHECK_COMPAT(buf)
  71. #endif /* WITH_BUFFER_COMPAT */
  72. /**
  73. * xmlBufMemoryError:
  74. * @extra: extra information
  75. *
  76. * Handle an out of memory condition
  77. * To be improved...
  78. */
  79. static void
  80. xmlBufMemoryError(xmlBufPtr buf, const char *extra)
  81. {
  82. __xmlSimpleError(XML_FROM_BUFFER, XML_ERR_NO_MEMORY, NULL, NULL, extra);
  83. if ((buf) && (buf->error == 0))
  84. buf->error = XML_ERR_NO_MEMORY;
  85. }
  86. /**
  87. * xmlBufOverflowError:
  88. * @extra: extra information
  89. *
  90. * Handle a buffer overflow error
  91. * To be improved...
  92. */
  93. static void
  94. xmlBufOverflowError(xmlBufPtr buf, const char *extra)
  95. {
  96. __xmlSimpleError(XML_FROM_BUFFER, XML_BUF_OVERFLOW, NULL, NULL, extra);
  97. if ((buf) && (buf->error == 0))
  98. buf->error = XML_BUF_OVERFLOW;
  99. }
  100. /**
  101. * xmlBufCreate:
  102. *
  103. * routine to create an XML buffer.
  104. * returns the new structure.
  105. */
  106. xmlBufPtr
  107. xmlBufCreate(void) {
  108. xmlBufPtr ret;
  109. ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
  110. if (ret == NULL) {
  111. xmlBufMemoryError(NULL, "creating buffer");
  112. return(NULL);
  113. }
  114. ret->use = 0;
  115. ret->error = 0;
  116. ret->buffer = NULL;
  117. ret->size = xmlDefaultBufferSize;
  118. UPDATE_COMPAT(ret);
  119. ret->alloc = xmlBufferAllocScheme;
  120. ret->content = (xmlChar *) xmlMallocAtomic(ret->size);
  121. if (ret->content == NULL) {
  122. xmlBufMemoryError(ret, "creating buffer");
  123. xmlFree(ret);
  124. return(NULL);
  125. }
  126. ret->content[0] = 0;
  127. ret->contentIO = NULL;
  128. return(ret);
  129. }
  130. /**
  131. * xmlBufCreateSize:
  132. * @size: initial size of buffer
  133. *
  134. * routine to create an XML buffer.
  135. * returns the new structure.
  136. */
  137. xmlBufPtr
  138. xmlBufCreateSize(size_t size) {
  139. xmlBufPtr ret;
  140. if (size == SIZE_MAX)
  141. return(NULL);
  142. ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
  143. if (ret == NULL) {
  144. xmlBufMemoryError(NULL, "creating buffer");
  145. return(NULL);
  146. }
  147. ret->use = 0;
  148. ret->error = 0;
  149. ret->buffer = NULL;
  150. ret->alloc = xmlBufferAllocScheme;
  151. ret->size = (size ? size + 1 : 0); /* +1 for ending null */
  152. UPDATE_COMPAT(ret);
  153. if (ret->size){
  154. ret->content = (xmlChar *) xmlMallocAtomic(ret->size);
  155. if (ret->content == NULL) {
  156. xmlBufMemoryError(ret, "creating buffer");
  157. xmlFree(ret);
  158. return(NULL);
  159. }
  160. ret->content[0] = 0;
  161. } else
  162. ret->content = NULL;
  163. ret->contentIO = NULL;
  164. return(ret);
  165. }
  166. /**
  167. * xmlBufDetach:
  168. * @buf: the buffer
  169. *
  170. * Remove the string contained in a buffer and give it back to the
  171. * caller. The buffer is reset to an empty content.
  172. * This doesn't work with immutable buffers as they can't be reset.
  173. *
  174. * Returns the previous string contained by the buffer.
  175. */
  176. xmlChar *
  177. xmlBufDetach(xmlBufPtr buf) {
  178. xmlChar *ret;
  179. if (buf == NULL)
  180. return(NULL);
  181. if (buf->buffer != NULL)
  182. return(NULL);
  183. if (buf->error)
  184. return(NULL);
  185. ret = buf->content;
  186. buf->content = NULL;
  187. buf->size = 0;
  188. buf->use = 0;
  189. UPDATE_COMPAT(buf);
  190. return ret;
  191. }
  192. /**
  193. * xmlBufGetAllocationScheme:
  194. * @buf: the buffer
  195. *
  196. * Get the buffer allocation scheme
  197. *
  198. * Returns the scheme or -1 in case of error
  199. */
  200. int
  201. xmlBufGetAllocationScheme(xmlBufPtr buf) {
  202. if (buf == NULL) {
  203. return(-1);
  204. }
  205. return(buf->alloc);
  206. }
  207. /**
  208. * xmlBufSetAllocationScheme:
  209. * @buf: the buffer to tune
  210. * @scheme: allocation scheme to use
  211. *
  212. * Sets the allocation scheme for this buffer
  213. *
  214. * returns 0 in case of success and -1 in case of failure
  215. */
  216. int
  217. xmlBufSetAllocationScheme(xmlBufPtr buf,
  218. xmlBufferAllocationScheme scheme) {
  219. if ((buf == NULL) || (buf->error != 0)) {
  220. return(-1);
  221. }
  222. if (buf->alloc == XML_BUFFER_ALLOC_IO)
  223. return(-1);
  224. if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
  225. (scheme == XML_BUFFER_ALLOC_EXACT) ||
  226. (scheme == XML_BUFFER_ALLOC_HYBRID) ||
  227. (scheme == XML_BUFFER_ALLOC_BOUNDED)) {
  228. buf->alloc = scheme;
  229. if (buf->buffer)
  230. buf->buffer->alloc = scheme;
  231. return(0);
  232. }
  233. /*
  234. * Switching a buffer ALLOC_IO has the side effect of initializing
  235. * the contentIO field with the current content
  236. */
  237. if (scheme == XML_BUFFER_ALLOC_IO) {
  238. buf->alloc = XML_BUFFER_ALLOC_IO;
  239. buf->contentIO = buf->content;
  240. }
  241. return(-1);
  242. }
  243. /**
  244. * xmlBufFree:
  245. * @buf: the buffer to free
  246. *
  247. * Frees an XML buffer. It frees both the content and the structure which
  248. * encapsulate it.
  249. */
  250. void
  251. xmlBufFree(xmlBufPtr buf) {
  252. if (buf == NULL) {
  253. return;
  254. }
  255. if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
  256. (buf->contentIO != NULL)) {
  257. xmlFree(buf->contentIO);
  258. } else if (buf->content != NULL) {
  259. xmlFree(buf->content);
  260. }
  261. xmlFree(buf);
  262. }
  263. /**
  264. * xmlBufEmpty:
  265. * @buf: the buffer
  266. *
  267. * empty a buffer.
  268. */
  269. void
  270. xmlBufEmpty(xmlBufPtr buf) {
  271. if ((buf == NULL) || (buf->error != 0)) return;
  272. if (buf->content == NULL) return;
  273. CHECK_COMPAT(buf)
  274. buf->use = 0;
  275. if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
  276. (buf->contentIO != NULL)) {
  277. size_t start_buf = buf->content - buf->contentIO;
  278. buf->size += start_buf;
  279. buf->content = buf->contentIO;
  280. buf->content[0] = 0;
  281. } else {
  282. buf->content[0] = 0;
  283. }
  284. UPDATE_COMPAT(buf)
  285. }
  286. /**
  287. * xmlBufShrink:
  288. * @buf: the buffer to dump
  289. * @len: the number of xmlChar to remove
  290. *
  291. * Remove the beginning of an XML buffer.
  292. * NOTE that this routine behaviour differs from xmlBufferShrink()
  293. * as it will return 0 on error instead of -1 due to size_t being
  294. * used as the return type.
  295. *
  296. * Returns the number of byte removed or 0 in case of failure
  297. */
  298. size_t
  299. xmlBufShrink(xmlBufPtr buf, size_t len) {
  300. if ((buf == NULL) || (buf->error != 0)) return(0);
  301. CHECK_COMPAT(buf)
  302. if (len == 0) return(0);
  303. if (len > buf->use) return(0);
  304. buf->use -= len;
  305. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  306. /*
  307. * we just move the content pointer, but also make sure
  308. * the perceived buffer size has shrunk accordingly
  309. */
  310. buf->content += len;
  311. buf->size -= len;
  312. /*
  313. * sometimes though it maybe be better to really shrink
  314. * on IO buffers
  315. */
  316. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  317. size_t start_buf = buf->content - buf->contentIO;
  318. if (start_buf >= buf->size) {
  319. memmove(buf->contentIO, &buf->content[0], buf->use);
  320. buf->content = buf->contentIO;
  321. buf->content[buf->use] = 0;
  322. buf->size += start_buf;
  323. }
  324. }
  325. } else {
  326. memmove(buf->content, &buf->content[len], buf->use);
  327. buf->content[buf->use] = 0;
  328. }
  329. UPDATE_COMPAT(buf)
  330. return(len);
  331. }
  332. /**
  333. * xmlBufGrowInternal:
  334. * @buf: the buffer
  335. * @len: the minimum free size to allocate
  336. *
  337. * Grow the available space of an XML buffer, @len is the target value
  338. * Error checking should be done on buf->error since using the return
  339. * value doesn't work that well
  340. *
  341. * Returns 0 in case of error or the length made available otherwise
  342. */
  343. static size_t
  344. xmlBufGrowInternal(xmlBufPtr buf, size_t len) {
  345. size_t size;
  346. xmlChar *newbuf;
  347. if ((buf == NULL) || (buf->error != 0)) return(0);
  348. CHECK_COMPAT(buf)
  349. if (len < buf->size - buf->use)
  350. return(buf->size - buf->use - 1);
  351. if (len >= SIZE_MAX - buf->use) {
  352. xmlBufMemoryError(buf, "growing buffer past SIZE_MAX");
  353. return(0);
  354. }
  355. if (buf->size > (size_t) len) {
  356. size = buf->size > SIZE_MAX / 2 ? SIZE_MAX : buf->size * 2;
  357. } else {
  358. size = buf->use + len;
  359. size = size > SIZE_MAX - 100 ? SIZE_MAX : size + 100;
  360. }
  361. if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
  362. /*
  363. * Used to provide parsing limits
  364. */
  365. if ((buf->use + len + 1 >= XML_MAX_TEXT_LENGTH) ||
  366. (buf->size >= XML_MAX_TEXT_LENGTH)) {
  367. xmlBufMemoryError(buf, "buffer error: text too long\n");
  368. return(0);
  369. }
  370. if (size >= XML_MAX_TEXT_LENGTH)
  371. size = XML_MAX_TEXT_LENGTH;
  372. }
  373. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  374. size_t start_buf = buf->content - buf->contentIO;
  375. newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
  376. if (newbuf == NULL) {
  377. xmlBufMemoryError(buf, "growing buffer");
  378. return(0);
  379. }
  380. buf->contentIO = newbuf;
  381. buf->content = newbuf + start_buf;
  382. } else {
  383. newbuf = (xmlChar *) xmlRealloc(buf->content, size);
  384. if (newbuf == NULL) {
  385. xmlBufMemoryError(buf, "growing buffer");
  386. return(0);
  387. }
  388. buf->content = newbuf;
  389. }
  390. buf->size = size;
  391. UPDATE_COMPAT(buf)
  392. return(buf->size - buf->use - 1);
  393. }
  394. /**
  395. * xmlBufGrow:
  396. * @buf: the buffer
  397. * @len: the minimum free size to allocate
  398. *
  399. * Grow the available space of an XML buffer, @len is the target value
  400. * This is been kept compatible with xmlBufferGrow() as much as possible
  401. *
  402. * Returns -1 in case of error or the length made available otherwise
  403. */
  404. int
  405. xmlBufGrow(xmlBufPtr buf, int len) {
  406. size_t ret;
  407. if ((buf == NULL) || (len < 0)) return(-1);
  408. if (len == 0)
  409. return(0);
  410. ret = xmlBufGrowInternal(buf, len);
  411. if (buf->error != 0)
  412. return(-1);
  413. return(ret > INT_MAX ? INT_MAX : ret);
  414. }
  415. /**
  416. * xmlBufDump:
  417. * @file: the file output
  418. * @buf: the buffer to dump
  419. *
  420. * Dumps an XML buffer to a FILE *.
  421. * Returns the number of #xmlChar written
  422. */
  423. size_t
  424. xmlBufDump(FILE *file, xmlBufPtr buf) {
  425. size_t ret;
  426. if ((buf == NULL) || (buf->error != 0)) {
  427. return(0);
  428. }
  429. if (buf->content == NULL) {
  430. return(0);
  431. }
  432. CHECK_COMPAT(buf)
  433. if (file == NULL)
  434. file = stdout;
  435. ret = fwrite(buf->content, 1, buf->use, file);
  436. return(ret);
  437. }
  438. /**
  439. * xmlBufContent:
  440. * @buf: the buffer
  441. *
  442. * Function to extract the content of a buffer
  443. *
  444. * Returns the internal content
  445. */
  446. xmlChar *
  447. xmlBufContent(const xmlBuf *buf)
  448. {
  449. if ((!buf) || (buf->error))
  450. return NULL;
  451. return(buf->content);
  452. }
  453. /**
  454. * xmlBufEnd:
  455. * @buf: the buffer
  456. *
  457. * Function to extract the end of the content of a buffer
  458. *
  459. * Returns the end of the internal content or NULL in case of error
  460. */
  461. xmlChar *
  462. xmlBufEnd(xmlBufPtr buf)
  463. {
  464. if ((!buf) || (buf->error))
  465. return NULL;
  466. CHECK_COMPAT(buf)
  467. return(&buf->content[buf->use]);
  468. }
  469. /**
  470. * xmlBufAddLen:
  471. * @buf: the buffer
  472. * @len: the size which were added at the end
  473. *
  474. * Sometime data may be added at the end of the buffer without
  475. * using the xmlBuf APIs that is used to expand the used space
  476. * and set the zero terminating at the end of the buffer
  477. *
  478. * Returns -1 in case of error and 0 otherwise
  479. */
  480. int
  481. xmlBufAddLen(xmlBufPtr buf, size_t len) {
  482. if ((buf == NULL) || (buf->error))
  483. return(-1);
  484. CHECK_COMPAT(buf)
  485. if (len >= (buf->size - buf->use))
  486. return(-1);
  487. buf->use += len;
  488. buf->content[buf->use] = 0;
  489. UPDATE_COMPAT(buf)
  490. return(0);
  491. }
  492. /**
  493. * xmlBufLength:
  494. * @buf: the buffer
  495. *
  496. * Function to get the length of a buffer
  497. *
  498. * Returns the length of data in the internal content
  499. */
  500. size_t
  501. xmlBufLength(const xmlBufPtr buf)
  502. {
  503. if ((!buf) || (buf->error))
  504. return 0;
  505. CHECK_COMPAT(buf)
  506. return(buf->use);
  507. }
  508. /**
  509. * xmlBufUse:
  510. * @buf: the buffer
  511. *
  512. * Function to get the length of a buffer
  513. *
  514. * Returns the length of data in the internal content
  515. */
  516. size_t
  517. xmlBufUse(const xmlBufPtr buf)
  518. {
  519. if ((!buf) || (buf->error))
  520. return 0;
  521. CHECK_COMPAT(buf)
  522. return(buf->use);
  523. }
  524. /**
  525. * xmlBufAvail:
  526. * @buf: the buffer
  527. *
  528. * Function to find how much free space is allocated but not
  529. * used in the buffer. It reserves one byte for the NUL
  530. * terminator character that is usually needed, so there is
  531. * no need to subtract 1 from the result anymore.
  532. *
  533. * Returns the amount, or 0 if none or if an error occurred.
  534. */
  535. size_t
  536. xmlBufAvail(const xmlBufPtr buf)
  537. {
  538. if ((!buf) || (buf->error))
  539. return 0;
  540. CHECK_COMPAT(buf)
  541. return((buf->size > buf->use) ? (buf->size - buf->use - 1) : 0);
  542. }
  543. /**
  544. * xmlBufIsEmpty:
  545. * @buf: the buffer
  546. *
  547. * Tell if a buffer is empty
  548. *
  549. * Returns 0 if no, 1 if yes and -1 in case of error
  550. */
  551. int
  552. xmlBufIsEmpty(const xmlBufPtr buf)
  553. {
  554. if ((!buf) || (buf->error))
  555. return(-1);
  556. CHECK_COMPAT(buf)
  557. return(buf->use == 0);
  558. }
  559. /**
  560. * xmlBufResize:
  561. * @buf: the buffer to resize
  562. * @size: the desired size
  563. *
  564. * Resize a buffer to accommodate minimum size of @size.
  565. *
  566. * Returns 0 in case of problems, 1 otherwise
  567. */
  568. int
  569. xmlBufResize(xmlBufPtr buf, size_t size)
  570. {
  571. size_t newSize;
  572. xmlChar* rebuf = NULL;
  573. size_t start_buf;
  574. if ((buf == NULL) || (buf->error))
  575. return(0);
  576. CHECK_COMPAT(buf)
  577. if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
  578. /*
  579. * Used to provide parsing limits
  580. */
  581. if (size >= XML_MAX_TEXT_LENGTH) {
  582. xmlBufMemoryError(buf, "buffer error: text too long\n");
  583. return(0);
  584. }
  585. }
  586. /* Don't resize if we don't have to */
  587. if (size < buf->size)
  588. return 1;
  589. /* figure out new size */
  590. switch (buf->alloc){
  591. case XML_BUFFER_ALLOC_IO:
  592. case XML_BUFFER_ALLOC_DOUBLEIT:
  593. /*take care of empty case*/
  594. if (buf->size == 0) {
  595. newSize = (size > SIZE_MAX - 10 ? SIZE_MAX : size + 10);
  596. } else {
  597. newSize = buf->size;
  598. }
  599. while (size > newSize) {
  600. if (newSize > SIZE_MAX / 2) {
  601. xmlBufMemoryError(buf, "growing buffer");
  602. return 0;
  603. }
  604. newSize *= 2;
  605. }
  606. break;
  607. case XML_BUFFER_ALLOC_EXACT:
  608. newSize = (size > SIZE_MAX - 10 ? SIZE_MAX : size + 10);
  609. break;
  610. case XML_BUFFER_ALLOC_HYBRID:
  611. if (buf->use < BASE_BUFFER_SIZE)
  612. newSize = size;
  613. else {
  614. newSize = buf->size;
  615. while (size > newSize) {
  616. if (newSize > SIZE_MAX / 2) {
  617. xmlBufMemoryError(buf, "growing buffer");
  618. return 0;
  619. }
  620. newSize *= 2;
  621. }
  622. }
  623. break;
  624. default:
  625. newSize = (size > SIZE_MAX - 10 ? SIZE_MAX : size + 10);
  626. break;
  627. }
  628. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  629. start_buf = buf->content - buf->contentIO;
  630. if (start_buf > newSize) {
  631. /* move data back to start */
  632. memmove(buf->contentIO, buf->content, buf->use);
  633. buf->content = buf->contentIO;
  634. buf->content[buf->use] = 0;
  635. buf->size += start_buf;
  636. } else {
  637. rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
  638. if (rebuf == NULL) {
  639. xmlBufMemoryError(buf, "growing buffer");
  640. return 0;
  641. }
  642. buf->contentIO = rebuf;
  643. buf->content = rebuf + start_buf;
  644. }
  645. } else {
  646. if (buf->content == NULL) {
  647. rebuf = (xmlChar *) xmlMallocAtomic(newSize);
  648. buf->use = 0;
  649. if (rebuf != NULL)
  650. rebuf[buf->use] = 0;
  651. } else if (buf->size - buf->use < 100) {
  652. rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
  653. } else {
  654. /*
  655. * if we are reallocating a buffer far from being full, it's
  656. * better to make a new allocation and copy only the used range
  657. * and free the old one.
  658. */
  659. rebuf = (xmlChar *) xmlMallocAtomic(newSize);
  660. if (rebuf != NULL) {
  661. memcpy(rebuf, buf->content, buf->use);
  662. xmlFree(buf->content);
  663. rebuf[buf->use] = 0;
  664. }
  665. }
  666. if (rebuf == NULL) {
  667. xmlBufMemoryError(buf, "growing buffer");
  668. return 0;
  669. }
  670. buf->content = rebuf;
  671. }
  672. buf->size = newSize;
  673. UPDATE_COMPAT(buf)
  674. return 1;
  675. }
  676. /**
  677. * xmlBufAdd:
  678. * @buf: the buffer to dump
  679. * @str: the #xmlChar string
  680. * @len: the number of #xmlChar to add
  681. *
  682. * Add a string range to an XML buffer. if len == -1, the length of
  683. * str is recomputed.
  684. *
  685. * Returns 0 successful, a positive error code number otherwise
  686. * and -1 in case of internal or API error.
  687. */
  688. int
  689. xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len) {
  690. size_t needSize;
  691. if ((str == NULL) || (buf == NULL) || (buf->error))
  692. return -1;
  693. CHECK_COMPAT(buf)
  694. if (len < -1) {
  695. return -1;
  696. }
  697. if (len == 0) return 0;
  698. if (len < 0)
  699. len = xmlStrlen(str);
  700. if (len < 0) return -1;
  701. if (len == 0) return 0;
  702. /* Note that both buf->size and buf->use can be zero here. */
  703. if ((size_t) len >= buf->size - buf->use) {
  704. if ((size_t) len >= SIZE_MAX - buf->use) {
  705. xmlBufMemoryError(buf, "growing buffer past SIZE_MAX");
  706. return(-1);
  707. }
  708. needSize = buf->use + len + 1;
  709. if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
  710. /*
  711. * Used to provide parsing limits
  712. */
  713. if (needSize >= XML_MAX_TEXT_LENGTH) {
  714. xmlBufMemoryError(buf, "buffer error: text too long\n");
  715. return(-1);
  716. }
  717. }
  718. if (!xmlBufResize(buf, needSize)){
  719. xmlBufMemoryError(buf, "growing buffer");
  720. return XML_ERR_NO_MEMORY;
  721. }
  722. }
  723. memmove(&buf->content[buf->use], str, len);
  724. buf->use += len;
  725. buf->content[buf->use] = 0;
  726. UPDATE_COMPAT(buf)
  727. return 0;
  728. }
  729. /**
  730. * xmlBufCat:
  731. * @buf: the buffer to add to
  732. * @str: the #xmlChar string
  733. *
  734. * Append a zero terminated string to an XML buffer.
  735. *
  736. * Returns 0 successful, a positive error code number otherwise
  737. * and -1 in case of internal or API error.
  738. */
  739. int
  740. xmlBufCat(xmlBufPtr buf, const xmlChar *str) {
  741. if ((buf == NULL) || (buf->error))
  742. return(-1);
  743. CHECK_COMPAT(buf)
  744. if (str == NULL) return -1;
  745. return xmlBufAdd(buf, str, -1);
  746. }
  747. /**
  748. * xmlBufCCat:
  749. * @buf: the buffer to dump
  750. * @str: the C char string
  751. *
  752. * Append a zero terminated C string to an XML buffer.
  753. *
  754. * Returns 0 successful, a positive error code number otherwise
  755. * and -1 in case of internal or API error.
  756. */
  757. int
  758. xmlBufCCat(xmlBufPtr buf, const char *str) {
  759. return xmlBufCat(buf, (const xmlChar *) str);
  760. }
  761. /**
  762. * xmlBufWriteQuotedString:
  763. * @buf: the XML buffer output
  764. * @string: the string to add
  765. *
  766. * routine which manage and grows an output buffer. This one writes
  767. * a quoted or double quoted #xmlChar string, checking first if it holds
  768. * quote or double-quotes internally
  769. *
  770. * Returns 0 if successful, a positive error code number otherwise
  771. * and -1 in case of internal or API error.
  772. */
  773. int
  774. xmlBufWriteQuotedString(xmlBufPtr buf, const xmlChar *string) {
  775. const xmlChar *cur, *base;
  776. if ((buf == NULL) || (buf->error))
  777. return(-1);
  778. CHECK_COMPAT(buf)
  779. if (xmlStrchr(string, '\"')) {
  780. if (xmlStrchr(string, '\'')) {
  781. xmlBufCCat(buf, "\"");
  782. base = cur = string;
  783. while(*cur != 0){
  784. if(*cur == '"'){
  785. if (base != cur)
  786. xmlBufAdd(buf, base, cur - base);
  787. xmlBufAdd(buf, BAD_CAST "&quot;", 6);
  788. cur++;
  789. base = cur;
  790. }
  791. else {
  792. cur++;
  793. }
  794. }
  795. if (base != cur)
  796. xmlBufAdd(buf, base, cur - base);
  797. xmlBufCCat(buf, "\"");
  798. }
  799. else{
  800. xmlBufCCat(buf, "\'");
  801. xmlBufCat(buf, string);
  802. xmlBufCCat(buf, "\'");
  803. }
  804. } else {
  805. xmlBufCCat(buf, "\"");
  806. xmlBufCat(buf, string);
  807. xmlBufCCat(buf, "\"");
  808. }
  809. return(0);
  810. }
  811. /**
  812. * xmlBufFromBuffer:
  813. * @buffer: incoming old buffer to convert to a new one
  814. *
  815. * Helper routine to switch from the old buffer structures in use
  816. * in various APIs. It creates a wrapper xmlBufPtr which will be
  817. * used for internal processing until the xmlBufBackToBuffer() is
  818. * issued.
  819. *
  820. * Returns a new xmlBufPtr unless the call failed and NULL is returned
  821. */
  822. xmlBufPtr
  823. xmlBufFromBuffer(xmlBufferPtr buffer) {
  824. xmlBufPtr ret;
  825. if (buffer == NULL)
  826. return(NULL);
  827. ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
  828. if (ret == NULL) {
  829. xmlBufMemoryError(NULL, "creating buffer");
  830. return(NULL);
  831. }
  832. ret->use = buffer->use;
  833. ret->size = buffer->size;
  834. UPDATE_COMPAT(ret);
  835. ret->error = 0;
  836. ret->buffer = buffer;
  837. ret->alloc = buffer->alloc;
  838. ret->content = buffer->content;
  839. ret->contentIO = buffer->contentIO;
  840. return(ret);
  841. }
  842. /**
  843. * xmlBufBackToBuffer:
  844. * @buf: new buffer wrapping the old one
  845. *
  846. * Function to be called once internal processing had been done to
  847. * update back the buffer provided by the user. This can lead to
  848. * a failure in case the size accumulated in the xmlBuf is larger
  849. * than what an xmlBuffer can support on 64 bits (INT_MAX)
  850. * The xmlBufPtr @buf wrapper is deallocated by this call in any case.
  851. *
  852. * Returns the old xmlBufferPtr unless the call failed and NULL is returned
  853. */
  854. xmlBufferPtr
  855. xmlBufBackToBuffer(xmlBufPtr buf) {
  856. xmlBufferPtr ret;
  857. if (buf == NULL)
  858. return(NULL);
  859. CHECK_COMPAT(buf)
  860. if ((buf->error) || (buf->buffer == NULL)) {
  861. xmlBufFree(buf);
  862. return(NULL);
  863. }
  864. ret = buf->buffer;
  865. /*
  866. * What to do in case of error in the buffer ???
  867. */
  868. if (buf->use > INT_MAX) {
  869. /*
  870. * Worse case, we really allocated and used more than the
  871. * maximum allowed memory for an xmlBuffer on this architecture.
  872. * Keep the buffer but provide a truncated size value.
  873. */
  874. xmlBufOverflowError(buf, "Used size too big for xmlBuffer");
  875. ret->use = INT_MAX;
  876. ret->size = INT_MAX;
  877. } else if (buf->size > INT_MAX) {
  878. /*
  879. * milder case, we allocated more than the maximum allowed memory
  880. * for an xmlBuffer on this architecture, but used less than the
  881. * limit.
  882. * Keep the buffer but provide a truncated size value.
  883. */
  884. xmlBufOverflowError(buf, "Allocated size too big for xmlBuffer");
  885. ret->use = buf->use;
  886. ret->size = INT_MAX;
  887. } else {
  888. ret->use = buf->use;
  889. ret->size = buf->size;
  890. }
  891. ret->alloc = buf->alloc;
  892. ret->content = buf->content;
  893. ret->contentIO = buf->contentIO;
  894. xmlFree(buf);
  895. return(ret);
  896. }
  897. /**
  898. * xmlBufMergeBuffer:
  899. * @buf: an xmlBufPtr
  900. * @buffer: the buffer to consume into @buf
  901. *
  902. * The content of @buffer is appended to @buf and @buffer is freed
  903. *
  904. * Returns -1 in case of error, 0 otherwise, in any case @buffer is freed
  905. */
  906. int
  907. xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer) {
  908. int ret = 0;
  909. if ((buf == NULL) || (buf->error)) {
  910. xmlBufferFree(buffer);
  911. return(-1);
  912. }
  913. CHECK_COMPAT(buf)
  914. if ((buffer != NULL) && (buffer->content != NULL) &&
  915. (buffer->use > 0)) {
  916. ret = xmlBufAdd(buf, buffer->content, buffer->use);
  917. }
  918. xmlBufferFree(buffer);
  919. return(ret);
  920. }
  921. /**
  922. * xmlBufResetInput:
  923. * @buf: an xmlBufPtr
  924. * @input: an xmlParserInputPtr
  925. *
  926. * Update the input to use the current set of pointers from the buffer.
  927. *
  928. * Returns -1 in case of error, 0 otherwise
  929. */
  930. int
  931. xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
  932. if (input == NULL)
  933. return(-1);
  934. if ((buf == NULL) || (buf->error)) {
  935. input->base = input->cur = input->end = BAD_CAST "";
  936. return(-1);
  937. }
  938. CHECK_COMPAT(buf)
  939. input->base = input->cur = buf->content;
  940. input->end = &buf->content[buf->use];
  941. return(0);
  942. }
  943. /**
  944. * xmlBufUpdateInput:
  945. * @buf: an xmlBufPtr
  946. * @input: an xmlParserInputPtr
  947. * @pos: the cur value relative to the beginning of the buffer
  948. *
  949. * Update the input to use the base and cur relative to the buffer
  950. * after a possible reallocation of its content
  951. *
  952. * Returns -1 in case of error, 0 otherwise
  953. */
  954. int
  955. xmlBufUpdateInput(xmlBufPtr buf, xmlParserInputPtr input, size_t pos) {
  956. if (input == NULL)
  957. return(-1);
  958. /*
  959. * TODO: It might be safer to keep using the buffer content if there
  960. * was an error.
  961. */
  962. if ((buf == NULL) || (buf->error)) {
  963. input->base = input->cur = input->end = BAD_CAST "";
  964. return(-1);
  965. }
  966. CHECK_COMPAT(buf)
  967. input->base = buf->content;
  968. input->cur = input->base + pos;
  969. input->end = &buf->content[buf->use];
  970. return(0);
  971. }