Annotation of embedaddon/libpdel/ppp/ppp_util.c, revision 1.1.1.1

1.1       misho       1: 
                      2: /*
                      3:  * Copyright (c) 2001-2002 Packet Design, LLC.
                      4:  * All rights reserved.
                      5:  * 
                      6:  * Subject to the following obligations and disclaimer of warranty,
                      7:  * use and redistribution of this software, in source or object code
                      8:  * forms, with or without modifications are expressly permitted by
                      9:  * Packet Design; provided, however, that:
                     10:  * 
                     11:  *    (i)  Any and all reproductions of the source or object code
                     12:  *         must include the copyright notice above and the following
                     13:  *         disclaimer of warranties; and
                     14:  *    (ii) No rights are granted, in any manner or form, to use
                     15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
                     16:  *         on advertising, endorsements, or otherwise except as such
                     17:  *         appears in the above copyright notice or in the software.
                     18:  * 
                     19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
                     20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
                     21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
                     22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
                     23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
                     24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
                     25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
                     26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
                     27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
                     28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
                     29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
                     30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
                     31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
                     32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
                     33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
                     35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
                     36:  * THE POSSIBILITY OF SUCH DAMAGE.
                     37:  *
                     38:  * Author: Archie Cobbs <archie@freebsd.org>
                     39:  */
                     40: 
                     41: #include "ppp/ppp_defs.h"
                     42: #include "ppp/ppp_util.h"
                     43: 
                     44: #define PATH_RANDOM            "/dev/urandom"
                     45: 
                     46: /*
                     47:  * Decode ASCII message
                     48:  */
                     49: void
                     50: ppp_util_ascify(char *buf, size_t bsiz, const u_char *data, size_t len)
                     51: {
                     52:        char *bp = buf;
                     53:        int i;
                     54: 
                     55:        for (bp = buf, i = 0; i < len; i++) {
                     56:                const char ch = (char)data[i];
                     57: 
                     58:                if (bsiz < 3)
                     59:                        break;
                     60:                switch (ch) {
                     61:                case '\t':
                     62:                        *bp++ = '\\';
                     63:                        *bp++ = 't';
                     64:                        bsiz -= 2;
                     65:                        break;
                     66:                case '\n':
                     67:                        *bp++ = '\\';
                     68:                        *bp++ = 'n';
                     69:                        bsiz -= 2;
                     70:                        break;
                     71:                case '\r':
                     72:                        *bp++ = '\\';
                     73:                        *bp++ = 'r';
                     74:                        bsiz -= 2;
                     75:                        break;
                     76:                default:
                     77:                        if (isprint(ch & 0x7f)) {
                     78:                                *bp++ = ch;
                     79:                                bsiz--;
                     80:                        } else {
                     81:                                *bp++ = '^';
                     82:                                *bp++ = '@' + (ch & 0x1f);
                     83:                                bsiz -= 2;
                     84:                        }
                     85:                        break;
                     86:                }
                     87:        }
                     88:        *bp = '\0';
                     89: }
                     90: 
                     91: /*
                     92:  * Generate random bytes
                     93:  */
                     94: int
                     95: ppp_util_random(void *buf, size_t len)
                     96: {
                     97:        int rlen;
                     98:        int fd;
                     99: 
                    100:        if ((fd = open(PATH_RANDOM, O_RDONLY)) == -1)
                    101:                return (-1);
                    102:        rlen = read(fd, buf, len);
                    103:        (void)close(fd);
                    104:        if (rlen == -1)
                    105:                return (-1);
                    106:        if (rlen != len) {
                    107:                errno = ENXIO;
                    108:                return (-1);
                    109:        }
                    110:        return (0);
                    111: }
                    112: 
                    113: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>