* @ingroup pbuf * Copy application supplied data into a pbuf. * This function can only be used to copy the equivalent of buf->tot_len data. * * @param buf pbuf to fill with data * @param dataptr application supplied data buffer * @param len length of the application supplied data buffer * * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough */
| 1193 | * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough |
| 1194 | */ |
| 1195 | err_t |
| 1196 | pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len) |
| 1197 | { |
| 1198 | struct pbuf *p; |
| 1199 | size_t buf_copy_len; |
| 1200 | size_t total_copy_len = len; |
| 1201 | size_t copied_total = 0; |
| 1202 | |
| 1203 | LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return ERR_ARG;); |
| 1204 | LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return ERR_ARG;); |
| 1205 | LWIP_ERROR("pbuf_take: buf not large enough", (buf->tot_len >= len), return ERR_MEM;); |
| 1206 | |
| 1207 | if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) { |
| 1208 | return ERR_ARG; |
| 1209 | } |
| 1210 | |
| 1211 | /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */ |
| 1212 | for (p = buf; total_copy_len != 0; p = p->next) { |
| 1213 | LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL); |
| 1214 | buf_copy_len = total_copy_len; |
| 1215 | if (buf_copy_len > p->len) { |
| 1216 | /* this pbuf cannot hold all remaining data */ |
| 1217 | buf_copy_len = p->len; |
| 1218 | } |
| 1219 | /* copy the necessary parts of the buffer */ |
| 1220 | MEMCPY(p->payload, &((const char *)dataptr)[copied_total], buf_copy_len); |
| 1221 | total_copy_len -= buf_copy_len; |
| 1222 | copied_total += buf_copy_len; |
| 1223 | } |
| 1224 | LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len); |
| 1225 | return ERR_OK; |
| 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * @ingroup pbuf |
no outgoing calls