BITS * bits_load (const char *buffer) { int block_nbr; /* Bitstring block number */ word comp_size; /* Size of compressed block */ BITBLOCK *block_ptr; /* Points to bitstring block */ BITS *bits; byte *buf, *position; /* Current position in the stream */ buf = (byte *) mem_strdup (buffer); if (buf == NULL) return (NULL); bits = bits_create (); /* Create a new, empty bitmap */ decode_base64 (buffer, buf, strlen ((char *) buf) + 1); position = (byte *) buf; /* Read bitstring header from buffer */ bits-> block_count = *((int *) position); position += sizeof (bits-> block_count); bits-> free_list = *((dbyte *) position); position += sizeof (bits-> free_list); /* Read bitstring blocks from file */ for (block_nbr = 0; block_nbr < bits-> block_count; block_nbr++) { block_nbr = alloc_block (bits, block_nbr); if (block_nbr < 0) { bits_destroy (bits); return (NULL); } block_ptr = bits-> block [block_nbr]; block_ptr-> right = *((dbyte *)position); position += sizeof (block_ptr-> right); block_ptr-> size = *((int *)position); position += sizeof (block_ptr-> size); comp_size = *((word *)position); position += sizeof (word); memcpy (compressed, position, comp_size); position += comp_size; expand_block (compressed, block_ptr-> block.data, comp_size); } mem_free (buf); return (bits); }
int main(int argc, char *argv[]) { long id; char name[NAME_LEN]; char rasdial[RAS_LEN]; memset(name,'\0',NAME_LEN); memset(rasdial,'\0',RAS_LEN); Bits bits; int ret = bits_init(&bits,LEN); if(-1 == ret){ fprintf(stderr,"bits_init error %s %d\n",__FILE__,__LINE__); } FILE *save; save = fopen("random_save.txt","a+"); if(NULL == save){ fprintf(stderr,"open random_save.text failed\n"); exit(1); } long times=0; long count=0; srand((int)time(0)); do{ id = rand(); id = ((id<<4) + rand()) % LEN; if(!bits_get(&bits,id)) { bits_set(&bits,id); } else { printf("Repeat times %ld\n",++count); continue; } if (LEN-1 == times){ printf("Have try all size=%ld\n",times); goto EXIT; } sprintf(name,"TYT010%03ld",id); sprintf(rasdial,"rasdial pppoe %s 8888\n",name); printf("times=%ld %s",++times,rasdial); ret = system(rasdial); }while(0 != ret); EXIT: bits_destroy(&bits); fprintf(save,"times=%ld %s",times,rasdial); printf("Final Rpeat times %ld\n",count); return 0; }
BITS * bits_fget (FILE *file) { int block_nbr; /* Bitstring block number */ word comp_size = 0; /* Size of compressed block */ BITBLOCK *block_ptr; /* Points to bitstring block */ BITS *bits; ASSERT (file); bits = bits_create (); /* Create a new, empty bitmap */ /* Read bitstring header from file */ ASSERT (fread (&bits-> block_count, sizeof (bits-> block_count), 1, file) == 1); ASSERT (fread (&bits-> free_list, sizeof (bits-> free_list), 1, file) == 1); /* Read bitstring blocks from file */ for (block_nbr = 0; block_nbr < bits-> block_count; block_nbr++) { block_nbr = alloc_block (bits, block_nbr); if (block_nbr < 0) { bits_destroy (bits); return (NULL); } block_ptr = bits-> block [block_nbr]; ASSERT (fread (&block_ptr-> right, sizeof (block_ptr-> right), 1, file) == 1); ASSERT (fread (&block_ptr-> size, sizeof (block_ptr-> size), 1, file) == 1); ASSERT (fread (&comp_size, sizeof (comp_size), 1, file) == 1); ASSERT (fread (compressed, comp_size, 1, file) == 1); expand_block (compressed, block_ptr-> block.data, comp_size); } return (bits); }