static int badUsage(const char* exename) { (void) exename; DISPLAY("wrong parameter\n"); return 1; }
static gboolean crtc_initialize (MateRRCrtc *crtc, XRRScreenResources *res, GError **error) { XRRCrtcInfo *info = XRRGetCrtcInfo (DISPLAY (crtc), res, crtc->id); GPtrArray *a; int i; #if 0 g_print ("CRTC %lx Timestamp: %u\n", crtc->id, (guint32)info->timestamp); #endif if (!info) { /* FIXME: We need to reaquire the screen resources */ /* FIXME: can we actually catch BadRRCrtc, and does it make sense to emit that? */ /* Translators: CRTC is a CRT Controller (this is X terminology). * It is *very* unlikely that you'll ever get this error, so it is * only listed for completeness. */ g_set_error (error, MATE_RR_ERROR, MATE_RR_ERROR_RANDR_ERROR, _("could not get information about CRTC %d"), (int) crtc->id); return FALSE; } /* MateRRMode */ crtc->current_mode = mode_by_id (crtc->info, info->mode); crtc->x = info->x; crtc->y = info->y; /* Current outputs */ a = g_ptr_array_new (); for (i = 0; i < info->noutput; ++i) { MateRROutput *output = mate_rr_output_by_id (crtc->info, info->outputs[i]); if (output) g_ptr_array_add (a, output); } g_ptr_array_add (a, NULL); crtc->current_outputs = (MateRROutput **)g_ptr_array_free (a, FALSE); /* Possible outputs */ a = g_ptr_array_new (); for (i = 0; i < info->npossible; ++i) { MateRROutput *output = mate_rr_output_by_id (crtc->info, info->possible[i]); if (output) g_ptr_array_add (a, output); } g_ptr_array_add (a, NULL); crtc->possible_outputs = (MateRROutput **)g_ptr_array_free (a, FALSE); /* Rotations */ crtc->current_rotation = mate_rr_rotation_from_xrotation (info->rotation); crtc->rotations = mate_rr_rotation_from_xrotation (info->rotations); XRRFreeCrtcInfo (info); /* get an store gamma size */ crtc->gamma_size = XRRGetCrtcGammaSize (DISPLAY (crtc), crtc->id); return TRUE; }
int main(int argc, char** argv) { int i, bench=0, decode=0, forceStdout=0, main_pause=0; unsigned fileNameStart = 0; unsigned nbFiles = 0; const char* programName = argv[0]; const char* inFileName = NULL; const char* outFileName = NULL; char* dynNameSpace = NULL; char extension[] = ZSTD_EXTENSION; displayOut = stderr; /* Pick out basename component. Don't rely on stdlib because of conflicting behavior. */ for (i = (int)strlen(programName); i > 0; i--) { if (programName[i] == '/') { i++; break; } } programName += i; /* zstdcat preset behavior */ if (!strcmp(programName, ZSTD_CAT)) { decode=1; forceStdout=1; displayLevel=1; outFileName=stdoutmark; } /* unzstd preset behavior */ if (!strcmp(programName, ZSTD_UNZSTD)) decode=1; /* command switches */ for(i=1; i<argc; i++) { char* argument = argv[i]; if(!argument) continue; /* Protection if argument empty */ /* long commands (--long-word) */ if (!strcmp(argument, "--version")) { displayOut=stdout; DISPLAY(WELCOME_MESSAGE); return 0; } if (!strcmp(argument, "--help")) { displayOut=stdout; return usage_advanced(programName); } if (!strcmp(argument, "--verbose")) { displayLevel=4; continue; } /* Decode commands (note : aggregated commands are allowed) */ if (argument[0]=='-') { /* '-' means stdin/stdout */ if (argument[1]==0) { if (!inFileName) inFileName=stdinmark; else outFileName=stdoutmark; continue; } argument++; while (argument[0]!=0) { switch(argument[0]) { /* Display help */ case 'V': displayOut=stdout; DISPLAY(WELCOME_MESSAGE); return 0; /* Version Only */ case 'H': case 'h': displayOut=stdout; return usage_advanced(programName); /* Compression (default) */ //case 'z': forceCompress = 1; break; /* Decoding */ case 'd': decode=1; argument++; break; /* Force stdout, even if stdout==console */ case 'c': forceStdout=1; outFileName=stdoutmark; displayLevel=1; argument++; break; // Test //case 't': decode=1; LZ4IO_setOverwrite(1); output_filename=nulmark; break; /* Overwrite */ case 'f': FIO_overwriteMode(); argument++; break; /* Verbose mode */ case 'v': displayLevel=4; argument++; break; /* Quiet mode */ case 'q': displayLevel--; argument++; break; /* keep source file (default anyway, so useless; only for xz/lzma compatibility) */ case 'k': argument++; break; /* Benchmark */ case 'b': bench=1; argument++; break; /* Modify Nb Iterations (benchmark only) */ case 'i': { int iters= 0; argument++; while ((*argument >='0') && (*argument <='9')) iters *= 10, iters += *argument++ - '0'; BMK_SetNbIterations(iters); } break; /* cut input into blocks (benchmark only) */ case 'B': { size_t bSize = 0; argument++; while ((*argument >='0') && (*argument <='9')) bSize *= 10, bSize += *argument++ - '0'; if (*argument=='K') bSize<<=10, argument++; /* allows using KB notation */ if (*argument=='M') bSize<<=20, argument++; if (*argument=='B') argument++; BMK_SetBlockSize(bSize); } break; /* Pause at the end (hidden option) */ case 'p': main_pause=1; argument++; break; /* unknown command */ default : return badusage(programName); } } continue; } /* first provided filename is input */ if (!inFileName) { inFileName = argument; fileNameStart = i; nbFiles = argc-i; continue; } /* second provided filename is output */ if (!outFileName) { outFileName = argument; if (!strcmp (outFileName, nullString)) outFileName = nulmark; continue; } } /* Welcome message (if verbose) */ DISPLAYLEVEL(3, WELCOME_MESSAGE); /* No input filename ==> use stdin */ if(!inFileName) { inFileName=stdinmark; } /* Check if input defined as console; trigger an error in this case */ if (!strcmp(inFileName, stdinmark) && IS_CONSOLE(stdin) ) return badusage(programName); /* Check if benchmark is selected */ if (bench) { BMK_benchFiles(argv+fileNameStart, nbFiles, 0); goto _end; } /* No output filename ==> try to select one automatically (when possible) */ while (!outFileName) { if (!IS_CONSOLE(stdout)) { outFileName=stdoutmark; break; } /* Default to stdout whenever possible (i.e. not a console) */ if (!decode) /* compression to file */ { size_t l = strlen(inFileName); dynNameSpace = (char*)calloc(1,l+5); if (dynNameSpace==NULL) { DISPLAY("not enough memory\n"); exit(1); } strcpy(dynNameSpace, inFileName); strcpy(dynNameSpace+l, ZSTD_EXTENSION); outFileName = dynNameSpace; DISPLAYLEVEL(2, "Compressed filename will be : %s \n", outFileName); break; } /* decompression to file (automatic name will work only if input filename has correct format extension) */ { size_t outl; size_t inl = strlen(inFileName); dynNameSpace = (char*)calloc(1,inl+1); if (dynNameSpace==NULL) { DISPLAY("not enough memory\n"); exit(1); } outFileName = dynNameSpace; strcpy(dynNameSpace, inFileName); outl = inl; if (inl>4) while ((outl >= inl-4) && (inFileName[outl] == extension[outl-inl+4])) dynNameSpace[outl--]=0; if (outl != inl-5) { DISPLAYLEVEL(1, "Cannot determine an output filename\n"); return badusage(programName); } DISPLAYLEVEL(2, "Decoding file %s \n", outFileName); } } /* Check if output is defined as console; trigger an error in this case */ if (!strcmp(outFileName,stdoutmark) && IS_CONSOLE(stdout) && !forceStdout) return badusage(programName); /* No warning message in pure pipe mode (stdin + stdout) */ if (!strcmp(inFileName, stdinmark) && !strcmp(outFileName,stdoutmark) && (displayLevel==2)) displayLevel=1; /* IO Stream/File */ FIO_setNotificationLevel(displayLevel); if (decode) FIO_decompressFilename(outFileName, inFileName); else FIO_compressFilename(outFileName, inFileName); _end: if (main_pause) waitEnter(); free(dynNameSpace); return 0; }
static void waitEnter(void) { DISPLAY("Press enter to continue...\n"); getchar(); }
static gboolean output_initialize (MateRROutput *output, XRRScreenResources *res, GError **error) { XRROutputInfo *info = XRRGetOutputInfo ( DISPLAY (output), res, output->id); GPtrArray *a; int i; #if 0 g_print ("Output %lx Timestamp: %u\n", output->id, (guint32)info->timestamp); #endif if (!info || !output->info) { /* FIXME: see the comment in crtc_initialize() */ /* Translators: here, an "output" is a video output */ g_set_error (error, MATE_RR_ERROR, MATE_RR_ERROR_RANDR_ERROR, _("could not get information about output %d"), (int) output->id); return FALSE; } output->name = g_strdup (info->name); /* FIXME: what is nameLen used for? */ output->current_crtc = crtc_by_id (output->info, info->crtc); output->width_mm = info->mm_width; output->height_mm = info->mm_height; output->connected = (info->connection == RR_Connected); output->connector_type = get_connector_type_string (output); /* Possible crtcs */ a = g_ptr_array_new (); for (i = 0; i < info->ncrtc; ++i) { MateRRCrtc *crtc = crtc_by_id (output->info, info->crtcs[i]); if (crtc) g_ptr_array_add (a, crtc); } g_ptr_array_add (a, NULL); output->possible_crtcs = (MateRRCrtc **)g_ptr_array_free (a, FALSE); /* Clones */ a = g_ptr_array_new (); for (i = 0; i < info->nclone; ++i) { MateRROutput *mate_rr_output = mate_rr_output_by_id (output->info, info->clones[i]); if (mate_rr_output) g_ptr_array_add (a, mate_rr_output); } g_ptr_array_add (a, NULL); output->clones = (MateRROutput **)g_ptr_array_free (a, FALSE); /* Modes */ a = g_ptr_array_new (); for (i = 0; i < info->nmode; ++i) { MateRRMode *mode = mode_by_id (output->info, info->modes[i]); if (mode) g_ptr_array_add (a, mode); } g_ptr_array_add (a, NULL); output->modes = (MateRRMode **)g_ptr_array_free (a, FALSE); output->n_preferred = info->npreferred; /* Edid data */ output->edid_data = read_edid_data (output); XRRFreeOutputInfo (info); return TRUE; }
void text_box_x11(textbox *t) { int i; float x,y, sx, sy; int len, xlen, cwidth, cheight, ascent; XWindow *xw; XFontStruct *font; if(!t || t->n == 0) { return; } len = 0; cmgdm.ihjust = 1; cmgdm.ivjust = 1; xw = plot_window( CURRENT ); xwindow_font_load(xw); if(xw->font->type == XFONT_TYPE_SOFTWARE) { font = NULL; /* All relative lengths scaled by window width */ cwidth = view_to_x11_x( cmgdm.twidth, xw ); cheight = view_to_x11_x( cmgdm.thgt, xw ); ascent = view_to_x11_x( cmgdm.thgt * 1.10, xw ); } if(xw->font->type == XFONT_TYPE_CORE) { font = xw->font->font_core; cwidth = font->max_bounds.width; cheight = font->ascent + font->descent; ascent = font->ascent; } #ifdef HAVE_XFT else if(xw->font->type == XFONT_TYPE_XFT) { cwidth = xw->font->font_xft->max_advance_width; cheight = xw->font->font_xft->ascent + xw->font->font_xft->descent; ascent = xw->font->font_xft->ascent; } #endif /* HAVE_XFT */ x = view_to_x11_x(t->x, xw); y = view_to_x11_y(t->y, xw); /* Adjust for Text Length */ if(t->location & TEXT_BOX_RIGHT) { for(i = 0; i < t->n; i++) { xlen = xwindow_text_width(xw, t->text[i]); if(xlen > len) { len = xlen; } } x = x - len; } /* Adjust for Text Height */ if(t->location & TEXT_BOX_LOWER) { y -= cheight * (t->n - 1); } else { y += cheight; } if(t->use_style && t->location & TEXT_BOX_LEFT) { x += 0.5 * cwidth + TEXTBOX_LINE_LENGTH_PIXELS; } /* Show Text in correct color */ for(i = 0; i < t->n; i++) { setcolor3(t->color[i]); XSetForeground(DISPLAY(xw), xw->gc, color3); if(t->text[i]) { xwindow_draw_string(xw, t->text[i], (int)x, (int)y); } sx = x11_to_view_x(x, xw); sy = x11_to_view_y(y, xw); if(t->use_style) { text_box_line(&x11, sx, sy, t->width[i], t->style[i], cwidth / (float)xw->width, ascent / (float)xw->width); } if(t->use_symbol) { text_box_symbol(&x11, sx, sy, t->symbol[i], t->use_style, cwidth / (float)xw->width, ascent / (float)xw->width); } y += cheight; } /* Return Color for Skeleton */ setcolor3( ( color_on() ) ? color_skeleton() : color_foreground_default() ); }
int BMK_benchFile(char** fileNamesTable, int nbFiles, int selection) { int fileIdx=0; struct hashFunctionPrototype hashP; U32 hashResult=0; U64 totals = 0; double totalc = 0.; // Init switch (selection) { #ifdef HASH0 case 0 : hashP.hashFunction = HASH0; break; #endif #ifdef HASH1 case 1 : hashP.hashFunction = HASH1; break; #endif #ifdef HASH2 case 2 : hashP.hashFunction = HASH2; break; #endif default: hashP.hashFunction = DEFAULTHASH; } DISPLAY("Selected fn %d", selection); // Loop for each file while (fileIdx<nbFiles) { FILE* inFile; char* inFileName; U64 inFileSize; size_t benchedSize; size_t readSize; char* buffer; char* alignedBuffer; // Check file existence inFileName = fileNamesTable[fileIdx++]; inFile = fopen( inFileName, "rb" ); if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; } // Memory allocation & restrictions inFileSize = BMK_GetFileSize(inFileName); benchedSize = (size_t) BMK_findMaxMem(inFileSize); if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize; if (benchedSize < inFileSize) { DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20)); } buffer = (char*)malloc((size_t )benchedSize+16); if(!buffer) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; } alignedBuffer = (buffer+15) - (((size_t)(buffer+15)) & 0xF); // align on next 16 bytes boundaries // Fill input buffer DISPLAY("\rLoading %s... \n", inFileName); readSize = fread(alignedBuffer, 1, benchedSize, inFile); fclose(inFile); if(readSize != benchedSize) { DISPLAY("\nError: problem reading file '%s' !! \n", inFileName); free(buffer); return 13; } // Bench XXH32 { int interationNb; double fastestC = 100000000.; DISPLAY("\r%79s\r", ""); // Clean display line for (interationNb = 1; interationNb <= nbIterations; interationNb++) { int nbHashes = 0; int milliTime; DISPLAY("%1i-%-14.14s : %10i ->\r", interationNb, "XXH32", (int)benchedSize); // Hash loop milliTime = BMK_GetMilliStart(); while(BMK_GetMilliStart() == milliTime); milliTime = BMK_GetMilliStart(); while(BMK_GetMilliSpan(milliTime) < TIMELOOP) { int i; for (i=0; i<100; i++) { hashResult = hashP.hashFunction(alignedBuffer, (int)benchedSize, 0); nbHashes++; } } milliTime = BMK_GetMilliSpan(milliTime); if ((double)milliTime < fastestC*nbHashes) fastestC = (double)milliTime/nbHashes; DISPLAY("%1i-%-14.14s : %10i -> %7.1f MB/s\r", interationNb, "XXH32", (int)benchedSize, (double)benchedSize / fastestC / 1000.); } DISPLAY("%-16.16s : %10i -> %7.1f MB/s 0x%08X\n", "XXH32", (int)benchedSize, (double)benchedSize / fastestC / 1000., hashResult); totals += benchedSize; totalc += fastestC; } // Bench Unaligned XXH32 { int interationNb; double fastestC = 100000000.; DISPLAY("\r%79s\r", ""); // Clean display line for (interationNb = 1; (interationNb <= nbIterations) && ((benchedSize>1)); interationNb++) { int nbHashes = 0; int milliTime; DISPLAY("%1i-%-14.14s : %10i ->\r", interationNb, "(unaligned)", (int)benchedSize); // Hash loop milliTime = BMK_GetMilliStart(); while(BMK_GetMilliStart() == milliTime); milliTime = BMK_GetMilliStart(); while(BMK_GetMilliSpan(milliTime) < TIMELOOP) { int i; for (i=0; i<100; i++) { hashResult = hashP.hashFunction(alignedBuffer+1, (int)benchedSize-1, 0); nbHashes++; } } milliTime = BMK_GetMilliSpan(milliTime); if ((double)milliTime < fastestC*nbHashes) fastestC = (double)milliTime/nbHashes; DISPLAY("%1i-%-14.14s : %10i -> %7.1f MB/s\r", interationNb, "XXH32 (unaligned)", (int)(benchedSize-1), (double)(benchedSize-1) / fastestC / 1000.); } DISPLAY("%-16.16s : %10i -> %7.1f MB/s \n", "XXH32 (unaligned)", (int)benchedSize-1, (double)(benchedSize-1) / fastestC / 1000.); } // Bench XXH64 { int interationNb; double fastestC = 100000000.; unsigned long long h64 = 0; DISPLAY("\r%79s\r", ""); // Clean display line for (interationNb = 1; interationNb <= nbIterations; interationNb++) { int nbHashes = 0; int milliTime; DISPLAY("%1i-%-14.14s : %10i ->\r", interationNb, "XXH64", (int)benchedSize); // Hash loop milliTime = BMK_GetMilliStart(); while(BMK_GetMilliStart() == milliTime); milliTime = BMK_GetMilliStart(); while(BMK_GetMilliSpan(milliTime) < TIMELOOP) { int i; for (i=0; i<100; i++) { h64 = XXH64(alignedBuffer, (int)benchedSize, 0); nbHashes++; } } milliTime = BMK_GetMilliSpan(milliTime); if ((double)milliTime < fastestC*nbHashes) fastestC = (double)milliTime/nbHashes; DISPLAY("%1i-%-14.14s : %10i -> %7.1f MB/s\r", interationNb, "XXH64", (int)benchedSize, (double)benchedSize / fastestC / 1000.); } DISPLAY("%-16.16s : %10i -> %7.1f MB/s 0x%08X%08X\n", "XXH64", (int)benchedSize, (double)benchedSize / fastestC / 1000., (U32)(h64>>32), (U32)(h64)); totals += benchedSize; totalc += fastestC; } free(buffer); }
/*-************************************ * Command Line **************************************/ static int usage(const char* programName) { DISPLAY( "Usage :\n"); DISPLAY( " %s [args] [FILE(s)] [-o file]\n", programName); DISPLAY( "\n"); DISPLAY( "FILE : a filename\n"); DISPLAY( " with no FILE, or when FILE is - , read standard input\n"); DISPLAY( "Arguments :\n"); #ifndef ZSTD_NOCOMPRESS DISPLAY( " -# : # compression level (1-%u, default:1) \n", ZSTD_maxCLevel()); #endif #ifndef ZSTD_NODECOMPRESS DISPLAY( " -d : decompression \n"); #endif DISPLAY( " -D file: use `file` as Dictionary \n"); DISPLAY( " -o file: result stored into `file` (only if 1 input file) \n"); DISPLAY( " -f : overwrite output without prompting \n"); DISPLAY( " -h/-H : display help/long help and exit\n"); return 0; }
static int usage_advanced(const char* programName) { DISPLAY(WELCOME_MESSAGE); usage(programName); DISPLAY( "\n"); DISPLAY( "Advanced arguments :\n"); DISPLAY( " -V : display Version number and exit\n"); DISPLAY( " -v : verbose mode\n"); DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n"); DISPLAY( " -c : force write to standard output, even if it is the console\n"); #ifdef UTIL_HAS_CREATEFILELIST DISPLAY( " -r : operate recursively on directories\n"); #endif DISPLAY( "--rm : remove source files after successful de/compression \n"); #ifndef ZSTD_NOCOMPRESS DISPLAY( "--ultra : enable ultra modes (requires more memory to decompress)\n"); DISPLAY( "--no-dictID : don't write dictID into header (dictionary compression)\n"); DISPLAY( "--[no-]check : integrity check (default:enabled)\n"); #endif #ifndef ZSTD_NODECOMPRESS DISPLAY( "--test : test compressed file integrity \n"); DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); #endif #ifndef ZSTD_NODICT DISPLAY( "\n"); DISPLAY( "Dictionary builder :\n"); DISPLAY( "--train ## : create a dictionary from a training set of files \n"); DISPLAY( " -o file : `file` is dictionary name (default: %s) \n", g_defaultDictName); DISPLAY( "--maxdict ## : limit dictionary to specified size (default : %u) \n", g_defaultMaxDictSize); DISPLAY( " -s# : dictionary selectivity level (default: %u)\n", g_defaultSelectivityLevel); DISPLAY( "--dictID ## : force dictionary ID to specified value (default: random)\n"); #endif #ifndef ZSTD_NOBENCH DISPLAY( "\n"); DISPLAY( "Benchmark arguments :\n"); DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); DISPLAY( " -e# : test all compression levels from -bX to # (default: 1)\n"); DISPLAY( " -i# : iteration loops [1-9](default : 3)\n"); DISPLAY( " -B# : cut file into independent blocks of size # (default: no block)\n"); #endif return 0; }
static int usage_longhelp(const char* exeName) { usage_advanced(exeName); DISPLAY( "\n"); DISPLAY( "****************************\n"); DISPLAY( "***** Advanced comment *****\n"); DISPLAY( "****************************\n"); DISPLAY( "\n"); DISPLAY( "Which values can [output] have ? \n"); DISPLAY( "---------------------------------\n"); DISPLAY( "[output] : a filename \n"); DISPLAY( " '%s', or '-' for standard output (pipe mode)\n", stdoutmark); DISPLAY( " '%s' to discard output (test mode) \n", NULL_OUTPUT); DISPLAY( "[output] can be left empty. In this case, it receives the following value :\n"); DISPLAY( " - if stdout is not the console, then [output] = stdout \n"); DISPLAY( " - if stdout is console : \n"); DISPLAY( " + for compression, output to filename%s \n", LZ4_EXTENSION); DISPLAY( " + for decompression, output to filename without '%s'\n", LZ4_EXTENSION); DISPLAY( " > if input filename has no '%s' extension : error \n", LZ4_EXTENSION); DISPLAY( "\n"); DISPLAY( "Compression levels : \n"); DISPLAY( "---------------------\n"); DISPLAY( "-0 ... -2 => Fast compression, all identicals\n"); DISPLAY( "-3 ... -%d => High compression; higher number == more compression but slower\n", LZ4HC_CLEVEL_MAX); DISPLAY( "\n"); DISPLAY( "stdin, stdout and the console : \n"); DISPLAY( "--------------------------------\n"); DISPLAY( "To protect the console from binary flooding (bad argument mistake)\n"); DISPLAY( "%s will refuse to read from console, or write to console \n", exeName); DISPLAY( "except if '-c' command is specified, to force output to console \n"); DISPLAY( "\n"); DISPLAY( "Simple example :\n"); DISPLAY( "----------------\n"); DISPLAY( "1 : compress 'filename' fast, using default output name 'filename.lz4'\n"); DISPLAY( " %s filename\n", exeName); DISPLAY( "\n"); DISPLAY( "Short arguments can be aggregated. For example :\n"); DISPLAY( "----------------------------------\n"); DISPLAY( "2 : compress 'filename' in high compression mode, overwrite output if exists\n"); DISPLAY( " %s -9 -f filename \n", exeName); DISPLAY( " is equivalent to :\n"); DISPLAY( " %s -9f filename \n", exeName); DISPLAY( "\n"); DISPLAY( "%s can be used in 'pure pipe mode'. For example :\n", exeName); DISPLAY( "-------------------------------------\n"); DISPLAY( "3 : compress data stream from 'generator', send result to 'consumer'\n"); DISPLAY( " generator | %s | consumer \n", exeName); if (g_lz4c_legacy_commands) { DISPLAY( "\n"); DISPLAY( "***** Warning ***** \n"); DISPLAY( "Legacy arguments take precedence. Therefore : \n"); DISPLAY( "--------------------------------- \n"); DISPLAY( " %s -hc filename \n", exeName); DISPLAY( "means 'compress filename in high compression mode' \n"); DISPLAY( "It is not equivalent to : \n"); DISPLAY( " %s -h -c filename \n", exeName); DISPLAY( "which displays help text and exits \n"); } return 0; }
int main(int argc, const char** argv) { int i, cLevel=1, cLevelLast=-10000, legacy_format=0, forceStdout=0, main_pause=0, multiple_inputs=0, all_arguments_are_files=0, operationResult=0; operationMode_e mode = om_auto; const char* input_filename = NULL; const char* output_filename= NULL; const char* dictionary_filename = NULL; char* dynNameSpace = NULL; const char** inFileNames = (const char**) calloc(argc, sizeof(char*)); unsigned ifnIdx=0; const char nullOutput[] = NULL_OUTPUT; const char extension[] = LZ4_EXTENSION; size_t blockSize = LZ4IO_setBlockSizeID(LZ4_BLOCKSIZEID_DEFAULT); const char* const exeName = lastNameFromPath(argv[0]); #ifdef UTIL_HAS_CREATEFILELIST const char** extendedFileList = NULL; char* fileNamesBuf = NULL; unsigned fileNamesNb, recursive=0; #endif /* Init */ if (inFileNames==NULL) { DISPLAY("Allocation error : not enough memory \n"); return 1; } inFileNames[0] = stdinmark; LZ4IO_setOverwrite(0); /* predefined behaviors, based on binary/link name */ if (exeNameMatch(exeName, LZ4CAT)) { mode = om_decompress; LZ4IO_setOverwrite(1); LZ4IO_setRemoveSrcFile(0); forceStdout=1; output_filename=stdoutmark; displayLevel=1; multiple_inputs=1; } if (exeNameMatch(exeName, UNLZ4)) { mode = om_decompress; } if (exeNameMatch(exeName, LZ4_LEGACY)) { g_lz4c_legacy_commands=1; } /* command switches */ for(i=1; i<argc; i++) { const char* argument = argv[i]; if(!argument) continue; /* Protection if argument empty */ /* Short commands (note : aggregated short commands are allowed) */ if (!all_arguments_are_files && argument[0]=='-') { /* '-' means stdin/stdout */ if (argument[1]==0) { if (!input_filename) input_filename=stdinmark; else output_filename=stdoutmark; continue; } /* long commands (--long-word) */ if (argument[1]=='-') { if (!strcmp(argument, "--")) { all_arguments_are_files = 1; continue; } if (!strcmp(argument, "--compress")) { mode = om_compress; continue; } if ((!strcmp(argument, "--decompress")) || (!strcmp(argument, "--uncompress"))) { mode = om_decompress; continue; } if (!strcmp(argument, "--multiple")) { multiple_inputs = 1; continue; } if (!strcmp(argument, "--test")) { mode = om_test; continue; } if (!strcmp(argument, "--force")) { LZ4IO_setOverwrite(1); continue; } if (!strcmp(argument, "--no-force")) { LZ4IO_setOverwrite(0); continue; } if ((!strcmp(argument, "--stdout")) || (!strcmp(argument, "--to-stdout"))) { forceStdout=1; output_filename=stdoutmark; continue; } if (!strcmp(argument, "--frame-crc")) { LZ4IO_setStreamChecksumMode(1); continue; } if (!strcmp(argument, "--no-frame-crc")) { LZ4IO_setStreamChecksumMode(0); continue; } if (!strcmp(argument, "--content-size")) { LZ4IO_setContentSize(1); continue; } if (!strcmp(argument, "--no-content-size")) { LZ4IO_setContentSize(0); continue; } if (!strcmp(argument, "--sparse")) { LZ4IO_setSparseFile(2); continue; } if (!strcmp(argument, "--no-sparse")) { LZ4IO_setSparseFile(0); continue; } if (!strcmp(argument, "--favor-decSpeed")) { LZ4IO_favorDecSpeed(1); continue; } if (!strcmp(argument, "--verbose")) { displayLevel++; continue; } if (!strcmp(argument, "--quiet")) { if (displayLevel) displayLevel--; continue; } if (!strcmp(argument, "--version")) { DISPLAY(WELCOME_MESSAGE); return 0; } if (!strcmp(argument, "--help")) { usage_advanced(exeName); goto _cleanup; } if (!strcmp(argument, "--keep")) { LZ4IO_setRemoveSrcFile(0); continue; } /* keep source file (default) */ if (!strcmp(argument, "--rm")) { LZ4IO_setRemoveSrcFile(1); continue; } if (longCommandWArg(&argument, "--fast")) { /* Parse optional acceleration factor */ if (*argument == '=') { U32 fastLevel; ++argument; fastLevel = readU32FromChar(&argument); if (fastLevel) { cLevel = -(int)fastLevel; } else { badusage(exeName); } } else if (*argument != 0) { /* Invalid character following --fast */ badusage(exeName); } else { cLevel = -1; /* default for --fast */ } continue; } } while (argument[1]!=0) { argument ++; if (g_lz4c_legacy_commands) { /* Legacy commands (-c0, -c1, -hc, -y) */ if (!strcmp(argument, "c0")) { cLevel=0; argument++; continue; } /* -c0 (fast compression) */ if (!strcmp(argument, "c1")) { cLevel=9; argument++; continue; } /* -c1 (high compression) */ if (!strcmp(argument, "c2")) { cLevel=12; argument++; continue; } /* -c2 (very high compression) */ if (!strcmp(argument, "hc")) { cLevel=12; argument++; continue; } /* -hc (very high compression) */ if (!strcmp(argument, "y")) { LZ4IO_setOverwrite(1); continue; } /* -y (answer 'yes' to overwrite permission) */ } if ((*argument>='0') && (*argument<='9')) { cLevel = readU32FromChar(&argument); argument--; continue; } switch(argument[0]) { /* Display help */ case 'V': DISPLAY(WELCOME_MESSAGE); goto _cleanup; /* Version */ case 'h': usage_advanced(exeName); goto _cleanup; case 'H': usage_longhelp(exeName); goto _cleanup; case 'e': argument++; cLevelLast = readU32FromChar(&argument); argument--; break; /* Compression (default) */ case 'z': mode = om_compress; break; case 'D': if (argument[1] == '\0') { /* path is next arg */ if (i + 1 == argc) { /* there is no next arg */ badusage(exeName); } dictionary_filename = argv[++i]; } else { /* path follows immediately */ dictionary_filename = argument + 1; } /* skip to end of argument so that we jump to parsing next argument */ argument += strlen(argument) - 1; break; /* Use Legacy format (ex : Linux kernel compression) */ case 'l': legacy_format = 1; blockSize = 8 MB; break; /* Decoding */ case 'd': mode = om_decompress; break; /* Force stdout, even if stdout==console */ case 'c': forceStdout=1; output_filename=stdoutmark; break; /* Test integrity */ case 't': mode = om_test; break; /* Overwrite */ case 'f': LZ4IO_setOverwrite(1); break; /* Verbose mode */ case 'v': displayLevel++; break; /* Quiet mode */ case 'q': if (displayLevel) displayLevel--; break; /* keep source file (default anyway, so useless) (for xz/lzma compatibility) */ case 'k': LZ4IO_setRemoveSrcFile(0); break; /* Modify Block Properties */ case 'B': while (argument[1]!=0) { int exitBlockProperties=0; switch(argument[1]) { case 'D': LZ4IO_setBlockMode(LZ4IO_blockLinked); argument++; break; case 'X': LZ4IO_setBlockChecksumMode(1); argument ++; break; /* disabled by default */ default : if (argument[1] < '0' || argument[1] > '9') { exitBlockProperties=1; break; } else { unsigned B; argument++; B = readU32FromChar(&argument); argument--; if (B < 4) badusage(exeName); if (B <= 7) { blockSize = LZ4IO_setBlockSizeID(B); BMK_setBlockSize(blockSize); DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); } else { if (B < 32) badusage(exeName); BMK_setBlockSize(B); if (B >= 1024) { DISPLAYLEVEL(2, "bench: using blocks of size %u KB \n", (U32)(B>>10)); } else { DISPLAYLEVEL(2, "bench: using blocks of size %u bytes \n", (U32)(B)); } } break; } } if (exitBlockProperties) break; } break; /* Benchmark */ case 'b': mode = om_bench; multiple_inputs=1; break; /* hidden command : benchmark files, but do not fuse result */ case 'S': BMK_setBenchSeparately(1); break; #ifdef UTIL_HAS_CREATEFILELIST /* recursive */ case 'r': recursive=1; #endif /* fall-through */ /* Treat non-option args as input files. See https://code.google.com/p/lz4/issues/detail?id=151 */ case 'm': multiple_inputs=1; break; /* Modify Nb Seconds (benchmark only) */ case 'i': { unsigned iters; argument++; iters = readU32FromChar(&argument); argument--; BMK_setNotificationLevel(displayLevel); BMK_setNbSeconds(iters); /* notification if displayLevel >= 3 */ } break; /* Pause at the end (hidden option) */ case 'p': main_pause=1; break; /* Unrecognised command */ default : badusage(exeName); }
static int usage_advanced(const char* exeName) { DISPLAY(WELCOME_MESSAGE); usage(exeName); DISPLAY( "\n"); DISPLAY( "Advanced arguments :\n"); DISPLAY( " -V : display Version number and exit \n"); DISPLAY( " -v : verbose mode \n"); DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n"); DISPLAY( " -c : force write to standard output, even if it is the console\n"); DISPLAY( " -t : test compressed file integrity\n"); DISPLAY( " -m : multiple input files (implies automatic output filenames)\n"); #ifdef UTIL_HAS_CREATEFILELIST DISPLAY( " -r : operate recursively on directories (sets also -m) \n"); #endif DISPLAY( " -l : compress using Legacy format (Linux kernel compression)\n"); DISPLAY( " -B# : Block size [4-7] (default : 7) \n"); DISPLAY( " -BD : Block dependency (improve compression ratio) \n"); DISPLAY( " -BX : enable block checksum (default:disabled) \n"); DISPLAY( "--no-frame-crc : disable stream checksum (default:enabled) \n"); DISPLAY( "--content-size : compressed frame includes original size (default:not present)\n"); DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); DISPLAY( "--favor-decSpeed: compressed files decompress faster, but are less compressed \n"); DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %u)\n", 1); DISPLAY( "Benchmark arguments : \n"); DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); DISPLAY( " -e# : test all compression levels from -bX to # (default : 1)\n"); DISPLAY( " -i# : minimum evaluation time in seconds (default : 3s) \n"); DISPLAY( " -B# : cut file into independent blocks of size # bytes [32+] \n"); DISPLAY( " or predefined block size [4-7] (default: 7) \n"); if (g_lz4c_legacy_commands) { DISPLAY( "Legacy arguments : \n"); DISPLAY( " -c0 : fast compression \n"); DISPLAY( " -c1 : high compression \n"); DISPLAY( " -c2,-hc: very high compression \n"); DISPLAY( " -y : overwrite output without prompting \n"); } return 0; }
/*-*************************** * Functions *****************************/ static int usage(const char* exeName) { DISPLAY( "Usage : \n"); DISPLAY( " %s [arg] [input] [output] \n", exeName); DISPLAY( "\n"); DISPLAY( "input : a filename \n"); DISPLAY( " with no FILE, or when FILE is - or %s, read standard input\n", stdinmark); DISPLAY( "Arguments : \n"); DISPLAY( " -1 : Fast compression (default) \n"); DISPLAY( " -9 : High compression \n"); DISPLAY( " -d : decompression (default for %s extension)\n", LZ4_EXTENSION); DISPLAY( " -z : force compression \n"); DISPLAY( " -D FILE: use FILE as dictionary \n"); DISPLAY( " -f : overwrite output without prompting \n"); DISPLAY( " -k : preserve source files(s) (default) \n"); DISPLAY( "--rm : remove source file(s) after successful de/compression \n"); DISPLAY( " -h/-H : display help/long help and exit \n"); return 0; }
int main (int argc, const char** argv) { U32 seed, startTestNb=0, pause=0, totalTest = FUZ_NB_TESTS; int argNb; seed = FUZ_GetMilliStart() % 10000; DISPLAYLEVEL (1, "HUF (%2i bits) automated test\n", (int)sizeof(void*)*8); for (argNb=1; argNb<argc; argNb++) { const char* argument = argv[argNb]; if (argument[0]=='-') { argument++; while (argument[0]!=0) { switch (argument[0]) { /* seed setting */ case 's': argument++; seed=0; while ((*argument>='0') && (*argument<='9')) seed *= 10, seed += *argument++ - '0'; break; /* total tests */ case 'i': argument++; totalTest=0; while ((*argument>='0') && (*argument<='9')) totalTest *= 10, totalTest += *argument++ - '0'; break; /* jump to test nb */ case 't': argument++; startTestNb=0; while ((*argument>='0') && (*argument<='9')) startTestNb *= 10, startTestNb += *argument++ - '0'; break; /* verbose mode */ case 'v': argument++; displayLevel=4; break; /* pause (hidden) */ case 'p': argument++; pause=1; break; default: return badUsage(argv[0]); } } } /* if (argument[0]=='-') */ } /* for (argNb=1; argNb<argc; argNb++) */ if (startTestNb == 0) unitTest(); DISPLAY("Fuzzer seed : %u \n", seed); FUZ_tests (seed, totalTest, startTestNb); DISPLAY ("\rAll %u tests passed \n", totalTest); if (pause) { int unused; DISPLAY("press enter ...\n"); unused = getchar(); (void)unused; } return 0; }
void XDrawString_90(XWindow *xw, Drawable d, int x, int y, char *string, int length) { int i, j; XCharStruct size; unsigned int width, height; unsigned int x0, y0; XImage *in, *out; Pixmap pix; XGCValues val; XGetGCValues(DISPLAY(xw), xw->gc, GCForeground | GCBackground, &val); XTextSize(xw->font->font_core, string, length, &size); width = -size.lbearing + size.rbearing; height = size.ascent + size.descent; x0 = -size.lbearing; y0 = size.ascent; pix = XCreatePixmap(DISPLAY(xw), ROOT(xw), width, height, DEPTH(xw)); /* Background Fill */ XSetForeground(DISPLAY(xw), xw->gc, val.background); XFillRectangle(DISPLAY(xw), pix, xw->gc, 0, 0, width, height); XSetForeground(DISPLAY(xw), xw->gc, val.foreground); XDrawString(DISPLAY(xw), pix, xw->gc, x0, y0, string, length); /* Convert from pixmap to image */ in = XGetImage(DISPLAY(xw), pix, 0,0, width, height, AllPlanes, ZPixmap); XFreePixmap(DISPLAY(xw), pix); /* Create Image with Width and Height exchanged */ out = XCreateImage(DISPLAY(xw), VISUAL(xw), DEPTH(xw), ZPixmap, 0, NULL, height, width, 32, 0); out->data = (char *) malloc(sizeof(char) * width * out->bytes_per_line); /* "Rotate" Image */ for(j = 0; j < (int)height; j++) { for(i = 0; i < (int)width; i++) { /* width - i - 1 : Flip the Image Vertically */ XPutPixel(out, j, width - i - 1, XGetPixel(in, i, j)); } } pix = XCreatePixmap(DISPLAY(xw), ROOT(xw), height, width, DEPTH(xw)); XPutImage(DISPLAY(xw), pix, xw->gc, out, 0, 0, 0, 0, height, width); XCopyArea(DISPLAY(xw), pix, d, xw->gc, 0, 0, height, width, x, y); XFreePixmap(DISPLAY(xw), pix); XDestroyImage(out); XDestroyImage(in); }
int main(int argCount, const char** argv) { int argNb, bench=0, decode=0, forceStdout=0, main_pause=0, nextEntryIsDictionary=0, operationResult=0, dictBuild=0, nextArgumentIsOutFileName=0, nextArgumentIsMaxDict=0, nextArgumentIsDictID=0; unsigned cLevel = 1; unsigned cLevelLast = 1; unsigned recursive = 0; const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */ unsigned filenameIdx = 0; const char* programName = argv[0]; const char* outFileName = NULL; const char* dictFileName = NULL; char* dynNameSpace = NULL; unsigned maxDictSize = g_defaultMaxDictSize; unsigned dictID = 0; unsigned dictCLevel = g_defaultDictCLevel; unsigned dictSelect = g_defaultSelectivityLevel; #ifdef UTIL_HAS_CREATEFILELIST const char** fileNamesTable = NULL; char* fileNamesBuf = NULL; unsigned fileNamesNb; #endif /* init */ (void)recursive; (void)cLevelLast; /* not used when ZSTD_NOBENCH set */ (void)dictCLevel; (void)dictSelect; (void)dictID; /* not used when ZSTD_NODICT set */ (void)decode; (void)cLevel; /* not used when ZSTD_NOCOMPRESS set */ if (filenameTable==NULL) { DISPLAY("not enough memory\n"); exit(1); } filenameTable[0] = stdinmark; displayOut = stderr; /* Pick out program name from path. Don't rely on stdlib because of conflicting behavior */ { size_t pos; for (pos = (int)strlen(programName); pos > 0; pos--) { if (programName[pos] == '/') { pos++; break; } } programName += pos; } /* preset behaviors */ if (!strcmp(programName, ZSTD_UNZSTD)) decode=1; if (!strcmp(programName, ZSTD_CAT)) { decode=1; forceStdout=1; displayLevel=1; outFileName=stdoutmark; } /* command switches */ for(argNb=1; argNb<argCount; argNb++) { const char* argument = argv[argNb]; if(!argument) continue; /* Protection if argument empty */ /* long commands (--long-word) */ if (!strcmp(argument, "--decompress")) { decode=1; continue; } if (!strcmp(argument, "--force")) { FIO_overwriteMode(); continue; } if (!strcmp(argument, "--version")) { displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); } if (!strcmp(argument, "--help")) { displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); } if (!strcmp(argument, "--verbose")) { displayLevel=4; continue; } if (!strcmp(argument, "--quiet")) { displayLevel--; continue; } if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; displayLevel=1; continue; } if (!strcmp(argument, "--ultra")) { FIO_setMaxWLog(0); continue; } if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(2); continue; } if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(0); continue; } if (!strcmp(argument, "--no-dictID")) { FIO_setDictIDFlag(0); continue; } if (!strcmp(argument, "--sparse")) { FIO_setSparseWrite(2); continue; } if (!strcmp(argument, "--no-sparse")) { FIO_setSparseWrite(0); continue; } if (!strcmp(argument, "--test")) { decode=1; outFileName=nulmark; FIO_overwriteMode(); continue; } if (!strcmp(argument, "--train")) { dictBuild=1; outFileName=g_defaultDictName; continue; } if (!strcmp(argument, "--maxdict")) { nextArgumentIsMaxDict=1; continue; } if (!strcmp(argument, "--dictID")) { nextArgumentIsDictID=1; continue; } if (!strcmp(argument, "--keep")) { continue; } /* does nothing, since preserving input is default; for gzip/xz compatibility */ if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(1); continue; } /* '-' means stdin/stdout */ if (!strcmp(argument, "-")){ if (!filenameIdx) { filenameIdx=1, filenameTable[0]=stdinmark; outFileName=stdoutmark; continue; } } /* Decode commands (note : aggregated commands are allowed) */ if (argument[0]=='-') { argument++; while (argument[0]!=0) { #ifndef ZSTD_NOCOMPRESS /* compression Level */ if ((*argument>='0') && (*argument<='9')) { cLevel = readU32FromChar(&argument); dictCLevel = cLevel; if (dictCLevel > ZSTD_maxCLevel()) CLEAN_RETURN(badusage(programName)); continue; } #endif switch(argument[0]) { /* Display help */ case 'V': displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); /* Version Only */ case 'H': case 'h': displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); /* Decoding */ case 'd': decode=1; argument++; break; /* Force stdout, even if stdout==console */ case 'c': forceStdout=1; outFileName=stdoutmark; displayLevel=1; argument++; break; /* Use file content as dictionary */ case 'D': nextEntryIsDictionary = 1; argument++; break; /* Overwrite */ case 'f': FIO_overwriteMode(); forceStdout=1; argument++; break; /* Verbose mode */ case 'v': displayLevel=4; argument++; break; /* Quiet mode */ case 'q': displayLevel--; argument++; break; /* keep source file (default anyway, so useless; for gzip/xz compatibility) */ case 'k': argument++; break; /* Checksum */ case 'C': argument++; FIO_setChecksumFlag(2); break; /* test compressed file */ case 't': decode=1; outFileName=nulmark; argument++; break; /* dictionary name */ case 'o': nextArgumentIsOutFileName=1; argument++; break; /* recursive */ case 'r': recursive=1; argument++; break; #ifndef ZSTD_NOBENCH /* Benchmark */ case 'b': bench=1; argument++; break; /* range bench (benchmark only) */ case 'e': /* compression Level */ argument++; cLevelLast = readU32FromChar(&argument); break; /* Modify Nb Iterations (benchmark only) */ case 'i': argument++; { U32 const iters = readU32FromChar(&argument); BMK_setNotificationLevel(displayLevel); BMK_SetNbIterations(iters); } break; /* cut input into blocks (benchmark only) */ case 'B': argument++; { size_t bSize = readU32FromChar(&argument); if (toupper(*argument)=='K') bSize<<=10, argument++; /* allows using KB notation */ if (toupper(*argument)=='M') bSize<<=20, argument++; if (toupper(*argument)=='B') argument++; BMK_setNotificationLevel(displayLevel); BMK_SetBlockSize(bSize); } break; #endif /* ZSTD_NOBENCH */ /* Dictionary Selection level */ case 's': argument++; dictSelect = readU32FromChar(&argument); break; /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */ case 'p': argument++; #ifndef ZSTD_NOBENCH if ((*argument>='0') && (*argument<='9')) { BMK_setAdditionalParam(readU32FromChar(&argument)); } else #endif main_pause=1; break; /* unknown command */ default : CLEAN_RETURN(badusage(programName)); } } continue; } /* if (argument[0]=='-') */ if (nextEntryIsDictionary) { nextEntryIsDictionary = 0; dictFileName = argument; continue; } if (nextArgumentIsOutFileName) { nextArgumentIsOutFileName = 0; outFileName = argument; if (!strcmp(outFileName, "-")) outFileName = stdoutmark; continue; } if (nextArgumentIsMaxDict) { nextArgumentIsMaxDict = 0; maxDictSize = readU32FromChar(&argument); if (toupper(*argument)=='K') maxDictSize <<= 10; if (toupper(*argument)=='M') maxDictSize <<= 20; continue; } if (nextArgumentIsDictID) { nextArgumentIsDictID = 0; dictID = readU32FromChar(&argument); continue; } /* add filename to list */ filenameTable[filenameIdx++] = argument; } /* Welcome message (if verbose) */ DISPLAYLEVEL(3, WELCOME_MESSAGE); #ifdef UTIL_HAS_CREATEFILELIST if (recursive) { fileNamesTable = UTIL_createFileList(filenameTable, filenameIdx, &fileNamesBuf, &fileNamesNb); if (fileNamesTable) { unsigned i; for (i=0; i<fileNamesNb; i++) DISPLAYLEVEL(3, "%d %s\n", i, fileNamesTable[i]); free((void*)filenameTable); filenameTable = fileNamesTable; filenameIdx = fileNamesNb; } } #endif /* Check if benchmark is selected */ if (bench) { #ifndef ZSTD_NOBENCH BMK_setNotificationLevel(displayLevel); BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel, cLevelLast); #endif goto _end; } /* Check if dictionary builder is selected */ if (dictBuild) { #ifndef ZSTD_NODICT ZDICT_params_t dictParams; dictParams.compressionLevel = dictCLevel; dictParams.selectivityLevel = dictSelect; dictParams.notificationLevel = displayLevel; dictParams.dictID = dictID; DiB_trainFromFiles(outFileName, maxDictSize, filenameTable, filenameIdx, dictParams); #endif goto _end; } /* No input filename ==> use stdin and stdout */ filenameIdx += !filenameIdx; /*< default input is stdin */ if (!strcmp(filenameTable[0], stdinmark) && !outFileName ) outFileName = stdoutmark; /*< when input is stdin, default output is stdout */ /* Check if input/output defined as console; trigger an error in this case */ if (!strcmp(filenameTable[0], stdinmark) && IS_CONSOLE(stdin) ) CLEAN_RETURN(badusage(programName)); if (outFileName && !strcmp(outFileName, stdoutmark) && IS_CONSOLE(stdout) && !(forceStdout && decode)) CLEAN_RETURN(badusage(programName)); /* user-selected output filename, only possible with a single file */ if (outFileName && strcmp(outFileName,stdoutmark) && strcmp(outFileName,nulmark) && (filenameIdx>1)) { DISPLAY("Too many files (%u) on the command line. \n", filenameIdx); CLEAN_RETURN(filenameIdx); } /* No warning message in pipe mode (stdin + stdout) or multiple mode */ if (!strcmp(filenameTable[0], stdinmark) && outFileName && !strcmp(outFileName,stdoutmark) && (displayLevel==2)) displayLevel=1; if ((filenameIdx>1) && (displayLevel==2)) displayLevel=1; /* IO Stream/File */ FIO_setNotificationLevel(displayLevel); #ifndef ZSTD_NOCOMPRESS if (!decode) { if (filenameIdx==1 && outFileName) operationResult = FIO_compressFilename(outFileName, filenameTable[0], dictFileName, cLevel); else operationResult = FIO_compressMultipleFilenames(filenameTable, filenameIdx, outFileName ? outFileName : ZSTD_EXTENSION, dictFileName, cLevel); } else #endif { /* decompression */ #ifndef ZSTD_NODECOMPRESS if (filenameIdx==1 && outFileName) operationResult = FIO_decompressFilename(outFileName, filenameTable[0], dictFileName); else operationResult = FIO_decompressMultipleFilenames(filenameTable, filenameIdx, outFileName ? outFileName : ZSTD_EXTENSION, dictFileName); #else DISPLAY("Decompression not supported\n"); #endif } _end: if (main_pause) waitEnter(); free(dynNameSpace); #ifdef UTIL_HAS_CREATEFILELIST if (fileNamesTable) UTIL_freeFileList(fileNamesTable, fileNamesBuf); else #endif free((void*)filenameTable); return operationResult; }
int xwindow_text_width_xft(XWindow *xw, char *text) { XGlyphInfo extents; XftTextExtents8(DISPLAY(xw), xw->font->font_xft, (FcChar8 *)text, strlen(text), &extents); return extents.xOff; }
static int basicUnitTests(U32 seed, double compressibility) { int testResult = 0; void* CNBuffer; void* compressedBuffer; void* decodedBuffer; U32 randState = seed; size_t result, cSize; U32 testNb=0; // Create compressible test buffer CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); compressedBuffer = malloc(ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)); decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); FUZ_generateSynthetic(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); // Basic tests DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), CNBuffer, COMPRESSIBLE_NOISE_LENGTH); if (ZSTD_isError(result)) goto _output_error; cSize = result; DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize); if (ZSTD_isError(result)) goto _output_error; DISPLAYLEVEL(4, "OK \n"); { size_t i; DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); for (i=0; i<COMPRESSIBLE_NOISE_LENGTH; i++) { if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; } DISPLAYLEVEL(4, "OK \n"); } DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++); result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize-1); if (!ZSTD_isError(result)) goto _output_error; if (result != (size_t)-ZSTD_ERROR_wrongSrcSize) goto _output_error; DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++); result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize+1); if (!ZSTD_isError(result)) goto _output_error; if (result != (size_t)-ZSTD_ERROR_wrongSrcSize) goto _output_error; DISPLAYLEVEL(4, "OK \n"); /* Decompression defense tests */ DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++); result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, CNBuffer, 3); if (!ZSTD_isError(result)) goto _output_error; if (result != (size_t)-ZSTD_ERROR_wrongSrcSize) goto _output_error; DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++); ((char*)(CNBuffer))[0] = 1; result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, CNBuffer, 4); if (!ZSTD_isError(result)) goto _output_error; if (result != (size_t)-ZSTD_ERROR_wrongMagicNumber) goto _output_error; DISPLAYLEVEL(4, "OK \n"); _end: free(CNBuffer); free(compressedBuffer); free(decodedBuffer); return testResult; _output_error: testResult = 1; DISPLAY("Error detected in Unit tests ! \n"); goto _end; }
void BMK_SetNbIterations(int nbLoops) { nbIterations = nbLoops; DISPLAY("- %i iterations-", nbIterations); }
int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility) { BYTE* srcBuffer; BYTE* cBuffer; BYTE* dstBuffer; size_t srcBufferSize = (size_t)1<<maxSrcLog; size_t dstBufferSize = (size_t)1<<maxSampleLog; size_t cBufferSize = ZSTD_compressBound(dstBufferSize); U32 result = 0; U32 testNb = 0; U32 coreSeed = seed, lseed = 0; (void)startTest; (void)compressibility; /* allocation */ srcBuffer = malloc (srcBufferSize); dstBuffer = malloc (dstBufferSize); cBuffer = malloc (cBufferSize); CHECK (!srcBuffer || !dstBuffer || !cBuffer, "Not enough memory, fuzzer tests cancelled"); /* Create initial sample */ FUZ_generateSynthetic(srcBuffer, srcBufferSize, 0.50, &coreSeed); /* catch up testNb */ for (testNb=0; testNb < startTest; testNb++) FUZ_rand(&coreSeed); /* test loop */ for (testNb=startTest; testNb < nbTests; testNb++) { size_t sampleSize, sampleStart; size_t cSize, dSize, dSupSize; U32 sampleSizeLog; U64 crcOrig, crcDest; /* init */ DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); FUZ_rand(&coreSeed); lseed = coreSeed ^ prime1; sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; sampleSize = (size_t)1<<sampleSizeLog; sampleSize += FUZ_rand(&lseed) & (sampleSize-1); sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); crcOrig = XXH64(srcBuffer + sampleStart, sampleSize, 0); /* compression tests*/ cSize = ZSTD_compress(cBuffer, cBufferSize, srcBuffer + sampleStart, sampleSize); CHECK(ZSTD_isError(cSize), "ZSTD_compress failed"); /* decompression tests*/ dSupSize = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1; dSize = ZSTD_decompress(dstBuffer, sampleSize + dSupSize, cBuffer, cSize); CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s)", ZSTD_getErrorName(dSize)); crcDest = XXH64(dstBuffer, sampleSize, 0); CHECK(crcOrig != crcDest, "dstBuffer corrupted (pos %u / %u)", (U32)findDiff(srcBuffer+sampleStart, dstBuffer, sampleSize), (U32)sampleSize); } DISPLAY("\rAll fuzzer tests completed \n"); _cleanup: free(srcBuffer); free(cBuffer); free(dstBuffer); return result; _output_error: result = 1; goto _cleanup; }
bool CSamMdFormatter::Format() { if( m_target == 0 || m_target[0] == 0 || m_target[1] == 0 ) return false; // tgt == "\xf" is possible const char * s = m_query; const char * t = m_target; const TTrSequence& tr = m_cigar; TTrSequence::const_iterator x = tr.begin(); if( x != tr.end() ) { switch( x->GetEvent() ) { case CTrBase::eEvent_SoftMask: s += x->GetCount(); case CTrBase::eEvent_HardMask: ++x; default: break; } } for( ; x != tr.end(); ++x ) { switch( x->GetEvent() ) { case CTrBase::eEvent_Replaced: case CTrBase::eEvent_Changed: case CTrBase::eEvent_Match: FormatAligned( s, t, x->GetCount() ); s += x->GetCount(); t += x->GetCount(); case CTrBase::eEvent_SoftMask: case CTrBase::eEvent_HardMask: case CTrBase::eEvent_Padding: continue; default: break; case CTrBase::eEvent_Splice: case CTrBase::eEvent_Overlap: m_data.clear(); return false; // MD can't be produced } // Here we have I or D switch( x->GetEvent() ) { case CTrBase::eEvent_Insertion: FormatInsertion( s, x->GetCount() ); s += x->GetCount(); break; case CTrBase::eEvent_Deletion: FormatDeletion( t, x->GetCount() ); t += x->GetCount(); break; default: THROW( logic_error, "Unexpected event " << x->GetEvent() << " @ " << __FILE__ << ":" << __LINE__ << DISPLAY( tr.ToString() ) ); } } FormatTerminal(); return m_data.length() > 0; }
int main(int argc, char** argv) { U32 seed=0; int seedset=0; int argNb; int nbTests = nbTestsDefault; int testNb = 0; int proba = FUZ_COMPRESSIBILITY_DEFAULT; int result=0; U32 mainPause = 0; char* programName; /* Check command line */ programName = argv[0]; for(argNb=1; argNb<argc; argNb++) { char* argument = argv[argNb]; if(!argument) continue; /* Protection if argument empty */ /* Handle commands. Aggregated commands are allowed */ if (argument[0]=='-') { argument++; while (*argument!=0) { switch(*argument) { case 'h': return FUZ_usage(programName); case 'v': argument++; g_displayLevel=4; break; case 'q': argument++; g_displayLevel--; break; case 'p': /* pause at the end */ argument++; mainPause = 1; break; case 'i': argument++; nbTests=0; while ((*argument>='0') && (*argument<='9')) { nbTests *= 10; nbTests += *argument - '0'; argument++; } break; case 's': argument++; seed=0; seedset=1; while ((*argument>='0') && (*argument<='9')) { seed *= 10; seed += *argument - '0'; argument++; } break; case 't': argument++; testNb=0; while ((*argument>='0') && (*argument<='9')) { testNb *= 10; testNb += *argument - '0'; argument++; } break; case 'P': /* compressibility % */ argument++; proba=0; while ((*argument>='0') && (*argument<='9')) { proba *= 10; proba += *argument - '0'; argument++; } if (proba<0) proba=0; if (proba>100) proba=100; break; default: return FUZ_usage(programName); } } } } /* Get Seed */ DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION); if (!seedset) seed = FUZ_GetMilliStart() % 10000; DISPLAY("Seed = %u\n", seed); if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); if (nbTests<=0) nbTests=1; if (testNb==0) result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ if (!result) result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); if (mainPause) { DISPLAY("Press Enter \n"); getchar(); } return result; }
static GObject * display_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) { GObject * obj; GObjectClass * parent_class; Display * self; parent_class = G_OBJECT_CLASS (display_parent_class); obj = parent_class->constructor (type, n_construct_properties, construct_properties); self = DISPLAY (obj); { GtkLabel* _label0; GtkHBox* _hbox0; GtkLabel* _label1; GtkLabel* _label2; GtkLabel* _label3; GtkLabel* _tmp0_; GtkComboBox* _tmp1_; GtkHBox* _tmp2_; GtkLabel* _tmp3_; GtkSpinButton* _tmp4_; GtkLabel* _tmp5_; GtkSpinButton* _tmp6_; GtkLabel* _tmp7_; GtkSpinButton* _tmp8_; GtkVBox* _tmp9_; GtkVBox* _tmp10_; GtkVBox* _tmp11_; GtkVBox* _tmp12_; _label0 = NULL; _hbox0 = NULL; _label1 = NULL; _label2 = NULL; _label3 = NULL; _label0 = (_tmp0_ = g_object_ref_sink ((GtkLabel*) gtk_label_new_with_mnemonic ("Display data blocks as images using imagemagick")), _g_object_unref0 (_label0), _tmp0_); self->priv->cb = (_tmp1_ = g_object_ref_sink ((GtkComboBox*) gtk_combo_box_new_text ()), _g_object_unref0 (self->priv->cb), _tmp1_); _hbox0 = (_tmp2_ = g_object_ref_sink ((GtkHBox*) gtk_hbox_new (FALSE, 10)), _g_object_unref0 (_hbox0), _tmp2_); _label1 = (_tmp3_ = g_object_ref_sink ((GtkLabel*) gtk_label_new_with_mnemonic ("Width:")), _g_object_unref0 (_label1), _tmp3_); self->priv->width = (_tmp4_ = g_object_ref_sink ((GtkSpinButton*) gtk_spin_button_new_with_range ((double) 50, (double) 5000, (double) 2)), _g_object_unref0 (self->priv->width), _tmp4_); _label2 = (_tmp5_ = g_object_ref_sink ((GtkLabel*) gtk_label_new_with_mnemonic ("Height:")), _g_object_unref0 (_label2), _tmp5_); self->priv->height = (_tmp6_ = g_object_ref_sink ((GtkSpinButton*) gtk_spin_button_new_with_range ((double) 50, (double) 5000, (double) 2)), _g_object_unref0 (self->priv->height), _tmp6_); _label3 = (_tmp7_ = g_object_ref_sink ((GtkLabel*) gtk_label_new_with_mnemonic ("Depth:")), _g_object_unref0 (_label3), _tmp7_); self->priv->depth = (_tmp8_ = g_object_ref_sink ((GtkSpinButton*) gtk_spin_button_new_with_range ((double) 1, (double) 32, (double) 1)), _g_object_unref0 (self->priv->depth), _tmp8_); gtk_window_set_title ((GtkWindow*) self, "Display data blocks"); gtk_dialog_set_has_separator ((GtkDialog*) self, FALSE); gtk_container_set_border_width ((GtkContainer*) self, (guint) 5); g_object_set ((GtkWindow*) self, "default-width", 300, NULL); g_object_set ((GtkWindow*) self, "default-height", 80, NULL); g_signal_connect_object ((GtkDialog*) self, "response", (GCallback) _display_on_response_gtk_dialog_response, self, 0); g_signal_connect ((GtkObject*) self, "destroy", (GCallback) _gtk_main_quit_gtk_object_destroy, NULL); gtk_label_set_mnemonic_widget (_label0, (GtkWidget*) self->priv->cb); gtk_box_pack_start ((GtkBox*) (_tmp9_ = ((GtkDialog*) self)->vbox, GTK_IS_VBOX (_tmp9_) ? ((GtkVBox*) _tmp9_) : NULL), (GtkWidget*) _label0, FALSE, TRUE, (guint) 0); g_signal_connect_object (self->priv->cb, "changed", (GCallback) __lambda0__gtk_combo_box_changed, self, 0); gtk_box_pack_start ((GtkBox*) (_tmp10_ = ((GtkDialog*) self)->vbox, GTK_IS_VBOX (_tmp10_) ? ((GtkVBox*) _tmp10_) : NULL), (GtkWidget*) self->priv->cb, FALSE, TRUE, (guint) 0); gtk_label_set_mnemonic_widget (_label1, (GtkWidget*) self->priv->width); gtk_box_pack_start ((GtkBox*) _hbox0, (GtkWidget*) _label1, FALSE, TRUE, (guint) 0); gtk_spin_button_set_value (self->priv->width, (double) 320); gtk_container_add ((GtkContainer*) _hbox0, (GtkWidget*) self->priv->width); gtk_label_set_mnemonic_widget (_label2, (GtkWidget*) self->priv->height); gtk_box_pack_start ((GtkBox*) _hbox0, (GtkWidget*) _label2, FALSE, TRUE, (guint) 0); gtk_spin_button_set_value (self->priv->height, (double) 240); gtk_container_add ((GtkContainer*) _hbox0, (GtkWidget*) self->priv->height); gtk_label_set_mnemonic_widget (_label3, (GtkWidget*) self->priv->depth); gtk_box_pack_start ((GtkBox*) _hbox0, (GtkWidget*) _label3, FALSE, TRUE, (guint) 0); gtk_spin_button_set_value (self->priv->depth, (double) 8); gtk_container_add ((GtkContainer*) _hbox0, (GtkWidget*) self->priv->depth); gtk_container_add ((GtkContainer*) (_tmp11_ = ((GtkDialog*) self)->vbox, GTK_IS_VBOX (_tmp11_) ? ((GtkVBox*) _tmp11_) : NULL), (GtkWidget*) _hbox0); gtk_box_set_spacing ((GtkBox*) (_tmp12_ = ((GtkDialog*) self)->vbox, GTK_IS_VBOX (_tmp12_) ? ((GtkVBox*) _tmp12_) : NULL), 10); _g_object_unref0 (_label0); _g_object_unref0 (_hbox0); _g_object_unref0 (_label1); _g_object_unref0 (_label2); _g_object_unref0 (_label3); } return obj; }
int basicTests(U32 seed, double compressibility) { int testResult = 0; void* CNBuffer; void* compressedBuffer; void* decodedBuffer; U32 randState = seed; int cSize, testSize; LZ4F_preferences_t prefs; LZ4F_compressionContext_t cctx = NULL; U64 crcOrig; LZ4SG_in_t sg_cin [MAX_SG_BUFFERS]; LZ4SG_out_t sg_cout[MAX_SG_BUFFERS]; LZ4SG_in_t sg_din [MAX_SG_BUFFERS]; LZ4SG_out_t sg_dout[MAX_SG_BUFFERS]; size_t sg_in_len, sg_out_len; size_t maxDstSize = LZ4_SG_compressBound(COMPRESSIBLE_NOISE_LENGTH, NELEMS(sg_cin), NELEMS(sg_cout)); size_t sourceSizeOut; /* Create compressible test buffer */ memset(&prefs, 0, sizeof(prefs)); CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); compressedBuffer = malloc(maxDstSize); decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH + DECODE_GUARD_LENGTH); FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); crcOrig = XXH64(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); /* Trivial tests : one input and one output buffers */ testSize = COMPRESSIBLE_NOISE_LENGTH; DISPLAYLEVEL(3, "One input and one output buffers : \n"); sg_cin[0].sg_base = CNBuffer; sg_cin[0].sg_len = COMPRESSIBLE_NOISE_LENGTH; sg_in_len = 1; sg_cout[0].sg_base = compressedBuffer; sg_cout[0].sg_len = maxDstSize; sg_out_len = 1; sourceSizeOut = testSize; cSize = LZ4_SG_compress(&sg_cin[0], sg_in_len, &sg_cout[0], sg_out_len, &sourceSizeOut, maxDstSize, DEFAULT_ACCEL); DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); UT_VERIFY(cSize > 0, goto _output_error); DISPLAYLEVEL(3, "Decompress test various valid sg_out and maxDstSize combinations \n"); { U64 crcDest; // sg_out.sg_len == maxDstSize == originalSize sg_din[0].sg_base = compressedBuffer; sg_din[0].sg_len = cSize; sg_dout[0].sg_base = decodedBuffer; sg_dout[0].sg_len = COMPRESSIBLE_NOISE_LENGTH; sourceSizeOut = cSize; maxDstSize = testSize; memset(decodedBuffer, 0, testSize); testResult = LZ4_SG_decompress(&sg_din[0], sg_in_len, &sg_dout[0], sg_out_len, &sourceSizeOut, maxDstSize); crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); DISPLAYLEVEL(3, "Decompressed %i bytes (out of %i bytes) to a %i bytes (out of %i bytes) dst_limit %i bytes\n", (int)sourceSizeOut, (int)cSize, (int)testResult, (int)testSize, (int)maxDstSize); UT_VERIFY(testResult > 0 , goto _output_error); UT_VERIFY(testResult == testSize, goto _output_error); UT_VERIFY( crcDest == crcOrig , goto _output_error); // maxDstSize > originalSize maxDstSize = COMPRESSIBLE_NOISE_LENGTH + DECODE_GUARD_LENGTH; sourceSizeOut = cSize; memset(decodedBuffer, 0, testSize); testResult = LZ4_SG_decompress(&sg_din[0], sg_in_len, &sg_dout[0], sg_out_len, &sourceSizeOut, maxDstSize); crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); DISPLAYLEVEL(3, "Decompressed %i bytes (out of %i bytes) to a %i bytes (out of %i bytes) dst_limit %i bytes\n", (int)sourceSizeOut, (int)cSize, (int)testResult, (int)testSize, (int)maxDstSize); UT_VERIFY(testResult > 0 , goto _output_error); UT_VERIFY(testResult == testSize, goto _output_error); UT_VERIFY( crcDest == crcOrig , goto _output_error); // sg_out.sg_len > originalSize maxDstSize = testSize; sg_dout[0].sg_len = COMPRESSIBLE_NOISE_LENGTH + DECODE_GUARD_LENGTH; sourceSizeOut = cSize; memset(decodedBuffer, 0, testSize); testResult = LZ4_SG_decompress(&sg_din[0], sg_in_len, &sg_dout[0], sg_out_len, &sourceSizeOut, maxDstSize); crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); DISPLAYLEVEL(3, "Decompressed %i bytes (out of %i bytes) to a %i bytes (out of %i bytes) dst_limit %i bytes\n", (int)sourceSizeOut, (int)cSize, (int)testResult, (int)testSize, (int)maxDstSize); UT_VERIFY(testResult > 0 , goto _output_error); UT_VERIFY(testResult == testSize, goto _output_error); UT_VERIFY( crcDest == crcOrig , goto _output_error); } DISPLAYLEVEL(3, "Frame Decompression test (%08x): \n", (unsigned int)crcOrig); { int lz4f_result = verify_basic_LZ4F_decompression(compressedBuffer, cSize, crcOrig, decodedBuffer, testSize); UT_VERIFY(0 == lz4f_result, goto _output_error); } // basic SGL test { // prepare SGL input const size_t num_data_buffers = 16; const size_t buf_size_bytes = 4 KB; unsigned int i; for (i = 0; i < 1+num_data_buffers; i++) { sg_cin [i].sg_base = malloc(buf_size_bytes); sg_cin [i].sg_len = buf_size_bytes; sg_cout[i].sg_base = malloc(buf_size_bytes); sg_cout[i].sg_len = buf_size_bytes; sg_din [i].sg_base = malloc(buf_size_bytes); sg_din [i].sg_len = buf_size_bytes; sg_dout[i].sg_base = malloc(buf_size_bytes); sg_dout[i].sg_len = buf_size_bytes; } for (i = 0; i < num_data_buffers; i++) { memcpy((void *)sg_cin [i].sg_base, ((const char *)CNBuffer)+(i*buf_size_bytes), buf_size_bytes); } sg_in_len = num_data_buffers; sg_out_len = 1+num_data_buffers; testSize = num_data_buffers * buf_size_bytes; maxDstSize = (1+num_data_buffers) * buf_size_bytes; crcOrig = XXH64(CNBuffer, testSize, 1); DISPLAYLEVEL(3, "Compress 16 4KB buffers into 17 4KB buffers : \n"); sourceSizeOut = testSize; cSize = LZ4_SG_compress(&sg_cin[0], sg_in_len, &sg_cout[0], sg_out_len, &sourceSizeOut, maxDstSize, DEFAULT_ACCEL); DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); UT_VERIFY(cSize > 0, goto _output_error); DISPLAYLEVEL(3, "Decompress 17 4KB buffers into 16 4KB buffers : \n"); for (i = 0; i < 1+num_data_buffers; i++) { memcpy((void *)sg_din[i].sg_base, sg_cout[i].sg_base, buf_size_bytes); } sourceSizeOut = cSize; maxDstSize = testSize; sg_in_len = 1+num_data_buffers; sg_out_len = num_data_buffers; memset(decodedBuffer, 0, testSize); testResult = LZ4_SG_decompress(&sg_din[0], sg_in_len, &sg_dout[0], sg_out_len, &sourceSizeOut, maxDstSize); DISPLAYLEVEL(3, "Decompressed %i bytes (out of %i bytes) to a %i bytes (out of %i bytes) dst_limit %i bytes\n", (int)sourceSizeOut, (int)cSize, (int)testResult, (int)testSize, (int)maxDstSize); for (i = 0; i < num_data_buffers; i++) { memcpy(((char *)decodedBuffer)+(i*buf_size_bytes), sg_dout[i].sg_base, buf_size_bytes); } UT_VERIFY(testResult > 0 , goto _output_error); UT_VERIFY(testResult == testSize, goto _output_error); U64 crcDest; crcDest = XXH64(decodedBuffer, testSize, 1); UT_VERIFY( crcDest == crcOrig , goto _output_error); DISPLAYLEVEL(3, "verify frame decompress on concatenated buffer: \n"); for (i = 0; i < 1+num_data_buffers; i++) { memcpy(((char *)compressedBuffer)+(i*buf_size_bytes), sg_cout[i].sg_base, buf_size_bytes); } int lz4f_result = verify_basic_LZ4F_decompression(compressedBuffer, cSize, crcOrig, decodedBuffer, testSize); UT_VERIFY(0 == lz4f_result, goto _output_error); // release SGL for (i = 0; i < 1+num_data_buffers; i++) { free((void *)sg_cin [i].sg_base); sg_cin [i].sg_base = NULL; sg_cin [i].sg_len = 0; free(sg_cout[i].sg_base); sg_cout[i].sg_base = NULL; sg_cout[i].sg_len = 0; free((void *)sg_din [i].sg_base); sg_din [i].sg_base = NULL; sg_din [i].sg_len = 0; free(sg_dout[i].sg_base); sg_dout[i].sg_base = NULL; sg_dout[i].sg_len = 0; } } DISPLAY("Basic tests completed \n"); testResult = 0; _end: free(CNBuffer); free(compressedBuffer); free(decodedBuffer); LZ4F_freeCompressionContext(cctx); cctx = NULL; return testResult; _output_error: testResult = 1; DISPLAY("Error detected ! \n"); locateBuffDiff(CNBuffer, decodedBuffer, testSize); goto _end; // unreachable return -1; }
gboolean mate_rr_crtc_set_config_with_time (MateRRCrtc *crtc, guint32 timestamp, int x, int y, MateRRMode *mode, MateRRRotation rotation, MateRROutput **outputs, int n_outputs, GError **error) { #ifdef HAVE_RANDR ScreenInfo *info; GArray *output_ids; Status status; gboolean result; int i; g_return_val_if_fail (crtc != NULL, FALSE); g_return_val_if_fail (mode != NULL || outputs == NULL || n_outputs == 0, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); info = crtc->info; if (mode) { if (x + mode->width > info->max_width || y + mode->height > info->max_height) { g_set_error (error, MATE_RR_ERROR, MATE_RR_ERROR_BOUNDS_ERROR, /* Translators: the "position", "size", and "maximum" * words here are not keywords; please translate them * as usual. A CRTC is a CRT Controller (this is X terminology) */ _("requested position/size for CRTC %d is outside the allowed limit: " "position=(%d, %d), size=(%d, %d), maximum=(%d, %d)"), (int) crtc->id, x, y, mode->width, mode->height, info->max_width, info->max_height); return FALSE; } } output_ids = g_array_new (FALSE, FALSE, sizeof (RROutput)); if (outputs) { for (i = 0; i < n_outputs; ++i) g_array_append_val (output_ids, outputs[i]->id); } status = XRRSetCrtcConfig (DISPLAY (crtc), info->resources, crtc->id, timestamp, x, y, mode ? mode->id : None, xrotation_from_rotation (rotation), (RROutput *)output_ids->data, output_ids->len); g_array_free (output_ids, TRUE); if (status == RRSetConfigSuccess) result = TRUE; else { result = FALSE; /* Translators: CRTC is a CRT Controller (this is X terminology). * It is *very* unlikely that you'll ever get this error, so it is * only listed for completeness. */ g_set_error (error, MATE_RR_ERROR, MATE_RR_ERROR_RANDR_ERROR, _("could not set the configuration for CRTC %d"), (int) crtc->id); } return result; #else return FALSE; #endif /* HAVE_RANDR */ }
int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility, U32 duration) { unsigned testResult = 0; unsigned testNb = 0; void* srcBuffer = NULL; void* compressedBuffer = NULL; void* decodedBuffer = NULL; U32 coreRand = seed; LZ4F_decompressionContext_t dCtx = NULL; LZ4F_compressionContext_t cCtx = NULL; size_t result; const U32 startTime = FUZ_GetMilliStart(); XXH64_state_t xxh64; # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } // backup all allocated addresses, from which we will later select buffers const size_t max_buf_size = 131 KB; size_t num_buf_size_distribution_deviations = 0; LZ4SG_in_t sg_in_buf_potential [2*MAX_SG_BUFFERS]; LZ4SG_out_t sg_out_buf_potential[2*MAX_SG_BUFFERS]; LZ4SG_in_t sg_cin [MAX_SG_BUFFERS]; LZ4SG_out_t sg_cout[MAX_SG_BUFFERS]; LZ4SG_in_t sg_din [MAX_SG_BUFFERS]; LZ4SG_out_t sg_dout[MAX_SG_BUFFERS]; size_t sg_cin_len, sg_cout_len, sg_din_len, sg_dout_len; const size_t maxDstSize = LZ4_SG_compressBound(srcDataLength, NELEMS(sg_cin), NELEMS(sg_cout)); unsigned int i; for (i = 0; i < NELEMS(sg_in_buf_potential); i++) { sg_in_buf_potential [i].sg_base = malloc(max_buf_size); sg_in_buf_potential [i].sg_len = max_buf_size; sg_out_buf_potential[i].sg_base = malloc(max_buf_size); sg_out_buf_potential[i].sg_len = max_buf_size; } /* Init */ duration *= 1000; /* Create buffers */ result = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); result = LZ4F_createCompressionContext(&cCtx, LZ4F_VERSION); CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); srcBuffer = malloc(srcDataLength); CHECK(srcBuffer==NULL, "srcBuffer Allocation failed"); const size_t compressedBufferLength = maxDstSize; compressedBuffer = malloc(compressedBufferLength); CHECK(compressedBuffer==NULL, "compressedBuffer Allocation failed"); decodedBuffer = calloc(1, srcDataLength); /* calloc avoids decodedBuffer being considered "garbage" by scan-build */ CHECK(decodedBuffer==NULL, "decodedBuffer Allocation failed"); FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &coreRand); /* jump to requested testNb */ for (testNb =0; (testNb < startTest); testNb++) (void)FUZ_rand(&coreRand); // sync randomizer /* main fuzzer test loop */ for ( ; (testNb < nbTests) || (duration > FUZ_GetMilliSpan(startTime)) ; testNb++) { U32 randState = coreRand ^ prime1; (void)FUZ_rand(&coreRand); /* update seed */ srand48(FUZ_rand(&randState)); DISPLAYUPDATE(2, "\r%5u ", testNb); const size_t max_src_buf_size = (4 MB > srcDataLength) ? srcDataLength : 4 MB; unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(max_src_buf_size-1) - 1)) + 1; const size_t min_src_size = 20; const size_t min_first_dest_buf_size = 21; const size_t min_src_buf_size = 1; const size_t min_dst_buf_size = 10; size_t srcSize = (FUZ_rand(&randState) & ((1<<nbBits)-1)) + min_src_size; size_t srcStart = FUZ_rand(&randState) % (srcDataLength - srcSize); size_t cSize; size_t dstSize; size_t dstSizeBound; U64 crcOrig, crcDecoded; unsigned int test_selection = FUZ_rand(&randState); //TODO: enable lz4f_compress_compatibility_test with LZ4_SG_decompress int lz4f_compress_compatibility_test = 0;//(test_selection % 4) == 0; if (!lz4f_compress_compatibility_test) { // SGL compress unsigned int buffer_selection = FUZ_rand(&randState); if ((buffer_selection & 0xF) == 1) { // SG compress single source and single target buffers sg_cin[0].sg_base = (BYTE*)srcBuffer+srcStart; sg_cin[0].sg_len = srcSize; sg_cin_len = 1; sg_cout[0].sg_base = compressedBuffer; sg_cout[0].sg_len = compressedBufferLength; sg_cout_len = 1; dstSizeBound = dstSize = compressedBufferLength; } else { // SG compress random number and size source and target buffers sg_cin_len = 1 + (FUZ_rand(&randState) % MAX_SG_BUFFERS); sg_cout_len = 1 + (FUZ_rand(&randState) % MAX_SG_BUFFERS); // single source buffer if (1 == sg_cin_len) { sg_cin[0].sg_base = (BYTE*)srcBuffer+srcStart; sg_cin[0].sg_len = srcSize; DISPLAYUPDATE(4, "INFO: single source buf size %i\n", (int)srcSize); } else { // multiple source buffers if (srcSize > sg_cin_len*max_buf_size/2) { srcSize = sg_cin_len*max_buf_size/2; num_buf_size_distribution_deviations++; DISPLAYUPDATE(4, "NOTE: source buffer total size deviation %i\n", (int)num_buf_size_distribution_deviations); } size_t exact_src_size = 0; unsigned int buf_size_mean = srcSize / sg_cin_len; for (i = 0; i < sg_cin_len; i++) { size_t buf_size = rnd_exponential(buf_size_mean, min_src_buf_size, max_buf_size); DISPLAYUPDATE(4, "INFO: source buf %i size %i\n", i, (int)buf_size); if (srcStart+exact_src_size+buf_size > srcDataLength) { buf_size = srcDataLength-(srcStart+exact_src_size); } sg_cin[i].sg_base = sg_in_buf_potential[i*2+1].sg_base; sg_cin[i].sg_len = buf_size; memcpy((void *)sg_cin[i].sg_base, (BYTE*)srcBuffer+srcStart+exact_src_size, buf_size); exact_src_size += buf_size; if (srcStart+exact_src_size == srcDataLength) { num_buf_size_distribution_deviations++; sg_cin_len = i+1; DISPLAYUPDATE(4, "NOTE: final source buffer size deviation %i (buffers number limited to %i)\n", (int)num_buf_size_distribution_deviations, (int)sg_cin_len); } } srcSize = exact_src_size; } // we can now derive the required limit for output dstSizeBound = LZ4_SG_compressBound(srcSize, sg_cin_len, sg_cout_len); // single target buffer if (1 == sg_cout_len) { sg_cout[0].sg_base = compressedBuffer; sg_cout[0].sg_len = compressedBufferLength; } else { // multiple target buffers int finalBufferTruncated = 0; dstSize = 0; unsigned int buf_size_mean = dstSizeBound / sg_cout_len; for (i = 0; i < sg_cout_len; i++) { const size_t min_buf_size = (i == 0) ? min_first_dest_buf_size : min_dst_buf_size; size_t buf_size = rnd_exponential(buf_size_mean, min_buf_size, max_buf_size); DISPLAYUPDATE(4, "INFO: target buf %i size %i\n", (int)i, (int)buf_size); if (dstSize+buf_size > dstSizeBound) { buf_size = dstSizeBound-dstSize; finalBufferTruncated = 1; } dstSize += buf_size; sg_cout[i].sg_base = sg_out_buf_potential[i*2+1].sg_base; sg_cout[i].sg_len = buf_size; if (finalBufferTruncated) { num_buf_size_distribution_deviations++; if (buf_size < min_buf_size) { // merge truncated with previous? if (i > 0) { sg_cout[i-1].sg_len += buf_size; if (sg_cout[i-1].sg_len > max_buf_size) { // skip, too much hassle DISPLAYUPDATE(4, "NOTE: unable to truncate final target buffer size (deviations %i), skipping\n", (int)num_buf_size_distribution_deviations); sg_cout_len = 0; break; } } else { // can this happen? DISPLAYUPDATE(4, "NOTE: unable to truncate first and final target buffer size (deviations %i), skipping\n", (int)num_buf_size_distribution_deviations); sg_cout_len = 0; break; } sg_cout_len = i; } else { sg_cout_len = i+1; } DISPLAYUPDATE(4, "NOTE: final target buffer size truncated (%i), buffers number limited to %i, final's size is now %i (deviations %i)\n", (int)buf_size, (int)sg_cout_len, (int)sg_cout[sg_cout_len-1].sg_len, (int)num_buf_size_distribution_deviations); } } // skip/abort condition if (0 == sg_cout_len) continue; } if ((buffer_selection & 0xF) == 0) { //TODO: select a random input and output buffer and split it in two, // feeding consecutive addresses as consecutive entries in SGL } } crcOrig = XXH64((BYTE*)srcBuffer+srcStart, srcSize, 1); size_t sourceSizeOut = srcSize; result = LZ4_SG_compress(&sg_cin[0], sg_cin_len, &sg_cout[0], sg_cout_len, &sourceSizeOut, maxDstSize, DEFAULT_ACCEL); if (((result == 0) || (sourceSizeOut != srcSize)) && (dstSize < dstSizeBound)) { // forgive compression failure when output total size is lower than bound num_buf_size_distribution_deviations++; DISPLAYUPDATE(4, "NOTE: dstSize %i < %i dstSizeBound, compression attempt failed, not totally unexpected (deviations %i), skipping\n", (int)dstSize, (int)dstSizeBound, (int)num_buf_size_distribution_deviations); continue; } CHECK(result <= 0, "Compression failed (error %i)", (int)result); CHECK(sourceSizeOut != srcSize, "Compression stopped at %i out of %i", (int)sourceSizeOut, (int)srcSize); cSize = result; } else { // LZ4F compression - use it in order to verify SGL decompress compatibility with it DISPLAYUPDATE(4, "INFO: LZ4F compression\n"); // alternative // size_t dstMaxSize = LZ4F_compressFrameBound(srcSize, prefsPtr); // DISPLAYLEVEL(3, "compressFrame srcSize %zu dstMaxSize %zu\n", // srcSize, dstMaxSize); // cSize = LZ4F_compressFrame(compressedBuffer, dstMaxSize, (char*)srcBuffer + srcStart, srcSize, prefsPtr); // CHECK(LZ4F_isError(cSize), "LZ4F_compressFrame failed : error %i (%s)", (int)cSize, LZ4F_getErrorName(cSize)); crcOrig = XXH64((BYTE*)srcBuffer+srcStart, srcSize, 1); unsigned BSId = 4 + (FUZ_rand(&randState) & 3); unsigned BMId = FUZ_rand(&randState) & 1; unsigned CCflag = FUZ_rand(&randState) & 1; unsigned autoflush = (FUZ_rand(&randState) & 7) == 2; U64 frameContentSize = ((FUZ_rand(&randState) & 0xF) == 1) ? srcSize : 0; LZ4F_preferences_t prefs; LZ4F_compressOptions_t cOptions; LZ4F_preferences_t* prefsPtr = &prefs; memset(&prefs, 0, sizeof(prefs)); memset(&cOptions, 0, sizeof(cOptions)); prefs.frameInfo.blockMode = (LZ4F_blockMode_t)BMId; prefs.frameInfo.blockSizeID = (LZ4F_blockSizeID_t)BSId; prefs.frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)CCflag; prefs.frameInfo.contentSize = frameContentSize; prefs.autoFlush = autoflush; prefs.compressionLevel = FUZ_rand(&randState) % 5; if ((FUZ_rand(&randState) & 0xF) == 1) prefsPtr = NULL; const BYTE* ip = (const BYTE*)srcBuffer + srcStart; const BYTE* const iend = ip + srcSize; BYTE* op = (BYTE*)compressedBuffer; BYTE* const oend = op + LZ4F_compressFrameBound(srcDataLength, NULL); unsigned maxBits = FUZ_highbit((U32)srcSize); result = LZ4F_compressBegin(cCtx, op, oend-op, prefsPtr); CHECK(LZ4F_isError(result), "Compression header failed (error %i)", (int)result); op += result; while (ip < iend) { unsigned nbBitsSeg = FUZ_rand(&randState) % maxBits; size_t iSize = (FUZ_rand(&randState) & ((1<<nbBitsSeg)-1)) + 1; size_t oSize = LZ4F_compressBound(iSize, prefsPtr); unsigned forceFlush = ((FUZ_rand(&randState) & 3) == 1); if (iSize > (size_t)(iend-ip)) iSize = iend-ip; cOptions.stableSrc = ((FUZ_rand(&randState) & 3) == 1); DISPLAYLEVEL(3, "compressUpdate ip %d iSize %zu oSize %zu forceFlush %d\n", (int)(ip-((const BYTE*)srcBuffer + srcStart)), iSize, oSize, forceFlush); result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); op += result; ip += iSize; if (forceFlush) { result = LZ4F_flush(cCtx, op, oend-op, &cOptions); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); op += result; } } result = LZ4F_compressEnd(cCtx, op, oend-op, &cOptions); CHECK(LZ4F_isError(result), "Compression completion failed (error %i)", (int)result); op += result; cSize = op-(BYTE*)compressedBuffer; } //DECOMPRESS test_selection = FUZ_rand(&randState); if (lz4f_compress_compatibility_test || ((test_selection % 2) == 0)) { //TODO: SGL decompress with random buffer sizes // SGL decompress with same buffer sizes used for compression // prepare din with cout's data sg_din_len = sg_cout_len; for (i = 0; i < sg_din_len; i++) { sg_din[i].sg_len = sg_cout[i].sg_len; if (sg_cout[i].sg_len <= max_buf_size) { // enough room to copy - do it sg_din[i].sg_base = sg_in_buf_potential[i*2+0].sg_base; if (sg_din[i].sg_base != sg_cout[i].sg_base) { memcpy((void *)sg_din[i].sg_base, sg_cout[i].sg_base, sg_cout[i].sg_len); } } else { // this is probably single output buffer - skip copy, use directly sg_din[i].sg_base = sg_cout[i].sg_base; } } // prepare dout to receive decompressed data sg_dout_len = sg_cin_len; for (i = 0; i < sg_dout_len; i++) { sg_dout[i].sg_len = sg_cin[i].sg_len; if (sg_cin[i].sg_len <= max_buf_size) { // enough room to decompress into independent buffer sg_dout[i].sg_base = sg_out_buf_potential[i*2+0].sg_base; } else { // this is probably single input buffer, use an external output buffer sg_dout[i].sg_base = decodedBuffer; } } size_t sourceSizeOut = cSize; size_t maxOutputSize = srcSize; int decomp_result = LZ4_SG_decompress(&sg_din[0], sg_din_len, &sg_dout[0], sg_dout_len, &sourceSizeOut, maxOutputSize); CHECK(decomp_result <= 0, "SG decompression failed (error %i)", (int)decomp_result); CHECK(decomp_result != (int)srcSize, "SG decompression stopped at %i", (int)decomp_result); // verify result checksum size_t total_checked = 0; XXH64_reset(&xxh64, 1); for (i = 0; (i < sg_dout_len) && ((int)total_checked < decomp_result); i++) { size_t cur_size = sg_dout[i].sg_len; size_t rem = decomp_result - total_checked; if (rem < cur_size) cur_size = rem; total_checked += cur_size; XXH64_update(&xxh64, sg_dout[i].sg_base, cur_size); } crcDecoded = XXH64_digest(&xxh64); if (crcDecoded != crcOrig) { DISPLAYLEVEL(1, "checked %i out of %i (crcDecoded %08x, crcOrig %08x)\n", (int)total_checked, decomp_result, (unsigned)crcDecoded, (unsigned)crcOrig); // locate error if any total_checked = 0; for (i = 0; (i < sg_dout_len) && ((int)total_checked < decomp_result); i++) { size_t cur_size = sg_dout[i].sg_len; size_t rem = decomp_result - total_checked; if (rem < cur_size) cur_size = rem; total_checked += cur_size; U64 crc_in = XXH64(sg_cin [i].sg_base, cur_size, 1); U64 crc_out = XXH64(sg_dout[i].sg_base, cur_size, 1); if (crc_in != crc_out) { locateBuffDiff(sg_cin[i].sg_base, sg_dout[i].sg_base, cur_size); break; } } DISPLAYLEVEL(1, "checked %i out of %i\n", (int)total_checked, decomp_result); } CHECK(crcDecoded != crcOrig, "Decompression corruption"); } else { // prepare compressedBuffer from SGL size_t total_copied = 0; for (i = 0; i < sg_cout_len; i++) { size_t buf_size_bytes = cSize - total_copied; if (buf_size_bytes == 0) break; if (buf_size_bytes > sg_cout[i].sg_len) buf_size_bytes = sg_cout[i].sg_len; if (((char *)compressedBuffer)+total_copied != sg_cout[i].sg_base) { memcpy(((char *)compressedBuffer)+total_copied, sg_cout[i].sg_base, buf_size_bytes); } total_copied += buf_size_bytes; } LZ4F_decompressOptions_t dOptions; memset(&dOptions, 0, sizeof(dOptions)); const BYTE* ip = (const BYTE*)compressedBuffer; const BYTE* const iend = ip + cSize; BYTE* op = (BYTE*)decodedBuffer; BYTE* const oend = op + srcDataLength; size_t totalOut = 0; unsigned maxBits = FUZ_highbit((U32)cSize); XXH64_reset(&xxh64, 1); if (maxBits < 3) maxBits = 3; while (ip < iend) { unsigned nbBitsI = (FUZ_rand(&randState) % (maxBits-1)) + 1; unsigned nbBitsO = (FUZ_rand(&randState) % (maxBits)) + 1; size_t iSize = (FUZ_rand(&randState) & ((1<<nbBitsI)-1)) + 1; size_t oSize = (FUZ_rand(&randState) & ((1<<nbBitsO)-1)) + 2; if (iSize > (size_t)(iend-ip)) iSize = iend-ip; if (oSize > (size_t)(oend-op)) oSize = oend-op; dOptions.stableDst = FUZ_rand(&randState) & 1; result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, &dOptions); if (result == (size_t)-LZ4F_ERROR_contentChecksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize); CHECK(LZ4F_isError(result), "Decompression failed (error %i:%s ip %d)", (int)result, LZ4F_getErrorName((LZ4F_errorCode_t)result), (int)(ip-(const BYTE*)compressedBuffer)); XXH64_update(&xxh64, op, (U32)oSize); totalOut += oSize; op += oSize; ip += iSize; } CHECK(result != 0, "Frame decompression failed (error %i)", (int)result); if (totalOut) /* otherwise, it's a skippable frame */ { crcDecoded = XXH64_digest(&xxh64); if (crcDecoded != crcOrig) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize); CHECK(crcDecoded != crcOrig, "Decompression corruption"); } } } DISPLAYLEVEL(2, "\rAll tests completed \n"); _end: LZ4F_freeDecompressionContext(dCtx); LZ4F_freeCompressionContext(cCtx); free(srcBuffer); free(compressedBuffer); free(decodedBuffer); for (i = 0; i < NELEMS(sg_in_buf_potential); i++) { free((void *)(sg_in_buf_potential [i].sg_base)); free( sg_out_buf_potential[i].sg_base); } if (num_buf_size_distribution_deviations > 0) { DISPLAYLEVEL(2, "NOTE: %i buffer size deviations \n", (int)num_buf_size_distribution_deviations); } if (pause) { DISPLAY("press enter to finish \n"); (void)getchar(); } return testResult; _output_error: testResult = 1; goto _end; // unreachable return -1; #undef CHECK }
static int usage_advanced(const char* programName) { DISPLAY(WELCOME_MESSAGE); usage(programName); DISPLAY( "\n"); DISPLAY( "Advanced arguments :\n"); DISPLAY( " -V : display Version number and exit\n"); DISPLAY( " -v : verbose mode\n"); DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n"); DISPLAY( " -c : force write to standard output, even if it is the console\n"); //DISPLAY( " -t : test compressed file integrity\n"); DISPLAY( "Benchmark arguments :\n"); DISPLAY( " -b : benchmark file(s)\n"); DISPLAY( " -B# : cut file into independent blocks of size # (default : no block)\n"); DISPLAY( " -i# : iteration loops [1-9](default : 3)\n"); return 0; }
int FUZ_usage(void) { DISPLAY( "Usage :\n"); DISPLAY( " %s [args]\n", programName); DISPLAY( "\n"); DISPLAY( "Arguments :\n"); DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); DISPLAY( " -T# : Duration of tests, in seconds (default: use Nb of tests) \n"); DISPLAY( " -s# : Select seed (default:prompt user)\n"); DISPLAY( " -t# : Select starting test number (default:0)\n"); DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); DISPLAY( " -v : verbose\n"); DISPLAY( " -h : display help and exit\n"); return 0; }
static void *thread_fun(int idx) { unsigned int loops = LOOPS; struct timespec abstime; char name[7]; sprintf(name, "TASK%d", idx); pthread_setschedparam_np(NTASKS - idx + 1, SCHED_FIFO, 0, 0x1, PTHREAD_HARD_REAL_TIME_NP); mlockall(MCL_CURRENT | MCL_FUTURE); RT_SET_REAL_TIME_MODE(); while(loops--) { DISPLAY("TASK %d 1 COND/TIMED PREWLOCKED\n", idx); clock_gettime(0, &abstime); abstime.tv_sec += 2; if (idx%2) { if (pthread_rwlock_trywrlock(rwl)) { DISPLAY("TASK %d 1 COND PREWLOCKED FAILED GO UNCOND\n", idx); pthread_rwlock_wrlock(rwl); } } else if (pthread_rwlock_timedwrlock(rwl, &abstime) >= SEM_TIMOUT) { DISPLAY("TASK %d 1 TIMED PREWLOCKED FAILED GO UNCOND\n", idx); pthread_rwlock_wrlock(rwl); } DISPLAY("TASK %d 1 WLOCKED\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 2 COND PREWLOCK\n", idx); if (pthread_rwlock_trywrlock(rwl)) { DISPLAY("TASK %d 2 COND PREWLOCK FAILED GO UNCOND\n", idx); pthread_rwlock_wrlock(rwl); } DISPLAY("TASK %d 2 WLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 3 PREWLOCK\n", idx); pthread_rwlock_wrlock(rwl); DISPLAY("TASK %d 3 WLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 3 PREWUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 3 WUNLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 2 PREWUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 2 WUNLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 1 PREWUNLOCKED\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 1 WUNLOCKED\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 1 COND/TIMED PRERDLOCKED\n", idx); clock_gettime(0, &abstime); abstime.tv_sec += 2; if (idx%2) { if (pthread_rwlock_tryrdlock(rwl)) { DISPLAY("TASK %d 1 COND PRERDLOCKED FAILED GO UNCOND\n", idx); pthread_rwlock_rdlock(rwl); } } else if (pthread_rwlock_timedrdlock(rwl, &abstime) >= SEM_TIMOUT) { DISPLAY("TASK %d 1 TIMED PRERDLOCKED FAILED GO UNCOND\n", idx); pthread_rwlock_rdlock(rwl); } DISPLAY("TASK %d 1 RDLOCKED\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 2 COND PRERDLOCK\n", idx); if (pthread_rwlock_tryrdlock(rwl)) { DISPLAY("TASK %d 2 COND PRERDLOCK FAILED GO UNCOND\n", idx); pthread_rwlock_rdlock(rwl); } DISPLAY("TASK %d 2 RDLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 3 PRERDLOCK\n", idx); pthread_rwlock_rdlock(rwl); DISPLAY("TASK %d 3 RDLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 3 PRERDUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 3 RDUNLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 2 PRERDUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 2 RDUNLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 1 PRERDUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 1 RDUNLOCK\n", idx); rt_busy_sleep(100000); } rt_make_soft_real_time(); pthread_barrier_wait(&barrier); DISPLAY("TASK %d EXITED\n", idx); return NULL; }
static void FUZ_tests (U32 seed, U32 totalTest, U32 startTestNb) { BYTE* bufferP0 = (BYTE*) malloc (BUFFERSIZE+64); BYTE* bufferP1 = (BYTE*) malloc (BUFFERSIZE+64); BYTE* bufferP15 = (BYTE*) malloc (BUFFERSIZE+64); BYTE* bufferP90 = (BYTE*) malloc (BUFFERSIZE+64); BYTE* bufferP100 = (BYTE*) malloc (BUFFERSIZE+64); BYTE* bufferDst = (BYTE*) malloc (BUFFERSIZE+64); BYTE* bufferVerif = (BYTE*) malloc (BUFFERSIZE+64); size_t const bufferDstSize = BUFFERSIZE+64; unsigned testNb; size_t const maxTestSizeMask = 0x1FFFF; /* 128 KB - 1 */ U32 rootSeed = seed; U32 time = FUZ_GetMilliStart(); generateNoise (bufferP0, BUFFERSIZE, &rootSeed); generate (bufferP1 , BUFFERSIZE, 0.01, &rootSeed); generate (bufferP15 , BUFFERSIZE, 0.15, &rootSeed); generate (bufferP90 , BUFFERSIZE, 0.90, &rootSeed); memset(bufferP100, (BYTE)FUZ_rand(&rootSeed), BUFFERSIZE); memset(bufferDst, 0, BUFFERSIZE); { U32 u; for (u=0; u<startTestNb; u++) FUZ_rand (&rootSeed); } for (testNb=startTestNb; testNb<totalTest; testNb++) { U32 roundSeed = rootSeed ^ 0xEDA5B371; FUZ_rand(&rootSeed); int tag=0; BYTE* bufferTest = NULL; DISPLAYLEVEL (4, "\r test %5u ", testNb); if (FUZ_GetMilliSpan (time) > FUZ_UPDATERATE) { DISPLAY ("\r test %5u ", testNb); time = FUZ_GetMilliStart(); } /* Compression / Decompression tests */ DISPLAYLEVEL (4,"%3i ", tag++); { /* determine test sample */ size_t const sizeOrig = (FUZ_rand(&roundSeed) & maxTestSizeMask) + 1; size_t const offset = (FUZ_rand(&roundSeed) % (BUFFERSIZE - 64 - maxTestSizeMask)); size_t sizeCompressed; U32 hashOrig; if (FUZ_rand(&roundSeed) & 7) bufferTest = bufferP15 + offset; else { switch(FUZ_rand(&roundSeed) & 3) { case 0: bufferTest = bufferP0 + offset; break; case 1: bufferTest = bufferP1 + offset; break; case 2: bufferTest = bufferP90 + offset; break; default : bufferTest = bufferP100 + offset; break; } } hashOrig = XXH32 (bufferTest, sizeOrig, 0); /* compression test */ sizeCompressed = HUF_compress (bufferDst, bufferDstSize, bufferTest, sizeOrig); CHECK(HUF_isError(sizeCompressed), "HUF_compress failed"); if (sizeCompressed > 1) { /* don't check uncompressed & rle corner cases */ /* failed compression test */ { BYTE const saved = bufferVerif[sizeCompressed-1] = 253; size_t const errorCode = HUF_compress (bufferVerif, sizeCompressed-1, bufferTest, sizeOrig); CHECK(errorCode!=0, "HUF_compress should have failed (too small destination buffer)") CHECK(bufferVerif[sizeCompressed-1] != saved, "HUF_compress w/ too small dst : bufferVerif overflow"); } /* decompression test */ { BYTE const saved = bufferVerif[sizeOrig] = 253; size_t const result = HUF_decompress (bufferVerif, sizeOrig, bufferDst, sizeCompressed); CHECK(bufferVerif[sizeOrig] != saved, "HUF_decompress : bufferVerif overflow"); CHECK(HUF_isError(result), "HUF_decompress failed : %s", HUF_getErrorName(result)); { U32 const hashEnd = XXH32 (bufferVerif, sizeOrig, 0); if (hashEnd!=hashOrig) findDifferentByte(bufferVerif, sizeOrig, bufferTest, sizeOrig); CHECK(hashEnd != hashOrig, "HUF_decompress : Decompressed data corrupted"); } } /* quad decoder test (more fragile) */ /* if (sizeOrig > 64) { BYTE const saved = bufferVerif[sizeOrig] = 253; size_t const result = HUF_decompress4X6 (bufferVerif, sizeOrig, bufferDst, sizeCompressed); CHECK(bufferVerif[sizeOrig] != saved, "HUF_decompress4X6 : bufferVerif overflow"); CHECK(HUF_isError(result), "HUF_decompress4X6 failed : %s", HUF_getErrorName(result)); { U32 const hashEnd = XXH32 (bufferVerif, sizeOrig, 0); if (hashEnd!=hashOrig) findDifferentByte(bufferVerif, sizeOrig, bufferTest, sizeOrig); CHECK(hashEnd != hashOrig, "HUF_decompress4X6 : Decompressed data corrupted"); } } */ /* truncated src decompression test */ if (sizeCompressed>4) { /* note : in some rare cases, the truncated bitStream may still generate by chance a valid output of correct size */ size_t const missing = (FUZ_rand(&roundSeed) % (sizeCompressed-3)) + 2; /* no problem, as sizeCompressed > 4 */ size_t const tooSmallSize = sizeCompressed - missing; void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */ CHECK(cBufferTooSmall == NULL, "not enough memory !"); memcpy(cBufferTooSmall, bufferDst, tooSmallSize); { size_t const errorCode = HUF_decompress(bufferVerif, sizeOrig, cBufferTooSmall, tooSmallSize); CHECK(!HUF_isError(errorCode) && (errorCode!=sizeOrig), "HUF_decompress should have failed ! (truncated src buffer)"); } free(cBufferTooSmall); } } } /* Compression / Decompression tests */ /* Attempt decompression on bogus data */ { size_t const maxDstSize = FUZ_rand (&roundSeed) & maxTestSizeMask; size_t const sizeCompressed = FUZ_rand (&roundSeed) & maxTestSizeMask; BYTE const saved = (bufferDst[maxDstSize] = 253); size_t result; DISPLAYLEVEL (4,"\b\b\b\b%3i ", tag++);; result = HUF_decompress (bufferDst, maxDstSize, bufferTest, sizeCompressed); CHECK(!HUF_isError(result) && (result > maxDstSize), "Decompression overran output buffer"); CHECK(bufferDst[maxDstSize] != saved, "HUF_decompress noise : bufferDst overflow"); } } /* for (testNb=startTestNb; testNb<totalTest; testNb++) */ /* exit */ free (bufferP0); free (bufferP1); free (bufferP15); free (bufferP90); free (bufferP100); free (bufferDst); free (bufferVerif); }