Advertisement
Shiny_

base64

Sep 1st, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.28 KB | None | 0 0
  1. // Conditions:
  2.     #if defined _base64_included
  3.         #endinput
  4.     #endif
  5.     #define _base64_included
  6.  
  7. // Variables:
  8.     new const LettersTable[] = {
  9.         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  10.     };
  11.    
  12.     new const Decoder[] = {
  13.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  14.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  15.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  16.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  17.         0,  0,  0,  62, 0,  0,  0,  63, 52, 53,
  18.         54, 55, 56, 57, 58, 59, 60, 61, 0,  0,
  19.         0,  0,  0,  0,  0,  0,  1,  2,  3,  4,
  20.         5,  6,  7,  8,  9,  10, 11, 12, 13, 14,
  21.         15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  22.         25, 0,  0,  0,  0,  0,  0,  26, 27, 28,
  23.         29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  24.         39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  25.         49, 50, 51, 0,  0,  0,  0,  0,  0,  0,
  26.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  27.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  28.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  29.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  30.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  31.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  32.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  33.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  34.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  35.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  36.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  37.         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  38.         0,  0,  0,  0,  0,  0
  39.     };
  40.  
  41. // Functions:
  42.     /** Funkcja:
  43.      * decode 64
  44.      - string[] Dekoduje ciąg znaków.
  45.      - second_string[] Zapisuje ciąg znaków w string[]
  46.     **/
  47.     stock decode64(const string[], second_string[]) {
  48.         /** Zmienne:
  49.          *  SpaceReplacer - zamienia spacje na znaki równości;
  50.          *  len - oznacza długość zakodowanego ciągu znaków string[];
  51.          *  Position - oznacza pozycję w ciągu znaków;
  52.         **/
  53.        
  54.         new const SpaceReplacer = '=';
  55.         new len = strlen(string);
  56.         new Position;
  57.        
  58.         /** Zmienne:
  59.          *  i - tworzy zwykłą wartość typu int o wartości zero;
  60.          *  char_[2] - oznacza przypisany znak w ciągu do zdekodowania;
  61.         **/
  62.        
  63.         for(new i = 0 ; i < len ; i++) {
  64.             new char_[2];
  65.             char_[0] = Decoder[string[i++]];
  66.             char_[1] = Decoder[string[i]];
  67.             char_[0] = (char_[0] << 2) | ( (char_[1] >> 4) & 0x3 );
  68.             Position += format(second_string[Position], len, "%c", char_[0]);
  69.            
  70.             if(++i < len) {
  71.                 char_[0] = string[i];
  72.                 if(char_[0] == SpaceReplacer)
  73.                     break;
  74.                 char_[0] = Decoder[string[i]];
  75.                 char_[1] = ((char_[1] << 4) & 240) | ((char_[0] >> 2) & 15);
  76.                 Position += format(second_string[Position], len, "%c", char_[1]);
  77.             }
  78.            
  79.             if(++i < len) {
  80.                 char_[1] = string[i];
  81.                 if(char_[1] == SpaceReplacer)
  82.                     break;
  83.                 char_[1] = Decoder[string[i]];
  84.                 char_[0] = ((char_[0] << 6) & 192) | char_[1];
  85.                 Position += format(second_string[Position], len, "%c", char_[0]);
  86.             }
  87.         }
  88.         return true;
  89.     }
  90.    
  91.     /** Funkcja:
  92.      * encode 64
  93.      - string[] Koduje ciąg znaków.
  94.      - second_string[] Zapisuje ciąg znaków w string[]
  95.     **/
  96.     stock encode64(const string[], second_string[]) {
  97.         /** Zmienne:
  98.          *  SpaceReplacer - zamienia spacje na znaki równości;
  99.          *  len - oznacza długość zakodowanego ciągu znaków string[];
  100.          *  Position - oznacza pozycję w ciągu znaków;
  101.         **/
  102.        
  103.         new const SpaceReplacer = '=';
  104.         new len = strlen(string);
  105.         new Position;
  106.        
  107.         /** Zmienne:
  108.          *  i - tworzy zwykłą wartość typu int o wartości zero;
  109.          *  Character - oznacza przypisany znak w ciągu do zakodowania;
  110.         **/
  111.        
  112.         for(new i = 0 ; i < len ; i++) {
  113.             new Character;
  114.             Character = (string[i] >> 2) & 63;
  115.             Position += format(second_string[Position], len, "%c", LettersTable[Character]);
  116.             Character = (string[i] << 4) & 63;
  117.            
  118.             if(++i < len)
  119.                 Character |= (string[i] >> 4) & 15;
  120.             Position += format(second_string[Position], len, "%c", LettersTable[Character]);
  121.    
  122.             if(i < len) {
  123.                 Character = (string[i] << 2) & 63;
  124.                
  125.                 if(++i < len)
  126.                     Character |= (string[i] >> 6) & 3;
  127.                 Position += format(second_string[Position], len, "%c", LettersTable[Character]);
  128.             } else {
  129.                 i++;
  130.                 Position += format(second_string[Position], len, "%c", SpaceReplacer);
  131.             }
  132.    
  133.             if(i < len) {
  134.                 Character = string[i] & 63;
  135.                 Position += format(second_string[Position], len, "%c", LettersTable[Character]);
  136.             }
  137.             else Position += format(second_string[Position], len, "%c", SpaceReplacer);
  138.         }
  139.         return true;
  140.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement