0

I`m trying to make multi converter.

and i got unexpected result.

Here is my Code (this code can compile immediately)

#include <iostream>
#include <string.h>
#include <iconv.h>
using namespace std;

static int DecodeMimeBase64[256] = {
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
    52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
    -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
    15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
    -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
    41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
};

int fnBase64_decode(char *text, unsigned char *dst, int numBytes) {
    char *cp;
    int space_idx = 0, phase;
    int d, prev_d = 0;
    unsigned char c;
    printf("text : %s\n", text);
    printf("dst : %s\n", dst);
    space_idx = 0;
    phase = 0;

    for (cp = text; *cp != '\0'; ++cp) {
        d = DecodeMimeBase64[(int) *cp];
        if (d != -1) {
            switch (phase) {
            case 0:
                ++phase;
                break;
            case 1:
                c = ((prev_d << 2) | ((d & 0x30) >> 4));
                if (space_idx < numBytes)
                    dst[space_idx++] = c;
                ++phase;
                break;
            case 2:
                c = (((prev_d & 0xf) << 4) | ((d & 0x3c) >> 2));
                if (space_idx < numBytes)
                    dst[space_idx++] = c;
                ++phase;
                break;
            case 3:
                c = (((prev_d & 0x03) << 6) | d);
                if (space_idx < numBytes)
                    dst[space_idx++] = c;
                phase = 0;
                break;
            }
            prev_d = d;
        }
    }
    printf("text : %s\n", text);
    printf("dst : %s\n", dst);
    return space_idx;
}

int main() {

string SubjectString;
string Temp1SubString;
string Temp2SubString;
printf("SubjectString -1 : %s\n", SubjectString.c_str());
printf("Temp1SubString-1 : %s\n", Temp1SubString.c_str());
printf("Temp2SubString-1 : %s\n", Temp2SubString.c_str());

Temp1SubString = "VGVzdMbtwfY=?=";

fnBase64_decode((char *) Temp1SubString.c_str(), (unsigned char *) Temp2SubString.c_str(), strlen((char *) Temp1SubString.c_str()));
printf("SubjectString -2 : %s\n", SubjectString.c_str());
printf("Temp1SubString-2 : %s\n", Temp1SubString.c_str());
printf("Temp2SubString-2 : %s\n", Temp2SubString.c_str());

return 0;
}

Result should be like this

SubjectString -1 :
Temp1SubString-1 :
Temp2SubString-1 :
text : VGVzdMbtwfY=?=
dst :
text : VGVzdMbtwfY=?=
dst : Test▦▦▦▦
SubjectString -2 : 
Temp1SubString-2 : VGVzdMbtwfY=?=
Temp2SubString-2 : Test▦▦▦▦

But in my Result

SubjectString -1 :
Temp1SubString-1 :
Temp2SubString-1 :
text : VGVzdMbtwfY=?=
dst :
text : VGVzdMbtwfY=?=
dst : Test▦▦▦▦
SubjectString -2 : Test▦▦▦▦
Temp1SubString-2 : VGVzdMbtwfY=?=
Temp2SubString-2 : Test▦▦▦▦

SubjectString -2 : Test▦▦▦▦ -> should not be happen!

but SubjectString got same value with Temp2SubString.

I need some help.

edit1:

and i try to SubjectString.clear();

after funtion 'fnBase64_decode' but it is not work..

Thanks.

hybang
  • 65
  • 10
  • It looks like the decoded string isn't `\0` terminated. – Alex Mar 24 '17 at 04:59
  • 1
    `(char *) Temp1SubString.c_str()` is very, very dangerous! You haven't [`reserve`](http://en.cppreference.com/w/cpp/string/basic_string/reserve)d any room for these strings, so you could be overwriting memory anywhere! [`std::string::data`](http://en.cppreference.com/w/cpp/string/basic_string/data) is a more standard way to get at the bytes, but remember that you can only write to [`capacity()`](http://en.cppreference.com/w/cpp/string/basic_string/capacity) bytes! – Ken Y-N Mar 24 '17 at 05:00
  • 3
    The need to cast away `const`ness is generally a hint that you're doing it wrong. – Ignacio Vazquez-Abrams Mar 24 '17 at 05:01
  • I tried Temp1SubString.c_str() before.. But this method is const char .. and make compile error. – hybang Mar 24 '17 at 05:05
  • @hybang, Your code works fine with clang and I get the correct result. However, there are a bunch of warnings as I suspected. I suggest to turn on *all* warning flags while compiling (with clang its `-Weverything`) and address them. – Arash Mar 24 '17 at 05:29
  • @Arash i dnt knw how it correctly work in clang.. even in C++ not work correctly.. and thanks for suggest.. i think answer is http://stackoverflow.com/questions/7352099/stdstring-to-char – hybang Mar 24 '17 at 05:36
  • @hybang -- What I don't understand is: how come the value of `SubjectString` changes based on your output while you don't even modify this string in your code. Are you sure the current code is complete and you included all parts? – Arash Mar 24 '17 at 05:57
  • @Arash Yes! i copy and paste whole code from include to end. i also dont understand, even i didnt do anything with `SubjectString` but get value. and now i got an answer to correct. if you want i can paste new one. – hybang Mar 24 '17 at 06:11

0 Answers0