summaryrefslogtreecommitdiffstats
path: root/code/jasmine/buffer.c
blob: 0567026bbcc35b880dd72edbd258df43d280444a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .e
/*##############################################################################
# Copyright (c) 2017 ZTE Coreporation and others.
# hu.zhijiang@zte.com.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################*/

#include <stdlib.h>
#include <string.h>

#include "buffer.h"
#include "misc.h"

struct buffer_ctl buffctl;
struct packet_ctl *packetctl[PACKETS_PER_BUFFER];
struct packet_ctl empty;

void buffer_init()
{
    int i;

    for (i = 0; i < PACKETS_PER_BUFFER; i++) {
        packetctl[i] = (struct packet_ctl *)wrapper_malloc(PACKET_SIZE);
        memset(packetctl[i], 0, PACKET_SIZE);
        packetctl[i]->data_size = 0;
        packetctl[i]->seq = 0;
    }

    buffctl.buffer_id = 0;
    buffctl.packet_id_base = 0;
    buffctl.pkt_count = 0;
    empty.data_size = 0;
}

void packetctl_precheck()
{
    int i;

    for (i = 0; i < PACKETS_PER_BUFFER; i++) {
        if (packetctl[i]->data_size != 0) {
            crit("Error precheck packet slot %d,%d", i, packetctl[i]->data_size);
        }
    }
}

// Calculate packet checksum
uint32_t packet_csum(uint8_t *data, int len)
{
    uint32_t *item, sum = 0;
    int i = 0;

    while (len % 4) {
        data[len] = 0; len++;
    }

    while (i < len) {
        item = (uint32_t *)((char *)data + i);
        sum += *item;
        i += 4;
    }

    return sum;
}

// Fill buffers from file descriptor
long buffer_fill(int fd)
{
    int s = 0;
    int r = PACKET_PAYLOAD_SIZE;

    buffctl.buffer_id++;
    buffctl.packet_id_base += buffctl.pkt_count;
    buffctl.buffer_size = 0;
    while (r > 0 && s < PACKETS_PER_BUFFER) {
        if ((r = read(fd, packetctl[s]->data, PACKET_PAYLOAD_SIZE)) < 0) {
            crit("Error reading data from stdin");
        }

        // r == 0 means EOF

        if (r > 0) {
            buffctl.buffer_size += r;
            packetctl[s]->data_size = r;
            packetctl[s]->seq = buffctl.packet_id_base + s;
            packetctl[s]->crc = packet_csum(packetctl[s]->data, r);
            s++;
        }
    }

    log(6, "input %d bytes of data in %d packets", buffctl.buffer_size, s);
    buffctl.pkt_count = s;
    return s;
}

long buffer_flush(int fd)
{
    int s = 0;
    while (buffctl.pkt_count--) {
        write(fd, packetctl[s]->data, packetctl[s]->data_size);
        buffctl.buffer_size -= packetctl[s]->data_size;
        packetctl[s]->data_size = 0;
        packetctl[s]->seq = 0;
        packetctl[s]->crc = 0;
        s++;
    }

    return s;
}

/* Caller should use new_pkt as packet buffer again if this returns NULL */
struct packet_ctl *packet_put(struct packet_ctl *new_pkt)
{
    struct packet_ctl *freed_pkt;
    uint32_t i;

    if (new_pkt->seq < buffctl.packet_id_base ||
        new_pkt->seq - buffctl.packet_id_base >= buffctl.pkt_count) {
        return NULL;
    }

    if (new_pkt->data_size == 0) {
        return NULL;
    }

    i = packet_csum(new_pkt->data, new_pkt->data_size);
    if (new_pkt->crc != i) {
        return NULL;
    }

    i = new_pkt->seq - buffctl.packet_id_base;
    freed_pkt = packetctl[i];
    packetctl[i] = new_pkt;
    return freed_pkt;
}

struct packet_ctl *packet_get(uint32_t seq)
{
    empty.seq = seq;

    if (seq < buffctl.packet_id_base ||
        seq - buffctl.packet_id_base >= buffctl.pkt_count) {
        return &empty;
    }

    if (packetctl[seq - buffctl.packet_id_base]->data_size == 0) {
        return &empty;
    }

    return packetctl[seq - buffctl.packet_id_base];
}