Changeset 4c0fe8


Ignore:
Timestamp:
09/20/09 17:11:04 (3 years ago)
Author:
J. A. Bezemer <J.A.Bezemer@…>
Branches:
master
Children:
05a23a
Parents:
fb1747
git-author:
J. A. Bezemer <J.A.Bezemer@…> (09/20/09 17:11:04)
git-committer:
Erik Ekman <erik@…> (02/04/12 20:34:03)
Message:

add txt read/write #75

Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/read.c

    ra5031e r4c0fe8  
    127127 
    128128int 
     129readtxtbin(char *packet, char **src, size_t srcremain, char *dst, size_t dstremain) 
     130{ 
     131        unsigned char *uc; 
     132        int tocopy; 
     133        int dstused = 0; 
     134 
     135        while (srcremain > 0) 
     136        { 
     137                uc = (unsigned char*) (*src); 
     138                tocopy = *uc; 
     139                (*src)++; 
     140                srcremain--; 
     141 
     142                if (tocopy > srcremain) 
     143                        return 0;       /* illegal, better have nothing */ 
     144                if (tocopy > dstremain) 
     145                        return 0;       /* doesn't fit, better have nothing */ 
     146 
     147                memcpy(dst, *src, tocopy); 
     148                dst += tocopy; 
     149                (*src) += tocopy; 
     150                srcremain -= tocopy; 
     151                dstremain -= tocopy; 
     152                dstused += tocopy; 
     153        } 
     154        return dstused; 
     155} 
     156 
     157int 
    129158putname(char **buf, size_t buflen, const char *host) 
    130159{ 
     
    213242} 
    214243 
     244int 
     245puttxtbin(char **buf, size_t bufremain, char *from, size_t fromremain) 
     246{ 
     247        unsigned char uc; 
     248        unsigned char *ucp = &uc; 
     249        char *cp = (char *) ucp; 
     250        int tocopy; 
     251        int bufused = 0; 
     252 
     253        while (fromremain > 0) 
     254        { 
     255                tocopy = fromremain; 
     256                if (tocopy > 252) 
     257                        tocopy = 252;   /* allow off-by-1s in caches etc */ 
     258                if (tocopy + 1 > bufremain) 
     259                        return -1;      /* doesn't fit, better have nothing */ 
     260 
     261                uc = tocopy; 
     262                **buf = *cp; 
     263                (*buf)++; 
     264                bufremain--; 
     265                bufused++; 
     266 
     267                memcpy(*buf, from, tocopy); 
     268                (*buf) += tocopy; 
     269                from += tocopy; 
     270                bufremain -= tocopy; 
     271                fromremain -= tocopy; 
     272                bufused += tocopy; 
     273        } 
     274        return bufused; 
     275} 
  • src/read.h

    ra5031e r4c0fe8  
    2222int readlong(char *, char **, uint32_t *); 
    2323int readdata(char *, char **, char *, size_t); 
     24int readtxtbin(char *, char **, size_t, char *, size_t); 
    2425 
    2526int putname(char **, size_t, const char *); 
     
    2829int putlong(char **, uint32_t); 
    2930int putdata(char **, char *, size_t); 
     31int puttxtbin(char **, size_t, char *, size_t); 
    3032 
    3133#endif 
Note: See TracChangeset for help on using the changeset viewer.