File: dumpber.c

package info (click to toggle)
yaz 5.27.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,184 kB
  • sloc: xml: 123,414; ansic: 72,530; sh: 5,007; tcl: 2,169; makefile: 1,321; yacc: 382
file content (141 lines) | stat: -rw-r--r-- 3,586 bytes parent folder | download | duplicates (3)
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
/* This file is part of the YAZ toolkit.
 * Copyright (C) Index Data
 * See the file LICENSE for details.
 */

/**
 * \file dumpber.c
 * \brief Implements BER dumping
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include "odr-priv.h"

static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset)
{
    int res, ll, zclass, tag, cons, lenlen, taglen;
    const char *b = buf;
    char level_str[80];

    if (level >= 15)
        sprintf(level_str, "level=%-6d%*s", level, 18, "");
    else
        sprintf(level_str, "%*s", level * 2, "");

    if (!len)
        return 0;
    if (!buf[0] && !buf[1])
        return 0;
    if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
        return 0;
    if (res > len)
    {
        fprintf(f, "%5d: %s : Unexpected end of buffer\n", offset, level_str);
        return 0;
    }
    fprintf(f, "%5d: %s", offset, level_str);
    if (zclass == ODR_UNIVERSAL)
    {
        static char *nl[] =
        {
            "[Univ 0]", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
            "NULL", "OID", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
            "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
            "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
            "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
            "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
        };

        if (tag >= 0 && tag < 28)
            fprintf(f, "%s", nl[tag]);
        else
            fprintf(f, "[UNIV %d]", tag);
    }
    else if (zclass == ODR_CONTEXT)
        fprintf(f, "[%d]", tag);
    else
        fprintf(f, "[%d:%d]", zclass, tag);
    b += res;
    taglen = res;
    len -= res;
    if ((res = ber_declen(b, &ll, len)) <= 0)
    {
        fprintf(f, "\n%sBad length\n", level_str);
        return 0;
    }
    lenlen = res;
    b += res;
    len -= res;
    if (ll >= 0)
        fprintf(f, " len=%d", ll);
    else
        fprintf(f, " len=?");
    fprintf(f, " tl=%d, ll=%d cons=%d\n", taglen, lenlen, cons);
    if (!cons)
    {
        if (ll < 0 || ll > len)
        {
            fprintf(f, "%sBad length on primitive type. ll=%d len=%d\n",
                    level_str, ll, len);
            return 0;
        }
        return ll + (b - buf);
    }
    if (ll >= 0)
    {
        if (ll > len)
        {
            fprintf(f, "%sBad length of constructed type ll=%d len=%d\n",
                    level_str, ll, len);
            return 0;
        }
        len = ll;
    }
    /* constructed - cycle through children */
    while ((ll == -1 && len >= 2) || (ll >= 0 && len))
    {
        if (ll == -1 && *b == 0 && *(b + 1) == 0)
            break;
        if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
        {
            fprintf(f, "%s Dump of content element failed\n", level_str);
            return 0;
        }
        b += res;
        len -= res;
        if (len < 0)
        {
            fprintf(f, "%sBad length\n", level_str);
            return 0;
        }
    }
    if (ll == -1)
    {
        if (len < 2)
        {
            fprintf(f, "%sBuffer too short in indefinite length\n",
                    level_str);
            return 0;
        }
        return (b - buf) + 2;
    }
    return b - buf;
}

int odr_dumpBER(FILE *f, const char *buf, int len)
{
    return do_dumpBER(f, buf, len, 0, 0);
}
/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */