Coalesce into build_enc()

This commit is contained in:
John Whitington 2023-10-30 17:55:19 +00:00
parent d26e6ed8f1
commit 14ac303523
1 changed files with 42 additions and 77 deletions

View File

@ -3290,6 +3290,23 @@ let check_clashing_output_name () =
Pdfe.log "Warning: output file name clashes with input file name. Malformed file may result.\n" Pdfe.log "Warning: output file name clashes with input file name. Malformed file may result.\n"
| _ -> () | _ -> ()
let build_enc () =
match args.crypt_method with
| "" -> None
| _ ->
Some
{Pdfwrite.encryption_method =
(match args.crypt_method with
| "40bit" -> Pdfwrite.PDF40bit
| "128bit" -> Pdfwrite.PDF128bit
| "AES" -> Pdfwrite.AES128bit args.encrypt_metadata
| "AES256" -> Pdfwrite.AES256bit args.encrypt_metadata
| "AES256ISO" -> Pdfwrite.AES256bitISO args.encrypt_metadata
| _ -> assert false (* Pre-checked *));
Pdfwrite.owner_password = args.owner;
Pdfwrite.user_password = args.user;
Pdfwrite.permissions = banlist_of_args ()}
(* Main function *) (* Main function *)
let go () = let go () =
check_bookmarks_mistake (); check_bookmarks_mistake ();
@ -3710,23 +3727,7 @@ let go () =
begin match args.inputs, args.out with begin match args.inputs, args.out with
| [(f, ranges, _, _, _, _)], File output_spec -> | [(f, ranges, _, _, _, _)], File output_spec ->
let pdf = get_single_pdf args.op true in let pdf = get_single_pdf args.op true in
let enc = let enc = build_enc () in
match args.crypt_method with
| "" -> None
| _ ->
Some
{Pdfwrite.encryption_method =
(match args.crypt_method with
| "40bit" -> Pdfwrite.PDF40bit
| "128bit" -> Pdfwrite.PDF128bit
| "AES" -> Pdfwrite.AES128bit args.encrypt_metadata
| "AES256" -> Pdfwrite.AES256bit args.encrypt_metadata
| "AES256ISO" -> Pdfwrite.AES256bitISO args.encrypt_metadata
| _ -> assert false (* Pre-checked *));
Pdfwrite.owner_password = args.owner;
Pdfwrite.user_password = args.user;
Pdfwrite.permissions = banlist_of_args ()}
in
args.create_objstm <- args.preserve_objstm; args.create_objstm <- args.preserve_objstm;
split_pdf split_pdf
enc args.original_filename args.chunksize args.linearize ~cpdflin:args.cpdflin enc args.original_filename args.chunksize args.linearize ~cpdflin:args.cpdflin
@ -3740,23 +3741,7 @@ let go () =
begin match args.out with begin match args.out with
| File output_spec -> | File output_spec ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let enc = let enc = build_enc () in
match args.crypt_method with
| "" -> None
| _ ->
Some
{Pdfwrite.encryption_method =
(match args.crypt_method with
| "40bit" -> Pdfwrite.PDF40bit
| "128bit" -> Pdfwrite.PDF128bit
| "AES" -> Pdfwrite.AES128bit args.encrypt_metadata
| "AES256" -> Pdfwrite.AES256bit args.encrypt_metadata
| "AES256ISO" -> Pdfwrite.AES256bitISO args.encrypt_metadata
| _ -> assert false (* Pre-checked *));
Pdfwrite.owner_password = args.owner;
Pdfwrite.user_password = args.user;
Pdfwrite.permissions = banlist_of_args ()}
in
args.create_objstm <- args.preserve_objstm; args.create_objstm <- args.preserve_objstm;
split_at_bookmarks split_at_bookmarks
enc args.original_filename ~squeeze:args.squeeze level output_spec pdf enc args.original_filename ~squeeze:args.squeeze level output_spec pdf
@ -3767,31 +3752,23 @@ let go () =
begin match args.out with begin match args.out with
| File output_spec -> | File output_spec ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let enc = let enc = build_enc () in
match args.crypt_method with
| "" -> None
| _ ->
Some
{Pdfwrite.encryption_method =
(match args.crypt_method with
| "40bit" -> Pdfwrite.PDF40bit
| "128bit" -> Pdfwrite.PDF128bit
| "AES" -> Pdfwrite.AES128bit args.encrypt_metadata
| "AES256" -> Pdfwrite.AES256bit args.encrypt_metadata
| "AES256ISO" -> Pdfwrite.AES256bitISO args.encrypt_metadata
| _ -> assert false (* Pre-checked *));
Pdfwrite.owner_password = args.owner;
Pdfwrite.user_password = args.user;
Pdfwrite.permissions = banlist_of_args ()}
in
args.create_objstm <- args.preserve_objstm; args.create_objstm <- args.preserve_objstm;
split_max enc args.original_filename ~squeeze:args.squeeze output_spec s pdf split_max enc args.original_filename ~squeeze:args.squeeze output_spec s pdf
| Stdout -> error "Can't split to standard output" | Stdout -> error "Can't split to standard output"
| NoOutputSpecified -> error "Split: No output format specified" | NoOutputSpecified -> error "Split: No output format specified"
end end
| Some Spray -> | Some Spray ->
() begin match args.out with
(* FIXME First deduplicate the enc stuff in this and previous entries *) | File output_spec ->
let pdf = get_single_pdf args.op false in
let enc = build_enc () in
args.create_objstm <- args.preserve_objstm;
ignore pdf;
ignore enc
| Stdout -> error "Can't spray to standard output"
| NoOutputSpecified -> error "Spray: No output format specified"
end
| Some Presentation -> | Some Presentation ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in
@ -4065,19 +4042,7 @@ let go () =
| Some Encrypt -> | Some Encrypt ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let pdf = Cpdfsqueeze.recompress_pdf pdf let pdf = Cpdfsqueeze.recompress_pdf pdf
and encryption = and encryption = build_enc () in
{Pdfwrite.encryption_method =
(match args.crypt_method with
| "40bit" -> Pdfwrite.PDF40bit
| "128bit" -> Pdfwrite.PDF128bit
| "AES" -> Pdfwrite.AES128bit args.encrypt_metadata
| "AES256" -> Pdfwrite.AES256bit args.encrypt_metadata
| "AES256ISO" -> Pdfwrite.AES256bitISO args.encrypt_metadata
| _ -> assert false (* Pre-checked *));
Pdfwrite.owner_password = args.owner;
Pdfwrite.user_password = args.user;
Pdfwrite.permissions = banlist_of_args ()}
in
Pdf.remove_unreferenced pdf; Pdf.remove_unreferenced pdf;
if not args.keepversion then if not args.keepversion then
begin begin
@ -4088,7 +4053,7 @@ let go () =
let newversion = if args.create_objstm then 5 else newversion in let newversion = if args.create_objstm then 5 else newversion in
if pdf.Pdf.major = 1 then pdf.Pdf.minor <- max pdf.Pdf.minor newversion if pdf.Pdf.major = 1 then pdf.Pdf.minor <- max pdf.Pdf.minor newversion
end; end;
write_pdf ~encryption:(Some encryption) false pdf write_pdf ~encryption false pdf
| Some Decrypt -> | Some Decrypt ->
args.recrypt <- false; args.recrypt <- false;
write_pdf false (get_single_pdf args.op false) write_pdf false (get_single_pdf args.op false)