lasarus / C-Compiler

Yet another C compiler.
27 stars 6 forks source link

hi,this base64 run erro #1

Closed icyfox168168 closed 1 year ago

icyfox168168 commented 2 years ago

void base64_encode(char dst, const unsigned char src, int len) { static const char b64[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

for (int i = 0; i < len; i += 3, dst += 4) {
    unsigned long x = (src[i] & 0xfful) << 16;
    dst[3] = i + 2 >= len ? '=' : b64[(x |= src[i + 2] & 0xfful) & 0x3f];
    dst[2] = i + 1 >= len ? '=' : b64[(x |= (src[i + 1] & 0xfful) << 8) >> 6 & 0x3f];
    dst[1] = b64[x >> 12 & 0x3f];
    dst[0] = b64[x >> 18];
}
*dst = '\0';

}

int base64_decode(unsigned char dst, const char src) { static const char b64[] = { ['A'] = 0, ['B'] = 1, ['C'] = 2, ['D'] = 3, ['E'] = 4, ['F'] = 5, ['G'] = 6, ['H'] = 7, ['I'] = 8, ['J'] = 9, ['K'] = 10, ['L'] = 11, ['M'] = 12, ['N'] = 13, ['O'] = 14, ['P'] = 15, ['Q'] = 16, ['R'] = 17, ['S'] = 18, ['T'] = 19, ['U'] = 20, ['V'] = 21, ['W'] = 22, ['X'] = 23, ['Y'] = 24, ['Z'] = 25, ['a'] = 26, ['b'] = 27, ['c'] = 28, ['d'] = 29, ['e'] = 30, ['f'] = 31, ['g'] = 32, ['h'] = 33, ['i'] = 34, ['j'] = 35, ['k'] = 36, ['l'] = 37, ['m'] = 38, ['n'] = 39, ['o'] = 40, ['p'] = 41, ['q'] = 42, ['r'] = 43, ['s'] = 44, ['t'] = 45, ['u'] = 46, ['v'] = 47, ['w'] = 48, ['x'] = 49, ['y'] = 50, ['z'] = 51, ['0'] = 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ['+'] = 62, ['/'] = 63, ['='] = 0, }; unsigned long x; int i, c, len, pad;

for (i = 0, x = 0, len = 0, pad = 0; src[i]; ++i) {
    c = src[i];
    if (c == '=' && (!src[i + 1] || (src[i + 1] == '=' && !src[i + 2])))
        ++pad;
    else if (c >= sizeof(b64) || (!b64[c] && c != 'A'))
        return 0;
    x = x << 6 | b64[c];
    if (i % 4 == 3) {
        dst[len + 2] = x & 0xff, x >>= 8;
        dst[len + 1] = x & 0xff, x >>= 8;
        dst[len + 0] = x & 0xff;
        len += 3;
    }
}
if (i % 4 != 0)
    return 0;
return len - pad;

}

int main () {

char base64_src[100] = { 0 }; base64_src[0] = '9';
char base64_dst[100] = { 0 };
char base64_nop[100] = { 0 };
base64_encode(base64_dst, base64_src, 1);// run ok

base64_decode(base64_nop, base64_dst);// run erro

return 0;

}

lasarus commented 2 years ago

Thank you for creating this issue. I have fixed what I think caused your error. Feel free to try again with the latest commit.

The source of the bug is that the compiler didn't take into account that arrays could be declared in an arbitrary order. Since ['='] = 0 comes last in the initializer-list, it thought that the array could only have 61 entries ('=' in ASCII is 61), even though 'z' in ASCII is 122.

icyfox168168 commented 2 years ago

Perfect, the test passed

icyfox168168 commented 2 years ago

Can this writing be supported? Next version

static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

Error on line 452 file src/arch/x64.c: "Not implemented"

base64_encode(char dst, const unsigned char src, int len) { / static const char b64[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; / static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

for (int i = 0; i < len; i += 3, dst += 4) {
    unsigned long x = (src[i] & 0xfful) << 16;
    dst[3] = i + 2 >= len ? '=' : b64[(x |= src[i + 2] & 0xfful) & 0x3f];
    dst[2] = i + 1 >= len ? '=' : b64[(x |= (src[i + 1] & 0xfful) << 8) >> 6 & 0x3f];
    dst[1] = b64[x >> 12 & 0x3f];
    dst[0] = b64[x >> 18];
}
*dst = '\0';

}

lasarus commented 2 years ago

I have fixed this now as well. The implementation is a bit hacky, but should work for most cases.

icyfox168168 commented 2 years ago

good,Perfect, the test passed

icyfox168168 commented 2 years ago

There may be a problem with code generation. I need to set the image address to 32 bits to compile

-Wl,--image-base -Wl,0x10000000

gcc -m64 -Wl,--image-base -Wl,0x10000000 test.s -o test.exe ok

gcc -m64 test.s -o test2.exe ko

C:\Users\168\AppData\Local\Temp\ccg2cHev.o:fake:(.text+0x28a): relocation truncated to fit: R_X86_64_32S against .data' C:\Users\168\AppData\Local\Temp\ccg2cHev.o:fake:(.text+0x533): relocation truncated to fit: R_X86_64_32S against.data'

icyfox168168 commented 2 years ago

This problem also exists in the tinycc compiler. Some codes can be compiled when the image address is lower than 32 bits, and when the image address is higher than 32 bits, but there are many running errors, but no one has corrected them

lasarus commented 2 years ago

I have never seen that error message before, but it might be related to the fact that the compiler doesn't generate position independent code. Try running gcc with the -no-pie flag.

icyfox168168 commented 2 years ago

TDM-GCC-64 10.3

mingw32-make

cc.exe -I test.c -D test.s

gcc -m64 -Wl,--image-base -Wl,0x10000000 test.s -o test.exe //ok

gcc -m64 -no-pie test.s -o test2.exe //ko

lasarus commented 2 years ago

I don't think I'm able to help you with this. I don't have much experience with linking on Windows.

If it is as you say, and tcc also has this problem, they can probably help you better.

icyfox168168 commented 2 years ago

I'm pretty sure it's the method of calling the function

The problem has been located, and it is repeatedly compared with the file generated by gcc

//call str_len1 gcc build ok //movq $str_len1,%rax //erro //callq *%rbx

lasarus commented 2 years ago

The current version of the compiler doesn't support calling labels directly. I could see that causing an issue if str_len1 has an address that doesn't fit in 32 bits. But that would also be a problem if you wanted to get the address of a function.

How does your GCC handle the following code?

void f();

void (*g())() {
    return f;
}

This requires the compiler to generate code to fetch the address of f.

Does replacing movq $str_len1,%rax with movabsq $str_len1,%rax fix the issue?

icyfox168168 commented 2 years ago

movabsq $str_len1,%rax //ok

lasarus commented 2 years ago

You can now use large code models with the flag -fcmodel=large. So, for example:

cc test.c test.s -fcmodel=large

This should solve your problem.

icyfox168168 commented 2 years ago

string need fix call fun is ok

movq $.L_rodata0, -132(%rbp)

.L_rodata0: .string "123"

lasarus commented 2 years ago

Accidentally put an if statement the wrong way around. It should work now.

icyfox168168 commented 2 years ago

ok,good,Perfect, the test passed

icyfox168168 commented 2 years ago

Error on line 521 file src/arch/x64.c: "Not implemented"

char* _88 ="88";
char decrypt[16] ={0};
MD5_CTX md5;
MD5Init(&md5);
//MD5Update(&md5, _88, str_len1(_88)); //ok
//MD5Update(&md5, "88", str_len1(_88)); // ko
MD5Final(&md5, decrypt);
icyfox168168 commented 2 years ago

rcx rdx r8 r9 。。。。。 Can parameter register allocation be compatible with Microsoft fastcall standard?

icyfox168168 commented 2 years ago

Error on line 62 file src/codegen/codegen.c: "Invalid operand type 1"

void cypher_msg(char RC4_SBOX, char msg, int length) { int i=0, j=0;

while(length > 0)
{
    i = (i+1) % 256;
    j = (j+RC4_SBOX[i]) % 256;
    swap(&RC4_SBOX[i], &RC4_SBOX[j]);
    //*msg++ ^= RC4_SBOX[(RC4_SBOX[i] + RC4_SBOX[j]) % 256]; // ko
    length--;
}

}

lasarus commented 2 years ago

Passing of strings to functions and ^= when lhs is char now works. Adding support for fastcall is outside of the scope of this project.

icyfox168168 commented 2 years ago

good ok

icyfox168168 commented 2 years ago

void init_sbox(char* RC4_SBOX) // ok

void init_sbox(unsigned char* RC4_SBOX) // ko

lasarus commented 2 years ago

What error message? Can you share code that reproduces the problem?

icyfox168168 commented 2 years ago

Error on line 389 file src/parser/expression.c: "NOt imp 15

lasarus commented 2 years ago

I am unable to reproduce. I need some sample code to solve this.

icyfox168168 commented 2 years ago

void init_sbox(unsigned char* RC4_SBOX) { for (int i = 0; i < 256; i++) RC4_SBOX[i] = i; }

icyfox168168 commented 2 years ago

define size_t int

define uint32_t unsigned int

void swap(char aa, char bb) { char tmp = *aa;

*aa = *bb;
*bb = tmp;

}

void init_sbox(unsigned char* RC4_SBOX) { for (int i = 0; i < 256; i++) RC4_SBOX[i] = i; }

void init_sbox_key( unsigned char RC4_SBOX, char key, int length) { int j = 0;

for(int i = 0; i < 256; i++)
{
    j = (j + RC4_SBOX[i] + key[i % length]) % 256;
    swap(&RC4_SBOX[i], &RC4_SBOX[j]);
}

}

void cypher_msg( unsigned char RC4_SBOX, char msg, int length) { int i=0, j=0;

while(length > 0)
{
    i = (i+1) % 256;
    j = (j+RC4_SBOX[i]) % 256;
    swap(&RC4_SBOX[i], &RC4_SBOX[j]);
    *msg++ ^= RC4_SBOX[(RC4_SBOX[i] + RC4_SBOX[j]) % 256];
    length--;
}

}

typedef struct { unsigned int count[2]; unsigned int state[4]; unsigned char buffer[64]; }MD5_CTX;

void __movsb(void dest, void src, size_t n) { char csrc = (char )src; char cdest = (char )dest;

for (int i=0; i<n; i++) cdest[i] = csrc[i]; }

define F(x,y,z) ((x & y) | (~x & z))

define G(x,y,z) ((x & z) | (y & ~z))

define H(x,y,z) (x^y^z)

define I(x,y,z) (y ^ (x | ~z))

define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))

define FF(a,b,c,d,x,s,ac) \

{ \
    a += F(b,c,d) + x + ac; \
    a = ROTATE_LEFT(a,s); \
    a += b; \
}

define GG(a,b,c,d,x,s,ac) \

{ \
    a += G(b,c,d) + x + ac; \
    a = ROTATE_LEFT(a,s); \
    a += b; \
}

define HH(a,b,c,d,x,s,ac) \

{ \
    a += H(b,c,d) + x + ac; \
    a = ROTATE_LEFT(a,s); \
    a += b; \
}

define II(a,b,c,d,x,s,ac) \

{ \
    a += I(b,c,d) + x + ac; \
    a = ROTATE_LEFT(a,s); \
    a += b; \
}

void MD5Transform(unsigned int state[4],unsigned char block[64]); void MD5Encode(unsigned char output,unsigned int input,unsigned int len); void MD5Decode(unsigned int output,unsigned char input,unsigned int len);

// __forceinline void MD5Init(MD5_CTX context) { context->count[0] = 0; context->count[1] = 0; context->state[0] = 0x67452301; context->state[1] = 0xEFCDAB89; context->state[2] = 0x98BADCFE; context->state[3] = 0x10325476; } // __forceinline void MD5Update(MD5_CTX context,unsigned char *input,unsigned int inputlen) { unsigned int i = 0,index = 0,partlen = 0; index = (context->count[0] >> 3) & 0x3F; partlen = 64 - index; context->count[0] += inputlen << 3; if(context->count[0] < (inputlen << 3)) { context->count[1]++; } context->count[1] += inputlen >> 29;

if(inputlen >= partlen) {
    __movsb(&context->buffer[index],input,partlen);
    MD5Transform(context->state,context->buffer);
    for(i = partlen;i+64 <= inputlen;i+=64) {
        MD5Transform(context->state,&input[i]);
    }
    index = 0;
} else {
    i = 0;
}

__movsb(&context->buffer[index],&input[i],inputlen-i);

}

// __forceinline void MD5Final(MD5_CTX *context,unsigned char digest[16]) {

unsigned char PADDING[] = {

0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };

unsigned int index = 0,padlen = 0;
unsigned char bits[8];
index = (context->count[0] >> 3) & 0x3F;
padlen = (index < 56)?(56-index):(120-index);
MD5Encode(bits,context->count,8);
MD5Update(context,PADDING,padlen);
MD5Update(context,bits,8);
MD5Encode(digest,context->state,16);

} // forceinline void MD5Encode(unsigned char output,unsigned int input,unsigned int len) { unsigned int i = 0,j = 0; while(j < len) { output[j] = input[i] & 0xFF; output[j+1] = (input[i] >> 8) & 0xFF; output[j+2] = (input[i] >> 16) & 0xFF; output[j+3] = (input[i] >> 24) & 0xFF; i++; j+=4; } } // forceinline void MD5Decode(unsigned int output,unsigned char input,unsigned int len) { unsigned int i = 0,j = 0; while(j < len) { output[i] = (input[j]) | (input[j+1] << 8) | (input[j+2] << 16) | (input[j+3] << 24); i++; j+=4; } } // __forceinline void MD5Transform(unsigned int state[4],unsigned char block[64]) { unsigned int a = state[0]; unsigned int bb = state[1]; unsigned int c = state[2]; unsigned int d = state[3]; unsigned int x[64];

MD5Decode(x,block,64);

FF(a, bb, c, d, x[ 0], 7, 0xd76aa478);
FF(d, a, bb, c, x[ 1], 12, 0xe8c7b756);
FF(c, d, a, bb, x[ 2], 17, 0x242070db);
FF(bb, c, d, a, x[ 3], 22, 0xc1bdceee);
FF(a, bb, c, d, x[ 4], 7, 0xf57c0faf);
FF(d, a, bb, c, x[ 5], 12, 0x4787c62a);
FF(c, d, a, bb, x[ 6], 17, 0xa8304613);
FF(bb, c, d, a, x[ 7], 22, 0xfd469501);
FF(a, bb, c, d, x[ 8], 7, 0x698098d8);
FF(d, a, bb, c, x[ 9], 12, 0x8b44f7af);
FF(c, d, a, bb, x[10], 17, 0xffff5bb1);
FF(bb, c, d, a, x[11], 22, 0x895cd7be);
FF(a, bb, c, d, x[12], 7, 0x6b901122);
FF(d, a, bb, c, x[13], 12, 0xfd987193);
FF(c, d, a, bb, x[14], 17, 0xa679438e);
FF(bb, c, d, a, x[15], 22, 0x49b40821);

GG(a, bb, c, d, x[ 1], 5, 0xf61e2562);
GG(d, a, bb, c, x[ 6], 9, 0xc040b340);
GG(c, d, a, bb, x[11], 14, 0x265e5a51);
GG(bb, c, d, a, x[ 0], 20, 0xe9b6c7aa);
GG(a, bb, c, d, x[ 5], 5, 0xd62f105d);
GG(d, a, bb, c, x[10], 9,  0x2441453);
GG(c, d, a, bb, x[15], 14, 0xd8a1e681);
GG(bb, c, d, a, x[ 4], 20, 0xe7d3fbc8);
GG(a, bb, c, d, x[ 9], 5, 0x21e1cde6);
GG(d, a, bb, c, x[14], 9, 0xc33707d6);
GG(c, d, a, bb, x[ 3], 14, 0xf4d50d87);
GG(bb, c, d, a, x[ 8], 20, 0x455a14ed);
GG(a, bb, c, d, x[13], 5, 0xa9e3e905);
GG(d, a, bb, c, x[ 2], 9, 0xfcefa3f8);
GG(c, d, a, bb, x[ 7], 14, 0x676f02d9);
GG(bb, c, d, a, x[12], 20, 0x8d2a4c8a);

HH(a, bb, c, d, x[ 5], 4, 0xfffa3942);
HH(d, a, bb, c, x[ 8], 11, 0x8771f681);
HH(c, d, a, bb, x[11], 16, 0x6d9d6122);
HH(bb, c, d, a, x[14], 23, 0xfde5380c);
HH(a, bb, c, d, x[ 1], 4, 0xa4beea44);
HH(d, a, bb, c, x[ 4], 11, 0x4bdecfa9);
HH(c, d, a, bb, x[ 7], 16, 0xf6bb4b60);
HH(bb, c, d, a, x[10], 23, 0xbebfbc70);
HH(a, bb, c, d, x[13], 4, 0x289b7ec6);
HH(d, a, bb, c, x[ 0], 11, 0xeaa127fa);
HH(c, d, a, bb, x[ 3], 16, 0xd4ef3085);
HH(bb, c, d, a, x[ 6], 23,  0x4881d05);
HH(a, bb, c, d, x[ 9], 4, 0xd9d4d039);
HH(d, a, bb, c, x[12], 11, 0xe6db99e5);
HH(c, d, a, bb, x[15], 16, 0x1fa27cf8);
HH(bb, c, d, a, x[ 2], 23, 0xc4ac5665);

II(a, bb, c, d, x[ 0], 6, 0xf4292244);
II(d, a, bb, c, x[ 7], 10, 0x432aff97);
II(c, d, a, bb, x[14], 15, 0xab9423a7);
II(bb, c, d, a, x[ 5], 21, 0xfc93a039);
II(a, bb, c, d, x[12], 6, 0x655b59c3);
II(d, a, bb, c, x[ 3], 10, 0x8f0ccc92);
II(c, d, a, bb, x[10], 15, 0xffeff47d);
II(bb, c, d, a, x[ 1], 21, 0x85845dd1);
II(a, bb, c, d, x[ 8], 6, 0x6fa87e4f);
II(d, a, bb, c, x[15], 10, 0xfe2ce6e0);
II(c, d, a, bb, x[ 6], 15, 0xa3014314);
II(bb, c, d, a, x[13], 21, 0x4e0811a1);
II(a, bb, c, d, x[ 4], 6, 0xf7537e82);
II(d, a, bb, c, x[11], 10, 0xbd3af235);
II(c, d, a, bb, x[ 2], 15, 0x2ad7d2bb);
II(bb, c, d, a, x[ 9], 21, 0xeb86d391);
state[0] += a;
state[1] += bb;
state[2] += c;
state[3] += d;

}

unsigned int str_len1(const char *s) { int l;for(l=0;s[l]!='\0';l++);return l; }

size_t str_len2 (const char *str) { for (size_t len = 0;;++len) if (str[len]==0) return len; }

size_t str_len3 (const char str) { return (str) ? str_len3(++str) + 1 : 0; }

void base64_encode(char dst, const unsigned char src, size_t len) { static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

for (size_t i = 0; i < len; i += 3, dst += 4) {
    unsigned long x = (src[i] & 0xfful) << 16;
    dst[3] = i + 2 >= len ? '=' : b64[(x |= src[i + 2] & 0xfful) & 0x3f];
    dst[2] = i + 1 >= len ? '=' : b64[(x |= (src[i + 1] & 0xfful) << 8) >> 6 & 0x3f];
    dst[1] = b64[x >> 12 & 0x3f];
    dst[0] = b64[x >> 18];
}
*dst = '\0';

}

size_t base64_decode(unsigned char dst, const char src) { static const char b64[] = { ['A'] = 0, ['B'] = 1, ['C'] = 2, ['D'] = 3, ['E'] = 4, ['F'] = 5, ['G'] = 6, ['H'] = 7, ['I'] = 8, ['J'] = 9, ['K'] = 10, ['L'] = 11, ['M'] = 12, ['N'] = 13, ['O'] = 14, ['P'] = 15, ['Q'] = 16, ['R'] = 17, ['S'] = 18, ['T'] = 19, ['U'] = 20, ['V'] = 21, ['W'] = 22, ['X'] = 23, ['Y'] = 24, ['Z'] = 25, ['a'] = 26, ['b'] = 27, ['c'] = 28, ['d'] = 29, ['e'] = 30, ['f'] = 31, ['g'] = 32, ['h'] = 33, ['i'] = 34, ['j'] = 35, ['k'] = 36, ['l'] = 37, ['m'] = 38, ['n'] = 39, ['o'] = 40, ['p'] = 41, ['q'] = 42, ['r'] = 43, ['s'] = 44, ['t'] = 45, ['u'] = 46, ['v'] = 47, ['w'] = 48, ['x'] = 49, ['y'] = 50, ['z'] = 51, ['0'] = 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ['+'] = 62, ['/'] = 63, ['='] = 0, };

unsigned long x;
size_t i, c, len, pad;

for (i = 0, x = 0, len = 0, pad = 0; src[i]; ++i) {
    c = src[i];
    if (c == '=' && (!src[i + 1] || (src[i + 1] == '=' && !src[i + 2])))
        ++pad;
    else if (c >= sizeof(b64) || (!b64[c] && c != 'A'))
        return 0;
    x = x << 6 | b64[c];
    if (i % 4 == 3) {
        dst[len + 2] = x & 0xff, x >>= 8;
        dst[len + 1] = x & 0xff, x >>= 8;
        dst[len + 0] = x & 0xff;
        len += 3;
    }
}
if (i % 4 != 0)
    return 0;
return len - pad;

}

void tea_encrypt(uint32_t vv, uint32_t kk) { uint32_t v0 = vv[0], v1 = vv[1], sum = 0, i; / set up / uint32_t delta = 0x9e3779b9; / a key schedule constant / uint32_t k0 = kk[0], k1 = kk[1], k2 = kk[2], k3 = kk[3]; / cache key / for (i = 0; i < 16; i++) { / basic cycle start / sum += delta; v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1); v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3); } / end cycle / vv[0] = v0; vv[1] = v1; }

void tea_decrypt(uint32_t vv, uint32_t kk) { uint32_t v0 = vv[0], v1 = vv[1], i; / set up / uint32_t delta = 0x9e3779b9; / a key schedule constant / uint32_t sum = delta << 4; uint32_t k0 = kk[0], k1 = kk[1], k2 = kk[2], k3 = kk[3]; / cache key / for (i = 0; i < 16; i++) { / basic cycle start / v1 -= ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3); v0 -= ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1); sum -= delta; } / end cycle / vv[0] = v0; vv[1] = v1; }

int get_multi_value(int a, int bb) { int res = 0; int sign = 1; if (bb < 0) { bb = -bb; sign = -1; }

while (bb)
{
    if (bb & 0x1)res += a;
    a <<= 1;
    bb >>= 1;
}

if (sign == -1)res = -res;
return res;

}

//attribute((dllexport)) int main () {

get_multi_value(10,10);

char* _88 ="88";
char decrypt[16] ={0};
MD5_CTX md5;
MD5Init(&md5);
MD5Update(&md5, "88", str_len1("88"));
MD5Final(&md5, decrypt);

char rc4sbox2[256] ={0}; char rc4key[32] ={0};

init_sbox(rc4sbox2); init_sbox_key(rc4sbox2, rc4key, sizeof(rc4key)); cypher_msg(rc4sbox2, decrypt, sizeof(decrypt));
init_sbox(rc4sbox2); init_sbox_key(rc4sbox2, rc4key, sizeof(rc4key)); cypher_msg(rc4sbox2, decrypt, sizeof(decrypt));

tea_encrypt((uint32_t*)&decrypt[0], (uint32_t*)rc4key);
tea_decrypt((uint32_t*)&decrypt[0], (uint32_t*)rc4key);

if (decrypt[0] == 0x2A && decrypt[1] == 0x38) { return 1; }

long long ret = str_len1("123");
if (ret == 3)
{
    str_len1("123");str_len2("123");str_len3("123");
}
char base64_src[100] = { 0 }; base64_src[0] = '9';
char base64_dst[100] = { 0 };
char base64_nop[100] = { 0 };
base64_encode(base64_dst, base64_src, 1);

base64_decode(base64_nop, base64_dst);

return 0;

}

lasarus commented 2 years ago

It is fixed now.

icyfox168168 commented 2 years ago

build ok

but

char unsigned char cc.exe build same asm code

gcc and tcc build Different

The results after running are also different

void init_sbox(char* RC4_SBOX)

void init_sbox_key( char RC4_SBOX, char key, int length)

void cypher_msg( char RC4_SBOX, char msg, int length)

// void init_sbox(unsigned char* RC4_SBOX)

void init_sbox_key(unsigned char RC4_SBOX, char key, int length)

void cypher_msg( unsigned char RC4_SBOX, char msg, int length)

lasarus commented 2 years ago

There were some errors in the codegen of the type conversions. It should be fixed now.

icyfox168168 commented 2 years ago

//build ok // run point erro

void init_sbox(char* RC4_SBOX)

void init_sbox_key( char RC4_SBOX, char key, int length)

void cypher_msg( char RC4_SBOX, char msg, int length)

// build ok //run ok void init_sbox(unsigned char* RC4_SBOX)

void init_sbox_key(unsigned char RC4_SBOX, char key, int length)

void cypher_msg( unsigned char RC4_SBOX, char msg, int length)

lasarus commented 2 years ago

This is an error in your code. j becomes negative in init_sbox_key when RC4_SBOX is signed. The array acces is then undefined behavior.

icyfox168168 commented 2 years ago

Well, there is a flaw in the code. My doubt is that gcc and tcc will not have pointer errors, although the values ​​are incorrect.

icyfox168168 commented 2 years ago

Got ; (109), expected END OF INPUT (117) Error on line 20 file src/parser/parser.c: "Expected other token"

icyfox168168 commented 2 years ago

/ Period parameters /

define N 624

define M 397

define MATRIX_A 0x9908b0df / constant vector a /

define UPPER_MASK 0x80000000 / most significant w-r bits /

define LOWER_MASK 0x7fffffff / least significant r bits /

/ Tempering parameters /

define TEMPERING_MASK_B 0x9d2c5680

define TEMPERING_MASK_C 0xefc60000

define TEMPERING_SHIFT_U(y) (y >> 11)

define TEMPERING_SHIFT_S(y) (y << 7)

define TEMPERING_SHIFT_T(y) (y << 15)

define TEMPERING_SHIFT_L(y) (y >> 18)

static unsigned long mt[N]; / the array for the state vector / static int mti = N + 1; / mti==N+1 means mt[N] is not initialized /

/ initializing the array with a NONZERO seed / void sgenrand(unsigned long seed) { / setting initial seeds to mt[N] using / / the generator Line 25 of Table 1 in / / [KNUTH 1981, The Art of Computer Programming / / Vol. 2 (2nd Ed.), pp102] / mt[0] = seed & 0xffffffff; for (mti = 1; mti < N; mti++) mt[mti] = (69069 * mt[mti - 1]) & 0xffffffff; }

unsigned long genrand() { unsigned long y; static unsigned long mag01[2] = { 0x0, MATRIX_A }; / mag01[x] = x MATRIX_A for x=0,1 */

if (mti >= N) { /* generate N words at one time */
    int kk;

    if (mti == N + 1)   /* if sgenrand() has not been called, */
        sgenrand(4357); /* a default initial seed is used   */

    for (kk = 0; kk < N - M; kk++) {
        y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
        mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1];
    }
    for (; kk < N - 1; kk++) {
        y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
        mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1];
    }
    y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
    mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1];

    mti = 0;
}

y = mt[mti++];
y ^= TEMPERING_SHIFT_U(y);
y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
y ^= TEMPERING_SHIFT_L(y);

return y;

} int rand_reg(int begin, int end) { if (end < begin) { return -1; };

if (end == begin)
    return end;

return (genrand() / (0xFFFFFFFF / (end - begin + 1))) + begin;

};

//attribute((dllexport)) int main () {

rand_reg(1,10); return 0; }

lasarus commented 2 years ago

You have an extra ; after rand_reg. This is not legal C, and not supported by this compiler.

icyfox168168 commented 2 years ago

Removed ;it works very well

icyfox168168 commented 2 years ago

A possible bug, also a tinycc bug

Can’t get consistent results, please see if it’s a bug?

include

define SIZE 10

extern int myarr[SIZE]; int myarr[] = { 11, 34, };

int main(void) { for (int i = 0; i < SIZE; ++i) { printf("%d\n",myarr[i]); } getchar(); return 0; }

gcc out

11 34 0 0 0 0 0 0 0 0

cc out

11 34 xx xx xx xx xx xx xx xx

lasarus commented 2 years ago

Yes, this is an bug in my compiler. But the standard is not very clear on this point. I have fixed it with the latest commit.

icyfox168168 commented 2 years ago

Error on line 347 file src/parser/function_parser.c: "Not implemented"

int main() { int ret = 0; goto label; ret = 123321; label: ret = 456654; return ret; }

icyfox168168 commented 2 years ago

build cc.exe loop loop loop loop ...............................

int a () { return 10; }

int (*b())() { return a; }

int ((pf())())() { return b; }

int main() { int r = pf()()(); int ret = 0; goto label; ret = 123321; label: ret = 456654; return ret; }

icyfox168168 commented 2 years ago

cc.exe out ok

11 34 0 0 0 0 0 0 0 0

icyfox168168 commented 2 years ago

Can’t get consistent results, please see if it’s a bug?

void My_Fun2(void point) { return point; }

typedef void(P_Fun)(void* point);static P_Fun Old_Fun = My_Fun2;

void My_Fun(void point) { //Old_Fun = (P_Fun)&My_Fun2; return Old_Fun(point); } int main() {

My_Fun(&main); return 0; }

icyfox168168 commented 2 years ago

//typedef

This line of code was swallowed by github*, remember to add when copying

lasarus commented 2 years ago

All of these are now fixed with the latest commits.

icyfox168168 commented 2 years ago

add some label build ko /
goto label2; goto label; label2: ret = 123321; goto labelexit; label: ret = 456654; labelexit:
/

int a () { return 10; }

int (*b())() { return a; }

int ((pf())())() { return b; }

int main() { int r = pf()()(); int ret = 0; goto label2; goto label; label2: ret = 123321; goto labelexit; label: ret = 456654; labelexit: return ret; }

lasarus commented 2 years ago

There was a mistake with a loop condition. It should work now.

icyfox168168 commented 2 years ago

all ok

icyfox168168 commented 2 years ago

Error on line 560 file src/arch/x64.c: "Not implemented"

struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book = {"1", "2", "3", 123456};

int main() {

int ret = 0;

if (book.book_id == 123456) { ret++; } return ret; }