Package: dune-grid / 2.6.0-3

0001-fix-base64-encoder.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
From 6c9b134f7aceb70e4e3ec346f12e9f97553c1bd5 Mon Sep 17 00:00:00 2001
From: Ansgar Burchardt <Ansgar.Burchardt@tu-dresden.de>
Date: Wed, 20 Jun 2018 19:54:22 +0200
Subject: [PATCH] fix base64 encoder

The old implementation relied on undefined behavior (reading from a
union member not most recently written to), implementation-defined
layout and only worked on little endian.

This should make the `vtktest` test pass on big endian architectures.
---
 dune/grid/io/file/vtk/b64enc.hh  | 49 ++++++++++++++------------------
 dune/grid/io/file/vtk/streams.hh | 12 ++++----
 2 files changed, 28 insertions(+), 33 deletions(-)

diff --git a/dune/grid/io/file/vtk/b64enc.hh b/dune/grid/io/file/vtk/b64enc.hh
index 0f0aacbb1..f0fc4af27 100644
--- a/dune/grid/io/file/vtk/b64enc.hh
+++ b/dune/grid/io/file/vtk/b64enc.hh
@@ -27,53 +27,48 @@ namespace Dune {
     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
   };
 
-  /** @brief struct with three bytes of text */
-  struct b64txt
+  /** @brief struct representing the three byte text as well as the four 6 bit chunks */
+  struct b64chunk
   {
-    typedef unsigned char size_type;
+    using size_type = unsigned char;
     size_type size;
     char txt[3];
+
+    void reset()
+    {
+      size = 0;
+      txt[0] = txt[1] = txt[2] = 0;
+    }
+
     int read(const char* t, size_type s)
     {
       size = s>=3 ? 3 : s;
-      txt[2] = s>0 ? t[0] : 0;
+      txt[0] = s>0 ? t[0] : 0;
       txt[1] = s>1 ? t[1] : 0;
-      txt[0] = s>2 ? t[2] : 0;
+      txt[2] = s>2 ? t[2] : 0;
       return size;
     }
+
     void put(const char c)
     {
       assert (size < 3);
-      txt[2-size++] = c;
+      txt[size++] = c;
     }
-  };
 
-  /** struct with four six bit chunks */
-  struct b64data
-  {
-    typedef unsigned char size_type;
-    size_type size;
-    unsigned A : 6;
-    unsigned B : 6;
-    unsigned C : 6;
-    unsigned D : 6;
     void write(char* t)
     {
-      t[3] = size>2 ? base64table[A] : '=';
-      t[2] = size>1 ? base64table[B] : '=';
-      t[1] = size>0 ? base64table[C] : '=';
-      t[0] = size>0 ? base64table[D] : '=';
+      const unsigned A = (txt[0] & 0b1111'1100) >> 2;
+      const unsigned B = (txt[0] & 0b0000'0011) << 4 | (txt[1] & 0b1111'0000) >> 4;
+      const unsigned C = (txt[1] & 0b0000'1111) << 2 | (txt[2] & 0b1100'0000) >> 6;
+      const unsigned D = txt[2] & 0b0011'1111;
+      t[0] = size>0 ? base64table[A] : '=';
+      t[1] = size>0 ? base64table[B] : '=';
+      t[2] = size>1 ? base64table[C] : '=';
+      t[3] = size>2 ? base64table[D] : '=';
       size = 0;
     }
   };
 
-  /** @brief union representing the three byte text as well as the four 6 bit chunks */
-  union b64chunk
-  {
-    b64txt txt;
-    b64data data;
-  };
-
   /** @} */
 
 } // namespace Dune
diff --git a/dune/grid/io/file/vtk/streams.hh b/dune/grid/io/file/vtk/streams.hh
index b6f84c7a2..2df7c26e1 100644
--- a/dune/grid/io/file/vtk/streams.hh
+++ b/dune/grid/io/file/vtk/streams.hh
@@ -26,7 +26,7 @@ namespace Dune {
       : s(s_)
     {
       // reset chunk
-      chunk.txt.read(0,0);
+      chunk.reset();
     }
 
     //! encode a data item
@@ -42,10 +42,10 @@ namespace Dune {
       char* p = reinterpret_cast<char*>(&data);
       for (size_t len = sizeof(X); len > 0; len--,p++)
       {
-        chunk.txt.put(*p);
-        if (chunk.txt.size == 3)
+        chunk.put(*p);
+        if (chunk.size == 3)
         {
-          chunk.data.write(obuf);
+          chunk.write(obuf);
           s.write(obuf,4);
         }
       }
@@ -61,9 +61,9 @@ namespace Dune {
      */
     void flush()
     {
-      if (chunk.txt.size > 0)
+      if (chunk.size > 0)
       {
-        chunk.data.write(obuf);
+        chunk.write(obuf);
         s.write(obuf,4);
       }
     }
-- 
2.17.1