xrootd
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 <string.h>
34 
36 {
37 public:
38 
39 static const int NameSize = 16; // Max name length is NameSize - 1
40 static const int ValuSize = 64; // Max value length is 512 bits
41 
42 char Name[NameSize]; // Checksum algorithm name
43 long long fmTime; // File's mtime when checksum was computed.
44 int csTime; // Delta from fmTime when checksum was computed.
45 short Rsvd1; // Reserved field
46 char Rsvd2; // Reserved field
47 char Length; // Length, in bytes, of the checksum value
48 char Value[ValuSize]; // The binary checksum value
49 
50 inline
51 int operator==(const XrdCksData &oth)
52  {return (!strncmp(Name, oth.Name, NameSize)
53  && Length == oth.Length
54  && !memcmp(Value, oth.Value, Length));
55  }
56 
57 inline
58 int operator!=(const XrdCksData &oth)
59  {return (strncmp(Name, oth.Name, NameSize)
60  || Length != oth.Length
61  || memcmp(Value, oth.Value, Length));
62  }
63 
64 int Get(char *Buff, int Blen)
65  {const char *hv = "0123456789abcdef";
66  int i, j = 0;
67  if (Blen < Length*2+1) return 0;
68  for (i = 0; i < Length; i++)
69  {Buff[j++] = hv[(Value[i] >> 4) & 0x0f];
70  Buff[j++] = hv[ Value[i] & 0x0f];
71  }
72  Buff[j] = '\0';
73  return Length*2;
74  }
75 
76 int Set(const char *csName)
77  {size_t len = strlen(csName);
78  if (len >= sizeof(Name)) return 0;
79  memcpy(Name, csName, len);
80  Name[len]=0;
81  return 1;
82  }
83 
84 int Set(const void *csVal, int csLen)
85  {if (csLen > ValuSize || csLen < 1) return 0;
86  memcpy(Value, csVal, csLen);
87  Length = csLen;
88  return 1;
89  }
90 
91 int Set(const char *csVal, int csLen)
92  {int n, i = 0, Odd = 0;
93  if (csLen > (int)sizeof(Value)*2 || (csLen & 1)) return 0;
94  Length = csLen/2;
95  while(csLen--)
96  { if (*csVal >= '0' && *csVal <= '9') n = *csVal-48;
97  else if (*csVal >= 'a' && *csVal <= 'f') n = *csVal-87;
98  else if (*csVal >= 'A' && *csVal <= 'F') n = *csVal-55;
99  else return 0;
100  if (Odd) Value[i++] |= n;
101  else Value[i ] = n << 4;
102  csVal++; Odd = ~Odd;
103  }
104  return 1;
105  }
106 
107  void Reset()
108  {memset(Name, 0, sizeof(Name));
109  memset(Value,0, sizeof(Value));
110  fmTime = 0;
111  csTime = 0;
112  Rsvd1 = 0;
113  Rsvd2 = 0;
114  Length = 0;
115  }
116 
118  {Reset();}
119 
120 bool HasValue()
121  {
122  return *Value;
123  }
124 };
125 #endif
int operator!=(const XrdCksData &oth)
Definition: XrdCksData.hh:58
char Rsvd2
Definition: XrdCksData.hh:46
int Get(char *Buff, int Blen)
Definition: XrdCksData.hh:64
bool HasValue()
Definition: XrdCksData.hh:120
int operator==(const XrdCksData &oth)
Definition: XrdCksData.hh:51
int csTime
Definition: XrdCksData.hh:44
char Length
Definition: XrdCksData.hh:47
int Set(const void *csVal, int csLen)
Definition: XrdCksData.hh:84
short Rsvd1
Definition: XrdCksData.hh:45
Definition: XrdCksData.hh:35
void Reset()
Definition: XrdCksData.hh:107
char Name[NameSize]
Definition: XrdCksData.hh:42
int Set(const char *csName)
Definition: XrdCksData.hh:76
char Value[ValuSize]
Definition: XrdCksData.hh:48
long long fmTime
Definition: XrdCksData.hh:43
static const int NameSize
Definition: XrdCksData.hh:39
static const int ValuSize
Definition: XrdCksData.hh:40
int Set(const char *csVal, int csLen)
Definition: XrdCksData.hh:91
XrdCksData()
Definition: XrdCksData.hh:117