~ubuntu-branches/ubuntu/precise/graphviz/precise-security

« back to all changes in this revision

Viewing changes to lib/agraph/imap.c

  • Committer: Bazaar Package Importer
  • Author(s): David Claughton
  • Date: 2010-03-24 22:45:18 UTC
  • mfrom: (1.2.7 upstream) (6.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100324224518-do441tthbqjaqjzd
Tags: 2.26.3-4
Add patch to fix segfault in circo. Backported from upstream snapshot
release.  Thanks to Francis Russell for his work on this.
(Closes: #575255)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: imap.c,v 1.2 2006/07/08 17:33:49 ellson Exp $ $Revision: 1.2 $ */
2
 
/* vim:set shiftwidth=4 ts=8: */
3
 
 
4
 
/**********************************************************
5
 
*      This software is part of the graphviz package      *
6
 
*                http://www.graphviz.org/                 *
7
 
*                                                         *
8
 
*            Copyright (c) 1994-2004 AT&T Corp.           *
9
 
*                and is licensed under the                *
10
 
*            Common Public License, Version 1.0           *
11
 
*                      by AT&T Corp.                      *
12
 
*                                                         *
13
 
*        Information and Software Systems Research        *
14
 
*              AT&T Research, Florham Park NJ             *
15
 
**********************************************************/
16
 
 
17
 
 
18
 
#include "aghdr.h"
19
 
 
20
 
typedef struct IMapEntry_s {
21
 
    Dtlink_t namedict_link;
22
 
    Dtlink_t iddict_link;
23
 
    unsigned long id;
24
 
    char *str;
25
 
} IMapEntry_t;
26
 
 
27
 
static int idcmpf(Dict_t * d, void *arg_p0, void *arg_p1, Dtdisc_t * disc)
28
 
{
29
 
    IMapEntry_t *p0, *p1;
30
 
 
31
 
    NOTUSED(d);
32
 
    p0 = arg_p0;
33
 
    p1 = arg_p1;
34
 
    NOTUSED(disc);
35
 
    return (p0->id - p1->id);
36
 
}
37
 
 
38
 
/* note, OK to compare pointers into shared string pool 
39
 
 * but can't probe with an arbitrary string pointer
40
 
 */
41
 
static int namecmpf(Dict_t * d, void *arg_p0, void *arg_p1,
42
 
                    Dtdisc_t * disc)
43
 
{
44
 
    IMapEntry_t *p0, *p1;
45
 
 
46
 
    NOTUSED(d);
47
 
    p0 = arg_p0;
48
 
    p1 = arg_p1;
49
 
    NOTUSED(disc);
50
 
    return (p0->str - p1->str);
51
 
}
52
 
 
53
 
static Dtdisc_t LookupByName = {
54
 
    0,                          /* object ptr is passed as key */
55
 
    0,                          /* size (ignored) */
56
 
    offsetof(IMapEntry_t, namedict_link),
57
 
    NIL(Dtmake_f),
58
 
    NIL(Dtfree_f),
59
 
    namecmpf,
60
 
    NIL(Dthash_f),
61
 
    agdictobjmem,
62
 
    NIL(Dtevent_f)
63
 
};
64
 
 
65
 
static Dtdisc_t LookupById = {
66
 
    0,                          /* object ptr is passed as key */
67
 
    0,                          /* size (ignored) */
68
 
    offsetof(IMapEntry_t, iddict_link),
69
 
    NIL(Dtmake_f),
70
 
    NIL(Dtfree_f),
71
 
    idcmpf,
72
 
    NIL(Dthash_f),
73
 
    agdictobjmem,
74
 
    NIL(Dtevent_f)
75
 
};
76
 
 
77
 
int aginternalmaplookup(Agraph_t * g, int objtype, char *str,
78
 
                        unsigned long *result)
79
 
{
80
 
    Dict_t *d;
81
 
    IMapEntry_t *sym, template;
82
 
    char *search_str;
83
 
 
84
 
    if (objtype == AGINEDGE)
85
 
        objtype = AGEDGE;
86
 
    if ((d = g->clos->lookup_by_name[objtype])) {
87
 
        if ((search_str = agstrbind(g, str))) {
88
 
            template.str = search_str;
89
 
            sym = (IMapEntry_t *) dtsearch(d, &template);
90
 
            if (sym) {
91
 
                *result = sym->id;
92
 
                return TRUE;
93
 
            }
94
 
        }
95
 
    }
96
 
    return FALSE;
97
 
}
98
 
 
99
 
/* caller GUARANTEES that this is a new entry */
100
 
void aginternalmapinsert(Agraph_t * g, int objtype, char *str,
101
 
                         unsigned long id)
102
 
{
103
 
    IMapEntry_t *ent;
104
 
    Dict_t *d_name_to_id, *d_id_to_name;
105
 
 
106
 
    ent = AGNEW(g, IMapEntry_t);
107
 
    ent->id = id;
108
 
    ent->str = agstrdup(g, str);
109
 
 
110
 
    if (objtype == AGINEDGE)
111
 
        objtype = AGEDGE;
112
 
    if ((d_name_to_id = g->clos->lookup_by_name[objtype]) == NIL(Dict_t *))
113
 
        d_name_to_id = g->clos->lookup_by_name[objtype] =
114
 
            agdtopen(g, &LookupByName, Dttree);
115
 
    if ((d_id_to_name = g->clos->lookup_by_id[objtype]) == NIL(Dict_t *))
116
 
        d_id_to_name = g->clos->lookup_by_id[objtype] =
117
 
            agdtopen(g, &LookupById, Dttree);
118
 
    dtinsert(d_name_to_id, ent);
119
 
    dtinsert(d_id_to_name, ent);
120
 
}
121
 
 
122
 
static IMapEntry_t *find_isym(Agraph_t * g, int objtype, unsigned long id)
123
 
{
124
 
    Dict_t *d;
125
 
    IMapEntry_t *isym, itemplate;
126
 
 
127
 
    if (objtype == AGINEDGE)
128
 
        objtype = AGEDGE;
129
 
    if ((d = g->clos->lookup_by_id[objtype])) {
130
 
        itemplate.id = id;
131
 
        isym = (IMapEntry_t *) dtsearch(d, &itemplate);
132
 
    } else
133
 
        isym = NIL(IMapEntry_t *);
134
 
    return isym;
135
 
}
136
 
 
137
 
char *aginternalmapprint(Agraph_t * g, int objtype, unsigned long id)
138
 
{
139
 
    IMapEntry_t *isym;
140
 
 
141
 
    if ((isym = find_isym(g, objtype, id)))
142
 
        return isym->str;
143
 
    return NILstr;
144
 
}
145
 
 
146
 
 
147
 
int aginternalmapdelete(Agraph_t * g, int objtype, unsigned long id)
148
 
{
149
 
    IMapEntry_t *isym;
150
 
 
151
 
    if (objtype == AGINEDGE)
152
 
        objtype = AGEDGE;
153
 
    if ((isym = find_isym(g, objtype, id))) {
154
 
        dtdelete(g->clos->lookup_by_name[objtype], isym);
155
 
        dtdelete(g->clos->lookup_by_id[objtype], isym);
156
 
        agstrfree(g, isym->str);
157
 
        agfree(g, isym);
158
 
        return TRUE;
159
 
    }
160
 
    return FALSE;
161
 
}
162
 
 
163
 
void aginternalmapclearlocalnames(Agraph_t * g)
164
 
{
165
 
    int i;
166
 
    IMapEntry_t *sym, *nxt;
167
 
    Dict_t **d_name;
168
 
    /* Dict_t **d_id; */
169
 
 
170
 
    Ag_G_global = g;
171
 
    d_name = g->clos->lookup_by_name;
172
 
    /* d_id = g->clos->lookup_by_id; */
173
 
    for (i = 0; i < 3; i++) {
174
 
        if (d_name[i]) {
175
 
            for (sym = dtfirst(d_name[i]); sym; sym = nxt) {
176
 
                nxt = dtnext(d_name[i], sym);
177
 
                if (sym->str[0] == LOCALNAMEPREFIX)
178
 
                    aginternalmapdelete(g, i, sym->id);
179
 
            }
180
 
        }
181
 
    }
182
 
}
183
 
 
184
 
static void closeit(Dict_t ** d)
185
 
{
186
 
    int i;
187
 
 
188
 
    for (i = 0; i < 3; i++) {
189
 
        if (d[i]) {
190
 
            dtclose(d[i]);
191
 
            d[i] = NIL(Dict_t *);
192
 
        }
193
 
    }
194
 
}
195
 
 
196
 
void aginternalmapclose(Agraph_t * g)
197
 
{
198
 
    Ag_G_global = g;
199
 
    closeit(g->clos->lookup_by_name);
200
 
    closeit(g->clos->lookup_by_id);
201
 
}