convS2M.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include "lib9.h"
  2. #include "fcall.h"
  3. static
  4. uchar*
  5. pstring(uchar *p, char *s)
  6. {
  7. uint n;
  8. if(s == nil){
  9. PBIT16(p, 0);
  10. p += BIT16SZ;
  11. return p;
  12. }
  13. n = strlen(s);
  14. PBIT16(p, n);
  15. p += BIT16SZ;
  16. memmove(p, s, n);
  17. p += n;
  18. return p;
  19. }
  20. static
  21. uchar*
  22. pqid(uchar *p, Qid *q)
  23. {
  24. PBIT8(p, q->type);
  25. p += BIT8SZ;
  26. PBIT32(p, q->vers);
  27. p += BIT32SZ;
  28. PBIT64(p, q->path);
  29. p += BIT64SZ;
  30. return p;
  31. }
  32. static
  33. uint
  34. stringsz(char *s)
  35. {
  36. if(s == nil)
  37. return BIT16SZ;
  38. return BIT16SZ+strlen(s);
  39. }
  40. uint
  41. sizeS2M(Fcall *f)
  42. {
  43. uint n;
  44. int i;
  45. n = 0;
  46. n += BIT32SZ; /* size */
  47. n += BIT8SZ; /* type */
  48. n += BIT16SZ; /* tag */
  49. switch(f->type)
  50. {
  51. default:
  52. return 0;
  53. case Tversion:
  54. n += BIT32SZ;
  55. n += stringsz(f->version);
  56. break;
  57. case Tflush:
  58. n += BIT16SZ;
  59. break;
  60. case Tauth:
  61. n += BIT32SZ;
  62. n += stringsz(f->uname);
  63. n += stringsz(f->aname);
  64. break;
  65. case Tattach:
  66. n += BIT32SZ;
  67. n += BIT32SZ;
  68. n += stringsz(f->uname);
  69. n += stringsz(f->aname);
  70. break;
  71. case Twalk:
  72. n += BIT32SZ;
  73. n += BIT32SZ;
  74. n += BIT16SZ;
  75. for(i=0; i<f->nwname; i++)
  76. n += stringsz(f->wname[i]);
  77. break;
  78. case Topen:
  79. n += BIT32SZ;
  80. n += BIT8SZ;
  81. break;
  82. case Tcreate:
  83. n += BIT32SZ;
  84. n += stringsz(f->name);
  85. n += BIT32SZ;
  86. n += BIT8SZ;
  87. break;
  88. case Tread:
  89. n += BIT32SZ;
  90. n += BIT64SZ;
  91. n += BIT32SZ;
  92. break;
  93. case Twrite:
  94. n += BIT32SZ;
  95. n += BIT64SZ;
  96. n += BIT32SZ;
  97. n += f->count;
  98. break;
  99. case Tclunk:
  100. case Tremove:
  101. n += BIT32SZ;
  102. break;
  103. case Tstat:
  104. n += BIT32SZ;
  105. break;
  106. case Twstat:
  107. n += BIT32SZ;
  108. n += BIT16SZ;
  109. n += f->nstat;
  110. break;
  111. /*
  112. */
  113. case Rversion:
  114. n += BIT32SZ;
  115. n += stringsz(f->version);
  116. break;
  117. case Rerror:
  118. n += stringsz(f->ename);
  119. break;
  120. case Rflush:
  121. break;
  122. case Rauth:
  123. n += QIDSZ;
  124. break;
  125. case Rattach:
  126. n += QIDSZ;
  127. break;
  128. case Rwalk:
  129. n += BIT16SZ;
  130. n += f->nwqid*QIDSZ;
  131. break;
  132. case Ropen:
  133. case Rcreate:
  134. n += QIDSZ;
  135. n += BIT32SZ;
  136. break;
  137. case Rread:
  138. n += BIT32SZ;
  139. n += f->count;
  140. break;
  141. case Rwrite:
  142. n += BIT32SZ;
  143. break;
  144. case Rclunk:
  145. break;
  146. case Rremove:
  147. break;
  148. case Rstat:
  149. n += BIT16SZ;
  150. n += f->nstat;
  151. break;
  152. case Rwstat:
  153. break;
  154. }
  155. return n;
  156. }
  157. uint
  158. convS2M(Fcall *f, uchar *ap, uint nap)
  159. {
  160. uchar *p;
  161. uint i, size;
  162. size = sizeS2M(f);
  163. if(size == 0)
  164. return 0;
  165. if(size > nap)
  166. return 0;
  167. p = (uchar*)ap;
  168. PBIT32(p, size);
  169. p += BIT32SZ;
  170. PBIT8(p, f->type);
  171. p += BIT8SZ;
  172. PBIT16(p, f->tag);
  173. p += BIT16SZ;
  174. switch(f->type)
  175. {
  176. default:
  177. return 0;
  178. case Tversion:
  179. PBIT32(p, f->msize);
  180. p += BIT32SZ;
  181. p = pstring(p, f->version);
  182. break;
  183. case Tflush:
  184. PBIT16(p, f->oldtag);
  185. p += BIT16SZ;
  186. break;
  187. case Tauth:
  188. PBIT32(p, f->afid);
  189. p += BIT32SZ;
  190. p = pstring(p, f->uname);
  191. p = pstring(p, f->aname);
  192. break;
  193. case Tattach:
  194. PBIT32(p, f->fid);
  195. p += BIT32SZ;
  196. PBIT32(p, f->afid);
  197. p += BIT32SZ;
  198. p = pstring(p, f->uname);
  199. p = pstring(p, f->aname);
  200. break;
  201. case Twalk:
  202. PBIT32(p, f->fid);
  203. p += BIT32SZ;
  204. PBIT32(p, f->newfid);
  205. p += BIT32SZ;
  206. PBIT16(p, f->nwname);
  207. p += BIT16SZ;
  208. if(f->nwname > MAXWELEM)
  209. return 0;
  210. for(i=0; i<f->nwname; i++)
  211. p = pstring(p, f->wname[i]);
  212. break;
  213. case Topen:
  214. PBIT32(p, f->fid);
  215. p += BIT32SZ;
  216. PBIT8(p, f->mode);
  217. p += BIT8SZ;
  218. break;
  219. case Tcreate:
  220. PBIT32(p, f->fid);
  221. p += BIT32SZ;
  222. p = pstring(p, f->name);
  223. PBIT32(p, f->perm);
  224. p += BIT32SZ;
  225. PBIT8(p, f->mode);
  226. p += BIT8SZ;
  227. break;
  228. case Tread:
  229. PBIT32(p, f->fid);
  230. p += BIT32SZ;
  231. PBIT64(p, f->offset);
  232. p += BIT64SZ;
  233. PBIT32(p, f->count);
  234. p += BIT32SZ;
  235. break;
  236. case Twrite:
  237. PBIT32(p, f->fid);
  238. p += BIT32SZ;
  239. PBIT64(p, f->offset);
  240. p += BIT64SZ;
  241. PBIT32(p, f->count);
  242. p += BIT32SZ;
  243. memmove(p, f->data, f->count);
  244. p += f->count;
  245. break;
  246. case Tclunk:
  247. case Tremove:
  248. PBIT32(p, f->fid);
  249. p += BIT32SZ;
  250. break;
  251. case Tstat:
  252. PBIT32(p, f->fid);
  253. p += BIT32SZ;
  254. break;
  255. case Twstat:
  256. PBIT32(p, f->fid);
  257. p += BIT32SZ;
  258. PBIT16(p, f->nstat);
  259. p += BIT16SZ;
  260. memmove(p, f->stat, f->nstat);
  261. p += f->nstat;
  262. break;
  263. /*
  264. */
  265. case Rversion:
  266. PBIT32(p, f->msize);
  267. p += BIT32SZ;
  268. p = pstring(p, f->version);
  269. break;
  270. case Rerror:
  271. p = pstring(p, f->ename);
  272. break;
  273. case Rflush:
  274. break;
  275. case Rauth:
  276. p = pqid(p, &f->aqid);
  277. break;
  278. case Rattach:
  279. p = pqid(p, &f->qid);
  280. break;
  281. case Rwalk:
  282. PBIT16(p, f->nwqid);
  283. p += BIT16SZ;
  284. if(f->nwqid > MAXWELEM)
  285. return 0;
  286. for(i=0; i<f->nwqid; i++)
  287. p = pqid(p, &f->wqid[i]);
  288. break;
  289. case Ropen:
  290. case Rcreate:
  291. p = pqid(p, &f->qid);
  292. PBIT32(p, f->iounit);
  293. p += BIT32SZ;
  294. break;
  295. case Rread:
  296. PBIT32(p, f->count);
  297. p += BIT32SZ;
  298. memmove(p, f->data, f->count);
  299. p += f->count;
  300. break;
  301. case Rwrite:
  302. PBIT32(p, f->count);
  303. p += BIT32SZ;
  304. break;
  305. case Rclunk:
  306. break;
  307. case Rremove:
  308. break;
  309. case Rstat:
  310. PBIT16(p, f->nstat);
  311. p += BIT16SZ;
  312. memmove(p, f->stat, f->nstat);
  313. p += f->nstat;
  314. break;
  315. case Rwstat:
  316. break;
  317. }
  318. if(size != p-ap)
  319. return 0;
  320. return size;
  321. }