Translate javascript program into C

I have a javascript file and I want to translate it to C, I did it, but I have a big runtime error. Everything works well until the end of the function when it returns an int. If you have any ideas where the error is. Many thanks.

#ifndef max
    #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
#endif

char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size)
{
   int count = stop - start;
   if ( count >= --size )
   {
      count = size;
   }
   sprintf(dst, "%.*s", count, src + start);
   return dst;
}

int CrackLog(char log[], char pw[])
{
    int tabc=3696619; //7
    char tab[]="                   azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN0123456789_$&#@";
    int i,checksum;

        checksum=tabc;
        int nblog=strlen(log);
        int nbpass=6;//6
        int sum=1;
        int n = max(nblog,nbpass);

        for (i=0;i<n;i++)
        {
            char *to;
            to = substring(i, i+1, log, to, sizeof to);
            int index1=strcspn(tab, to)+10;

            to = substring(i, i+1, pw, to, sizeof to);
            int index2=strcspn(tab, to)+10;

            sum=sum+(index1*n*(i+1))*(index2*(i+1)*(i+1));
        }

        if (sum==checksum) {
            return 1;
        }else
                    return 0;
}

Forgive my English, I am free. Mac fly

+3
source share
2 answers

sprintf requires you to allocate memory yourself.

Try changing char *to; to

char *to = (char*) malloc(sizeof(char)*(stop-start));

where start and stop are the first two arguments of the substring

You may need to turn it on stdlib.hif you are not already

+1
source

, log pw. n , , - . nbpass 6, , . , , (, min?).

tzenes, , dst . malloc, , , ( ).

. , :

  char to[2];
  to[1] = '\0';  // null terminate
  for (i=0;i<n;i++)
     {
     to[0] = log[i];
     int index1=strcspn(tab, to)+10;

     to[0] = pw[i];
     int index2=strcspn(tab, to)+10;

     sum=sum+(index1*n*(i+1))*(index2*(i+1)*(i+1));
     }
0

Source: https://habr.com/ru/post/1733985/


All Articles