cpack.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 1. Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  * 2. Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  * 3. The name of David Young may not be used to endorse or promote
00013  *    products derived from this software without specific prior
00014  *    written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
00017  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00018  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00019  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DAVID
00020  * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00021  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00022  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00025  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00027  * OF SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Modified by Poggi Jerome for WiFiScanner
00032  */
00033 
00034 #include <include.h>
00035 #include <tcpdump-stdinc.h>
00036 
00037 #include "cpack.h"
00038 #include "extract.h"
00039 
00040 static u_int8_t *
00041 cpack_next_boundary(u_int8_t *buf, u_int8_t *p, size_t alignment)
00042 {
00043   size_t misalignment = (size_t)(p - buf) % alignment;
00044 
00045   if (misalignment == 0)
00046     return p;
00047 
00048   return p + (alignment - misalignment);
00049 }
00050 
00051 /* Advance to the next wordsize boundary. Return NULL if fewer than
00052  * wordsize bytes remain in the buffer after the boundary.  Otherwise,
00053  * return a pointer to the boundary.
00054  */
00055 static u_int8_t *
00056 cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize)
00057 {
00058   u_int8_t *next;
00059 
00060   /* Ensure alignment. */
00061   next = cpack_next_boundary(cs->c_buf, cs->c_next, wordsize);
00062 
00063   /* Too little space for wordsize bytes? */
00064   if (next - cs->c_buf + wordsize > cs->c_len)
00065     return NULL;
00066 
00067   return next;
00068 }
00069 
00070 int
00071 cpack_init(struct cpack_state *cs, u_int8_t *buf, size_t buflen)
00072 {
00073   memset(cs, 0, sizeof(*cs));
00074 
00075   cs->c_buf = buf;
00076   cs->c_len = buflen;
00077   cs->c_next = cs->c_buf;
00078 
00079   return 0;
00080 }
00081 
00082 /* Unpack a 64-bit unsigned integer. */
00083 int
00084 cpack_uint64(struct cpack_state *cs, u_int64_t *u)
00085 {
00086   u_int8_t *next;
00087 
00088   if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
00089     return -1;
00090 
00091   *u = EXTRACT_LE_64BITS(next);
00092 
00093   /* Move pointer past the u_int64_t. */
00094   cs->c_next = next + sizeof(*u);
00095   return 0;
00096 }
00097 
00098 /* Unpack a 32-bit unsigned integer. */
00099 int
00100 cpack_uint32(struct cpack_state *cs, u_int32_t *u)
00101 {
00102   u_int8_t *next;
00103 
00104   if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
00105     return -1;
00106 
00107   *u = EXTRACT_LE_32BITS(next);
00108 
00109   /* Move pointer past the u_int32_t. */
00110   cs->c_next = next + sizeof(*u);
00111   return 0;
00112 }
00113 
00114 /* Unpack a 16-bit unsigned integer. */
00115 int
00116 cpack_uint16(struct cpack_state *cs, u_int16_t *u)
00117 {
00118   u_int8_t *next;
00119 
00120   if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
00121     return -1;
00122 
00123   *u = EXTRACT_LE_16BITS(next);
00124 
00125   /* Move pointer past the u_int16_t. */
00126   cs->c_next = next + sizeof(*u);
00127   return 0;
00128 }
00129 
00130 /* Unpack an 8-bit unsigned integer. */
00131 int
00132 cpack_uint8(struct cpack_state *cs, u_int8_t *u)
00133 {
00134   /* No space left? */
00135   if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len)
00136     return -1;
00137 
00138   *u = *cs->c_next;
00139 
00140   /* Move pointer past the u_int8_t. */
00141   cs->c_next++;
00142   return 0;
00143 }

Generated on Fri Jul 25 17:10:33 2008 for WifiScanner by  doxygen 1.5.5