"Fossies" - the Fresh Open Source Software Archive

Member "rgdbm-2.1.42/sps.c" (20 Jun 2007, 2493 Bytes) of package /linux/privat/old/rgdbm-2.1.42.tgz:


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 "sps.c" see the Fossies "Dox" file reference documentation.

    1 /* 
    2  * RGDBM skip-past-stop i/o function (C) Peter T. Breuer 2007.
    3  *
    4  * This code is releasd under the GNU General Public Licence (GPL)
    5  * version 2 or such later version as the FSF may release in the
    6  * fullness of time (when this licence automatically becomes THAT
    7  * licence). See GPL_V2 file in the code archive for details.
    8  */
    9 
   10 
   11 #include "config.h"
   12 #if STDC_HEADERS
   13 # include <stdlib.h>
   14 # include <stdarg.h>
   15 # include <string.h>
   16 #else
   17 # ifndef HAVE_STRCHR
   18 #  define strchr index
   19 #  define strrchr rindex
   20 # endif
   21 char *strchr (), *strrchr ();
   22 # ifndef HAVE_MEMCPY
   23 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
   24 #  define memmove(d, s, n) bcopy ((s), (d), (n))
   25 # endif
   26 #endif          /* STDC_HEADERS */
   27 #include <inttypes.h>          /* for uint64_t */
   28 #include <arpa/inet.h>         /* for ntohl */
   29 
   30 #ifdef HAVE_GDBM_H         /* won't get far without gdbm.h */
   31 # include <gdbm.h>
   32 #elif defined(HAVE_NDBM_H) /* worth checking for ndbm too */
   33 # include <ndbm.h>
   34 #elif defined(HAVE_DBM_H)  /* even worth checking for dbm */
   35 # include <dbm.h>
   36 #endif
   37 
   38 #include "io.h"                /* for CIGAM */
   39 #include "xfr.h"
   40 #include "sup.h"
   41 #include "sps.h"
   42 
   43 /*
   44  * resync input with next incoming request, with luck
   45  */
   46 int
   47 skip_past_stop(struct io_ctx *ctx) {
   48 
   49     int err;
   50     int ngot  = 0;                 // bytes of stop we have seen
   51     int nleft = sizeof(uint64_t);  // bytes of stop we need to have seen
   52     const uint64_t stop = htonll(RGDBM_CIGAM);
   53 
   54     char c[sizeof(uint64_t)];      // accumulate plausible stop bytes here
   55 
   56     while (nleft > 0) {
   57 
   58         err = rgdbm_xfr(ctx, &c[ngot], 1, RGDBM_READ);
   59         if (err < 0)
   60             return err;            // can't read from socket
   61 
   62         if (c[ngot] == ((char *)&stop)[ngot]) {
   63             // move on
   64             ngot++;                // next byte matched planned stop seq
   65             nleft--;
   66 
   67         } else if (ngot < 1) {
   68             // no matches so far. Try again.
   69             continue;
   70         } else {                   // hit mismatch. will drop 1st byte in buffer
   71 
   72             int i;                 // how many buffer bytes match init stop seq
   73 
   74             for (i = 1; i < ngot; i++) {
   75                 if (memcmp(c+i, &stop, ngot - i) == 0)
   76                     break;
   77             }
   78 
   79             ngot = ngot - i;      // have this many stop bytes at offset i
   80             nleft = sizeof(uint64_t) - ngot;
   81             memmove(c, c+i, ngot);// shift possible stop bytes to front of buf
   82         }
   83     }
   84     return 0;
   85 }
   86