/******************************************************************** * Print a block. * Output a Block with printf. ********************************************************************/ void printBlock(struct Block *stBlock) { int i; DEBUG ? printf("printBlock\n") : 0; DEBUG ? printf("block iLength = %d\n", stBlock->iLength) : 0; for (i = 0; i < stBlock->iLength; i++) { printSentence(stBlock->stSentence[i]); } }
int main( int argc, char **argv ) { if( argc <= 2 ) { usage(argv[0]); return 1; } printSentence( argv[1], atoi(argv[2]) ); return 0; }
/******************************************************************** * Write a sentence (multiple words) to the socket ********************************************************************/ void writeSentence(int fdSock, struct Sentence *stWriteSentence) { int iIndex; if (stWriteSentence->iLength == 0) { return; } DEBUG ? printf("Writing sentence\n") : 0; DEBUG ? printSentence(stWriteSentence) : 0; for (iIndex = 0; iIndex < stWriteSentence->iLength; iIndex++) { writeWord(fdSock, stWriteSentence->szSentence[iIndex]); } writeWord(fdSock, ""); }
/*Funcão de busca de uma palavra na árvore*/ void STsearchWord(char *word,char op){ int *aux = STsearchWordText(lowerCaseLetter(word)); int x; printf ("Word : %s\n\n",word); if(aux != NULL){ for (x = 1; x < aux[0]; x++){ if(aux[x] != aux[x-1]){ if (op == 'V'){ printAnnotated(aux[x]-1); } else printSentence(aux[x] -1,op); } } } else{ printf ("A palavra nao esta disponivel!\n"); } printf ("\n"); }
void STseekWord(char *word,char op){ int *aux = STinjectedWord (lowerCaseLetter(word)); int x = 1; printf ("Word : %s\n\n",word); if(aux != NULL){ for (; x < aux[0]; x++){ /*if(aux[x] != aux[x-1]){*/ if (op == 'V'){ printf ("\n\n---------------------------------------------------------\n\n"); printAnnotated(aux[x]-1); } else printSentence(aux[x] -1,op); } } else{ printf ("A palavra nao esta disponivel!\n"); } printf ("\n"); }
/******************************************************************** * Login to the API * 1 is returned on successful login * 0 is returned on unsuccessful login ********************************************************************/ int login(int fdSock, char *username, char *password) { struct Sentence stReadSentence; struct Sentence stWriteSentence; char *szMD5Challenge; char *szMD5ChallengeBinary; char *szMD5PasswordToSend; char *szLoginUsernameResponseToSend; char *szLoginPasswordResponseToSend; md5_state_t state; md5_byte_t digest[16]; char cNull[1] = { 0 }; writeWord(fdSock, "/login"); writeWord(fdSock, ""); stReadSentence = readSentence(fdSock); DEBUG ? printSentence(&stReadSentence) : 0; if (stReadSentence.iReturnValue != DONE) { //printf("error.\n"); //exit(0); return 0; } // extract md5 string from the challenge sentence szMD5Challenge = strtok(stReadSentence.szSentence[1], "="); szMD5Challenge = strtok(NULL, "="); DEBUG ? printf("MD5 of challenge = %s\n", szMD5Challenge) : 0; // convert szMD5Challenge to binary szMD5ChallengeBinary = md5ToBinary(szMD5Challenge); // get md5 of the password + challenge concatenation md5_init(&state); md5_append(&state, cNull, 1); md5_append(&state, (const md5_byte_t *)password, strlen(password)); md5_append(&state, (const md5_byte_t *)szMD5ChallengeBinary, 16); md5_finish(&state, digest); // convert this digest to a string representation of the hex values // digest is the binary format of what we want to send // szMD5PasswordToSend is the "string" hex format szMD5PasswordToSend = md5DigestToHexString(digest); DEBUG ? printf("szPasswordToSend = %s\n", szMD5PasswordToSend) : 0; // put together the login sentence initializeSentence(&stWriteSentence); addWordToSentence(&stWriteSentence, "/login"); addWordToSentence(&stWriteSentence, "=name="); addPartWordToSentence(&stWriteSentence, username); addWordToSentence(&stWriteSentence, "=response=00"); addPartWordToSentence(&stWriteSentence, szMD5PasswordToSend); free(szMD5ChallengeBinary); free(szMD5PasswordToSend); DEBUG ? printSentence(&stWriteSentence) : 0; writeSentence(fdSock, &stWriteSentence); stReadSentence = readSentence(fdSock); DEBUG ? printSentence(&stReadSentence) : 0; if (stReadSentence.iReturnValue == DONE) { return 1; } else { return 0; } }