@@ -168,8 +168,9 @@ static const Resource *get_resource(BWIPP *ctx, const char *name) {
168168}
169169
170170/* Read resource body from file on demand; returns NULL on I/O error */
171- static char * load_code (BWIPP * ctx , const Resource * resource ) {
171+ static char * load_code (BWIPP * ctx , const Resource * resource , size_t * out_len ) {
172172 char * code ;
173+ size_t i , j ;
173174
174175 if (resource -> code_offset < 0 || !ctx -> f )
175176 return NULL ;
@@ -188,7 +189,14 @@ static char *load_code(BWIPP *ctx, const Resource *resource) {
188189 return NULL ;
189190 }
190191
191- code [resource -> code_len ] = '\0' ;
192+ /* Strip \r from CRLF sequences (file opened in binary mode) */
193+ for (i = 0 , j = 0 ; i < resource -> code_len ; i ++ ) {
194+ if (code [i ] != '\r' || i + 1 >= resource -> code_len || code [i + 1 ] != '\n' )
195+ code [j ++ ] = code [i ];
196+ }
197+ code [j ] = '\0' ;
198+ * out_len = j ;
199+
192200 return code ;
193201}
194202
@@ -373,7 +381,7 @@ BWIPP_API BWIPP *bwipp_load_ex(const bwipp_load_init_opts_t *opts) {
373381 ctx -> hexify_width = hexify_width ;
374382 tail = & ctx -> resourcelist ;
375383
376- f = fopen (filename , "r " );
384+ f = fopen (filename , "rb " );
377385 if (!f )
378386 goto error ;
379387
@@ -386,6 +394,14 @@ BWIPP_API BWIPP *bwipp_load_ex(const bwipp_load_init_opts_t *opts) {
386394 skip = true;
387395 while (fgets (buf , sizeof buf , f )) {
388396 size_t line_len = strlen (buf );
397+ size_t raw_len = line_len ;
398+
399+ /* Strip \r from CRLF (binary mode preserves \r) */
400+ if (line_len >= 2 && buf [line_len - 2 ] == '\r' && buf [line_len - 1 ] == '\n' ) {
401+ buf [line_len - 2 ] = '\n' ;
402+ buf [line_len - 1 ] = '\0' ;
403+ line_len -- ;
404+ }
389405
390406 /* Reject marker lines truncated by fgets */
391407 if (strncmp (buf , "% --" , 4 ) == 0 &&
@@ -453,6 +469,8 @@ BWIPP_API BWIPP *bwipp_load_ex(const bwipp_load_init_opts_t *opts) {
453469 * code = '\0' ;
454470 code_len = 0 ;
455471 code_start = ftell (f );
472+ if (code_start < 0 )
473+ goto error ;
456474
457475 continue ;
458476
@@ -478,6 +496,8 @@ BWIPP_API BWIPP *bwipp_load_ex(const bwipp_load_init_opts_t *opts) {
478496 if (!add_property (resource , key , value ))
479497 goto error ;
480498 code_start = ftell (f );
499+ if (code_start < 0 )
500+ goto error ;
481501 continue ;
482502 }
483503 }
@@ -501,6 +521,8 @@ BWIPP_API BWIPP *bwipp_load_ex(const bwipp_load_init_opts_t *opts) {
501521 goto error ;
502522
503523 code_start = ftell (f );
524+ if (code_start < 0 )
525+ goto error ;
504526 continue ;
505527
506528 } /* REQUIRES */
@@ -531,15 +553,19 @@ BWIPP_API BWIPP *bwipp_load_ex(const bwipp_load_init_opts_t *opts) {
531553 goto error ;
532554
533555 if (lazy ) {
556+ long end_pos = ftell (f );
557+ if (end_pos < 0 )
558+ goto error ;
534559 resource -> code = NULL ;
535560 resource -> code_offset = code_start ;
561+ resource -> code_len = (size_t )(end_pos - (long )raw_len - code_start );
536562 } else {
537563 resource -> code = strdup (code );
538564 if (!resource -> code )
539565 goto error ;
540566 * code = '\0' ;
567+ resource -> code_len = code_len ;
541568 }
542- resource -> code_len = code_len ;
543569 code_len = 0 ;
544570
545571 /* Add to ResourceList */
@@ -563,13 +589,9 @@ BWIPP_API BWIPP *bwipp_load_ex(const bwipp_load_init_opts_t *opts) {
563589 } /* END */
564590
565591 /* PS Code */
566- if (resource ) {
567- if (lazy ) {
568- code_len += line_len ;
569- } else {
570- if (!safe_append_n (code , & code_len , MAX_CODE , buf , line_len ))
571- goto error ;
572- }
592+ if (resource && !lazy ) {
593+ if (!safe_append_n (code , & code_len , MAX_CODE , buf , line_len ))
594+ goto error ;
573595 }
574596 }
575597
@@ -862,11 +884,12 @@ static bool append_resource_code(BWIPP *ctx, const Resource *res,
862884 if (res -> code ) {
863885 return safe_append_n (buf , pos , capacity , res -> code , res -> code_len );
864886 } else {
865- char * lazy_code = load_code (ctx , res );
887+ size_t len ;
888+ char * lazy_code = load_code (ctx , res , & len );
866889 bool ok ;
867890 if (!lazy_code )
868891 return false;
869- ok = safe_append_n (buf , pos , capacity , lazy_code , res -> code_len );
892+ ok = safe_append_n (buf , pos , capacity , lazy_code , len );
870893 free (lazy_code );
871894 return ok ;
872895 }
0 commit comments