XrdCryptoBasic.cc

Go to the documentation of this file.
00001 // $Id: XrdCryptoBasic.cc 30949 2009-11-02 16:37:58Z ganis $
00002 
00003 const char *XrdCryptoBasicCVSID = "$Id: XrdCryptoBasic.cc 30949 2009-11-02 16:37:58Z ganis $";
00004 /******************************************************************************/
00005 /*                                                                            */
00006 /*                     X r d C r y p t o B a s i c. h h                       */
00007 /*                                                                            */
00008 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00009 /*       All Rights Reserved. See XrdInfo.cc for complete License Terms       */
00010 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00011 /*              DE-AC03-76-SFO0515 with the Department of Energy              */
00012 /******************************************************************************/
00013 
00014 /* ************************************************************************** */
00015 /*                                                                            */
00016 /* Generic buffer for crypto functions needed in XrdCrypto                    */
00017 /* Different crypto implementation (OpenSSL, Botan, ...) available as plug-in */
00018 /*                                                                            */
00019 /* ************************************************************************** */
00020 
00021 #include <stdio.h>
00022 #include <string.h>
00023 
00024 #include <XrdSut/XrdSutAux.hh>
00025 #include <XrdCrypto/XrdCryptoAux.hh>
00026 #include <XrdCrypto/XrdCryptoBasic.hh>
00027 
00028 // ---------------------------------------------------------------------------//
00029 //
00030 // Basic crypto buffer implementation
00031 //
00032 // ---------------------------------------------------------------------------//
00033 
00034 //_____________________________________________________________________________
00035 XrdCryptoBasic::XrdCryptoBasic(const char *t, int l, const char *b)
00036 {
00037    // Basic constructor.
00038    // This class has responsibility over both its buffers.
00039 
00040    type = 0;
00041    membuf = 0;
00042    lenbuf = 0;
00043    //
00044    // Fill in the type, if any
00045    if (t) {
00046       int tl = strlen(t);
00047       if (tl) {
00048          type = new char[tl+1];
00049          if (type) {
00050             memcpy(type,t,tl);
00051             type[tl] = 0;
00052          }
00053       }
00054    }
00055    //
00056    // Fill the buffer and length
00057    if (l > 0) {
00058       membuf = new char[l];
00059       if (membuf) {
00060          lenbuf = l;
00061          if (b)
00062             memcpy(membuf,b,l);
00063          else
00064             memset(membuf,0,l);
00065       }
00066    }
00067 }
00068 
00069 //_____________________________________________________________________________
00070 XrdSutBucket *XrdCryptoBasic::AsBucket()
00071 {
00072    // Return pointer to a bucket created using the internal buffer
00073    // Type is not copied.
00074    // The bucket is responsible for the allocated memory
00075 
00076    XrdSutBucket *buck = (XrdSutBucket *)0;
00077 
00078    if (Length()) {
00079       char *nbuf = new char[Length()];
00080       if (nbuf) {
00081          memcpy(nbuf,Buffer(),Length());
00082          buck = new XrdSutBucket(nbuf,Length());
00083       }
00084    }
00085 
00086    return buck;
00087 }
00088 
00089 //_____________________________________________________________________________
00090 char *XrdCryptoBasic::AsHexString()
00091 {
00092    // Return the internal buffer as a hexadecimal string
00093    static char out[XrdSutMAXBUF];
00094 
00095    int lmax = XrdSutMAXBUF / 2 - 1 ;
00096    int lconv = (Length() > lmax) ? lmax : Length();
00097 
00098    if (!XrdSutToHex(Buffer(),lconv,&out[0]))
00099       return &out[0];
00100    return 0;
00101 }
00102 
00103 //_____________________________________________________________________________
00104 int XrdCryptoBasic::FromHex(const char *hex)
00105 {
00106    // Set a binary buffer from a null-terminated hexadecimal string
00107    // Returns 0 in case of success, -1 otherwise.
00108 
00109    if (!hex)
00110       return -1;
00111 
00112    // Determine length
00113    int lhex = strlen(hex);
00114    int lout = lhex / 2;
00115    if (lout * 2 < lhex) lout++;
00116 
00117    // Allocate buffer
00118    char *bout = new char[lout];
00119    if (bout) {
00120       if (XrdSutFromHex(hex, bout, lout) != 0) {
00121          delete[] bout;
00122          return -1;
00123       }
00124       UseBuffer(lout,bout);
00125       return 0;
00126    }
00127 
00128    // Failure
00129    return -1;
00130 }
00131 
00132 //_____________________________________________________________________________
00133 int XrdCryptoBasic::SetLength(int l)
00134 {
00135    // Truncate or enlarge the data buffer length to l; new bytes are filled
00136    // with 0 in case of enlargement
00137    // Returns 0 in case of success, -1 in case of error (in buffer allocation).
00138 
00139    if (l > 0) {
00140       //
00141       // Create new buffer
00142       char *newbuf = new char[l];
00143       if (newbuf) {
00144          //
00145          // Save existing info
00146          memcpy(newbuf,membuf,l);
00147          //
00148          // Reset additional bytes, if any
00149          if (l > lenbuf)
00150             memset(newbuf+lenbuf,0,(l-lenbuf));
00151          //
00152          // Release old buffer
00153          delete[] membuf;
00154          //
00155          // Set the new length and buffer
00156          lenbuf = l;
00157          membuf = newbuf;
00158       } else
00159          return -1;
00160    } else {
00161       //
00162       // Release existing buffer, if any
00163       if (membuf)
00164          delete[] membuf;
00165       lenbuf = 0;
00166       membuf = 0;
00167    }
00168 
00169    return 0;
00170 }
00171 
00172 //_____________________________________________________________________________
00173 int XrdCryptoBasic::SetBuffer(int l, const char *b)
00174 {
00175    // Substitute buffer with the l bytes at b.
00176    // Returns 0 in case of success, -1 in case of error (in buffer allocation).
00177 
00178    if (l > 0) {
00179       //
00180       // Allocate new buffer
00181       char *tmpbuf = new char[l];
00182       if (tmpbuf) {
00183          if (b)
00184             memcpy(tmpbuf,b,l);
00185          else
00186             memset(tmpbuf,0,l);
00187          if (membuf)
00188             delete[] membuf;
00189          lenbuf = l;
00190          membuf = tmpbuf;
00191       } else
00192          return -1;
00193    } else {
00194       //
00195       // Release existing buffer, if any
00196       if (membuf)
00197          delete[] membuf;
00198       lenbuf = 0;
00199       membuf = 0;
00200    }
00201 
00202    return 0;
00203 }
00204 
00205 //_____________________________________________________________________________
00206 int XrdCryptoBasic::SetType(const char *t)
00207 {
00208    // Substitute type with the string at t.
00209    // Returns 0 in case of success, -1 in case of error (in buffer allocation).
00210 
00211    if (t) {
00212       //
00213       // Allocate new buffer
00214       int tl = strlen(t);
00215       char *tmpbuf = new char[tl+1];
00216       if (tmpbuf) {
00217          strcpy(tmpbuf,t);
00218          delete[] type;
00219          type = tmpbuf;
00220       } else
00221          return -1;
00222    } else {
00223       //
00224       // Release existing buffer, if any
00225       if (type)
00226          delete[] type;
00227       type = 0;
00228    }
00229 
00230    return 0;
00231 }

Generated on Tue Jul 5 14:46:32 2011 for ROOT_528-00b_version by  doxygen 1.5.1