C-从命令行获得128位整数



我正在尝试施放一个unsigned long long键来执行微小的加密算法算法。

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv){
    unsigned int key[4] = { 0 };
    *key = strtoll(argv[1], NULL, 10);
    printf("%s key = %llun", argv[0], key);
    return 0;
}

这是我的输入:

./a.out 9223372036854775700

这是输出:

./a.out key = 140723741574976

所以我在argv[1]中传递了128位键。不应该在内存中正确地施放在unsigned int数组中吗?

所以,我试图弄清楚为什么这是我程序的输出。这与endians有关系吗?

long long仅指定至少包含64位。您可能会在十六进制并手动解析字节阵列

时将其分析时,您可能会更好地传递钥匙。

退后一步,看看您要实现的内容。微小的加密算法在128位整数上不起作用,而是在128位键上。钥匙由四个32位未签名的整数组成。

您实际需要的是一种解析小数(或十六进制或其他基部(128位无符号整数从字符串到四个32位未签名的整数元素的方法。

我建议编写一个乘add函数,该功能采用Quad-32位值,将其乘以32位常数,并添加了另一个32位常数:

#include <stdint.h>
uint32_t muladd128(uint32_t quad[4], const uint32_t mul, const uint32_t add)
{
    uint64_t  temp = 0;
    temp = (uint64_t)quad[3] * (uint64_t)mul + add;
    quad[3] = temp;
    temp = (uint64_t)quad[2] * (uint64_t)mul + (temp >> 32);
    quad[2] = temp;
    temp = (uint64_t)quad[1] * (uint64_t)mul + (temp >> 32);
    quad[1] = temp;
    temp = (uint64_t)quad[0] * (uint64_t)mul + (temp >> 32);
    quad[0] = temp;
    return temp >> 32;
}

上面使用最重要的第一个单词顺序。如果结果溢出,它将返回非零;实际上,它返回32位溢出本身。

这样,解析描述二进制,八分,十进制或十六进制的非负128位整数的字符串非常容易:

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
static void clear128(uint32_t quad[4])
{
    quad[0] = quad[1] = quad[2] = quad[3] = 0;
}
/* muladd128() */
static const char *parse128(uint32_t quad[4], const char *from)
{
    if (!from) {
        errno = EINVAL;
        return NULL;
    }
    while (*from == 't' || *from == 'n' || *from == 'v' ||
           *from == 'f' || *from == 'r' || *from == ' ')
        from++;
    if (from[0] == '0' && (from[1] == 'x' || from[1] == 'X') &&
        ((from[2] >= '0' && from[2] <= '9') ||
         (from[2] >= 'A' && from[2] <= 'F') ||
         (from[2] >= 'a' && from[2] <= 'f'))) {
        /* Hexadecimal */
        from += 2;
        clear128(quad);
        while (1)
            if (*from >= '0' && *from <= '9') {
                if (muladd128(quad, 16, *from - '0')) {
                    errno = ERANGE;
                    return NULL;
                }
                from++;
            } else
            if (*from >= 'A' && *from <= 'F') {
                if (muladd128(quad, 16, *from - 'A' + 10)) {
                    errno = ERANGE;
                    return NULL;
                }
                from++;
            } else
            if (*from >= 'a' && *from <= 'f') {
                if (muladd128(quad, 16, *from - 'a' + 10)) {
                    errno = ERANGE;
                    return NULL;
                }
                from++;
            } else
                return from;
    }
    if (from[0] == '0' && (from[1] == 'b' || from[1] == 'B') &&
        (from[2] >= '0' && from[2] <= '1')) {
        /* Binary */
        from += 2;
        clear128(quad);
        while (1)
            if (*from >= '0' && *from <= '1') {
                if (muladd128(quad, 2, *from - '0')) {
                    errno = ERANGE;
                    return NULL;
                }
                from++;
            } else
                return from;
    }
    if (from[0] == '0' &&
        (from[1] >= '0' && from[1] <= '7')) {
        /* Octal */
        from += 1;
        clear128(quad);
        while (1)
            if (*from >= '0' && *from <= '7') {
                if (muladd128(quad, 8, *from - '0')) {
                    errno = ERANGE;
                    return NULL;
                }
                from++;
            } else
                return from;
    }
    if (from[0] >= '0' && from[0] <= '9') {
        /* Decimal */
        clear128(quad);
        while (1)
            if (*from >= '0' && *from <= '9') {
                if (muladd128(quad, 10, *from - '0')) {
                    errno = ERANGE;
                    return NULL;
                }
                from++;
            } else
                return from;
    }
    /* Not a recognized number. */
    errno = EINVAL;
    return NULL;
}
int main(int argc, char *argv[])
{
    uint32_t key[4];
    int      arg;
    for (arg = 1; arg < argc; arg++) {
        const char *end = parse128(key, argv[arg]);
        if (end) {
            if (*end != '')
                printf("%s: 0x%08x%08x%08x%08x (+ "%s")n", argv[arg], key[0], key[1], key[2], key[3], end);
            else
                printf("%s: 0x%08x%08x%08x%08xn", argv[arg], key[0], key[1], key[2], key[3]);
            fflush(stdout);
        } else {
            switch (errno) {
            case ERANGE:
                fprintf(stderr, "%s: Too large.n", argv[arg]);
                break;
            case EINVAL:
                fprintf(stderr, "%s: Not a nonnegative integer in binary, octal, decimal, or hexadecimal notation.n", argv[arg]);
                break;
            default:
                fprintf(stderr, "%s: %s.n", argv[arg], strerror(errno));
                break;
            }
        }
    }
    return EXIT_SUCCESS;
}

添加对base64和base85的支持非常简单,有时会使用。或实际上对于任何小于2 32 的基数。

,如果您考虑上述内容,这全都取决于您需要的东西。

代码正在尝试打印数组key[0]的地址而不是其值。这不是一个末日的问题。启用所有编译器警告以节省时间。

*key = strtoll(argv[1], NULL, 10);试图将long long(至少64位(保存到unsigned int中,这可能只有32。

字符串" 9223720368547775700"代表一个63位。

首先尝试使用至少64位编号的unsigned long long

int main(int argc, char** argv){
    // unsigned int key[4] = { 0 };
    unsigned long long  key = strtoull(argv[1], NULL, 10);
    printf("%s key = %llun", argv[0], key);
    return 0;
}

C不指定对128位整数的支持。可以编写用户代码来应对。@c_elegans使用十六进制文本的想法很好。

int可能具有各种尺寸,最好使用

#include <stdint.h>
// unsigned int key[4];
uint32_t key[4];

示例代码想法

#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
  uint16_t u[8];
} my_uint128_t;
my_uint128_t strtomy_uint128(const char *s, char **endptr, int base) {
  my_uint128_t y = {0};
  while (isalnum((unsigned char ) *s)) {
    char *endptr;
    uint32_t sum = (uint32_t) strtoul((char[2]) {*s, ''}, &endptr, base);
    if (*endptr) {
      break;
    }
    for (int i = 0; i < 8; i++) {
      sum +=  y.u[i] * (uint32_t) base;
      y.u[i] = (uint16_t) sum;
      sum >>= 16;
    }
    if (sum) {
      errno = ERANGE;
      for (int i = 0; i < 8; i++) {
        y.u[i] = UINT16_MAX;
      }
    }
    s++;
  }
  if (endptr) {
    *endptr = (char *) s;
  }
  return y;
}
void uint128_dump(my_uint128_t x) {
  for (int i = 8; i > 0; ) {
    i--;
    printf("%04" PRIX16 "%c", x.u[i], i ? ' ' : 'n');
  }
}
int main(void) {
  my_uint128_t a = strtomy_uint128("9223372036854775700", 0, 10);
  uint128_dump(a);
}

输出

0000 0000 0000 0000 7FFF FFFF FFFF FF94

为什么不手动呢?获取__int128类型变量,浏览输入的每个数字,然后将其插入您的变量:

int main(int argc, char** argv){
    __int128 key = 0;
    int i;
    for (i=0; i<strlen(argv[1]); i++){
        key *= 10; // "shift" current value to make space for adding one more decimal
        key += argv[1][i] - '0'; // convert ascii character to number 
    }
    printf("%s key = %llun", argv[0], key);
    return 0;
}

请注意,如果argv[1]太长,则钥匙将丢弃其第一个数字,而不是最后一个数字。因此,也许这也是您的喜好

也需要照顾的。

最新更新