1 /* |
1 /* |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 * |
3 * |
4 * Copyright 2016 Olaf Wintermann. All rights reserved. |
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
5 * |
5 * |
6 * Redistribution and use in source and binary forms, with or without |
6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions are met: |
7 * modification, are permitted provided that the following conditions are met: |
8 * |
8 * |
9 * 1. Redistributions of source code must retain the above copyright |
9 * 1. Redistributions of source code must retain the above copyright |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include "buffer.h" |
29 #include "ucx/buffer.h" |
|
30 |
30 #include <stdarg.h> |
31 #include <stdarg.h> |
31 #include <stdlib.h> |
32 #include <stdlib.h> |
32 #include <string.h> |
33 #include <string.h> |
33 |
34 |
34 UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags) { |
35 UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags) { |
62 free(buffer); |
63 free(buffer); |
63 } |
64 } |
64 |
65 |
65 UcxBuffer* ucx_buffer_extract( |
66 UcxBuffer* ucx_buffer_extract( |
66 UcxBuffer *src, size_t start, size_t length, int flags) { |
67 UcxBuffer *src, size_t start, size_t length, int flags) { |
67 |
68 if (src->size == 0 || length == 0 || |
68 if (src->size == 0 || length == 0 || start+length > src->capacity) { |
69 ((size_t)-1) - start < length || start+length > src->capacity) |
|
70 { |
69 return NULL; |
71 return NULL; |
70 } |
72 } |
71 |
73 |
72 UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); |
74 UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); |
73 if (dst) { |
75 if (dst) { |
148 } |
150 } |
149 |
151 |
150 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, |
152 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, |
151 UcxBuffer *buffer) { |
153 UcxBuffer *buffer) { |
152 size_t len = size * nitems; |
154 size_t len = size * nitems; |
153 const char *string = ptr; |
|
154 size_t required = buffer->pos + len; |
155 size_t required = buffer->pos + len; |
155 if (buffer->pos > required) { |
156 if (buffer->pos > required) { |
156 return 0; |
157 return 0; |
157 } |
158 } |
158 |
159 |