使用字符数组位移连续内存块

问题描述

尝试创建我自己的 base64 编码,我知道还有其他的,但每次我尝试重新发明轮子时,我都会学到一些东西。

我正在尝试使用完整的字符数组作为内存块并移出我需要的内容。 我试过将它转换为 uintptr_t。 在这一点上,我的想法是,如果可能的话,我只是正确地移动指针地址而不是连续的内存块。

任何想法表示赞赏。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "base_n.h"
#include <stdint.h>




int main (void){

    

    char *foo = "foo";
        
    char * base64encode(char * input_string){
    
     /*
     * result needs to be 24 bits.
     * +--first octet--+-second octet--+--third octet--+
     * |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0| input blcoks
     * +-----------+---+-------+-------+---+-----------+
     * |5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0| output blocks
     * +--1.index--+--2.index--+--3.index--+--4.index--+
     * lets define a block as 3 bytes for input and 4 bytes for output
     * if input has a remainder when divisible by 3 then we need to add that
     * remainder for to complete the block.
     * the constant k has a 4:3 ratio from output:input block.
     * size of memory needed for output is 
     * 4(((strlen(input) % 3) + (strlen(input)))/3)
     */
     
        unsigned int input_string_length = strlen(input_string);
        
        unsigned int result_length = (4*(((input_string_length % 3) + (input_string_length))/3));
        
        char *result = malloc(result_length *sizeof(char));
    
        char *encode = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    
        /* the number of bits in input_string is input_string_length * 8
         * if 6 bits are subtracted from the bit size of string length each
         *iteration and masked with 0b0011111 or 0x3f then you get value needed for
         * that iteration.
         * keep interating while you have more than 8 bits.
         * */
        
        /* +----+
         * |ptr | 4 or 8 bytes
         * +----+
         *   |
         *   |              +---+---+---+---+
         *   -------> array | f | o | o |\0 |
         *                  +---+---+---+---+    
         * 
         * 
         */
        
        unsigned int shifter = (strlen(input_string)*8); 
        
        unsigned int blocks_used = 0;
       
        while(shifter >= 6){
        
        printf(" shifter: %d\n",shifter);
            
        shifter-= 6;
            
             
        printf(" input_string shifted %d is %d: \n",shifter,((*input_string >> shifter) & 0x3F));
        
        *result++ = encode[((*input_string >> shifter) & 0x3F)];
        
        printf(" result : %s\n",result);
        
        blocks_used++;
                
        
        }
        
        /* result_length - blocks_used tells you how many 6 bit blocks are left 
        * if the amoult left is 3 then you need the least two significant
        * bits from from the 8 byte block since only two bits of the 8 bit
        * blocks are left,this makes the last two 6 bit blocks padding ('=').
        * If 2 6 bit blocks are left then the last 2 6 bit blocks are padding
        * ('=').
        * If 1 6 bit block is left then just add padding to the last block 
        * is just padding ('=').
        * */
            
        printf(" result_length - blocks_used :  %d\n",result_length - blocks_used);
       // Todo fix my broke ass idea that is below 
        
        
        switch(result_length - blocks_used){
                
                case 3:
                    
                    *result++ = encode[input_string[input_string_length-1] & 0x03];
                    
                    *result++ = '=';
                    
                    *result = '=';
                    
                    break;
                case 2:
                    
                    *result++ = '=';
                    
                    *result = '=';
                    
                    break;
                    
                case 1:
                    
                    *result = '=';
                    
                    break;
                    
                default:
                    
                    break;
                
            }
        // END Todo
        return result;
        
    }
    
    
   char *out = base64encode(foo); 
   
   printf(" base64encode : %s \n",out); 
   
    return 0;
    
    
}

输出

shifter: 24

input_string shifted 18 is 0:

result : 
 
shifter: 18
 
input_string shifted 12 is 0: 
 
result : 
 
shifter: 12
 
input_string shifted 6 is 1: 
 
result : 
 
shifter: 6
 
input_string shifted 0 is 38: 
 
result : 
 
result_length - blocks_used :  0
 
base64encode :  

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)