首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ascii85解码算法在某些情况下失效

ascii85解码算法在某些情况下失效
EN

Stack Overflow用户
提问于 2014-12-15 14:33:12
回答 1查看 1.1K关注 0票数 1

我正试图在Ascii85中开发一个c++解码器,以便从adobe文件(*ai)中解析位图文件。

我在java 这里中找到了一个算法,并尝试用c++重写它。

问题是,我有一些情况,我的编码文本没有被正确解码。例如,如果字符"a“是字符串中的第7个也是最后一个字符(在编码之前),则作为结果,它被解码为"", which is the previous character in the ascii table. It is weird because I have tried to make the calculations of the algorithm manually, and I get "”。我想知道算法中是否有错误,或者它是否是adobe ascii85解码的正确算法。

这是我的代码:

代码语言:javascript
复制
#include <QCoreApplication>
#include <stdio.h>
#include <string.h>
#include <QDebug>

// returns 1 when there are no more bytes to decode
// 0 otherwise
int decodeBlock(char *input, unsigned char *output, unsigned int inputSize) {
    qDebug() << input << output << inputSize;
    if (inputSize > 0) {
        unsigned int bytesToDecode = (inputSize < 5)? inputSize : 5;

        unsigned int x[5] = { 0 };
        unsigned int i;
        for (i = 0; i < bytesToDecode; i++) {
            x[i] = input[i] - 33;
            qDebug() << x[i] << ", i: " << i;
        }

        if (i > 0)
            i--;

        unsigned int value =
                x[0] * 85 * 85 * 85 * 85 +
                x[1] * 85 * 85 * 85 +
                x[2] * 85 * 85 +
                x[3] * 85 +
                x[4];

        for (unsigned int j = 0; j < i; j++) {
            int shift = 8 * (3 - j); // 8 * 3, 8 * 2, 8 * 1, 8 * 0
            unsigned char byte = (unsigned char)((value >> shift) & 0xff);
            printf("byte: %c, %d\n", byte, byte);
            *output = byte;
            output++;
        }
    }

    return inputSize <= 5;
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    char x__input[] = "<~FE1f+@;K?~>";

    unsigned char x__output[128] = { 0 };

    char *input = x__input + 2;
    unsigned int inputSize = (unsigned int)strlen(input);
    inputSize -= 2;

    unsigned char *output = x__output;

    printf("decoding %s\n", input);
    for (unsigned int i = 0; i < inputSize; i += 5, input += 5, output += 4)
        if(decodeBlock(input, output, inputSize - i))
            break;

    printf("Output is: %s\n", x__output);

    return a.exec();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-15 14:44:23

当inputSize不是5的倍数时会发生什么??

代码语言:javascript
复制
    unsigned int bytesToDecode = (inputSize < 5)? inputSize : 5;

假设bytesToDecode为5,而有些字节的值未知

所以,当你的角色是第7位的最后一位时,上述条件是正确的。

如果输入不是5的倍数,则必须加上值"u“。有关编码/解码过程的更多细节,请查看Wikipedia页面,在该页面中解释得很清楚:

Ascii85

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27486372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档