@@ -32,14 +32,13 @@ use crate::dpx_pdfobj::{
32
32
} ;
33
33
use crate :: mfree;
34
34
use crate :: shims:: sprintf;
35
- use crate :: { info, warn} ;
35
+ use crate :: { info, warn, FromBEByteSlice } ;
36
36
use libc:: { free, memcmp, memcpy, memset, strcmp, strcpy, strlen} ;
37
37
use md5:: { Digest , Md5 } ;
38
38
use std:: error:: Error ;
39
39
use std:: ffi:: { CStr , CString } ;
40
40
use std:: fmt;
41
41
use std:: ptr;
42
- use std:: slice:: from_raw_parts;
43
42
44
43
#[ derive( Debug ) ]
45
44
pub ( crate ) enum PdfColorError {
@@ -395,13 +394,11 @@ unsafe fn iccp_version_supported(major: i32, minor: i32) -> i32 {
395
394
}
396
395
0i32
397
396
}
398
- unsafe fn str2iccSig ( s : * const libc:: c_void ) -> iccSig {
399
- let p = s as * const i8 ;
400
- return ( ( * p. offset ( 0 ) as i32 ) << 24i32
401
- | ( * p. offset ( 1 ) as i32 ) << 16i32
402
- | ( * p. offset ( 2 ) as i32 ) << 8i32
403
- | * p. offset ( 3 ) as i32 ) as iccSig ;
397
+
398
+ fn str2iccSig ( p : & [ u8 ] ) -> iccSig {
399
+ u32:: from_be_byte_slice ( p)
404
400
}
401
+
405
402
unsafe fn iccp_init_iccHeader ( icch : & mut iccHeader ) {
406
403
icch. size = 0i32 ;
407
404
icch. CMMType = 0i32 as iccSig ;
@@ -414,7 +411,7 @@ unsafe fn iccp_init_iccHeader(icch: &mut iccHeader) {
414
411
0i32 ,
415
412
12 ,
416
413
) ;
417
- icch. acsp = str2iccSig ( b"ascp\x00 " as * const u8 as * const i8 as * const libc :: c_void ) ;
414
+ icch. acsp = str2iccSig ( b"ascp" ) ;
418
415
icch. platform = 0i32 as iccSig ;
419
416
memset ( icch. flags . as_mut_ptr ( ) as * mut libc:: c_void , 0i32 , 4 ) ;
420
417
icch. devMnfct = 0i32 as iccSig ;
@@ -491,27 +488,20 @@ pub(crate) unsafe fn iccp_check_colorspace(colortype: i32, profile: &[u8]) -> i3
491
488
if profile. len ( ) < 128 {
492
489
return -1i32 ;
493
490
}
494
- let p = profile. as_ptr ( ) ;
495
- let colorspace = str2iccSig ( p. offset ( 16 ) as * const libc:: c_void ) ;
491
+ let colorspace = str2iccSig ( & profile[ 16 ..20 ] ) ;
496
492
match colortype {
497
493
3 | -3 => {
498
- if colorspace
499
- != str2iccSig ( b"RGB \x00 " as * const u8 as * const i8 as * const libc:: c_void )
500
- {
494
+ if colorspace != str2iccSig ( b"RGB " ) {
501
495
return -1i32 ;
502
496
}
503
497
}
504
498
1 | -1 => {
505
- if colorspace
506
- != str2iccSig ( b"GRAY\x00 " as * const u8 as * const i8 as * const libc:: c_void )
507
- {
499
+ if colorspace != str2iccSig ( b"GRAY" ) {
508
500
return -1i32 ;
509
501
}
510
502
}
511
503
-4 => {
512
- if colorspace
513
- != str2iccSig ( b"CMYK\x00 " as * const u8 as * const i8 as * const libc:: c_void )
514
- {
504
+ if colorspace != str2iccSig ( b"CMYK" ) {
515
505
return -1i32 ;
516
506
}
517
507
}
@@ -543,131 +533,108 @@ pub(crate) unsafe fn iccp_get_rendering_intent(profile: &[u8]) -> *mut pdf_obj {
543
533
}
544
534
}
545
535
}
546
- unsafe fn iccp_unpack_header (
547
- icch : & mut iccHeader ,
548
- profile : * const libc:: c_void ,
549
- proflen : i32 ,
550
- check_size : i32 ,
551
- ) -> i32 {
536
+ unsafe fn iccp_unpack_header ( icch : & mut iccHeader , profile : & [ u8 ] , check_size : i32 ) -> i32 {
537
+ let proflen = profile. len ( ) ;
552
538
if check_size != 0 {
553
- if profile . is_null ( ) || proflen < 128i32 || proflen % 4i32 != 0i32 {
539
+ if proflen < 128 || proflen % 4 != 0 {
554
540
warn ! ( "Profile size: {}" , proflen) ;
555
541
return -1i32 ;
556
542
}
557
543
}
558
- let mut p = profile as * const u8 ;
559
- let endptr = p. offset ( 128 ) ;
560
- icch. size = ( * p. offset ( 0 ) as i32 ) << 24i32
561
- | ( * p. offset ( 1 ) as i32 ) << 16i32
562
- | ( * p. offset ( 2 ) as i32 ) << 8i32
563
- | * p. offset ( 3 ) as i32 ;
544
+ let mut p = & profile[ ..128 ] ;
545
+ icch. size = u32:: from_be_byte_slice ( & p[ ..4 ] ) as i32 ;
564
546
if check_size != 0 {
565
- if icch. size != proflen {
566
- warn ! ( "ICC Profile size: {}(header) != {}" , icch. size, proflen, ) ;
547
+ if icch. size != proflen as i32 {
548
+ warn ! ( "ICC Profile size: {}(header) != {}" , icch. size, proflen) ;
567
549
return -1i32 ;
568
550
}
569
551
}
570
- p = p. offset ( 4 ) ;
571
- icch. CMMType = str2iccSig ( p as * const libc:: c_void ) ;
572
- p = p. offset ( 4 ) ;
573
- icch. version = ( * p. offset ( 0 ) as i32 ) << 24i32
574
- | ( * p. offset ( 1 ) as i32 ) << 16i32
575
- | ( * p. offset ( 2 ) as i32 ) << 8i32
576
- | * p. offset ( 3 ) as i32 ;
577
- p = p. offset ( 4 ) ;
578
- icch. devClass = str2iccSig ( p as * const libc:: c_void ) ;
579
- p = p. offset ( 4 ) ;
580
- icch. colorSpace = str2iccSig ( p as * const libc:: c_void ) ;
581
- p = p. offset ( 4 ) ;
582
- icch. PCS = str2iccSig ( p as * const libc:: c_void ) ;
583
- p = p. offset ( 4 ) ;
552
+ p = & p[ 4 ..] ;
553
+ icch. CMMType = str2iccSig ( & p[ ..4 ] ) ;
554
+ p = & p[ 4 ..] ;
555
+ icch. version = u32:: from_be_byte_slice ( & p[ ..4 ] ) as i32 ;
556
+ p = & p[ 4 ..] ;
557
+ icch. devClass = str2iccSig ( & p[ ..4 ] ) ;
558
+ p = & p[ 4 ..] ;
559
+ icch. colorSpace = str2iccSig ( & p[ ..4 ] ) ;
560
+ p = & p[ 4 ..] ;
561
+ icch. PCS = str2iccSig ( & p[ ..4 ] ) ;
562
+ p = & p[ 4 ..] ;
584
563
memcpy (
585
564
icch. creationDate . as_mut_ptr ( ) as * mut libc:: c_void ,
586
- p as * const libc:: c_void ,
565
+ p. as_ptr ( ) as * const libc:: c_void ,
587
566
12 ,
588
567
) ;
589
- p = p . offset ( 12 ) ;
590
- icch. acsp = str2iccSig ( p as * const libc :: c_void ) ;
591
- if icch. acsp != str2iccSig ( b"acsp\x00 " as * const u8 as * const i8 as * const libc :: c_void ) {
568
+ p = & p [ 12 .. ] ;
569
+ icch. acsp = str2iccSig ( & p [ .. 4 ] ) ;
570
+ if icch. acsp != str2iccSig ( b"acsp" ) {
592
571
warn ! (
593
572
"Invalid ICC profile: not \" acsp\" - {}{}{}{} " ,
594
- char :: from( * p . offset ( 0 ) ) ,
595
- char :: from( * p . offset ( 1 ) ) ,
596
- char :: from( * p . offset ( 2 ) ) ,
597
- char :: from( * p . offset ( 3 ) ) ,
573
+ char :: from( p [ 0 ] ) ,
574
+ char :: from( p [ 1 ] ) ,
575
+ char :: from( p [ 2 ] ) ,
576
+ char :: from( p [ 3 ] ) ,
598
577
) ;
599
578
return -1i32 ;
600
579
}
601
- p = p . offset ( 4 ) ;
602
- icch. platform = str2iccSig ( p as * const libc :: c_void ) ;
603
- p = p . offset ( 4 ) ;
580
+ p = & p [ 4 .. ] ;
581
+ icch. platform = str2iccSig ( & p [ .. 4 ] ) ;
582
+ p = & p [ 4 .. ] ;
604
583
memcpy (
605
584
icch. flags . as_mut_ptr ( ) as * mut libc:: c_void ,
606
- p as * const libc:: c_void ,
585
+ p. as_ptr ( ) as * const libc:: c_void ,
607
586
4 ,
608
587
) ;
609
- p = p . offset ( 4 ) ;
610
- icch. devMnfct = str2iccSig ( p as * const libc :: c_void ) ;
611
- p = p . offset ( 4 ) ;
612
- icch. devModel = str2iccSig ( p as * const libc :: c_void ) ;
613
- p = p . offset ( 4 ) ;
588
+ p = & p [ 4 .. ] ;
589
+ icch. devMnfct = str2iccSig ( & p [ .. 4 ] ) ;
590
+ p = & p [ 4 .. ] ;
591
+ icch. devModel = str2iccSig ( & p [ .. 4 ] ) ;
592
+ p = & p [ 4 .. ] ;
614
593
memcpy (
615
594
icch. devAttr . as_mut_ptr ( ) as * mut libc:: c_void ,
616
- p as * const libc:: c_void ,
595
+ p. as_ptr ( ) as * const libc:: c_void ,
617
596
8 ,
618
597
) ;
619
- p = p. offset ( 8 ) ;
620
- icch. intent = ( * p. offset ( 0 ) as i32 ) << 24i32
621
- | ( * p. offset ( 1 ) as i32 ) << 16i32
622
- | ( * p. offset ( 2 ) as i32 ) << 8i32
623
- | * p. offset ( 3 ) as i32 ;
624
- p = p. offset ( 4 ) ;
625
- icch. illuminant . X = ( * p. offset ( 0 ) as i32 ) << 24i32
626
- | ( * p. offset ( 1 ) as i32 ) << 16i32
627
- | ( * p. offset ( 2 ) as i32 ) << 8i32
628
- | * p. offset ( 3 ) as i32 ;
629
- p = p. offset ( 4 ) ;
630
- icch. illuminant . Y = ( * p. offset ( 0 ) as i32 ) << 24i32
631
- | ( * p. offset ( 1 ) as i32 ) << 16i32
632
- | ( * p. offset ( 2 ) as i32 ) << 8i32
633
- | * p. offset ( 3 ) as i32 ;
634
- p = p. offset ( 4 ) ;
635
- icch. illuminant . Z = ( * p. offset ( 0 ) as i32 ) << 24i32
636
- | ( * p. offset ( 1 ) as i32 ) << 16i32
637
- | ( * p. offset ( 2 ) as i32 ) << 8i32
638
- | * p. offset ( 3 ) as i32 ;
639
- p = p. offset ( 4 ) ;
640
- icch. creator = str2iccSig ( p as * const libc:: c_void ) ;
641
- p = p. offset ( 4 ) ;
598
+ p = & p[ 8 ..] ;
599
+ icch. intent = u32:: from_be_byte_slice ( & p[ ..4 ] ) as i32 ;
600
+ p = & p[ 4 ..] ;
601
+ icch. illuminant . X = u32:: from_be_byte_slice ( & p[ ..4 ] ) as i32 ;
602
+ p = & p[ 4 ..] ;
603
+ icch. illuminant . Y = u32:: from_be_byte_slice ( & p[ ..4 ] ) as i32 ;
604
+ p = & p[ 4 ..] ;
605
+ icch. illuminant . Z = u32:: from_be_byte_slice ( & p[ ..4 ] ) as i32 ;
606
+ p = & p[ 4 ..] ;
607
+ icch. creator = str2iccSig ( & p[ ..4 ] ) ;
608
+ p = & p[ 4 ..] ;
642
609
memcpy (
643
610
icch. ID . as_mut_ptr ( ) as * mut libc:: c_void ,
644
- p as * const libc:: c_void ,
611
+ p. as_ptr ( ) as * const libc:: c_void ,
645
612
16 ,
646
613
) ;
647
- p = p . offset ( 16 ) ;
614
+ p = & p [ 16 .. ] ;
648
615
/* 28 bytes reserved - must be set to zeros */
649
- while p < endptr {
650
- if * p as i32 != '\u{0}' as i32 {
616
+ while !p . is_empty ( ) {
617
+ if p [ 0 ] != 0 {
651
618
warn ! (
652
619
"Reserved pad not zero: {:02x} (at offset {} in ICC profile header.)" ,
653
- * p as i32 ,
654
- 128i32 - endptr . wrapping_offset_from ( p ) as i64 as i32 ,
620
+ p [ 0 ] ,
621
+ 128 - p . len ( ) ,
655
622
) ;
656
623
return -1i32 ;
657
624
}
658
- p = p . offset ( 1 )
625
+ p = & p [ 1 .. ] ;
659
626
}
660
627
0i32
661
628
}
662
- unsafe fn iccp_get_checksum ( profile : * const u8 , proflen : usize ) -> [ u8 ; 16 ] {
629
+ unsafe fn iccp_get_checksum ( profile : & [ u8 ] ) -> [ u8 ; 16 ] {
663
630
let mut md5 = Md5 :: new ( ) ;
664
- md5. input ( from_raw_parts ( profile. offset ( 0 ) , 56 ) ) ;
631
+ md5. input ( & profile[ .. 56 ] ) ;
665
632
md5. input ( & [ 0u8 ; 12 ] ) ;
666
- md5. input ( from_raw_parts ( profile. offset ( 68 ) , 16 ) ) ;
633
+ md5. input ( & profile[ 68 .. 84 ] ) ;
667
634
md5. input ( & [ 0u8 ; 16 ] ) ;
668
- md5. input ( from_raw_parts ( profile. offset ( 100 ) , 28 ) ) ;
635
+ md5. input ( & profile[ 100 .. 128 ] ) ;
669
636
/* body */
670
- md5. input ( from_raw_parts ( profile. offset ( 128 ) , proflen - 128 ) ) ;
637
+ md5. input ( & profile[ 128 .. ] ) ;
671
638
md5. result ( ) . into ( )
672
639
}
673
640
@@ -929,13 +896,10 @@ unsafe fn print_iccp_header(icch: &mut iccHeader, checksum: *mut u8) {
929
896
}
930
897
unsafe fn iccp_devClass_allowed ( dev_class : i32 ) -> i32 {
931
898
let _colormode = pdf_dev_get_param ( 2i32 ) ; // TODO: check
932
- if dev_class as u32 != str2iccSig ( b"scnr\x00 " as * const u8 as * const i8 as * const libc:: c_void )
933
- && dev_class as u32
934
- != str2iccSig ( b"mntr\x00 " as * const u8 as * const i8 as * const libc:: c_void )
935
- && dev_class as u32
936
- != str2iccSig ( b"prtr\x00 " as * const u8 as * const i8 as * const libc:: c_void )
937
- && dev_class as u32
938
- != str2iccSig ( b"spac\x00 " as * const u8 as * const i8 as * const libc:: c_void )
899
+ if dev_class as u32 != str2iccSig ( b"scnr" )
900
+ && dev_class as u32 != str2iccSig ( b"mntr" )
901
+ && dev_class as u32 != str2iccSig ( b"prtr" )
902
+ && dev_class as u32 != str2iccSig ( b"spac" )
939
903
{
940
904
return 0i32 ;
941
905
}
@@ -947,13 +911,7 @@ pub(crate) unsafe fn iccp_load_profile(ident: *const i8, profile: &[u8]) -> i32
947
911
let mut icch = iccHeader:: default ( ) ;
948
912
let colorspace;
949
913
iccp_init_iccHeader ( & mut icch) ;
950
- if iccp_unpack_header (
951
- & mut icch,
952
- profile. as_ptr ( ) as * const libc:: c_void ,
953
- profile. len ( ) as i32 ,
954
- 1i32 ,
955
- ) < 0i32
956
- {
914
+ if iccp_unpack_header ( & mut icch, profile, 1i32 ) < 0i32 {
957
915
/* check size */
958
916
warn ! (
959
917
"Invalid ICC profile header in \" {}\" " ,
@@ -980,22 +938,18 @@ pub(crate) unsafe fn iccp_load_profile(ident: *const i8, profile: &[u8]) -> i32
980
938
print_iccp_header ( & mut icch, ptr:: null_mut ( ) ) ;
981
939
return -1i32 ;
982
940
}
983
- if icch. colorSpace == str2iccSig ( b"RGB \x00 " as * const u8 as * const i8 as * const libc :: c_void ) {
941
+ if icch. colorSpace == str2iccSig ( b"RGB " ) {
984
942
colorspace = -3i32
985
- } else if icch. colorSpace
986
- == str2iccSig ( b"GRAY\x00 " as * const u8 as * const i8 as * const libc:: c_void )
987
- {
943
+ } else if icch. colorSpace == str2iccSig ( b"GRAY" ) {
988
944
colorspace = -1i32
989
- } else if icch. colorSpace
990
- == str2iccSig ( b"CMYK\x00 " as * const u8 as * const i8 as * const libc:: c_void )
991
- {
945
+ } else if icch. colorSpace == str2iccSig ( b"CMYK" ) {
992
946
colorspace = -4i32
993
947
} else {
994
948
warn ! ( "Unsupported input color space." ) ;
995
949
print_iccp_header ( & mut icch, ptr:: null_mut ( ) ) ;
996
950
return -1i32 ;
997
951
}
998
- let mut checksum = iccp_get_checksum ( profile. as_ptr ( ) , profile . len ( ) ) ;
952
+ let mut checksum = iccp_get_checksum ( profile) ;
999
953
if memcmp (
1000
954
icch. ID . as_mut_ptr ( ) as * const libc:: c_void ,
1001
955
NULLBYTES16 . as_mut_ptr ( ) as * const libc:: c_void ,
0 commit comments