src/openvpn/compstub.c
38d96bd7
 /*
  *  OpenVPN -- An application to securely tunnel IP networks
  *             over a single UDP port, with support for SSL/TLS-based
  *             session authentication and key exchange,
  *             packet encryption, packet authentication, and
  *             packet compression.
  *
49979459
  *  Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net>
38d96bd7
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2
  *  as published by the Free Software Foundation.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
caa54ac3
  *  You should have received a copy of the GNU General Public License along
  *  with this program; if not, write to the Free Software Foundation, Inc.,
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38d96bd7
  */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #elif defined(_MSC_VER)
 #include "config-msvc.h"
 #endif
 
 #include "syshead.h"
 
 #if defined(USE_COMP)
 
 #include "comp.h"
 #include "error.h"
 #include "otime.h"
 
 #include "memdbg.h"
 
 static void
81d882d5
 stub_compress_init(struct compress_context *compctx)
38d96bd7
 {
 }
 
 static void
81d882d5
 stub_compress_uninit(struct compress_context *compctx)
38d96bd7
 {
 }
 
 static void
81d882d5
 stub_compress(struct buffer *buf, struct buffer work,
               struct compress_context *compctx,
               const struct frame *frame)
38d96bd7
 {
81d882d5
     if (buf->len <= 0)
     {
         return;
     }
     if (compctx->flags & COMP_F_SWAP)
38d96bd7
     {
81d882d5
         uint8_t *head = BPTR(buf);
         uint8_t *tail  = BEND(buf);
         ASSERT(buf_safe(buf, 1));
         ++buf->len;
 
         /* move head byte of payload to tail */
         *tail = *head;
         *head = NO_COMPRESS_BYTE_SWAP;
38d96bd7
     }
81d882d5
     else
38d96bd7
     {
81d882d5
         uint8_t *header = buf_prepend(buf, 1);
         *header = NO_COMPRESS_BYTE;
38d96bd7
     }
 }
 
 static void
81d882d5
 stub_decompress(struct buffer *buf, struct buffer work,
                 struct compress_context *compctx,
                 const struct frame *frame)
38d96bd7
 {
81d882d5
     uint8_t c;
     if (buf->len <= 0)
     {
         return;
     }
     if (compctx->flags & COMP_F_SWAP)
38d96bd7
     {
81d882d5
         uint8_t *head = BPTR(buf);
         c = *head;
         --buf->len;
         *head = *BEND(buf);
         if (c != NO_COMPRESS_BYTE_SWAP)
         {
             dmsg(D_COMP_ERRORS, "Bad compression stub (swap) decompression header byte: %d", c);
             buf->len = 0;
         }
38d96bd7
     }
81d882d5
     else
38d96bd7
     {
81d882d5
         c = *BPTR(buf);
         ASSERT(buf_advance(buf, 1));
         if (c != NO_COMPRESS_BYTE)
         {
             dmsg(D_COMP_ERRORS, "Bad compression stub decompression header byte: %d", c);
             buf->len = 0;
         }
38d96bd7
     }
 }
 
a75bb2e4
 
 static void
81d882d5
 stubv2_compress(struct buffer *buf, struct buffer work,
                 struct compress_context *compctx,
                 const struct frame *frame)
a75bb2e4
 {
     if (buf->len <= 0)
81d882d5
     {
         return;
     }
a75bb2e4
 
81d882d5
     compv2_escape_data_ifneeded(buf);
a75bb2e4
 }
 
 static void
81d882d5
 stubv2_decompress(struct buffer *buf, struct buffer work,
                   struct compress_context *compctx,
                   const struct frame *frame)
a75bb2e4
 {
81d882d5
     if (buf->len <= 0)
     {
         return;
     }
a75bb2e4
 
81d882d5
     uint8_t *head = BPTR(buf);
a75bb2e4
 
81d882d5
     /* no compression or packet to short*/
     if (head[0] != COMP_ALGV2_INDICATOR_BYTE)
     {
         return;
     }
a75bb2e4
 
81d882d5
     /* compression header (0x50) is present */
     buf_advance(buf, 1);
a75bb2e4
 
81d882d5
     /* Packet buffer too short (only 1 byte) */
     if (buf->len <= 0)
     {
         return;
     }
a75bb2e4
 
81d882d5
     head = BPTR(buf);
     buf_advance(buf, 1);
a75bb2e4
 
81d882d5
     if (head[0] != COMP_ALGV2_UNCOMPRESSED_BYTE)
     {
         dmsg(D_COMP_ERRORS, "Bad compression stubv2 decompression header byte: %d", *head);
         buf->len = 0;
         return;
     }
a75bb2e4
 }
 
 const struct compress_alg compv2_stub_alg = {
81d882d5
     "stubv2",
     stub_compress_init,
     stub_compress_uninit,
     stubv2_compress,
     stubv2_decompress
a75bb2e4
 };
 
38d96bd7
 const struct compress_alg comp_stub_alg = {
81d882d5
     "stub",
     stub_compress_init,
     stub_compress_uninit,
     stub_compress,
     stub_decompress
38d96bd7
 };
 
81d882d5
 #else  /* if defined(USE_COMP) */
 static void
4cd4899e
 dummy(void)
 {
81d882d5
 }
38d96bd7
 #endif /* USE_STUB */