about summary refs log tree commit diff
path: root/urt/rle_hdr.c
blob: bdb728d2410ee7a32465c4d4991a976c7e032e99 (plain) (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
/*
 * This software is copyrighted as noted below.  It may be freely copied,
 * modified, and redistributed, provided that the copyright notice is
 * preserved on all copies.
 *
 * There is no warranty or other guarantee of fitness for this software,
 * it is provided solely "as is".  Bug reports or fixes may be sent
 * to the author, who may or may not act on them as he desires.
 *
 * You may not include this software in a program or other software product
 * without supplying the source, or without informing the end-user that the
 * source is available for no extra charge.
 *
 * If you modify this software, you should include a notice giving the
 * name of the person performing the modification, the date of modification,
 * and the reason for such modification.
 */
/*
 * rle_hdr.c - Functions to manipulate rle_hdr structures.
 *
 * Author:  Spencer W. Thomas
 *      EECS Dept.
 *      University of Michigan
 * Date:    Mon May 20 1991
 * Copyright (c) 1991, University of Michigan
 */

#include <string.h>

#include "nstring.h"
#include "mallocvar.h"

#include "rle_config.h"
#include "rle.h"



void
rle_names(rle_hdr *    const hdrP,
          const char * const pgmname,
          const char * const fname,
          int          const imgNum) {
/*---------------------------------------------------------------------------- 
 * Load program and file names into header.
 * Inputs:
 *  hdrP:      Header to modify.
 *  pgmname:   The program name.
 *  fname:     The file name.
 *  imgNum:    Number of the image within the file.
 * Outputs:
 *  *hdrP:     Modified header.
-----------------------------------------------------------------------------*/

    /* Algorithm:
       If values previously filled in (by testing is_init field),
       free them.  Make copies of file name and program name,
       modifying file name for standard i/o.  Set is_init field.
    */
    const char * newFname;
    const char * newPgmname;

    /* Mark as filled in. */
    hdrP->is_init = RLE_INIT_MAGIC;

    /* Default file name for stdin/stdout. */
    if (!fname || streq(fname, "-") || strlen(fname) == 0)
        newFname = "Standard I/O";
    else
        newFname = fname;

    if (pgmname)
        newPgmname = pgmname;
    else
        newPgmname = rle_dflt_hdr.cmd;

    /* Fill in with copies of the strings. */
    if (hdrP->cmd != newPgmname)
        hdrP->cmd = pm_strdup(newPgmname);

    if (hdrP->file_name != newFname)
        hdrP->file_name = pm_strdup(newFname);

    hdrP->img_num = imgNum;
}



/* Used by rle_hdr_cp and rle_hdr_init to avoid recursion loops. */
static int noRecurse = 0;



rle_hdr *
rle_hdr_cp(rle_hdr * const fromHdrP,
           rle_hdr * const toHdrArgP) {
/*----------------------------------------------------------------------------
 * Make a "safe" copy of a rle_hdr structure.
 * Inputs:
 *  *fromHdrP:   Header to be copied.
 * Outputs:
 *  *toHdrPd:    Copy of from_hdr, with all memory referred to
 *               by pointers copied.  Also returned as function
 *               value.  If NULL, a static header is used.
 * Assumptions:
 *  It is safe to call rle_hdr_init on *toHdrP.
-----------------------------------------------------------------------------*/
    /* Algorithm:
       Initialize *toHdrP, copy *fromHdrP to it, then copy the memory
       referred to by all non-null pointers.
    */
    static rle_hdr dfltHdr;
    rle_hdr * toHdrP;
    const char * cmd;
    const char * file;
    unsigned int num;

    /* Save command, file name, and image number if already initialized. */
    if (toHdrArgP &&  toHdrArgP->is_init == RLE_INIT_MAGIC) {
        cmd  = toHdrArgP->cmd;
        file = toHdrArgP->file_name;
        num  = toHdrArgP->img_num;
    } else {
        cmd = file = NULL;
        num = 0;
    }

    if (!noRecurse) {
        ++noRecurse;
        rle_hdr_init(toHdrArgP);
        --noRecurse;
    }

    toHdrP = toHdrArgP ? toHdrArgP : &dfltHdr;

    *toHdrP = *fromHdrP;

    if (toHdrP->bg_color) {
        unsigned int i;

        MALLOCARRAY(toHdrP->bg_color, toHdrP->ncolors);
        if (!toHdrP->bg_color)
            pm_error("Failed to allocate array for %u background colors",
                     toHdrP->ncolors);
        for (i = 0; i < toHdrP->ncolors; ++i)
            toHdrP->bg_color[i] = fromHdrP->bg_color[i];
    }

    if (toHdrP->cmap) {
        size_t const size =
            toHdrP->ncmap * (1 << toHdrP->cmaplen) * sizeof(rle_map);
        toHdrP->cmap = malloc(size);
        if (!toHdrP->cmap)
            pm_error("Failed to allocate memory for %u color maps "
                     "of length %u", toHdrP->ncmap, 1 << toHdrP->cmaplen);
        memcpy(toHdrP->cmap, fromHdrP->cmap, size);
    }

    /* Only copy array of pointers, as the original comment memory
     * never gets overwritten.
     */
    if (toHdrP->comments) {
        unsigned int  size;
        const char ** cp;

        /* Count the comments. */
        for (cp = toHdrP->comments, size = 0; *cp; ++cp)
            ++size;

        /* Check if there are really any comments. */
        if (size > 0) {
            ++size;     /* Copy the NULL pointer, too. */
            size *= sizeof(char *);
            toHdrP->comments = malloc(size);
            if (!toHdrP->comments)
                pm_error("Failed to allocation %u bytes for comments", size);
            memcpy(toHdrP->comments, fromHdrP->comments, size);
        } else
            toHdrP->comments = NULL;    /* Blow off empty comment list. */
    }

    /* Restore the names to their original values. */
    toHdrP->cmd       = cmd;
    toHdrP->file_name = file;

    /* Lines above mean nothing much happens if cmd and file are != NULL. */
    rle_names(toHdrP, toHdrP->cmd, toHdrP->file_name, num);

    return toHdrP;
}



void
rle_hdr_clear(rle_hdr * const hdrP) {
/*----------------------------------------------------------------------------
 * Clear out the allocated memory pieces of a header.
 *
 * This routine is intended to be used internally by the library, to
 * clear a header before putting new data into it.  It clears all the
 * fields that would be set by reading in a new image header.
 * Therefore, it does not clear the program and file names.
 *
 * Inputs:
 *  hdrP:    To be cleared.
 * Outputs:
 *  *hdrP:   After clearing.
 * Assumptions:
 *  If is_init field is RLE_INIT_MAGIC, the header has been
 *  properly initialized.  This will fail every 2^(-32) times, on
 *  average.
-----------------------------------------------------------------------------*/
    /* Algorithm:
       Free memory and set to zero all pointers, except program and
       file name.
    */

    /* Try to free memory.  Assume if is_init is properly set that this
     * header has been previously initialized, therefore it is safe to
     * free memory.
     */
    if (hdrP && hdrP->is_init == RLE_INIT_MAGIC) {
        if (hdrP->bg_color )
            free(hdrP->bg_color);
        hdrP->bg_color = NULL;
        if (hdrP->cmap )
            free(hdrP->cmap);
        hdrP->cmap = NULL;
        /* Unfortunately, we don't know how to free the comment memory. */
        if (hdrP->comments)
            free(hdrP->comments);
        hdrP->comments = NULL;
    }
}



rle_hdr *
rle_hdr_init(rle_hdr * const hdrP) {
/*----------------------------------------------------------------------------
 * Initialize a rle_hdr structure.
 * Inputs:
 *  hdrP:    Header to be initialized.
 * Outputs:
 *  *hdrP:   Initialized header.
 * Assumptions:
 *  If hdrP->is_init is RLE_INIT_MAGIC, the header has been
 *  previously initialized.
 *  If the_hdr is a copy of another rle_hdr structure, the copy
 *  was made with rle_hdr_cp.
-----------------------------------------------------------------------------*/
    /* Algorithm:
       Fill in fields of rle_dflt_hdr that could not be set by the loader
       If the_hdr is rle_dflt_hdr, do nothing else
       Else:
         If hdrP is NULL, return a copy of rle_dflt_hdr in static storage
         If hdrP->is_init is RLE_INIT_MAGIC, free all memory
            pointed to by non-null pointers.
         If this is a recursive call to rle_hdr_init, clear *hdrP and
           return hdrP.
         Else make a copy of rle_dflt_hdr and return its address.  Make the
           copy in static storage if hdrP is NULL, and in *hdrP otherwise.
    */
    rle_hdr * retval;

    rle_dflt_hdr.rle_file = stdout;

    /* The rest of rle_dflt_hdr is set by the loader's data initialization */

    if (hdrP == &rle_dflt_hdr)
        retval = hdrP;
    else {
        rle_hdr_clear(hdrP);

        /* Call rle_hdr_cp only if not called from there. */
        if (!noRecurse) {
            ++noRecurse;
            retval = rle_hdr_cp(&rle_dflt_hdr, hdrP);
            --noRecurse;
        } else
            retval = hdrP;
    }
    return retval;
}