Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

netutil.cc

Go to the documentation of this file.
00001 //***************************************************************************
00002 // This source code is copyrighted 2002 by Google Inc.  All rights
00003 // reserved.  You are given a limited license to use this source code for
00004 // purposes of participating in the Google programming contest.  If you
00005 // choose to use or distribute the source code for any other purpose, you
00006 // must either (1) first obtain written approval from Google, or (2)
00007 // prominently display the foregoing copyright notice and the following
00008 // warranty and liability disclaimer on each copy used or distributed.
00009 // 
00010 // The source code and repository (the "Software") is provided "AS IS",
00011 // with no warranty, express or implied, including but not limited to the
00012 // implied warranties of merchantability and fitness for a particular
00013 // use.  In no event shall Google Inc. be liable for any damages, direct
00014 // or indirect, even if advised of the possibility of such damages.
00015 //***************************************************************************
00016 
00017 
00018 #include <string>
00019 #include <limits.h>
00020 #include <stdio.h>
00021 #include <netdb.h>
00022 #include <sys/types.h>
00023 #include <netinet/in.h>
00024 #include <arpa/inet.h>
00025 #include <sys/socket.h>
00026 #include <sys/param.h>
00027 #include "netutil.h"
00028 
00029 int Encoder::varint32_length(uint32 v) {
00030   return Varint::Length32(v);
00031 }
00032 
00033 int Encoder::varint64_length(uint64 v) {
00034   return Varint::Length64(v);
00035 }
00036 
00037 // Special optimized version: does not use Varint
00038 bool Decoder::get_varint32(uint32* v) {
00039   uint32 result;
00040   unsigned char byte;
00041   const unsigned char* ptr = buf_;
00042   const unsigned char* limit = limit_;
00043   if (Encoder::kVarintMax32 == 5 && ptr + Encoder::kVarintMax32 < limit) {
00044     // Byte 1
00045     result = *(ptr++);
00046     if (result < 128) { buf_ = ptr; *v = result; return true; }
00047     result &= 127;
00048 
00049     // Byte 2
00050     byte = *(ptr++);
00051     result |= static_cast<uint32>(byte & 127) << 7;
00052     if ((byte & 128) == 0) { buf_ = ptr; *v = result; return true; }
00053     
00054     // Byte 3
00055     byte = *(ptr++);
00056     result |= static_cast<uint32>(byte & 127) << 14;
00057     if ((byte & 128) == 0) { buf_ = ptr; *v = result; return true; }
00058     
00059     // Byte 4
00060     byte = *(ptr++);
00061     result |= static_cast<uint32>(byte & 127) << 21;
00062     if ((byte & 128) == 0) { buf_ = ptr; *v = result; return true; }
00063     
00064     // Byte 5
00065     byte = *(ptr++);
00066     result |= static_cast<uint32>(byte & 127) << 28;
00067     buf_ = ptr; 
00068     *v = result; 
00069     return ((byte & 128) == 0);  // We're done, whether we succeeded or not
00070     
00071   } else {
00072     // A slow path when we are near the end of the decoding buffer.
00073     int shift = 0;        // How much to shift next set of bits
00074     result = 0;
00075     do {
00076       if ((shift >= 32) || (ptr >= limit_)) {
00077         // Out of range
00078         return false;
00079       }
00080 
00081       // Get 7 bits from next byte
00082       byte = *(ptr++);
00083       result |= static_cast<uint32>(byte & 127) << shift;
00084       shift += 7;
00085     } while ((byte & 128) != 0);
00086     buf_ = ptr;
00087     *v = result;
00088     return true;
00089   }
00090 }
00091 
00092 // Special optimized version: does not use Varint
00093 bool Decoder::get_varint64(uint64* v) {
00094   uint64 result = 0;
00095   int shift = 0;        // How much to shift next set of bits
00096   unsigned char byte;
00097   do {
00098     if ((shift >= 64) || (buf_ >= limit_)) {
00099       // Out of range
00100       return false;
00101     }
00102 
00103     // Get 7 bits from next byte
00104     byte = *(buf_++);
00105     result |= static_cast<uint64>(byte & 127) << shift;
00106     shift += 7;
00107   } while ((byte & 128) != 0);
00108   *v = result;
00109   return true;
00110 }

Generated on Wed May 29 11:37:14 2002 for MarkovPR by doxygen1.2.15