Skip to content

Commit 7c814bd

Browse files
authored
Merge pull request tectonic-typesetting#253 from burrbull/arg
spc_arg refs
2 parents 343e737 + 6ff2506 commit 7c814bd

13 files changed

+836
-966
lines changed

dpx/src/dpx_pdfcolor.rs

+78-124
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ use crate::dpx_pdfobj::{
3232
};
3333
use crate::mfree;
3434
use crate::shims::sprintf;
35-
use crate::{info, warn};
35+
use crate::{info, warn, FromBEByteSlice};
3636
use libc::{free, memcmp, memcpy, memset, strcmp, strcpy, strlen};
3737
use md5::{Digest, Md5};
3838
use std::error::Error;
3939
use std::ffi::{CStr, CString};
4040
use std::fmt;
4141
use std::ptr;
42-
use std::slice::from_raw_parts;
4342

4443
#[derive(Debug)]
4544
pub(crate) enum PdfColorError {
@@ -395,13 +394,11 @@ unsafe fn iccp_version_supported(major: i32, minor: i32) -> i32 {
395394
}
396395
0i32
397396
}
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)
404400
}
401+
405402
unsafe fn iccp_init_iccHeader(icch: &mut iccHeader) {
406403
icch.size = 0i32;
407404
icch.CMMType = 0i32 as iccSig;
@@ -414,7 +411,7 @@ unsafe fn iccp_init_iccHeader(icch: &mut iccHeader) {
414411
0i32,
415412
12,
416413
);
417-
icch.acsp = str2iccSig(b"ascp\x00" as *const u8 as *const i8 as *const libc::c_void);
414+
icch.acsp = str2iccSig(b"ascp");
418415
icch.platform = 0i32 as iccSig;
419416
memset(icch.flags.as_mut_ptr() as *mut libc::c_void, 0i32, 4);
420417
icch.devMnfct = 0i32 as iccSig;
@@ -491,27 +488,20 @@ pub(crate) unsafe fn iccp_check_colorspace(colortype: i32, profile: &[u8]) -> i3
491488
if profile.len() < 128 {
492489
return -1i32;
493490
}
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]);
496492
match colortype {
497493
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 ") {
501495
return -1i32;
502496
}
503497
}
504498
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") {
508500
return -1i32;
509501
}
510502
}
511503
-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") {
515505
return -1i32;
516506
}
517507
}
@@ -543,131 +533,108 @@ pub(crate) unsafe fn iccp_get_rendering_intent(profile: &[u8]) -> *mut pdf_obj {
543533
}
544534
}
545535
}
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();
552538
if check_size != 0 {
553-
if profile.is_null() || proflen < 128i32 || proflen % 4i32 != 0i32 {
539+
if proflen < 128 || proflen % 4 != 0 {
554540
warn!("Profile size: {}", proflen);
555541
return -1i32;
556542
}
557543
}
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;
564546
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);
567549
return -1i32;
568550
}
569551
}
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..];
584563
memcpy(
585564
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,
587566
12,
588567
);
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") {
592571
warn!(
593572
"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]),
598577
);
599578
return -1i32;
600579
}
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..];
604583
memcpy(
605584
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,
607586
4,
608587
);
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..];
614593
memcpy(
615594
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,
617596
8,
618597
);
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..];
642609
memcpy(
643610
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,
645612
16,
646613
);
647-
p = p.offset(16);
614+
p = &p[16..];
648615
/* 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 {
651618
warn!(
652619
"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(),
655622
);
656623
return -1i32;
657624
}
658-
p = p.offset(1)
625+
p = &p[1..];
659626
}
660627
0i32
661628
}
662-
unsafe fn iccp_get_checksum(profile: *const u8, proflen: usize) -> [u8; 16] {
629+
unsafe fn iccp_get_checksum(profile: &[u8]) -> [u8; 16] {
663630
let mut md5 = Md5::new();
664-
md5.input(from_raw_parts(profile.offset(0), 56));
631+
md5.input(&profile[..56]);
665632
md5.input(&[0u8; 12]);
666-
md5.input(from_raw_parts(profile.offset(68), 16));
633+
md5.input(&profile[68..84]);
667634
md5.input(&[0u8; 16]);
668-
md5.input(from_raw_parts(profile.offset(100), 28));
635+
md5.input(&profile[100..128]);
669636
/* body */
670-
md5.input(from_raw_parts(profile.offset(128), proflen - 128));
637+
md5.input(&profile[128..]);
671638
md5.result().into()
672639
}
673640

@@ -929,13 +896,10 @@ unsafe fn print_iccp_header(icch: &mut iccHeader, checksum: *mut u8) {
929896
}
930897
unsafe fn iccp_devClass_allowed(dev_class: i32) -> i32 {
931898
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")
939903
{
940904
return 0i32;
941905
}
@@ -947,13 +911,7 @@ pub(crate) unsafe fn iccp_load_profile(ident: *const i8, profile: &[u8]) -> i32
947911
let mut icch = iccHeader::default();
948912
let colorspace;
949913
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 {
957915
/* check size */
958916
warn!(
959917
"Invalid ICC profile header in \"{}\"",
@@ -980,22 +938,18 @@ pub(crate) unsafe fn iccp_load_profile(ident: *const i8, profile: &[u8]) -> i32
980938
print_iccp_header(&mut icch, ptr::null_mut());
981939
return -1i32;
982940
}
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 ") {
984942
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") {
988944
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") {
992946
colorspace = -4i32
993947
} else {
994948
warn!("Unsupported input color space.");
995949
print_iccp_header(&mut icch, ptr::null_mut());
996950
return -1i32;
997951
}
998-
let mut checksum = iccp_get_checksum(profile.as_ptr(), profile.len());
952+
let mut checksum = iccp_get_checksum(profile);
999953
if memcmp(
1000954
icch.ID.as_mut_ptr() as *const libc::c_void,
1001955
NULLBYTES16.as_mut_ptr() as *const libc::c_void,

dpx/src/dpx_pdfobj.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -1270,22 +1270,22 @@ impl std::borrow::Borrow<[u8]> for pdf_name {
12701270
}
12711271

12721272
impl pdf_dict {
1273-
pub(crate) unsafe fn foreach(
1273+
pub(crate) unsafe fn foreach<T>(
12741274
&mut self,
1275-
f: unsafe fn(_: &pdf_name, _: *mut pdf_obj, _: *mut libc::c_void) -> i32,
1276-
pdata: *mut libc::c_void,
1275+
f: unsafe fn(_: &pdf_name, _: &mut pdf_obj, _: &mut T) -> i32,
1276+
pdata: &mut T,
12771277
) -> i32 {
12781278
self.foreach_dict(
12791279
|k, v, pdata| {
1280-
let e = f(k, v, pdata);
1280+
let e = f(k, &mut *v, pdata);
12811281
e
12821282
},
12831283
pdata,
12841284
)
12851285
}
1286-
fn foreach_dict<F>(&mut self, f: F, pdata: *mut libc::c_void) -> i32
1286+
fn foreach_dict<F, T>(&mut self, f: F, pdata: &mut T) -> i32
12871287
where
1288-
F: Fn(&pdf_name, *mut pdf_obj, *mut libc::c_void) -> i32,
1288+
F: Fn(&pdf_name, *mut pdf_obj, &mut T) -> i32,
12891289
{
12901290
let mut error: i32 = 0i32;
12911291
for (k, &v) in self.inner.iter() {
@@ -3805,13 +3805,12 @@ pub(crate) unsafe extern "C" fn check_for_pdf(handle: &mut InputHandleWrapper) -
38053805
}
38063806

38073807
#[inline]
3808-
unsafe fn import_dict(key: &pdf_name, value: *mut pdf_obj, pdata: *mut libc::c_void) -> i32 {
3809-
let copy = &mut *(pdata as *mut pdf_obj);
3808+
unsafe fn import_dict(key: &pdf_name, value: &mut pdf_obj, pdata: &mut pdf_dict) -> i32 {
38103809
let tmp = pdf_import_object(value);
38113810
if tmp.is_null() {
38123811
return -1i32;
38133812
}
3814-
copy.as_dict_mut().set(key.to_bytes(), tmp); // TODO: check
3813+
pdata.set(key.to_bytes(), tmp); // TODO: check
38153814
0i32
38163815
}
38173816
static mut loop_marker: pdf_obj = pdf_obj {
@@ -3887,17 +3886,11 @@ pub(crate) unsafe fn pdf_import_object(object: *mut pdf_obj) -> *mut pdf_obj {
38873886
imported.into_obj()
38883887
}
38893888
PdfObjVariant::DICT(v) => {
3890-
let imported = pdf_dict::new().into_obj();
3891-
if (&mut *v).foreach(
3892-
import_dict
3893-
as unsafe fn(_: &pdf_name, _: *mut pdf_obj, _: *mut libc::c_void) -> i32,
3894-
imported as *mut libc::c_void,
3895-
) < 0i32
3896-
{
3897-
pdf_release_obj(imported);
3889+
let mut imported = pdf_dict::new();
3890+
if (&mut *v).foreach(import_dict, &mut imported) < 0i32 {
38983891
return ptr::null_mut();
38993892
}
3900-
imported
3893+
imported.into_obj()
39013894
}
39023895
PdfObjVariant::ARRAY(v) => {
39033896
let mut imported = vec![];

0 commit comments

Comments
 (0)