6 #include <9p-mixp/mixp.h>
7 #include <9p-mixp/convert.h>
8 #include <9p-mixp/stat.h>
9 #include "mixp_local.h"
22 mixp_puint(MIXP_MESSAGE *msg, unsigned int size, unsigned int *val) {
25 if(msg->pos + size <= msg->end) {
30 printf("pack int: v=%d\n", v);
36 printf("pack int: SDW[3]=%d\n", b);
38 printf("pack int: SDW[2]=%d\n", b);
42 printf("pack int: SDW[1]=%d\n", b);
46 printf("pack int: SDW[0]=%d\n", b);
49 printf("mixp_puint32: unhandled int size: %d\n", size);
58 printf("SDW pos[3]=%d\n", a);
60 printf("SDW pos[2]=%d\n", a);
65 printf("SW pos[1]=%d\n", a);
69 printf("B pos[0]=%d\n", a);
74 printf("unpack int: %d\n", v);
81 void mixp_pu8_store(MIXP_MESSAGE
* msg
, uint8_t val
)
86 if(msg
->pos
+ size
<= msg
->end
)
90 unsigned char* packtext
= (unsigned char*)msg
->pos
;
94 printf("mixp_pu8_store() val=%d text=%02X\n",
102 void mixp_pu32_store(MIXP_MESSAGE
* msg
, uint32_t val
)
107 if(msg
->pos
+ size
<= msg
->end
)
110 unsigned char p3
= v
>>24;
111 unsigned char p2
= v
>>16;
112 unsigned char p1
= v
>>8;
113 unsigned char p0
= v
;
114 unsigned char* packtext
= (unsigned char*)msg
->pos
;
121 printf("mixp_pu32_store() val=%d text=%02X:%02X:%02X:%02X\n",
132 void mixp_pu64_store(MIXP_MESSAGE
* msg
, uint64_t val
)
137 if(msg
->pos
+ size
<= msg
->end
)
141 unsigned char p7
= v
>>56;
142 unsigned char p6
= v
>>48;
143 unsigned char p5
= v
>>40;
144 unsigned char p4
= v
>>32;
145 unsigned char p3
= v
>>24;
146 unsigned char p2
= v
>>16;
147 unsigned char p1
= v
>>8;
148 unsigned char p0
= v
;
149 unsigned char* packtext
= (unsigned char*)msg
->pos
;
160 fprintf(stderr
,"mixp_pu64_store() val=%llu text=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
176 void mixp_pu16_store(MIXP_MESSAGE
* msg
, uint16_t val
)
180 if(msg
->pos
+ SWord
<= msg
->end
)
183 unsigned char p1
= v
>>8;
184 unsigned char p0
= v
;
185 unsigned char* packtext
= (unsigned char*)msg
->pos
;
190 printf("mixp_pu16_store() val=%d text=%02X:%02X\n",
199 uint8_t mixp_pu8_load(MIXP_MESSAGE
* msg
)
202 if(msg
->pos
+ SByte
<= msg
->end
)
204 unsigned char* packtext
= (unsigned char*)msg
->pos
;
207 printf("mixp_pu8_load() val=%d text=%02X\n", v
, packtext
[0]);
214 uint32_t mixp_pu32_load(MIXP_MESSAGE
* msg
)
218 if(msg
->pos
+ size
<= msg
->end
)
220 unsigned char* buf
= (unsigned char*)msg
->pos
;
227 printf("mixp_pu32_load() value=%d text=%02X:%02X:%02X:%02X\n",
239 uint64_t mixp_pu64_load(MIXP_MESSAGE
* msg
)
243 if(msg
->pos
+ size
<= msg
->end
)
245 unsigned char* buf
= (unsigned char*)msg
->pos
;
251 ((uint64_t)buf
[4]<<32) +
252 ((uint64_t)buf
[5]<<40) +
253 ((uint64_t)buf
[6]<<48) +
254 ((uint64_t)buf
[7]<<52);
257 fprintf(stderr
,"mixp_pu64_load() value=%llu text=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
273 uint16_t mixp_pu16_load(MIXP_MESSAGE
* msg
)
277 if(msg
->pos
+ size
<= msg
->end
)
279 unsigned char* buf
= (unsigned char*)msg
->pos
;
284 printf("mixp_pu16_load() value=%d text=%02X:%02X\n",
295 mixp_pu32(MIXP_MESSAGE
*msg
, uint32_t *val
)
297 if (msg
->mode
== MsgPack
)
298 mixp_pu32_store(msg
, *val
);
300 *val
= mixp_pu32_load(msg
);
304 mixp_psize(MIXP_MESSAGE
*msg
, size_t *val
) {
309 mixp_pu8(MIXP_MESSAGE
*msg
, unsigned char *val
)
311 if (msg
->mode
==MsgPack
)
312 mixp_pu8_store(msg
,*val
);
314 *val
= mixp_pu8_load(msg
);
318 mixp_pu16(MIXP_MESSAGE
*msg
, unsigned short *val
)
320 if (msg
->mode
== MsgPack
)
321 mixp_pu16_store(msg
,*val
);
323 *val
= mixp_pu16_load(msg
);
327 mixp_pu64(MIXP_MESSAGE
*msg
, uint64_t *val
)
329 if (msg
->mode
== MsgPack
)
330 mixp_pu64_store(msg
,*val
);
332 *val
= mixp_pu64_load(msg
);
336 mixp_pstring(MIXP_MESSAGE
*msg
, char **s
) {
339 if(msg
->mode
== MsgPack
)
341 mixp_pu16(msg
, &len
);
343 if(msg
->pos
+ len
<= msg
->end
) {
344 if(msg
->mode
== MsgUnpack
) {
345 *s
= malloc(len
+ 1);
346 memcpy(*s
, msg
->pos
, len
);
349 memcpy(msg
->pos
, *s
, len
);
355 mixp_pstrings(MIXP_MESSAGE
*msg
, unsigned short *num
, char *strings
[]) {
356 char *s
= NULL
; // suppress compiler warning. YES, it's really okay (look carefully ;-P)
357 unsigned int i
, size
;
361 if(*num
> IXP_MAX_WELEM
) {
362 msg
->pos
= msg
->end
+1;
366 if(msg
->mode
== MsgUnpack
) {
369 for(i
=0; i
< *num
; i
++) {
370 mixp_pu16(msg
, &len
);
373 if(msg
->pos
> msg
->end
)
381 for(i
=0; i
< *num
; i
++) {
382 if(msg
->mode
== MsgPack
)
383 len
= strlen(strings
[i
]);
384 mixp_pu16(msg
, &len
);
386 if(msg
->mode
== MsgUnpack
) {
387 memcpy(s
, msg
->pos
, len
);
393 mixp_pdata(msg
, &strings
[i
], len
);
398 mixp_pdata(MIXP_MESSAGE
*msg
, char **data
, unsigned int len
) {
399 if(msg
->pos
+ len
<= msg
->end
) {
400 if(msg
->mode
== MsgUnpack
) {
402 memcpy(*data
, msg
->pos
, len
);
404 memcpy(msg
->pos
, *data
, len
);
410 mixp_pqid(MIXP_MESSAGE
*msg
, MIXP_QID
*qid
) {
411 mixp_pu8(msg
, &qid
->type
);
412 mixp_pu32(msg
, &qid
->version
);
413 mixp_pu64(msg
, &qid
->path
);
417 mixp_pqids(MIXP_MESSAGE
*msg
, unsigned short *num
, MIXP_QID qid
[]) {
421 if(*num
> IXP_MAX_WELEM
) {
422 msg
->pos
= msg
->end
+1;
426 for(i
= 0; i
< *num
; i
++)
427 mixp_pqid(msg
, &qid
[i
]);
431 mixp_pstat(MIXP_MESSAGE
*msg
, MIXP_STAT
*stat
) {
436 fprintf(stderr
,"mixp_pstat() stat ptr NULL\n");
440 if(msg
->mode
== MsgPack
)
441 size
= mixp_stat_sizeof(stat
) - 2;
443 mixp_pu16(msg
, &size
);
444 mixp_pu16(msg
, &stat
->type
);
445 mixp_pu32(msg
, &stat
->dev
);
446 mixp_pqid(msg
, &stat
->qid
);
447 mixp_pu32(msg
, &stat
->mode
);
448 mixp_pu32(msg
, &stat
->atime
);
449 mixp_pu32(msg
, &stat
->mtime
);
450 mixp_pu64(msg
, &stat
->length
);
451 mixp_pstring(msg
, &stat
->name
);
452 mixp_pstring(msg
, &stat
->uid
);
453 mixp_pstring(msg
, &stat
->gid
);
454 mixp_pstring(msg
, &stat
->muid
);
457 if (stat
->name
== NULL
)
458 stat
->name
= strdup("");
459 if (stat
->uid
== NULL
)
460 stat
->uid
= strdup("");
461 if (stat
->gid
== NULL
)
462 stat
->gid
= strdup("");
463 if (stat
->muid
== NULL
)
464 stat
->muid
= strdup("");