Skip to content

Commit

Permalink
#15: use the C locale to portably parse the PDF header
Browse files Browse the repository at this point in the history
On a machine where the locale is set to something not equivalent to
ASCII, the call to `awk' in `get_pdf_version()' will throw the following
error:

  awk: cmd. line:1: (FILENAME=- FNR=1) warning: Invalid multibyte data
  detected. There may be a mismatch between your data and your locale.

This can be fixed by forcing the C locale by prepending the call to awk
with `LC_ALL=C'.

Reported and fixed by Laurent MUGNIER.
  • Loading branch information
aklomp committed Aug 17, 2023
1 parent 376daa7 commit 68f38c5
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions shrinkpdf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# https://github.com/aklomp/shrinkpdf
# Licensed under the 3-clause BSD license:
#
# Copyright (c) 2014-2022, Alfred Klomp
# Copyright (c) 2014-2023, Alfred Klomp
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -65,13 +65,12 @@ get_pdf_version ()
{
# $1 is the input file. The PDF version is contained in the
# first 1024 bytes and will be extracted from the PDF file.
pdf_version=$(cut -b -1024 "$1" | awk 'BEGIN { found=0 }{ if (match($0, "%PDF-[0-9]\\.[0-9]") && ! found) { print substr($0, RSTART + 5, 3); found=1 } }')
pdf_version=$(cut -b -1024 "$1" | LC_ALL=C awk 'BEGIN { found=0 }{ if (match($0, "%PDF-[0-9]\\.[0-9]") && ! found) { print substr($0, RSTART + 5, 3); found=1 } }')
if [ -z "$pdf_version" ] || [ "${#pdf_version}" != "3" ]; then
return 1
fi
}


check_input_file ()
{
# Check if the given file exists.
Expand Down

0 comments on commit 68f38c5

Please sign in to comment.