FIDO-Raw

 view release on metacpan or  search on metacpan

deps/libcbor/src/cbor/streaming.c  view on Meta::CPAN

/*
 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include "streaming.h"
#include "internal/loaders.h"

bool static _cbor_claim_bytes(size_t required, size_t provided,
                              struct cbor_decoder_result *result) {
  if (required > (provided - result->read)) {
    /* We need to keep all the metadata if parsing is to be resumed */
    result->read = 0;
    result->status = CBOR_DECODER_NEDATA;
    result->required = required;
    return false;
  } else {
    result->read += required;
    result->required = 0;
    return true;
  }
}

struct cbor_decoder_result cbor_stream_decode(
    cbor_data source, size_t source_size,
    const struct cbor_callbacks *callbacks, void *context) {
  /* If we have no data, we cannot read even the MTB */
  if (source_size < 1) {
    return (struct cbor_decoder_result){0, CBOR_DECODER_EBUFFER};
  }

  /* If we have a byte, assume it's the MTB */
  struct cbor_decoder_result result = {1, CBOR_DECODER_FINISHED};

  switch (*source) {
    case 0x00: /* Fallthrough */
    case 0x01: /* Fallthrough */
    case 0x02: /* Fallthrough */
    case 0x03: /* Fallthrough */
    case 0x04: /* Fallthrough */
    case 0x05: /* Fallthrough */
    case 0x06: /* Fallthrough */
    case 0x07: /* Fallthrough */
    case 0x08: /* Fallthrough */
    case 0x09: /* Fallthrough */
    case 0x0A: /* Fallthrough */
    case 0x0B: /* Fallthrough */
    case 0x0C: /* Fallthrough */
    case 0x0D: /* Fallthrough */
    case 0x0E: /* Fallthrough */
    case 0x0F: /* Fallthrough */
    case 0x10: /* Fallthrough */
    case 0x11: /* Fallthrough */
    case 0x12: /* Fallthrough */
    case 0x13: /* Fallthrough */
    case 0x14: /* Fallthrough */
    case 0x15: /* Fallthrough */
    case 0x16: /* Fallthrough */
    case 0x17:
      /* Embedded one byte unsigned integer */
      {
        callbacks->uint8(context, _cbor_load_uint8(source));
        return result;
      }
    case 0x18:
      /* One byte unsigned integer */
      {
        if (_cbor_claim_bytes(1, source_size, &result)) {
          callbacks->uint8(context, _cbor_load_uint8(source + 1));
        }
        return result;
      }
    case 0x19:
      /* Two bytes unsigned integer */
      {
        if (_cbor_claim_bytes(2, source_size, &result)) {
          callbacks->uint16(context, _cbor_load_uint16(source + 1));
        }



( run in 1.014 second using v1.01-cache-2.11-cpan-0d23b851a93 )