STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
snmp_pbuf_stream.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Martin Hentschel <info@cl-soft.de>
30  *
31  */
32 
33 #include "lwip/apps/snmp_opts.h"
34 
35 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
36 
37 #include "snmp_pbuf_stream.h"
38 #include "lwip/def.h"
39 #include <string.h>
40 
41 err_t
42 snmp_pbuf_stream_init(struct snmp_pbuf_stream* pbuf_stream, struct pbuf* p, u16_t offset, u16_t length)
43 {
44  pbuf_stream->offset = offset;
45  pbuf_stream->length = length;
46  pbuf_stream->pbuf = p;
47 
48  return ERR_OK;
49 }
50 
51 err_t
52 snmp_pbuf_stream_read(struct snmp_pbuf_stream* pbuf_stream, u8_t* data)
53 {
54  if (pbuf_stream->length == 0) {
55  return ERR_BUF;
56  }
57 
58  if(pbuf_copy_partial(pbuf_stream->pbuf, data, 1, pbuf_stream->offset) == 0) {
59  return ERR_BUF;
60  }
61 
62  pbuf_stream->offset++;
63  pbuf_stream->length--;
64 
65  return ERR_OK;
66 }
67 
68 err_t
69 snmp_pbuf_stream_write(struct snmp_pbuf_stream* pbuf_stream, u8_t data)
70 {
71  return snmp_pbuf_stream_writebuf(pbuf_stream, &data, 1);
72 }
73 
74 err_t
75 snmp_pbuf_stream_writebuf(struct snmp_pbuf_stream* pbuf_stream, const void* buf, u16_t buf_len)
76 {
77  if (pbuf_stream->length < buf_len) {
78  return ERR_BUF;
79  }
80 
81  if(pbuf_take_at(pbuf_stream->pbuf, buf, buf_len, pbuf_stream->offset) != ERR_OK) {
82  return ERR_BUF;
83  }
84 
85  pbuf_stream->offset += buf_len;
86  pbuf_stream->length -= buf_len;
87 
88  return ERR_OK;
89 }
90 
91 err_t
92 snmp_pbuf_stream_writeto(struct snmp_pbuf_stream* pbuf_stream, struct snmp_pbuf_stream* target_pbuf_stream, u16_t len)
93 {
94 
95  if ((pbuf_stream == NULL) || (target_pbuf_stream == NULL)) {
96  return ERR_ARG;
97  }
98  if ((len > pbuf_stream->length) || (len > target_pbuf_stream->length)) {
99  return ERR_ARG;
100  }
101 
102  if (len == 0) {
103  len = LWIP_MIN(pbuf_stream->length, target_pbuf_stream->length);
104  }
105 
106  while (len > 0) {
107  u16_t chunk_len;
108  err_t err;
109  u16_t target_offset;
110  struct pbuf* pbuf = pbuf_skip(pbuf_stream->pbuf, pbuf_stream->offset, &target_offset);
111 
112  if ((pbuf == NULL) || (pbuf->len == 0)) {
113  return ERR_BUF;
114  }
115 
116  chunk_len = LWIP_MIN(len, pbuf->len);
117  err = snmp_pbuf_stream_writebuf(target_pbuf_stream, &((u8_t*)pbuf->payload)[target_offset], chunk_len);
118  if (err != ERR_OK) {
119  return err;
120  }
121 
122  pbuf_stream->offset += chunk_len;
123  pbuf_stream->length -= chunk_len;
124  len -= chunk_len;
125  }
126 
127  return ERR_OK;
128 }
129 
130 err_t
131 snmp_pbuf_stream_seek(struct snmp_pbuf_stream* pbuf_stream, s32_t offset)
132 {
133  if ((offset < 0) || (offset > pbuf_stream->length)) {
134  /* we cannot seek backwards or forward behind stream end */
135  return ERR_ARG;
136  }
137 
138  pbuf_stream->offset += (u16_t)offset;
139  pbuf_stream->length -= (u16_t)offset;
140 
141  return ERR_OK;
142 }
143 
144 err_t
145 snmp_pbuf_stream_seek_abs(struct snmp_pbuf_stream* pbuf_stream, u32_t offset)
146 {
147  s32_t rel_offset = offset - pbuf_stream->offset;
148  return snmp_pbuf_stream_seek(pbuf_stream, rel_offset);
149 }
150 
151 #endif /* LWIP_SNMP */
struct pbuf * pbuf_skip(struct pbuf *in, u16_t in_offset, u16_t *out_offset)
Definition: pbuf.c:1048
u16_t len
Definition: pbuf.h:125
#define LWIP_MIN(x, y)
Definition: def.h:50
#define NULL
Definition: usbd_def.h:53
unsigned long u32_t
Definition: cc.h:42
#define ERR_OK
Definition: err.h:52
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
#define ERR_ARG
Definition: err.h:71
u16_t pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:952
unsigned char u8_t
Definition: cc.h:38
signed long s32_t
Definition: cc.h:43
err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1117
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113
#define ERR_BUF
Definition: err.h:54