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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
#ifndef lint
static char *rcsid = "$Id: imdata.c,v 1.7 1994/06/02 02:20:23 ishisone Exp $";
#endif
/*
* Copyright (c) 1994 Software Research Associates, Inc.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Software Research Associates not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission. Software Research
* Associates makes no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied
* warranty.
*
* Author: Makoto Ishisone, Software Research Associates, Inc., Japan
*/
#include "im.h"
#define B_GET16(p) (((p)[0]<<8) + (p)[1])
#define B_GET32(p) (((p)[0]<<24) + ((p)[1]<<16) + ((p)[2]<<8) + (p)[3])
#define L_GET16(p) ((p)[0] + ((p)[1]<<8))
#define L_GET32(p) ((p)[0] + ((p)[1]<<8) + ((p)[2]<<16) + ((p)[3] << 24))
#define B_PUT16(x, p) (p)[0] = ((x)>>8) & 0xff; (p)[1] = (x) & 0xff
#define B_PUT32(x, p) (p)[0] = ((x)>>24) & 0xff; (p)[1] = ((x)>>16) & 0xff; \
(p)[2] = ((x)>>8) & 0xff; (p)[3] = (x) & 0xff
#define L_PUT16(x, p) (p)[0] = (x) & 0xff; (p)[1] = ((x)>>8) & 0xff
#define L_PUT32(x, p) (p)[0] = (x) & 0xff; (p)[1] = ((x)>>8) & 0xff; \
(p)[2] = ((x)>>16) & 0xff; (p)[3] = ((x)>>24) & 0xff;
int
IMGetC8(conn, offset)
IMConnection *conn;
int offset;
{
IMBuffer *ibp = IM_INBUF(conn);
int x;
x = *((unsigned char *)IMBUFDATA(ibp) + offset);
return x;
}
unsigned int
IMGetC16(conn, offset)
IMConnection *conn;
int offset;
{
IMBuffer *ibp = IM_INBUF(conn);
unsigned char *p = (unsigned char *)IMBUFDATA(ibp) + offset;
unsigned int x;
if (conn->byte_order == ORDER_BIG) {
x = B_GET16(p);
} else {
x = L_GET16(p);
}
return x;
}
int
IMGetI16(conn, offset)
IMConnection *conn;
int offset;
{
long x;
x = (long)IMGetC16(conn, offset);
return (x < 32768) ? (int)x : (int)(x - 65536L);
}
unsigned long
IMGetC32(conn, offset)
IMConnection *conn;
int offset;
{
IMBuffer *ibp = IM_INBUF(conn);
unsigned char *p = (unsigned char *)IMBUFDATA(ibp) + offset;
unsigned long x;
if (conn->byte_order == ORDER_BIG) {
x = B_GET32(p);
} else {
x = L_GET32(p);
}
return x;
}
void
IMGetString(conn, offset, buf, len)
IMConnection *conn;
int offset;
char *buf;
int len;
{
IMBuffer *ibp = IM_INBUF(conn);
char *p = IMBUFDATA(ibp) + offset;
bcopy(p, buf, len);
buf[len] = '\0';
}
void
IMPutC8(conn, x)
IMConnection *conn;
int x;
{
IMBuffer *ibp = IM_OUTBUF(conn);
unsigned char c = (unsigned char)x;
IMBufAdd(ibp, (char *)&c, 1);
}
void
IMPutC16(conn, x)
IMConnection *conn;
unsigned int x;
{
IMBuffer *ibp = IM_OUTBUF(conn);
unsigned char *p = (unsigned char *)IMBufAlloc(ibp, 2);
if (conn->byte_order == ORDER_BIG) {
B_PUT16(x, p);
} else {
L_PUT16(x, p);
}
}
void
IMPutC32(conn, x)
IMConnection *conn;
unsigned long x;
{
IMBuffer *ibp = IM_OUTBUF(conn);
unsigned char *p = (unsigned char *)IMBufAlloc(ibp, 4);
if (conn->byte_order == ORDER_BIG) {
B_PUT32(x, p);
} else {
L_PUT32(x, p);
}
}
void
IMPutI16(conn, x)
IMConnection *conn;
int x;
{
IMBuffer *ibp = IM_OUTBUF(conn);
unsigned char *p = (unsigned char *)IMBufAlloc(ibp, 2);
if (conn->byte_order == ORDER_BIG) {
B_PUT16(x, p);
} else {
L_PUT16(x, p);
}
}
void
IMPutString(conn, s, len)
IMConnection *conn;
char *s;
int len;
{
if (len < 0) len = strlen(s);
IMBufAdd(&conn->out_buf, s, len);
}
void
IMPutPad(conn)
IMConnection *conn;
{
int remainder;
int npad;
static char padding[] = { 0, 0, 0, 0 };
if ((remainder = IMBUFLEN(&conn->out_buf) % 4) != 0) {
npad = 4 - remainder;
IMBufAdd(IM_OUTBUF(conn), padding, npad);
}
}
void
IMRewriteC16(conn, pos, x)
IMConnection *conn;
int pos;
unsigned int x;
{
IMBuffer *ibp = IM_OUTBUF(conn);
unsigned char p[2];
if (conn->byte_order == ORDER_BIG) {
B_PUT16(x, p);
} else {
L_PUT16(x, p);
}
IMBufOverwrite(ibp, pos, (char *)p, 2);
}
int
IMWritePos(conn)
IMConnection *conn;
{
IMBuffer *ibp = IM_OUTBUF(conn);
return IMBUFLEN(ibp);
}
int
IMPutHeader(conn, major, minor, arglen)
IMConnection *conn;
int major;
int minor;
int arglen;
{
int offset;
offset = IMBUFLEN(&conn->out_buf);
arglen = (arglen + 3) / 4;
#ifdef XIM_BC
if (conn->has_length_bug) arglen *= 4;
#endif
IMPutC8(conn, major);
IMPutC8(conn, minor);
IMPutC16(conn, (unsigned int)arglen);
return offset;
}
void
IMFinishRequest(conn, offset)
IMConnection *conn;
int offset;
{
IMBuffer *ibp = IM_OUTBUF(conn);
unsigned int arglen;
/*
* Append padding bytes, if needed.
*/
IMPutPad(conn);
/*
* Offset points the beginning of the request.
* Following is a Request header which is 4-byte long.
*/
arglen = (unsigned int)((IMBUFLEN(ibp) - offset - 4) / 4);
#ifdef XIM_BC
if (conn->has_length_bug) arglen *= 4;
#endif
/*
* rewrite the length field of the request header.
*/
IMRewriteC16(conn, offset + 2, arglen);
IMSchedule(conn, SCHED_WRITE);
}
void
IMCancelRequest(conn, offset)
IMConnection *conn;
int offset;
{
IMBuffer *ibp = IM_OUTBUF(conn);
IMBufDiscard(ibp, offset - IMBUFLEN(ibp));
}
void
IMSendSimpleRequest(conn, major, minor)
IMConnection *conn;
int major;
int minor;
{
IMPutC8(conn, major);
IMPutC8(conn, minor);
/*
* This function (IMSendSimpleRequest) might be called when
* client's byte order is yet unknown, so usually it is unwise to
* call IMPutC16(). But in this particular case where the value
* to be put is zero, it is perfectly ok.
*/
IMPutC16(conn, 0);
IMSchedule(conn, SCHED_WRITE);
}
void
IMSendRequestWithIC(conn, major, minor, icp)
IMConnection *conn;
int major;
int minor;
IMIC *icp;
{
(void)IMPutHeader(conn, major, minor, 4);
IMPutC16(conn, icp->im->id);
IMPutC16(conn, icp->id);
IMSchedule(conn, SCHED_WRITE);
}
void
IMSendError(conn, code, imid, icid, msg)
IMConnection *conn;
int code;
unsigned int imid;
unsigned int icid;
char *msg;
{
int offset;
int msg_len;
unsigned int valid_flag = 0;
msg_len = strlen(msg);
if (imid != 0) valid_flag |= 1;
if (icid != 0) valid_flag |= 2;
offset = IMPutHeader(conn, XIM_ERROR, 0, 0);
IMPutC16(conn, imid);
IMPutC16(conn, icid);
IMPutC16(conn, valid_flag);
IMPutC16(conn, (unsigned int)code);
IMPutC16(conn, (unsigned int)msg_len);
IMPutC16(conn, 0);
IMPutString(conn, msg, msg_len);
IMFinishRequest(conn, offset);
}
void
IMSendBadProtocol(conn, msg)
IMConnection *conn;
char *msg;
{
IMSendError(conn, IMBadProtocol, 0, 0, msg);
}
void
IMSendBadLength(conn, imid, icid)
IMConnection *conn;
unsigned int imid;
unsigned int icid;
{
IMSendError(conn, IMBadSomething, imid, icid, "Bad request length");
}
|