Lines Matching refs:context

679 static BROTLI_BOOL NextFile(Context* context) {
684 context->iterator++;
686 context->input_file_length = -1;
689 if (context->input_count == 0) {
690 if (context->iterator > 1) return BROTLI_FALSE;
691 context->current_input_path = NULL;
693 context->current_output_path = context->output_path;
698 while (context->iterator == context->not_input_indices[context->ignore]) {
699 context->iterator++;
700 context->ignore++;
704 if (context->iterator >= context->argc) return BROTLI_FALSE;
707 arg = context->argv[context->iterator];
711 context->current_input_path = NULL;
712 context->current_output_path = context->output_path;
716 context->current_input_path = arg;
717 context->input_file_length = FileSize(arg);
718 context->current_output_path = context->output_path;
720 if (context->output_path) return BROTLI_TRUE;
721 if (context->write_to_stdout) return BROTLI_TRUE;
723 strcpy(context->modified_path, arg);
724 context->current_output_path = context->modified_path;
726 if (context->decompress) {
727 size_t suffix_len = strlen(context->suffix);
728 char* name = (char*)FileName(context->modified_path);
734 context->iterator_error = BROTLI_TRUE;
738 if (strcmp(context->suffix, name_suffix) != 0) {
741 context->iterator_error = BROTLI_TRUE;
747 strcpy(context->modified_path + arg_len, context->suffix);
752 static BROTLI_BOOL OpenFiles(Context* context) {
753 BROTLI_BOOL is_ok = OpenInputFile(context->current_input_path, &context->fin);
754 if (!context->test_integrity && is_ok) {
756 context->current_output_path, &context->fout, context->force_overwrite);
761 static BROTLI_BOOL CloseFiles(Context* context, BROTLI_BOOL success) {
763 if (!context->test_integrity && context->fout) {
764 if (!success && context->current_output_path) {
765 unlink(context->current_output_path);
767 if (fclose(context->fout) != 0) {
770 PrintablePath(context->current_output_path), strerror(errno));
776 if (success && is_ok && context->copy_stat) {
777 CopyStat(context->current_input_path, context->current_output_path);
781 if (context->fin) {
782 if (fclose(context->fin) != 0) {
785 PrintablePath(context->current_input_path), strerror(errno));
790 if (success && context->junk_source && context->current_input_path) {
791 unlink(context->current_input_path);
794 context->fin = NULL;
795 context->fout = NULL;
802 static void InitializeBuffers(Context* context) {
803 context->available_in = 0;
804 context->next_in = NULL;
805 context->available_out = kFileBufferSize;
806 context->next_out = context->output;
807 context->total_in = 0;
808 context->total_out = 0;
813 static BROTLI_BOOL HasMoreInput(Context* context) {
814 return feof(context->fin) ? BROTLI_FALSE : BROTLI_TRUE;
817 static BROTLI_BOOL ProvideInput(Context* context) {
818 context->available_in =
819 fread(context->input, 1, kFileBufferSize, context->fin);
820 context->total_in += context->available_in;
821 context->next_in = context->input;
822 if (ferror(context->fin)) {
824 PrintablePath(context->current_input_path), strerror(errno));
831 static BROTLI_BOOL WriteOutput(Context* context) {
832 size_t out_size = (size_t)(context->next_out - context->output);
833 context->total_out += out_size;
835 if (context->test_integrity) return BROTLI_TRUE;
837 fwrite(context->output, 1, out_size, context->fout);
838 if (ferror(context->fout)) {
840 PrintablePath(context->current_output_path), strerror(errno));
846 static BROTLI_BOOL ProvideOutput(Context* context) {
847 if (!WriteOutput(context)) return BROTLI_FALSE;
848 context->available_out = kFileBufferSize;
849 context->next_out = context->output;
853 static BROTLI_BOOL FlushOutput(Context* context) {
854 if (!WriteOutput(context)) return BROTLI_FALSE;
855 context->available_out = 0;
871 static void PrintFileProcessingProgress(Context* context) {
872 fprintf(stderr, "[%s]: ", PrintablePath(context->current_input_path));
873 PrintBytes(context->total_in);
875 PrintBytes(context->total_out);
878 static BROTLI_BOOL DecompressFile(Context* context, BrotliDecoderState* s) {
880 InitializeBuffers(context);
883 if (!HasMoreInput(context)) {
885 PrintablePath(context->current_input_path));
888 if (!ProvideInput(context)) return BROTLI_FALSE;
890 if (!ProvideOutput(context)) return BROTLI_FALSE;
892 if (!FlushOutput(context)) return BROTLI_FALSE;
894 (context->available_in != 0) || (fgetc(context->fin) != EOF);
897 PrintablePath(context->current_input_path));
900 if (context->verbosity > 0) {
902 PrintFileProcessingProgress(context);
908 PrintablePath(context->current_input_path));
912 result = BrotliDecoderDecompressStream(s, &context->available_in,
913 &context->next_in, &context->available_out, &context->next_out, 0);
917 static BROTLI_BOOL DecompressFiles(Context* context) {
918 while (NextFile(context)) {
929 is_ok = OpenFiles(context);
930 if (is_ok && !context->current_input_path &&
931 !context->force_overwrite && isatty(STDIN_FILENO)) {
935 if (is_ok) is_ok = DecompressFile(context, s);
937 if (!CloseFiles(context, is_ok)) is_ok = BROTLI_FALSE;
943 static BROTLI_BOOL CompressFile(Context* context, BrotliEncoderState* s) {
945 InitializeBuffers(context);
947 if (context->available_in == 0 && !is_eof) {
948 if (!ProvideInput(context)) return BROTLI_FALSE;
949 is_eof = !HasMoreInput(context);
954 &context->available_in, &context->next_in,
955 &context->available_out, &context->next_out, NULL)) {
958 PrintablePath(context->current_input_path));
962 if (context->available_out == 0) {
963 if (!ProvideOutput(context)) return BROTLI_FALSE;
967 if (!FlushOutput(context)) return BROTLI_FALSE;
968 if (context->verbosity > 0) {
970 PrintFileProcessingProgress(context);
978 static BROTLI_BOOL CompressFiles(Context* context) {
979 while (NextFile(context)) {
987 BROTLI_PARAM_QUALITY, (uint32_t)context->quality);
988 if (context->lgwin > 0) {
991 if (context->lgwin > BROTLI_MAX_WINDOW_BITS) {
995 BROTLI_PARAM_LGWIN, (uint32_t)context->lgwin);
1000 if (context->input_file_length >= 0) {
1003 (uint64_t)context->input_file_length) {
1010 if (context->input_file_length > 0) {
1011 uint32_t size_hint = context->input_file_length < (1 << 30) ?
1012 (uint32_t)context->input_file_length : (1u << 30);
1015 is_ok = OpenFiles(context);
1016 if (is_ok && !context->current_output_path &&
1017 !context->force_overwrite && isatty(STDOUT_FILENO)) {
1021 if (is_ok) is_ok = CompressFile(context, s);
1023 if (!CloseFiles(context, is_ok)) is_ok = BROTLI_FALSE;
1031 Context context;
1035 context.quality = 11;
1036 context.lgwin = -1;
1037 context.verbosity = 0;
1038 context.force_overwrite = BROTLI_FALSE;
1039 context.junk_source = BROTLI_FALSE;
1040 context.copy_stat = BROTLI_TRUE;
1041 context.test_integrity = BROTLI_FALSE;
1042 context.write_to_stdout = BROTLI_FALSE;
1043 context.decompress = BROTLI_FALSE;
1044 context.large_window = BROTLI_FALSE;
1045 context.output_path = NULL;
1046 context.suffix = DEFAULT_SUFFIX;
1047 for (i = 0; i < MAX_OPTIONS; ++i) context.not_input_indices[i] = 0;
1048 context.longest_path_len = 1;
1049 context.input_count = 0;
1051 context.argc = argc;
1052 context.argv = argv;
1053 context.modified_path = NULL;
1054 context.iterator = 0;
1055 context.ignore = 0;
1056 context.iterator_error = BROTLI_FALSE;
1057 context.buffer = NULL;
1058 context.current_input_path = NULL;
1059 context.current_output_path = NULL;
1060 context.fin = NULL;
1061 context.fout = NULL;
1063 command = ParseParams(&context);
1069 context.longest_path_len + strlen(context.suffix) + 1;
1070 context.modified_path = (char*)malloc(modified_path_len);
1071 context.buffer = (uint8_t*)malloc(kFileBufferSize * 2);
1072 if (!context.modified_path || !context.buffer) {
1076 context.input = context.buffer;
1077 context.output = context.buffer + kFileBufferSize;
1093 is_ok = CompressFiles(&context);
1098 is_ok = DecompressFiles(&context);
1109 if (context.iterator_error) is_ok = BROTLI_FALSE;
1111 free(context.modified_path);
1112 free(context.buffer);