[BACK]Return to sshbuf-misc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sshbuf-misc.c, Revision 1.1

1.1     ! djm         1: /*     $OpenBSD$       */
        !             2: /*
        !             3:  * Copyright (c) 2011 Damien Miller
        !             4:  *
        !             5:  * Permission to use, copy, modify, and distribute this software for any
        !             6:  * purpose with or without fee is hereby granted, provided that the above
        !             7:  * copyright notice and this permission notice appear in all copies.
        !             8:  *
        !             9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !            10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            16:  */
        !            17:
        !            18: #include <sys/types.h>
        !            19: #include <sys/socket.h>
        !            20: #include <netinet/in.h>
        !            21: #include <errno.h>
        !            22: #include <stdlib.h>
        !            23: #include <stdio.h>
        !            24: #include <limits.h>
        !            25: #include <string.h>
        !            26: #include <resolv.h>
        !            27: #include <ctype.h>
        !            28:
        !            29: #include "ssherr.h"
        !            30: #define SSHBUF_INTERNAL
        !            31: #include "sshbuf.h"
        !            32:
        !            33: void
        !            34: sshbuf_dump(struct sshbuf *buf, FILE *f)
        !            35: {
        !            36:        const u_char *p = sshbuf_ptr(buf);
        !            37:        size_t i, j, len = sshbuf_len(buf);
        !            38:
        !            39:        fprintf(f, "buffer %p len = %zu\n", buf, len);
        !            40:        for (i = 0; i < len; i += 16) {
        !            41:                fprintf(f, "%.4zd: ", i);
        !            42:                for (j = i; j < i + 16; j++) {
        !            43:                        if (j < len)
        !            44:                                fprintf(f, "%02x ", p[j]);
        !            45:                        else
        !            46:                                fprintf(f, "   ");
        !            47:                }
        !            48:                fprintf(f, " ");
        !            49:                for (j = i; j < i + 16; j++) {
        !            50:                        if (j < len) {
        !            51:                                if  (isascii(p[j]) && isprint(p[j]))
        !            52:                                        fprintf(f, "%c", p[j]);
        !            53:                                else
        !            54:                                        fprintf(f, ".");
        !            55:                        }
        !            56:                }
        !            57:                fprintf(f, "\n");
        !            58:        }
        !            59: }
        !            60:
        !            61: char *
        !            62: sshbuf_dtob16(struct sshbuf *buf)
        !            63: {
        !            64:        size_t i, j, len = sshbuf_len(buf);
        !            65:        const u_char *p = sshbuf_ptr(buf);
        !            66:        char *ret;
        !            67:        const char hex[] = "0123456789abcdef";
        !            68:
        !            69:        if (len == 0)
        !            70:                return strdup("");
        !            71:        if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
        !            72:                return NULL;
        !            73:        for (i = j = 0; i < len; i++) {
        !            74:                ret[j++] = hex[(p[i] >> 4) & 0xf];
        !            75:                ret[j++] = hex[p[i] & 0xf];
        !            76:        }
        !            77:        ret[j] = '\0';
        !            78:        return ret;
        !            79: }
        !            80:
        !            81: char *
        !            82: sshbuf_dtob64(struct sshbuf *buf)
        !            83: {
        !            84:        size_t len = sshbuf_len(buf), plen;
        !            85:        const u_char *p = sshbuf_ptr(buf);
        !            86:        char *ret;
        !            87:        int r;
        !            88:
        !            89:        if (len == 0)
        !            90:                return strdup("");
        !            91:        plen = ((len + 2) / 3) * 4 + 1;
        !            92:        if (SIZE_MAX / 2 <= len || (ret = malloc(plen)) == NULL)
        !            93:                return NULL;
        !            94:        if ((r = b64_ntop(p, len, ret, plen)) == -1) {
        !            95:                bzero(ret, plen);
        !            96:                free(ret);
        !            97:                return NULL;
        !            98:        }
        !            99:        return ret;
        !           100: }
        !           101:
        !           102: int
        !           103: sshbuf_b64tod(struct sshbuf *buf, const char *b64)
        !           104: {
        !           105:        size_t plen = strlen(b64);
        !           106:        int nlen, r;
        !           107:        u_char *p;
        !           108:
        !           109:        if (plen == 0)
        !           110:                return 0;
        !           111:        if ((p = malloc(plen)) == NULL)
        !           112:                return SSH_ERR_ALLOC_FAIL;
        !           113:        if ((nlen = b64_pton(b64, p, plen)) < 0) {
        !           114:                bzero(p, plen);
        !           115:                free(p);
        !           116:                return SSH_ERR_INVALID_FORMAT;
        !           117:        }
        !           118:        if ((r = sshbuf_put(buf, p, nlen)) < 0) {
        !           119:                bzero(p, plen);
        !           120:                free(p);
        !           121:                return r;
        !           122:        }
        !           123:        bzero(p, plen);
        !           124:        free(p);
        !           125:        return 0;
        !           126: }
        !           127: