circbuffer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2002-2004 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #include "dbutil.h"
  26. #include "circbuffer.h"
  27. #define MAX_CBUF_SIZE 100000000
  28. circbuffer * cbuf_new(unsigned int size) {
  29. circbuffer *cbuf = NULL;
  30. if (size > MAX_CBUF_SIZE) {
  31. dropbear_exit("Bad cbuf size");
  32. }
  33. cbuf = (circbuffer*)m_malloc(sizeof(circbuffer));
  34. /* data is malloced on first write */
  35. cbuf->data = NULL;
  36. cbuf->used = 0;
  37. cbuf->readpos = 0;
  38. cbuf->writepos = 0;
  39. cbuf->size = size;
  40. return cbuf;
  41. }
  42. void cbuf_free(circbuffer * cbuf) {
  43. if (cbuf->data) {
  44. m_burn(cbuf->data, cbuf->size);
  45. m_free(cbuf->data);
  46. }
  47. m_free(cbuf);
  48. }
  49. unsigned int cbuf_getused(const circbuffer * cbuf) {
  50. return cbuf->used;
  51. }
  52. unsigned int cbuf_getavail(const circbuffer * cbuf) {
  53. return cbuf->size - cbuf->used;
  54. }
  55. unsigned int cbuf_writelen(const circbuffer *cbuf) {
  56. dropbear_assert(cbuf->used <= cbuf->size);
  57. dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size);
  58. dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size);
  59. if (cbuf->used == cbuf->size) {
  60. TRACE(("cbuf_writelen: full buffer"))
  61. return 0; /* full */
  62. }
  63. if (cbuf->writepos < cbuf->readpos) {
  64. return cbuf->readpos - cbuf->writepos;
  65. }
  66. return cbuf->size - cbuf->writepos;
  67. }
  68. void cbuf_readptrs(const circbuffer *cbuf,
  69. unsigned char **p1, unsigned int *len1,
  70. unsigned char **p2, unsigned int *len2) {
  71. *p1 = &cbuf->data[cbuf->readpos];
  72. *len1 = MIN(cbuf->used, cbuf->size - cbuf->readpos);
  73. if (*len1 < cbuf->used) {
  74. *p2 = cbuf->data;
  75. *len2 = cbuf->used - *len1;
  76. } else {
  77. *p2 = NULL;
  78. *len2 = 0;
  79. }
  80. }
  81. unsigned char* cbuf_writeptr(circbuffer *cbuf, unsigned int len) {
  82. if (len > cbuf_writelen(cbuf)) {
  83. dropbear_exit("Bad cbuf write");
  84. }
  85. if (!cbuf->data) {
  86. /* lazy allocation */
  87. cbuf->data = (unsigned char*)m_malloc(cbuf->size);
  88. }
  89. return &cbuf->data[cbuf->writepos];
  90. }
  91. void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) {
  92. if (len > cbuf_writelen(cbuf)) {
  93. dropbear_exit("Bad cbuf write");
  94. }
  95. cbuf->used += len;
  96. dropbear_assert(cbuf->used <= cbuf->size);
  97. cbuf->writepos = (cbuf->writepos + len) % cbuf->size;
  98. }
  99. void cbuf_incrread(circbuffer *cbuf, unsigned int len) {
  100. dropbear_assert(cbuf->used >= len);
  101. cbuf->used -= len;
  102. cbuf->readpos = (cbuf->readpos + len) % cbuf->size;
  103. }