akaros/tools/compilers/gcc-glibc/glibc-2.19-akaros/sysdeps/akaros/convS2M.c
<<
>>
Prefs
   1/* 
   2 * This file is part of the UCB release of Plan 9. It is subject to the license
   3 * terms in the LICENSE file found in the top-level directory of this
   4 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   5 * part of the UCB release of Plan 9, including this file, may be copied,
   6 * modified, propagated, or distributed except according to the terms contained
   7 * in the LICENSE file.
   8 */
   9
  10#include <string.h>
  11#include <fcall.h>
  12
  13static
  14uint8_t *pstring(uint8_t * p, char *s)
  15{
  16        unsigned int n;
  17
  18        if (s == NULL) {
  19                PBIT16(p, 0);
  20                p += BIT16SZ;
  21                return p;
  22        }
  23
  24        n = strlen(s);
  25        /*
  26         * We are moving the string before the length,
  27         * so you can S2M a struct into an existing message
  28         */
  29        memmove(p + BIT16SZ, s, n);
  30        PBIT16(p, n);
  31        p += n + BIT16SZ;
  32        return p;
  33}
  34
  35static
  36uint8_t *pqid(uint8_t * p, struct qid *q)
  37{
  38        PBIT8(p, q->type);
  39        p += BIT8SZ;
  40        PBIT32(p, q->vers);
  41        p += BIT32SZ;
  42        PBIT64(p, q->path);
  43        p += BIT64SZ;
  44        return p;
  45}
  46
  47static
  48unsigned int stringsz(char *s)
  49{
  50        if (s == NULL)
  51                return BIT16SZ;
  52
  53        return BIT16SZ + strlen(s);
  54}
  55
  56unsigned int sizeS2M(struct fcall *f)
  57{
  58        unsigned int n;
  59        int i;
  60
  61        n = 0;
  62        n += BIT32SZ;   /* size */
  63        n += BIT8SZ;    /* type */
  64        n += BIT16SZ;   /* tag */
  65
  66        switch (f->type) {
  67        default:
  68                return 0;
  69
  70        case Tversion:
  71                n += BIT32SZ;
  72                n += stringsz(f->version);
  73                break;
  74
  75        case Tflush:
  76                n += BIT16SZ;
  77                break;
  78
  79        case Tauth:
  80                n += BIT32SZ;
  81                n += stringsz(f->uname);
  82                n += stringsz(f->aname);
  83                break;
  84
  85        case Tattach:
  86                n += BIT32SZ;
  87                n += BIT32SZ;
  88                n += stringsz(f->uname);
  89                n += stringsz(f->aname);
  90                break;
  91
  92        case Twalk:
  93                n += BIT32SZ;
  94                n += BIT32SZ;
  95                n += BIT16SZ;
  96                for (i = 0; i < f->nwname; i++)
  97                        n += stringsz(f->wname[i]);
  98                break;
  99
 100        case Topen:
 101                n += BIT32SZ;
 102                n += BIT8SZ;
 103                break;
 104
 105        case Tcreate:
 106                n += BIT32SZ;
 107                n += stringsz(f->name);
 108                n += BIT32SZ;
 109                n += BIT8SZ;
 110                break;
 111
 112        case Tread:
 113                n += BIT32SZ;
 114                n += BIT64SZ;
 115                n += BIT32SZ;
 116                break;
 117
 118        case Twrite:
 119                n += BIT32SZ;
 120                n += BIT64SZ;
 121                n += BIT32SZ;
 122                n += f->count;
 123                break;
 124
 125        case Tclunk:
 126        case Tremove:
 127                n += BIT32SZ;
 128                break;
 129
 130        case Tstat:
 131                n += BIT32SZ;
 132                break;
 133
 134        case Twstat:
 135                n += BIT32SZ;
 136                n += BIT16SZ;
 137                n += f->nstat;
 138                break;
 139
 140
 141
 142        case Rversion:
 143                n += BIT32SZ;
 144                n += stringsz(f->version);
 145                break;
 146
 147        case Rerror:
 148                n += stringsz(f->ename);
 149                break;
 150
 151        case Rflush:
 152                break;
 153
 154        case Rauth:
 155                n += QIDSZ;
 156                break;
 157
 158        case Rattach:
 159                n += QIDSZ;
 160                break;
 161
 162        case Rwalk:
 163                n += BIT16SZ;
 164                n += f->nwqid * QIDSZ;
 165                break;
 166
 167        case Ropen:
 168        case Rcreate:
 169                n += QIDSZ;
 170                n += BIT32SZ;
 171                break;
 172
 173        case Rread:
 174                n += BIT32SZ;
 175                n += f->count;
 176                break;
 177
 178        case Rwrite:
 179                n += BIT32SZ;
 180                break;
 181
 182        case Rclunk:
 183                break;
 184
 185        case Rremove:
 186                break;
 187
 188        case Rstat:
 189                n += BIT16SZ;
 190                n += f->nstat;
 191                break;
 192
 193        case Rwstat:
 194                break;
 195        }
 196        return n;
 197}
 198
 199unsigned int convS2M(struct fcall *f, uint8_t * ap, unsigned int nap)
 200{
 201        uint8_t *p;
 202        unsigned int i, size;
 203
 204        size = sizeS2M(f);
 205        if (size == 0)
 206                return 0;
 207        if (size > nap)
 208                return 0;
 209
 210        p = (uint8_t *) ap;
 211
 212        PBIT32(p, size);
 213        p += BIT32SZ;
 214        PBIT8(p, f->type);
 215        p += BIT8SZ;
 216        PBIT16(p, f->tag);
 217        p += BIT16SZ;
 218
 219        switch (f->type) {
 220        default:
 221                return 0;
 222
 223        case Tversion:
 224                PBIT32(p, f->msize);
 225                p += BIT32SZ;
 226                p = pstring(p, f->version);
 227                break;
 228
 229        case Tflush:
 230                PBIT16(p, f->oldtag);
 231                p += BIT16SZ;
 232                break;
 233
 234        case Tauth:
 235                PBIT32(p, f->afid);
 236                p += BIT32SZ;
 237                p = pstring(p, f->uname);
 238                p = pstring(p, f->aname);
 239                break;
 240
 241        case Tattach:
 242                PBIT32(p, f->fid);
 243                p += BIT32SZ;
 244                PBIT32(p, f->afid);
 245                p += BIT32SZ;
 246                p = pstring(p, f->uname);
 247                p = pstring(p, f->aname);
 248                break;
 249
 250        case Twalk:
 251                PBIT32(p, f->fid);
 252                p += BIT32SZ;
 253                PBIT32(p, f->newfid);
 254                p += BIT32SZ;
 255                PBIT16(p, f->nwname);
 256                p += BIT16SZ;
 257                if (f->nwname > MAXWELEM)
 258                        return 0;
 259                for (i = 0; i < f->nwname; i++)
 260                        p = pstring(p, f->wname[i]);
 261                break;
 262
 263        case Topen:
 264                PBIT32(p, f->fid);
 265                p += BIT32SZ;
 266                PBIT8(p, f->mode);
 267                p += BIT8SZ;
 268                break;
 269
 270        case Tcreate:
 271                PBIT32(p, f->fid);
 272                p += BIT32SZ;
 273                p = pstring(p, f->name);
 274                PBIT32(p, f->perm);
 275                p += BIT32SZ;
 276                PBIT8(p, f->mode);
 277                p += BIT8SZ;
 278                break;
 279
 280        case Tread:
 281                PBIT32(p, f->fid);
 282                p += BIT32SZ;
 283                PBIT64(p, f->offset);
 284                p += BIT64SZ;
 285                PBIT32(p, f->count);
 286                p += BIT32SZ;
 287                break;
 288
 289        case Twrite:
 290                PBIT32(p, f->fid);
 291                p += BIT32SZ;
 292                PBIT64(p, f->offset);
 293                p += BIT64SZ;
 294                PBIT32(p, f->count);
 295                p += BIT32SZ;
 296                memmove(p, f->data, f->count);
 297                p += f->count;
 298                break;
 299
 300        case Tclunk:
 301        case Tremove:
 302                PBIT32(p, f->fid);
 303                p += BIT32SZ;
 304                break;
 305
 306        case Tstat:
 307                PBIT32(p, f->fid);
 308                p += BIT32SZ;
 309                break;
 310
 311        case Twstat:
 312                PBIT32(p, f->fid);
 313                p += BIT32SZ;
 314                PBIT16(p, f->nstat);
 315                p += BIT16SZ;
 316                memmove(p, f->stat, f->nstat);
 317                p += f->nstat;
 318                break;
 319
 320
 321
 322        case Rversion:
 323                PBIT32(p, f->msize);
 324                p += BIT32SZ;
 325                p = pstring(p, f->version);
 326                break;
 327
 328        case Rerror:
 329                p = pstring(p, f->ename);
 330                break;
 331
 332        case Rflush:
 333                break;
 334
 335        case Rauth:
 336                p = pqid(p, &f->aqid);
 337                break;
 338
 339        case Rattach:
 340                p = pqid(p, &f->qid);
 341                break;
 342
 343        case Rwalk:
 344                PBIT16(p, f->nwqid);
 345                p += BIT16SZ;
 346                if (f->nwqid > MAXWELEM)
 347                        return 0;
 348                for (i = 0; i < f->nwqid; i++)
 349                        p = pqid(p, &f->wqid[i]);
 350                break;
 351
 352        case Ropen:
 353        case Rcreate:
 354                p = pqid(p, &f->qid);
 355                PBIT32(p, f->iounit);
 356                p += BIT32SZ;
 357                break;
 358
 359        case Rread:
 360                PBIT32(p, f->count);
 361                p += BIT32SZ;
 362                memmove(p, f->data, f->count);
 363                p += f->count;
 364                break;
 365
 366        case Rwrite:
 367                PBIT32(p, f->count);
 368                p += BIT32SZ;
 369                break;
 370
 371        case Rclunk:
 372                break;
 373
 374        case Rremove:
 375                break;
 376
 377        case Rstat:
 378                PBIT16(p, f->nstat);
 379                p += BIT16SZ;
 380                memmove(p, f->stat, f->nstat);
 381                p += f->nstat;
 382                break;
 383
 384        case Rwstat:
 385                break;
 386        }
 387        if (size != p - ap)
 388                return 0;
 389        return size;
 390}
 391