varbuf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * varbuf.c - variable length expandable buffer handling
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2008-2011 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <dpkg/i18n.h>
  27. #include <dpkg/dpkg.h>
  28. #include <dpkg/dpkg-db.h>
  29. void
  30. varbuf_add_char(struct varbuf *v, int c)
  31. {
  32. varbuf_grow(v, 1);
  33. v->buf[v->used++]= c;
  34. }
  35. void
  36. varbuf_dup_char(struct varbuf *v, int c, size_t n)
  37. {
  38. varbuf_grow(v, n);
  39. memset(v->buf + v->used, c, n);
  40. v->used += n;
  41. }
  42. void
  43. varbuf_map_char(struct varbuf *v, int c_src, int c_dst)
  44. {
  45. size_t i;
  46. for (i = 0; i < v->used; i++)
  47. if (v->buf[i] == c_src)
  48. v->buf[i] = c_dst;
  49. }
  50. int
  51. varbuf_printf(struct varbuf *v, const char *fmt, ...)
  52. {
  53. int r;
  54. va_list args;
  55. va_start(args, fmt);
  56. r = varbuf_vprintf(v, fmt, args);
  57. va_end(args);
  58. return r;
  59. }
  60. int
  61. varbuf_vprintf(struct varbuf *v, const char *fmt, va_list args)
  62. {
  63. va_list args_copy;
  64. int needed, r;
  65. va_copy(args_copy, args);
  66. needed = vsnprintf(NULL, 0, fmt, args_copy);
  67. va_end(args_copy);
  68. if (needed < 0)
  69. ohshite(_("error formatting string into varbuf variable"));
  70. varbuf_grow(v, needed + 1);
  71. r = vsnprintf(v->buf + v->used, needed + 1, fmt, args);
  72. if (r < 0)
  73. ohshite(_("error formatting string into varbuf variable"));
  74. v->used += r;
  75. return r;
  76. }
  77. void
  78. varbuf_add_buf(struct varbuf *v, const void *s, size_t size)
  79. {
  80. varbuf_grow(v, size);
  81. memcpy(v->buf + v->used, s, size);
  82. v->used += size;
  83. }
  84. void
  85. varbuf_end_str(struct varbuf *v)
  86. {
  87. varbuf_grow(v, 1);
  88. v->buf[v->used] = '\0';
  89. }
  90. void
  91. varbuf_init(struct varbuf *v, size_t size)
  92. {
  93. v->used = 0;
  94. v->size = size;
  95. if (size)
  96. v->buf = m_malloc(size);
  97. else
  98. v->buf = NULL;
  99. }
  100. void
  101. varbuf_reset(struct varbuf *v)
  102. {
  103. v->used= 0;
  104. }
  105. void
  106. varbuf_grow(struct varbuf *v, size_t need_size)
  107. {
  108. /* Make sure the varbuf is in a sane state. */
  109. if (v->size < v->used)
  110. internerr("varbuf used(%zu) > size(%zu)", v->used, v->size);
  111. /* Check if we already have enough room. */
  112. if ((v->size - v->used) >= need_size)
  113. return;
  114. v->size = (v->size + need_size) * 2;
  115. v->buf = m_realloc(v->buf, v->size);
  116. }
  117. void
  118. varbuf_trunc(struct varbuf *v, size_t used_size)
  119. {
  120. /* Make sure the caller does not claim more than available. */
  121. if (v->size < used_size)
  122. internerr("varbuf new_used(%zu) > size(%zu)", used_size, v->size);
  123. v->used = used_size;
  124. }
  125. char *
  126. varbuf_detach(struct varbuf *v)
  127. {
  128. char *buf = v->buf;
  129. v->buf = NULL;
  130. v->size = 0;
  131. v->used = 0;
  132. return buf;
  133. }
  134. void
  135. varbuf_destroy(struct varbuf *v)
  136. {
  137. free(v->buf); v->buf=NULL; v->size=0; v->used=0;
  138. }