home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Format / asn / ut_f.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  4.5 KB  |  289 lines

  1. /* ut_f.c: Common Function Routines (used by f_*.c files) */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Format/asn/RCS/ut_f.c,v 6.0 1991/12/18 20:16:07 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Format/asn/RCS/ut_f.c,v 6.0 1991/12/18 20:16:07 jpo Rel $
  9.  *
  10.  * $Log: ut_f.c,v $
  11.  * Revision 6.0  1991/12/18  20:16:07  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "head.h"
  19. #include "asn.h"
  20.  
  21.  
  22. extern int    CRline;
  23.  
  24.  
  25. typedef struct decoded_struct {
  26.     struct qbuf        *line;
  27.     struct decoded_struct    *next;
  28. } DECODED;
  29.  
  30.  
  31. #define NULLDECODED    ((DECODED *)0)
  32. #define STR2QB(s)    str2qb(s, strlen(s), 1)
  33.  
  34.  
  35.  
  36.  
  37. /* ------------------------  Start Routines --------------------------------- */
  38.  
  39.  
  40.  
  41.  
  42. /* --- *** ---
  43. writes an asn1 decoded body part info into a common struct called ASNBODY
  44. --- *** --- */
  45.  
  46.  
  47.  
  48. struct2body (dstruct, body)
  49. DECODED        *dstruct;
  50. ASNBODY        **body;
  51. {
  52.     DECODED    *dp;
  53.     char    *cp, *np;
  54.  
  55.     PP_TRACE (("struct2body()"));
  56.  
  57.     for (dp = dstruct; dp; dp = dp -> next) {
  58.         if ((cp = qb2str (dp -> line)) == NULLCP) {
  59.             PP_LOG (LLOG_EXCEPTIONS,
  60.                 ("Unable to decode - qb2str error"));
  61.             exit (1);
  62.         }
  63.  
  64.         rn2n (cp, strlen(cp), &np);
  65.         free (cp);
  66.         asnbody_add (body, np, strlen(np));
  67.     }
  68. }
  69.  
  70.  
  71.  
  72.  
  73. body2struct (asnbody, dstruct)
  74. ASNBODY        *asnbody;
  75. DECODED        **dstruct;
  76. {
  77.     ASNBODY    *body;
  78.     DECODED    *new, *bak;
  79.     char    *np;
  80.  
  81.     PP_TRACE (("body2struct()"));
  82.  
  83.  
  84.     for (bak=NULLDECODED, body=asnbody; body; body=body->next) {
  85.  
  86.         n2rn (body->line, body->length, &np); 
  87.  
  88.         new = (DECODED *) smalloc (sizeof *new);
  89.         bzero (new, sizeof (*new));
  90.  
  91.         new -> line = str2qb(NULLCP, 0, 1);
  92.         new -> line -> qb_forw -> qb_data = np;
  93.         new -> line -> qb_forw -> qb_len  = strlen(np);
  94.  
  95.  
  96.         /* -- free body lines now to reduce space -- */
  97.         if (body->line) free (body->line);
  98.         body -> line = NULLCP;
  99.         body -> length = 0;
  100.  
  101.         if (bak)
  102.             bak -> next = new;
  103.         else
  104.             *dstruct = new;
  105.         bak = new; 
  106.     }
  107.  
  108.     asnbody_free (asnbody);
  109. }
  110.  
  111.  
  112.  
  113.  
  114. qbuf2body (qstruct, body)
  115. struct qbuf    *qstruct;
  116. ASNBODY        **body;
  117. {
  118.     struct qbuf    *qp;
  119.     char        *np;
  120.  
  121.     PP_TRACE (("qbuf2body()"));
  122.     
  123.     for (qp = qstruct -> qb_forw; qp != qstruct; qp = qp -> qb_forw) {
  124.  
  125.         if (qp -> qb_len == 0)
  126.             asnbody_add (body, strdup(""), 0);
  127.         else {
  128.             rn2n (qp -> qb_data, qp -> qb_len, &np);
  129.             asnbody_add (body, np, strlen(np));
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135.  
  136.  
  137. body2qbuf (asnbody, qb)
  138. ASNBODY        *asnbody;
  139. struct qbuf    **qb;
  140. {
  141.     ASNBODY        *body;
  142.     struct qbuf    *new, *bak;
  143.     char        *np;
  144.  
  145.     PP_TRACE (("body2qbuf()"));
  146.  
  147.     for (bak=NULL, body=asnbody; body; body=body->next) {
  148.  
  149.         n2rn (body->line, body->length, &np);
  150.  
  151.         if (bak == NULL) {
  152.             new = str2qb(NULLCP, 0, 1);
  153.             new -> qb_forw -> qb_data = np;
  154.             new -> qb_forw -> qb_len  = strlen (np);
  155.             *qb = new;
  156.             bak = new -> qb_forw;
  157.         }
  158.         else {
  159.             new = str2qb(NULLCP, 0, 0);
  160.             new -> qb_data = np;
  161.             new -> qb_len  = strlen (np);
  162.             bak -> qb_forw = new;
  163.             new -> qb_back = bak;
  164.             bak = new; 
  165.         }    
  166.  
  167.         if (body -> line)  free (body -> line);
  168.         body -> line = NULLCP;
  169.         body -> length = NULL;
  170.     }
  171.  
  172.     bak -> qb_forw = *qb;
  173.  
  174.     asnbody_free (asnbody);
  175. }
  176.  
  177.  
  178.  
  179.  
  180. pe_done(pe, msg)    /* -- writes to oper log -- */
  181. PE    pe;
  182. char    *msg;
  183. {
  184.  
  185.     PP_TRACE (("pe_done(%x)", pe));
  186.  
  187.     if (pe->pe_errno)
  188.         PP_OPER(NULLCP,
  189.              ("%s: [%s] %s", msg, PY_pepy, pe_error(pe->pe_errno)));
  190.     else
  191.         PP_LOG(LLOG_EXCEPTIONS,
  192.              ("pe_done failure: [%s]", PY_pepy));
  193.  
  194.     exit (1);
  195. }
  196.  
  197.  
  198.  
  199.  
  200. /* -----------------------  Static  Routines  ------------------------------- */
  201.  
  202.  
  203.  
  204.  
  205. static rn2n (oldp, len, newpp)  /* convert from \r\n to \n */
  206. char    *oldp;
  207. int    len;
  208. char    **newpp;
  209. {
  210.     char    *op, *buf, *start;
  211.     char    lastr = NULL, lastn = TRUE;
  212.     int    n;
  213.  
  214.  
  215.     PP_TRACE (("rn2n(%d)", len));
  216.  
  217.     n = len * 2;
  218.     start = buf = smalloc (n);
  219.     bzero (buf, n);
  220.  
  221.     for (op=oldp, n=len; n > 0; n--, op++) {
  222.         switch (*op) {
  223.         case '\r':
  224.             lastr = *op;
  225.             break;
  226.         case '\n':
  227.             *buf++ = *op;
  228.             lastr = lastn = NULL;
  229.             break;
  230.         default:
  231.             if (lastr) {
  232.                 *buf++ = lastr;
  233.                 lastr = NULL;
  234.             }
  235.  
  236.             *buf++ = *op;
  237.             break;
  238.         }
  239.     }        
  240.  
  241.     if (lastn && CRline)
  242.         *buf++ = '\n';
  243.  
  244.     *buf++ = '\0';
  245.     *newpp = realloc (start, buf - start);
  246. }
  247.  
  248.  
  249.  
  250.  
  251. static n2rn (oldp, len, newpp)    /* convert from \n -> \r\n */
  252. char    *oldp;
  253. int    len;
  254. char    **newpp;
  255. {
  256.     char    *op, *buf, *start;
  257.     char    lastr = NULL, lastn = TRUE;
  258.     int    n;
  259.  
  260.     PP_TRACE (("n2rn(%d)", len));
  261.  
  262.  
  263.     n = len * 2;
  264.     start = buf = smalloc (n);
  265.     bzero (buf, n);
  266.  
  267.  
  268.     for (op=oldp, n=len; n > 0; n--, op++) {
  269.         switch (*op) {
  270.         case '\n':
  271.             *buf++ = '\r';
  272.             *buf++ = '\n';
  273.             lastn = 0;
  274.             break;
  275.         default:
  276.             *buf++ = *op;
  277.             break;
  278.         }
  279.     }
  280.  
  281.     if (lastn && CRline) {
  282.         *buf++ = '\r';
  283.         *buf++ = '\n';
  284.     }
  285.  
  286.     *buf++ = '\0';
  287.     *newpp = realloc (start, buf - start);
  288. }
  289.