"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/wifipcap/cpack.cpp" (19 Feb 2021, 3669 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cpack.cpp" see the Fossies "Dox" file reference documentation.

    1 /*-
    2  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
    3  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions
    6  * are met:
    7  * 1. Redistributions of source code must retain the above copyright
    8  *    notice, this list of conditions and the following disclaimer.
    9  * 2. Redistributions in binary form must reproduce the above copyright
   10  *    notice, this list of conditions and the following disclaimer in the
   11  *    documentation and/or other materials provided with the distribution.
   12  * 3. The name of David Young may not be used to endorse or promote
   13  *    products derived from this software without specific prior
   14  *    written permission.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
   17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
   18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   19  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DAVID
   20  * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
   22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
   27  * OF SUCH DAMAGE.
   28  */
   29 
   30 #ifndef WIN32
   31 
   32 #include <string.h>
   33 
   34 #include "cpack.h"
   35 #include "extract.h"
   36 
   37 static u_int8_t *
   38 cpack_next_boundary(u_int8_t *buf, u_int8_t *p, size_t alignment)
   39 {
   40     size_t misalignment = (size_t)(p - buf) % alignment;
   41 
   42     if (misalignment == 0)
   43         return p;
   44 
   45     return p + (alignment - misalignment);
   46 }
   47 
   48 /* Advance to the next wordsize boundary. Return NULL if fewer than
   49  * wordsize bytes remain in the buffer after the boundary.  Otherwise,
   50  * return a pointer to the boundary.
   51  */
   52 static u_int8_t *
   53 cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize)
   54 {
   55     u_int8_t *next;
   56 
   57     /* Ensure alignment. */
   58     next = cpack_next_boundary(cs->c_buf, cs->c_next, wordsize);
   59 
   60     /* Too little space for wordsize bytes? */
   61     if (next - cs->c_buf + wordsize > cs->c_len)
   62         return 0;
   63 
   64     return next;
   65 }
   66 
   67 int
   68 cpack_init(struct cpack_state *cs, u_int8_t *buf, size_t buflen)
   69 {
   70     memset(cs, 0, sizeof(*cs));
   71 
   72     cs->c_buf = buf;
   73     cs->c_len = buflen;
   74     cs->c_next = cs->c_buf;
   75 
   76     return 0;
   77 }
   78 
   79 /* Unpack a 64-bit unsigned integer. */
   80 int
   81 cpack_uint64(struct cpack_state *cs, u_int64_t *u)
   82 {
   83     u_int8_t *next;
   84 
   85     if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
   86         return -1;
   87 
   88     *u = EXTRACT_LE_64BITS(next);
   89 
   90     /* Move pointer past the u_int64_t. */
   91     cs->c_next = next + sizeof(*u);
   92     return 0;
   93 }
   94 
   95 /* Unpack a 32-bit unsigned integer. */
   96 int
   97 cpack_uint32(struct cpack_state *cs, u_int32_t *u)
   98 {
   99     u_int8_t *next;
  100 
  101     if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
  102         return -1;
  103 
  104     *u = EXTRACT_LE_32BITS(next);
  105 
  106     /* Move pointer past the u_int32_t. */
  107     cs->c_next = next + sizeof(*u);
  108     return 0;
  109 }
  110 
  111 /* Unpack a 16-bit unsigned integer. */
  112 int
  113 cpack_uint16(struct cpack_state *cs, u_int16_t *u)
  114 {
  115     u_int8_t *next;
  116 
  117     if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
  118         return -1;
  119 
  120     *u = EXTRACT_LE_16BITS(next);
  121 
  122     /* Move pointer past the u_int16_t. */
  123     cs->c_next = next + sizeof(*u);
  124     return 0;
  125 }
  126 
  127 /* Unpack an 8-bit unsigned integer. */
  128 int
  129 cpack_uint8(struct cpack_state *cs, u_int8_t *u)
  130 {
  131     /* No space left? */
  132     if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len)
  133         return -1;
  134 
  135     *u = *cs->c_next;
  136 
  137     /* Move pointer past the u_int8_t. */
  138     cs->c_next++;
  139     return 0;
  140 }
  141 #endif