Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

FAST BASE64 / UUENCODING ENCODAGE/DECODAGE


Information sur la source

Catégorie :Réseaux & Internet Classé sous : base64, uuencode, network, mime, codage Niveau : Initié Date de création : 14/03/2008 Date de mise à jour : 05/09/2008 14:08:18 Vu / téléchargé: 4 757 / 440

Note :
7 / 10 - par 1 personne
7,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

Description

Classes C++ permettant de coder/décoder rapidement et simplement une string en/depuis Base64/Uuencoding.
 

Source

  • /////////////////////BASE64.HPP/////////////////////
  • #ifndef __ENCODING_BASE64_HPP__
  • #define __ENCODING_BASE64_HPP__
  • #include <iostream>
  • /// Base64 alphabet
  • static const std::string b64table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  • class Base64
  • {
  • private :
  • /// Filling character
  • static const char fillchar = '=';
  • // The masks
  • static const uint32_t mask1 = 0xFC000000;
  • static const uint32_t mask2 = 0x03F00000;
  • static const uint32_t mask3 = 0x000FC000;
  • static const uint32_t mask4 = 0x00003F00;
  • typedef union
  • {
  • uint32_t l ;
  • char c[4];
  • }un32;
  • public:
  • static std::string encode(const std::string & data);
  • static std::string decode(const std::string & data);
  • };
  • #endif
  • ////////////////////////////////////////////////////
  • /////////////////////BASE64.CPP/////////////////////
  • #include <base64.hpp>
  • using namespace std;
  • string Base64::encode(const std::string & data)
  • {
  • const size_t trail = data.size()%3;
  • size_t sz = data.size()/3*4;
  • sz += (trail != 0) ? 4 : 0;
  • un32 b64;
  • string out;
  • out.resize(sz);
  • size_t i = 0, k = 0;
  • while(i < data.size()-trail)
  • {
  • b64.c[3] = data[i++];
  • b64.c[2] = data[i++];
  • b64.c[1] = data[i++];
  • out[k++] = b64table[static_cast <int> ((b64.l & mask1) >> 26)];
  • out[k++] = b64table[static_cast <int> ((b64.l & mask2) >> 20)];
  • out[k++] = b64table[static_cast <int> ((b64.l & mask3) >> 14)];
  • out[k++] = b64table[static_cast <int> ((b64.l & mask4) >> 8)];
  • }
  • b64.l = 0;
  • switch(trail)
  • {
  • case 1:
  • b64.c[3] = data[i++];
  • out[k++] = b64table[static_cast <int> ((b64.l & mask1) >> 26)];
  • out[k++] = b64table[static_cast <int> ((b64.l & mask2) >> 20)];
  • out[k++] = fillchar;
  • out[k++] = fillchar;
  • break;
  • case 2:
  • b64.c[3] = data[i++];
  • b64.c[2] = data[i++];
  • out[k++] = b64table[static_cast <int> ((b64.l & mask1) >> 26)];
  • out[k++] = b64table[static_cast <int> ((b64.l & mask2) >> 20)];
  • out[k++] = b64table[static_cast <int> ((b64.l & mask3) >> 14)];
  • out[k++] = fillchar;
  • break;
  • }
  • return out;
  • }
  • string Base64::decode(const std::string & data)
  • {
  • // Number of trailing '='
  • const size_t trail = (data[data.size()-1] == fillchar) ? ((data[data.size()-2] == fillchar) ? 2 : 1 ) : 0;
  • // Number of char to decode
  • const size_t szin = (trail == 0) ? data.size() : data.size()-4;
  • // Output string size
  • const size_t szout = szin/4*3+ ((trail == 0)? 0 : ((trail == 1) ? 3 : 2));
  • un32 b64;
  • string out;
  • out.resize(szout);
  • size_t i = 0, k = 0;
  • while(i < szin)
  • {
  • b64.l = 0;
  • b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) << 26;
  • b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) << 20;
  • b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) << 14;
  • b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) << 8;
  • out[k++] = b64.c[3];
  • out[k++] = b64.c[2];
  • out[k++] = b64.c[1];
  • }
  • b64.l = 0;
  • switch(trail)
  • {
  • case 1:
  • b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) << 26;
  • b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) << 20;
  • b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) << 14;
  • out[k++] = b64.c[3];
  • out[k++] = b64.c[2];
  • break;
  • case 2:
  • b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) << 26;
  • b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) << 20;
  • out[k++] = b64.c[3];
  • break;
  • }
  • return out;
  • }
  • ////////////////////////////////////////////////////
/////////////////////BASE64.HPP/////////////////////
#ifndef __ENCODING_BASE64_HPP__
#define __ENCODING_BASE64_HPP__

#include <iostream>

/// Base64 alphabet
static const std::string b64table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

class Base64
{
private :
  /// Filling character
  static const char fillchar = '=';
  
  // The masks
  static const uint32_t mask1 = 0xFC000000;
  static const uint32_t mask2 = 0x03F00000;
  static const uint32_t mask3 = 0x000FC000;
  static const uint32_t mask4 = 0x00003F00;

  typedef union
  {
    uint32_t l   ;
    char     c[4];
  }un32;
  
public:
  static std::string encode(const std::string & data);
  static std::string decode(const std::string & data);
}; 
  
#endif
////////////////////////////////////////////////////

/////////////////////BASE64.CPP/////////////////////

#include <base64.hpp>

using namespace std;

string Base64::encode(const std::string & data)
{
  const size_t trail = data.size()%3;
  size_t sz          = data.size()/3*4;
  sz += (trail != 0) ? 4 : 0;
  
  un32 b64; 

  string out;
  out.resize(sz); 

  size_t i = 0, k = 0;
  
  while(i < data.size()-trail)
    {
      b64.c[3] = data[i++];
      b64.c[2] = data[i++];
      b64.c[1] = data[i++];
       
      out[k++] = b64table[static_cast <int> ((b64.l & mask1) >> 26)];
      out[k++] = b64table[static_cast <int> ((b64.l & mask2) >> 20)];
      out[k++] = b64table[static_cast <int> ((b64.l & mask3) >> 14)];
      out[k++] = b64table[static_cast <int> ((b64.l & mask4) >>  8)];
    }

  b64.l = 0;
  
  switch(trail)
    {
    case 1:
      b64.c[3] = data[i++];
      out[k++] = b64table[static_cast <int> ((b64.l & mask1) >> 26)];
      out[k++] = b64table[static_cast <int> ((b64.l & mask2) >> 20)];
      out[k++] = fillchar;
      out[k++] = fillchar; 
      break;

    case 2:
      b64.c[3] = data[i++];
      b64.c[2] = data[i++];
      out[k++] = b64table[static_cast <int> ((b64.l & mask1) >> 26)];
      out[k++] = b64table[static_cast <int> ((b64.l & mask2) >> 20)];
      out[k++] = b64table[static_cast <int> ((b64.l & mask3) >> 14)];
      out[k++] = fillchar;
      break;
    }
  
  return out;
}


string Base64::decode(const std::string & data)
{
  // Number of trailing '='
  const size_t trail = (data[data.size()-1] == fillchar) ? ((data[data.size()-2] == fillchar) ? 2 : 1 ) : 0;
  // Number of char to decode
  const size_t szin  = (trail == 0) ? data.size() : data.size()-4;
  // Output string size
  const size_t szout = szin/4*3+ ((trail == 0)? 0 : ((trail == 1) ? 3 : 2)); 

  un32 b64; 

  string out;
  out.resize(szout); 

  size_t i = 0, k = 0;

  while(i < szin)
    {
      b64.l  = 0; 
      b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) <<  26;
      b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) <<  20;
      b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) <<  14;
      b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) <<   8;

      out[k++] = b64.c[3];
      out[k++] = b64.c[2];
      out[k++] = b64.c[1];
    }

  b64.l = 0;
  
  switch(trail)
    {
    case 1:
      b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) <<  26;
      b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) <<  20;
      b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) <<  14;
      
      out[k++] =  b64.c[3];
      out[k++] =  b64.c[2];
      break;

    case 2:
      b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) <<  26;
      b64.l += (static_cast < uint32_t > (b64table.find_first_of(data[i++])) ) <<  20;
      
      out[k++] = b64.c[3];
      break;
    }
  
  return out;
}



////////////////////////////////////////////////////

Conclusion

Ici pour le Base64, le Uuencoding est dans le zip mais le principe reste le même.
 

Fichier Zip

Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

Historique

15 avril 2008 10:09:26 :
Ajout conclusion
05 septembre 2008 14:08:18 :
Orthographe

Commentaires et avis

signaler à un administrateur
Commentaire de jphippie2 le 07/10/2008 14:36:03 7/10

Merci pour ce code que je trouve joliment écrit.

Il fonctionne bien à deux petits bugs près:
base64.cpp Ligne 100 : dans le decodeur base 64, remplacer la ligne:
const size_t szout = szin/4*3+ ((trail == 0)? 0 : ((trail == 1) ? 3 : 2));
par la ligne:
const size_t szout = szin/4*3+ ((trail == 0)? 0 : ((trail == 1) ? 2 : 1));
Sinon, un caractère supplémentaire non initialisé est ajouté au buffer.

uuencoding.cpp Ligne 6 : remplacer
#define SPtoBACKQ(x) (x == 32) ? 96 : x
par:
#define SPtoBACKQ(x) (x)
En effet, le fait de remplacer systématiquement les blancs par des accents graves n'est pas une bonne idée car le même caractère est utilisé pour le padding en fin de ligne.
Par exemple la chaîne "LA CONSTITUTION Française " est codée avec deux accents graves à la fin plutôt que espace+accent grave. Lors du décodage on obtient une chaîne trop courte d'un caractère!

Ajouter un commentaire

Discussions en rapport avec ce code source dans le forum

Codage Base64 en C [ par jcf1981 ] Bonjour je cherche une fonction C pour d&#233;coder une chaine de caract&#232;re en base64 . Pour la coder en PHP , pas de soucis mais pour la d&#23 bibliotheque d'encodage et de decodage MIME [ par ebooserge ] salut a tous,voila je suis en train de realiser un petit serveur http et j'ai vu sur le net que pour envoyer des fichier binaires au navigateur il fal Codage de Huffman [ par Trinity_vv ] Salut, Je souhaiterais trouver un programme en C le plus simple possible me permettant de compresser et de decompresser des fichiers en utilisant la Winsock Mail et MIME ! [ par wxccxw ] salut ! j'avai une question : les envoi de Mail avec MIME jutilise winsock2 et j'envoi des commande sur un smtp : exemple : helo mail from: etc.... Problème avec une DLL C++ appelant une DLL C# depuis un network drive [ par SinaC ] Bonjour,&nbsp;&nbsp;&nbsp; Le but du projet &#233;tait d'utiliser une DLL C# depuis Powerbuilder, la solution &#224; laquelle nous avons pens&#233; &# Codage image .img [ par gouzi_666 ] Bonjour, je travaille sur un projet de traitement d'image. Les images ou plut&#244;t les fichiers images sur lesquels je travaille sont des fichiers d connaitre le codage des caractéres d'un fichier texte [ par faico ] Salam Est ce qu'il ya une methode pour connaitre le codage des caract&#233;res d'un fichier texte ?et merci d'avance ! &nbsp;<FONT face=Tahoma color= Base64 de Unrealircd [ par logant83 ] Bonsoir, voila enfaite je suis entrain de faire un services IRC en socket, mais voila le probléme c'est que je n'arrive pas à obtenir le hostname (L'a Codage [ par ProgVal ] Bonjour,   J'ai construit un programme dans le but d'écrire, d'enregistrer, de charger... Tout fonctionne. Il y a:-un Memo(entrer et modifier les donn Base64 [ par jean84 ] Hello Je cherche a implémenter ma propre fonction de codage/décodage en base64. J'ai trouvé pas mal de codes ainsi que des articles traitant du sujet


Nos sponsors

Sondage...

CalendriCode

Juillet 2009
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Téléchargements

Comparez les prix Nouvelle version

Photothèque Nouveau !



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,702 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.