prep.cpp

00001 /*
00002   Copyright (c) 2004-2006 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 #ifdef WIN32
00015 #include "../config.h.win"
00016 #else
00017 #include "config.h"
00018 #endif
00019 
00020 #include "prep.h"
00021 
00022 #include <cstdlib>
00023 #include <string>
00024 
00025 #ifdef HAVE_LIBIDN
00026 #include <stringprep.h>
00027 #include <idna.h>
00028 #endif
00029 
00030 #define JID_PORTION_SIZE 1023
00031 
00032 namespace gloox
00033 {
00034 
00035   std::string Prep::nodeprep( const std::string& node )
00036   {
00037     if( node.empty() )
00038       return node;
00039 
00040     if( node.length() > JID_PORTION_SIZE )
00041       return "";
00042 
00043 #ifdef HAVE_LIBIDN
00044   char* p;
00045   char buf[JID_PORTION_SIZE + 1];
00046   memset( &buf, '\0', JID_PORTION_SIZE + 1 );
00047   strncpy( buf, node.c_str(), node.length() );
00048   p = stringprep_locale_to_utf8( buf );
00049     if ( p )
00050     {
00051       strncpy( buf, p, JID_PORTION_SIZE + 1 );
00052       free( p );
00053     }
00054 
00055     int rc = stringprep( (char*)&buf, JID_PORTION_SIZE,
00056                          (Stringprep_profile_flags)0, stringprep_xmpp_nodeprep );
00057     if ( rc != STRINGPREP_OK )
00058     {
00059       return "";
00060     }
00061     return buf;
00062 #else
00063     return node;
00064 #endif
00065   }
00066 
00067   std::string Prep::nameprep( const std::string& domain )
00068   {
00069     if( domain.empty() )
00070       return domain;
00071 
00072     if( domain.length() > JID_PORTION_SIZE )
00073       return "";
00074 
00075 #ifdef HAVE_LIBIDN
00076     char* p;
00077     char buf[JID_PORTION_SIZE + 1];
00078     memset( &buf, '\0', JID_PORTION_SIZE + 1 );
00079     strncpy( buf, domain.c_str(), domain.length() );
00080     p = stringprep_locale_to_utf8( buf );
00081     if ( p )
00082     {
00083       strncpy( buf, p, JID_PORTION_SIZE + 1 );
00084       free( p );
00085     }
00086 
00087     int rc = stringprep( (char*)&buf, JID_PORTION_SIZE,
00088                          (Stringprep_profile_flags)0, stringprep_nameprep );
00089     if ( rc != STRINGPREP_OK )
00090     {
00091       return "";
00092     }
00093     return buf;
00094 #else
00095     return domain;
00096 #endif
00097   }
00098 
00099   std::string Prep::resourceprep( const std::string& resource )
00100   {
00101     if( resource.empty() )
00102       return resource;
00103 
00104     if( resource.length() > JID_PORTION_SIZE )
00105       return "";
00106 
00107 #ifdef HAVE_LIBIDN
00108     char* p;
00109     char buf[JID_PORTION_SIZE + 1];
00110     memset( &buf, '\0', JID_PORTION_SIZE + 1 );
00111     strncpy( buf, resource.c_str(), resource.length() );
00112     p = stringprep_locale_to_utf8( buf );
00113     if ( p )
00114     {
00115       strncpy( buf, p, JID_PORTION_SIZE + 1 );
00116       free( p );
00117     }
00118 
00119     int rc = stringprep( (char*)&buf, JID_PORTION_SIZE,
00120                           (Stringprep_profile_flags)0, stringprep_xmpp_resourceprep );
00121     if ( rc != STRINGPREP_OK )
00122     {
00123       return "";
00124     }
00125     return buf;
00126 #else
00127     return resource;
00128 #endif
00129   }
00130 
00131   std::string Prep::idna( const std::string& domain )
00132   {
00133     if( domain.empty() )
00134       return domain;
00135 
00136     if( domain.length() > JID_PORTION_SIZE )
00137       return "";
00138 
00139 #ifdef HAVE_LIBIDN
00140     char* p;
00141     char buf[JID_PORTION_SIZE + 1];
00142     memset( &buf, '\0', JID_PORTION_SIZE + 1 );
00143     strncpy( buf, domain.c_str(), domain.length() );
00144     p = stringprep_locale_to_utf8( buf );
00145     if ( p )
00146     {
00147       strncpy( buf, p, JID_PORTION_SIZE + 1 );
00148       free( p );
00149     }
00150 
00151     int rc = idna_to_ascii_8z( (char*)&buf, &p, (Idna_flags)0 );
00152     if ( rc != IDNA_SUCCESS )
00153     {
00154       return "";
00155     }
00156     return p;
00157 #else
00158     return domain;
00159 #endif
00160   }
00161 
00162 }

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1