Natools

Check-in [210677c061]
Login
Overview
Comment:smaz_implementations-base_64: rewrite variable-length verbatim code

At some point in the development I changed the base-64 scheme, but failed to propagate it everywhere. Mostly I intend to use only one mode in base-64, and the other one is merely a place-holder, which explains how it got neglected. Now at least it works, even if it's useless.

Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 210677c0612ba94cc105be42f18c5d010cd089f7
User & Date: nat on 2017-01-07 22:53:28
Other Links: manifest | tags
Context
2017-01-27
20:44
tools/timekey: refactor command-line argument processing check-in: f9d3f1aa38 user: nat tags: trunk
2017-01-07
22:53
smaz_implementations-base_64: rewrite variable-length verbatim code

At some point in the development I changed the base-64 scheme, but failed to propagate it everywhere. Mostly I intend to use only one mode in base-64, and the other one is merely a place-holder, which explains how it got neglected. Now at least it works, even if it's useless. check-in: 210677c061 user: nat tags: trunk

2017-01-06
22:44
smaz_tests: add a test for base-64 variable-length verbatim check-in: 3e04c004e7 user: nat tags: trunk
Changes

Modified src/natools-smaz_implementations-base_64.adb from [1a162cf272] to [de5cce5c1a].

39
40
41
42
43
44
45

46





47
48
49
50
51
52
53
39
40
41
42
43
44
45
46

47
48
49
50
51
52
53
54
55
56
57
58







+
-
+
+
+
+
+







   begin
      Tools.Next_Digit (Input, Offset, Code);

      if Code <= Last_Code then
         Verbatim_Length := 0;

      elsif Variable_Length_Verbatim then
         if Code < 63 then
         Verbatim_Length := 64 - Natural (Code);
            Verbatim_Length := 63 - Natural (Code);
         else
            Tools.Next_Digit (Input, Offset, Code);
            Verbatim_Length := Natural (Code) + 63 - Natural (Last_Code);
         end if;
         Code := 0;

      elsif Code = 63 then
         Tools.Next_Digit (Input, Offset, Code);
         Verbatim_Length := Natural (Code) * 3 + 3;
         Code := 0;

112
113
114
115
116
117
118

119
120


121
122
123


124
125
126
127



128
129
130
131
132
133
134
117
118
119
120
121
122
123
124


125
126
127
128
129
130
131
132



133
134
135
136
137
138
139
140
141
142







+
-
-
+
+



+
+

-
-
-
+
+
+







     (Input_Length : in Positive;
      Last_Code : in Natools.Smaz_Implementations.Base_64_Tools.Base_64_Digit;
      Variable_Length_Verbatim : in Boolean)
     return Ada.Streams.Stream_Element_Count is
   begin
      if Variable_Length_Verbatim then
         declare
            Largest_Single : constant Positive := 62 - Natural (Last_Code);
            Largest_Run : constant Positive := 63 - Natural (Last_Code);
            Run_Count : constant Positive
            Largest_Run : constant Positive := 64 + Largest_Single;
            Run_Count : constant Natural
              := (Input_Length + Largest_Run - 1) / Largest_Run;
            Last_Run_Size : constant Positive
              := Input_Length - (Run_Count - 1) * Largest_Run;
            Last_Run_Header_Size : constant Ada.Streams.Stream_Element_Count
              := (if Last_Run_Size > Largest_Single then 2 else 1);
         begin
            return Ada.Streams.Stream_Element_Count (Run_Count - 1)
                    * (Tools.Image_Length (Largest_Run) + 1)
                 + Tools.Image_Length (Last_Run_Size) + 1;
            return Ada.Streams.Stream_Element_Count (Run_Count)
                    * (Tools.Image_Length (Largest_Run) + 2)
                 + Tools.Image_Length (Last_Run_Size) + Last_Run_Header_Size;
         end;
      else
         declare
            Largest_Prefix : constant Natural
              := (case Input_Length mod 3 is
                  when 1 => 15 * 3 + 1,
                  when 2 => ((62 - Natural (Last_Code)) * 4 - 1) * 3 + 2,
173
174
175
176
177
178
179

180
181


182
183
184
185
186
187
188
189
190




























191
192
193
194
195
196
197
181
182
183
184
185
186
187
188


189
190
191
192
193






194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228







+
-
-
+
+



-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







      Last_Code : in Natools.Smaz_Implementations.Base_64_Tools.Base_64_Digit;
      Variable_Length_Verbatim : in Boolean)
   is
      Index : Positive := Input'First;
   begin
      if Variable_Length_Verbatim then
         declare
            Largest_Single : constant Positive := 62 - Natural (Last_Code);
            Largest_Run : constant Positive := 63 - Natural (Last_Code);
            Length, Last : Positive;
            Largest_Run : constant Positive := 64 + Largest_Single;
            Length, Last : Natural;
         begin
            while Index in Input'Range loop
               Length := Positive'Min (Largest_Run, Input'Last + 1 - Index);
               Last := Index + Length - 1;
               Output (Offset)
                 := Tools.Image (63 - Tools.Base_64_Digit (Length - 1));
               Offset := Offset + 1;
               Tools.Encode (Input (Index .. Last), Output, Offset);
               Index := Last + 1;

               if Length > Largest_Single then
                  Write_Code (Output, Offset, 63);
                  Write_Code
                    (Output, Offset,
                     Tools.Base_64_Digit (Length - Largest_Single - 1));
               else
                  Write_Code
                    (Output, Offset,
                     Tools.Base_64_Digit (63 - Length));
               end if;

               if Length mod 3 = 1 then
                  Tools.Encode_Single (Input (Index), 0, Output, Offset);
                  Index := Index + 1;
                  Length := Length - 1;
               elsif Length mod 3 = 2 then
                  Tools.Encode_Double
                    (Input (Index .. Index + 1), 0, Output, Offset);
                  Index := Index + 2;
                  Length := Length - 2;
               end if;

               if Length > 0 then
                  Last := Index + Length - 1;
                  Tools.Encode (Input (Index .. Last), Output, Offset);
                  Index := Last + 1;
               end if;
            end loop;
         end;
      else
         if Input'Length mod 3 = 1 then
            declare
               Extra_Blocks : constant Natural
                 := Natural'Min (15, Input'Length / 3);