void Poly1305::add_data(const byte input[], size_t length) { BOTAN_ASSERT_EQUAL(m_poly.size(), 8, "Initialized"); if(m_buf_pos) { buffer_insert(m_buf, m_buf_pos, input, length); if(m_buf_pos + length >= m_buf.size()) { poly1305_blocks(m_poly, m_buf.data(), 1); input += (m_buf.size() - m_buf_pos); length -= (m_buf.size() - m_buf_pos); m_buf_pos = 0; } } const size_t full_blocks = length / m_buf.size(); const size_t remaining = length % m_buf.size(); if(full_blocks) poly1305_blocks(m_poly, input, full_blocks); buffer_insert(m_buf, m_buf_pos, input + full_blocks * m_buf.size(), remaining); m_buf_pos += remaining; }
/* * PSSR Encode Operation */ secure_vector<byte> PSSR::encoding_of(const secure_vector<byte>& msg, size_t output_bits, RandomNumberGenerator& rng) { const size_t HASH_SIZE = m_hash->output_length(); if(msg.size() != HASH_SIZE) throw Encoding_Error("PSSR::encoding_of: Bad input length"); if(output_bits < 8*HASH_SIZE + 8*m_SALT_SIZE + 9) throw Encoding_Error("PSSR::encoding_of: Output length is too small"); const size_t output_length = (output_bits + 7) / 8; secure_vector<byte> salt = rng.random_vec(m_SALT_SIZE); for(size_t j = 0; j != 8; ++j) m_hash->update(0); m_hash->update(msg); m_hash->update(salt); secure_vector<byte> H = m_hash->final(); secure_vector<byte> EM(output_length); EM[output_length - HASH_SIZE - m_SALT_SIZE - 2] = 0x01; buffer_insert(EM, output_length - 1 - HASH_SIZE - m_SALT_SIZE, salt); mgf1_mask(*m_hash, H.data(), HASH_SIZE, EM.data(), output_length - HASH_SIZE - 1); EM[0] &= 0xFF >> (8 * ((output_bits + 7) / 8) - output_bits); buffer_insert(EM, output_length - 1 - HASH_SIZE, H); EM[output_length-1] = 0xBC; return EM; }
/* * OAEP Pad Operation */ secure_vector<byte> OAEP::pad(const byte in[], size_t in_length, size_t key_length, RandomNumberGenerator& rng) const { key_length /= 8; if(key_length < in_length + 2*m_Phash.size() + 1) throw Invalid_Argument("OAEP: Input is too large"); secure_vector<byte> out(key_length); rng.randomize(out.data(), m_Phash.size()); buffer_insert(out, m_Phash.size(), m_Phash.data(), m_Phash.size()); out[out.size() - in_length - 1] = 0x01; buffer_insert(out, out.size() - in_length, in, in_length); mgf1_mask(*m_hash, out.data(), m_Phash.size(), &out[m_Phash.size()], out.size() - m_Phash.size()); mgf1_mask(*m_hash, &out[m_Phash.size()], out.size() - m_Phash.size(), out.data(), m_Phash.size()); return out; }
/* * Update the hash */ void MDx_HashFunction::add_data(const uint8_t input[], size_t length) { m_count += length; if(m_position) { buffer_insert(m_buffer, m_position, input, length); if(m_position + length >= m_buffer.size()) { compress_n(m_buffer.data(), 1); input += (m_buffer.size() - m_position); length -= (m_buffer.size() - m_position); m_position = 0; } } const size_t full_blocks = length / m_buffer.size(); const size_t remaining = length % m_buffer.size(); if(full_blocks) compress_n(input, full_blocks); buffer_insert(m_buffer, m_position, input + full_blocks * m_buffer.size(), remaining); m_position += remaining; }
/** * Hash additional inputs */ void GOST_34_11::add_data(const uint8_t input[], size_t length) { m_count += length; if(m_position) { buffer_insert(m_buffer, m_position, input, length); if(m_position + length >= hash_block_size()) { compress_n(m_buffer.data(), 1); input += (hash_block_size() - m_position); length -= (hash_block_size() - m_position); m_position = 0; } } const size_t full_blocks = length / hash_block_size(); const size_t remaining = length % hash_block_size(); if(full_blocks) compress_n(input, full_blocks); buffer_insert(m_buffer, m_position, input + full_blocks * hash_block_size(), remaining); m_position += remaining; }
/* * Update the hash */ void Streebog::add_data(const uint8_t input[], size_t length) { const size_t block_size = m_buffer.size(); if(m_position) { buffer_insert(m_buffer, m_position, input, length); if(m_position + length >= block_size) { compress(m_buffer.data()); m_count += 512; input += (block_size - m_position); length -= (block_size - m_position); m_position = 0; } } const size_t full_blocks = length / block_size; const size_t remaining = length % block_size; for(size_t i = 0; i != full_blocks; ++i) { compress(input + block_size * i); m_count += 512; } buffer_insert(m_buffer, m_position, input + full_blocks * block_size, remaining); m_position += remaining; }
bool buffer_insert0(Buffer *buf, size_t pos, const char *data) { if (pos == 0) return buffer_prepend0(buf, data); if (pos == buf->len) return buffer_append0(buf, data); return buffer_insert(buf, pos, data, strlen(data)); }
int string_insert(string *s, size_t pos, char *data) { if (pos > string_length(s)) return -1; return buffer_insert(&s->buffer, pos, data, strlen(data)); }
/** * @brief circular buffer test application */ int main(int argc, char **argv) { unsigned char data = {0}; int size = 0; printf("How much characteres you want to put? "); scanf("%d", &size); for(unsigned int i = 0; i < size; ++i) { printf("Enter the %d value: ",i); scanf("%hu", &data); /* put the new byte on buffer * NOTE! We dont need to manage our buffer */ buffer_insert(&console_buffer,&data, 1); } /* prints the buffer contents */ printf("The buffer contents are \n\r"); while(buffer_empty(&console_buffer) == 0) { /* extracts byte to byte from buffer, higher sizes streams are supported * but this mode shows better the capabilities of circ buffer */ buffer_retrieve(&console_buffer, &data, 1); printf("%hu",&data ); } /* wait for a key */ scanf("%hu", &data); return(0); }
static int luastream_insert (lua_State *L) { int i, top = lua_gettop(L); struct writer_t W; lua_Stream *self = (lua_Stream *)luaL_checkudata(L, 1, LUA_STREAM); size_t size, pos = luaL_checkint(L, 2); luaL_check(self->buf, "%s (released) #1", LUA_STREAM); luaL_check(pos <= buffer_tell(&self->buf), "out of range #2"); W.count = 0; if (pos < buffer_tell(&self->buf)) { buffer_t buf = buffer_new(BUFF_SIZE); for (i = 3, W.pos = 0; i <= top; ++i, W.pos = buffer_tell(&buf)) buffer_writeobject(L, &buf, i, &W); size = buffer_tell(&buf); buffer_insert(&self->buf, pos, buffer_ptr(&buf), size); buffer_delete(&buf); } else { for (i = 3, W.pos = pos; i <= top; ++i, W.pos = buffer_tell(&self->buf)) buffer_writeobject(L, &self->buf, i, &W); size = buffer_tell(&self->buf) - pos; } lua_pushnumber(L, pos); lua_pushnumber(L, pos + size); return 2; }
/* * PSSR Decode/Verify Operation */ bool PSSR::verify(const secure_vector<byte>& const_coded, const secure_vector<byte>& raw, size_t key_bits) { const size_t HASH_SIZE = hash->output_length(); const size_t KEY_BYTES = (key_bits + 7) / 8; if(key_bits < 8*HASH_SIZE + 9) return false; if(raw.size() != HASH_SIZE) return false; if(const_coded.size() > KEY_BYTES || const_coded.size() <= 1) return false; if(const_coded[const_coded.size()-1] != 0xBC) return false; secure_vector<byte> coded = const_coded; if(coded.size() < KEY_BYTES) { secure_vector<byte> temp(KEY_BYTES); buffer_insert(temp, KEY_BYTES - coded.size(), coded); coded = temp; } const size_t TOP_BITS = 8 * ((key_bits + 7) / 8) - key_bits; if(TOP_BITS > 8 - high_bit(coded[0])) return false; byte* DB = &coded[0]; const size_t DB_size = coded.size() - HASH_SIZE - 1; const byte* H = &coded[DB_size]; const size_t H_size = HASH_SIZE; mgf1_mask(*hash, &H[0], H_size, &DB[0], DB_size); DB[0] &= 0xFF >> TOP_BITS; size_t salt_offset = 0; for(size_t j = 0; j != DB_size; ++j) { if(DB[j] == 0x01) { salt_offset = j + 1; break; } if(DB[j]) return false; } if(salt_offset == 0) return false; for(size_t j = 0; j != 8; ++j) hash->update(0); hash->update(raw); hash->update(&DB[salt_offset], DB_size - salt_offset); secure_vector<byte> H2 = hash->final(); return same_mem(&H[0], &H2[0], HASH_SIZE); }
void OFB::set_iv(const byte iv[], size_t iv_len) { if(!valid_iv_length(iv_len)) throw Invalid_IV_Length(name(), iv_len); zeroise(m_buffer); buffer_insert(m_buffer, 0, iv, iv_len); m_cipher->encrypt(m_buffer); m_buf_pos = 0; }
void producer(int nconsumers) { char buffer[MAXLINELEN]; int number; printf(" producer: starting\n"); while (fgets(buffer, MAXLINELEN, stdin) != NULL) { number = atoi(buffer); printf("producer: %d\n", number); buffer_insert(number); } printf("producer: read EOF, sending %d '-1' numbers\n", nconsumers); for (number = 0; number < nconsumers; number++) buffer_insert(-1); printf("producer: exiting\n"); }
bool buffer_insert_ts (Buffer* buffer, int32_t index, unsigned char* data, int32_t length) { assert(NULL != buffer); assert(NULL != buffer->mutex); pthread_mutex_lock(buffer->mutex); bool ret = buffer_insert(buffer, index, data, length); pthread_mutex_unlock(buffer->mutex); return ret; }
void ppmoutput_cleanup_frame( stream_node_t *node, framebuffer_t *frame ) { // Either destroy the frame or insert it in the output buffer if( node->output == NULL ) { framebuffer_destroy( frame ); free( frame ); } else { buffer_insert( node->output, frame ); } }
int main( int argc, char* argv[] ) { struct buffer* buf = buffer_new(); buffer_insert_from_array( buf, 0, "Hello World!", 0, 12 ); printf( "%s\n", buffer_pointer( buf ) ); buffer_insert( buf, buffer_length( buf ), '\n' ); buffer_insert_from_array( buf, buffer_length( buf ), "Hello World!", 0, 12 ); printf( "%s\n", buffer_pointer( buf ) ); buffer_del( &buf ); return 0; }
void text_dirty(struct text *text) { if (text->path && !text->dirties && text->flags & TEXT_RDONLY) message("%s: read-only, %s", path_format(text->path), make_writable ? "will be made writable" : "changes won't be saved here"); text->dirties++; if (!text->buffer) { text->buffer = buffer_create(text->fd >= 0 ? text->path : NULL); if (text->clean) buffer_insert(text->buffer, text->clean, 0, text->clean_bytes); grab_mtime(text); } }
/* * Convert some data into hex format */ void Hex_Encoder::write(const byte input[], size_t length) { buffer_insert(in, position, input, length); if(position + length >= in.size()) { encode_and_send(in.data(), in.size()); input += (in.size() - position); length -= (in.size() - position); while(length >= in.size()) { encode_and_send(input, in.size()); input += in.size(); length -= in.size(); } copy_mem(in.data(), input, length); position = 0; } position += length; }
void fill_buffer( buffer_t *buffer ) { int32 i; framebuffer_t *frame; printf( "Filling start thread's input buffer...\n" ); for( i = 0; i < buffer->size; i++ ) { // Allocate a new frame frame = (framebuffer_t*)malloc( sizeof(framebuffer_t) ); if( frame != NULL ) { framebuffer_create( frame, INPUT_WIDTH, INPUT_HEIGHT ); } // Insert the frame into the buffer buffer_insert( buffer, frame ); } }
/* * Convert some data into Base64 */ void Base64_Encoder::write(const byte input[], size_t length) { buffer_insert(m_in, m_position, input, length); if(m_position + length >= m_in.size()) { encode_and_send(m_in.data(), m_in.size()); input += (m_in.size() - m_position); length -= (m_in.size() - m_position); while(length >= m_in.size()) { encode_and_send(input, m_in.size()); input += m_in.size(); length -= m_in.size(); } copy_mem(m_in.data(), input, length); m_position = 0; } m_position += length; }
string *string_substr(string *s, size_t pos, size_t len) { string *result; size_t n, l = string_length(s); if (pos > l) return NULL; result = malloc(sizeof *result); if (!result) return NULL; n = l - pos < len ? l - pos : len; buffer_init(&result->buffer); buffer_reserve(&result->buffer, n + 1); buffer_insert(&result->buffer, 0, string_data(s) + pos, n); string_data(result)[n] = 0; return result; }
/* * PKCS1 Pad Operation */ secure_vector<byte> EME_PKCS1v15::pad(const byte in[], size_t inlen, size_t olen, RandomNumberGenerator& rng) const { olen /= 8; if(olen < 10) throw Encoding_Error("PKCS1: Output space too small"); if(inlen > olen - 10) throw Encoding_Error("PKCS1: Input is too large"); secure_vector<byte> out(olen); out[0] = 0x02; for(size_t j = 1; j != olen - inlen - 1; ++j) while(out[j] == 0) out[j] = rng.next_byte(); buffer_insert(out, olen - inlen, in, inlen); return out; }
void* tgainput_thread( void *arg ) { Hint res; stream_node_t *node; framebuffer_t image; framebuffer_t *frame; // Get the argument to the thread node = (stream_node_t*)arg; // Check that the node was setup correctly tgainput_check_buffers( node ); // Read the image in res = framebuffer_tgain( &image, "image.tga" ); if( res != 0 ) { perror( "cannot read input image" ); exit(1); } // Run the input thread while( 1 ) { // Get the next video frame frame = tgainput_next_frame( node, &image ); // Determine if we should halt the processing if( frame == NULL ) break; // Copy the video into the output frame framebuffer_copy( frame, &image ); // Insert the image to the output buffer buffer_insert( node->output, frame ); } // Finish running the thread return NULL; }
/* * Update an CMAC Calculation */ void CMAC::add_data(const uint8_t input[], size_t length) { const size_t bs = output_length(); buffer_insert(m_buffer, m_position, input, length); if(m_position + length > bs) { xor_buf(m_state, m_buffer, bs); m_cipher->encrypt(m_state); input += (bs - m_position); length -= (bs - m_position); while(length > bs) { xor_buf(m_state, input, bs); m_cipher->encrypt(m_state); input += bs; length -= bs; } copy_mem(m_buffer.data(), input, length); m_position = 0; } m_position += length; }
static int luastream_copy (lua_State *L) { lua_Stream *self, *other; int index = 1; size_t pos, total, size; self = (lua_Stream *)luaL_checkudata(L, index, LUA_STREAM); luaL_check(self->buf, "%s (released) #1", LUA_STREAM); if (lua_isnumber(L, index)) pos = lua_tonumber(L, ++index); else pos = buffer_tell(&self->buf); other = (lua_Stream *)luaL_checkudata(L, ++index, LUA_STREAM); luaL_check(pos <= buffer_tell(&self->buf), "out of range #2"); luaL_check(other->buf, "%s (released) #%d", LUA_STREAM, index); total = buffer_tell(&other->buf); size = luaL_optint(L, ++index, total > other->pos ? total - other->pos : 0); luaL_check(other->pos + size <= total, "size overflow #%d", index); if (size) buffer_insert(&self->buf, pos, buffer_at(&other->buf, other->pos), size); lua_pushnumber(L, pos); lua_pushnumber(L, pos + size); return 2; }
static void reactor_stream_read(reactor_stream *stream) { char buffer[REACTOR_STREAM_BLOCK_SIZE]; reactor_stream_data data; ssize_t n; n = read(reactor_desc_fd(&stream->desc), buffer, sizeof buffer); if (n <= 0) { if (n == 0) reactor_user_dispatch(&stream->user, REACTOR_STREAM_CLOSE, stream); else if (errno != EAGAIN) reactor_user_dispatch(&stream->user, REACTOR_STREAM_ERROR, stream); return; } reactor_stream_hold(stream); if (buffer_size(&stream->read) == 0) { data = (reactor_stream_data) {.base = buffer, .size = n}; reactor_user_dispatch(&stream->user, REACTOR_STREAM_READ, &data); if (data.size) buffer_insert(&stream->read, buffer_size(&stream->read), data.base, data.size); }
static ssize_t old_fashioned_read(struct text *text) { char *raw; ssize_t got, total = 0; size_t max; #define CHUNK 1024 do { buffer_insert(text->buffer, NULL, total, CHUNK); max = buffer_raw(text->buffer, &raw, total, CHUNK); errno = 0; got = read(text->fd, raw, max); if (got < 0) { message("%s: can't read", path_format(text->path)); buffer_delete(text->buffer, 0, total + CHUNK); return -1; } buffer_delete(text->buffer, total + got, CHUNK - got); total += got; } while (got); return total; }
int string_append(string *s, char *data) { return buffer_insert(&s->buffer, string_length(s), data, strlen(data)); }
int string_prepend(string *s, char *data) { return buffer_insert(&s->buffer, 0, data, strlen(data)); }
void string_clear(string *s) { buffer_clear(&s->buffer); buffer_insert(&s->buffer, 0, "", 1); }