* Compress/encrypt a packet and put the result in a new mbuf at *resultp. * The original mbuf is not free'd. */
| 417 | * The original mbuf is not free'd. |
| 418 | */ |
| 419 | static int |
| 420 | ng_deflate_compress(node_p node, struct mbuf *m, struct mbuf **resultp) |
| 421 | { |
| 422 | const priv_p priv = NG_NODE_PRIVATE(node); |
| 423 | int outlen, inlen; |
| 424 | int rtn; |
| 425 | |
| 426 | /* Initialize. */ |
| 427 | *resultp = NULL; |
| 428 | |
| 429 | inlen = m->m_pkthdr.len; |
| 430 | |
| 431 | priv->stats.FramesPlain++; |
| 432 | priv->stats.InOctets+=inlen; |
| 433 | |
| 434 | if (inlen > DEFLATE_BUF_SIZE) { |
| 435 | priv->stats.Errors++; |
| 436 | NG_FREE_M(m); |
| 437 | return (ENOMEM); |
| 438 | } |
| 439 | |
| 440 | /* We must own the mbuf chain exclusively to modify it. */ |
| 441 | m = m_unshare(m, M_NOWAIT); |
| 442 | if (m == NULL) { |
| 443 | priv->stats.Errors++; |
| 444 | return (ENOMEM); |
| 445 | } |
| 446 | |
| 447 | /* Work with contiguous regions of memory. */ |
| 448 | m_copydata(m, 0, inlen, (caddr_t)priv->inbuf); |
| 449 | outlen = DEFLATE_BUF_SIZE; |
| 450 | |
| 451 | /* Compress "inbuf" into "outbuf". */ |
| 452 | /* Prepare to compress. */ |
| 453 | if (priv->inbuf[0] != 0) { |
| 454 | priv->cx.next_in = priv->inbuf; |
| 455 | priv->cx.avail_in = inlen; |
| 456 | } else { |
| 457 | priv->cx.next_in = priv->inbuf + 1; /* compress protocol */ |
| 458 | priv->cx.avail_in = inlen - 1; |
| 459 | } |
| 460 | priv->cx.next_out = priv->outbuf + 2 + DEFLATE_HDRLEN; |
| 461 | priv->cx.avail_out = outlen - 2 - DEFLATE_HDRLEN; |
| 462 | |
| 463 | /* Compress. */ |
| 464 | rtn = deflate(&priv->cx, Z_SYNC_FLUSH); |
| 465 | |
| 466 | /* Check return value. */ |
| 467 | if (rtn != Z_OK) { |
| 468 | priv->stats.Errors++; |
| 469 | log(LOG_NOTICE, "ng_deflate: compression error: %d (%s)\n", |
| 470 | rtn, priv->cx.msg); |
| 471 | NG_FREE_M(m); |
| 472 | return (EINVAL); |
| 473 | } |
| 474 | |
| 475 | /* Calculate resulting size. */ |
| 476 | outlen -= priv->cx.avail_out; |
no test coverage detected