XRootD
Loading...
Searching...
No Matches
XrdSutBucket.cc
Go to the documentation of this file.
1
2/******************************************************************************/
3/* */
4/* X r d S u t B u c k e t . c c */
5/* */
6/* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
7/* Produced by Gerri Ganis for CERN */
8/* */
9/* This file is part of the XRootD software suite. */
10/* */
11/* XRootD is free software: you can redistribute it and/or modify it under */
12/* the terms of the GNU Lesser General Public License as published by the */
13/* Free Software Foundation, either version 3 of the License, or (at your */
14/* option) any later version. */
15/* */
16/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19/* License for more details. */
20/* */
21/* You should have received a copy of the GNU Lesser General Public License */
22/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24/* */
25/* The copyright holder's institutional names and contributor's names may not */
26/* be used to endorse or promote products derived from this software without */
27/* specific prior written permission of the institution or contributor. */
28/******************************************************************************/
29
30#include <cstdio>
31#include <cstring>
32
35#include "XrdSut/XrdSutTrace.hh"
36
37/******************************************************************************/
38/* M a s k s f o r A S C I I c h a r a c t e r s */
39/******************************************************************************/
41 { {0x00000000, -1, -1, -2}, // any printable char
42 {0x00000000, 0x0000ffc0, 0x7fffffe0, 0x7fffffe0}, // letters/numbers (up/low case)
43 {0x00000000, 0x0000ffc0, 0x7e000000, 0x7e000000}, // hex characters (up/low case)
44 {0x00000000, 0x03ffc000, 0x07fffffe, 0x07fffffe} }; // crypt like [a-zA-Z0-9./]
45
46/******************************************************************************/
47/* */
48/* Unit for information exchange */
49/* */
50/******************************************************************************/
51//______________________________________________________________________________
52XrdSutBucket::XrdSutBucket(char *bp, int sz, int ty)
53{
54 // Default constructor
55
56 buffer = membuf = bp;
57 size=sz;
58 type=ty;
59}
60
61//______________________________________________________________________________
63{
64 // Constructor
65
66 membuf = 0;
67 size = 0;
68 type = ty;
69
70 if (s.length()) {
71 membuf = new char [s.length()];
72 if (membuf) {
73 memcpy(membuf,s.c_str(),s.length());
74 buffer = membuf;
75 size = s.length();
76 }
77 }
78}
79
80//______________________________________________________________________________
82{
83 // Copy constructor
84
85 membuf = new char[b.size];
86 if (membuf) {
87 memcpy(membuf,b.buffer,b.size);
88 buffer = membuf;
89 type = b.type;
90 size = b.size;
91 }
92}
93
94//______________________________________________________________________________
95void XrdSutBucket::Update(char *nb, int ns, int ty)
96{
97 // Update content
98
99 if (membuf)
100 delete[] membuf;
101 buffer = membuf = nb;
102 size = ns;
103
104 if (ty)
105 type = ty;
106}
107
108//______________________________________________________________________________
110{
111 // Update content
112 // Returns 0 if ok, -1 otherwise.
113
114 if (membuf)
115 delete[] membuf;
116 membuf = buffer = 0;
117 if (s.length()) {
118 membuf = new char [s.length()];
119 if (membuf) {
120 memcpy(membuf,s.c_str(),s.length());
121 buffer = membuf;
122 size = s.length();
123 if (ty)
124 type = ty;
125 return 0;
126 }
127 }
128 return -1;
129}
130
131//______________________________________________________________________________
132int XrdSutBucket::SetBuf(const char *nb, int ns)
133{
134 // Fill local buffer with ns bytes at nb.
135 // Memory is properly allocated / deallocated
136 // Returns 0 if ok, -1 otherwise.
137
138 if (membuf)
139 delete[] membuf;
140 size = 0;
141 membuf = buffer = 0;
142 if (nb && ns) {
143 membuf = new char [ns];
144 if (membuf) {
145 memcpy(membuf,nb,ns);
146 buffer = membuf;
147 size = ns;
148 return 0;
149 }
150 }
151 return -1;
152}
153
154//______________________________________________________________________________
156{
157 // Convert content into a null terminated string
158 // (nb: the caller must be sure that the operation makes sense)
159
160 s = "";
161 char *b = new char[size+1];
162 if (b) {
163 memcpy(b,buffer,size);
164 b[size] = 0;
165 s = (const char *)b;
166 delete[] b;
167 }
168}
169
170//_____________________________________________________________________________
172{
173 // Dump content of bucket
174 // Options:
175 // 1 print header and tail (default)
176 // 0 dump only content
177 EPNAME("Bucket::Dump");
178
179 if (opt == 1) {
180 PRINT("//-----------------------------------------------------//");
181 PRINT("// //");
182 PRINT("// XrdSutBucket DUMP //");
183 PRINT("// //");
184 }
185
186 PRINT("// addr: " <<this);
187 PRINT("// type: " <<type<<" ("<<XrdSutBuckStr(type)<<")");
188 PRINT("// size: " <<size <<" bytes");
189 PRINT("// content:");
190 std::string bhex;
191 bhex.reserve( XrdSutPRINTLEN );
192 char bpri[XrdSutPRINTLEN] = {0};
193 unsigned int nby = size;
194 unsigned int k = 0, curpri = 0;
195 unsigned char i = 0, j = 0, l = 0;
196 for (k = 0; k < nby; k++) {
197 i = (unsigned char)buffer[k];
198 bool isascii = (i > 127) ? 0 : 1;
199 if (isascii) {
200 j = i / 32;
201 l = i - j * 32;
202 }
203 char chex[8];
204 sprintf(chex," 0x%02x",(int)(i & 0xFF));
205 bhex.append( chex );
206 if (isascii && ((XrdSutCharMsk[0][j] & (1 << (31-l+1))) || i == 0x20)) {
207 bpri[curpri] = i;
208 } else {
209 bpri[curpri] = '.';
210 }
211 curpri++;
212 if (curpri > 7) {
213 bpri[curpri] = 0;
214 PRINT("// " <<bhex<<" "<<bpri);
215 bhex.clear();
216 memset(bpri,0,sizeof(bpri));
217 curpri = 0;
218 }
219 }
220 bpri[curpri] = 0;
221 if (curpri > 0) {
222 while (curpri++ < 8) {
223 bhex.append( " " );
224 }
225 }
226 PRINT("// " <<bhex<<" "<<bpri);
227
228 if (opt == 1) {
229 PRINT("// //");
230 PRINT("//-----------------------------------------------------//");
231 }
232}
233
234//______________________________________________________________________________
236{
237 // Compare bucket b to local bucket: return 1 if matches, 0 if not
238
239 if (b.size == size)
240 if (!memcmp(buffer,b.buffer,size))
241 return 1;
242 return 0;
243}
int kXR_int32
Definition XPtypes.hh:89
#define EPNAME(x)
#define PRINT(y)
const char * XrdSutBuckStr(int kbck)
Definition XrdSutAux.cc:121
#define XrdSutPRINTLEN
Definition XrdSutAux.hh:52
static kXR_int32 XrdSutCharMsk[4][4]
int length() const
const char * c_str() const
kXR_int32 type
kXR_int32 size
XrdSutBucket(char *bp=0, int sz=0, int ty=0)
int SetBuf(const char *nb=0, int ns=0)
void ToString(XrdOucString &s)
void Dump(int opt=1)
int operator==(const XrdSutBucket &b)
void Update(char *nb=0, int ns=0, int ty=0)