Codebase list erlang-p1-xmpp / HEAD src / xep0084.erl
HEAD

Tree @HEAD (Download .tar.gz)

xep0084.erl @HEADraw · history · blame

  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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
%% Created automatically by XML generator (fxml_gen.erl)
%% Source: xmpp_codec.spec

-module(xep0084).

-compile(export_all).

do_decode(<<"metadata">>,
          <<"urn:xmpp:avatar:metadata">>, El, Opts) ->
    decode_avatar_meta(<<"urn:xmpp:avatar:metadata">>,
                       Opts,
                       El);
do_decode(<<"pointer">>, <<"urn:xmpp:avatar:metadata">>,
          El, Opts) ->
    decode_avatar_pointer(<<"urn:xmpp:avatar:metadata">>,
                          Opts,
                          El);
do_decode(<<"info">>, <<"urn:xmpp:avatar:metadata">>,
          El, Opts) ->
    decode_avatar_info(<<"urn:xmpp:avatar:metadata">>,
                       Opts,
                       El);
do_decode(<<"data">>, <<"urn:xmpp:avatar:data">>, El,
          Opts) ->
    decode_avatar_data(<<"urn:xmpp:avatar:data">>,
                       Opts,
                       El);
do_decode(Name, <<>>, _, _) ->
    erlang:error({xmpp_codec, {missing_tag_xmlns, Name}});
do_decode(Name, XMLNS, _, _) ->
    erlang:error({xmpp_codec, {unknown_tag, Name, XMLNS}}).

tags() ->
    [{<<"metadata">>, <<"urn:xmpp:avatar:metadata">>},
     {<<"pointer">>, <<"urn:xmpp:avatar:metadata">>},
     {<<"info">>, <<"urn:xmpp:avatar:metadata">>},
     {<<"data">>, <<"urn:xmpp:avatar:data">>}].

do_encode({avatar_data, _} = Data, TopXMLNS) ->
    encode_avatar_data(Data, TopXMLNS);
do_encode({avatar_info, _, _, _, _, _, _} = Info,
          TopXMLNS) ->
    encode_avatar_info(Info, TopXMLNS);
do_encode({avatar_pointer, _, _, _, _, _, _} = Pointer,
          TopXMLNS) ->
    encode_avatar_pointer(Pointer, TopXMLNS);
do_encode({avatar_meta, _, _} = Metadata, TopXMLNS) ->
    encode_avatar_meta(Metadata, TopXMLNS).

do_get_name({avatar_data, _}) -> <<"data">>;
do_get_name({avatar_info, _, _, _, _, _, _}) ->
    <<"info">>;
do_get_name({avatar_meta, _, _}) -> <<"metadata">>;
do_get_name({avatar_pointer, _, _, _, _, _, _}) ->
    <<"pointer">>.

do_get_ns({avatar_data, _}) ->
    <<"urn:xmpp:avatar:data">>;
do_get_ns({avatar_info, _, _, _, _, _, _}) ->
    <<"urn:xmpp:avatar:metadata">>;
do_get_ns({avatar_meta, _, _}) ->
    <<"urn:xmpp:avatar:metadata">>;
do_get_ns({avatar_pointer, _, _, _, _, _, _}) ->
    <<"urn:xmpp:avatar:metadata">>.

get_els({avatar_pointer,
         _bytes,
         _id,
         _type,
         _height,
         _width,
         _sub_els}) ->
    _sub_els.

set_els({avatar_pointer,
         _bytes,
         _id,
         _type,
         _height,
         _width,
         _},
        _sub_els) ->
    {avatar_pointer,
     _bytes,
     _id,
     _type,
     _height,
     _width,
     _sub_els}.

pp(avatar_data, 1) -> [data];
pp(avatar_info, 6) ->
    [bytes, id, type, height, width, url];
pp(avatar_pointer, 6) ->
    [bytes, id, type, height, width, sub_els];
pp(avatar_meta, 2) -> [info, pointer];
pp(_, _) -> no.

records() ->
    [{avatar_data, 1},
     {avatar_info, 6},
     {avatar_pointer, 6},
     {avatar_meta, 2}].

dec_int(Val, Min, Max) ->
    case erlang:binary_to_integer(Val) of
        Int when Int =< Max, Min == infinity -> Int;
        Int when Int =< Max, Int >= Min -> Int
    end.

enc_int(Int) -> erlang:integer_to_binary(Int).

decode_avatar_meta(__TopXMLNS, __Opts,
                   {xmlel, <<"metadata">>, _attrs, _els}) ->
    {Pointer, Info} = decode_avatar_meta_els(__TopXMLNS,
                                             __Opts,
                                             _els,
                                             undefined,
                                             []),
    {avatar_meta, Info, Pointer}.

decode_avatar_meta_els(__TopXMLNS, __Opts, [], Pointer,
                       Info) ->
    {Pointer, lists:reverse(Info)};
decode_avatar_meta_els(__TopXMLNS, __Opts,
                       [{xmlel, <<"info">>, _attrs, _} = _el | _els], Pointer,
                       Info) ->
    case xmpp_codec:get_attr(<<"xmlns">>,
                             _attrs,
                             __TopXMLNS)
        of
        <<"urn:xmpp:avatar:metadata">> ->
            decode_avatar_meta_els(__TopXMLNS,
                                   __Opts,
                                   _els,
                                   Pointer,
                                   [decode_avatar_info(<<"urn:xmpp:avatar:metadata">>,
                                                       __Opts,
                                                       _el)
                                    | Info]);
        _ ->
            decode_avatar_meta_els(__TopXMLNS,
                                   __Opts,
                                   _els,
                                   Pointer,
                                   Info)
    end;
decode_avatar_meta_els(__TopXMLNS, __Opts,
                       [{xmlel, <<"pointer">>, _attrs, _} = _el | _els],
                       Pointer, Info) ->
    case xmpp_codec:get_attr(<<"xmlns">>,
                             _attrs,
                             __TopXMLNS)
        of
        <<"urn:xmpp:avatar:metadata">> ->
            decode_avatar_meta_els(__TopXMLNS,
                                   __Opts,
                                   _els,
                                   decode_avatar_pointer(<<"urn:xmpp:avatar:metadata">>,
                                                         __Opts,
                                                         _el),
                                   Info);
        _ ->
            decode_avatar_meta_els(__TopXMLNS,
                                   __Opts,
                                   _els,
                                   Pointer,
                                   Info)
    end;
decode_avatar_meta_els(__TopXMLNS, __Opts, [_ | _els],
                       Pointer, Info) ->
    decode_avatar_meta_els(__TopXMLNS,
                           __Opts,
                           _els,
                           Pointer,
                           Info).

encode_avatar_meta({avatar_meta, Info, Pointer},
                   __TopXMLNS) ->
    __NewTopXMLNS =
        xmpp_codec:choose_top_xmlns(<<"urn:xmpp:avatar:metadata">>,
                                    [],
                                    __TopXMLNS),
    _els =
        lists:reverse('encode_avatar_meta_$pointer'(Pointer,
                                                    __NewTopXMLNS,
                                                    'encode_avatar_meta_$info'(Info,
                                                                               __NewTopXMLNS,
                                                                               []))),
    _attrs = xmpp_codec:enc_xmlns_attrs(__NewTopXMLNS,
                                        __TopXMLNS),
    {xmlel, <<"metadata">>, _attrs, _els}.

'encode_avatar_meta_$pointer'(undefined, __TopXMLNS,
                              _acc) ->
    _acc;
'encode_avatar_meta_$pointer'(Pointer, __TopXMLNS,
                              _acc) ->
    [encode_avatar_pointer(Pointer, __TopXMLNS) | _acc].

'encode_avatar_meta_$info'([], __TopXMLNS, _acc) ->
    _acc;
'encode_avatar_meta_$info'([Info | _els], __TopXMLNS,
                           _acc) ->
    'encode_avatar_meta_$info'(_els,
                               __TopXMLNS,
                               [encode_avatar_info(Info, __TopXMLNS) | _acc]).

decode_avatar_pointer(__TopXMLNS, __Opts,
                      {xmlel, <<"pointer">>, _attrs, _els}) ->
    __Els = decode_avatar_pointer_els(__TopXMLNS,
                                      __Opts,
                                      _els,
                                      []),
    {Bytes, Id, Type, Height, Width} =
        decode_avatar_pointer_attrs(__TopXMLNS,
                                    _attrs,
                                    undefined,
                                    undefined,
                                    undefined,
                                    undefined,
                                    undefined),
    {avatar_pointer, Bytes, Id, Type, Height, Width, __Els}.

decode_avatar_pointer_els(__TopXMLNS, __Opts, [],
                          __Els) ->
    lists:reverse(__Els);
decode_avatar_pointer_els(__TopXMLNS, __Opts,
                          [{xmlel, _name, _attrs, _} = _el | _els], __Els) ->
    decode_avatar_pointer_els(__TopXMLNS,
                              __Opts,
                              _els,
                              [_el | __Els]);
decode_avatar_pointer_els(__TopXMLNS, __Opts,
                          [_ | _els], __Els) ->
    decode_avatar_pointer_els(__TopXMLNS,
                              __Opts,
                              _els,
                              __Els).

decode_avatar_pointer_attrs(__TopXMLNS,
                            [{<<"bytes">>, _val} | _attrs], _Bytes, Id, Type,
                            Height, Width) ->
    decode_avatar_pointer_attrs(__TopXMLNS,
                                _attrs,
                                _val,
                                Id,
                                Type,
                                Height,
                                Width);
decode_avatar_pointer_attrs(__TopXMLNS,
                            [{<<"id">>, _val} | _attrs], Bytes, _Id, Type,
                            Height, Width) ->
    decode_avatar_pointer_attrs(__TopXMLNS,
                                _attrs,
                                Bytes,
                                _val,
                                Type,
                                Height,
                                Width);
decode_avatar_pointer_attrs(__TopXMLNS,
                            [{<<"type">>, _val} | _attrs], Bytes, Id, _Type,
                            Height, Width) ->
    decode_avatar_pointer_attrs(__TopXMLNS,
                                _attrs,
                                Bytes,
                                Id,
                                _val,
                                Height,
                                Width);
decode_avatar_pointer_attrs(__TopXMLNS,
                            [{<<"height">>, _val} | _attrs], Bytes, Id, Type,
                            _Height, Width) ->
    decode_avatar_pointer_attrs(__TopXMLNS,
                                _attrs,
                                Bytes,
                                Id,
                                Type,
                                _val,
                                Width);
decode_avatar_pointer_attrs(__TopXMLNS,
                            [{<<"width">>, _val} | _attrs], Bytes, Id, Type,
                            Height, _Width) ->
    decode_avatar_pointer_attrs(__TopXMLNS,
                                _attrs,
                                Bytes,
                                Id,
                                Type,
                                Height,
                                _val);
decode_avatar_pointer_attrs(__TopXMLNS, [_ | _attrs],
                            Bytes, Id, Type, Height, Width) ->
    decode_avatar_pointer_attrs(__TopXMLNS,
                                _attrs,
                                Bytes,
                                Id,
                                Type,
                                Height,
                                Width);
decode_avatar_pointer_attrs(__TopXMLNS, [], Bytes, Id,
                            Type, Height, Width) ->
    {decode_avatar_pointer_attr_bytes(__TopXMLNS, Bytes),
     decode_avatar_pointer_attr_id(__TopXMLNS, Id),
     decode_avatar_pointer_attr_type(__TopXMLNS, Type),
     decode_avatar_pointer_attr_height(__TopXMLNS, Height),
     decode_avatar_pointer_attr_width(__TopXMLNS, Width)}.

encode_avatar_pointer({avatar_pointer,
                       Bytes,
                       Id,
                       Type,
                       Height,
                       Width,
                       __Els},
                      __TopXMLNS) ->
    __NewTopXMLNS =
        xmpp_codec:choose_top_xmlns(<<"urn:xmpp:avatar:metadata">>,
                                    [],
                                    __TopXMLNS),
    _els = [xmpp_codec:encode(_el, __NewTopXMLNS)
            || _el <- __Els],
    _attrs = encode_avatar_pointer_attr_width(Width,
                                              encode_avatar_pointer_attr_height(Height,
                                                                                encode_avatar_pointer_attr_type(Type,
                                                                                                                encode_avatar_pointer_attr_id(Id,
                                                                                                                                              encode_avatar_pointer_attr_bytes(Bytes,
                                                                                                                                                                               xmpp_codec:enc_xmlns_attrs(__NewTopXMLNS,
                                                                                                                                                                                                          __TopXMLNS)))))),
    {xmlel, <<"pointer">>, _attrs, _els}.

decode_avatar_pointer_attr_bytes(__TopXMLNS,
                                 undefined) ->
    undefined;
decode_avatar_pointer_attr_bytes(__TopXMLNS, _val) ->
    case catch dec_int(_val, 0, infinity) of
        {'EXIT', _} ->
            erlang:error({xmpp_codec,
                          {bad_attr_value,
                           <<"bytes">>,
                           <<"pointer">>,
                           __TopXMLNS}});
        _res -> _res
    end.

encode_avatar_pointer_attr_bytes(undefined, _acc) ->
    _acc;
encode_avatar_pointer_attr_bytes(_val, _acc) ->
    [{<<"bytes">>, enc_int(_val)} | _acc].

decode_avatar_pointer_attr_id(__TopXMLNS, undefined) ->
    <<>>;
decode_avatar_pointer_attr_id(__TopXMLNS, _val) -> _val.

encode_avatar_pointer_attr_id(<<>>, _acc) -> _acc;
encode_avatar_pointer_attr_id(_val, _acc) ->
    [{<<"id">>, _val} | _acc].

decode_avatar_pointer_attr_type(__TopXMLNS,
                                undefined) ->
    <<>>;
decode_avatar_pointer_attr_type(__TopXMLNS, _val) ->
    _val.

encode_avatar_pointer_attr_type(<<>>, _acc) -> _acc;
encode_avatar_pointer_attr_type(_val, _acc) ->
    [{<<"type">>, _val} | _acc].

decode_avatar_pointer_attr_height(__TopXMLNS,
                                  undefined) ->
    undefined;
decode_avatar_pointer_attr_height(__TopXMLNS, _val) ->
    case catch dec_int(_val, 0, infinity) of
        {'EXIT', _} ->
            erlang:error({xmpp_codec,
                          {bad_attr_value,
                           <<"height">>,
                           <<"pointer">>,
                           __TopXMLNS}});
        _res -> _res
    end.

encode_avatar_pointer_attr_height(undefined, _acc) ->
    _acc;
encode_avatar_pointer_attr_height(_val, _acc) ->
    [{<<"height">>, enc_int(_val)} | _acc].

decode_avatar_pointer_attr_width(__TopXMLNS,
                                 undefined) ->
    undefined;
decode_avatar_pointer_attr_width(__TopXMLNS, _val) ->
    case catch dec_int(_val, 0, infinity) of
        {'EXIT', _} ->
            erlang:error({xmpp_codec,
                          {bad_attr_value,
                           <<"width">>,
                           <<"pointer">>,
                           __TopXMLNS}});
        _res -> _res
    end.

encode_avatar_pointer_attr_width(undefined, _acc) ->
    _acc;
encode_avatar_pointer_attr_width(_val, _acc) ->
    [{<<"width">>, enc_int(_val)} | _acc].

decode_avatar_info(__TopXMLNS, __Opts,
                   {xmlel, <<"info">>, _attrs, _els}) ->
    {Bytes, Id, Type, Url, Height, Width} =
        decode_avatar_info_attrs(__TopXMLNS,
                                 _attrs,
                                 undefined,
                                 undefined,
                                 undefined,
                                 undefined,
                                 undefined,
                                 undefined),
    {avatar_info, Bytes, Id, Type, Height, Width, Url}.

decode_avatar_info_attrs(__TopXMLNS,
                         [{<<"bytes">>, _val} | _attrs], _Bytes, Id, Type, Url,
                         Height, Width) ->
    decode_avatar_info_attrs(__TopXMLNS,
                             _attrs,
                             _val,
                             Id,
                             Type,
                             Url,
                             Height,
                             Width);
decode_avatar_info_attrs(__TopXMLNS,
                         [{<<"id">>, _val} | _attrs], Bytes, _Id, Type, Url,
                         Height, Width) ->
    decode_avatar_info_attrs(__TopXMLNS,
                             _attrs,
                             Bytes,
                             _val,
                             Type,
                             Url,
                             Height,
                             Width);
decode_avatar_info_attrs(__TopXMLNS,
                         [{<<"type">>, _val} | _attrs], Bytes, Id, _Type, Url,
                         Height, Width) ->
    decode_avatar_info_attrs(__TopXMLNS,
                             _attrs,
                             Bytes,
                             Id,
                             _val,
                             Url,
                             Height,
                             Width);
decode_avatar_info_attrs(__TopXMLNS,
                         [{<<"url">>, _val} | _attrs], Bytes, Id, Type, _Url,
                         Height, Width) ->
    decode_avatar_info_attrs(__TopXMLNS,
                             _attrs,
                             Bytes,
                             Id,
                             Type,
                             _val,
                             Height,
                             Width);
decode_avatar_info_attrs(__TopXMLNS,
                         [{<<"height">>, _val} | _attrs], Bytes, Id, Type, Url,
                         _Height, Width) ->
    decode_avatar_info_attrs(__TopXMLNS,
                             _attrs,
                             Bytes,
                             Id,
                             Type,
                             Url,
                             _val,
                             Width);
decode_avatar_info_attrs(__TopXMLNS,
                         [{<<"width">>, _val} | _attrs], Bytes, Id, Type, Url,
                         Height, _Width) ->
    decode_avatar_info_attrs(__TopXMLNS,
                             _attrs,
                             Bytes,
                             Id,
                             Type,
                             Url,
                             Height,
                             _val);
decode_avatar_info_attrs(__TopXMLNS, [_ | _attrs],
                         Bytes, Id, Type, Url, Height, Width) ->
    decode_avatar_info_attrs(__TopXMLNS,
                             _attrs,
                             Bytes,
                             Id,
                             Type,
                             Url,
                             Height,
                             Width);
decode_avatar_info_attrs(__TopXMLNS, [], Bytes, Id,
                         Type, Url, Height, Width) ->
    {decode_avatar_info_attr_bytes(__TopXMLNS, Bytes),
     decode_avatar_info_attr_id(__TopXMLNS, Id),
     decode_avatar_info_attr_type(__TopXMLNS, Type),
     decode_avatar_info_attr_url(__TopXMLNS, Url),
     decode_avatar_info_attr_height(__TopXMLNS, Height),
     decode_avatar_info_attr_width(__TopXMLNS, Width)}.

encode_avatar_info({avatar_info,
                    Bytes,
                    Id,
                    Type,
                    Height,
                    Width,
                    Url},
                   __TopXMLNS) ->
    __NewTopXMLNS =
        xmpp_codec:choose_top_xmlns(<<"urn:xmpp:avatar:metadata">>,
                                    [],
                                    __TopXMLNS),
    _els = [],
    _attrs = encode_avatar_info_attr_width(Width,
                                           encode_avatar_info_attr_height(Height,
                                                                          encode_avatar_info_attr_url(Url,
                                                                                                      encode_avatar_info_attr_type(Type,
                                                                                                                                   encode_avatar_info_attr_id(Id,
                                                                                                                                                              encode_avatar_info_attr_bytes(Bytes,
                                                                                                                                                                                            xmpp_codec:enc_xmlns_attrs(__NewTopXMLNS,
                                                                                                                                                                                                                       __TopXMLNS))))))),
    {xmlel, <<"info">>, _attrs, _els}.

decode_avatar_info_attr_bytes(__TopXMLNS, undefined) ->
    erlang:error({xmpp_codec,
                  {missing_attr, <<"bytes">>, <<"info">>, __TopXMLNS}});
decode_avatar_info_attr_bytes(__TopXMLNS, _val) ->
    case catch dec_int(_val, 0, infinity) of
        {'EXIT', _} ->
            erlang:error({xmpp_codec,
                          {bad_attr_value,
                           <<"bytes">>,
                           <<"info">>,
                           __TopXMLNS}});
        _res -> _res
    end.

encode_avatar_info_attr_bytes(_val, _acc) ->
    [{<<"bytes">>, enc_int(_val)} | _acc].

decode_avatar_info_attr_id(__TopXMLNS, undefined) ->
    erlang:error({xmpp_codec,
                  {missing_attr, <<"id">>, <<"info">>, __TopXMLNS}});
decode_avatar_info_attr_id(__TopXMLNS, _val) -> _val.

encode_avatar_info_attr_id(_val, _acc) ->
    [{<<"id">>, _val} | _acc].

decode_avatar_info_attr_type(__TopXMLNS, undefined) ->
    erlang:error({xmpp_codec,
                  {missing_attr, <<"type">>, <<"info">>, __TopXMLNS}});
decode_avatar_info_attr_type(__TopXMLNS, _val) -> _val.

encode_avatar_info_attr_type(_val, _acc) ->
    [{<<"type">>, _val} | _acc].

decode_avatar_info_attr_url(__TopXMLNS, undefined) ->
    <<>>;
decode_avatar_info_attr_url(__TopXMLNS, _val) -> _val.

encode_avatar_info_attr_url(<<>>, _acc) -> _acc;
encode_avatar_info_attr_url(_val, _acc) ->
    [{<<"url">>, _val} | _acc].

decode_avatar_info_attr_height(__TopXMLNS, undefined) ->
    undefined;
decode_avatar_info_attr_height(__TopXMLNS, _val) ->
    case catch dec_int(_val, 0, infinity) of
        {'EXIT', _} ->
            erlang:error({xmpp_codec,
                          {bad_attr_value,
                           <<"height">>,
                           <<"info">>,
                           __TopXMLNS}});
        _res -> _res
    end.

encode_avatar_info_attr_height(undefined, _acc) -> _acc;
encode_avatar_info_attr_height(_val, _acc) ->
    [{<<"height">>, enc_int(_val)} | _acc].

decode_avatar_info_attr_width(__TopXMLNS, undefined) ->
    undefined;
decode_avatar_info_attr_width(__TopXMLNS, _val) ->
    case catch dec_int(_val, 0, infinity) of
        {'EXIT', _} ->
            erlang:error({xmpp_codec,
                          {bad_attr_value,
                           <<"width">>,
                           <<"info">>,
                           __TopXMLNS}});
        _res -> _res
    end.

encode_avatar_info_attr_width(undefined, _acc) -> _acc;
encode_avatar_info_attr_width(_val, _acc) ->
    [{<<"width">>, enc_int(_val)} | _acc].

decode_avatar_data(__TopXMLNS, __Opts,
                   {xmlel, <<"data">>, _attrs, _els}) ->
    Data = decode_avatar_data_els(__TopXMLNS,
                                  __Opts,
                                  _els,
                                  <<>>),
    {avatar_data, Data}.

decode_avatar_data_els(__TopXMLNS, __Opts, [], Data) ->
    decode_avatar_data_cdata(__TopXMLNS, Data);
decode_avatar_data_els(__TopXMLNS, __Opts,
                       [{xmlcdata, _data} | _els], Data) ->
    decode_avatar_data_els(__TopXMLNS,
                           __Opts,
                           _els,
                           <<Data/binary, _data/binary>>);
decode_avatar_data_els(__TopXMLNS, __Opts, [_ | _els],
                       Data) ->
    decode_avatar_data_els(__TopXMLNS, __Opts, _els, Data).

encode_avatar_data({avatar_data, Data}, __TopXMLNS) ->
    __NewTopXMLNS =
        xmpp_codec:choose_top_xmlns(<<"urn:xmpp:avatar:data">>,
                                    [],
                                    __TopXMLNS),
    _els = encode_avatar_data_cdata(Data, []),
    _attrs = xmpp_codec:enc_xmlns_attrs(__NewTopXMLNS,
                                        __TopXMLNS),
    {xmlel, <<"data">>, _attrs, _els}.

decode_avatar_data_cdata(__TopXMLNS, <<>>) ->
    erlang:error({xmpp_codec,
                  {missing_cdata, <<>>, <<"data">>, __TopXMLNS}});
decode_avatar_data_cdata(__TopXMLNS, _val) ->
    case catch base64:decode(_val) of
        {'EXIT', _} ->
            erlang:error({xmpp_codec,
                          {bad_cdata_value, <<>>, <<"data">>, __TopXMLNS}});
        _res -> _res
    end.

encode_avatar_data_cdata(_val, _acc) ->
    [{xmlcdata, base64:encode(_val)} | _acc].