xrootd
XrdOucPup.hh
Go to the documentation of this file.
00001 #ifndef __XRDOUCPUP_HH__
00002 #define __XRDOUCPUP_HH__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                          X r d O u c P u p . h h                           */
00006 /*                                                                            */
00007 /* (c) 2007 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*                            All Rights Reserved                             */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00011 /*                                                                            */
00012 /* This file is part of the XRootD software suite.                            */
00013 /*                                                                            */
00014 /* XRootD is free software: you can redistribute it and/or modify it under    */
00015 /* the terms of the GNU Lesser General Public License as published by the     */
00016 /* Free Software Foundation, either version 3 of the License, or (at your     */
00017 /* option) any later version.                                                 */
00018 /*                                                                            */
00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00022 /* License for more details.                                                  */
00023 /*                                                                            */
00024 /* You should have received a copy of the GNU Lesser General Public License   */
00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00027 /*                                                                            */
00028 /* The copyright holder's institutional names and contributor's names may not */
00029 /* be used to endorse or promote products derived from this software without  */
00030 /* specific prior written permission of the institution or contributor.       */
00031 /******************************************************************************/
00032 
00033 #include <stddef.h>
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036   
00037 class  XrdSysError;
00038 struct iovec;
00039 
00040 /******************************************************************************/
00041 /*                            P a r a m e t e r s                             */
00042 /******************************************************************************/
00043   
00044 enum XrdOucPupType
00045 {//  PT_Convert = 0x00, // Convert based on the below (same as char here)
00046      PT_Ignore  = 0x01, // Both: Skip the pup entry
00047      PT_Mark    = 0x02, // Pack: Mark &iov element in *Data
00048      PT_Skip    = 0x03, // Pack: Skip  iov element
00049      PT_MandS   = 0x04, // Pack: Mark  and Skip
00050      PT_Fence   = 0x05, // Unpk: Remaining entries are optional
00051      PT_Datlen  = 0x06, // Unpk: Set prv unpacked data length as an int
00052      PT_Totlen  = 0x07, // Pack: Set total packed data length as an int
00053      PT_End     = 0x0e, // Both: End of list (always the last element)
00054      PT_EndFill = 0x0f, // Both: End of list (always the last element)
00055                         // Pack: *(Base+Doffs) = totlen as net short
00056 
00057      PT_char    = 0x00, // Both: Character
00058      PT_short   = 0x80, // Both: Unsigned
00059      PT_int     = 0xa0, // Both: Unsigned
00060      PT_longlong= 0xc0, // Both: Unsigned
00061      PT_special = 0xe0, // Both: Reserved
00062 
00063      PT_Inline  = 0x10, // Internal use
00064      PT_MaskB   = 0x60, // Internal Use
00065      PT_MaskT   = 0xe0, // Internal Use
00066      PT_Mask    = 0xf0, // Internal Use
00067      PT_MaskD   = 0x0f  // Internal Use
00068 };
00069 
00070 struct  XrdOucPupArgs
00071 {       int                 Doffs;     // Offset(data source or target)
00072         short               Dlen;      // If (Dlen < 0) Dlen = strlen(Data)+1;
00073         unsigned char       Name;      // Name index of this element
00074         unsigned char       Dtype;     // One of XrdOucPupType
00075 };
00076 
00077 struct XrdOucPupNames
00078 {      const char         **NList;     // -> Array of name pointers
00079        int                  NLnum;     // Number of elements in NList
00080 
00081        XrdOucPupNames(const char **nlist=0, int nlnum=0)
00082                      {NList = nlist; NLnum = nlnum;}
00083       ~XrdOucPupNames() {}
00084 };
00085 
00086 #define setPUP0(Type) {0, -1, 0, PT_ ## Type}
00087 
00088 #define setPUP1(Name,Type,Base,Var) \
00089                {offsetof(Base,Var),   -1, Name, PT_ ## Type}
00090 
00091 #define setPUP2(Name,Type,Base,Var,Dlen) \
00092                {offsetof(Base,Var), Dlen, Name, PT_ ## Type}
00093 
00094 /******************************************************************************/
00095 /*                             X r d O u c P u p                              */
00096 /******************************************************************************/
00097   
00098 class XrdOucPup
00099 {
00100 public:
00101 
00102 static const int MaxLen = 0x7ff;
00103 
00104 // Pack #1: Packs a true null terminated character string. The result is placed
00105 //          in iovec which must have at least two elements. Always returns the
00106 //          length of the packed result with iovec updated to point to the
00107 //          next free element.
00108 //
00109 static int   Pack(struct iovec **, const char *, unsigned short &buff);
00110 
00111 // Pack #2: Packs a binary stream of length dlen. The result is placed
00112 //          in iovec which must have at least two elements. Always returns the
00113 //          length of the packed result with iovec updated to point to the
00114 //          next free element.
00115 //
00116 static int   Pack(struct iovec **, const char *, unsigned short &, int dlen);
00117 
00118 // Pack #3: Packs an int into buff and returns the length of the result. The
00119 //          pointer to buff is updated to point to the next free byte. The
00120 //          sizeof(buff) must be at least sizeof(int)+1.
00121 //
00122 static int   Pack(char **buff, unsigned int data);
00123 
00124 // Pack #4: Packs a binary stream of length dlen when dlen >= 0; Otherwise,
00125 //          it packs a string where dlen is strlen(data)+1. The results is
00126 //          placed in buff which must be atleast dlen+2 long. It returns the
00127 //          length of the packed result with buff updated to point to the
00128 //          next free byte.
00129 //
00130 static int   Pack(char **buff, const char *data, int dlen=-1);
00131 
00132 // Pack #5: Packs arbitrary data as directed by XrdOucPupArgs. Data comes from
00133 //          an area pointed to by (Base+PupArgs.Doffs). The reults is placed in
00134 //          iovec (1st arg). The 2nd iovec arg points to the last element+1 and
00135 //          is used to check for an over-run. The Work buffer is used to hold
00136 //          interleaved meta-data and should be sized 9*number of conversions.
00137 //          Returns the actual number of elements used or zero upon an error.
00138 //
00139        int   Pack(struct iovec *, struct iovec *, XrdOucPupArgs *,
00140                   char *Base,     char *Work);
00141 
00142 // Unpack #1: Unpacks a character or binary string in buff bounded by bend.
00143 //            The pointer to the string is placed in data and it length in dlen.
00144 //            Returns true upon success and false upon failure.
00145 //
00146 static int   Unpack(char **buff, const char *bend, char **data, int &dlen);
00147 
00148 // Unpack #2: Unpacks an arbitrary stream in buff bounded by bend as directed
00149 //            by pup. Arg Base is the address of the buffer where data is to be
00150 //            placed as directed by (base+pup->Doffs). All variables in this
00151 //            buffer (e.g., pointers, ints, etc) must be properly aligned.
00152 //            Returns true upon success and false otherwise.
00153 //
00154        int   Unpack(const char *buff, const char *bend, XrdOucPupArgs *pup,
00155                           char *base);
00156 
00157        XrdOucPup(XrdSysError *erp=0, XrdOucPupNames *nms=0)
00158                 {eDest = erp, Names = nms;}
00159       ~XrdOucPup() {}
00160 
00161 private:
00162        int eMsg(const char *etxt, int ino, XrdOucPupArgs *pup);
00163 
00164        XrdSysError    *eDest;
00165        XrdOucPupNames *Names;
00166 };
00167 #endif