Sha256: bd9cf3ffecb01bda8383061f9f6c20ead8ab2307fe104686ae905b2925a32f2d

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
/* vi: set expandtab shiftwidth=4 tabstop=4: */

/**
 * \file modp_b2.c
 * <PRE>
 * MODP_B2 - Ascii Binary string encode/decode
 * https://github.com/client9/stringencoders
 *
 * Copyright &copy; 2005-2016  Nick Galbreath
 * All rights reserved.
 *
 * Released under MIT license. See LICENSE for details.
 *
 */
#include "modp_b2.h"
#include "config.h"
#include "modp_b2_data.h"
#include "modp_stdint.h"
#include <string.h>

size_t modp_b2_encode(char* dest, const char* str, size_t len)
{
    const uint8_t* orig = (const uint8_t*)str;
#if 0
    /* THIS IS A STANDARD VERSION */
    static const uint8_t gsBinaryChars[] = "01";
    int i,j;
    for (i = 0; i < len; ++i) {
        for (j = 0; j <= 7; ++j) {
            *dest++ = gsBinaryChars[(orig[i] >> (7-j)) & 1];
        }
    }
#else
    /* THIS IS 10X FASTER */
    size_t i;
    for (i = 0; i < len; ++i) {
        memcpy((void*)dest, modp_b2_encodemap[orig[i]], (size_t)8);
        dest += 8;
    }
#endif
    *dest = '\0';
    return len * 8;
}

size_t modp_b2_decode(char* dest, const char* str, size_t len)
{
    char d;
    size_t i;
    int j;
    const size_t buckets = len / 8;
    const size_t leftover = len % 8;
    if (leftover != 0) {
        return (size_t)-1;
    }

    for (i = 0; i < buckets; ++i) {
        d = 0;
        for (j = 0; j <= 7; ++j) {
            char c = *str++;
            if (c == '1') {
                d ^= (char)(1 << (7 - j));
            }
        }
        *dest++ = d;
    }

    return buckets;
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ffi-hydrogen-0.1.5 vendor/stringencoders/src/modp_b2.c
ffi-hydrogen-0.1.4 vendor/stringencoders/src/modp_b2.c
ffi-hydrogen-0.1.3 vendor/stringencoders/src/modp_b2.c
ffi-hydrogen-0.1.2 vendor/stringencoders/src/modp_b2.c
ffi-hydrogen-0.1.1 vendor/stringencoders/src/modp_b2.c
ffi-hydrogen-0.1.0 vendor/stringencoders/src/modp_b2.c