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  {if (strlen(csName) >= sizeof(Name)) return 0;
78  strncpy(Name, csName, sizeof(Name));
79  return 1;
80  }
81 
82 int Set(const void *csVal, int csLen)
83  {if (csLen > ValuSize || csLen < 1) return 0;
84  memcpy(Value, csVal, csLen);
85  Length = csLen;
86  return 1;
87  }
88 
89 int Set(const char *csVal, int csLen)
90  {int n, i = 0, Odd = 0;
91  if (csLen > (int)sizeof(Value)*2 || (csLen & 1)) return 0;
92  Length = csLen/2;
93  while(csLen--)
94  { if (*csVal >= '0' && *csVal <= '9') n = *csVal-48;
95  else if (*csVal >= 'a' && *csVal <= 'f') n = *csVal-87;
96  else if (*csVal >= 'A' && *csVal <= 'F') n = *csVal-55;
97  else return 0;
98  if (Odd) Value[i++] |= n;
99  else Value[i ] = n << 4;
100  csVal++; Odd = ~Odd;
101  }
102  return 1;
103  }
104 
105  XrdCksData() : Rsvd1(0), Rsvd2(0), Length(0)
106  {memset(Name, 0, sizeof(Name));
107  memset(Value,0, sizeof(Value));
108  }
109 };
110 #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
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:82
short Rsvd1
Definition: XrdCksData.hh:45
Definition: XrdCksData.hh:35
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:89
XrdCksData()
Definition: XrdCksData.hh:105