Lines Matching refs:uu

30 static const char lib[] = "libuu:uu";
64 * initialize the uu map from dp
90 * grab uu header from input
94 uu_header(register Uu_t* uu)
106 if (!(s = sfgetr(uu->ip, '\n', 1)))
108 if (uu->disc->errorf)
109 (*uu->disc->errorf)(uu, uu->disc, 2, "unknown encoding");
122 uu->flags |= options[n].flag;
128 uu->mode = strperm(s, &t, 0) & ~(S_IXUSR|S_IXGRP|S_IXOTH);
131 if (!uu->path)
133 uu->path = strdup(t);
134 uu->flags |= UU_FREEPATH;
136 if (meth->name != uu->meth.name)
138 if (!(((Uudata_t*)uu->meth.data)->flags & UU_DEFAULT) && !streq(meth->id, uu->meth.id) && uu->disc->errorf)
139 (*uu->disc->errorf)(uu, uu->disc, 1, "switching to %s encoding", meth->name);
140 uu->meth = *meth;
151 * uu encode input to output
155 uu_encode(register Uu_t* uu)
173 dp = (Uudata_t*)uu->meth.data;
176 text = !!(uu->flags & UU_TEXT);
178 if (uu->flags & UU_HEADER)
180 if (fstat(sffileno(uu->ip), &st) || !(c = modex(st.st_mode) & 0777))
182 sfprintf(uu->op, "%s%s%s %03o %s\n", UU_BEGIN, uu->meth.id, text ? "-text" : "", c, uu->path ? uu->path : "-");
198 if ((c1 = sfgetc(uu->ip)) == EOF)
211 if ((c2 = sfgetc(uu->ip)) == EOF)
231 if ((c3 = sfgetc(uu->ip)) == EOF)
256 sfwrite(uu->op, buf, p - buf);
263 sfwrite(uu->op, buf, p - buf);
266 sfprintf(uu->op, "%c\n", m[0]);
267 if (uu->flags & UU_HEADER)
268 sfputr(uu->op, dp->end, '\n');
273 * uu decode input to output
277 uu_decode(register Uu_t* uu)
293 dp = (Uudata_t*)uu->meth.data;
294 if (uu->path && (uu->flags & UU_CLOSEOUT) && (dp->flags & uu->flags & UU_HEADER) && chmod(uu->path, uu->mode) && uu->disc->errorf)
295 (*uu->disc->errorf)(uu, uu->disc, ERROR_SYSTEM|2, "%s: cannot change mode to %s", uu->path, fmtperm(uu->mode));
296 text = !!(uu->flags & UU_TEXT);
302 while (((s = sfgetr(uu->ip, '\n', 0)) || (s = sfgetr(uu->ip, '\n', -1))) && ((n = sfvalue(uu->ip)) != tl || !strneq(s, t, tl - 1)))
307 if (uu->disc->errorf)
308 (*uu->disc->errorf)(uu, uu->disc, 2, "input is not %s encoded", uu->meth.name);
341 sfwrite(uu->op, buf, c);
343 if (!s && (uu->flags & UU_HEADER) && uu->disc->errorf)
344 (*uu->disc->errorf)(uu, uu->disc, 1, "end sequence `%s' omitted", t);
350 while ((c = m[sfgetc(uu->ip)]) >= 64)
354 while ((c = m[sfgetc(uu->ip)]) >= 64)
357 if (uu->disc->errorf)
358 (*uu->disc->errorf)(uu, uu->disc, 1, "%c: extra input character ignored", c);
362 while ((c = m[sfgetc(uu->ip)]) >= 64)
368 sfputc(uu->op, x);
370 else sfputc(uu->op, n >> 4);
374 while ((c = m[sfgetc(uu->ip)]) >= 64)
380 sfputc(uu->op, x);
382 sfputc(uu->op, x);
386 sfputc(uu->op, n >> 10);
387 sfputc(uu->op, n >> 2);
395 sfputc(uu->op, x);
397 sfputc(uu->op, x);
399 sfputc(uu->op, x);
403 sfputc(uu->op, (n >> 16));
404 sfputc(uu->op, (n >> 8));
405 sfputc(uu->op, (n));
410 while ((c = sfgetc(uu->ip)) != EOF)
413 if (n < 4 && (uu->flags & UU_HEADER) && uu->disc->errorf)
414 (*uu->disc->errorf)(uu, uu->disc, 1, "input end sequence `%s' omitted", dp->end);
426 qp_encode(register Uu_t* uu)
437 while ((s = (unsigned char*)sfgetr(uu->ip, '\n', 0)) || (s = (unsigned char*)sfgetr(uu->ip, '\n', -1)))
439 e = s + sfvalue(uu->ip);
462 sfwrite(uu->op, buf, b - buf);
470 sfwrite(uu->op, buf, b - buf);
496 sfwrite(uu->op, buf, b - buf);
506 qp_decode(register Uu_t* uu)
520 while (s = (unsigned char*)sfgetr(uu->ip, '\n', 1))
522 if (((b = s + sfvalue(uu->ip)) > s) && !*--b)
547 sfwrite(uu->op, x, b - x);
605 bx_q_getc(register Uu_t* uu, register Bx_t* bx)
629 while ((c = m[sfgetc(uu->ip)]) >= 64)
652 x = bx_q_getc(uu, bx);
678 bx_q_getn(register Uu_t* uu, register Bx_t* bx, register int n)
683 v = (v << 8) | bx_q_getc(uu, bx);
692 bx_q_gets(register Uu_t* uu, register Bx_t* bx, register char* s, size_t n)
700 if ((c = bx_q_getc(uu, bx)) == EOF)
712 bx_q_put(register Uu_t* uu, register Bx_t* bx, register int c)
720 m = (unsigned char*)((Uudata_t*)uu->meth.data)->map;
723 sfputc(uu->op, m[(c >> 18) & 0x3f]);
724 sfputc(uu->op, m[(c >> 12) & 0x3f]);
725 sfputc(uu->op, m[(c >> 6) & 0x3f]);
726 sfputc(uu->op, m[(c ) & 0x3f]);
730 sfputc(uu->op, '\n');
741 bx_q_putc(register Uu_t* uu, register Bx_t* bx, register int c)
747 bx_q_put(uu, bx, c);
748 bx_q_put(uu, bx, BX_REPEAT);
753 bx_q_put(uu, bx, bx->repeat);
762 bx_q_put(uu, bx, bx->repeat);
768 bx_q_put(uu, bx, c);
769 bx_q_put(uu, bx, 0);
772 else if (c == '\n' && (uu->flags & UU_TEXT))
774 bx_q_put(uu, bx, '\r');
776 bx_q_put(uu, bx, '\n');
781 bx_q_put(uu, bx, c);
794 bx_q_putn(register Uu_t* uu, register Bx_t* bx, register unsigned long v, int n)
798 case 4: bx_q_putc(uu, bx, (v >> 24) & 0xFF);
799 case 3: bx_q_putc(uu, bx, (v >> 16) & 0xFF);
800 case 2: bx_q_putc(uu, bx, (v >> 8) & 0xFF);
801 case 1: bx_q_putc(uu, bx, (v >> 0) & 0xFF);
811 bx_header(register Uu_t* uu)
813 register Bx_t* bx = (Bx_t*)(uu + 1);
814 Uudata_t* dp = (Uudata_t*)uu->meth.data;
823 if (uu->flags & UU_HEADER)
826 if (!(s = sfgetr(uu->ip, '\n', 0)))
828 if (uu->disc->errorf)
829 (*uu->disc->errorf)(uu, uu->disc, 2, "unknown encoding");
836 switch (c = sfgetc(uu->ip))
866 if ((c = bx_q_getc(uu, bx)) == EOF)
868 if (bx_q_gets(uu, bx, buf, c + 1) < 0)
870 if (!uu->path)
872 uu->path = strdup(buf);
873 uu->flags |= UU_FREEPATH;
875 if (bx_q_gets(uu, bx, buf, 4) < 0)
877 if (bx_q_gets(uu, bx, buf, 4) < 0)
879 bx_q_getn(uu, bx, 2);
880 bx->size = bx_q_getn(uu, bx, 4);
881 bx_q_getn(uu, bx, 4);
885 crx = bx_q_getn(uu, bx, 2);
888 if (uu->disc->errorf)
889 (*uu->disc->errorf)(uu, uu->disc, 2, "%s format header checksum mismatch", uu->meth.name);
902 sfungetc(uu->ip, c);
903 uu->flags |= BX_OLD;
909 if (!sfgetr(uu->ip, '\n', 0))
926 bx_o_decode(register Uu_t* uu, Bx_t* bx, char* buf, register size_t n)
944 sfputc(uu->op, c);
958 bx_c_decode(register Uu_t* uu, Bx_t* bx, register char* s, size_t n)
984 sfputc(uu->op, c);
987 sfputc(uu->op, c);
990 sfputc(uu->op, c);
1002 bx_decode(register Uu_t* uu)
1004 register Bx_t* bx = (Bx_t*)(uu + 1);
1013 if (uu->flags & BX_OLD)
1017 while (s = sfgetr(uu->ip, '\n', 0))
1049 while (s = sfgetr(uu->ip, '\n', 0))
1053 if ((*decode)(uu, bx, s, sfvalue(uu->ip) - 1) < 0)
1073 if ((c = bx_q_getc(uu, bx)) == EOF)
1075 sfputc(uu->op, c);
1085 bx->crc = bx_q_getn(uu, bx, 2);
1089 if (uu->disc->errorf)
1092 (*uu->disc->errorf)(uu, uu->disc, 2, "%s format checksum missing", uu->meth.name);
1094 (*uu->disc->errorf)(uu, uu->disc, 2, "%s format data checksum mismatch", uu->meth.name);
1106 bx_encode(register Uu_t* uu)
1108 register Bx_t* bx = (Bx_t*)(uu + 1);
1117 if (fstat(sffileno(uu->ip), &st))
1119 sfprintf(uu->op, "(This file must be converted with BinHex 4.0)\n:");
1120 if (!(m = (unsigned char*)uu->path))
1124 bx_q_putc(uu, bx, c);
1126 bx_q_putc(uu, bx, m[i]);
1127 bx_q_putc(uu, bx, 0);
1128 bx_q_putn(uu, bx, 0, 4);
1129 bx_q_putn(uu, bx, 0, 4);
1130 bx_q_putn(uu, bx, 0xF800, 2);
1131 bx_q_putn(uu, bx, st.st_size, 4);
1132 bx_q_putn(uu, bx, 0, 4);
1135 bx_q_putn(uu, bx, bx->crc, 2);
1137 while ((c = sfgetc(uu->ip)) != EOF)
1138 bx_q_putc(uu, bx, c);
1141 bx_q_putn(uu, bx, bx->crc, 2);
1145 bx_q_putn(uu, bx, bx->crc, 2);
1147 bx_q_putc(uu, bx, 0);
1148 sfputc(uu->op, ':');
1149 sfputc(uu->op, '\n');
1158 cat(register Uu_t* uu)
1160 return sfmove(uu->ip, uu->op, SF_UNBOUND, -1) >= 0 && sfeof(uu->ip) ? 0 : -1;
1331 register Uu_t* uu;
1339 if (!(uu = newof(0, Uu_t, 1, extra)))
1341 uu->id = lib;
1342 uu->disc = disc;
1343 uu->meth = *meth;
1344 return uu;
1352 uuclose(Uu_t* uu)
1354 if (!uu)
1356 if ((uu->flags & UU_FREEPATH) && uu->path)
1357 free(uu->path);
1358 free(uu);
1367 uuop(register Uu_t* uu, Uu_f fun)
1373 if (uu->count != SF_UNBOUND && ((p = sfseek(uu->ip, (Sfoff_t)0, SEEK_CUR)) < 0 || !(uu->ip = sfdcsubstream(NiL, uu->lp = uu->ip, p, uu->count))))
1375 if (uu->disc->errorf)
1376 (*uu->disc->errorf)(uu, uu->disc, 2, "cannot initialize substream at %I*d for %I*d bytes", sizeof(p), p, sizeof(uu->count), uu->count);
1379 p = sfseek(uu->op, (Sfoff_t)0, SEEK_CUR);
1380 n = (*fun)(uu);
1381 if (uu->lp)
1383 sfclose(uu->ip);
1384 uu->ip = uu->lp;
1385 uu->lp = 0;
1389 else if (sfsync(uu->op) || sferror(uu->op))
1392 if (uu->disc->errorf)
1393 (*uu->disc->errorf)(uu, uu->disc, 2, "write error");
1395 else if (sferror(uu->ip))
1398 if (uu->disc->errorf)
1399 (*uu->disc->errorf)(uu, uu->disc, 2, "read error");
1402 r = sfseek(uu->op, (Sfoff_t)0, SEEK_CUR) - p;
1403 if (uu->flags & UU_CLOSEOUT)
1405 uu->flags &= ~UU_CLOSEOUT;
1406 sfclose(uu->op);
1416 uuencode(register Uu_t* uu, Sfio_t* ip, Sfio_t* op, size_t n, const char* path)
1418 if (!uu->meth.encodef)
1420 if (uu->disc->errorf)
1421 (*uu->disc->errorf)(uu, uu->disc, 2, "%s format encoding not supported", uu->meth.name);
1424 if (!(uu->ip = ip) || !(uu->op = op))
1426 uu->count = n;
1427 if ((uu->flags & UU_FREEPATH) && uu->path)
1428 free(uu->path);
1429 uu->path = (char*)path;
1430 uu->flags = uu->disc->flags;
1431 return uuop(uu, uu->meth.encodef);
1439 uudecode(register Uu_t* uu, Sfio_t* ip, Sfio_t* op, size_t n, const char* path)
1449 if (!uu->meth.decodef)
1451 if (uu->disc->errorf)
1452 (*uu->disc->errorf)(uu, uu->disc, 2, "%s format decoding not supported", uu->meth.name);
1455 if (!(uu->ip = ip))
1457 uu->op = op;
1458 uu->count = n;
1459 if ((uu->flags & UU_FREEPATH) && uu->path)
1460 free(uu->path);
1461 uu->path = (char*)path;
1462 uu->flags = uu->disc->flags;
1463 data = (Uudata_t*)uu->meth.data;
1464 if (((uu->flags & UU_HEADER) || data && (data->flags & UU_HEADERMUST)) && uu->meth.headerf)
1466 if ((*uu->meth.headerf)(uu))
1473 if (data && (data->flags & UU_DEFAULT) && (c = sfgetc(uu->ip)) != EOF)
1475 sfungetc(uu->ip, c);
1484 uu->meth = *mp;
1485 if (uu->disc->errorf)
1486 (*uu->disc->errorf)(uu, uu->disc, 1, "assuming %s encoding", mp->name);
1490 if (!uu->op)
1492 if (!uu->path && headerpath)
1494 if (uu->disc->errorf)
1495 (*uu->disc->errorf)(uu, uu->disc, 2, "%s format header has no output file name", uu->meth.name);
1498 if (!uu->path || !*uu->path || streq(uu->path, "-") || streq(uu->path, "/dev/stdout"))
1499 uu->op = sfstdout;
1502 if (headerpath && (uu->flags & UU_LOCAL))
1504 for (s = uu->path; *s; s++)
1507 s = uu->path;
1511 if (!(uu->op = sfopen(NiL, uu->path, "w")))
1513 if (uu->disc->errorf)
1514 (*uu->disc->errorf)(uu, uu->disc, 2, "%s: cannot write", uu->path);
1517 uu->flags |= UU_CLOSEOUT;
1520 return uuop(uu, uu->meth.decodef);