Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Buffer management functions. |
| 3 | * |
| 4 | * Copyright 2000-2006 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <string.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | |
| 15 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <proto/buffers.h> |
| 17 | |
| 18 | void **pool_buffer = NULL; |
| 19 | |
| 20 | /* writes <len> bytes from message <msg> to buffer <buf>. Returns 0 in case of |
| 21 | * success, or the number of bytes available otherwise. |
| 22 | * FIXME-20060521: handle unaligned data. |
| 23 | */ |
| 24 | int buffer_write(struct buffer *buf, const char *msg, int len) |
| 25 | { |
| 26 | int max; |
| 27 | |
| 28 | max = buffer_realign(buf); |
| 29 | |
| 30 | if (len > max) |
| 31 | return max; |
| 32 | |
| 33 | memcpy(buf->r, msg, len); |
| 34 | buf->l += len; |
| 35 | buf->r += len; |
| 36 | if (buf->r == buf->data + BUFSIZE) |
| 37 | buf->r = buf->data; |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 43 | * and moves <end> just after the end of <str>. |
| 44 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 45 | * the shift value (positive or negative) is returned. |
| 46 | * If there's no space left, the move is not done. |
| 47 | * |
| 48 | */ |
| 49 | int buffer_replace(struct buffer *b, char *pos, char *end, char *str) |
| 50 | { |
| 51 | int delta; |
| 52 | int len; |
| 53 | |
| 54 | len = strlen(str); |
| 55 | delta = len - (end - pos); |
| 56 | |
| 57 | if (delta + b->r >= b->data + BUFSIZE) |
| 58 | return 0; /* no space left */ |
| 59 | |
| 60 | /* first, protect the end of the buffer */ |
| 61 | memmove(end + delta, end, b->data + b->l - end); |
| 62 | |
| 63 | /* now, copy str over pos */ |
| 64 | memcpy(pos, str,len); |
| 65 | |
| 66 | /* we only move data after the displaced zone */ |
| 67 | if (b->r > pos) b->r += delta; |
| 68 | if (b->w > pos) b->w += delta; |
| 69 | if (b->h > pos) b->h += delta; |
| 70 | if (b->lr > pos) b->lr += delta; |
| 71 | b->l += delta; |
| 72 | |
| 73 | return delta; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * same except that the string length is given, which allows str to be NULL if |
| 78 | * len is 0. |
| 79 | */ |
| 80 | int buffer_replace2(struct buffer *b, char *pos, char *end, char *str, int len) |
| 81 | { |
| 82 | int delta; |
| 83 | |
| 84 | delta = len - (end - pos); |
| 85 | |
| 86 | if (delta + b->r >= b->data + BUFSIZE) |
| 87 | return 0; /* no space left */ |
| 88 | |
| 89 | if (b->data + b->l < end) { |
| 90 | /* The data has been stolen, we could have crashed. |
| 91 | * Maybe we should abort() ? */ |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* first, protect the end of the buffer */ |
| 96 | memmove(end + delta, end, b->data + b->l - end); |
| 97 | |
| 98 | /* now, copy str over pos */ |
| 99 | if (len) |
| 100 | memcpy(pos, str, len); |
| 101 | |
| 102 | /* we only move data after the displaced zone */ |
| 103 | if (b->r > pos) b->r += delta; |
| 104 | if (b->w > pos) b->w += delta; |
| 105 | if (b->h > pos) b->h += delta; |
| 106 | if (b->lr > pos) b->lr += delta; |
| 107 | b->l += delta; |
| 108 | |
| 109 | return delta; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /* |
| 114 | * Local variables: |
| 115 | * c-indent-level: 8 |
| 116 | * c-basic-offset: 8 |
| 117 | * End: |
| 118 | */ |