xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdCksData.hh
Go to the documentation of this file.
1 #ifndef __XRDCKSDATA_HH__
2 #define __XRDCKSDATA_HH__
3 /******************************************************************************/
4 /* */
5 /* X r d C k s D a t a . h h */
6 /* */
7 /* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC02-76-SFO0515 with the Department of Energy */
11 /* */
12 /* This file is part of the XRootD software suite. */
13 /* */
14 /* XRootD is free software: you can redistribute it and/or modify it under */
15 /* the terms of the GNU Lesser General Public License as published by the */
16 /* Free Software Foundation, either version 3 of the License, or (at your */
17 /* option) any later version. */
18 /* */
19 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22 /* License for more details. */
23 /* */
24 /* You should have received a copy of the GNU Lesser General Public License */
25 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27 /* */
28 /* The copyright holder's institutional names and contributor's names may not */
29 /* be used to endorse or promote products derived from this software without */
30 /* specific prior written permission of the institution or contributor. */
31 /******************************************************************************/
32 
33 #include <cstring>
34 
35 class XrdOucEnv;
36 
38 {
39 public:
40 
41 static const int NameSize = 16; // Max name length is NameSize - 1
42 static const int ValuSize = 64; // Max value length is 512 bits
43 
44 char Name[NameSize]; // Checksum algorithm name
45 union {
46 long long fmTime; // Out: File's mtime when checksum was computed.
47 XrdOucEnv*envP; // In: Set for get & calc, only!
48  };
49 int csTime; // Delta from fmTime when checksum was computed.
50 short Rsvd1; // Reserved field
51 char Rsvd2; // Reserved field
52 char Length; // Length, in bytes, of the checksum value
53 char Value[ValuSize]; // The binary checksum value
54 
55 inline
56 int operator==(const XrdCksData &oth)
57  {return (!strncmp(Name, oth.Name, NameSize)
58  && Length == oth.Length
59  && !memcmp(Value, oth.Value, Length));
60  }
61 
62 inline
63 int operator!=(const XrdCksData &oth)
64  {return (strncmp(Name, oth.Name, NameSize)
65  || Length != oth.Length
66  || memcmp(Value, oth.Value, Length));
67  }
68 
69 int Get(char *Buff, int Blen)
70  {const char *hv = "0123456789abcdef";
71  int i, j = 0;
72  if (Blen < Length*2+1) return 0;
73  for (i = 0; i < Length; i++)
74  {Buff[j++] = hv[(Value[i] >> 4) & 0x0f];
75  Buff[j++] = hv[ Value[i] & 0x0f];
76  }
77  Buff[j] = '\0';
78  return Length*2;
79  }
80 
81 int Set(const char *csName)
82  {size_t len = strlen(csName);
83  if (len >= sizeof(Name)) return 0;
84  memcpy(Name, csName, len);
85  Name[len]=0;
86  return 1;
87  }
88 
89 int Set(const void *csVal, int csLen)
90  {if (csLen > ValuSize || csLen < 1) return 0;
91  memcpy(Value, csVal, csLen);
92  Length = csLen;
93  return 1;
94  }
95 
96 int Set(const char *csVal, int csLen)
97  {int n, i = 0, Odd = 0;
98  if (csLen > (int)sizeof(Value)*2 || (csLen & 1)) return 0;
99  Length = csLen/2;
100  while(csLen--)
101  { if (*csVal >= '0' && *csVal <= '9') n = *csVal-48;
102  else if (*csVal >= 'a' && *csVal <= 'f') n = *csVal-87;
103  else if (*csVal >= 'A' && *csVal <= 'F') n = *csVal-55;
104  else return 0;
105  if (Odd) Value[i++] |= n;
106  else Value[i ] = n << 4;
107  csVal++; Odd = ~Odd;
108  }
109  return 1;
110  }
111 
112  void Reset()
113  {memset(Name, 0, sizeof(Name));
114  memset(Value,0, sizeof(Value));
115  fmTime = 0;
116  csTime = 0;
117  Rsvd1 = 0;
118  Rsvd2 = 0;
119  Length = 0;
120  }
121 
123  {Reset();}
124 
125 bool HasValue()
126  {
127  return *Value;
128  }
129 };
130 #endif
int operator!=(const XrdCksData &oth)
Definition: XrdCksData.hh:63
char Rsvd2
Definition: XrdCksData.hh:51
int Get(char *Buff, int Blen)
Definition: XrdCksData.hh:69
bool HasValue()
Definition: XrdCksData.hh:125
int operator==(const XrdCksData &oth)
Definition: XrdCksData.hh:56
int csTime
Definition: XrdCksData.hh:49
char Length
Definition: XrdCksData.hh:52
int Set(const void *csVal, int csLen)
Definition: XrdCksData.hh:89
short Rsvd1
Definition: XrdCksData.hh:50
Definition: XrdCksData.hh:37
void Reset()
Definition: XrdCksData.hh:112
char Name[NameSize]
Definition: XrdCksData.hh:44
int Set(const char *csName)
Definition: XrdCksData.hh:81
Definition: XrdOucEnv.hh:41
XrdOucEnv * envP
Definition: XrdCksData.hh:47
char Value[ValuSize]
Definition: XrdCksData.hh:53
long long fmTime
Definition: XrdCksData.hh:46
static const int NameSize
Definition: XrdCksData.hh:41
static const int ValuSize
Definition: XrdCksData.hh:42
int Set(const char *csVal, int csLen)
Definition: XrdCksData.hh:96
XrdCksData()
Definition: XrdCksData.hh:122