From 85e44c2daea34f90d2b5cf51bbe525c4d811c310 Mon Sep 17 00:00:00 2001
From: jean-pierre charras <jp.charras@wanadoo.fr>
Date: Wed, 9 Nov 2022 09:33:51 +0100
Subject: [PATCH] pcbnew, SVG import: fix an issue for SVG files using a CR+LF
 end of file The issue was in a validity test working only if CR+LF is not
 replaced by LF Fixes #10096 https://gitlab.com/kicad/code/kicad/issues/10096

---
 pcbnew/import_gfx/svg_import_plugin.cpp | 7 +++++--
 thirdparty/nanosvg/nanosvg.cpp          | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/pcbnew/import_gfx/svg_import_plugin.cpp b/pcbnew/import_gfx/svg_import_plugin.cpp
index 903aaea5d0..d6fb4efda4 100644
--- a/pcbnew/import_gfx/svg_import_plugin.cpp
+++ b/pcbnew/import_gfx/svg_import_plugin.cpp
@@ -54,8 +54,11 @@ bool SVG_IMPORT_PLUGIN::Load( const wxString& aFileName )
 {
     wxCHECK( m_importer, false );
 
-    // wxFopen takes care of unicode filenames across platforms
-    FILE* fp = wxFopen( aFileName, wxT( "rt" ) );
+    // 1- wxFopen takes care of unicode filenames across platforms
+    // 2 - nanosvg (exactly nsvgParseFromFile) expects a binary file (exactly the CRLF eof must
+    // not be replaced by LF and changes the byte count) in one validity test,
+    // so open it in binary mode.
+    FILE* fp = wxFopen( aFileName, wxT( "rb" ) );
 
     if( fp == nullptr )
         return false;
diff --git a/thirdparty/nanosvg/nanosvg.cpp b/thirdparty/nanosvg/nanosvg.cpp
index b6f3ca559b..2444b429b8 100644
--- a/thirdparty/nanosvg/nanosvg.cpp
+++ b/thirdparty/nanosvg/nanosvg.cpp
@@ -3688,6 +3688,8 @@ NSVGimage* nsvgParseFromFile( FILE *fp, const char* units, float dpi )
     if( data == NULL )
         goto error;
 
+    // This test works only if fp is open in binary mode, i.e. if the CRLF eol is not
+    // replaced by LF when reading the file
     if( fread( data, 1, size, fp ) != size )
         goto error;