Debian Codegraph @

pdf/pdf_stack.h

raw
1 /* Copyright (C) 2018-2025 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /* Stack operations for the PDF interpreter */
17
18 #ifndef PDF_STACK_OPERATIONS
19 #define PDF_STACK_OPERATIONS
20
21 #include "pdf_int.h"
22 #include "pdf_types.h"
23 #include "ghostpdf.h"
24 #include "pdf_obj.h"
25
26 int pdfi_pop(pdf_context *ctx, int num);
27 int pdfi_push(pdf_context *ctx, pdf_obj *o);
28 int pdfi_mark_stack(pdf_context *ctx, pdf_obj_type type);
29 void pdfi_clearstack(pdf_context *ctx);
30 int pdfi_count_to_mark(pdf_context *ctx, uint64_t *count);
31 int pdfi_clear_to_mark(pdf_context *ctx);
32 int pdfi_destack_real(pdf_context *ctx, double *d);
33 int pdfi_destack_reals(pdf_context *ctx, double *d, int n);
34 int pdfi_destack_floats(pdf_context *ctx, float *d, int n);
35 int pdfi_destack_int(pdf_context *ctx, int64_t *i);
36 int pdfi_destack_ints(pdf_context *ctx, int64_t *i, int n);
37
38 static inline void pdfi_countup_impl(pdf_obj *o)
39 {
40 if ((uintptr_t)o < TOKEN__LAST_KEY)
41 {
42 #if REFCNT_DEBUG
43 if (o == NULL)
44 dprintf("Incrementing reference count of NULL pointer\n");
45 #endif
46 return;
47 }
48 o->refcnt++;
49 #if REFCNT_DEBUG
50 outprintf(ctx->memory, "Incrementing reference count of object %d, UID %lu, to %d\n", o->object_num, o->UID, o->refcnt);
51 #endif
52 }
53
54 static inline void pdfi_countdown_impl(pdf_obj *o)
55 {
56 #if defined(DEBUG) || REFCNT_DEBUG
57 pdf_context *ctx;
58 #endif
59
60 /* A 'low' pointer value indicates a type that is not an
61 * actual object (typically keyword). This includes the
62 * NULL case. Nothing to free in that case. */
63 if ((uintptr_t)o < TOKEN__LAST_KEY)
64 return;
65
66 #if defined(DEBUG) || REFCNT_DEBUG
67 ctx = (pdf_context *)o->ctx;
68 #endif
69 #ifdef DEBUG
70 if (o->refcnt == 0)
71 emprintf(OBJ_MEMORY(o), "Decrementing object with refcount at 0!\n");
72 #endif
73 o->refcnt--;
74 #if REFCNT_DEBUG
75 outprintf(ctx->memory, "Decrementing reference count of object %d, UID %lu, to %d\n", o->object_num, o->UID, o->refcnt);
76 #endif
77 if (o->refcnt != 0)
78 return;
79 #if REFCNT_DEBUG
80 if (ctx != NULL && ctx->cache_entries != 0) {
81 pdf_obj_cache_entry *entry = ctx->cache_LRU, *next;
82
83 while(entry) {
84 next = entry->next;
85 if (entry->o->object_num != 0 && entry->o->object_num == o->object_num)
86 outprintf(ctx->memory, "Freeing object %d, UID %lu, but there is still a cache entry!\n", o->object_num, o->UID);
87 entry = next;
88 }
89 }
90 outprintf(ctx->memory, "Freeing object %d, UID %lu\n", o->object_num, o->UID);
91 #endif
92 #ifdef DEBUG
93 if (ctx->xref_table != NULL && o->object_num > 0 &&
94 o->object_num < ctx->xref_table->xref_size &&
95 ctx->xref_table->xref[o->object_num].cache != NULL &&
96 ctx->xref_table->xref[o->object_num].cache->o == o) {
97 outprintf(ctx->memory, "Freeing object %d while it is still in the object cache!\n", o->object_num);
98 }
99 #endif
100 pdfi_free_object(o);
101 }
102
103 /* These two macros are present simply to add a cast to the generic object type, so that
104 * we don't get warnings in the implementation routines, the alternative would be to use
105 * a cast everywhere we use the inline functions above, or to have them take a void *
106 *
107 * Ordinarily we would capitalise the name of a macro to differentiate it from a function
108 * we make an exception in this case because hte macro descends to an inline function which
109 * can be debugged without expanding macros.
110 */
111 #define pdfi_countup(x) pdfi_countup_impl((pdf_obj *)x)
112
113 #define pdfi_countdown(x) pdfi_countdown_impl((pdf_obj *)x)
114
115 /* Why two functions ? The difference is that when interpreting 'sub' streams
116 * such as the content stream for a Form XObject, we may have entries on the
117 * stack at the start of the stream interpretation, and we don't want to
118 * pop any of those off during the course of the stream. The stack depth stored in
119 * the context is used to prevent this in pdfi_pop().
120 * This means that, during the course of a stream, the stack top - bottom may
121 * not be an accurate reflection of the number of available items on the stack.
122 *
123 * So pdfi_count_stack() returns the number of available items, and
124 * pdfi_count_stack_total() returns the entire size of the stack, and is used to
125 * record the saved stack depth when we start a stream.
126 *
127 * Although these are currently simple calculations, they are abstracted in order
128 * to facilitate later replacement if required.
129 */
130 static inline int pdfi_count_total_stack(pdf_context *ctx)
131 {
132 return (ctx->stack_top - ctx->stack_bot);
133 }
134
135 static inline int pdfi_count_stack(pdf_context *ctx)
136 {
137 return (pdfi_count_total_stack(ctx)) - ctx->current_stream_save.stack_count;
138 }
139
140 #endif