libpurple/circbuffer.c

branch
release-2.x.y
changeset 36256
a437550a9308
parent 27361
62f1aa8045bb
equal deleted inserted replaced
36255:a3fe30a2666b 36256:a437550a9308
40 g_free(buf->buffer); 40 g_free(buf->buffer);
41 g_free(buf); 41 g_free(buf);
42 } 42 }
43 43
44 static void grow_circ_buffer(PurpleCircBuffer *buf, gsize len) { 44 static void grow_circ_buffer(PurpleCircBuffer *buf, gsize len) {
45 int in_offset = 0, out_offset = 0; 45 gsize in_offset = 0, out_offset = 0;
46 int start_buflen; 46 gsize start_buflen;
47 47
48 g_return_if_fail(buf != NULL); 48 g_return_if_fail(buf != NULL);
49 49
50 start_buflen = buf->buflen; 50 start_buflen = buf->buflen;
51 51
68 68
69 /* If the fill pointer is wrapped to before the remove 69 /* If the fill pointer is wrapped to before the remove
70 * pointer, we need to shift the data */ 70 * pointer, we need to shift the data */
71 if (in_offset < out_offset 71 if (in_offset < out_offset
72 || (in_offset == out_offset && buf->bufused > 0)) { 72 || (in_offset == out_offset && buf->bufused > 0)) {
73 int shift_n = MIN(buf->buflen - start_buflen, 73 gsize shift_n = MIN(buf->buflen - start_buflen, in_offset);
74 in_offset); 74 memcpy(buf->buffer + start_buflen, buf->buffer, shift_n);
75 memcpy(buf->buffer + start_buflen, buf->buffer,
76 shift_n);
77 75
78 /* If we couldn't fit the wrapped read buffer 76 /* If we couldn't fit the wrapped read buffer
79 * at the end */ 77 * at the end */
80 if (shift_n < in_offset) { 78 if (shift_n < in_offset) {
81 memmove(buf->buffer, 79 memmove(buf->buffer,
90 } 88 }
91 } 89 }
92 90
93 void purple_circ_buffer_append(PurpleCircBuffer *buf, gconstpointer src, gsize len) { 91 void purple_circ_buffer_append(PurpleCircBuffer *buf, gconstpointer src, gsize len) {
94 92
95 int len_stored; 93 gsize len_stored;
96 94
97 g_return_if_fail(buf != NULL); 95 g_return_if_fail(buf != NULL);
98 96
99 /* Grow the buffer, if necessary */ 97 /* Grow the buffer, if necessary */
100 if ((buf->buflen - buf->bufused) < len) 98 if ((buf->buflen - buf->bufused) < len)
103 /* If there is not enough room to copy all of src before hitting 101 /* If there is not enough room to copy all of src before hitting
104 * the end of the buffer then we will need to do two copies. 102 * the end of the buffer then we will need to do two copies.
105 * One copy from inptr to the end of the buffer, and the 103 * One copy from inptr to the end of the buffer, and the
106 * second copy from the start of the buffer to the end of src. */ 104 * second copy from the start of the buffer to the end of src. */
107 if (buf->inptr >= buf->outptr) 105 if (buf->inptr >= buf->outptr)
108 len_stored = MIN(len, buf->buflen 106 len_stored = MIN(len, buf->buflen - (buf->inptr - buf->buffer));
109 - (buf->inptr - buf->buffer));
110 else 107 else
111 len_stored = len; 108 len_stored = len;
112 109
113 if (len_stored > 0) 110 if (len_stored > 0)
114 memcpy(buf->inptr, src, len_stored); 111 memcpy(buf->inptr, src, len_stored);
143 g_return_val_if_fail(purple_circ_buffer_get_max_read(buf) >= len, FALSE); 140 g_return_val_if_fail(purple_circ_buffer_get_max_read(buf) >= len, FALSE);
144 141
145 buf->outptr += len; 142 buf->outptr += len;
146 buf->bufused -= len; 143 buf->bufused -= len;
147 /* wrap to the start if we're at the end */ 144 /* wrap to the start if we're at the end */
148 if ((buf->outptr - buf->buffer) == buf->buflen) 145 if ((gsize)(buf->outptr - buf->buffer) == buf->buflen)
149 buf->outptr = buf->buffer; 146 buf->outptr = buf->buffer;
150 147
151 return TRUE; 148 return TRUE;
152 } 149 }
153 150

mercurial