5 changed files with 155 additions and 27 deletions
-
1Makefile
-
79src/imei.c
-
30src/imei.h
-
70src/wstationd.c
-
2src/wstationd.h
@ -0,0 +1,79 @@ |
|||
#include <stdlib.h> |
|||
#include <inttypes.h> |
|||
#include "imei.h" |
|||
|
|||
|
|||
imei_set* imei_set_new(size_t capacity) |
|||
{ |
|||
imei_set* set = (imei_set*)malloc( |
|||
sizeof(imei_set) + (capacity * sizeof(uint64_t)) |
|||
); |
|||
if(set == NULL) return NULL; |
|||
|
|||
set->sz = 0; |
|||
set->cp = capacity; |
|||
|
|||
return set; |
|||
} |
|||
|
|||
|
|||
// This is not efficient at all. It could be made vastly |
|||
// more efficient by having imei_set_add() insert in order |
|||
// and then implementing a binary search algorithm here |
|||
int imei_set_search(imei_set* set, uint64_t imei) |
|||
{ |
|||
for(size_t i = 0; i < set->sz; i++){ |
|||
if(set->imeis[i] == imei) return 1; |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
|
|||
void imei_set_free(imei_set *set){ |
|||
if(set != NULL) free(set); |
|||
} |
|||
|
|||
|
|||
int imei_set_add(imei_set** set, uint64_t imei) |
|||
{ |
|||
// Don't allow out-of-bounds imeis |
|||
if(imei == UINT64_MAX) return 1; |
|||
|
|||
// Don't allow duplicate entries |
|||
if(imei_set_search(*set, imei)) return 2; |
|||
|
|||
// If the set is full, expand it |
|||
if((*set)->sz == (*set)->cp){ |
|||
imei_set* nset = realloc( |
|||
*set, sizeof(imei_set) + (((*set)->cp + 1000) * sizeof(uint64_t)) |
|||
); |
|||
if(nset == NULL) return 1; |
|||
nset->cp += 1000; |
|||
(*set) = nset; |
|||
} |
|||
|
|||
(*set)->imeis[(*set)->sz] = imei; |
|||
(*set)->sz++; |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
|
|||
uint64_t imei_uint64(unsigned char *imei, size_t sz) |
|||
{ |
|||
// IMEI's are always 15 bytes in length |
|||
if(sz != 15) return UINT64_MAX; |
|||
|
|||
uint64_t r = 0; |
|||
for(int i = 0; i < 15; i++){ |
|||
// If any byte in the IMEI isn't a proper digit, |
|||
// return an error - UINT64_MAX is used because |
|||
// an imei will only use 60 bits, so the |
|||
// max for a 64-bit unsigned int is out of bounds. |
|||
if(imei[i] < '0' || imei[i] > '9') return UINT64_MAX; |
|||
|
|||
r = (r<<4) + (imei[i] - '0'); |
|||
} |
|||
|
|||
return r; |
|||
} |
@ -0,0 +1,30 @@ |
|||
#ifndef IMEI_SET_H |
|||
#define IMEI_SET_H |
|||
|
|||
typedef struct |
|||
{ |
|||
size_t sz; |
|||
size_t cp; |
|||
uint64_t imeis[]; |
|||
} imei_set; |
|||
|
|||
|
|||
// Convert a string representation of an IMEI |
|||
// to a uint64_t |
|||
uint64_t imei_uint64(unsigned char *, size_t); |
|||
|
|||
// Create a new IMEI set n size |
|||
imei_set* imei_set_new(size_t); |
|||
|
|||
// Free existing imei set |
|||
void imei_set_free(imei_set *); |
|||
|
|||
// Search an existing set for an IMEI, |
|||
// returns 1 if found, or 0 if not found |
|||
int imei_set_search(imei_set*, uint64_t); |
|||
|
|||
// Adds IMEI to existing set, |
|||
// returns 0 on success, non-zero on failure |
|||
int imei_set_add(imei_set**, uint64_t); |
|||
|
|||
#endif // IMEI_SET_H |
@ -1 +1 @@ |
|||
#define VERSION "1.1" |
|||
#define VERSION "1.5" |
Write
Preview
Loading…
Cancel
Save
Reference in new issue