gnatprove_13.2.1_28fc3583/share/examples/spark/tokeneer/msgproc.adb

  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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
------------------------------------------------------------------
-- Tokeneer ID Station Support Software
--
-- Copyright (2003) United States Government, as represented
-- by the Director, National Security Agency. All rights reserved.
--
-- This material was originally developed by Praxis High Integrity
-- Systems Ltd. under contract to the National Security Agency.
------------------------------------------------------------------

------------------------------------------------------------------
-- MsgProc
--
-- Implementation notes:
--    None.
--
------------------------------------------------------------------

with Ada.Strings;
with Ada.Strings.Fixed;

package body MsgProc
  with SPARK_Mode => Off  --  exception handlers
is

   ------------------------------------------------------------------
   -- GetStringByPos
   --
   -- Implementation Notes:
   --    Assumes the response code has been removed.
   --
   --    Performed by searching for single quotes.
   --    Will not work any responses include additional single quotes.
   --
   ------------------------------------------------------------------
   function GetStringByPos (Msg : String;
                            Arg : Positive)
                           return String
   is
      ValStart, ValFin : Natural;
      LocalMsg         : String := Msg;
   begin

      -- Now loop through the list of arguments, deleting each
      -- as we find it
      for i in 1 .. Arg loop

         ValStart := Ada.Strings.Fixed.Index(Source  => LocalMsg,
                                             Pattern => "'");

         Ada.Strings.Fixed.Delete(Source  => LocalMsg,
                                  From    => 1,
                                  Through => ValStart);

         -- If closing quote is not found, return null string
         ValFin := Ada.Strings.Fixed.Index(Source  => LocalMsg,
                                           Pattern => "'");

         if i /= Arg then
            Ada.Strings.Fixed.Delete(Source  => LocalMsg,
                                     From    => 1,
                                     Through => ValFin);
         end if;
      end loop;

      return LocalMsg (1 .. ValFin-1);
   end GetStringByPos;

   ------------------------------------------------------------------
   -- GetDictionary
   --
   -- Implementation Notes:
   --    Performed by searching for open and close braces.
   --
   ------------------------------------------------------------------
   function GetDictionary (Msg : String;
                           Arg : Positive)
                          return DictionaryT is
      DicStart,
      DicFin,
      PrimaryCount,
      SecondaryCount : Natural := 0;
      PrimaryOpen    : Boolean := False;
      LocalReturn    : String := Msg;
   begin
      -- Search for the required dictionary, given by Arg.
      for i in 1 .. Msg'Length loop
         if Msg (i) = '{' then
            -- Found a dictionary start
            -- Is it a dictionary at the highest level?
            if not PrimaryOpen then
               PrimaryOpen := True;
               PrimaryCount := PrimaryCount + 1;
               -- Is this the dictionary we want?
               if PrimaryCount = Arg then
                  DicStart := i;
               end if;
            else
               -- an internal dictionary, increment count
               SecondaryCount := SecondaryCount + 1;
            end if;

         elsif Msg (i) = '}' then
            -- Found a dictionary end
            -- Is there an internal dictionary to close?
            if SecondaryCount /= 0 then
               SecondaryCount := SecondaryCount - 1;
            else
               PrimaryOpen := False;
               -- Is this the end of the dictionary we want?
               if PrimaryCount = Arg then
                  DicFin := i;
                  exit;
               end if;
            end if;
         end if;
      end loop;

      Ada.Strings.Fixed.Move
        (Source => Msg(DicStart+1 .. DicFin-1),
         Target => LocalReturn);

      return DictionaryT(LocalReturn(1..DicFin - 1 - DicStart));
   end GetDictionary;

   ------------------------------------------------------------------
   -- GetStringByKey
   --
   -- Implementation notes:
   --    Perform get by searching for the correct key, and then extracting
   --    the string within the next two single quotes.
   --    For this to work, ALL parameters in the reply must be of the form
   --    'Key' : 'Value' or 'Key' : {Dic}.
   --
   ------------------------------------------------------------------
   function GetStringByKey (Dic : DictionaryT;
                            Key : String)
                           return String
   is
      ValStart,
      ValFin,
      KeyStart  : TcpIp.MessageLengthT := 0;
      LocalMsg  : String := String(Dic);
   begin
      loop
         KeyStart := Ada.Strings.Fixed.Index(Source  => LocalMsg,
                                             Pattern => Key);

         if KeyStart /= 0 then
            -- Found the Key string - is it enclosed by single quotes?
            if LocalMsg(KeyStart - 1) = ''' and
               LocalMsg(KeyStart + Key'Length) = ''' then

               exit;

            else
               -- What we found was not the key - delete everything up to
               -- this point and search again.
               Ada.Strings.Fixed.Delete(Source  => LocalMsg,
                                        From    => 1,
                                        Through => KeyStart + Key'Length);
            end if;
         else
            -- Did not find the key - return null string
            exit;

         end if;
      end loop;

      if KeyStart /= 0 then
         -- should delete everything up to the key and its enclosing quote.
         Ada.Strings.Fixed.Delete(Source  => LocalMsg,
                                  From    => 1,
                                  Through => KeyStart + Key'Length);

         ValStart := Ada.Strings.Fixed.Index(Source  => LocalMsg,
                                             Pattern => "'");

         Ada.Strings.Fixed.Delete(Source  => LocalMsg,
                                  From    => 1,
                                  Through => ValStart);

         -- If closing quote is not found, return null string
         ValFin := Ada.Strings.Fixed.Index(Source  => LocalMsg,
                                           Pattern => "'") - 1;

      end if; -- else key does not appear, return null string

      return LocalMsg (1 .. ValFin);

   exception
      when E : others =>
         return "";
   end GetStringByKey;

   ------------------------------------------------------------------
   -- GetDictionaryByKey
   --
   -- Implementation notes:
   --    Perform get by searching for the correct key, and then extracting
   --    the string within the next two (matching) braces.
   --    For this to work, ALL parameters in the reply must be of the form
   --    'Key' : 'Value' or 'Key' : {Dic}.
   --
   ------------------------------------------------------------------
   function GetDictionaryByKey (Dic : DictionaryT;
                                Key : String)
                               return DictionaryT
   is
      KeyStart : TcpIp.MessageLengthT := 0;
      LocalMsg : String := String(Dic);
   begin

      loop
         KeyStart := Ada.Strings.Fixed.Index(Source  => LocalMsg,
                                             Pattern => Key);

         if KeyStart /= 0 then
            -- Found the Key string - is it enclosed by single quotes?
            if LocalMsg(KeyStart - 1) = ''' and
               LocalMsg(KeyStart + Key'Length) = ''' then

               -- We've found the Key - exit search loop
               exit;

            else
               -- What we found was not the key - delete everything up to
               -- this point and search again.
               Ada.Strings.Fixed.Delete(Source  => LocalMsg,
                                        From    => 1,
                                        Through => KeyStart + Key'Length);
            end if;
         else
            -- Did not find the key - return null string
            exit;

         end if;
      end loop;

      if KeyStart /= 0 then

         -- Delete everything up to the key.
         Ada.Strings.Fixed.Delete(Source  => LocalMsg,
                                  From    => 1,
                                  Through => KeyStart + Key'Length);

         return GetDictionary(Msg => LocalMsg,
                              Arg => 1);

      else -- key does not appear, return null string
         return "";
      end if;

   exception
      when E : others =>
         return "";
   end GetDictionaryByKey;

   ------------------------------------------------------------------
   -- GetResponseFromMsg
   --
   -- Implementation Notes:
   --    SPREs response code is the first 'string'.
   --
   ------------------------------------------------------------------
   function GetResponseFromMsg (Msg : in     TcpIp.MessageT) return String
   is
      CodeStart, CodeFin : Natural;
      LocalMsg           : String := Msg.Data(1..Msg.Length);
   begin

      -- Locally remove the SPRE response code...
      CodeStart := Ada.Strings.Fixed.Index(Source  => LocalMsg,
                                           Pattern => "'");

      -- delete start '
      Ada.Strings.Fixed.Delete(Source  => LocalMsg,
                               From    => CodeStart,
                               Through => CodeStart);

      CodeFin := Ada.Strings.Fixed.Index(Source  => LocalMsg,
                                         Pattern => "'");

      -- delete from code start to end '
      Ada.Strings.Fixed.Delete(Source  => LocalMsg,
                               From    => CodeStart,
                               Through => CodeFin);

      return LocalMsg;

   exception
      when E : others =>
         return "";
   end GetResponseFromMsg;

   ------------------------------------------------------------------
   -- StringFrom32
   --
   -- Implementation Notes:
   --    Trims the image of the number.
   --
   ------------------------------------------------------------------
   function StringFrom32 (Num : CommonTypes.Unsigned32T) return String is
   begin
      return Ada.Strings.Fixed.Trim
        (CommonTypes.Unsigned32T'Image(Num), Ada.Strings.Both);
   end StringFrom32;

   ------------------------------------------------------------------
   -- StringFromInt
   --
   -- Implementation Notes:
   --    Trims the image of the number.
   --
   ------------------------------------------------------------------
   function StringFromInt (Int : Integer) return String is
   begin
      return Ada.Strings.Fixed.Trim(Integer'Image(Int), Ada.Strings.Both);
   end StringFromInt;

end MsgProc;