File: cffwrite_fdselect.c

package info (click to toggle)
afdko 4.0.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,360 kB
  • sloc: ansic: 148,322; python: 24,622; cpp: 16,785; yacc: 421; makefile: 76; cs: 47; sh: 13
file content (254 lines) | stat: -rw-r--r-- 7,022 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
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
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
   This software is licensed as OpenSource, under the Apache License, Version 2.0.
   This license is available at: http://opensource.org/licenses/Apache-2.0. */
/***********************************************************************/

#include "cffwrite_fdselect.h"

#include <string.h>

/* Declarations used for determining sizes and for format documentation */

// typedef struct {
//     uint8_t format;
//     uint8_t *fds; /* [nGlyphs] */
// } Format0;
#define FORMAT0_SIZE(nGlyphs) (1 + (nGlyphs)*1)

// typedef struct {
//     uint16_t first;
//     uint8_t fd;
// } Range3;
#define RANGE3_SIZE (2 + 1)

// typedef struct {
//     uint8_t format;
//     uint16_t nRanges;
//     Range3 *range;
//     uint16_t sentinel;
// } Format3;
#define FORMAT3_SIZE(nRanges) (1 + 2 + (nRanges)*RANGE3_SIZE + 2)

// typedef struct {
//     uint32_t first;
//     uint16_t fd;
// } Range4;
#define RANGE4_SIZE (4 + 2)

// typedef struct {
//     uint8_t format;
//     uint32_t nRanges;
//     Range4 *range;
//     uint32_t sentinel;
// } Format4;
#define FORMAT4_SIZE(nRanges) (1 + 4 + (nRanges)*RANGE4_SIZE + 4)

typedef struct {
    dnaDCL(uint16_t, fds);
    unsigned short nRanges;
    char format;
    Offset offset;
} Selector;

/* ----------------------------- Module context ---------------------------- */

struct fdselectCtx_ {
    dnaDCL(Selector, selectors);
    Selector *_new;
    cfwCtx g; /* Package context */
};

/* Initialize selector. */
static void initSelector(void *ctx, long cnt, Selector *selector) {
    cfwCtx g = (cfwCtx)ctx;
    while (cnt--) {
        dnaINIT(g->ctx.dnaSafe, selector->fds, 10000, 20000);
        selector++;
    }
}

/* Initialize module */
void cfwFdselectNew(cfwCtx g) {
    fdselectCtx h = (fdselectCtx)cfwMemNew(g, sizeof(struct fdselectCtx_));

    /* Link contexts */
    h->g = g;
    g->ctx.fdselect = h;

    dnaINIT(g->ctx.dnaSafe, h->selectors, 1, 5);
    h->selectors.func = initSelector;
}

/* Prepare module for reuse. */
void cfwFdselectReuse(cfwCtx g) {
    fdselectCtx h = g->ctx.fdselect;
    h->selectors.cnt = 0;
}

/* Free resources */
void cfwFdselectFree(cfwCtx g) {
    fdselectCtx h = g->ctx.fdselect;
    long i;

    if (h == NULL) {
        return;
    }

    for (i = 0; i < h->selectors.size; i++) {
        dnaFREE(h->selectors.array[i].fds);
    }
    dnaFREE(h->selectors);

    cfwMemFree(g, h);
    g->ctx.fdselect = NULL;
}

/* Begin new fdselect. */
void cfwFdselectBeg(cfwCtx g) {
    fdselectCtx h = g->ctx.fdselect;
    h->_new = dnaNEXT(h->selectors);
    h->_new->fds.cnt = 0;
}

/* Add fd index to selector. */
void cfwFdselectAddIndex(cfwCtx g, uint16_t fd) {
    fdselectCtx h = g->ctx.fdselect;
    *dnaNEXT(h->_new->fds) = fd;
}

/* End fd index addition and check for duplicate selector. */
int cfwFdselectEnd(cfwCtx g) {
    fdselectCtx h = g->ctx.fdselect;
    int i;

    for (i = 0; i < h->selectors.cnt - 1; i++) {
        Selector *selector = &h->selectors.array[i];
        if (h->_new->fds.cnt <= selector->fds.cnt &&
            memcmp(h->_new->fds.array, selector->fds.array,
                   h->_new->fds.cnt) == 0) {
            /* Found match; remove new selector from list */
            h->selectors.cnt--;
            return i;
        }
    }

    /* No match found; leave selector in list */
    return h->selectors.cnt - 1;
}

/* Fill selectors. */
long cfwFdselectFill(cfwCtx g) {
    fdselectCtx h = g->ctx.fdselect;
    int i;
    Offset offset;

    offset = 0;
    for (i = 0; i < h->selectors.cnt; i++) {
        unsigned j;
        Selector *selector = &h->selectors.array[i];

        /* Count ranges */
        selector->nRanges = 1;
        for (j = 1; j < (unsigned)selector->fds.cnt; j++) {
            if (selector->fds.array[j - 1] != selector->fds.array[j]) {
                selector->nRanges++;
            }
        }

        /* Save format and offset */
        selector->offset = offset;
        if (selector->nRanges > 256) {
            selector->format = 4;
            offset += FORMAT4_SIZE(selector->nRanges);
        } else {
            long size0;
            long size3;
            size0 = FORMAT0_SIZE(selector->fds.cnt);
            size3 = FORMAT3_SIZE(selector->nRanges);
            if (size0 <= size3) {
                selector->format = 0;
                offset += size0;
            } else {
                selector->format = 3;
                offset += size3;
            }
        }
    }
    return offset;
}

/* Write selectors. */
void cfwFdselectWrite(cfwCtx g) {
    fdselectCtx h = g->ctx.fdselect;
    int i;

    for (i = 0; i < h->selectors.cnt; i++) {
        Selector *selector = &h->selectors.array[i];

        cfwWrite1(g, selector->format);
        switch (selector->format) {
            case 0: {
                /* Write format 0 */
                uint32_t j;
                for (j = 0; j < selector->fds.cnt; j++) {
                    cfwWrite1(g, selector->fds.array[j]);
                }
            } break;

            case 3: {
                /* Write format 3 */
                uint8_t lastfd;
                long j;

                cfwWrite2(g, selector->nRanges);

                /* Begin first range */
                cfwWrite2(g, 0);
                lastfd = selector->fds.array[0];
                for (j = 1; j < selector->fds.cnt; j++) {
                    uint8_t fd = selector->fds.array[j];
                    if (fd != lastfd) {
                        /* Complete last range and start new one */
                        cfwWrite1(g, lastfd);
                        cfwWrite2(g, j);
                        lastfd = fd;
                    }
                }
                /* Complete last range and write sentinel */
                cfwWrite1(g, lastfd);
                cfwWrite2(g, j);
            } break;

            case 4: {
                /* Write format 4 */
                uint16_t lastfd;
                uint32_t j;

                cfwWriteN(g, 4, selector->nRanges);

                /* Begin first range */
                cfwWriteN(g, 4, 0);
                lastfd = selector->fds.array[0];
                for (j = 1; j < selector->fds.cnt; j++) {
                    uint16_t fd = selector->fds.array[j];
                    if (fd != lastfd) {
                        /* Complete last range and start new one */
                        cfwWrite2(g, lastfd);
                        cfwWriteN(g, 4, j);
                        lastfd = fd;
                    }
                }
                /* Complete last range and write sentinel */
                cfwWrite2(g, lastfd);
                cfwWriteN(g, 4, j);
            } break;
        }
    }
}

/* Get absolute selector offset */
Offset cfwFdselectGetOffset(cfwCtx g, int iSelector, Offset base) {
    fdselectCtx h = g->ctx.fdselect;
    return base +
           ((h->selectors.cnt != 0) ? h->selectors.array[iSelector].offset : 0);
}