ext/iodine/fio.c
/*
 * The actual encoding logic. The map can be switched for encoding variations.
 */
The actual encoding logic. The map can be switched for encoding variations.
The actual encoding logic . The map can be switched for encoding variations .
static inline int fio_base64_encode_internal(char *target, const char *data,
                                             int len,
                                             const char *base64_encodes) {
  int groups = len / 3;
  const int mod = len - (groups * 3);
  const int target_size = (groups + (mod != 0)) * 4;
  char *writer = target + target_size - 1;
  const char *reader = data + len - 1;
  writer[1] = 0;
  switch (mod) {
  case 2: {
    char tmp2 = *(reader--);
    char tmp1 = *(reader--);
    *(writer--) = '=';
    *(writer--) = base64_encodes[((tmp2 & 15) << 2)];
    *(writer--) = base64_encodes[((tmp1 & 3) << 4) | ((tmp2 >> 4) & 15)];
    *(writer--) = base64_encodes[(tmp1 >> 2) & 63];
  } break;
  case 1: {
    char tmp1 = *(reader--);
    *(writer--) = '=';
    *(writer--) = '=';
    *(writer--) = base64_encodes[(tmp1 & 3) << 4];
    *(writer--) = base64_encodes[(tmp1 >> 2) & 63];
  } break;
  }
  while (groups) {
    groups--;
    const char tmp3 = *(reader--);
    const char tmp2 = *(reader--);
    const char tmp1 = *(reader--);
    *(writer--) = base64_encodes[tmp3 & 63];
    *(writer--) = base64_encodes[((tmp2 & 15) << 2) | ((tmp3 >> 6) & 3)];
    *(writer--) = base64_encodes[(((tmp1 & 3) << 4) | ((tmp2 >> 4) & 15))];
    *(writer--) = base64_encodes[(tmp1 >> 2) & 63];
  }
  return target_size;
}
static inline int fio_base64_encode_internal ( char * target , const char * data , int len , const char * base64_encodes ) { int groups = len / 3 ; const int mod = len - ( groups * 3 ) ; const int target_size = ( groups + ( mod != 0 ) ) * 4 ; char * writer = target + target_size - 1 ; const char * reader = data + len - 1 ; writer [ 1 ] = 0 ; switch ( mod ) { case 2 : { char tmp2 = * ( reader -- ) ; char tmp1 = * ( reader -- ) ; * ( writer -- ) = ' ' ; * ( writer -- ) = base64_encodes [ ( ( tmp2 & 15 ) << 2 ) ] ; * ( writer -- ) = base64_encodes [ ( ( tmp1 & 3 ) << 4 ) | ( ( tmp2 >> 4 ) & 15 ) ] ; * ( writer -- ) = base64_encodes [ ( tmp1 >> 2 ) & 63 ] ; } break ; case 1 : { char tmp1 = * ( reader -- ) ; * ( writer -- ) = ' ' ; * ( writer -- ) = ' ' ; * ( writer -- ) = base64_encodes [ ( tmp1 & 3 ) << 4 ] ; * ( writer -- ) = base64_encodes [ ( tmp1 >> 2 ) & 63 ] ; } break ; } while ( groups ) { groups -- ; const char tmp3 = * ( reader -- ) ; const char tmp2 = * ( reader -- ) ; const char tmp1 = * ( reader -- ) ; * ( writer -- ) = base64_encodes [ tmp3 & 63 ] ; * ( writer -- ) = base64_encodes [ ( ( tmp2 & 15 ) << 2 ) | ( ( tmp3 >> 6 ) & 3 ) ] ; * ( writer -- ) = base64_encodes [ ( ( ( tmp1 & 3 ) << 4 ) | ( ( tmp2 >> 4 ) & 15 ) ) ] ; * ( writer -- ) = base64_encodes [ ( tmp1 >> 2 ) & 63 ] ; } return target_size ; }
The actual encoding logic.
The actual encoding logic .
/* walk backwards, allowing fo inplace decoding (target == data) */