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); }
void build_white_opening_book() { struct tree_type *root; short bb[64],x,value; unsigned char blk[14],tp[64],t[64]; long size,posit=0,wps=0,bps=0; FILE *h1,*h2; root=init_obook_tree(); if(!root) fatal_error("Not enough memory, darn!"); h1=fopen(DATABASEFILE,"rb"); if(!h1) fatal_error("Data Base error!"); size=fileln(h1); if(size%14!=0) fatal_error("Opening book file is corrupted"); printf("Loading and sorting opening book...\n"); while(size>0) { fread(blk,1,12,h1); expand_block(blk,t); for(x=0;x<64;x++) bb[x]=t[x]; get_lower(bb,tp); collapse_position(tp,blk); blk[12]=getc(h1); blk[13]=getc(h1); if(posit>0) insert_obook_tree(blk,root); else memcpy(root->position,blk,14); if(blk[13]==0) wps++; else bps++; posit++; size-=14; } fclose(h1); printf("Flushing out ordered opening book...\n"); h2=fopen(WHITE_BOOK,"wb"); if(!h2) fatal_error("Cannot write output file!"); flush_out_obtree(root,h2); free_obook_tree(root); fclose(h2); printf("Positions for white %ld; positions for black %ld\n\n",wps,bps); }
local expand_file_lz (FILE *input, FILE *output) { word in_size, out_size; while ((fread (&in_size, 1, sizeof (in_size), input)) > 0) { ASSERT (fread (in_block, 1, in_size, input)); out_size = expand_block (in_block, out_block, in_size); ASSERT (fwrite (out_block, 1, out_size, output)); printf ("lz expand: in=%d out=%d\n", in_size, out_size); } }
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); }
char * fortune_read (const char *fortune_file) { static Bool first_time = TRUE; FILE *fortunes; /* FFF input stream */ byte *inbuf, /* Block read from file */ *outbuf; /* And after decompression */ int nbr_blocks, /* Number of blocks in data file */ nbr_paras, /* Number of paragraphs in block */ paragraph; dbyte input_dbyte = 0, block_size; /* Size of current block */ qbyte input_qbyte = 0, block_offset; /* Offset of block in file */ char *para_start; /* Start of paragraph */ Bool compressed; /* Compressed fortune data? */ /* Initialise random-number generator if necessary */ if (first_time) { randomize (); first_time = FALSE; } /* Look for fortunes file */ fortunes = file_locate ("PATH", fortune_file, NULL); if (fortunes == NULL) return (NULL); /* Allocate working buffers */ inbuf = mem_alloc (BLOCK_SIZE); outbuf = mem_alloc (BLOCK_SIZE); if (inbuf == NULL || outbuf == NULL) { mem_free (inbuf); mem_free (outbuf); return (NULL); } /* Indexed Fortune File format starts with IFFCMP or IFFTXT */ file_read (fortunes, (char *) inbuf); if (memcpy (inbuf, "IFFTXT", 6) == 0) compressed = FALSE; else compressed = TRUE; while (fgetc (fortunes) != 26); /* Skip past file header */ /* Get total number of blocks in file */ ASSERT (fread (&input_dbyte, 2, 1, fortunes)); nbr_blocks = ntohs (input_dbyte); /* Look at random block address in Toc */ fseek (fortunes, random (nbr_blocks) * 4, SEEK_CUR); ASSERT (fread (&input_qbyte, 4, 1, fortunes)); block_offset = ntohl (input_qbyte); /* Go read, then decompress the block */ fseek (fortunes, block_offset, SEEK_SET); ASSERT (fread (&input_dbyte, 2, 1, fortunes)); block_size = ntohs (input_dbyte); if (compressed) { ASSERT (fread (inbuf, 1, block_size, fortunes)); expand_block (inbuf, outbuf, block_size); } else ASSERT (fread (outbuf, 1, block_size, fortunes)); /* Chose random paragraph from block */ input_dbyte = *(dbyte *) outbuf; nbr_paras = ntohs (input_dbyte); paragraph = random (nbr_paras); para_start = (char *) outbuf + 2; while (paragraph--) para_start = strchr (para_start, '\0') + 1; para_start = mem_strdup (para_start); /* Release allocated memory */ fclose (fortunes); mem_free (inbuf); mem_free (outbuf); return (para_start); }