1 /* dvipos-20070107
2
3 Copyright (C) 2003 by Jin-Hwan <chofchof@ktug.or.kr>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 */
19
20 #include "utils.h"
21
22 #include <stdarg.h>
23
get_unsigned_byte(FILE * fp)24 UNSIGNED_BYTE get_unsigned_byte (FILE *fp)
25 {
26 return (UNSIGNED_BYTE)(fgetc(fp) & 0xFF);
27 }
28
get_signed_byte(FILE * fp)29 SIGNED_BYTE get_signed_byte (FILE *fp)
30 {
31 UNSIGNED_BYTE byte = get_unsigned_byte(fp);
32 return (SIGNED_BYTE)(byte & 0x80 ? byte - 0x100 : byte);
33 }
34
get_unsigned_pair(FILE * fp)35 UNSIGNED_PAIR get_unsigned_pair (FILE *fp)
36 {
37 UNSIGNED_PAIR pair = get_unsigned_byte(fp);
38 /* Read the second byte */
39 pair = pair << 8; pair += get_unsigned_byte(fp);
40 return pair;
41 }
42
get_signed_pair(FILE * fp)43 SIGNED_PAIR get_signed_pair (FILE *fp)
44 {
45 UNSIGNED_PAIR pair;
46 UNSIGNED_BYTE byte = get_unsigned_byte(fp);
47 pair = (byte & 0x80 ? byte - 0x100 : byte);
48 /* Read the second byte */
49 pair = pair << 8; pair += get_unsigned_byte(fp);
50 return (SIGNED_PAIR)pair;
51 }
52
get_unsigned_triple(FILE * fp)53 UNSIGNED_TRIPLE get_unsigned_triple (FILE *fp)
54 {
55 UNSIGNED_TRIPLE triple = get_unsigned_byte(fp);
56 /* Read the second, the third byte */
57 triple = triple << 8; triple += get_unsigned_byte(fp);
58 triple = triple << 8; triple += get_unsigned_byte(fp);
59 return triple;
60 }
61
get_signed_triple(FILE * fp)62 SIGNED_TRIPLE get_signed_triple (FILE *fp)
63 {
64 UNSIGNED_TRIPLE triple;
65 /* Read the first byte and check the sign */
66 UNSIGNED_BYTE byte = get_unsigned_byte(fp);
67 triple = (byte & 0x80 ? byte - 0x100 : byte);
68 /* Read the second, the third byte */
69 triple = triple << 8; triple += get_unsigned_byte(fp);
70 triple = triple << 8; triple += get_unsigned_byte(fp);
71 return (SIGNED_TRIPLE)triple;
72 }
73
get_unsigned_quad(FILE * fp)74 UNSIGNED_QUAD get_unsigned_quad (FILE *fp)
75 {
76 UNSIGNED_QUAD quad = get_unsigned_byte(fp);
77 /* Read the second, the third, and the fourth byte */
78 quad = quad << 8; quad += get_unsigned_byte(fp);
79 quad = quad << 8; quad += get_unsigned_byte(fp);
80 quad = quad << 8; quad += get_unsigned_byte(fp);
81 return quad;
82 }
83
get_signed_quad(FILE * fp)84 SIGNED_QUAD get_signed_quad (FILE *fp)
85 {
86 UNSIGNED_QUAD quad;
87 /* Read the first byte and check the sign */
88 UNSIGNED_BYTE byte = get_unsigned_byte(fp);
89 quad = (byte & 0x80 ? byte - 0x100 : byte);
90 /* Read the second, the third, and the fourth byte */
91 quad = quad << 8; quad += get_unsigned_byte(fp);
92 quad = quad << 8; quad += get_unsigned_byte(fp);
93 quad = quad << 8; quad += get_unsigned_byte(fp);
94 return (SIGNED_QUAD)quad;
95 }
96
put_unsigned_byte(SIGNED_QUAD quad,FILE * fp)97 void put_unsigned_byte (SIGNED_QUAD quad, FILE *fp)
98 {
99 fputc(quad & 0xff, fp);
100 dbg_location++;
101 }
102
put_signed_byte(SIGNED_QUAD quad,FILE * fp)103 void put_signed_byte (SIGNED_QUAD quad, FILE *fp)
104 {
105 if (quad < 0) fputc((quad + 0x100) & 0xff, fp);
106 else fputc(quad & 0xff, fp);
107 dbg_location++;
108 }
109
put_unsigned_pair(SIGNED_QUAD quad,FILE * fp)110 void put_unsigned_pair (SIGNED_QUAD quad, FILE *fp)
111 {
112 put_unsigned_byte(quad >> 8, fp);
113 put_unsigned_byte(quad, fp);
114 }
115
put_signed_pair(SIGNED_QUAD quad,FILE * fp)116 void put_signed_pair (SIGNED_QUAD quad, FILE *fp)
117 {
118 put_signed_byte(quad >> 8, fp);
119 put_unsigned_byte(quad, fp);
120 }
121
put_unsigned_triple(SIGNED_QUAD quad,FILE * fp)122 void put_unsigned_triple (SIGNED_QUAD quad, FILE *fp)
123 {
124 put_unsigned_byte(quad >> 16, fp);
125 put_unsigned_pair(quad & 0xffff, fp);
126 }
127
put_signed_triple(SIGNED_QUAD quad,FILE * fp)128 void put_signed_triple (SIGNED_QUAD quad, FILE *fp)
129 {
130 put_signed_byte(quad >> 16, fp);
131 put_unsigned_pair(quad & 0xffff, fp);
132 }
133
put_signed_quad(SIGNED_QUAD quad,FILE * fp)134 void put_signed_quad (SIGNED_QUAD quad, FILE *fp)
135 {
136 put_signed_byte(quad >> 24, fp);
137 put_unsigned_triple(quad & 0xffffff, fp);
138 }
139
sput_signed_pair(char * buf,SIGNED_QUAD quad)140 void sput_signed_pair (char *buf, SIGNED_QUAD quad)
141 {
142 if (quad < 0) *buf++ = (quad >> 8) + 0x100;
143 else *buf++ = quad >> 8;
144 *buf = quad & 0xff;
145 }
146
sput_signed_quad(char * buf,SIGNED_QUAD quad)147 void sput_signed_quad (char *buf, SIGNED_QUAD quad)
148 {
149 if (quad < 0) *buf++ = (quad >> 24) + 0x100;
150 else *buf++ = quad >> 24;
151 *buf++ = quad >> 16;
152 *buf++ = quad >> 8;
153 *buf = quad & 0xff;
154 }
155
156 #ifndef KPATHSEA
157 /* Borrowed from the kpathsea library, concat3.c */
concat(const char * s1,const char * s2)158 char *concat (const char *s1, const char *s2)
159 {
160 unsigned s1len = strlen(s1);
161 unsigned s2len = strlen(s2);
162 char *answer = (char *)xmalloc(s1len + s2len + 1);
163 strcpy(answer, s1);
164 strcat(answer + s1len, s2);
165 return answer;
166 }
167
168 /* Borrowed from the kpathsea library, concat3.c */
concat3(const char * s1,const char * s2,const char * s3)169 char *concat3 (const char *s1, const char *s2, const char *s3)
170 {
171 char *answer = (char *)xmalloc(strlen(s1) + strlen(s2) + strlen(s3) + 1);
172 strcpy(answer, s1);
173 strcat(answer, s2);
174 strcat(answer, s3);
175 return answer;
176 }
177
178 /* Borrowed from the kpathsea library, make-suffix.c */
179 /* Return a new string: S suffixed with SUFFIX, regardless of what it
180 * was before. This returns a newly allocated string. */
make_suffix(const char * s,const char * suffix)181 char *make_suffix (const char *s, const char *suffix)
182 {
183 char *new_s;
184 const char *dot_pos = strrchr(s, '.');
185 const char *slash_pos;
186
187 for (slash_pos = s + strlen(s) - 1; slash_pos > dot_pos && slash_pos > s; slash_pos--)
188 if (IS_DIR_SEP(*slash_pos)) break;
189
190 if (dot_pos == NULL || slash_pos > dot_pos)
191 new_s = concat3(s, ".", suffix);
192 else {
193 unsigned past_dot_index = dot_pos + 1 - s;
194 new_s = (char *)xmalloc (past_dot_index + strlen (suffix) + 1);
195 strncpy(new_s, s, dot_pos + 1 - s);
196 strcpy(new_s + past_dot_index, suffix);
197 }
198
199 return new_s;
200 }
201 #endif
202
msg_out(int level,const char * fmt,...)203 void msg_out (int level, const char *fmt, ...)
204 {
205 va_list args;
206 if (verbose & level) {
207 va_start(args, fmt);
208 vfprintf(stdout, fmt, args);
209 va_end(args);
210 }
211 if (level == M_FAIL)
212 exit(1);
213 }
214