revamped KeyRing functions: stricter checks
This commit is contained in:
@@ -41,48 +41,43 @@ int ReceiveBufferTailPeek;
|
|||||||
|
|
||||||
#define KEYRINGSIZE 16
|
#define KEYRINGSIZE 16
|
||||||
unsigned char KeyRing[KEYRINGSIZE];
|
unsigned char KeyRing[KEYRINGSIZE];
|
||||||
int KeyHead;
|
int KeyHead = 0;
|
||||||
int KeyTail;
|
int KeyTail = 0;
|
||||||
|
|
||||||
/*-OK-----------------------------------------------------------------------*/
|
/*-OK-----------------------------------------------------------------------*/
|
||||||
void EmptyKeyRing(void)
|
void EmptyKeyRing(void)
|
||||||
{
|
{
|
||||||
KeyHead=KeyTail=0;
|
KeyHead = KeyTail = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-OK-----------------------------------------------------------------------*/
|
/*-OK-----------------------------------------------------------------------*/
|
||||||
int AddKeyToKeyRing(unsigned char key) {
|
int AddKeyToKeyRing(unsigned char key)
|
||||||
int oldhead;
|
{
|
||||||
|
if (((KeyHead + 1) % KEYRINGSIZE) != (KeyTail % KEYRINGSIZE)) {
|
||||||
|
/* printf("We add key: %d\n", key); */
|
||||||
|
|
||||||
/* printf("We add key: %d\n", key); */
|
KeyRing[KeyHead % KEYRINGSIZE] = key;
|
||||||
|
KeyHead = (KeyHead + 1) % KEYRINGSIZE;
|
||||||
oldhead=KeyHead;
|
|
||||||
KeyHead++;
|
|
||||||
if (KeyHead==KEYRINGSIZE) KeyHead=0;
|
|
||||||
if (KeyHead!=KeyTail) {
|
|
||||||
KeyRing[oldhead]=key;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/* We have a KeyRing overflow */
|
/* KeyRing overflow: do not accept extra key */
|
||||||
/* We do not accept extra key */
|
|
||||||
KeyHead=oldhead;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-OK-----------------------------------------------------------------------*/
|
/*-OK-----------------------------------------------------------------------*/
|
||||||
unsigned char GetKeyFromKeyRing(void) {
|
unsigned char GetKeyFromKeyRing(void)
|
||||||
int retval=0;
|
{
|
||||||
|
int retval = 0;
|
||||||
|
|
||||||
if (KeyHead!=KeyTail) {
|
if ((KeyHead % KEYRINGSIZE) != (KeyTail % KEYRINGSIZE )) {
|
||||||
retval=KeyRing[KeyTail];
|
retval = KeyRing[KeyTail % KEYRINGSIZE];
|
||||||
KeyTail++;
|
KeyTail = (KeyTail + 1) % KEYRINGSIZE;
|
||||||
if (KeyTail==KEYRINGSIZE) KeyTail=0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if (retval) printf("We remove key: %d\n", retval); */
|
/* if (retval) printf("We remove key: %d\n", retval); */
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user