anchor.diff:  %%      AVP MUST contain an example of the missing AVP complete with the
   %%      Vendor-Id if applicable.  The value field of the missing AVP
   %%      should be of correct minimum length and contain zeros.
  -
  -missing(Arities, Opts, Mod, AM) ->
  -    lists:foldl(fun(T,A) -> missing(T, AM, Opts, Mod, A) end,
  -                [],
  -                Arities).
  -
  -%% missing/5
  -
  -missing({Name, Arity}, AM, Opts, Mod, Acc) ->
  -    case missing(Name, Arity, AM) of
  -        true  -> [{5005, empty_avp(Name, Opts, Mod)} | Acc];
  -        false -> Acc
  +%%
  +%%   DIAMETER_AVP_OCCURS_TOO_MANY_TIMES 5009
  +%%      A message was received that included an AVP that appeared more
  +%%      often than permitted in the message definition.  The Failed-AVP
  +%%      AVP MUST be included and contain a copy of the first instance of
  +%%      the offending AVP that exceeded the maximum number of occurrences
  +
  +arities(Arities, Opts, Mod, AM, Avps) ->
  +    [Count, Map | More]
  +        = lists:foldl(fun({N,T}, A) -> more(N, T, Opts, Mod, AM, A) end,
  +                      [0, #{}],
  +                      Arities),
  +    less(Count, Map, Avps) ++ lists:reverse(More).
  +
  +%% more/6
  +
  +more(_Name, {0, '*'}, _Opts, _Mod, _AM, Acc) ->
  +    Acc;
  +
  +more(Name, {Mn, Mx}, Opts, Mod, AM, Acc) ->
  +    more(Name, Mn, Mx, Opts, Mod, AM, Acc);
  +
  +more(Name, 1, Opts, Mod, AM, Acc) ->
  +    more(Name, 1, 1, Opts, Mod, AM, Acc).
  +
  +%% more/7
  +
  +more(Name, Mn, Mx, Opts, Mod, AM, Acc) ->
  +    macc(Name, Mn, maps:get(Name, AM, 0), Mx, Opts, Mod, Acc).
  +
  +%% macc/7
  +
  +macc(Name, Mn, N, _, Opts, Mod, [M, Map | T])
  +  when N < Mn ->
  +    [M, Map, {5005, empty_avp(Name, Opts, Mod)} | T];
  +
  +macc(Name, _, N, Mx, _Opts, _Mod, [M, Map | T])
  +  when Mx < N ->
  +    K = N - Mx,
  +    [M + K, maps:put(Name, K, Map) | T];
  +
  +macc(_Name, _, _, _, _Opts, _Mod, Acc) ->
  +    Acc.
  +
  +%% less/3
  +
  +less(0, _, _) ->
  +    [];
  +
  +less(N, Map, [#diameter_avp{name = undefined} | Avps]) ->
  +    less(N, Map, Avps);
  +
  +less(N, Map, [#diameter_avp{name = Name} = Avp | Avps]) ->
  +    case Map of
  +        #{Name := 0} ->
  +            [{5009, Avp} | less(N-1, Map, Avps)];
  +        #{Name := M} ->
  +            less(N, maps:put(Name, M-1, Map), Avps);
  +        _ ->
  +            less(N, Map, Avps)
       end.
   
  -%% missing/3
  -
  -missing(Name, Arity, AM) ->
  -    'AVP' /= Name andalso too_few(Name, Arity, AM).
  -
  -%% too_few/3
  -%%
  -%% Maximum arities have already been checked during the decode.
  -
  -too_few(_, {0, _}, _) ->
  -    false;
  -
  -too_few(FieldName, 1, AM) ->
  -    not maps:is_key(FieldName, AM);
  -
  -too_few(FieldName, {M, _}, AM) ->
  -    maps:get(FieldName, AM, 0) < M.
  -
   %% empty_avp/3
  +
  +empty_avp('AVP', _, _) ->
  +    #diameter_avp{data = <<0:64>>};
   
   empty_avp(Name, Opts, Mod) ->
       {Code, Flags, VId} = Mod:avp_header(Name),
  
positive.code:             A = Avp#diameter_avp{name = AvpName,
                                   value = Rec,
                                   type = Type},
              {[A|As], pack_avp(Name, A, Opts, Mod, Acc)};
  
          V when Type /= 'Grouped' ->
              A = Avp#diameter_avp{name = AvpName,
                                   value = V,
                                   type = Type},
              {A, pack_avp(Name, A, Opts, Mod, Acc)}
      catch
          throw: {?MODULE, {grouped, Error, ComponentAvps}} ->
              decode_error(Name,
  
negative.code:     pack(Arity, AvpName, Avp, Mod, Acc).
  
  %% pack_AVP/5
  
  pack_AVP(Name, Avp, Opts, Mod, Acc) ->
      pack_AVP(Name, Mod:avp_arity(Name, 'AVP'), Avp, Opts, Mod, Acc).
  
  %% pack_AVP/6
  
  %% Length failure was induced because of a header/payload length
  %% mismatch. The AVP Length is reset to match the received data if
  