blob: f574b6eadd355d1ec6434f2252789b8a25d39680 [file] [log] [blame]
adamdunkelsa2f3c422004-09-12 20:24:53 +00001/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
33 * $Id: uipbuf.c,v 1.6 2004/09/12 20:24:56 adamdunkels Exp $
34 */
adamdunkels56807862004-08-09 20:50:11 +000035
36#include "uipbuf.h"
37
38#include <string.h>
39
40/*---------------------------------------------------------------------------*/
41void
42uipbuf_setup(struct uipbuf_buffer *buf,
43 u8_t *bufptr, u16_t bufsize)
44{
45 buf->buffer = buf->ptr = bufptr;
46 buf->bufsize = buf->left = bufsize;
47}
48/*---------------------------------------------------------------------------*/
49u8_t
50uipbuf_bufdata(struct uipbuf_buffer *buf, u16_t len,
51 u8_t **dataptr, u16_t *datalen)
52{
53 if(*datalen < buf->left) {
54 memcpy(buf->ptr, *dataptr, *datalen);
55 buf->ptr += *datalen;
56 buf->left -= *datalen;
57 *dataptr += *datalen;
58 *datalen = 0;
59 return UIPBUF_NOT_FULL;
60 } else if(*datalen == buf->left) {
61 memcpy(buf->ptr, *dataptr, *datalen);
62 buf->ptr += *datalen;
63 buf->left = 0;
64 *dataptr += *datalen;
65 *datalen = 0;
66 return UIPBUF_FULL;
67 } else {
68 memcpy(buf->ptr, *dataptr, buf->left);
69 buf->ptr += buf->left;
70 *datalen -= buf->left;
71 *dataptr += buf->left;
72 buf->left = 0;
73 return UIPBUF_FULL;
74 }
75
76}
77/*---------------------------------------------------------------------------*/
78u8_t
adamdunkelsbce2a812004-09-03 10:00:40 +000079uipbuf_bufto(register struct uipbuf_buffer *buf, u8_t endmarker,
80 register u8_t **dataptr, register u16_t *datalen)
adamdunkels56807862004-08-09 20:50:11 +000081{
82 u8_t c;
adamdunkelsbce2a812004-09-03 10:00:40 +000083 /*
84 int len;
85
86 ptr = memchr(*dataptr, endmarker, *datalen);
87 if(ptr != NULL) {
88 len = ptr - *dataptr;
89 } else {
90 len = *datalen;
91 }
92 memcpy(buf->ptr, *dataptr, len);
93 *dataptr += len;
94 *datalen -= len;
95 buf->ptr += len;
96 */
adamdunkels56807862004-08-09 20:50:11 +000097 while(buf->left > 0 && *datalen > 0) {
98 c = *buf->ptr = **dataptr;
99 ++*dataptr;
100 ++buf->ptr;
101 --*datalen;
102 --buf->left;
103
104 if(c == endmarker) {
105 return UIPBUF_FOUND;
106 }
107 }
108
109 if(*datalen == 0) {
110 return UIPBUF_NOT_FOUND;
111 }
112
113 while(*datalen > 0) {
adamdunkels5c22b922004-08-11 21:25:30 +0000114 c = **dataptr;
adamdunkels56807862004-08-09 20:50:11 +0000115 --*datalen;
116 ++*dataptr;
adamdunkels5c22b922004-08-11 21:25:30 +0000117
118 if(c == endmarker) {
119 return UIPBUF_FOUND | UIPBUF_FULL;
120 }
adamdunkels56807862004-08-09 20:50:11 +0000121 }
122
123 return UIPBUF_FULL;
124}
oliverschmidt4b4c2892004-08-20 12:34:33 +0000125/*----------------------------------------------------------------------------*/
126u16_t
adamdunkels56807862004-08-09 20:50:11 +0000127uipbuf_len(struct uipbuf_buffer *buf)
128{
129 return buf->bufsize - buf->left;
130}
131/*----------------------------------------------------------------------------*/