]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/circularbytebuffer.c
clearification of keysize descriptor stuff
[arm-crypto-lib.git] / test_src / circularbytebuffer.c
1 /* circularbytebuffer.c */
2 /*
3     This file is part of the ARM-circularbytebuffer.
4     Copyright (C) 2009  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * \file     circularbytebuffer.c
21  * \email    daniel.otte@rub.de
22  * \author   Daniel Otte 
23  * \date     2009-07-24
24  * \license  GPLv3 or later
25  * \ingroup  circularbytebuffer
26  * \brief    implementation for circular byte buffer
27  */
28  #include <stdint.h>
29  #include <stdlib.h>
30  #include <string.h>
31  #include "circularbytebuffer.h"
32  
33
34 #ifndef CIRCULARBYTEBUFFER_NO_MALLOC
35 #  define CIRCULARBYTEBUFFER_NO_MALLOC 0
36 #endif
37
38 #ifndef CIRCULARBYTEBUFFER_NO_INIT2
39 #  define CIRCULARBYTEBUFFER_NO_INIT2 0
40 #endif
41
42 #if CIRCULARBYTEBUFFER_NO_MALLOC==0
43 uint8_t circularbytebuffer_init(uint32_t buffersize, circularbytebuffer_t* cb){
44         cb->buffer_size = buffersize;
45         cb->buffer = malloc(buffersize);
46         cb->head = cb->tail = cb->buffer;
47         cb->top = cb->buffer + cb->buffer_size;
48         cb->fillcount = 0;
49         if(cb->buffer)
50                 return 1; /* success */
51         return 0; /* malloc failed */   
52 }
53 #endif
54
55 #if CIRCULARBYTEBUFFER_NO_INIT2==0
56 void circularbytebuffer_init2(uint32_t buffersize, circularbytebuffer_t* cb, void* buffer){
57         cb->buffer_size = buffersize;
58         cb->buffer = buffer;
59         cb->head = cb->tail = cb->buffer;
60         cb->top = cb->buffer + cb->buffer_size;
61         cb->fillcount = 0;
62 }
63 #endif
64
65 uint16_t circularbytebuffer_get_lifo(circularbytebuffer_t* cb){
66         uint8_t ret;
67         if(cb->fillcount==0)
68                 return 0xffff;
69         --cb->fillcount;
70         ret=*(cb->tail);
71         cb->tail = (uint8_t*)(cb->tail) + 1;
72         if(cb->tail>=cb->top)   
73                 cb->tail = (uint8_t*)(cb->tail) - cb->buffer_size;
74         return ret;     
75 }
76
77 uint16_t circularbytebuffer_get_fifo(circularbytebuffer_t* cb){
78         uint8_t ret;
79         if(cb->fillcount==0)
80                 return 0xffff;
81         --cb->fillcount;
82         ret=*(cb->head);
83         cb->head = (uint8_t*)(cb->head) - 1;
84         if(cb->head<cb->buffer) 
85                 cb->head = (uint8_t*)(cb->head) + cb->buffer_size;
86         return ret;
87 }
88
89 uint8_t circularbytebuffer_append(uint8_t elem, circularbytebuffer_t* cb){
90         if(cb->fillcount==cb->buffer_size)
91                 return 1;
92         cb->fillcount++;        
93         cb->tail = cb->tail - 1;
94         if(cb->tail<cb->buffer) 
95                 cb->tail = (uint8_t*)(cb->tail) + cb->buffer_size;
96         if(cb->fillcount==1)
97                 cb->head = cb->tail;
98         *(cb->tail) = elem;
99         return 0;
100 }
101
102 uint8_t circularbytebuffer_push(uint8_t elem, circularbytebuffer_t* cb){
103         if(cb->fillcount==cb->buffer_size)
104                 return 1;
105         cb->fillcount++;        
106         cb->head = cb->head + 1;
107         if(cb->head>=cb->top)   
108                 cb->head = (uint8_t*)(cb->head) - cb->buffer_size;
109         if(cb->fillcount==1)
110                 cb->tail = cb->head;
111         *(cb->head) = elem;
112         return 0;
113 }
114
115 uint32_t circularbytebuffer_cnt(circularbytebuffer_t* cb){
116         return (cb->fillcount);
117 }
118
119 #if CIRCULARBYTEBUFFER_NO_MALLOC==0
120 void circularbytebuffer_free(circularbytebuffer_t* cb){
121         free(cb->buffer);
122 }
123 #endif