text
stringlengths
12
786k
let string_of_privatedict_entry = function | Subrs offset -> " Subrs : offset is " ^ string_of_int offset ^ " \ n "
let dbg = ref false
let read_encoding b = function | 0 -> if ! dbg then flprint " read_encoding : This font uses the standard encoding \ n " ; standard_encoding , false | 1 -> if ! dbg then flprint " read_encoding : This font uses the expert encoding \ n " ; expert_encoding , false | off -> if ! dbg then fl...
let read_charset b off numglyphs = assert ( numglyphs >= 0 ) ; discard_bytes b off ; let fmt = read_card8 b in match fmt with | 0 -> if ! dbg then flprint " Format 0 \ n " ; let sids = ref [ ] and glyphsleft = ref numglyphs in while ! glyphsleft > 0 do sids =| read_card16 b ; decr gl...
let read_charstrings b charstringtype off = discard_bytes b off ; map ( read_string b ) ( read_index b )
type charstring_elt = | CSOperator of int | CSOperator2 of int | CSInt of int32 | CSEndHere
let string_of_charstring_elt = function | CSOperator i -> Printf . sprintf " CSOperator : % i \ n " i | CSOperator2 i -> Printf . sprintf " CSOperator2 : % i \ n " i | CSInt i -> Printf . sprintf " CSInt % li \ n " i | CSEndHere -> " CSEndHere \ n "
let string_of_charstring elts = fold_left ( ^ ) " " ( map string_of_charstring_elt elts )
let lex_charstring_elt hintbits i = let b = i . input_byte ( ) in if b = Pdfio . no_more then raise End_of_file else if b < 11 then CSOperator b else if b = 12 then let b1 = i . input_byte ( ) in CSOperator2 b1 else if b <= 18 then CSOperator b else if b <= 20 then if hintbits = - 1 then CS...
let count_hintbits i = let hintbits = ref 0 and stk = ref [ ] and hint_ops = ref [ ] in try while true do match lex_charstring_elt ~- 1 i with | CSOperator ( ( 19 | 1 | 3 | 18 | 23 ) as p ) -> hint_ops =| p ; hintbits += length ! stk / 2 ; stk := [ ] | CSEndHere when ( ...
let lex_charstring s = let elts = ref [ ] and i = input_of_string s in let hintbits = count_hintbits i in let i = input_of_string s in try while true do elts =| lex_charstring_elt hintbits i done ; [ ] with _ -> rev ! elts
let mflprint x = ( )
let rec parse_charstring_inner ( cx , cy ) stk prev thing = match thing with | CSOperator 14 :: _ | [ ] -> mflprint " op : endchar \ n " ; if prev = [ ] then [ ] else rev ( Pdfops . Op_f :: prev ) | CSOperator 21 :: t -> mflprint " op : rmoveto \ n " ; begin match st...
let parse_charstring lexemes = try parse_charstring_inner ( 0 . , 0 . ) [ ] [ ] lexemes with e -> raise e
let debug_letter ops = let page = Pdfpage . blankpage Pdfpaper . a4 in let page = { page with Pdfpage . content = let trop = Pdfops . Op_cm ( Pdftransform . matrix_of_op ( Pdftransform . Scale ( ( 0 . , 0 . ) , 0 . 1 , 0 . 1 ) ) ) in [ Pdfops . stream_of_ops ( ...
let notdef = { Pdfgraphics . elements = [ ] ; Pdfgraphics . fonts = [ ] ; Pdfgraphics . resources = Pdf . Dictionary [ ] }
let parse_cff_font data = let get_bitstream ( ) = bitbytes_of_input ( input_of_bytes data ) in let b = get_bitstream ( ) in let _ = read_card8 b in let _ = read_card8 b in let header_size = read_card8 b in let _ = read_card8 b * 4 in discard_bytes b ( header_size - 4 ) ; if ! dbg then P...
let charprocbytes_of_graphic g matrix = Pdfops . stream_of_ops ( Pdfops . Op_d1 ( 0 . , 0 . , 0 . , 0 . , 0 . , 0 . ) :: lose ( function | Pdfops . Op_q | Pdfops . Op_Q | Pdfops . Op_cm _ | Pdfops . Op_w _ | Pdfops . Op_J _ | Pdfops . Op_j _ | Pdfops . ...
let bbox_of_graphics gs = fold_left box_union_float ( max_float , min_float , max_float , min_float ) ( map Pdfgraphics . bbox_of_graphic gs )
let to_type3 pdf = function | Pdftext . SimpleFont ( { Pdftext . fonttype = Pdftext . Type1 ; Pdftext . encoding = original_encoding ; Pdftext . fontdescriptor = Some ( { Pdftext . fontfile = Some Pdftext . FontFile3 fontfileobj } as fontdescriptor ) } as fontrec ) -> begin try ...
let debug = ref false
let flate_level = ref 6
let rec get_streamchar skipped s = match s . input_byte ( ) with | x when x = Pdfio . no_more -> raise End_of_file | x -> let chr = Char . unsafe_chr x in if Pdf . is_whitespace chr then begin incr skipped ; get_streamchar skipped s end else chr
let encode_ASCIIHex stream = let size = bytes_size stream in let stream ' = mkbytes ( size * 2 + 1 ) in bset stream ' ( size * 2 ) ( int_of_char ' ' ) ; > for p = 0 to size - 1 do let chars = explode ( Printf . sprintf " % 02X " ( bget stream p ) ) in bset stream ' ...
let char_of_hex a b = char_of_int ( int_of_string ( " 0x " ^ string_of_char a ^ string_of_char b ) )
let decode_ASCIIHex i = let output = ref [ ] in let enddata = ref false in try while not ! enddata do let b = get_streamchar ( ref 0 ) i in let b ' = get_streamchar ( ref 0 ) i in match b , b ' with | ' ' , > _ -> set enddata ; rewind i | c , ' ' > when ( c >= ' 0 ' ...
let decode_5bytes c1 c2 c3 c4 c5 n o = let d x p = i32mul ( i32ofi ( int_of_char x - 33 ) ) ( i32ofi ( pow p 85 ) ) in let total = i32add ( i32add ( d c1 4 ) ( d c2 3 ) ) ( i32add ( d c3 2 ) ( i32add ( d c4 1 ) ( d c5 0 ) ) ) in let extract t = char_of_int ( ...
let conso cs o = match cs with | [ c1 ; c2 ; c3 ; c4 ; c5 ] -> decode_5bytes c1 c2 c3 c4 c5 4 o | [ c1 ; c2 ; c3 ; c4 ] -> decode_5bytes c1 c2 c3 c4 ' ' ~ 3 o | [ c1 ; c2 ; c3 ] -> decode_5bytes c1 c2 c3 ' ' ~ ' ' > 2 o | [ c1 ; c2 ] -> decode_5bytes c1 c2 ...
let rec decode_ASCII85_i i cs o = match i . input_byte ( ) with | x when x = Pdfio . no_more -> raise End_of_file | x -> match char_of_int x with | c when Pdf . is_whitespace c -> if length cs = 5 then decode_ASCII85_i i [ ] ( conso ( rev cs ) o ) else decode_ASCII85_i i cs o | ' z ' ...
let decode_ASCII85 i = try decode_ASCII85_i i [ ] [ ] with _ -> raise ( Pdf . PDFError " Error decoding ASCII85 stream " )
let encode_4bytes = function | [ b1 ; b2 ; b3 ; b4 ] -> let ( * ) = Int64 . mul in let ( - ) = Int64 . sub in let ( / ) = Int64 . div in let rem = Int64 . rem in let numbers = [ i64ofi ( int_of_char b1 ) * i64ofi ( pow 3 256 ) ; i64ofi ( int_of_char b2 ) * i6...
let encode_ASCII85 stream = let output = ref [ ] in let enddata = ref false in let istream = input_of_bytes stream in while not ! enddata do let b1 = istream . input_char ( ) in let b2 = istream . input_char ( ) in let b3 = istream . input_char ( ) in let b4 = istream . input_char ( )...
let rec total_length n = function | [ ] -> n | h :: t -> total_length ( n + String . length h ) t
let bytes_of_strings_rev strings = let len = total_length 0 strings in let s = mkbytes len and pos = ref ( len - 1 ) in iter ( fun str -> for x = String . length str - 1 downto 0 do bset_unsafe s ! pos ( int_of_char ( String . unsafe_get str x ) ) ; decr pos done ) strings ; s
let flate_process f data = let strings = ref [ ] and pos = ref 0 and inlength = bytes_size data in let input = ( fun buf -> let s = Bytes . length buf in let towrite = min ( inlength - ! pos ) s in for x = 0 to towrite - 1 do Bytes . unsafe_set buf x ( Char . unsafe_chr ( bget_unsafe ...
let decode_flate_input i = let strings = ref [ ] in let input = ( fun buf -> let s = Bytes . length buf in if s > 0 then begin match i . input_byte ( ) with | x when x = Pdfio . no_more -> raise End_of_file | x -> Bytes . unsafe_set buf 0 ( char_of_int x ) ; 1 end else 0 ) and o...
let is_js = match Sys . backend_type with Sys . Other " js_of_ocaml " -> true | _ -> false
let encode_flate stream = if is_js then Pdfio . bytes_of_string ( camlpdf_caml_zlib_compress ( Pdfio . string_of_bytes stream ) ) else flate_process ( Pdfflate . compress ~ level :! flate_level ) stream
let debug_stream_serial = ref 0
let debug_stream s = let name = string_of_int ( bytes_size s ) ^ " _ " ^ string_of_int ! debug_stream_serial ^ " . zlib " in Printf . eprintf " Writing % s \ n " %! name ; debug_stream_serial += 1 ; let fh = open_out_bin name in for x = 0 to bytes_size s - 1 do output_byte fh ( ...
let decode_flate stream = if bytes_size stream = 0 then mkbytes 0 else try if is_js then Pdfio . bytes_of_string ( camlpdf_caml_zlib_decompress ( Pdfio . string_of_bytes stream ) ) else flate_process Pdfflate . uncompress stream with Pdfflate . Error ( a , b ) -> if ! debug then debug_stream...
let decode_lzw early i = let prefix_code = Array . make 4096 0 and append_character = Array . make 4096 0 and bit_count = ref 0 and bit_buffer = ref 0l and endflush = ref 4 and code_length = ref 9 and next_code = ref 258 and new_code = ref 0 and old_code = ref 256 and character = ref 0 in let...
let rec read_white_code i = let a = getbitint i in let b = getbitint i in let c = getbitint i in let d = getbitint i in match a , b , c , d with | 0 , 1 , 1 , 1 -> 2 | 1 , 0 , 0 , 0 -> 3 | 1 , 0 , 1 , 1 -> 4 | 1 , 1 , 0 , 0 -> 5 | 1 , 1 , 1 , 0 ...
let rec read_black_code i = let a = getbitint i in let b = getbitint i in match a , b with | 1 , 1 -> 2 | 1 , 0 -> 3 | _ -> let c = getbitint i in match a , b , c with | 0 , 1 , 0 -> 1 | 0 , 1 , 1 -> 4 | _ -> let d = getbitint i in match a , b , c , d with | 0 ...
type modes = | Pass | Horizontal | Vertical of int | Uncompressed | EOFB
let read_mode i = let a = getbitint i in match a with | 1 -> Vertical 0 | _ -> let b = getbitint i in let c = getbitint i in match a , b , c with | 0 , 1 , 1 -> Vertical ( - 1 ) | 0 , 1 , 0 -> Vertical 1 | 0 , 0 , 1 -> Horizontal | _ -> let d = getbitint i in match a...
let decode_CCITTFax k eol eba c r eob bone dra input = if k > 0 then raise ( DecodeNotSupported " CCITTFax k > 0 " ) else let whiteval , blackval = if bone then 0 , 1 else 1 , 0 in let output = make_write_bitstream ( ) in let b = bitbytes_of_input input in let column = ref 0 in let row ...
let get0 a i = if i < 0 then 0 else a . ( i )
let decode_tiff_predictor colors bpc columns stream = match bpc with | 1 -> raise ( DecodeNotSupported " TIFF Predictor for 1bpc not supported " ) | 2 -> raise ( DecodeNotSupported " TIFF Predictor for 2bpc not supported " ) | 4 -> raise ( DecodeNotSupported " TIFF Predictor for 4bpc no...
let decode_scanline_pair prior_encoded prior_decoded current pred bpc cols = let output = Array . copy current in begin match pred with | 0 -> ( ) | 1 -> for x = 0 to Array . length output - 1 do output . ( x ) <- ( get0 current x + get0 output ( x - cols ) ) mod 256 done | 2 ->...
let decode_predictor pred colors bpc columns stream = if pred = 2 then decode_tiff_predictor colors bpc columns stream else let i = input_of_bytes stream in let scanline_width = ( colors * bpc * columns + 7 ) / 8 in let blank ( ) = ref ( Array . make scanline_width 0 ) in let prev , curr , ...
let encode_predictor pred colors bpc columns stream = if pred = 12 then begin let get0 a i = if i < 0 then 0 else bget a i in let o , bytes = Pdfio . input_output_of_bytes 4096 in for scanline = 0 to bytes_size stream / columns - 1 do o . Pdfio . output_byte 2 ; for byte = 0 to columns - 1 ...
let encode_runlength stream = let i = input_of_bytes stream in let data_in = ref [ ] in begin try while true do data_in =| begin match i . input_byte ( ) with | x when x = Pdfio . no_more -> raise End_of_file | x -> x end done with End_of_file -> data_in := rev ! data_in end ; let rec runs_of_...
let decode_runlength i = let o , data = input_output_of_bytes 4096 in let eod = ref false in begin try while not ! eod do let l = match i . input_byte ( ) with | x when x = Pdfio . no_more -> raise End_of_file | x -> x in if l < 128 then for x = 1 to l + 1 do o . output_byte begin match i ....
type source = | StreamSource of bytes | InputSource of input
let decoder pdf dict source name = let input_of_source = function | InputSource i -> i | StreamSource s -> input_of_bytes s in let i = input_of_source source in match name with | " / ASCIIHexDecode " | " / AHx " -> decode_ASCIIHex i | " / ASCII85Decode " | " / A85 " -> decode_ASCII85 i ...
let decode_one pdf dict source = match Pdf . lookup_direct_orelse pdf " / Filter " " / F " dict with | None | Some ( Pdf . Array [ ] ) -> begin match source with | StreamSource s -> s | InputSource _ -> raise ( DecodeNotSupported " decode_one " ) end | Some ( Pdf . Name n ) ...
let prepare_decoder pdf d = map ( function ( ( " / Filter " | " / F " | " / DecodeParms " | " / DP " ) as k , v ) -> k , Pdf . direct pdf v | x -> x ) d
let remove_decoder d = let d ' = match lookup " / Filter " d , lookup " / F " d with | None , None -> d | Some ( Pdf . Name _ | Pdf . Array [ _ ] ) , None -> lose ( fun ( n , _ ) -> n = " / Filter " ) d | None , Some ( Pdf . Name _ | Pdf . Array [ _ ...
let decode_pdfstream_onestage pdf stream = Pdf . getstream stream ; match stream with | Pdf . Stream ( { contents = ( Pdf . Dictionary d as dict , Pdf . Got s ) } as stream_contents ) -> begin match Pdf . direct pdf ( Pdf . lookup_fail " no / Length " pdf " / Length " dict ) ...
let string_of_pdf = ref ( fun _ -> " " )
let rec decode_pdfstream pdf = function | Pdf . Stream { contents = d , _ } as stream -> Pdf . getstream stream ; begin match Pdf . lookup_direct_orelse pdf " / Filter " " / F " d with | None -> ( ) | Some ( Pdf . Name _ | Pdf . Array _ ) -> begin decode_pdfstream_onestag...
let decode_pdfstream_until_unknown pdf s = try decode_pdfstream pdf s with DecodeNotSupported _ -> ( )
let decode_from_input i dict = match Pdf . lookup_direct_orelse ( Pdf . empty ( ) ) " / F " " / Filter " dict with | Some ( Pdf . Name _ ) -> Some ( decode_one ( Pdf . empty ( ) ) dict ( InputSource i ) ) | Some ( Pdf . Array ( _ :: t ) ) -> let stream = ...
type encoding = | ASCIIHex | ASCII85 | RunLength | Flate
type predictor = TIFF2 | PNGNone | PNGSub | PNGUp | PNGAverage | PNGPaeth | PNGOptimum
let name_of_encoding = function | ASCIIHex -> " / ASCIIHexDecode " | ASCII85 -> " / ASCII85Decode " | RunLength -> " / RunLengthDecode " | Flate -> " / FlateDecode "
let add_encoding length pdf encoding d = let filter ' = match Pdf . lookup_direct pdf " / Filter " d with | None -> Pdf . Name ( name_of_encoding encoding ) | Some ( Pdf . Name n ) -> Pdf . Array ( Pdf . Name ( name_of_encoding encoding ) [ :: Pdf . Name n ] ) | Some ( P...
let encoder_of_encoding = function | ASCIIHex -> encode_ASCIIHex | ASCII85 -> encode_ASCII85 | RunLength -> encode_runlength | Flate -> encode_flate
let process_prediction_data predictor predictor_columns d = match predictor with PNGUp -> encode_predictor 12 1 8 predictor_columns d | _ -> raise ( Pdf . PDFError " process_predction_data " )
let process_prediction predictor predictor_columns stream = match stream with { contents = d , Pdf . Got s } -> begin match predictor with Some PNGUp -> let data = process_prediction_data PNGUp predictor_columns s and d ' = let decodeparms = Pdf . Dictionary [ ( " / Columns " , Pdf . Int...
let encode_pdfstream pdf encoding ? predictor ( ? predictor_columns = 1 ) stream = Pdf . getstream stream ; match stream with | Pdf . Stream ( { contents = d , Pdf . Got s } as stream ) -> let d ' , predicted = if predictor <> None then process_prediction predictor predictor_columns ...
let crypt_debug = ref false
let paddings = [ | 0x28 ; 0xbf ; 0x4e ; 0x5e ; 0x4e ; 0x75 ; 0x8a ; 0x41 ; 0x64 ; 0x00 ; 0x4e ; 0x56 ; 0xff ; 0xfa ; 0x01 ; 0x08 ; 0x2e ; 0x2e ; 0x00 ; 0xb6 ; 0xd0 ; 0x68 ; 0x3e ; 0x80 ; 0x2f ; 0x0c ; 0xa9 ; 0xfe ; 0x64 ; 0x53 ;...
let pad_password password = let pw = Array . make 32 0 in Array . iteri ( fun i v -> if i < 32 then pw . ( i ) <- v ) password ; let n = Array . length password in if n < 32 then for x = n to 31 do pw . ( x ) <- paddings . ( x - n ) done ; pw
let find_key no_encrypt_metadata password r o p id keylength = let password = int_array_of_string password in let o = int_array_of_string o in let id = int_array_of_string id in let pw = pad_password password in let from_p = [ | i32toi ( land32 p 0x000000ffl ) ; i32toi ( lsr32 ( land32 p 0x0000ff00l...
let authenticate_user no_encrypt_metadata password r u o p id keylength = let u = int_array_of_string u in let key = find_key no_encrypt_metadata password r o p id keylength in if r >= 3 then let id = int_array_of_string id in let todigest = [ paddings ; id ] in let hash_input = string_of_int_arrays todig...
let string_of_pdf : ( Pdf . pdfobject -> string ) ref = ref ( function _ -> " " )
let rec decrypt crypt_type pdf no_encrypt_metadata encrypt obj gen key keylength r file_encryption_key l = match l with | Pdf . String s -> begin match fst ( Pdf . pdfobjmap_find obj pdf . Pdf . objects . Pdf . pdfobjects ) with | { contents = Pdf . ParsedAlreadyDecrypted _ } -> Pdf . Str...
let process_cryption no_encrypt_metadata encrypt pdf crypt_type user_pw r u o p id keylength file_encryption_key = let encryption_object_number = match pdf . Pdf . trailerdict with | Pdf . Dictionary d -> begin match lookup " / Encrypt " d with | Some ( Pdf . Indirect i ) -> i | _ -> - 1 end...
let printable_of_string s = String . concat " " ( map ( fun c -> Printf . sprintf " % 02x " ( int_of_char c ) ) ( explode s ) )
let get_encryption_values pdf = match Pdf . lookup_direct pdf " / Encrypt " pdf . Pdf . trailerdict with | None -> raise ( Pdf . PDFError " get_encryption_values : unencrypted pdf " ) | Some encryptdict -> let crypt_type = match Pdf . lookup_direct pdf " / Filter " encryptdict , Pdf...
type permission = | NoEdit | NoPrint | NoCopy | NoAnnot | NoForms | NoExtract | NoAssemble | NoHqPrint
let string_of_permission = function | NoEdit -> " NoEdit " | NoPrint -> " NoPrint " | NoCopy -> " NoCopy " | NoAnnot -> " NoAnnot " | NoForms -> " NoForms " | NoExtract -> " NoExtract " | NoAssemble -> " NoAssemble " | NoHqPrint -> " NoHqPrint "
let string_of_bans bans = fold_left ( ^ ) " " ( interleave " " ( map string_of_permission bans ) )
let p_of_banlist toban = let p = ref 0l in let setbit n b = if b then p := Int32 . logor ! p ( Int32 . shift_left 1l ( n - 1 ) ) in let notin = notpred ( mem ' toban ) in setbit 3 ( notin NoPrint ) ; setbit 4 ( notin NoEdit ) ; setbit 5 ( notin NoCopy ) ; setbit 6 ( n...
let banlist_of_p p = let l = ref [ ] in let bitset n = Int32 . logand ( Int32 . shift_right p ( n - 1 ) ) 1l = 0l in if bitset 3 then l =| NoPrint ; if bitset 4 then l =| NoEdit ; if bitset 5 then l =| NoCopy ; if bitset 6 then l =| NoAnnot ; if bitset 9 then l =| NoForms ; i...
let print_string name s = flprint name ; iter ( Printf . printf " % i " ) ( map int_of_char ( explode s ) ) ; flprint " \ n "
let mod3 b = let x = ref 0 in for i = 0 to 15 do x += int_of_char b . [ i ] done ; ! x mod 3
let prs s = String . iter ( fun x -> Printf . printf " % 02x " ( int_of_char x ) ) ( if String . length s > 16 then String . sub s 0 16 else s ) ; flprint " \ n "
let shamix_cache = Hashtbl . create 16
let shamix password udata s = try Hashtbl . find shamix_cache ( password , udata , s ) with Not_found -> let i = input_of_string s in if ! crypt_debug then begin flprint " Beginning of shamix \ n Password is \ n " ; prs password ; flprint " udata is \ n " ; prs ( match udata with None ...
let make_utf8 pw = if String . length pw > 127 then String . sub pw 0 127 else pw
let zero_iv = String . make 16 ' \ 000 '
let file_encryption_key_aesv3 ? digest iso utf8pw o oe u = if String . length o < 48 || String . length u < 48 then raise ( Pdf . PDFError " / O too short in make_intermediate_owner_key_aesv3 " ) else let d = match digest with | Some d -> if Array . length d <> 32 then Printf . eprintf " ...
let file_encryption_key_aesv3_user iso utf8pw u ue = if String . length u < 48 then raise ( Pdf . PDFError " / U too short in file_encryption_key_aesv3_user " ) else Pdfcryptprimitives . aes_decrypt_data ~ remove_padding : false 8 ( int_array_of_string ( ( if iso then shamix utf8pw None else ...
let authenticate_owner_password_aesv3 iso utf8pw u o = if String . length o < 48 || String . length u < 48 then raise ( Pdf . PDFError " / O too short in authenticate_owner_password " ) else ( if iso then shamix utf8pw ( Some u ) else ( fun x -> Pdfcryptprimitives . sha256 ( Pdfio . ...
let authenticate_user_password_aesv3 iso utf8pw u = if String . length u < 48 then raise ( Pdf . PDFError " / U too short in authenticate_owner_password " ) else ( if iso then shamix utf8pw None else ( fun x -> Pdfcryptprimitives . sha256 ( Pdfio . input_of_string x ) ) ) ( String . ...
let p_of_perms key perms = if String . length perms < 16 then raise ( Pdf . PDFError " Wrong length in / Perms " ) else let ps = Pdfcryptprimitives . aes_decrypt_data_ecb ~ remove_padding : false 8 ( int_array_of_bytes key ) ( bytes_of_string perms ) in let ints = int_array_of_bytes ps in...
let decrypt_pdf ? keyfromowner user_pw pdf = match Pdf . lookup_direct pdf " / Encrypt " pdf . Pdf . trailerdict with | None -> Some pdf , [ ] | Some encrypt_dict -> let crypt_type , u , o , p , id , ue , oe = get_encryption_values pdf in let r , keylength , file_encryption_key ...