Add check for integer overflow in odr_write YAZ-816
[yaz-moved-to-github.git] / src / init_diag.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file init_diag.c
7  * \brief Decoding of diagnostics embedded in init response
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <yaz/proto.h>
14
15 static Z_DefaultDiagFormat *interpret_init_diag2(int *no,
16                                                  Z_DiagnosticFormat *diag)
17 {
18     int i;
19     for (i = 0; i < diag->num; i++)
20     {
21         Z_DiagnosticFormat_s *ds = diag->elements[i];
22         if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
23         {
24             if (*no == 0)
25                 return ds->u.defaultDiagRec;
26             (*no)--;
27         }
28     }
29     return 0;
30 }
31
32 Z_DefaultDiagFormat *yaz_decode_init_diag(int no, Z_InitResponse *initrs)
33 {
34     Z_External *uif = initrs->userInformationField;
35     if (uif && uif->which == Z_External_userInfo1)
36     {
37         int i;
38         Z_OtherInformation *ui = uif->u.userInfo1;
39         for (i = 0; i < ui->num_elements; i++)
40         {
41             Z_OtherInformationUnit *unit = ui->list[i];
42             if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
43                 unit->information.externallyDefinedInfo &&
44                 unit->information.externallyDefinedInfo->which ==
45                 Z_External_diag1)
46             {
47                 Z_DefaultDiagFormat *r =
48                     interpret_init_diag2
49                     (&no, unit->information.externallyDefinedInfo->u.diag1);
50                 if (r)
51                     return r;
52             }
53         }
54     }
55     return 0;
56 }
57 /*
58  * Local variables:
59  * c-basic-offset: 4
60  * c-file-style: "Stroustrup"
61  * indent-tabs-mode: nil
62  * End:
63  * vim: shiftwidth=4 tabstop=8 expandtab
64  */