buf.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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"