opennurbs_base32.cpp
Go to the documentation of this file.
00001 /* $NoKeywords: $ */
00002 /*
00003 //
00004 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
00005 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
00006 // McNeel & Associates.
00007 //
00008 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
00009 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
00010 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
00011 //                              
00012 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
00013 //
00015 */
00016 
00017 #include "pcl/surface/3rdparty/opennurbs/opennurbs.h"
00018 
00019 bool ON_Base32ToString( const ON_SimpleArray<unsigned char>& base32_digits, ON_String& sBase32 )
00020 {
00021   int digit_count = base32_digits.Count();
00022   sBase32.ReserveArray(digit_count);
00023   sBase32.SetLength(digit_count);
00024   bool rc = ON_Base32ToString( base32_digits, digit_count, sBase32.Array() );
00025   if (!rc)
00026     sBase32.SetLength(0);
00027   return rc;
00028 }
00029 
00030 bool ON_Base32ToString( const ON_SimpleArray<unsigned char>& base32_digits, ON_wString& sBase32 )
00031 {
00032   ON_String s;
00033   bool rc = ON_Base32ToString( base32_digits, s );
00034   if (rc)
00035     sBase32 = s;
00036   return rc;
00037 }
00038 
00039 bool ON_Base32ToString( const unsigned char* base32_digits, int base32_digit_count, char* sBase32 )
00040 {
00041   const char* base32_digit_symbol = "0123456789ABCDEFGHJKMNPQRTUVWXYZ";
00042   const char error_symbol = '#';
00043   unsigned char d;
00044   bool rc = false;
00045 
00046   if ( 0 == sBase32 )
00047     return false;
00048 
00049   if ( 0 == base32_digits || base32_digit_count <= 0 )
00050   {
00051     *sBase32++ = error_symbol;
00052   }
00053   else
00054   {
00055     rc = true;
00056     while(base32_digit_count--)
00057     {
00058       d = *base32_digits++;
00059       if ( d < 32 )
00060       {
00061         *sBase32++ = base32_digit_symbol[d];
00062       }
00063       else
00064       {
00065         rc = false;
00066         *sBase32++ = error_symbol;
00067       }
00068     }
00069   }
00070   *sBase32 = 0; // NULL terminate string
00071 
00072   return rc;
00073 }
00074 
00075 int ON_CorrectBase32StringTypos( const char* sBase32, ON_String& sBase32clean )
00076 {
00077   char* sClean = 0;
00078   if ( sBase32 == sBase32clean.Array() )
00079     sClean = sBase32clean.Array();
00080   else
00081   {
00082     sBase32clean.SetLength(0);
00083     sBase32clean.ReserveArray(strlen(sBase32));
00084     sClean = sBase32clean.Array();
00085   }
00086   int length = ON_CorrectBase32StringTypos( sBase32, sClean );
00087   sBase32clean.SetLength(length);
00088   return length;
00089 }
00090 
00091 int ON_CorrectBase32StringTypos( const wchar_t* sBase32, ON_wString& sBase32clean )
00092 {
00093   if ( 0 == sBase32 || 0 == sBase32[0] )
00094     return 0;
00095   ON_String s = sBase32;
00096   int length = ON_CorrectBase32StringTypos(s.Array(),s.Array());
00097   if ( length > 0 )
00098     sBase32clean = s;
00099   else
00100     sBase32clean.SetLength(0);
00101   return length;
00102 }
00103 
00104 int ON_CorrectBase32StringTypos( const char* sBase32, char* sBase32clean )
00105 {
00106   char c;
00107   int length = 0;
00108   if ( 0 == sBase32clean )
00109     return 0;
00110   if (0 != sBase32 )
00111   {
00112     while ( 0 != (c = *sBase32++) )
00113     {
00114       if ( c >= '0' && c <= '9' )
00115       {
00116         sBase32clean[length++] = c;
00117       }
00118       else
00119       {
00120         if ( c >= 'a' && c < 'z' )
00121           c -= 'a'-'A';
00122 
00123         if ( 'I' == c || 'L' == c )
00124           c = '1';
00125         else if ('O' == c )
00126           c = '0';
00127         else if ( 'S' == c )
00128           c = '5';
00129         else if ( c < 'A' || c > 'Z' )
00130         {
00131           length = 0;
00132           break;
00133         }
00134 
00135         sBase32clean[length++] = c;
00136       }
00137     }
00138   }
00139   sBase32clean[length] = 0;
00140   return length;
00141 }
00142 
00143 int ON_StringToBase32(const ON_wString& sBase32, ON_SimpleArray<unsigned char>& base32_digits )
00144 {
00145   ON_String s(sBase32);
00146   return ON_StringToBase32(s,base32_digits);
00147 }
00148 
00149 int ON_StringToBase32(const ON_String& sBase32, ON_SimpleArray<unsigned char>& base32_digits )
00150 {
00151   const char* s = sBase32;
00152   if ( 0 == s || 0 == s[0] )
00153     return 0;
00154   base32_digits.Reserve(sBase32.Length());
00155   int digit_count = ON_StringToBase32(sBase32,base32_digits.Array());
00156   base32_digits.SetCount(digit_count);
00157   return digit_count;
00158 }
00159 
00160 int ON_StringToBase32(const char* sBase32, unsigned char* base32_digits )
00161 {
00162   char c;
00163   int digit_count = 0;
00164   if ( 0 == base32_digits )
00165     return 0;
00166   if ( 0 != sBase32 )
00167   {
00168     while ( 0 != (c = *sBase32++) )
00169     {
00170       if (c >= '0' && c <= '9' )
00171         base32_digits[digit_count++] = c - '0';
00172       else if ( c >= 'A' && c <= 'H' )
00173         base32_digits[digit_count++] = 10 + c - 'A';
00174       else if ( c >= 'J' && c <= 'K' )
00175         base32_digits[digit_count++] = 9 + c - 'A';
00176       else if ( c >= 'M' && c <= 'N' )
00177         base32_digits[digit_count++] = 8 + c - 'A';
00178       else if ( c >= 'P' && c <= 'R' )
00179         base32_digits[digit_count++] = 7 + c - 'A';
00180       else if ( c >= 'T' && c <= 'Z' )
00181         base32_digits[digit_count++] = 6 + c - 'A';
00182       else
00183       {
00184         digit_count = 0;
00185         break;
00186       }
00187     }
00188   }  
00189   return digit_count;
00190 }
00191 
00192 int ON_GetBase32Digits( const ON_SimpleArray<unsigned char>& x, ON_SimpleArray<unsigned char>& base32_digits )
00193 {
00194   int x_count = x.Count();
00195   int bit_count = 8*x_count;
00196   int base32_digit_count = (bit_count/5) + ((bit_count%5)?1:0);
00197   base32_digits.Reserve(base32_digit_count);
00198   base32_digit_count = ON_GetBase32Digits( x.Array(), x_count, base32_digits.Array() );
00199   base32_digits.SetCount(base32_digit_count);
00200   return base32_digit_count;  
00201 }
00202 
00203 int ON_GetBase32Digits( const unsigned char* x, int x_count, unsigned char* base32_digits )
00204 {
00205   int x_bit_count = 8*x_count;
00206 
00207   unsigned char mask, c;
00208   unsigned char bits[5] = {0,0,0,0,0};
00209   unsigned int bits_count = 0;
00210   unsigned int base32_digit_count = 0;
00211   int i;
00212 
00213   if ( 0 == base32_digits || 0 == x || x_count <= 0 )
00214     return 0;
00215 
00216   if ( x == base32_digits )
00217   {
00218     unsigned char* tmp = (unsigned char*)onmalloc(x_count*sizeof(x[0]));
00219     if ( 0 == tmp )
00220       return 0;
00221     memcpy(tmp,x,x_count*sizeof(x[0]));
00222     i = ON_GetBase32Digits(tmp,x_count,base32_digits);    
00223     onfree(tmp);
00224     return i;
00225   }
00226 
00227   i = x_bit_count % 5;
00228   if ( i )
00229     bits_count = 5-i;
00230 
00231   for ( i = 0; i < x_count; i++)
00232   {
00233     c = x[i];
00234     for (mask = 128; 0 != mask; mask /= 2 )
00235     {
00236       bits[bits_count++] = (0 != (c & mask)) ? 1 : 0;
00237       if ( 5 == bits_count )
00238       {
00239         base32_digits[base32_digit_count++] = 16*bits[0] + 8*bits[1] + 4*bits[2] + 2*bits[3] + bits[4];
00240         bits_count = 0;
00241       }
00242     }
00243   }
00244 
00245   return base32_digit_count;
00246 }
00247 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:26:59