buf.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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. #ifdef HAVE_CTYPE_H
  19. #include <ctype.h>
  20. #endif
  21. #ifdef HAVE_STDLIB_H
  22. #include <stdlib.h>
  23. #endif
  24. #include <libxml/tree.h>
  25. #include <libxml/globals.h>
  26. #include <libxml/tree.h>
  27. #include <libxml/parserInternals.h> /* for XML_MAX_TEXT_LENGTH */
  28. #include "buf.h"
  29. #define WITH_BUFFER_COMPAT
  30. /**
  31. * xmlBuf:
  32. *
  33. * A buffer structure. The base of the structure is somehow compatible
  34. * with struct _xmlBuffer to limit risks on application which accessed
  35. * directly the input->buf->buffer structures.
  36. */
  37. struct _xmlBuf {
  38. xmlChar *content; /* The buffer content UTF8 */
  39. unsigned int compat_use; /* for binary compatibility */
  40. unsigned int compat_size; /* for binary compatibility */
  41. xmlBufferAllocationScheme alloc; /* The realloc method */
  42. xmlChar *contentIO; /* in IO mode we may have a different base */
  43. size_t use; /* The buffer size used */
  44. size_t size; /* The buffer size */
  45. xmlBufferPtr buffer; /* wrapper for an old buffer */
  46. int error; /* an error code if a failure occurred */
  47. };
  48. #ifdef WITH_BUFFER_COMPAT
  49. /*
  50. * Macro for compatibility with xmlBuffer to be used after an xmlBuf
  51. * is updated. This makes sure the compat fields are updated too.
  52. */
  53. #define UPDATE_COMPAT(buf) \
  54. if (buf->size < INT_MAX) buf->compat_size = buf->size; \
  55. else buf->compat_size = INT_MAX; \
  56. if (buf->use < INT_MAX) buf->compat_use = buf->use; \
  57. else buf->compat_use = INT_MAX;
  58. /*
  59. * Macro for compatibility with xmlBuffer to be used in all the xmlBuf
  60. * entry points, it checks that the compat fields have not been modified
  61. * by direct call to xmlBuffer function from code compiled before 2.9.0 .
  62. */
  63. #define CHECK_COMPAT(buf) \
  64. if (buf->size != (size_t) buf->compat_size) \
  65. if (buf->compat_size < INT_MAX) \
  66. buf->size = buf->compat_size; \
  67. if (buf->use != (size_t) buf->compat_use) \
  68. if (buf->compat_use < INT_MAX) \
  69. buf->use = buf->compat_use;
  70. #else /* ! WITH_BUFFER_COMPAT */
  71. #define UPDATE_COMPAT(buf)
  72. #define CHECK_COMPAT(buf)
  73. #endif /* WITH_BUFFER_COMPAT */
  74. /**
  75. * xmlBufMemoryError:
  76. * @extra: extra information
  77. *
  78. * Handle an out of memory condition
  79. * To be improved...
  80. */
  81. static void
  82. xmlBufMemoryError(xmlBufPtr buf, const char *extra)
  83. {
  84. __xmlSimpleError(XML_FROM_BUFFER, XML_ERR_NO_MEMORY, NULL, NULL, extra);
  85. if ((buf) && (buf->error == 0))
  86. buf->error = XML_ERR_NO_MEMORY;
  87. }
  88. /**
  89. * xmlBufOverflowError:
  90. * @extra: extra information
  91. *
  92. * Handle a buffer overflow error
  93. * To be improved...
  94. */
  95. static void
  96. xmlBufOverflowError(xmlBufPtr buf, const char *extra)
  97. {
  98. __xmlSimpleError(XML_FROM_BUFFER, XML_BUF_OVERFLOW, NULL, NULL, extra);
  99. if ((buf) && (buf->error == 0))
  100. buf->error = XML_BUF_OVERFLOW;
  101. }
  102. /**
  103. * xmlBufCreate:
  104. *
  105. * routine to create an XML buffer.
  106. * returns the new structure.
  107. */
  108. xmlBufPtr
  109. xmlBufCreate(void) {
  110. xmlBufPtr ret;
  111. ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
  112. if (ret == NULL) {
  113. xmlBufMemoryError(NULL, "creating buffer");
  114. return(NULL);
  115. }
  116. ret->compat_use = 0;
  117. ret->use = 0;
  118. ret->error = 0;
  119. ret->buffer = NULL;
  120. ret->size = xmlDefaultBufferSize;
  121. ret->compat_size = xmlDefaultBufferSize;
  122. ret->alloc = xmlBufferAllocScheme;
  123. ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
  124. if (ret->content == NULL) {
  125. xmlBufMemoryError(ret, "creating buffer");
  126. xmlFree(ret);
  127. return(NULL);
  128. }
  129. ret->content[0] = 0;
  130. ret->contentIO = NULL;
  131. return(ret);
  132. }
  133. /**
  134. * xmlBufCreateSize:
  135. * @size: initial size of buffer
  136. *
  137. * routine to create an XML buffer.
  138. * returns the new structure.
  139. */
  140. xmlBufPtr
  141. xmlBufCreateSize(size_t size) {
  142. xmlBufPtr ret;
  143. ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
  144. if (ret == NULL) {
  145. xmlBufMemoryError(NULL, "creating buffer");
  146. return(NULL);
  147. }
  148. ret->compat_use = 0;
  149. ret->use = 0;
  150. ret->error = 0;
  151. ret->buffer = NULL;
  152. ret->alloc = xmlBufferAllocScheme;
  153. ret->size = (size ? size+2 : 0); /* +1 for ending null */
  154. ret->compat_size = (int) ret->size;
  155. if (ret->size){
  156. ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
  157. if (ret->content == NULL) {
  158. xmlBufMemoryError(ret, "creating buffer");
  159. xmlFree(ret);
  160. return(NULL);
  161. }
  162. ret->content[0] = 0;
  163. } else
  164. ret->content = NULL;
  165. ret->contentIO = NULL;
  166. return(ret);
  167. }
  168. /**
  169. * xmlBufDetach:
  170. * @buf: the buffer
  171. *
  172. * Remove the string contained in a buffer and give it back to the
  173. * caller. The buffer is reset to an empty content.
  174. * This doesn't work with immutable buffers as they can't be reset.
  175. *
  176. * Returns the previous string contained by the buffer.
  177. */
  178. xmlChar *
  179. xmlBufDetach(xmlBufPtr buf) {
  180. xmlChar *ret;
  181. if (buf == NULL)
  182. return(NULL);
  183. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
  184. return(NULL);
  185. if (buf->buffer != NULL)
  186. return(NULL);
  187. if (buf->error)
  188. return(NULL);
  189. ret = buf->content;
  190. buf->content = NULL;
  191. buf->size = 0;
  192. buf->use = 0;
  193. buf->compat_use = 0;
  194. buf->compat_size = 0;
  195. return ret;
  196. }
  197. /**
  198. * xmlBufCreateStatic:
  199. * @mem: the memory area
  200. * @size: the size in byte
  201. *
  202. * routine to create an XML buffer from an immutable memory area.
  203. * The area won't be modified nor copied, and is expected to be
  204. * present until the end of the buffer lifetime.
  205. *
  206. * returns the new structure.
  207. */
  208. xmlBufPtr
  209. xmlBufCreateStatic(void *mem, size_t size) {
  210. xmlBufPtr ret;
  211. if (mem == NULL)
  212. return(NULL);
  213. ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
  214. if (ret == NULL) {
  215. xmlBufMemoryError(NULL, "creating buffer");
  216. return(NULL);
  217. }
  218. if (size < INT_MAX) {
  219. ret->compat_use = size;
  220. ret->compat_size = size;
  221. } else {
  222. ret->compat_use = INT_MAX;
  223. ret->compat_size = INT_MAX;
  224. }
  225. ret->use = size;
  226. ret->size = size;
  227. ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
  228. ret->content = (xmlChar *) mem;
  229. ret->error = 0;
  230. ret->buffer = NULL;
  231. return(ret);
  232. }
  233. /**
  234. * xmlBufGetAllocationScheme:
  235. * @buf: the buffer
  236. *
  237. * Get the buffer allocation scheme
  238. *
  239. * Returns the scheme or -1 in case of error
  240. */
  241. int
  242. xmlBufGetAllocationScheme(xmlBufPtr buf) {
  243. if (buf == NULL) {
  244. #ifdef DEBUG_BUFFER
  245. xmlGenericError(xmlGenericErrorContext,
  246. "xmlBufGetAllocationScheme: buf == NULL\n");
  247. #endif
  248. return(-1);
  249. }
  250. return(buf->alloc);
  251. }
  252. /**
  253. * xmlBufSetAllocationScheme:
  254. * @buf: the buffer to tune
  255. * @scheme: allocation scheme to use
  256. *
  257. * Sets the allocation scheme for this buffer
  258. *
  259. * returns 0 in case of success and -1 in case of failure
  260. */
  261. int
  262. xmlBufSetAllocationScheme(xmlBufPtr buf,
  263. xmlBufferAllocationScheme scheme) {
  264. if ((buf == NULL) || (buf->error != 0)) {
  265. #ifdef DEBUG_BUFFER
  266. xmlGenericError(xmlGenericErrorContext,
  267. "xmlBufSetAllocationScheme: buf == NULL or in error\n");
  268. #endif
  269. return(-1);
  270. }
  271. if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
  272. (buf->alloc == XML_BUFFER_ALLOC_IO))
  273. return(-1);
  274. if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
  275. (scheme == XML_BUFFER_ALLOC_EXACT) ||
  276. (scheme == XML_BUFFER_ALLOC_HYBRID) ||
  277. (scheme == XML_BUFFER_ALLOC_IMMUTABLE) ||
  278. (scheme == XML_BUFFER_ALLOC_BOUNDED)) {
  279. buf->alloc = scheme;
  280. if (buf->buffer)
  281. buf->buffer->alloc = scheme;
  282. return(0);
  283. }
  284. /*
  285. * Switching a buffer ALLOC_IO has the side effect of initializing
  286. * the contentIO field with the current content
  287. */
  288. if (scheme == XML_BUFFER_ALLOC_IO) {
  289. buf->alloc = XML_BUFFER_ALLOC_IO;
  290. buf->contentIO = buf->content;
  291. }
  292. return(-1);
  293. }
  294. /**
  295. * xmlBufFree:
  296. * @buf: the buffer to free
  297. *
  298. * Frees an XML buffer. It frees both the content and the structure which
  299. * encapsulate it.
  300. */
  301. void
  302. xmlBufFree(xmlBufPtr buf) {
  303. if (buf == NULL) {
  304. #ifdef DEBUG_BUFFER
  305. xmlGenericError(xmlGenericErrorContext,
  306. "xmlBufFree: buf == NULL\n");
  307. #endif
  308. return;
  309. }
  310. if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
  311. (buf->contentIO != NULL)) {
  312. xmlFree(buf->contentIO);
  313. } else if ((buf->content != NULL) &&
  314. (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
  315. xmlFree(buf->content);
  316. }
  317. xmlFree(buf);
  318. }
  319. /**
  320. * xmlBufEmpty:
  321. * @buf: the buffer
  322. *
  323. * empty a buffer.
  324. */
  325. void
  326. xmlBufEmpty(xmlBufPtr buf) {
  327. if ((buf == NULL) || (buf->error != 0)) return;
  328. if (buf->content == NULL) return;
  329. CHECK_COMPAT(buf)
  330. buf->use = 0;
  331. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
  332. buf->content = BAD_CAST "";
  333. } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
  334. (buf->contentIO != NULL)) {
  335. size_t start_buf = buf->content - buf->contentIO;
  336. buf->size += start_buf;
  337. buf->content = buf->contentIO;
  338. buf->content[0] = 0;
  339. } else {
  340. buf->content[0] = 0;
  341. }
  342. UPDATE_COMPAT(buf)
  343. }
  344. /**
  345. * xmlBufShrink:
  346. * @buf: the buffer to dump
  347. * @len: the number of xmlChar to remove
  348. *
  349. * Remove the beginning of an XML buffer.
  350. * NOTE that this routine behaviour differs from xmlBufferShrink()
  351. * as it will return 0 on error instead of -1 due to size_t being
  352. * used as the return type.
  353. *
  354. * Returns the number of byte removed or 0 in case of failure
  355. */
  356. size_t
  357. xmlBufShrink(xmlBufPtr buf, size_t len) {
  358. if ((buf == NULL) || (buf->error != 0)) return(0);
  359. CHECK_COMPAT(buf)
  360. if (len == 0) return(0);
  361. if (len > buf->use) return(0);
  362. buf->use -= len;
  363. if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
  364. ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
  365. /*
  366. * we just move the content pointer, but also make sure
  367. * the perceived buffer size has shrunk accordingly
  368. */
  369. buf->content += len;
  370. buf->size -= len;
  371. /*
  372. * sometimes though it maybe be better to really shrink
  373. * on IO buffers
  374. */
  375. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  376. size_t start_buf = buf->content - buf->contentIO;
  377. if (start_buf >= buf->size) {
  378. memmove(buf->contentIO, &buf->content[0], buf->use);
  379. buf->content = buf->contentIO;
  380. buf->content[buf->use] = 0;
  381. buf->size += start_buf;
  382. }
  383. }
  384. } else {
  385. memmove(buf->content, &buf->content[len], buf->use);
  386. buf->content[buf->use] = 0;
  387. }
  388. UPDATE_COMPAT(buf)
  389. return(len);
  390. }
  391. /**
  392. * xmlBufGrowInternal:
  393. * @buf: the buffer
  394. * @len: the minimum free size to allocate
  395. *
  396. * Grow the available space of an XML buffer, @len is the target value
  397. * Error checking should be done on buf->error since using the return
  398. * value doesn't work that well
  399. *
  400. * Returns 0 in case of error or the length made available otherwise
  401. */
  402. static size_t
  403. xmlBufGrowInternal(xmlBufPtr buf, size_t len) {
  404. size_t size;
  405. xmlChar *newbuf;
  406. if ((buf == NULL) || (buf->error != 0)) return(0);
  407. CHECK_COMPAT(buf)
  408. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
  409. if (buf->use + len < buf->size)
  410. return(buf->size - buf->use);
  411. /*
  412. * Windows has a BIG problem on realloc timing, so we try to double
  413. * the buffer size (if that's enough) (bug 146697)
  414. * Apparently BSD too, and it's probably best for linux too
  415. * On an embedded system this may be something to change
  416. */
  417. #if 1
  418. if (buf->size > (size_t) len)
  419. size = buf->size * 2;
  420. else
  421. size = buf->use + len + 100;
  422. #else
  423. size = buf->use + len + 100;
  424. #endif
  425. if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
  426. /*
  427. * Used to provide parsing limits
  428. */
  429. if ((buf->use + len >= XML_MAX_TEXT_LENGTH) ||
  430. (buf->size >= XML_MAX_TEXT_LENGTH)) {
  431. xmlBufMemoryError(buf, "buffer error: text too long\n");
  432. return(0);
  433. }
  434. if (size >= XML_MAX_TEXT_LENGTH)
  435. size = XML_MAX_TEXT_LENGTH;
  436. }
  437. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  438. size_t start_buf = buf->content - buf->contentIO;
  439. newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
  440. if (newbuf == NULL) {
  441. xmlBufMemoryError(buf, "growing buffer");
  442. return(0);
  443. }
  444. buf->contentIO = newbuf;
  445. buf->content = newbuf + start_buf;
  446. } else {
  447. newbuf = (xmlChar *) xmlRealloc(buf->content, size);
  448. if (newbuf == NULL) {
  449. xmlBufMemoryError(buf, "growing buffer");
  450. return(0);
  451. }
  452. buf->content = newbuf;
  453. }
  454. buf->size = size;
  455. UPDATE_COMPAT(buf)
  456. return(buf->size - buf->use);
  457. }
  458. /**
  459. * xmlBufGrow:
  460. * @buf: the buffer
  461. * @len: the minimum free size to allocate
  462. *
  463. * Grow the available space of an XML buffer, @len is the target value
  464. * This is been kept compatible with xmlBufferGrow() as much as possible
  465. *
  466. * Returns -1 in case of error or the length made available otherwise
  467. */
  468. int
  469. xmlBufGrow(xmlBufPtr buf, int len) {
  470. size_t ret;
  471. if ((buf == NULL) || (len < 0)) return(-1);
  472. if (len == 0)
  473. return(0);
  474. ret = xmlBufGrowInternal(buf, len);
  475. if (buf->error != 0)
  476. return(-1);
  477. return((int) ret);
  478. }
  479. /**
  480. * xmlBufInflate:
  481. * @buf: the buffer
  482. * @len: the minimum extra free size to allocate
  483. *
  484. * Grow the available space of an XML buffer, adding at least @len bytes
  485. *
  486. * Returns 0 if successful or -1 in case of error
  487. */
  488. int
  489. xmlBufInflate(xmlBufPtr buf, size_t len) {
  490. if (buf == NULL) return(-1);
  491. xmlBufGrowInternal(buf, len + buf->size);
  492. if (buf->error)
  493. return(-1);
  494. return(0);
  495. }
  496. /**
  497. * xmlBufDump:
  498. * @file: the file output
  499. * @buf: the buffer to dump
  500. *
  501. * Dumps an XML buffer to a FILE *.
  502. * Returns the number of #xmlChar written
  503. */
  504. size_t
  505. xmlBufDump(FILE *file, xmlBufPtr buf) {
  506. size_t ret;
  507. if ((buf == NULL) || (buf->error != 0)) {
  508. #ifdef DEBUG_BUFFER
  509. xmlGenericError(xmlGenericErrorContext,
  510. "xmlBufDump: buf == NULL or in error\n");
  511. #endif
  512. return(0);
  513. }
  514. if (buf->content == NULL) {
  515. #ifdef DEBUG_BUFFER
  516. xmlGenericError(xmlGenericErrorContext,
  517. "xmlBufDump: buf->content == NULL\n");
  518. #endif
  519. return(0);
  520. }
  521. CHECK_COMPAT(buf)
  522. if (file == NULL)
  523. file = stdout;
  524. ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
  525. return(ret);
  526. }
  527. /**
  528. * xmlBufContent:
  529. * @buf: the buffer
  530. *
  531. * Function to extract the content of a buffer
  532. *
  533. * Returns the internal content
  534. */
  535. xmlChar *
  536. xmlBufContent(const xmlBuf *buf)
  537. {
  538. if ((!buf) || (buf->error))
  539. return NULL;
  540. return(buf->content);
  541. }
  542. /**
  543. * xmlBufEnd:
  544. * @buf: the buffer
  545. *
  546. * Function to extract the end of the content of a buffer
  547. *
  548. * Returns the end of the internal content or NULL in case of error
  549. */
  550. xmlChar *
  551. xmlBufEnd(xmlBufPtr buf)
  552. {
  553. if ((!buf) || (buf->error))
  554. return NULL;
  555. CHECK_COMPAT(buf)
  556. return(&buf->content[buf->use]);
  557. }
  558. /**
  559. * xmlBufAddLen:
  560. * @buf: the buffer
  561. * @len: the size which were added at the end
  562. *
  563. * Sometime data may be added at the end of the buffer without
  564. * using the xmlBuf APIs that is used to expand the used space
  565. * and set the zero terminating at the end of the buffer
  566. *
  567. * Returns -1 in case of error and 0 otherwise
  568. */
  569. int
  570. xmlBufAddLen(xmlBufPtr buf, size_t len) {
  571. if ((buf == NULL) || (buf->error))
  572. return(-1);
  573. CHECK_COMPAT(buf)
  574. if (len > (buf->size - buf->use))
  575. return(-1);
  576. buf->use += len;
  577. UPDATE_COMPAT(buf)
  578. if (buf->size > buf->use)
  579. buf->content[buf->use] = 0;
  580. else
  581. return(-1);
  582. return(0);
  583. }
  584. /**
  585. * xmlBufErase:
  586. * @buf: the buffer
  587. * @len: the size to erase at the end
  588. *
  589. * Sometime data need to be erased at the end of the buffer
  590. *
  591. * Returns -1 in case of error and 0 otherwise
  592. */
  593. int
  594. xmlBufErase(xmlBufPtr buf, size_t len) {
  595. if ((buf == NULL) || (buf->error))
  596. return(-1);
  597. CHECK_COMPAT(buf)
  598. if (len > buf->use)
  599. return(-1);
  600. buf->use -= len;
  601. buf->content[buf->use] = 0;
  602. UPDATE_COMPAT(buf)
  603. return(0);
  604. }
  605. /**
  606. * xmlBufLength:
  607. * @buf: the buffer
  608. *
  609. * Function to get the length of a buffer
  610. *
  611. * Returns the length of data in the internal content
  612. */
  613. size_t
  614. xmlBufLength(const xmlBufPtr buf)
  615. {
  616. if ((!buf) || (buf->error))
  617. return 0;
  618. CHECK_COMPAT(buf)
  619. return(buf->use);
  620. }
  621. /**
  622. * xmlBufUse:
  623. * @buf: the buffer
  624. *
  625. * Function to get the length of a buffer
  626. *
  627. * Returns the length of data in the internal content
  628. */
  629. size_t
  630. xmlBufUse(const xmlBufPtr buf)
  631. {
  632. if ((!buf) || (buf->error))
  633. return 0;
  634. CHECK_COMPAT(buf)
  635. return(buf->use);
  636. }
  637. /**
  638. * xmlBufAvail:
  639. * @buf: the buffer
  640. *
  641. * Function to find how much free space is allocated but not
  642. * used in the buffer. It does not account for the terminating zero
  643. * usually needed
  644. *
  645. * Returns the amount or 0 if none or an error occurred
  646. */
  647. size_t
  648. xmlBufAvail(const xmlBufPtr buf)
  649. {
  650. if ((!buf) || (buf->error))
  651. return 0;
  652. CHECK_COMPAT(buf)
  653. return(buf->size - buf->use);
  654. }
  655. /**
  656. * xmlBufIsEmpty:
  657. * @buf: the buffer
  658. *
  659. * Tell if a buffer is empty
  660. *
  661. * Returns 0 if no, 1 if yes and -1 in case of error
  662. */
  663. int
  664. xmlBufIsEmpty(const xmlBufPtr buf)
  665. {
  666. if ((!buf) || (buf->error))
  667. return(-1);
  668. CHECK_COMPAT(buf)
  669. return(buf->use == 0);
  670. }
  671. /**
  672. * xmlBufResize:
  673. * @buf: the buffer to resize
  674. * @size: the desired size
  675. *
  676. * Resize a buffer to accommodate minimum size of @size.
  677. *
  678. * Returns 0 in case of problems, 1 otherwise
  679. */
  680. int
  681. xmlBufResize(xmlBufPtr buf, size_t size)
  682. {
  683. unsigned int newSize;
  684. xmlChar* rebuf = NULL;
  685. size_t start_buf;
  686. if ((buf == NULL) || (buf->error))
  687. return(0);
  688. CHECK_COMPAT(buf)
  689. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
  690. if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
  691. /*
  692. * Used to provide parsing limits
  693. */
  694. if (size >= XML_MAX_TEXT_LENGTH) {
  695. xmlBufMemoryError(buf, "buffer error: text too long\n");
  696. return(0);
  697. }
  698. }
  699. /* Don't resize if we don't have to */
  700. if (size < buf->size)
  701. return 1;
  702. /* figure out new size */
  703. switch (buf->alloc){
  704. case XML_BUFFER_ALLOC_IO:
  705. case XML_BUFFER_ALLOC_DOUBLEIT:
  706. /*take care of empty case*/
  707. newSize = (buf->size ? buf->size*2 : size + 10);
  708. while (size > newSize) {
  709. if (newSize > UINT_MAX / 2) {
  710. xmlBufMemoryError(buf, "growing buffer");
  711. return 0;
  712. }
  713. newSize *= 2;
  714. }
  715. break;
  716. case XML_BUFFER_ALLOC_EXACT:
  717. newSize = size+10;
  718. break;
  719. case XML_BUFFER_ALLOC_HYBRID:
  720. if (buf->use < BASE_BUFFER_SIZE)
  721. newSize = size;
  722. else {
  723. newSize = buf->size * 2;
  724. while (size > newSize) {
  725. if (newSize > UINT_MAX / 2) {
  726. xmlBufMemoryError(buf, "growing buffer");
  727. return 0;
  728. }
  729. newSize *= 2;
  730. }
  731. }
  732. break;
  733. default:
  734. newSize = size+10;
  735. break;
  736. }
  737. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  738. start_buf = buf->content - buf->contentIO;
  739. if (start_buf > newSize) {
  740. /* move data back to start */
  741. memmove(buf->contentIO, buf->content, buf->use);
  742. buf->content = buf->contentIO;
  743. buf->content[buf->use] = 0;
  744. buf->size += start_buf;
  745. } else {
  746. rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
  747. if (rebuf == NULL) {
  748. xmlBufMemoryError(buf, "growing buffer");
  749. return 0;
  750. }
  751. buf->contentIO = rebuf;
  752. buf->content = rebuf + start_buf;
  753. }
  754. } else {
  755. if (buf->content == NULL) {
  756. rebuf = (xmlChar *) xmlMallocAtomic(newSize);
  757. } else if (buf->size - buf->use < 100) {
  758. rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
  759. } else {
  760. /*
  761. * if we are reallocating a buffer far from being full, it's
  762. * better to make a new allocation and copy only the used range
  763. * and free the old one.
  764. */
  765. rebuf = (xmlChar *) xmlMallocAtomic(newSize);
  766. if (rebuf != NULL) {
  767. memcpy(rebuf, buf->content, buf->use);
  768. xmlFree(buf->content);
  769. rebuf[buf->use] = 0;
  770. }
  771. }
  772. if (rebuf == NULL) {
  773. xmlBufMemoryError(buf, "growing buffer");
  774. return 0;
  775. }
  776. buf->content = rebuf;
  777. }
  778. buf->size = newSize;
  779. UPDATE_COMPAT(buf)
  780. return 1;
  781. }
  782. /**
  783. * xmlBufAdd:
  784. * @buf: the buffer to dump
  785. * @str: the #xmlChar string
  786. * @len: the number of #xmlChar to add
  787. *
  788. * Add a string range to an XML buffer. if len == -1, the length of
  789. * str is recomputed.
  790. *
  791. * Returns 0 successful, a positive error code number otherwise
  792. * and -1 in case of internal or API error.
  793. */
  794. int
  795. xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len) {
  796. unsigned int needSize;
  797. if ((str == NULL) || (buf == NULL) || (buf->error))
  798. return -1;
  799. CHECK_COMPAT(buf)
  800. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
  801. if (len < -1) {
  802. #ifdef DEBUG_BUFFER
  803. xmlGenericError(xmlGenericErrorContext,
  804. "xmlBufAdd: len < 0\n");
  805. #endif
  806. return -1;
  807. }
  808. if (len == 0) return 0;
  809. if (len < 0)
  810. len = xmlStrlen(str);
  811. if (len < 0) return -1;
  812. if (len == 0) return 0;
  813. needSize = buf->use + len + 2;
  814. if (needSize > buf->size){
  815. if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
  816. /*
  817. * Used to provide parsing limits
  818. */
  819. if (needSize >= XML_MAX_TEXT_LENGTH) {
  820. xmlBufMemoryError(buf, "buffer error: text too long\n");
  821. return(-1);
  822. }
  823. }
  824. if (!xmlBufResize(buf, needSize)){
  825. xmlBufMemoryError(buf, "growing buffer");
  826. return XML_ERR_NO_MEMORY;
  827. }
  828. }
  829. memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
  830. buf->use += len;
  831. buf->content[buf->use] = 0;
  832. UPDATE_COMPAT(buf)
  833. return 0;
  834. }
  835. /**
  836. * xmlBufAddHead:
  837. * @buf: the buffer
  838. * @str: the #xmlChar string
  839. * @len: the number of #xmlChar to add
  840. *
  841. * Add a string range to the beginning of an XML buffer.
  842. * if len == -1, the length of @str is recomputed.
  843. *
  844. * Returns 0 successful, a positive error code number otherwise
  845. * and -1 in case of internal or API error.
  846. */
  847. int
  848. xmlBufAddHead(xmlBufPtr buf, const xmlChar *str, int len) {
  849. unsigned int needSize;
  850. if ((buf == NULL) || (buf->error))
  851. return(-1);
  852. CHECK_COMPAT(buf)
  853. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
  854. if (str == NULL) {
  855. #ifdef DEBUG_BUFFER
  856. xmlGenericError(xmlGenericErrorContext,
  857. "xmlBufAddHead: str == NULL\n");
  858. #endif
  859. return -1;
  860. }
  861. if (len < -1) {
  862. #ifdef DEBUG_BUFFER
  863. xmlGenericError(xmlGenericErrorContext,
  864. "xmlBufAddHead: len < 0\n");
  865. #endif
  866. return -1;
  867. }
  868. if (len == 0) return 0;
  869. if (len < 0)
  870. len = xmlStrlen(str);
  871. if (len <= 0) return -1;
  872. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  873. size_t start_buf = buf->content - buf->contentIO;
  874. if (start_buf > (unsigned int) len) {
  875. /*
  876. * We can add it in the space previously shrunk
  877. */
  878. buf->content -= len;
  879. memmove(&buf->content[0], str, len);
  880. buf->use += len;
  881. buf->size += len;
  882. UPDATE_COMPAT(buf)
  883. return(0);
  884. }
  885. }
  886. needSize = buf->use + len + 2;
  887. if (needSize > buf->size){
  888. if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
  889. /*
  890. * Used to provide parsing limits
  891. */
  892. if (needSize >= XML_MAX_TEXT_LENGTH) {
  893. xmlBufMemoryError(buf, "buffer error: text too long\n");
  894. return(-1);
  895. }
  896. }
  897. if (!xmlBufResize(buf, needSize)){
  898. xmlBufMemoryError(buf, "growing buffer");
  899. return XML_ERR_NO_MEMORY;
  900. }
  901. }
  902. memmove(&buf->content[len], &buf->content[0], buf->use);
  903. memmove(&buf->content[0], str, len);
  904. buf->use += len;
  905. buf->content[buf->use] = 0;
  906. UPDATE_COMPAT(buf)
  907. return 0;
  908. }
  909. /**
  910. * xmlBufCat:
  911. * @buf: the buffer to add to
  912. * @str: the #xmlChar string
  913. *
  914. * Append a zero terminated string to an XML buffer.
  915. *
  916. * Returns 0 successful, a positive error code number otherwise
  917. * and -1 in case of internal or API error.
  918. */
  919. int
  920. xmlBufCat(xmlBufPtr buf, const xmlChar *str) {
  921. if ((buf == NULL) || (buf->error))
  922. return(-1);
  923. CHECK_COMPAT(buf)
  924. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
  925. if (str == NULL) return -1;
  926. return xmlBufAdd(buf, str, -1);
  927. }
  928. /**
  929. * xmlBufCCat:
  930. * @buf: the buffer to dump
  931. * @str: the C char string
  932. *
  933. * Append a zero terminated C string to an XML buffer.
  934. *
  935. * Returns 0 successful, a positive error code number otherwise
  936. * and -1 in case of internal or API error.
  937. */
  938. int
  939. xmlBufCCat(xmlBufPtr buf, const char *str) {
  940. const char *cur;
  941. if ((buf == NULL) || (buf->error))
  942. return(-1);
  943. CHECK_COMPAT(buf)
  944. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
  945. if (str == NULL) {
  946. #ifdef DEBUG_BUFFER
  947. xmlGenericError(xmlGenericErrorContext,
  948. "xmlBufCCat: str == NULL\n");
  949. #endif
  950. return -1;
  951. }
  952. for (cur = str;*cur != 0;cur++) {
  953. if (buf->use + 10 >= buf->size) {
  954. if (!xmlBufResize(buf, buf->use+10)){
  955. xmlBufMemoryError(buf, "growing buffer");
  956. return XML_ERR_NO_MEMORY;
  957. }
  958. }
  959. buf->content[buf->use++] = *cur;
  960. }
  961. buf->content[buf->use] = 0;
  962. UPDATE_COMPAT(buf)
  963. return 0;
  964. }
  965. /**
  966. * xmlBufWriteCHAR:
  967. * @buf: the XML buffer
  968. * @string: the string to add
  969. *
  970. * routine which manages and grows an output buffer. This one adds
  971. * xmlChars at the end of the buffer.
  972. *
  973. * Returns 0 if successful, a positive error code number otherwise
  974. * and -1 in case of internal or API error.
  975. */
  976. int
  977. xmlBufWriteCHAR(xmlBufPtr buf, const xmlChar *string) {
  978. if ((buf == NULL) || (buf->error))
  979. return(-1);
  980. CHECK_COMPAT(buf)
  981. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
  982. return(-1);
  983. return(xmlBufCat(buf, string));
  984. }
  985. /**
  986. * xmlBufWriteChar:
  987. * @buf: the XML buffer output
  988. * @string: the string to add
  989. *
  990. * routine which manage and grows an output buffer. This one add
  991. * C chars at the end of the array.
  992. *
  993. * Returns 0 if successful, a positive error code number otherwise
  994. * and -1 in case of internal or API error.
  995. */
  996. int
  997. xmlBufWriteChar(xmlBufPtr buf, const char *string) {
  998. if ((buf == NULL) || (buf->error))
  999. return(-1);
  1000. CHECK_COMPAT(buf)
  1001. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
  1002. return(-1);
  1003. return(xmlBufCCat(buf, string));
  1004. }
  1005. /**
  1006. * xmlBufWriteQuotedString:
  1007. * @buf: the XML buffer output
  1008. * @string: the string to add
  1009. *
  1010. * routine which manage and grows an output buffer. This one writes
  1011. * a quoted or double quoted #xmlChar string, checking first if it holds
  1012. * quote or double-quotes internally
  1013. *
  1014. * Returns 0 if successful, a positive error code number otherwise
  1015. * and -1 in case of internal or API error.
  1016. */
  1017. int
  1018. xmlBufWriteQuotedString(xmlBufPtr buf, const xmlChar *string) {
  1019. const xmlChar *cur, *base;
  1020. if ((buf == NULL) || (buf->error))
  1021. return(-1);
  1022. CHECK_COMPAT(buf)
  1023. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
  1024. return(-1);
  1025. if (xmlStrchr(string, '\"')) {
  1026. if (xmlStrchr(string, '\'')) {
  1027. #ifdef DEBUG_BUFFER
  1028. xmlGenericError(xmlGenericErrorContext,
  1029. "xmlBufWriteQuotedString: string contains quote and double-quotes !\n");
  1030. #endif
  1031. xmlBufCCat(buf, "\"");
  1032. base = cur = string;
  1033. while(*cur != 0){
  1034. if(*cur == '"'){
  1035. if (base != cur)
  1036. xmlBufAdd(buf, base, cur - base);
  1037. xmlBufAdd(buf, BAD_CAST "&quot;", 6);
  1038. cur++;
  1039. base = cur;
  1040. }
  1041. else {
  1042. cur++;
  1043. }
  1044. }
  1045. if (base != cur)
  1046. xmlBufAdd(buf, base, cur - base);
  1047. xmlBufCCat(buf, "\"");
  1048. }
  1049. else{
  1050. xmlBufCCat(buf, "\'");
  1051. xmlBufCat(buf, string);
  1052. xmlBufCCat(buf, "\'");
  1053. }
  1054. } else {
  1055. xmlBufCCat(buf, "\"");
  1056. xmlBufCat(buf, string);
  1057. xmlBufCCat(buf, "\"");
  1058. }
  1059. return(0);
  1060. }
  1061. /**
  1062. * xmlBufFromBuffer:
  1063. * @buffer: incoming old buffer to convert to a new one
  1064. *
  1065. * Helper routine to switch from the old buffer structures in use
  1066. * in various APIs. It creates a wrapper xmlBufPtr which will be
  1067. * used for internal processing until the xmlBufBackToBuffer() is
  1068. * issued.
  1069. *
  1070. * Returns a new xmlBufPtr unless the call failed and NULL is returned
  1071. */
  1072. xmlBufPtr
  1073. xmlBufFromBuffer(xmlBufferPtr buffer) {
  1074. xmlBufPtr ret;
  1075. if (buffer == NULL)
  1076. return(NULL);
  1077. ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
  1078. if (ret == NULL) {
  1079. xmlBufMemoryError(NULL, "creating buffer");
  1080. return(NULL);
  1081. }
  1082. ret->use = buffer->use;
  1083. ret->size = buffer->size;
  1084. ret->compat_use = buffer->use;
  1085. ret->compat_size = buffer->size;
  1086. ret->error = 0;
  1087. ret->buffer = buffer;
  1088. ret->alloc = buffer->alloc;
  1089. ret->content = buffer->content;
  1090. ret->contentIO = buffer->contentIO;
  1091. return(ret);
  1092. }
  1093. /**
  1094. * xmlBufBackToBuffer:
  1095. * @buf: new buffer wrapping the old one
  1096. *
  1097. * Function to be called once internal processing had been done to
  1098. * update back the buffer provided by the user. This can lead to
  1099. * a failure in case the size accumulated in the xmlBuf is larger
  1100. * than what an xmlBuffer can support on 64 bits (INT_MAX)
  1101. * The xmlBufPtr @buf wrapper is deallocated by this call in any case.
  1102. *
  1103. * Returns the old xmlBufferPtr unless the call failed and NULL is returned
  1104. */
  1105. xmlBufferPtr
  1106. xmlBufBackToBuffer(xmlBufPtr buf) {
  1107. xmlBufferPtr ret;
  1108. if (buf == NULL)
  1109. return(NULL);
  1110. CHECK_COMPAT(buf)
  1111. if ((buf->error) || (buf->buffer == NULL)) {
  1112. xmlBufFree(buf);
  1113. return(NULL);
  1114. }
  1115. ret = buf->buffer;
  1116. /*
  1117. * What to do in case of error in the buffer ???
  1118. */
  1119. if (buf->use > INT_MAX) {
  1120. /*
  1121. * Worse case, we really allocated and used more than the
  1122. * maximum allowed memory for an xmlBuffer on this architecture.
  1123. * Keep the buffer but provide a truncated size value.
  1124. */
  1125. xmlBufOverflowError(buf, "Used size too big for xmlBuffer");
  1126. ret->use = INT_MAX;
  1127. ret->size = INT_MAX;
  1128. } else if (buf->size > INT_MAX) {
  1129. /*
  1130. * milder case, we allocated more than the maximum allowed memory
  1131. * for an xmlBuffer on this architecture, but used less than the
  1132. * limit.
  1133. * Keep the buffer but provide a truncated size value.
  1134. */
  1135. xmlBufOverflowError(buf, "Allocated size too big for xmlBuffer");
  1136. ret->use = (int) buf->use;
  1137. ret->size = INT_MAX;
  1138. } else {
  1139. ret->use = (int) buf->use;
  1140. ret->size = (int) buf->size;
  1141. }
  1142. ret->alloc = buf->alloc;
  1143. ret->content = buf->content;
  1144. ret->contentIO = buf->contentIO;
  1145. xmlFree(buf);
  1146. return(ret);
  1147. }
  1148. /**
  1149. * xmlBufMergeBuffer:
  1150. * @buf: an xmlBufPtr
  1151. * @buffer: the buffer to consume into @buf
  1152. *
  1153. * The content of @buffer is appended to @buf and @buffer is freed
  1154. *
  1155. * Returns -1 in case of error, 0 otherwise, in any case @buffer is freed
  1156. */
  1157. int
  1158. xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer) {
  1159. int ret = 0;
  1160. if ((buf == NULL) || (buf->error)) {
  1161. xmlBufferFree(buffer);
  1162. return(-1);
  1163. }
  1164. CHECK_COMPAT(buf)
  1165. if ((buffer != NULL) && (buffer->content != NULL) &&
  1166. (buffer->use > 0)) {
  1167. ret = xmlBufAdd(buf, buffer->content, buffer->use);
  1168. }
  1169. xmlBufferFree(buffer);
  1170. return(ret);
  1171. }
  1172. /**
  1173. * xmlBufResetInput:
  1174. * @buf: an xmlBufPtr
  1175. * @input: an xmlParserInputPtr
  1176. *
  1177. * Update the input to use the current set of pointers from the buffer.
  1178. *
  1179. * Returns -1 in case of error, 0 otherwise
  1180. */
  1181. int
  1182. xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
  1183. if ((input == NULL) || (buf == NULL) || (buf->error))
  1184. return(-1);
  1185. CHECK_COMPAT(buf)
  1186. input->base = input->cur = buf->content;
  1187. input->end = &buf->content[buf->use];
  1188. return(0);
  1189. }
  1190. /**
  1191. * xmlBufGetInputBase:
  1192. * @buf: an xmlBufPtr
  1193. * @input: an xmlParserInputPtr
  1194. *
  1195. * Get the base of the @input relative to the beginning of the buffer
  1196. *
  1197. * Returns the size_t corresponding to the displacement
  1198. */
  1199. size_t
  1200. xmlBufGetInputBase(xmlBufPtr buf, xmlParserInputPtr input) {
  1201. size_t base;
  1202. if ((input == NULL) || (buf == NULL) || (buf->error))
  1203. return(-1);
  1204. CHECK_COMPAT(buf)
  1205. base = input->base - buf->content;
  1206. /*
  1207. * We could do some pointer arithmetic checks but that's probably
  1208. * sufficient.
  1209. */
  1210. if (base > buf->size) {
  1211. xmlBufOverflowError(buf, "Input reference outside of the buffer");
  1212. base = 0;
  1213. }
  1214. return(base);
  1215. }
  1216. /**
  1217. * xmlBufSetInputBaseCur:
  1218. * @buf: an xmlBufPtr
  1219. * @input: an xmlParserInputPtr
  1220. * @base: the base value relative to the beginning of the buffer
  1221. * @cur: the cur value relative to the beginning of the buffer
  1222. *
  1223. * Update the input to use the base and cur relative to the buffer
  1224. * after a possible reallocation of its content
  1225. *
  1226. * Returns -1 in case of error, 0 otherwise
  1227. */
  1228. int
  1229. xmlBufSetInputBaseCur(xmlBufPtr buf, xmlParserInputPtr input,
  1230. size_t base, size_t cur) {
  1231. if (input == NULL)
  1232. return(-1);
  1233. if ((buf == NULL) || (buf->error)) {
  1234. input->base = input->cur = input->end = BAD_CAST "";
  1235. return(-1);
  1236. }
  1237. CHECK_COMPAT(buf)
  1238. input->base = &buf->content[base];
  1239. input->cur = input->base + cur;
  1240. input->end = &buf->content[buf->use];
  1241. return(0);
  1242. }
  1243. #define bottom_buf
  1244. #include "elfgcchack.h"