Need text of void encode( char* buf, int size );

Pages: 12
Need text of presumably encode.cpp presumably from GCC static library.

Known info:

void encode( char* buf, int size );
encode( m_buf, m_size );

Need decode() too, unsure about the exact signature.

Thanks!

nb: for some reason I have trouble finding this in the gcc source code...

Hello Oknel,

Welcome to the forum.

I am not familiar with the GCC headerm but you may find this useful https://www.gnu.org/software/libc/manual/html_node/crypt.html

Hope that helps,

Andy
привет Oknel.

It does not exist in the C or C++ standard library, or in any GCC extension that I know of.

If you tell us what you want to do, we can try to help you.
Last edited on
Not sure where exactly but it does exist. I need to do a bunch of tests with it after installing Cygwin with g++ (GCC). For my own good I want to see the code, not just the result of function calls
There's a library called cryptopp-cygwin, which looks like it's part of cygwin.

https://github.com/cawka/cryptopp-cygwin

https://github.com/cawka/cryptopp-cygwin/blob/master/integer.h
https://github.com/cawka/cryptopp-cygwin/blob/master/integer.cpp

integer.h
 
void Encode(byte *output, size_t outputLen, Signedness=UNSIGNED) const;


integer.cpp
1
2
3
4
5
void Integer::Encode(byte *output, size_t outputLen, Signedness signedness) const
{
	ArraySink sink(output, outputLen);
	Encode(sink, outputLen, signedness);
}


I don't know if that meets your requirements. You still have not said what you're trying to do. Are you trying to convert from big-endian to little-endian?
Last edited on
How did you find out that there is such a function? "encode" is a very generic name so it's hard to know where it belongs and what kind of encoding it's doing.
I have 2 files test.h and test.cpp
I am supposed to write a bunch of code without touching
void encode( char* buf, int size );

main(){} doesn't contain much and there is no custom implementation of encode(). There are only 3 files and so encode() comes from g++. Since I have char* I expect it to be for strings.
Try using grep -r "encode" . or grep -r "encode(" . or grep -r "void encode.*(" . or similar inside the directory where your compiler lives. If it's compiling and not crashing, the definition is somewhere.
Last edited on
I have 2 files test.h and test.cpp
I am supposed to write a bunch of code without touching
void encode( char* buf, int size );

Are you sure you are not the one who's supposed to write that function? If it's declared in test.h it should probably be defined in test.cpp.
Lol: I AM sure. I am prohibited to write/modify this function, very explicitly. Declared in test.h. Shows up in test.cpp inside a function as

::encode( tbuf, tsize );

Used in a couple of other places too :)
Last edited on
Is this school work or something like that? Are you sure you understood the problem statement correctly? "Write an implementation of this function that matches this signature and passes these tests. Don't modify the signature" is quite a common problem in programming courses.
Last edited on
1
2
3
4
5
6
/*
 * This function is defined in encode.cpp and provided as
 * GCC static library.
 *
 */
void encode( char* buf, int size );
I don't suppose you've tried searching your computer for encode.cpp?
Yeah, you've misunderstood the instructions.
There is no "encode" in gcc.
Now I think you're just being dishonest with that apparently fake comment.
And if you're not allowed to modify it, why do you need its implementation?
Last edited on
"Never attribute to malice that which can be adequately explained by stupidity."

I don't think he's lying, but rather it's ambiguity in the term "GCC static library". I do not think his instructor means that the code is part of the GCC C++ standard library+extensions.

@Oknel, your instructor most likely means the function is defined in a static library compatible with GCC. This is will most likely be called "lib*.a"

For example, libencode.a or something. Do you have any files like that available? (.a extension)
You need to link to this static library.
https://stackoverflow.com/questions/6578484/telling-gcc-directly-to-link-a-library-statically
Last edited on
just use base64 encoding/decoding.
The following is based on an upvoted StackOverflow answer from Manuel Martinez.

Edit: Somewhat cleaned, refactored, added a few tests.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <vector>
#include <array>

static const std::string BASE64_CHARS = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

static std::string Base64Encode(const std::string& input) 
{
    std::string encoded;

    unsigned val = 0;
    int valb = -6;
    for(unsigned char c : input) 
    {
        val = (val<<8) + c;
        valb += 8;
        while(valb >= 0) 
        {
            encoded.push_back(BASE64_CHARS[ (val>>valb) & 0x3F ]);
            valb-=6;
        }
    }
    if (valb>-6) 
        encoded.push_back(BASE64_CHARS[ ((val<<8)>>(valb+8)) & 0x3F ]);
    while(encoded.size() % 4) 
        encoded.push_back('=');

    return encoded;
}

static std::string Base64Decode(const std::string& encoded_string) 
{
    std::string decoded;

    // Prep the decode table
    std::array<int, 256> table;
    table.fill(-1);
    for (int i=0; i<64; i++) 
        table[BASE64_CHARS[i]] = i;

    unsigned val = 0;
    int valb = -8;
    for (unsigned char c : encoded_string)
    {
        if (table[c] == -1) 
            break;
        val = (val<<6) + table[c];
        valb += 6;
        if (valb>=0) 
        {
            decoded.push_back(char((val>>valb) & 0xFF));
            valb-=8;
        }
    }
    return decoded;
}

int main() 
{
    using namespace std;

    vector<string> v
    {
        "Hello!",
        "Natural selection is anything but random.",
        "Every beginning must have an end.",
        "Call me Ishmael.",
        "Cooper has scored 114 offenses against literary art out of a possible 115. It breaks the record."
    };

    string encoded, decoded;
    for (string& orig : v)
    {
        cout << "original: " << orig << '\n';
        encoded = Base64Encode(orig);
        cout << "encoded:  " << encoded << '\n';
        decoded = Base64Decode(encoded);
        cout << "decoded:  " << decoded << '\n' << '\n';
    }

    return 0;
}

original: Hello!
encoded:  SGVsbG8h
decoded:  Hello!

original: Natural selection is anything but random.
encoded:  TmF0dXJhbCBzZWxlY3Rpb24gaXMgYW55dGhpbmcgYnV0IHJhbmRvbS4=
decoded:  Natural selection is anything but random.

original: Every beginning must have an end.
encoded:  RXZlcnkgYmVnaW5uaW5nIG11c3QgaGF2ZSBhbiBlbmQu
decoded:  Every beginning must have an end.

original: Call me Ishmael.
encoded:  Q2FsbCBtZSBJc2htYWVsLg==
decoded:  Call me Ishmael.

original: Cooper has scored 114 offenses against literary art out of a possible 115. It breaks the record.
encoded:  Q29vcGVyIGhhcyBzY29yZWQgMTE0IG9mZmVuc2VzIGFnYWluc3QgbGl0ZXJhcnkgYXJ0IG91dCBvZiBhIHBvc3NpYmxlIDExNS4gSXQgYnJlYWtzIHRoZSByZWNvcmQu
decoded:  Cooper has scored 114 offenses against literary art out of a possible 115. It breaks the record.


This can obviously be altered to accept a char* and a size.

As Ganado found, the Russian thread where OP also posted https://www.linux.org.ru/forum/development/14380125 . One joker says, "to not start another thread, I'll write here -- I need a woman and food, thx!" Another chimes in, "Me too, w/ some beer." And another says "Preferably where it's warm near the sea".

They basically all agreed it's not really present in the GCC source/extensions, and one guy even offered a basic XOR implementation strategy.
Last edited on
libencode.a
Ganado wrote:
"Never attribute to malice that which can be adequately explained by stupidity."

You're absolutely right.
Ok, thanks all for the time spent on this issue.

Most useful info/suggestions:

. grep
. lib*.a
. Examples of encode/decode

I need implementation to avoid working with a black box. No more no less :).
Last edited on
Where did that libencode.a file come from?
Pages: 12