Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_snmp_pbuf_stream.c Source File

lwip_snmp_pbuf_stream.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * SNMP pbuf stream wrapper implementation (internal API, do not use in client code).
00004  */
00005 
00006 /*
00007  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright notice,
00014  *    this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  *    this list of conditions and the following disclaimer in the documentation
00017  *    and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00024  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00030  * OF SUCH DAMAGE.
00031  *
00032  * This file is part of the lwIP TCP/IP stack.
00033  *
00034  * Author: Martin Hentschel <info@cl-soft.de>
00035  *
00036  */
00037 
00038 #include "lwip/apps/snmp_opts.h"
00039 
00040 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
00041 
00042 #include "snmp_pbuf_stream.h"
00043 #include "lwip/def.h"
00044 #include <string.h>
00045 
00046 err_t
00047 snmp_pbuf_stream_init(struct snmp_pbuf_stream* pbuf_stream, struct pbuf* p, u16_t offset, u16_t length)
00048 {
00049   pbuf_stream->offset = offset;
00050   pbuf_stream->length = length;
00051   pbuf_stream->pbuf   = p;
00052 
00053   return ERR_OK;
00054 }
00055 
00056 err_t
00057 snmp_pbuf_stream_read(struct snmp_pbuf_stream* pbuf_stream, u8_t* data)
00058 {
00059   if (pbuf_stream->length == 0) {
00060     return ERR_BUF;
00061   }
00062 
00063   if (pbuf_copy_partial(pbuf_stream->pbuf, data, 1, pbuf_stream->offset) == 0) {
00064     return ERR_BUF;
00065   }
00066 
00067   pbuf_stream->offset++;
00068   pbuf_stream->length--;
00069 
00070   return ERR_OK;
00071 }
00072 
00073 err_t
00074 snmp_pbuf_stream_write(struct snmp_pbuf_stream* pbuf_stream, u8_t data)
00075 {
00076   return snmp_pbuf_stream_writebuf(pbuf_stream, &data, 1);
00077 }
00078 
00079 err_t
00080 snmp_pbuf_stream_writebuf(struct snmp_pbuf_stream* pbuf_stream, const void* buf, u16_t buf_len)
00081 {
00082   if (pbuf_stream->length < buf_len) {
00083     return ERR_BUF;
00084   }
00085 
00086   if (pbuf_take_at(pbuf_stream->pbuf, buf, buf_len, pbuf_stream->offset) != ERR_OK) {
00087     return ERR_BUF;
00088   }
00089 
00090   pbuf_stream->offset += buf_len;
00091   pbuf_stream->length -= buf_len;
00092 
00093   return ERR_OK;
00094 }
00095 
00096 err_t
00097 snmp_pbuf_stream_writeto(struct snmp_pbuf_stream* pbuf_stream, struct snmp_pbuf_stream* target_pbuf_stream, u16_t len)
00098 {
00099 
00100   if ((pbuf_stream == NULL) || (target_pbuf_stream == NULL)) {
00101     return ERR_ARG;
00102   }
00103   if ((len > pbuf_stream->length) || (len > target_pbuf_stream->length)) {
00104     return ERR_ARG;
00105   }
00106 
00107   if (len == 0) {
00108     len = LWIP_MIN(pbuf_stream->length, target_pbuf_stream->length);
00109   }
00110 
00111   while (len > 0) {
00112     u16_t chunk_len;
00113     err_t err;
00114     u16_t target_offset;
00115     struct pbuf* pbuf = pbuf_skip(pbuf_stream->pbuf, pbuf_stream->offset, &target_offset);
00116 
00117     if ((pbuf == NULL) || (pbuf->len == 0)) {
00118       return ERR_BUF;
00119     }
00120 
00121     chunk_len = LWIP_MIN(len, pbuf->len);
00122     err = snmp_pbuf_stream_writebuf(target_pbuf_stream, &((u8_t*)pbuf->payload)[target_offset], chunk_len);
00123     if (err != ERR_OK) {
00124       return err;
00125     }
00126 
00127     pbuf_stream->offset   += chunk_len;
00128     pbuf_stream->length   -= chunk_len;
00129     len -= chunk_len;
00130   }
00131 
00132   return ERR_OK;
00133 }
00134 
00135 err_t
00136 snmp_pbuf_stream_seek(struct snmp_pbuf_stream* pbuf_stream, s32_t offset)
00137 {
00138   if ((offset < 0) || (offset > pbuf_stream->length)) {
00139     /* we cannot seek backwards or forward behind stream end */
00140     return ERR_ARG;
00141   }
00142 
00143   pbuf_stream->offset += (u16_t)offset;
00144   pbuf_stream->length -= (u16_t)offset;
00145 
00146   return ERR_OK;
00147 }
00148 
00149 err_t
00150 snmp_pbuf_stream_seek_abs(struct snmp_pbuf_stream* pbuf_stream, u32_t offset)
00151 {
00152   s32_t rel_offset = offset - pbuf_stream->offset;
00153   return snmp_pbuf_stream_seek(pbuf_stream, rel_offset);
00154 }
00155 
00156 #endif /* LWIP_SNMP */