summaryrefslogtreecommitdiff
path: root/src/bytecode/dump.c
blob: 0cf54c8b7ecd5e4034a0df359147083bd0b064eb (plain)
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
/*
 * This file is part of LibCSS.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include "bytecode/bytecode.h"
#include "bytecode/opcodes.h"

/**
 * Opcode names, indexed by opcode
 */
static const char *opcode_names[] = {
	"azimuth",
	"background-attachment",
	"background-color",
	"background-image",
	"background-position",
	"background-repeat",
	"border-collapse",
	"border-spacing",
	"border-trbl-color",
	"border-trbl-style",
	"border-trbl-width",
	"bottom",
	"caption-side",
	"clear",
	"clip",
	"color",
	"content",
	"counter-increment",
	"counter-reset",
	"cue-after",
	"cue-before",
	"cursor",
	"direction",
	"display",
	"elevation",
	"empty-cells",
	"float",
	"font-family",
	"font-size",
	"font-style",
	"font-variant",
	"font-weight",
	"height",
	"left",
	"letter-spacing",
	"line-height",
	"list-style-image",
	"list-style-position",
	"list-style-type",
	"margin-trbl",
	"max-height",
	"max-width",
	"min-height",
	"min-width",
	"orphans",
	"outline-color",
	"outline-style",
	"outline-width",
	"overflow",
	"padding-trbl",
	"page-break-after",
	"page-break-before",
	"page-break-inside",
	"pause-after",
	"pause-before",
	"pitch-range",
	"pitch",
	"play-during",
	"position",
	"quotes",
	"richness",
	"right",
	"speak-header",
	"speak-numeral",
	"speak-punctuation",
	"speak",
	"speech-rate",
	"stress",
	"table-layout",
	"text-align",
	"text-decoration",
	"text-indent",
	"text-transform",
	"top",
	"unicode-bidi",
	"vertical-align",
	"visibility",
	"voice-family",
	"volume",
	"white-space",
	"widows",
	"width",
	"word-spacing",
	"z-index",
};

/**
 * Dump a CSS bytecode stream to the given file handle
 * 
 * \param bytecode  The stream to dump
 * \param length    Length, in bytes, of bytecode
 * \param fp        File handle to output to
 */
void css_bytecode_dump(void *bytecode, uint32_t length, FILE *fp)
{
	uint32_t offset = 0;

#define ADVANCE(n) do {					\
	offset += (n);					\
	bytecode = ((uint8_t *) bytecode) + (n);	\
} while(0)

	while (offset < length) {
		opcode op;
		uint8_t flags;
		uint16_t value;
		uint32_t opv = *((uint32_t *) bytecode);

		ADVANCE(sizeof(opv));

		op = getOpcode(opv);

		fprintf(fp, "%s: ", opcode_names[op]);

		flags = getFlags(opv);

		if (flags & FLAG_INHERIT) {
			fprintf(fp, "inherit");
		} else { 
			value = getValue(opv);

			switch (op) {
			case OP_BACKGROUND_ATTACHMENT:
				switch (value) {
				case BACKGROUND_ATTACHMENT_FIXED:
					fprintf(fp, "fixed");
					break;
				case BACKGROUND_ATTACHMENT_SCROLL:
					fprintf(fp, "scroll");
					break;
				}
				break;
			case OP_BACKGROUND_COLOR:
				switch (value) {
				case BACKGROUND_COLOR_TRANSPARENT:
					fprintf(fp, "transparent");
					break;
				case BACKGROUND_COLOR_SET:
				{
					uint32_t colour = 
						*((uint32_t *) bytecode);
					ADVANCE(sizeof(colour));
					fprintf(fp, "#%08x", colour);
				}	
					break;
				}
				break;
			case OP_BACKGROUND_IMAGE:
				switch (value) {
				case BACKGROUND_IMAGE_NONE:
					fprintf(fp, "none");
					break;
				case BACKGROUND_IMAGE_URI:
				{
					parserutils_hash_entry *ptr = 
						*((parserutils_hash_entry **) 
						bytecode);
					ADVANCE(sizeof(ptr));
					fprintf(fp, "url('%.*s')", 
							(int) ptr->len, 
							(char *) ptr->data);
				}
					break;
				}
				break;
			case OP_BACKGROUND_REPEAT:
				switch (value) {
				case BACKGROUND_REPEAT_NO_REPEAT:
					fprintf(fp, "no-repeat");
					break;
				case BACKGROUND_REPEAT_REPEAT_X:
					fprintf(fp, "repeat-x");
					break;
				case BACKGROUND_REPEAT_REPEAT_Y:
					fprintf(fp, "repeat-y");
					break;
				case BACKGROUND_REPEAT_REPEAT:
					fprintf(fp, "repeat");
					break;
				}
				break;
			case OP_CLEAR:
				switch (value) {
				case CLEAR_NONE:
					fprintf(fp, "none");
					break;
				case CLEAR_LEFT:
					fprintf(fp, "left");
					break;
				case CLEAR_RIGHT:
					fprintf(fp, "right");
					break;
				case CLEAR_BOTH:
					fprintf(fp, "both");
					break;
				}
				break;
			default:
				fprintf(fp, "Unknown opcode %x", op);
				break;
			}
		}

		if (flags & FLAG_IMPORTANT)
			fprintf(fp, " !important");

		fprintf(fp, "; ");
	}

#undef ADVANCE

}