|
|
View previous topic :: View next topic |
Author |
Message |
Indira Learner
Joined: 08 Sep 2017 Posts: 3
|
Comparing two strings |
Posted: Fri Sep 08, 2017 8:16 am |
|
|
I need to get a string from the user and then compare it with another one, already found in the pic. I wrote a code for it, however, i want to change it so the code works immediately i press the send button and not wait until the enter key is pressed. Here is what I have done already:
Code: |
#include <main.h>
#include <stdio.h>
#include <string.h>
#fuses HS
#use rs232(uart1, baud=9600)
char password[] = {"aaa"};
char input_str[];
char string_pass[];
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_EXT
void EXT_isr(void)
{
reset_cpu();
}
#int_RDA
void RDA_isr(void)
{
int t;
buffer[next_in] = getc();
t = next_in;
next_in =(next_in + 1) % BUFFER_SIZE;
if(next_in == next_out)
next_in = t;
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() //Gets characters
{
BYTE c;
while(!bkbhit);
c = buffer[next_out];
next_out =(next_out + 1) % BUFFER_SIZE;
return(c);
}
void get_string(char* s) //Stores the characters into a string
{
unsigned int8 length;
char c;
length = 0;
do
{
c = bgetc();
if((c >= ' ')&&(c <= '~'))
{
s[length++] = c;
putc(c);
}
}
while(c != 13);// Check for enter key. How can I modify this part?
s[length] = 0;
}
void toggle_function() //Toggle LED state if keywords match
{
int i = 0;
while(i < 20)
{
output_high(led1);
delay_ms(150);
i++;
}
}
void compare_function(char *string) //Checks whether both keywords match
{
if(strcmp(string,password) == 0)//Compares both strings
{
printf("\n\nCorrect Keyword ! ");
toggle_function();
break;
}
else
{
printf("\n\nERROR\n");
output_low(led2);
break;
}
}
void main()
{
output_D(0x00);
clear_interrupt(int_EXT);
enable_interrupts(int_EXT);
clear_interrupt(int_RDA);
enable_interrupts(int_RDA);
enable_interrupts(GLOBAL);
while(TRUE){
printf("Type string here: ");
get_string(input_str); //Calls the get_string function
strcpy(string_pass,input_str); //Gets string pointer used int the compare function
compare_function(string_pass); //Calls the compare functionhile(TRUE)
}
}
|
|
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Fri Sep 08, 2017 8:36 am |
|
|
In the grand scheme of things, changing a password test from "upon enter" to "on the fly" is a security risk. That said, to do that sort of thing will require you to examine each character individually. A finite state machine (FSM) implementation would work very well for this.
For example:
Code: | #define INITIAL_STATE_LOCKED 0
#define PASSWORD_BEING_SENT_AND_ALL_GOOD 1
#define PASSWORD_BEING_SENT_AND_ITS_WRONG 2
#define UNLOCKED 3
unsigned int8 lock_state_FSM = INITIAL_STATE_LOCKED;
unsigned int8 num_received_characters = 0;
int1 lock_state_changed_flag = FALSE;
int1 lock_is_locked_flag = TRUE; |
In your RDA interrupt you check the received character against:
Code: | password[num_received_characters++] |
Match, and advance the lock_state_FSM to PASSWORD_BEING_SENT_AND_ALL_GOOD; mismatch and set it to PASSWORD_BEING_SENT_AND_ITS_WRONG. If you match all characters in the password, it's now UNLOCKED, and use the other flags to a) change the lock state, and b) alert the user that the lock has now changed state. |
|
|
Indira Learner
Joined: 08 Sep 2017 Posts: 3
|
|
Posted: Sat Sep 09, 2017 3:43 am |
|
|
newguy wrote: |
In the grand scheme of things, changing a password test from "upon enter" to "on the fly" is a security risk. That said, to do that sort of thing will require you to examine each character individually. A finite state machine (FSM) implementation would work very well for this.
|
Sorry, i'm still a newbie in programming so i'm not really sure what to do. So i don't know how to implement a final state machine in c. Do you have an example i could use? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sat Sep 09, 2017 5:34 am |
|
|
You can make your code a lot simpler and smaller by NOT using 'strings'.
consider...
char password[] = {"abcdef"};
which is a 6 byte array.
Now in you 'valid_password_decoder' function simply
compare the 1st incoming character to the contents password[0]
ELSE 'abort'
IF equal THEN
compare the 2nd incoming character to the contents password[1)
ELSE 'abort'
If equal THEN
compare 3rd,4th,5th,6th.
ELSE 'abort'
IF any compare fails,t hen exit the 'get-password_and test' function, IE abort.
Now you will require a 'sync' byte so the PIC knows where the password begins.
sorry , no code, there are examples in the code library here and it's always best to 'learn while doing'. Start with one test, then build upon that. code will have 1 IF..THEN per password character.
Jay |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|