Skip to content

Instantly share code, notes, and snippets.

@kai3341
Last active August 29, 2015 14:25
Show Gist options
  • Save kai3341/487ef14942be44c454bb to your computer and use it in GitHub Desktop.
Save kai3341/487ef14942be44c454bb to your computer and use it in GitHub Desktop.
void* read_to_queue(Queue* queue)
{
Reading=1;
Object* pointer;
Object* rqo = (Object*) malloc(sizeof(Object)); //ReadQueueObj
rqo->item = (char*) malloc ((MAXPASSLEN + 1) * sizeof(char));
while (sig == 0 && Reading)
{
if (fgets(rqo->item, MAXPASSLEN + 1, fpin) == NULL)
{
if(feof(fpin) != 0)
{
Reading = 0;
free(rqo->item);
free(rqo);
break;
}
}
else
{
// Remove newline
((char*) rqo->item)[strlen((char*) rqo->item) - 1] = '\0';
rqo->size=strlen(rqo->item);
if (rqo->size < 8 || rqo->size > 63)
{
if (opt.verbose)
{
printf("Invalid passphrase length: %s (%d).\n",
(char*) rqo->item, (int) rqo->size);
}
continue;
}
else
{
printf("rqo->size = %02zu, rqo->item = \"%s\"\n", (size_t) rqo->size, (char*) rqo->item);
while (sig == 0)
{
if ((pointer = QueuePutWithExchange(queue, rqo)) == NULL)
{
//Queue full
return; //FIXME
pthread_mutex_trylock(&mx_read);
pthread_mutex_lock(&mx_read); //Self lock
}
else
{
rqo = pointer;
break;
}
}
}
}
}
return(0);
}
Object* ObjectGetWithExchange(Queue* queue, Object* obj)
{
//printf("ObjectGetWithExchange\n");
//pthread_mutex_unlock(&mx_read);
unsigned long int thswt; //ThreadSafe Words Tested
Object* pointer;
while(sig == 0)
{
if((pointer = QueueGetWithExchange(queue, obj)) == NULL)
{
//Queue Empty!
if(Reading)
{
printf("Warning! Too slow I/O\n");
pthread_mutex_unlock(&mx_read);
sleep(1);
continue;
}
else
{
return(NULL);
}
}
else
{
if (opt.verbose > 1)
{
printf("Testing passphrase: %s\n", (char*) pointer->item);
}
/*
* Test length of word. IEEE 802.11i indicates the passphrase must be
* at least 8 characters in length, and no more than 63 characters in
* length.
*/
pthread_mutex_lock(&mx_wordstested);
thswt = ++wordstested;
pthread_mutex_unlock(&mx_wordstested);
//printf("@ObjectGetWithExchange: pointer #%zu: \"%s\" (%zu)\n", (size_t) thswt, (char*) pointer->item, (size_t) pointer->size);
/* Status display */
if ((thswt % 1000) == 0)
{
printf("key no. %ld: %s\n", thswt, (char*) pointer->item);
fflush(stdout);
IOWait++;
if(thswt % IOWakeUp == 0)
{
pthread_mutex_unlock(&mx_read);
}
}
return(pointer);
}
}
return(0); //gcc warning, unreacheble
}
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "queue.h"
Queue* QueueInit(int size)
{
Queue* queue = (Queue*) malloc(sizeof(Queue));
queue->offset = queue->count = 0;
queue->size = size;
pthread_mutex_init(&(queue->mx_access), NULL);
queue->objects = (void**) malloc(size * sizeof(void*));
return (queue);
}
int QueuePut(Queue* queue, void* obj)
{
pthread_mutex_lock(&(queue->mx_access));
if (queue->size == queue->count)
{
pthread_mutex_unlock(&(queue->mx_access));
return (0); // Queue Full
}
else
{
queue->objects[queue->offset] = obj;
queue->offset = (queue->offset + 1) % queue->size;
queue->count++;
pthread_mutex_unlock(&(queue->mx_access));
return(1); // No errors
}
}
void* QueuePutWithExchange(Queue* queue, void* obj)
{
pthread_mutex_lock(&(queue->mx_access));
if (queue->size == queue->count)
{
pthread_mutex_unlock(&(queue->mx_access));
return (NULL); // Queue Full
}
else
{
void* ret = queue->objects[queue->offset];
queue->objects[queue->offset] = obj;
queue->offset = (queue->offset + 1) % queue->size;
queue->count++;
pthread_mutex_unlock(&(queue->mx_access));
return(ret); // No errors
}
}
void* QueueGet(Queue* queue)
{
pthread_mutex_lock(&(queue->mx_access));
if(queue->count == 0)
{
pthread_mutex_unlock(&(queue->mx_access));
return (NULL); /* Queue Empty - nothing to get*/
}
else
{
void* obj = queue->objects[queue->offset];
queue->offset++;
queue->count--;
pthread_mutex_unlock(&(queue->mx_access));
return (obj); // No errors
}
}
void* QueueGetWithExchange(Queue* queue, void* obj)
{
pthread_mutex_lock(&(queue->mx_access));
if(queue->count == 0)
{
pthread_mutex_unlock(&(queue->mx_access));
return (NULL); /* Queue Empty - nothing to get*/
}
else
{
void* ret = queue->objects[queue->offset];
queue->objects[queue->offset] = obj;
queue->count--;
queue->offset++;
pthread_mutex_unlock(&(queue->mx_access));
return (ret); // No errors
}
}
void QueueFree(Queue* queue)
{
free(queue->objects);
free(queue);
pthread_mutex_destroy(&(queue->mx_access));
}
#include <pthread.h>
typedef struct Queue
{
pthread_mutex_t mx_access;
int offset, size, count;
void** objects;
} Queue;
Queue* QueueInit(int size);
int QueuePut(Queue* queue, void* obj);
void* QueuePutWithExchange(Queue* queue, void* obj);
void* QueueGet(Queue* queue);
void* QueueGetWithExchange(Queue* queue, void* obj);
void QueueFree(Queue* queue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment