MCPcopy Create free account
hub / github.com/diasurgical/devilution / WriteCmpData

Function WriteCmpData

3rdParty/PKWare/implode.cpp:503–683  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

501}
502
503static void PKWAREAPI WriteCmpData(TCmpStruct * pWork)
504{
505 unsigned char * input_data_end; // Pointer to the end of the input data
506 unsigned char * input_data = pWork->work_buff + pWork->dsize_bytes + 0x204;
507 unsigned int input_data_ended = 0; // If 1, then all data from the input stream have been already loaded
508 unsigned int save_rep_length; // Saved length of current repetition
509 unsigned int save_distance = 0; // Saved distance of current repetition
510 unsigned int rep_length; // Length of the found repetition
511 unsigned int phase = 0; //
512
513 // Store the compression type and dictionary size
514 pWork->out_buff[0] = (char)pWork->ctype;
515 pWork->out_buff[1] = (char)pWork->dsize_bits;
516 pWork->out_bytes = 2;
517
518 // Reset output buffer to zero
519 memset(&pWork->out_buff[2], 0, sizeof(pWork->out_buff) - 2);
520 pWork->out_bits = 0;
521
522 while(input_data_ended == 0)
523 {
524 unsigned int bytes_to_load = 0x1000;
525 int total_loaded = 0;
526 int bytes_loaded;
527
528 // Load the bytes from the input stream, up to 0x1000 bytes
529 while(bytes_to_load != 0)
530 {
531 bytes_loaded = pWork->read_buf((char *)pWork->work_buff + pWork->dsize_bytes + 0x204 + total_loaded,
532 &bytes_to_load,
533 pWork->param);
534 if(bytes_loaded == 0)
535 {
536 if(total_loaded == 0 && phase == 0)
537 goto __Exit;
538 input_data_ended = 1;
539 break;
540 }
541 else
542 {
543 bytes_to_load -= bytes_loaded;
544 total_loaded += bytes_loaded;
545 }
546 }
547
548 input_data_end = pWork->work_buff + pWork->dsize_bytes + total_loaded;
549 if(input_data_ended)
550 input_data_end += 0x204;
551
552 //
553 // Warning: The end of the buffer passed to "SortBuffer" is actually 2 bytes beyond
554 // valid data. It is questionable if this is actually a bug or not,
555 // but it might cause the compressed data output to be dependent on random bytes
556 // that are in the buffer.
557 // To prevent that, the calling application must always zero the compression
558 // buffer before passing it to "implode"
559 //
560

Callers 1

implodeFunction · 0.85

Calls 3

SortBufferFunction · 0.85
FindRepFunction · 0.85
OutputBitsFunction · 0.85

Tested by

no test coverage detected