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 00013 // $Id: XrdOucPup.hh 24468 2008-06-22 16:47:03Z ganis $ 00014 00015 #include <stddef.h> 00016 #include <sys/types.h> 00017 #include <sys/stat.h> 00018 00019 class XrdSysError; 00020 struct iovec; 00021 00022 /******************************************************************************/ 00023 /* P a r a m e t e r s */ 00024 /******************************************************************************/ 00025 00026 enum XrdOucPupType 00027 {// PT_Convert = 0x00, // Convert based on the below (same as char here) 00028 PT_Ignore = 0x01, // Both: Skip the pup entry 00029 PT_Mark = 0x02, // Pack: Mark &iov element in *Data 00030 PT_Skip = 0x03, // Pack: Skip iov element 00031 PT_MandS = 0x04, // Pack: Mark and Skip 00032 PT_Fence = 0x05, // Unpk: Remaining entries are optional 00033 PT_Datlen = 0x06, // Unpk: Set prv unpacked data length as an int 00034 PT_Totlen = 0x07, // Pack: Set total packed data length as an int 00035 PT_End = 0x0e, // Both: End of list (always the last element) 00036 PT_EndFill = 0x0f, // Both: End of list (always the last element) 00037 // Pack: *(Base+Doffs) = totlen as net short 00038 00039 PT_char = 0x00, // Both: Character 00040 PT_short = 0x80, // Both: Unsigned 00041 PT_int = 0xa0, // Both: Unsigned 00042 PT_longlong= 0xc0, // Both: Unsigned 00043 PT_special = 0xe0, // Both: Reserved 00044 00045 PT_Inline = 0x10, // Internal use 00046 PT_MaskB = 0x60, // Internal Use 00047 PT_MaskT = 0xe0, // Internal Use 00048 PT_Mask = 0xf0, // Internal Use 00049 PT_MaskD = 0x0f // Internal Use 00050 }; 00051 00052 struct XrdOucPupArgs 00053 { int Doffs; // Offset(data source or target) 00054 short Dlen; // If (Dlen < 0) Dlen = strlen(Data)+1; 00055 unsigned char Name; // Name index of this element 00056 unsigned char Dtype; // One of XrdOucPupType 00057 }; 00058 00059 struct XrdOucPupNames 00060 { const char **NList; // -> Array of name pointers 00061 int NLnum; // Number of elements in NList 00062 00063 XrdOucPupNames(const char **nlist=0, int nlnum=0) 00064 {NList = nlist; NLnum = nlnum;} 00065 ~XrdOucPupNames() {} 00066 }; 00067 00068 #define setPUP0(Type) {0, -1, 0, PT_ ## Type} 00069 00070 #define setPUP1(Name,Type,Base,Var) \ 00071 {offsetof(Base,Var), -1, Name, PT_ ## Type} 00072 00073 #define setPUP2(Name,Type,Base,Var,Dlen) \ 00074 {offsetof(Base,Var), Dlen, Name, PT_ ## Type} 00075 00076 /******************************************************************************/ 00077 /* X r d O u c P u p */ 00078 /******************************************************************************/ 00079 00080 class XrdOucPup 00081 { 00082 public: 00083 00084 static const int MaxLen = 0x7ff; 00085 00086 // Pack #1: Packs a true null terminated character string. The result is placed 00087 // in iovec which must have at least two elements. Always returns the 00088 // length of the packed result with iovec updated to point to the 00089 // next free element. 00090 // 00091 static int Pack(struct iovec **, const char *, unsigned short &buff); 00092 00093 // Pack #2: Packs a binary stream of length dlen. The result is placed 00094 // in iovec which must have at least two elements. Always returns the 00095 // length of the packed result with iovec updated to point to the 00096 // next free element. 00097 // 00098 static int Pack(struct iovec **, const char *, unsigned short &, int dlen); 00099 00100 // Pack #3: Packs an int into buff and returns the length of the result. The 00101 // pointer to buff is updated to point to the next free byte. The 00102 // sizeof(buff) must be at least sizeof(int)+1. 00103 // 00104 static int Pack(char **buff, unsigned int data); 00105 00106 // Pack #4: Packs a binary stream of length dlen when dlen >= 0; Otherwise, 00107 // it packs a string where dlen is strlen(data)+1. The results is 00108 // placed in buff which must be atleast dlen+2 long. It returns the 00109 // length of the packed result with buff updated to point to the 00110 // next free byte. 00111 // 00112 static int Pack(char **buff, const char *data, int dlen=-1); 00113 00114 // Pack #5: Packs arbitrary data as directed by XrdOucPupArgs. Data comes from 00115 // an area pointed to by (Base+PupArgs.Doffs). The reults is placed in 00116 // iovec (1st arg). The 2nd iovec arg points to the last element+1 and 00117 // is used to check for an over-run. The Work buffer is used to hold 00118 // interleaved meta-data and should be sized 9*number of conversions. 00119 // Returns the actual number of elements used or zero upon an error. 00120 // 00121 int Pack(struct iovec *, struct iovec *, XrdOucPupArgs *, 00122 char *Base, char *Work); 00123 00124 // Unpack #1: Unpacks a character or binary string in buff bounded by bend. 00125 // The pointer to the string is placed in data and it length in dlen. 00126 // Returns true upon success and false upon failure. 00127 // 00128 static int Unpack(char **buff, const char *bend, char **data, int &dlen); 00129 00130 // Unpack #2: Unpacks an arbitrary stream in buff bounded by bend as directed 00131 // by pup. Arg Base is the address of the buffer where data is to be 00132 // placed as directed by (base+pup->Doffs). All variables in this 00133 // buffer (e.g., pointers, ints, etc) must be properly aligned. 00134 // Returns true upon success and false otherwise. 00135 // 00136 int Unpack(const char *buff, const char *bend, XrdOucPupArgs *pup, 00137 char *base); 00138 00139 XrdOucPup(XrdSysError *erp=0, XrdOucPupNames *nms=0) 00140 {eDest = erp, Names = nms;} 00141 ~XrdOucPup() {} 00142 00143 private: 00144 int eMsg(const char *etxt, int ino, XrdOucPupArgs *pup); 00145 00146 XrdSysError *eDest; 00147 XrdOucPupNames *Names; 00148 }; 00149 #endif