home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / rfc / 1900s / rfc1977.txt < prev    next >
Text File  |  1996-08-07  |  51KB  |  1,404 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                        V. Schryver
  8. Request for Comments: 1977                                   August 1996
  9. Category: Informational
  10.  
  11.  
  12.                       PPP BSD Compression Protocol
  13.  
  14. Status of This Memo
  15.  
  16.    This memo provides information for the Internet community.  This memo
  17.    does not specify an Internet standard of any kind.  Distribution of
  18.    this memo is unlimited.
  19.  
  20. Abstract
  21.  
  22.    The Point-to-Point Protocol (PPP) [1] provides a standard method for
  23.    transporting multi-protocol datagrams over point-to-point links.
  24.  
  25.    The PPP Compression Control Protocol [2] provides a method to
  26.    negotiate and utilize compression protocols over PPP encapsulated
  27.    links.
  28.  
  29.    This document describes the use of the Unix Compress compression
  30.    protocol for compressing PPP encapsulated packets.
  31.  
  32. Table of Contents
  33.  
  34.         1.     Introduction ....................................    1
  35.            1.1       Licensing .................................    2
  36.         2.     BSD Compress Packets ............................    2
  37.            2.1       Packet Format .............................    5
  38.         3.     Configuration Option Format .....................    6
  39.         APPENDICES .............................................    7
  40.         A.     BSD Compress Algorithm ..........................    7
  41.         SECURITY CONSIDERATIONS ................................   24
  42.         REFERENCES .............................................   24
  43.         ACKNOWLEDGEMENTS .......................................   24
  44.         CHAIR'S ADDRESS ........................................   25
  45.         AUTHOR'S ADDRESS .......................................   25
  46.  
  47. 1.  Introduction
  48.  
  49.    UNIX compress as embodied in the freely and widely distributed BSD
  50.    source has the following features:
  51.  
  52.        - dynamic table clearing when compression becomes less
  53.          effective.
  54.  
  55.  
  56.  
  57.  
  58. Schryver                     Informational                      [Page 1]
  59.  
  60. RFC 1977                    PPP BSD Compress                 August 1996
  61.  
  62.  
  63.        - automatic turning off of compression when the overall result
  64.          is not smaller than the input.
  65.  
  66.        - dynamic choice of code width within predetermined limits.
  67.  
  68.        - heavily used for many years in networks, on modem and other
  69.          point-to-point links to transfer netnews.
  70.  
  71.        - an effective code width requires less than 64KBytes of memory
  72.          on both sender and receive.
  73.  
  74. 1.1.  Licensing
  75.  
  76.    BSD Unix compress command source is widely and freely available, with
  77.    no additional license for many computer vendors.  The included source
  78.    code is based on the BSD compress command source and carries only the
  79.    copyright of The Regents of the University of California.  Use the
  80.    code entirely at your own risk. It has no warranties or
  81.    indemnifications of any sort.  Note that there are patents on LZW.
  82.  
  83. 2.  BSD Compress Packets
  84.  
  85.    Before any BSD Compress packets may be communicated, PPP must reach
  86.    the Network-Layer Protocol phase, and the CCP Control Protocol must
  87.    reach the Opened state.
  88.  
  89.    Exactly one BSD Compress datagram is encapsulated in the PPP
  90.    Information field, where the PPP Protocol field contains 0xFD or
  91.    0xFB.  0xFD is used when the PPP multilink protocol is not used or
  92.    "above" multilink.  0xFB is used "below" multilink, to compress
  93.    independently on individual links of a multilink bundle.
  94.  
  95.    The maximum length of the BSD Compress datagram transmitted over a
  96.    PPP link is the same as the maximum length of the Information field
  97.    of a PPP encapsulated packet.
  98.  
  99.    Only packets with PPP Protocol numbers in the range 0x0000 to 0x3FFF
  100.    and neither 0xFD nor 0xFB are compressed.  Other PPP packets are
  101.    always sent uncompressed.  Control packets are infrequent and should
  102.    not be compressed for robustness.
  103.  
  104.    Padding
  105.  
  106.       BSD Compress packets require the previous negotiation of the
  107.       Self-Describing-Padding Configuration Option [3] if padding is
  108.       added to packets.  If no padding is added, than Self-Describing-
  109.       Padding is not required.
  110.  
  111.  
  112.  
  113.  
  114. Schryver                     Informational                      [Page 2]
  115.  
  116. RFC 1977                    PPP BSD Compress                 August 1996
  117.  
  118.  
  119.    Reliability and Sequencing
  120.  
  121.       BSD Compress requires the packets to be delivered in sequence.  It
  122.       relies on Reset-Request and Reset-Ack CCP packets or on
  123.       renegotiation of the Compression Control Protocol [2] to indicate
  124.       loss of synchronization between the transmitter and receiver.  The
  125.       HDLC FCS detects corrupted packets and the normal mechanisms
  126.       discard them.  Missing or out of order packets are detected by the
  127.       sequence number in each packet.  The packet sequence number ought
  128.       to be checked before decoding the packet.
  129.  
  130.       Instead of transmitting a Reset-Request packet when detecting a
  131.       decompression error, the receiver MAY momentary force CCP to drop
  132.       out of the Opened state by transmitting a new CCP Configure-
  133.       Request.  This method is more expensive than using Reset-Requests.
  134.  
  135.       When the receiver first encounters an unexpected sequence number
  136.       it SHOULD send a Reset-Request CCP packet as defined in the
  137.       Compression Control Protocol.  When the transmitter sends the
  138.       Reset-Ack or when the receiver receives a Reset-ACK, they must
  139.       reset the sequence number to zero, clear the compression
  140.       dictionary, and resume sending and receiving compressed packets.
  141.       The receiver MUST discard all compressed packets after detecting
  142.       an error and until it receives a Reset-Ack.  This strategy can be
  143.       thought of as abandoning the transmission of one "file" and
  144.       starting the transmission of a new "file."
  145.  
  146.       The transmitter must clear its compression dictionary and respond
  147.       with a Reset-Ack each time it receives a Reset-Request, because it
  148.       cannot know if previous Reset-Acks reached the receiver.  The
  149.       receiver MUST clear its compression dictionary each time it
  150.       receives a Reset-Ack, because the transmitter will have cleared
  151.       its compression dictionary.
  152.  
  153.       When the link is busy, one decompression error is usually followed
  154.       by several more before the Reset-Ack can be received.  It is
  155.       undesirable to transmit Reset-Requests more frequently than the
  156.       round-trip-time of the link, because redundant Reset-Requests
  157.       cause unnecessary compression dictionary clearing.  The receiver
  158.       MAY transmit an additional Reset-Request each time it receives a
  159.       compressed or uncompressed packet until it finally receives a
  160.       Reset-Ack, but the receiver ought not transmit another Reset-
  161.       Request until the Reset-Ack for the previous one is late.  The
  162.       receiver MUST transmit enough Reset-Request packets to ensure that
  163.       the transmitter receives at least one.  For example, the receiver
  164.       might choose to not transmit another Reset-Request until after one
  165.       second (or, of course, a Reset-Ack has been received and
  166.       decompression resumed).
  167.  
  168.  
  169.  
  170. Schryver                     Informational                      [Page 3]
  171.  
  172. RFC 1977                    PPP BSD Compress                 August 1996
  173.  
  174.  
  175.    Data Expansion
  176.  
  177.       When significant data expansion is detected, the PPP packet MUST
  178.       be sent without compression.  Packets that would expand by fewer
  179.       than 3 bytes SHOULD be sent without compression, but MAY be sent
  180.       compressed provided the result does not exceed the MTU of the
  181.       link.  This makes moot standards document exegesises about exactly
  182.       which bytes, such as the Protocol fields, count toward expansion.
  183.  
  184.       When a packet is received with PPP Protocol numbers in the range
  185.       0x0000 to 0x3FFF, (except, of course, 0xFD and 0xFB) it is assumed
  186.       that the packet would have caused expansion.  The packet is
  187.       locally compressed to update the compression history.
  188.  
  189.       Sending incompressible packets in their native encapsulation
  190.       avoids maximum transmission unit complications.  If uncompressed
  191.       packets could be larger than their native form, then it would be
  192.       necessary for the upper layers of an implementation to treat the
  193.       PPP link as if it had a smaller MTU, to ensure that compressed
  194.       incompressible packets are never larger than the negotiated PPP
  195.       MTU.
  196.  
  197.       Using native encapsulation for incompressible packets complicates
  198.       the implementation.  The transmitter and the receiver must start
  199.       putting information into the compression dictionary starting with
  200.       the same packets, without relying upon seeing a compressed packet
  201.       for synchronization.  The first few packets after clearing the
  202.       dictionary are usually incompressible, and so are likely to sent
  203.       in their native encapsulation, just like packets before
  204.       compression is turned on.  If CCP or LCP packets are handled
  205.       separately from Network-Layer packets (e.g. a "daemon" for control
  206.       packets and "kernel code" for data packets), care must be taken to
  207.       ensure that the transmitter synchronizes clearing the dictionary
  208.       with the transmission of the configure-ACK or Reset-Ack that
  209.       starts compression, and the receiver must similarly ensure that
  210.       its dictionary is cleared before it processes the next packet.
  211.  
  212.       A difficulty caused by sending data that would expand uncompressed
  213.       is that the receiver must adaptively clear its dictionary at
  214.       precisely the same times as the sender.  In the classic BSD
  215.       compression code, the dictionary clearing is signaled by the
  216.       reserved code 256.  Because data that would expend is sent without
  217.       compression, there is no reliable way for the sender to signal
  218.       explicitly when it has cleared its dictionary.  This difficulty is
  219.       resolved by specifying the parameters that control the dictionary
  220.       clearing, and having both sender and receiver clear their
  221.       dictionaries at the same times.
  222.  
  223.  
  224.  
  225.  
  226. Schryver                     Informational                      [Page 4]
  227.  
  228. RFC 1977                    PPP BSD Compress                 August 1996
  229.  
  230.  
  231. 2.1.  Packet Format
  232.  
  233.    A summary of the BSD Compress packet format is shown below.
  234.  
  235.    The fields are transmitted from left to right.
  236.  
  237.     0                   1                   2                   3
  238.     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  239.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  240.    |         PPP Protocol          |           Sequence
  241.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  242.    |   Data ...
  243.    +-+-+-+-+-+-+-+-+
  244.  
  245.  
  246.    PPP Protocol
  247.  
  248.       The PPP Protocol field is described in the Point-to-Point Protocol
  249.       Encapsulation [1].
  250.  
  251.       When the BSD Compress compression protocol is successfully
  252.       negotiated by the PPP Compression Control Protocol [2], the value
  253.       of the protocol field is 0xFD or 0xFB.  This value MAY be
  254.       compressed when Protocol-Field-Compression is negotiated.
  255.  
  256.    Sequence
  257.  
  258.       The sequence number is sent most significant octet first.  It
  259.       starts at 0 when the dictionary is cleared, and is incremented by
  260.       1 after each packet, including uncompressed packets.  The sequence
  261.       number after 65535 is zero.  In other words, the sequence number
  262.       "wraps" in the usual way.
  263.  
  264.       The sequence number ensures that lost or out of order packets do
  265.       not cause the compression databases of the peers to become
  266.       unsynchronized.  When an unexpected sequence number is
  267.       encountered, the dictionaries must be resynchronized with a CCP
  268.       Reset-Request or Configure-Request.  The packet sequence number
  269.       can be checked before a compressed packet is decoded.
  270.  
  271.    Data
  272.  
  273.       The compressed PPP encapsulated packet, consisting of the Protocol
  274.       and Data fields of the original, uncompressed packet follows.
  275.  
  276.       The Protocol field compression MUST be applied to the protocol
  277.       field in the original packet before the sequence number is
  278.       computed or the entire packet is compressed, regardless of whether
  279.  
  280.  
  281.  
  282. Schryver                     Informational                      [Page 5]
  283.  
  284. RFC 1977                    PPP BSD Compress                 August 1996
  285.  
  286.  
  287.       the PPP protocol field compression has been negotiated.  Thus, if
  288.       the original protocol number was less than 0x100, it must be
  289.       compressed to a single byte.
  290.  
  291.       The format of the compressed data is more precisely described by
  292.       the example code in the "BSD Compress Algorithm" appendix.
  293.  
  294. 3.  Configuration Option Format
  295.  
  296.  
  297.    Description
  298.  
  299.       The CCP BSD Compress Configuration Option negotiates the use of
  300.       BSD Compress on the link.  By default or ultimate disagreement, no
  301.       compression is used.
  302.  
  303.    A summary of the BSD Compress Configuration Option format is shown
  304.    below.  The fields are transmitted from left to right.
  305.  
  306.     0                   1                   2
  307.     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
  308.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  309.    |     Type      |    Length     | Vers|   Dict  |
  310.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  311.  
  312.  
  313.    Type
  314.  
  315.       21 or 0x15 for BSD compress.
  316.  
  317.    Length
  318.  
  319.       3
  320.  
  321.    Vers
  322.  
  323.       Must be the binary number 001.
  324.  
  325.    Dict
  326.  
  327.       The size in bits of the largest code used.  It can range from 9 to
  328.       16.  A common choice is 12.  The code included below can support
  329.       code sizes from 9 to 15.
  330.  
  331.       It is convenient to treat the byte containing the Vers and Dict
  332.       fields as a single field with legal values ranging from 0x29 to
  333.       0x30.
  334.  
  335.  
  336.  
  337.  
  338. Schryver                     Informational                      [Page 6]
  339.  
  340. RFC 1977                    PPP BSD Compress                 August 1996
  341.  
  342.  
  343.       Note that the peer receiving compressed data must use the same
  344.       code size as the peer sending data. It is not practical for the
  345.       receiver to use a larger dictionary or code size, because both
  346.       dictionaries must be cleared at the same time, even when the data
  347.       is not compressible, so that uncompressed packets are being sent,
  348.       and so the receiver cannot receive LZW "CLEAR" codes.
  349.  
  350.       When a received Configure-Request specifies a smaller dictionary
  351.       than the local preference, it is often best to accept it instead
  352.       of using a Configure-Nak to ask the peer to specify a larger
  353.       dictionary.
  354.  
  355. A.  BSD Compress Algorithm
  356.  
  357.    This code is the core of a commercial workstation implementation.  It
  358.    was derived by transliterating the 4.*BSD compress command.  It is
  359.    unlikely to be of direct use in any system that does not have the
  360.    same mixture of mbufs and STREAMS buffers.  It may need to be retuned
  361.    for CPU's other than RISC's with many registers and certain
  362.    addressing modes.  However, the code is the most accurate and
  363.    unambiguous way of defining the changes to the BSD compress source
  364.    required to apply it to a stream instead of a file.
  365.  
  366.    Note that it assumes a "short" contains 16 bits and an "int" contains
  367.    at least 32 bits.  Where it would matter if more than 32 bits were in
  368.    an "int" or "long," __uint32_t is used instead.
  369.  
  370. /* Because this code is derived from the 4.3BSD compress source:
  371.  *
  372.  *
  373.  * Copyright (c) 1985, 1986 The Regents of the University of California.
  374.  * All rights reserved.
  375.  *
  376.  * This code is derived from software contributed to Berkeley by
  377.  * James A. Woods, derived from original work by Spencer Thomas
  378.  * and Joseph Orost.
  379.  *
  380.  * Redistribution and use in source and binary forms, with or without
  381.  * modification, are permitted provided that the following conditions
  382.  * are met:
  383.  * 1. Redistributions of source code must retain the above copyright
  384.  *    notice, this list of conditions and the following disclaimer.
  385.  * 2. Redistributions in binary form must reproduce the above copyright
  386.  *    notice, this list of conditions and the following disclaimer in
  387.  *    the documentation and/or other materials provided with the
  388.  *    distribution.
  389.  * 3. All advertising materials mentioning features or use of this
  390.  *    software must display the following acknowledgement:
  391.  
  392.  
  393.  
  394. Schryver                     Informational                      [Page 7]
  395.  
  396. RFC 1977                    PPP BSD Compress                 August 1996
  397.  
  398.  
  399.  *      This product includes software developed by the University of
  400.  *      California, Berkeley and its contributors.
  401.  * 4. Neither the name of the University nor the names of its
  402.  *    contributors may be used to endorse or promote products derived
  403.  *    from this software without specific prior written permission.
  404.  *
  405.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
  406.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  407.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  408.  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS
  409.  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  410.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  411.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  412.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  413.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  414.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  415.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  416.  */
  417.  
  418.  
  419. /* ***************** */
  420.  
  421. struct bsd_db {
  422.     int     totlen;                     /* length of this structure */
  423.     u_int   hsize;                      /* size of the hash table */
  424.     u_char  hshift;                     /* used in hash function */
  425.     u_char  n_bits;                     /* current bits/code */
  426.     u_char  debug;
  427.     u_char  unit;
  428.     u_short mru;
  429.     u_short seqno;                      /* # of last byte of packet */
  430.     u_int   maxmaxcode;                 /* largest valid code */
  431.     u_int   max_ent;                    /* largest code in use */
  432.     u_int   in_count;                   /* uncompressed bytes */
  433.     u_int   bytes_out;                  /* compressed bytes */
  434.     u_int   ratio;                      /* recent compression ratio */
  435.     u_int   checkpoint;                 /* when to next check ratio */
  436.     int     clear_count;                /* times dictionary cleared */
  437.     int     incomp_count;               /* incompressible packets */
  438.     int     decomp_count;               /* packets decompressed */
  439.     int     overshoot;                  /* excess decompression buf */
  440.     int     undershoot;                 /* insufficient decomp. buf */
  441.     u_short *lens;                      /* array of lengths of codes */
  442.     struct bsd_dict {
  443.         union {                         /* hash value */
  444.             __uint32_t  fcode;
  445.             struct {
  446. #ifdef BSD_LITTLE_ENDIAN
  447.  
  448.  
  449.  
  450. Schryver                     Informational                      [Page 8]
  451.  
  452. RFC 1977                    PPP BSD Compress                 August 1996
  453.  
  454.  
  455.                 u_short prefix;         /* preceding code */
  456.                 u_char  suffix;         /* last character of new code */
  457.                 u_char  pad;
  458. #else
  459.                 u_char  pad;
  460.                 u_char  suffix;         /* last character of new code */
  461.                 u_short prefix;         /* preceding code */
  462. #endif
  463.             } hs;
  464.         } f;
  465.         u_short codem1;                 /* output of hash table -1 */
  466.         u_short cptr;                   /* map code to hash table */
  467.     } dict[1];
  468. };
  469. #define BSD_OVHD (2+2)                  /* overhead/packet */
  470. #define MIN_BSD_BITS    9
  471. #define MAX_BSD_BITS    15              /* implementation limit */
  472. #define BSD_VERS        1               /* when shifted */
  473. #ifdef _KERNEL
  474. extern struct bsd_db *pf_bsd_init(struct bsd_db*, int, int, int);
  475. extern int pf_bsd_comp(struct bsd_db*,u_char*,int,struct mbuf*,int);
  476. extern mblk_t* pf_bsd_decomp(struct bsd_db*, mblk_t*);
  477. extern void pf_bsd_incomp(struct bsd_db*, mblk_t*, u_int);
  478. #endif
  479.  
  480.  
  481. /* ***************** */
  482. /* PPP "BSD compress" compression
  483.  *  The differences between this compression and the classic BSD LZW
  484.  *  source are obvious from the requirement that the classic code worked
  485.  *  with files while this handles arbitrarily long streams that
  486.  *  are broken into packets.  They are:
  487.  *
  488.  *      When the code size expands, a block of junk is not emitted by
  489.  *          the compressor and not expected by the decompressor.
  490.  *
  491.  *      New codes are not necessarily assigned every time an old
  492.  *          code is output by the compressor.  This is because a packet
  493.  *          end forces a code to be emitted, but does not imply that a
  494.  *          new sequence has been seen.
  495.  *
  496.  *      The compression ratio is checked at the first end of a packet
  497.  *          after the appropriate gap.  Besides simplifying and speeding
  498.  *          things up, this makes it more likely that the transmitter
  499.  *          and receiver will agree when the dictionary is cleared when
  500.  *          compression is not going well.
  501.  */
  502.  
  503.  
  504.  
  505.  
  506. Schryver                     Informational                      [Page 9]
  507.  
  508. RFC 1977                    PPP BSD Compress                 August 1996
  509.  
  510.  
  511. /*
  512.  * the next two codes should not be changed lightly, as they must not
  513.  * lie within the contiguous general code space.
  514.  */
  515. #define CLEAR   256                     /* table clear output code */
  516. #define FIRST   257                     /* first free entry */
  517. #define LAST    255
  518.  
  519. #define BSD_INIT_BITS   MIN_BSD_BITS
  520.  
  521. #define MAXCODE(b) ((1 << (b)) - 1)
  522. #define BADCODEM1 MAXCODE(MAX_BSD_BITS);
  523.  
  524. #define BSD_HASH(prefix,suffix,hshift) ((((__uint32_t)(suffix)) \
  525.                                          << (hshift)) \
  526.                                         ^ (__uint32_t)(prefix))
  527. #define BSD_KEY(prefix,suffix) ((((__uint32_t)(suffix)) << 16)  \
  528.                                 + (__uint32_t)(prefix))
  529.  
  530. #define CHECK_GAP       10000           /* Ratio check interval */
  531.  
  532. #define RATIO_SCALE_LOG 8
  533. #define RATIO_SCALE     (1<<RATIO_SCALE_LOG)
  534. #define RATIO_MAX       (0x7fffffff>>RATIO_SCALE_LOG)
  535.  
  536. /* clear the dictionary
  537.  */
  538. static void
  539. pf_bsd_clear(struct bsd_db *db)
  540. {
  541.         db->clear_count++;
  542.         db->max_ent = FIRST-1;
  543.         db->n_bits = BSD_INIT_BITS;
  544.         db->ratio = 0;
  545.         db->bytes_out = 0;
  546.         db->in_count = 0;
  547.         db->incomp_count = 0;
  548.         db->decomp_count = 0;
  549.         db->overshoot = 0;
  550.         db->undershoot = 0;
  551.         db->checkpoint = CHECK_GAP;
  552. }
  553.  
  554.  
  555. /* If the dictionary is full, then see if it is time to reset it.
  556.  *
  557.  * Compute the compression ratio using fixed-point arithmetic
  558.  * with 8 fractional bits.
  559.  
  560.  
  561.  
  562. Schryver                     Informational                     [Page 10]
  563.  
  564. RFC 1977                    PPP BSD Compress                 August 1996
  565.  
  566.  
  567.  *
  568.  * Since we have an infinite stream instead of a single file,
  569.  * watch only the local compression ratio.
  570.  *
  571.  * Since both peers must reset the dictionary at the same time even in
  572.  * the absence of CLEAR codes (while packets are incompressible), they
  573.  * must compute the same ratio.
  574.  */
  575. static int                              /* 1=output CLEAR */
  576. pf_bsd_check(struct bsd_db *db)
  577. {
  578.         register u_int new_ratio;
  579.  
  580.         if (db->in_count >= db->checkpoint) {
  581.                 /* age the ratio by limiting the size of the counts */
  582.                 if (db->in_count >= RATIO_MAX
  583.                     || db->bytes_out >= RATIO_MAX) {
  584.                         db->in_count -= db->in_count/4;
  585.                         db->bytes_out -= db->bytes_out/4;
  586.                 }
  587.  
  588.                 db->checkpoint = db->in_count + CHECK_GAP;
  589.  
  590.                 if (db->max_ent >= db->maxmaxcode) {
  591.                         /* Reset the dictionary only if the ratio is
  592.                          * worse, or if it looks as if it has been
  593.                          * poisoned by incompressible data.
  594.                          *
  595.                          * This does not overflow, because
  596.                          *      db->in_count <= RATIO_MAX.
  597.                          */
  598.                         new_ratio = db->in_count<<RATIO_SCALE_LOG;
  599.                         if (db->bytes_out != 0)
  600.                                 new_ratio /= db->bytes_out;
  601.  
  602.                         if (new_ratio < db->ratio
  603.                             || new_ratio < 1*RATIO_SCALE) {
  604.                                 pf_bsd_clear(db);
  605.                                 return 1;
  606.                         }
  607.                         db->ratio = new_ratio;
  608.                 }
  609.         }
  610.         return 0;
  611. }
  612.  
  613.  
  614. /* Initialize the database.
  615.  
  616.  
  617.  
  618. Schryver                     Informational                     [Page 11]
  619.  
  620. RFC 1977                    PPP BSD Compress                 August 1996
  621.  
  622.  
  623.  */
  624. struct bsd_db *
  625. pf_bsd_init(struct bsd_db *db,          /* initialize this database */
  626.             int unit,                   /* for debugging */
  627.             int bits,                   /* size of LZW code word */
  628.             int mru)                    /* MRU for input, 0 for output*/
  629. {
  630.         register int i;
  631.         register u_short *lens;
  632.         register u_int newlen, hsize, hshift, maxmaxcode;
  633.  
  634.         switch (bits) {
  635.         case 9:                         /* needs 82152 for both comp &*/
  636.         case 10:                        /* needs 84144          decomp*/
  637.         case 11:                        /* needs 88240 */
  638.         case 12:                        /* needs 96432 */
  639.                 hsize = 5003;
  640.                 hshift = 4;
  641.                 break;
  642.         case 13:                        /* needs 176784 */
  643.                 hsize = 9001;
  644.                 hshift = 5;
  645.                 break;
  646.         case 14:                        /* needs 353744 */
  647.                 hsize = 18013;
  648.                 hshift = 6;
  649.                 break;
  650.         case 15:                        /* needs 691440 */
  651.                 hsize = 35023;
  652.                 hshift = 7;
  653.                 break;
  654.         case 16:                        /* needs 1366160--far too much*/
  655.                 /* hsize = 69001; */    /* and 69001 is too big for */
  656.                 /* hshift = 8; */       /* cptr in struct bsd_db */
  657.                 /* break; */
  658.         default:
  659.                 if (db) {
  660.                         if (db->lens)
  661.                                 kern_free(db->lens);
  662.                         kern_free(db);
  663.                 }
  664.                 return 0;
  665.         }
  666.         maxmaxcode = MAXCODE(bits);
  667.         newlen = sizeof(*db) + (hsize-1)*(sizeof(db->dict[0]));
  668.  
  669.         if (db) {
  670.                 lens = db->lens;
  671.  
  672.  
  673.  
  674. Schryver                     Informational                     [Page 12]
  675.  
  676. RFC 1977                    PPP BSD Compress                 August 1996
  677.  
  678.  
  679.                 if (db->totlen != newlen) {
  680.                         if (lens)
  681.                                 kern_free(lens);
  682.                         kern_free(db);
  683.                         db = 0;
  684.                 }
  685.         }
  686.         if (!db) {
  687.                 db = (struct bsd_db*)kern_malloc(newlen);
  688.                 if (!db)
  689.                         return 0;
  690.                 if (mru == 0) {
  691.                         lens = 0;
  692.                 } else {
  693.                         lens = (u_short*)kern_malloc((maxmaxcode+1)
  694.                                                      * sizeof(*lens));
  695.                         if (!lens) {
  696.                                 kern_free(db);
  697.                                 return 0;
  698.                         }
  699.                         i = LAST+1;
  700.                         while (i != 0)
  701.                                 lens[--i] = 1;
  702.                 }
  703.                 i = hsize;
  704.                 while (i != 0) {
  705.                         db->dict[--i].codem1 = BADCODEM1;
  706.                         db->dict[i].cptr = 0;
  707.                 }
  708.         }
  709.  
  710.         bzero(db,sizeof(*db)-sizeof(db->dict));
  711.         db->lens = lens;
  712.         db->unit = unit;
  713.         db->mru = mru;
  714.         db->hsize = hsize;
  715.         db->hshift = hshift;
  716.         db->maxmaxcode = maxmaxcode;
  717.         db->clear_count = -1;
  718.  
  719.         pf_bsd_clear(db);
  720.  
  721.         return db;
  722. }
  723.  
  724.  
  725. /* compress a packet
  726.  *      Assume the protocol is known to be >= 0x21 and < 0xff.
  727.  
  728.  
  729.  
  730. Schryver                     Informational                     [Page 13]
  731.  
  732. RFC 1977                    PPP BSD Compress                 August 1996
  733.  
  734.  
  735.  *      One change from the BSD compress command is that when the
  736.  *      code size expands, we do not output a bunch of padding.
  737.  */
  738. int                                     /* new slen */
  739. pf_bsd_comp(struct bsd_db *db,
  740.             u_char *cp_buf,             /* compress into here */
  741.             int proto,                  /* this original PPP protocol */
  742.             struct mbuf *m,             /* from here */
  743.             int slen)
  744. {
  745.         register int hshift = db->hshift;
  746.         register u_int max_ent = db->max_ent;
  747.         register u_int n_bits = db->n_bits;
  748.         register u_int bitno = 32;
  749.         register __uint32_t accum = 0;
  750.         register struct bsd_dict *dictp;
  751.         register __uint32_t fcode;
  752.         register u_char c;
  753.         register int hval, disp, ent;
  754.         register u_char *rptr, *wptr;
  755.         struct mbuf *n;
  756.  
  757. #define OUTPUT(ent) {                   \
  758.         bitno -= n_bits;                \
  759.         accum |= ((ent) << bitno);      \
  760.         do {                            \
  761.                 *wptr++ = accum>>24;    \
  762.                 accum <<= 8;            \
  763.                 bitno += 8;             \
  764.         } while (bitno <= 24);          \
  765.         }
  766.  
  767.  
  768.         /* start with the protocol byte */
  769.         ent = proto;
  770.         db->in_count++;
  771.  
  772.         /* install sequence number */
  773.         cp_buf[0] = db->seqno>>8;
  774.         cp_buf[1] = db->seqno;
  775.         db->seqno++;
  776.  
  777.         wptr = &cp_buf[2];
  778.         slen = m->m_len;
  779.         db->in_count += slen;
  780.         rptr = mtod(m, u_char*);
  781.         n = m->m_next;
  782.         for (;;) {
  783.  
  784.  
  785.  
  786. Schryver                     Informational                     [Page 14]
  787.  
  788. RFC 1977                    PPP BSD Compress                 August 1996
  789.  
  790.  
  791.                 if (slen == 0) {
  792.                         if (!n)
  793.                                 break;
  794.                         slen = n->m_len;
  795.                         rptr = mtod(n, u_char*);
  796.                         n = n->m_next;
  797.                         if (!slen)
  798.                                 continue;   /* handle 0-length buffers*/
  799.                         db->in_count += slen;
  800.                 }
  801.  
  802.                 slen--;
  803.                 c = *rptr++;
  804.                 fcode = BSD_KEY(ent,c);
  805.                 hval = BSD_HASH(ent,c,hshift);
  806.                 dictp = &db->dict[hval];
  807.  
  808.                 /* Validate and then check the entry. */
  809.                 if (dictp->codem1 >= max_ent)
  810.                         goto nomatch;
  811.                 if (dictp->f.fcode == fcode) {
  812.                         ent = dictp->codem1+1;
  813.                         continue;       /* found (prefix,suffix) */
  814.                 }
  815.  
  816.                 /* continue probing until a match or invalid entry */
  817.                 disp = (hval == 0) ? 1 : hval;
  818.                 do {
  819.                         hval += disp;
  820.                         if (hval >= db->hsize)
  821.                                 hval -= db->hsize;
  822.                         dictp = &db->dict[hval];
  823.                         if (dictp->codem1 >= max_ent)
  824.                                 goto nomatch;
  825.                 } while (dictp->f.fcode != fcode);
  826.                 ent = dictp->codem1+1;  /* found (prefix,suffix) */
  827.                 continue;
  828.  
  829. nomatch:
  830.                 OUTPUT(ent);            /* output the prefix */
  831.  
  832.                 /* code -> hashtable */
  833.                 if (max_ent < db->maxmaxcode) {
  834.                         struct bsd_dict *dictp2;
  835.                         /* expand code size if needed */
  836.                         if (max_ent >= MAXCODE(n_bits))
  837.                                 db->n_bits = ++n_bits;
  838.  
  839.  
  840.  
  841.  
  842. Schryver                     Informational                     [Page 15]
  843.  
  844. RFC 1977                    PPP BSD Compress                 August 1996
  845.  
  846.  
  847.                         /* Invalidate old hash table entry using
  848.                          * this code, and then take it over.
  849.                          */
  850.                         dictp2 = &db->dict[max_ent+1];
  851.                         if (db->dict[dictp2->cptr].codem1 == max_ent)
  852.                                 db->dict[dictp2->cptr].codem1=BADCODEM1;
  853.                         dictp2->cptr = hval;
  854.                         dictp->codem1 = max_ent;
  855.                         dictp->f.fcode = fcode;
  856.  
  857.                         db->max_ent = ++max_ent;
  858.                 }
  859.                 ent = c;
  860.         }
  861.  
  862.         OUTPUT(ent);                    /* output the last code */
  863.         db->bytes_out += (wptr-&cp_buf[2]   /* count complete bytes */
  864.                           + (32-bitno+7)/8);
  865.  
  866.         if (pf_bsd_check(db))
  867.                 OUTPUT(CLEAR);          /* do not count the CLEAR */
  868.  
  869.         /* Pad dribble bits of last code with ones.
  870.          * Do not emit a completely useless byte of ones.
  871.          */
  872.         if (bitno != 32)
  873.                 *wptr++ = (accum | (0xff << (bitno-8))) >> 24;
  874.  
  875.         /* Increase code size if we would have without the packet
  876.          * boundary and as the decompressor will.
  877.          */
  878.         if (max_ent >= MAXCODE(n_bits)
  879.             && max_ent < db->maxmaxcode)
  880.                 db->n_bits++;
  881.  
  882.         return (wptr - cp_buf);
  883. #undef OUTPUT
  884. }
  885.  
  886.  
  887. /* Update the "BSD Compress" dictionary on the receiver for
  888.  * incompressible data by pretending to compress the incoming data.
  889.  */
  890. void
  891. pf_bsd_incomp(struct bsd_db *db,
  892.               mblk_t *dmsg,
  893.               u_int ent)                /* start with protocol byte */
  894. {
  895.  
  896.  
  897.  
  898. Schryver                     Informational                     [Page 16]
  899.  
  900. RFC 1977                    PPP BSD Compress                 August 1996
  901.  
  902.  
  903.         register u_int hshift = db->hshift;
  904.         register u_int max_ent = db->max_ent;
  905.         register u_int n_bits = db->n_bits;
  906.         register struct bsd_dict *dictp;
  907.         register __uint32_t fcode;
  908.         register u_char c;
  909.         register int hval, disp;
  910.         register int slen;
  911.         register u_int bitno = 7;
  912.         register u_char *rptr;
  913.  
  914.         db->incomp_count++;
  915.  
  916.         db->in_count++;                 /* count protocol as 1 byte */
  917.         db->seqno++;
  918.         rptr = dmsg->b_rptr+PPP_BUF_HEAD_INFO;
  919.         for (;;) {
  920.                 slen = dmsg->b_wptr - rptr;
  921.                 if (slen == 0) {
  922.                         dmsg = dmsg->b_cont;
  923.                         if (!dmsg)
  924.                                 break;
  925.                         rptr = dmsg->b_rptr;
  926.                         continue;       /* skip zero-length buffers */
  927.                 }
  928.                 db->in_count += slen;
  929.  
  930.                 do {
  931.                         c = *rptr++;
  932.                         fcode = BSD_KEY(ent,c);
  933.                         hval = BSD_HASH(ent,c,hshift);
  934.                         dictp = &db->dict[hval];
  935.  
  936.                         /* validate and then check the entry */
  937.                         if (dictp->codem1 >= max_ent)
  938.                                 goto nomatch;
  939.                         if (dictp->f.fcode == fcode) {
  940.                                 ent = dictp->codem1+1;
  941.                                 continue;   /* found (prefix,suffix) */
  942.                         }
  943.  
  944.                         /* continue until match or invalid entry */
  945.                         disp = (hval == 0) ? 1 : hval;
  946.                         do {
  947.                                 hval += disp;
  948.                                 if (hval >= db->hsize)
  949.                                         hval -= db->hsize;
  950.                                 dictp = &db->dict[hval];
  951.  
  952.  
  953.  
  954. Schryver                     Informational                     [Page 17]
  955.  
  956. RFC 1977                    PPP BSD Compress                 August 1996
  957.  
  958.  
  959.                                 if (dictp->codem1 >= max_ent)
  960.                                         goto nomatch;
  961.                         } while (dictp->f.fcode != fcode);
  962.                         ent = dictp->codem1+1;
  963.                         continue;       /* found (prefix,suffix) */
  964.  
  965. nomatch:                                /* output (count) the prefix */
  966.                         bitno += n_bits;
  967.  
  968.                         /* code -> hashtable */
  969.                         if (max_ent < db->maxmaxcode) {
  970.                             struct bsd_dict *dictp2;
  971.                             /* expand code size if needed */
  972.                             if (max_ent >= MAXCODE(n_bits))
  973.                                     db->n_bits = ++n_bits;
  974.                             /* Invalidate previous hash table entry
  975.                              * assigned this code, and then take it over
  976.                              */
  977.                             dictp2 = &db->dict[max_ent+1];
  978.                             if (db->dict[dictp2->cptr].codem1==max_ent)
  979.                                 db->dict[dictp2->cptr].codem1=BADCODEM1;
  980.                             dictp2->cptr = hval;
  981.                             dictp->codem1 = max_ent;
  982.                             dictp->f.fcode = fcode;
  983.  
  984.                             db->max_ent = ++max_ent;
  985.                             db->lens[max_ent] = db->lens[ent]+1;
  986.                         }
  987.                         ent = c;
  988.                 } while (--slen != 0);
  989.         }
  990.         bitno += n_bits;                /* output (count) last code */
  991.         db->bytes_out += bitno/8;
  992.  
  993.         (void)pf_bsd_check(db);
  994.  
  995.         /* Increase code size if we would have without the packet
  996.          * boundary and as the decompressor will.
  997.          */
  998.         if (max_ent >= MAXCODE(n_bits)
  999.             && max_ent < db->maxmaxcode)
  1000.                 db->n_bits++;
  1001. }
  1002.  
  1003.  
  1004. /* Decompress "BSD Compress"
  1005.  */
  1006. mblk_t*                                 /* 0=failed, so zap CCP */
  1007.  
  1008.  
  1009.  
  1010. Schryver                     Informational                     [Page 18]
  1011.  
  1012. RFC 1977                    PPP BSD Compress                 August 1996
  1013.  
  1014.  
  1015. pf_bsd_decomp(struct bsd_db *db,
  1016.               mblk_t *cmsg)
  1017. {
  1018.         register u_int max_ent = db->max_ent;
  1019.         register __uint32_t accum = 0;
  1020.         register u_int bitno = 32;      /* 1st valid bit in accum */
  1021.         register u_int n_bits = db->n_bits;
  1022.         register u_int tgtbitno = 32-n_bits; /* bitno when accum full */
  1023.         register struct bsd_dict *dictp;
  1024.         register int explen, i;
  1025.         register u_int incode, oldcode, finchar;
  1026.         register u_char *p, *rptr, *rptr9, *wptr0, *wptr;
  1027.         mblk_t *dmsg, *dmsg1, *bp;
  1028.  
  1029.         db->decomp_count++;
  1030.         rptr = cmsg->b_rptr;
  1031.         ASSERT(cmsg->b_wptr >= rptr+PPP_BUF_MIN);
  1032.         ASSERT(PPP_BUF_ALIGN(rptr));
  1033.         rptr += PPP_BUF_MIN;
  1034.  
  1035.         /* get the sequence number */
  1036.         i = 0;
  1037.         explen = 2;
  1038.         do {
  1039.                 while (rptr >= cmsg->b_wptr) {
  1040.                         bp = cmsg;
  1041.                         cmsg = cmsg->b_cont;
  1042.                         freeb(bp);
  1043.                         if (!cmsg) {
  1044.                                 if (db->debug)
  1045.                                         printf("bsd_decomp%d: missing"
  1046.                                                " %d header bytes\n",
  1047.                                                db->unit, explen);
  1048.                                 return 0;
  1049.                         }
  1050.                         rptr = cmsg->b_rptr;
  1051.                 }
  1052.                 i = (i << 8) + *rptr++;
  1053.         } while (--explen != 0);
  1054.         if (i != db->seqno++) {
  1055.                 freemsg(cmsg);
  1056.                 if (db->debug)
  1057.                         printf("bsd_decomp%d: bad sequence number 0x%x"
  1058.                                " instead of 0x%x\n",
  1059.                                db->unit, i, db->seqno-1);
  1060.                 return 0;
  1061.         }
  1062.  
  1063.  
  1064.  
  1065.  
  1066. Schryver                     Informational                     [Page 19]
  1067.  
  1068. RFC 1977                    PPP BSD Compress                 August 1996
  1069.  
  1070.  
  1071.         /* Guess how much memory we will need.  Assume this packet was
  1072.          * compressed by at least 1.5X regardless of the recent ratio.
  1073.          */
  1074.         if (db->ratio > (RATIO_SCALE*3)/2)
  1075.                 explen = (msgdsize(cmsg)*db->ratio)/RATIO_SCALE;
  1076.         else
  1077.                 explen = (msgdsize(cmsg)*3)/2;
  1078.         if (explen > db->mru)
  1079.                 explen = db->mru;
  1080.  
  1081.         dmsg = dmsg1 = allocb(explen+PPP_BUF_HEAD_INFO, BPRI_HI);
  1082.         if (!dmsg1) {
  1083.                 freemsg(cmsg);
  1084.                 return 0;
  1085.         }
  1086.  
  1087.         wptr = dmsg1->b_wptr;
  1088.  
  1089.         ((struct ppp_buf*)wptr)->type = BEEP_FRAME;
  1090.         /* the protocol field must be compressed */
  1091.         ((struct ppp_buf*)wptr)->proto = 0;
  1092.         wptr += PPP_BUF_HEAD_PROTO+1;
  1093.  
  1094.         rptr9 = cmsg->b_wptr;
  1095.         db->bytes_out += rptr9-rptr;
  1096.         wptr0 = wptr;
  1097.         explen = dmsg1->b_datap->db_lim - wptr;
  1098.         oldcode = CLEAR;
  1099.         for (;;) {
  1100.                 if (rptr >= rptr9) {
  1101.                         bp = cmsg;
  1102.                         cmsg = cmsg->b_cont;
  1103.                         freeb(bp);
  1104.                         if (!cmsg)      /* quit at end of message */
  1105.                                 break;
  1106.                         rptr = cmsg->b_rptr;
  1107.                         rptr9 = cmsg->b_wptr;
  1108.                         db->bytes_out += rptr9-rptr;
  1109.                         continue;       /* handle 0-length buffers */
  1110.                 }
  1111.  
  1112.                 /* Accumulate bytes until we have a complete code.
  1113.                  * Then get the next code, relying on the 32-bit,
  1114.                  * unsigned accum to mask the result.
  1115.                  */
  1116.                 bitno -= 8;
  1117.                 accum |= *rptr++ << bitno;
  1118.                 if (tgtbitno < bitno)
  1119.  
  1120.  
  1121.  
  1122. Schryver                     Informational                     [Page 20]
  1123.  
  1124. RFC 1977                    PPP BSD Compress                 August 1996
  1125.  
  1126.  
  1127.                         continue;
  1128.                 incode = accum >> tgtbitno;
  1129.                 accum <<= n_bits;
  1130.                 bitno += n_bits;
  1131.  
  1132.                 if (incode == CLEAR) {
  1133.                         /* The dictionary must only be cleared at
  1134.                          * the end of a packet.  But there could be an
  1135.                          * empty message block at the end.
  1136.                          */
  1137.                         if (rptr != rptr9
  1138.                             || cmsg->b_cont != 0) {
  1139.                                 cmsg->b_rptr = rptr;
  1140.                                 i = msgdsize(cmsg);
  1141.                                 if (i != 0) {
  1142.                                         freemsg(dmsg);
  1143.                                         freemsg(cmsg);
  1144.                                         if (db->debug)
  1145.                                                 printf("bsd_decomp%d: "
  1146.                                                        "bad CLEAR\n",
  1147.                                                        db->unit);
  1148.                                         return 0;
  1149.                                 }
  1150.                         }
  1151.                         pf_bsd_clear(db);
  1152.                         freemsg(cmsg);
  1153.                         wptr0 = wptr;
  1154.                         break;
  1155.                 }
  1156.  
  1157.                 /* Special case for KwKwK string. */
  1158.                 if (incode > max_ent) {
  1159.                         if (incode > max_ent+2
  1160.                             || incode > db->maxmaxcode
  1161.                             || oldcode == CLEAR) {
  1162.                                 freemsg(dmsg);
  1163.                                 freemsg(cmsg);
  1164.                                 if (db->debug)
  1165.                                    printf("bsd_decomp%d: bad code %x\n",
  1166.                                           db->unit, incode);
  1167.                                 return 0;
  1168.                         }
  1169.                         i = db->lens[oldcode];
  1170.                         /* do not write past end of buf */
  1171.                         explen -= i+1;
  1172.                         if (explen < 0) {
  1173.                                 db->undershoot -= explen;
  1174.                                 db->in_count += wptr-wptr0;
  1175.  
  1176.  
  1177.  
  1178. Schryver                     Informational                     [Page 21]
  1179.  
  1180. RFC 1977                    PPP BSD Compress                 August 1996
  1181.  
  1182.  
  1183.                                 dmsg1->b_wptr = wptr;
  1184.                                 CK_WPTR(dmsg1);
  1185.                                 explen = MAX(64,i+1);
  1186.                                 bp = allocb(explen, BPRI_HI);
  1187.                                 if (!bp) {
  1188.                                         freemsg(cmsg);
  1189.                                         freemsg(dmsg);
  1190.                                         return 0;
  1191.                                 }
  1192.                                 dmsg1->b_cont = bp;
  1193.                                 dmsg1 = bp;
  1194.                                 wptr0 = wptr = dmsg1->b_wptr;
  1195.                                explen=dmsg1->b_datap->db_lim-wptr-(i+1);
  1196.                         }
  1197.                         p = (wptr += i);
  1198.                         *wptr++ = finchar;
  1199.                         finchar = oldcode;
  1200.                 } else {
  1201.                         i = db->lens[finchar = incode];
  1202.                         explen -= i;
  1203.                         if (explen < 0) {
  1204.                                 db->undershoot -= explen;
  1205.                                 db->in_count += wptr-wptr0;
  1206.                                 dmsg1->b_wptr = wptr;
  1207.                                 CK_WPTR(dmsg1);
  1208.                                 explen = MAX(64,i);
  1209.                                 bp = allocb(explen, BPRI_HI);
  1210.                                 if (!bp) {
  1211.                                         freemsg(dmsg);
  1212.                                         freemsg(cmsg);
  1213.                                         return 0;
  1214.                                 }
  1215.                                 dmsg1->b_cont = bp;
  1216.                                 dmsg1 = bp;
  1217.                                 wptr0 = wptr = dmsg1->b_wptr;
  1218.                                 explen = dmsg1->b_datap->db_lim-wptr-i;
  1219.                         }
  1220.                         p = (wptr += i);
  1221.                 }
  1222.  
  1223.                 /* decode code and install in decompressed buffer */
  1224.                 while (finchar > LAST) {
  1225.                         dictp = &db->dict[db->dict[finchar].cptr];
  1226.                         *--p = dictp->f.hs.suffix;
  1227.                         finchar = dictp->f.hs.prefix;
  1228.                 }
  1229.                 *--p = finchar;
  1230.  
  1231.  
  1232.  
  1233.  
  1234. Schryver                     Informational                     [Page 22]
  1235.  
  1236. RFC 1977                    PPP BSD Compress                 August 1996
  1237.  
  1238.  
  1239.                 /* If not first code in a packet, and
  1240.                  * if not out of code space, then allocate a new code.
  1241.                  *
  1242.                  * Keep the hash table correct so it can be used
  1243.                  * with uncompressed packets.
  1244.                  */
  1245.                 if (oldcode != CLEAR
  1246.                     && max_ent < db->maxmaxcode) {
  1247.                         struct bsd_dict *dictp2;
  1248.                         __uint32_t fcode;
  1249.                         int hval, disp;
  1250.  
  1251.                         fcode = BSD_KEY(oldcode,finchar);
  1252.                         hval = BSD_HASH(oldcode,finchar,db->hshift);
  1253.                         dictp = &db->dict[hval];
  1254.                         /* look for a free hash table entry */
  1255.                         if (dictp->codem1 < max_ent) {
  1256.                                 disp = (hval == 0) ? 1 : hval;
  1257.                                 do {
  1258.                                         hval += disp;
  1259.                                         if (hval >= db->hsize)
  1260.                                                 hval -= db->hsize;
  1261.                                         dictp = &db->dict[hval];
  1262.                                 } while (dictp->codem1 < max_ent);
  1263.                         }
  1264.  
  1265.                         /* Invalidate previous hash table entry
  1266.                          * assigned this code, and then take it over
  1267.                          */
  1268.                         dictp2 = &db->dict[max_ent+1];
  1269.                         if (db->dict[dictp2->cptr].codem1 == max_ent) {
  1270.                                 db->dict[dictp2->cptr].codem1=BADCODEM1;
  1271.                         }
  1272.                         dictp2->cptr = hval;
  1273.                         dictp->codem1 = max_ent;
  1274.                         dictp->f.fcode = fcode;
  1275.  
  1276.                         db->max_ent = ++max_ent;
  1277.                         db->lens[max_ent] = db->lens[oldcode]+1;
  1278.  
  1279.                         /* Expand code size if needed.
  1280.                          */
  1281.                         if (max_ent >= MAXCODE(n_bits)
  1282.                             && max_ent < db->maxmaxcode) {
  1283.                                 db->n_bits = ++n_bits;
  1284.                                 tgtbitno = 32-n_bits;
  1285.                         }
  1286.                 }
  1287.  
  1288.  
  1289.  
  1290. Schryver                     Informational                     [Page 23]
  1291.  
  1292. RFC 1977                    PPP BSD Compress                 August 1996
  1293.  
  1294.  
  1295.                 oldcode = incode;
  1296.         }
  1297.  
  1298.         db->in_count += wptr-wptr0;
  1299.         dmsg1->b_wptr = wptr;
  1300.         CK_WPTR(dmsg1);
  1301.  
  1302.         db->overshoot += explen;
  1303.  
  1304.         /* Keep the checkpoint right so that incompressible packets
  1305.          * clear the dictionary at the right times.
  1306.          */
  1307.         if (pf_bsd_check(db)
  1308.             && db->debug) {
  1309.                 printf("bsd_decomp%d: peer should have "
  1310.                        "cleared dictionary\n", db->unit);
  1311.         }
  1312.  
  1313.         return dmsg;
  1314. }
  1315.  
  1316. Security Considerations
  1317.  
  1318.    Security issues are not discussed in this memo.
  1319.  
  1320. References
  1321.  
  1322.    [1]   Simpson, W., "The Point-to-Point Protocol (PPP)", STD 51,
  1323.          RFC 1661, July 1994.
  1324.  
  1325.    [2]   Rand, D., "The PPP Compression Control Protocol (CCP)", RFC
  1326.          1962, June 1996.
  1327.  
  1328.    [3]   Simpson, W., "PPP LCP Extensions", RFC 1570, January 1994.
  1329.  
  1330.    [4]   Simpson, W., "PPP in HDLC-like Framing", STD 51, RFC 1662,
  1331.          July 1994.
  1332.  
  1333. Acknowledgments
  1334.  
  1335.    William Simpson provided and supported the very valuable idea of not
  1336.    using any additional header bytes for incompressible packets.
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346. Schryver                     Informational                     [Page 24]
  1347.  
  1348. RFC 1977                    PPP BSD Compress                 August 1996
  1349.  
  1350.  
  1351. Chair's Address
  1352.  
  1353.    The working group can be contacted via the current chair:
  1354.  
  1355.    Karl Fox
  1356.    Ascend Communications
  1357.    3518 Riverside Drive, Suite 101
  1358.    Columbus, Ohio 43221
  1359.  
  1360.    EMail: karl@ascend.com
  1361.  
  1362. Author's Address
  1363.  
  1364.    Questions about this memo can also be directed to:
  1365.  
  1366.    Vernon Schryver
  1367.    2482 Lee Hill Drive
  1368.    Boulder, Colorado 80302
  1369.  
  1370.    EMail: vjs@rhyolite.com
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402. Schryver                     Informational                     [Page 25]
  1403.  
  1404.