16. bit
bits.c
/* * Copyright (C) 2018 Swift Navigation Inc. * Contact: Swift Navigation <dev@swiftnav.com> * * This source is subject to the license found in the file 'LICENSE' which must * be distributed together with this source. All other rights reserved. * * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. */ #include <rtcm3/bits.h> /** Get bit field from buffer as an unsigned integer. * Unpacks `len` bits at bit position `pos` from the start of the buffer. * Maximum bit field length is 32 bits, i.e. `len <= 32`. * * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * eturn Bit field as an unsigned value. */ uint32_t rtcm_getbitu(const uint8_t *buff, uint32_t pos, uint8_t len) { uint32_t bits = 0; for (uint32_t i = pos; i < pos + len; i++) { bits = (bits << 1) + ((buff[i / 8] >> (7 - i % 8)) & 1u); } return bits; } /** Get bit field from buffer as an unsigned long integer. * Unpacks `len` bits at bit position `pos` from the start of the buffer. * Maximum bit field length is 64 bits, i.e. `len <= 64`. * * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * eturn Bit field as an unsigned value. */ uint64_t rtcm_getbitul(const uint8_t *buff, uint32_t pos, uint8_t len) { uint64_t bits = 0; for (uint32_t i = pos; i < pos + len; i++) { bits = (bits << 1) + ((buff[i / 8] >> (7 - i % 8)) & 1u); } return bits; } /** Get bit field from buffer as a signed integer. * Unpacks `len` bits at bit position `pos` from the start of the buffer. * Maximum bit field length is 32 bits, i.e. `len <= 32`. * * This function sign extends the `len` bit field to a signed 32 bit integer. * * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * eturn Bit field as a signed value. */ int32_t rtcm_getbits(const uint8_t *buff, uint32_t pos, uint8_t len) { int32_t bits = (int32_t)rtcm_getbitu(buff, pos, len); /* Sign extend, taken from: * http://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend */ int32_t m = 1u << (len - 1); return (bits ^ m) - m; } /** Get bit field from buffer as a signed integer. * Unpacks `len` bits at bit position `pos` from the start of the buffer. * Maximum bit field length is 64 bits, i.e. `len <= 64`. * * This function sign extends the `len` bit field to a signed 64 bit integer. * * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * eturn Bit field as a signed value. */ int64_t rtcm_getbitsl(const uint8_t *buff, uint32_t pos, uint8_t len) { int64_t bits = (int64_t)rtcm_getbitul(buff, pos, len); /* Sign extend, taken from: * http://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend */ int64_t m = ((uint64_t)1) << (len - 1); return (bits ^ m) - m; } /** Set bit field in buffer from an unsigned integer. * Packs `len` bits into bit position `pos` from the start of the buffer. * Maximum bit field length is 32 bits, i.e. `len <= 32`. * * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * param data Unsigned integer to be packed into bit field. */ void rtcm_setbitu(uint8_t *buff, uint32_t pos, uint32_t len, uint32_t data) { uint32_t mask = 1u << (len - 1); if (len <= 0 || 32 < len) { return; } for (uint32_t i = pos; i < pos + len; i++, mask >>= 1) { if (data & mask) { buff[i / 8] |= 1u << (7 - i % 8); } else { buff[i / 8] &= ~(1u << (7 - i % 8)); } } } /** Set bit field in buffer from an unsigned integer. * Packs `len` bits into bit position `pos` from the start of the buffer. * Maximum bit field length is 64 bits, i.e. `len <= 64`. * * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * param data Unsigned integer to be packed into bit field. */ void rtcm_setbitul(uint8_t *buff, uint32_t pos, uint32_t len, uint64_t data) { uint64_t mask = ((uint64_t)1) << (len - 1); if (len <= 0 || 64 < len) { return; } for (uint32_t i = pos; i < pos + len; i++, mask >>= 1) { if (data & mask) { buff[i / 8] |= ((uint64_t)1) << (7 - i % 8); } else { buff[i / 8] &= ~(((uint64_t)1) << (7 - i % 8)); } } } /** Set bit field in buffer from a signed integer. * Packs `len` bits into bit position `pos` from the start of the buffer. * Maximum bit field length is 32 bits, i.e. `len <= 32`. * * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * param data Signed integer to be packed into bit field. */ void rtcm_setbits(uint8_t *buff, uint32_t pos, uint32_t len, int32_t data) { rtcm_setbitu(buff, pos, len, (uint32_t)data); } /** Set bit field in buffer from a signed integer. * Packs `len` bits into bit position `pos` from the start of the buffer. * Maximum bit field length is 32 bits, i.e. `len <= 32`. * * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * param data Signed integer to be packed into bit field. */ void rtcm_setbitsl(uint8_t *buff, uint32_t pos, uint32_t len, int64_t data) { rtcm_setbitul(buff, pos, len, (uint64_t)data); } /* Get sign-magnitude bits, See Note 1, Table 3.3-1, RTCM 3.3 * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * eturn Bit field as a signed value. */ int32_t rtcm_get_sign_magnitude_bit(const uint8_t *buff, uint32_t pos, uint8_t len) { int32_t value = rtcm_getbitu(buff, pos + 1, len - 1); return rtcm_getbitu(buff, pos, 1) ? -value : value; } /* Set sign-magnitude bits, See Note 1, Table 3.3-1, RTCM 3.3 * param buff * param pos Position in buffer of start of bit field in bits. * param len Length of bit field in bits. * data data to encode */ void rtcm_set_sign_magnitude_bit(uint8_t *buff, uint32_t pos, uint8_t len, int64_t data) { rtcm_setbitu(buff, pos, 1, (data < 0) ? 1 : 0); rtcm_setbitu(buff, pos + 1, len - 1, ((data < 0) ? -data : data)); }
bits.h
/* * Copyright (C) 2018 Swift Navigation Inc. * Contact: Swift Navigation <dev@swiftnav.com> * * This source is subject to the license found in the file 'LICENSE' which must * be distributed together with this source. All other rights reserved. * * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. */ #ifndef SWIFTNAV_RTCM3_BITS_H #define SWIFTNAV_RTCM3_BITS_H #ifdef __cplusplus extern "C" { #endif #include <stdint.h> uint32_t rtcm_getbitu(const uint8_t *buff, uint32_t pos, uint8_t len); uint64_t rtcm_getbitul(const uint8_t *buff, uint32_t pos, uint8_t len); int32_t rtcm_getbits(const uint8_t *buff, uint32_t pos, uint8_t len); int64_t rtcm_getbitsl(const uint8_t *buff, uint32_t pos, uint8_t len); void rtcm_setbitu(uint8_t *buff, uint32_t pos, uint32_t len, uint32_t data); void rtcm_setbitul(uint8_t *buff, uint32_t pos, uint32_t len, uint64_t data); void rtcm_setbits(uint8_t *buff, uint32_t pos, uint32_t len, int32_t data); void rtcm_setbitsl(uint8_t *buff, uint32_t pos, uint32_t len, int64_t data); int32_t rtcm_get_sign_magnitude_bit(const uint8_t *buff, uint32_t pos, uint8_t len); void rtcm_set_sign_magnitude_bit(uint8_t *buff, uint32_t pos, uint8_t len, int64_t data); #ifdef __cplusplus } #endif #endif /* SWIFTNAV_RTCM3_BITS_H */
15. list
redis-4.0.11srcadlist.h
/* adlist.h - A generic doubly linked list implementation * * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Redis nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef __ADLIST_H__ #define __ADLIST_H__ /* Node, List, and Iterator are the only data structures used currently. */ typedef struct listNode { struct listNode *prev; struct listNode *next; void *value; } listNode; typedef struct listIter { listNode *next; int direction; } listIter; typedef struct list { listNode *head; listNode *tail; void *(*dup)(void *ptr); void (*free)(void *ptr); int (*match)(void *ptr, void *key); unsigned long len; } list; /* Functions implemented as macros */ #define listLength(l) ((l)->len) #define listFirst(l) ((l)->head) #define listLast(l) ((l)->tail) #define listPrevNode(n) ((n)->prev) #define listNextNode(n) ((n)->next) #define listNodeValue(n) ((n)->value) #define listSetDupMethod(l,m) ((l)->dup = (m)) #define listSetFreeMethod(l,m) ((l)->free = (m)) #define listSetMatchMethod(l,m) ((l)->match = (m)) #define listGetDupMethod(l) ((l)->dup) #define listGetFree(l) ((l)->free) #define listGetMatchMethod(l) ((l)->match) /* Prototypes */ list *listCreate(void); void listRelease(list *list); void listEmpty(list *list); list *listAddNodeHead(list *list, void *value); list *listAddNodeTail(list *list, void *value); list *listInsertNode(list *list, listNode *old_node, void *value, int after); void listDelNode(list *list, listNode *node); listIter *listGetIterator(list *list, int direction); listNode *listNext(listIter *iter); void listReleaseIterator(listIter *iter); list *listDup(list *orig); listNode *listSearchKey(list *list, void *key); listNode *listIndex(list *list, long index); void listRewind(list *list, listIter *li); void listRewindTail(list *list, listIter *li); void listRotate(list *list); void listJoin(list *l, list *o); /* Directions for iterators */ #define AL_START_HEAD 0 #define AL_START_TAIL 1 #endif /* __ADLIST_H__ */
redis-4.0.11srcadlist.c
/* adlist.c - A generic doubly linked list implementation * * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Redis nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdlib.h> #include "adlist.h" /* Create a new list. The created list can be freed with * AlFreeList(), but private value of every node need to be freed * by the user before to call AlFreeList(). * * On error, NULL is returned. Otherwise the pointer to the new list. */ list *listCreate(void) { struct list *list; if ((list = malloc(sizeof(*list))) == NULL) return NULL; list->head = list->tail = NULL; list->len = 0; list->dup = NULL; list->free = NULL; list->match = NULL; return list; } /* Remove all the elements from the list without destroying the list itself. */ void listEmpty(list *list) { unsigned long len; listNode *current, *next; current = list->head; len = list->len; while(len--) { next = current->next; if (list->free) list->free(current->value); free(current); current = next; } list->head = list->tail = NULL; list->len = 0; } /* Free the whole list. * * This function can't fail. */ void listRelease(list *list) { listEmpty(list); free(list); } /* Add a new node to the list, to head, containing the specified 'value' * pointer as value. * * On error, NULL is returned and no operation is performed (i.e. the * list remains unaltered). * On success the 'list' pointer you pass to the function is returned. */ list *listAddNodeHead(list *list, void *value) { listNode *node; if ((node = malloc(sizeof(*node))) == NULL) return NULL; node->value = value; if (list->len == 0) { list->head = list->tail = node; node->prev = node->next = NULL; } else { node->prev = NULL; node->next = list->head; list->head->prev = node; list->head = node; } list->len++; return list; } /* Add a new node to the list, to tail, containing the specified 'value' * pointer as value. * * On error, NULL is returned and no operation is performed (i.e. the * list remains unaltered). * On success the 'list' pointer you pass to the function is returned. */ list *listAddNodeTail(list *list, void *value) { listNode *node; if ((node = malloc(sizeof(*node))) == NULL) return NULL; node->value = value; if (list->len == 0) { list->head = list->tail = node; node->prev = node->next = NULL; } else { node->prev = list->tail; node->next = NULL; list->tail->next = node; list->tail = node; } list->len++; return list; } list *listInsertNode(list *list, listNode *old_node, void *value, int after) { listNode *node; if ((node = malloc(sizeof(*node))) == NULL) return NULL; node->value = value; if (after) { node->prev = old_node; node->next = old_node->next; if (list->tail == old_node) { list->tail = node; } } else { node->next = old_node; node->prev = old_node->prev; if (list->head == old_node) { list->head = node; } } if (node->prev != NULL) { node->prev->next = node; } if (node->next != NULL) { node->next->prev = node; } list->len++; return list; } /* Remove the specified node from the specified list. * It's up to the caller to free the private value of the node. * * This function can't fail. */ void listDelNode(list *list, listNode *node) { if (node->prev) node->prev->next = node->next; else list->head = node->next; if (node->next) node->next->prev = node->prev; else list->tail = node->prev; if (list->free) list->free(node->value); free(node); list->len--; } /* Returns a list iterator 'iter'. After the initialization every * call to listNext() will return the next element of the list. * * This function can't fail. */ listIter *listGetIterator(list *list, int direction) { listIter *iter; if ((iter = malloc(sizeof(*iter))) == NULL) return NULL; if (direction == AL_START_HEAD) iter->next = list->head; else iter->next = list->tail; iter->direction = direction; return iter; } /* Release the iterator memory */ void listReleaseIterator(listIter *iter) { free(iter); } /* Create an iterator in the list private iterator structure */ void listRewind(list *list, listIter *li) { li->next = list->head; li->direction = AL_START_HEAD; } void listRewindTail(list *list, listIter *li) { li->next = list->tail; li->direction = AL_START_TAIL; } /* Return the next element of an iterator. * It's valid to remove the currently returned element using * listDelNode(), but not to remove other elements. * * The function returns a pointer to the next element of the list, * or NULL if there are no more elements, so the classical usage patter * is: * * iter = listGetIterator(list,<direction>); * while ((node = listNext(iter)) != NULL) { * doSomethingWith(listNodeValue(node)); * } * * */ listNode *listNext(listIter *iter) { listNode *current = iter->next; if (current != NULL) { if (iter->direction == AL_START_HEAD) iter->next = current->next; else iter->next = current->prev; } return current; } /* Duplicate the whole list. On out of memory NULL is returned. * On success a copy of the original list is returned. * * The 'Dup' method set with listSetDupMethod() function is used * to copy the node value. Otherwise the same pointer value of * the original node is used as value of the copied node. * * The original list both on success or error is never modified. */ list *listDup(list *orig) { list *copy; listIter iter; listNode *node; if ((copy = listCreate()) == NULL) return NULL; copy->dup = orig->dup; copy->free = orig->free; copy->match = orig->match; listRewind(orig, &iter); while((node = listNext(&iter)) != NULL) { void *value; if (copy->dup) { value = copy->dup(node->value); if (value == NULL) { listRelease(copy); return NULL; } } else value = node->value; if (listAddNodeTail(copy, value) == NULL) { listRelease(copy); return NULL; } } return copy; } /* Search the list for a node matching a given key. * The match is performed using the 'match' method * set with listSetMatchMethod(). If no 'match' method * is set, the 'value' pointer of every node is directly * compared with the 'key' pointer. * * On success the first matching node pointer is returned * (search starts from head). If no matching node exists * NULL is returned. */ listNode *listSearchKey(list *list, void *key) { listIter iter; listNode *node; listRewind(list, &iter); while((node = listNext(&iter)) != NULL) { if (list->match) { if (list->match(node->value, key)) { return node; } } else { if (key == node->value) { return node; } } } return NULL; } /* Return the element at the specified zero-based index * where 0 is the head, 1 is the element next to head * and so on. Negative integers are used in order to count * from the tail, -1 is the last element, -2 the penultimate * and so on. If the index is out of range NULL is returned. */ listNode *listIndex(list *list, long index) { listNode *n; if (index < 0) { index = (-index)-1; n = list->tail; while(index-- && n) n = n->prev; } else { n = list->head; while(index-- && n) n = n->next; } return n; } /* Rotate the list removing the tail node and inserting it to the head. */ void listRotate(list *list) { listNode *tail = list->tail; if (listLength(list) <= 1) return; /* Detach current tail */ list->tail = tail->prev; list->tail->next = NULL; /* Move it as head */ list->head->prev = tail; tail->prev = NULL; tail->next = list->head; list->head = tail; } /* Add all the elements of the list 'o' at the end of the * list 'l'. The list 'other' remains empty but otherwise valid. */ void listJoin(list *l, list *o) { if (o->head) o->head->prev = l->tail; if (l->tail) l->tail->next = o->head; else l->head = o->head; if (o->tail) l->tail = o->tail; l->len += o->len; /* Setup other as an empty list. */ o->head = o->tail = NULL; o->len = 0; }
14. array
pjproject-2.10pjlibincludepjarray.h
/* $Id$ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef __PJ_ARRAY_H__ #define __PJ_ARRAY_H__ /** * @file array.h * @brief PJLIB Array helper. */ #include <pj/types.h> PJ_BEGIN_DECL /** * @defgroup PJ_ARRAY Array helper. * @ingroup PJ_DS * @{ * * This module provides helper to manipulate array of elements of any size. * It provides most used array operations such as insert, erase, and search. */ /** * Insert value to the array at the given position, and rearrange the * remaining nodes after the position. * * @param array the array. * @param elem_size the size of the individual element. * @param count the CURRENT number of elements in the array. * @param pos the position where the new element is put. * @param value the value to copy to the new element. */ PJ_DECL(void) pj_array_insert( void *array, unsigned elem_size, unsigned count, unsigned pos, const void *value); /** * Erase a value from the array at given position, and rearrange the remaining * elements post the erased element. * * @param array the array. * @param elem_size the size of the individual element. * @param count the current number of elements in the array. * @param pos the index/position to delete. */ PJ_DECL(void) pj_array_erase( void *array, unsigned elem_size, unsigned count, unsigned pos); /** * Search the first value in the array according to matching function. * * @param array the array. * @param elem_size the individual size of the element. * @param count the number of elements. * @param matching the matching function, which MUST return PJ_SUCCESS if * the specified element match. * @param result the pointer to the value found. * * @return PJ_SUCCESS if value is found, otherwise the error code. */ PJ_DECL(pj_status_t) pj_array_find( const void *array, unsigned elem_size, unsigned count, pj_status_t (*matching)(const void *value), void **result); /** * @} */ PJ_END_DECL #endif /* __PJ_ARRAY_H__ */
pjproject-2.10pjlibsrcpjarray.c
/* $Id$ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <pj/array.h> #include <pj/string.h> #include <pj/assert.h> #include <pj/errno.h> PJ_DEF(void) pj_array_insert( void *array, unsigned elem_size, unsigned count, unsigned pos, const void *value) { if (count && pos < count) { pj_memmove( (char*)array + (pos+1)*elem_size, (char*)array + pos*elem_size, (count-pos)*elem_size); } pj_memmove((char*)array + pos*elem_size, value, elem_size); } PJ_DEF(void) pj_array_erase( void *array, unsigned elem_size, unsigned count, unsigned pos) { pj_assert(count != 0); if (pos < count-1) { pj_memmove( (char*)array + pos*elem_size, (char*)array + (pos+1)*elem_size, (count-pos-1)*elem_size); } } PJ_DEF(pj_status_t) pj_array_find( const void *array, unsigned elem_size, unsigned count, pj_status_t (*matching)(const void *value), void **result) { unsigned i; const char *char_array = (const char*)array; for (i=0; i<count; ++i) { if ( (*matching)(char_array) == PJ_SUCCESS) { if (result) { *result = (void*)char_array; } return PJ_SUCCESS; } char_array += elem_size; } return PJ_ENOTFOUND; }
13. memery pool
12. base64
pjproject-2.10pjlib-utilincludepjlib-utilase64.h
/* $Id$ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef __PJLIB_UTIL_BASE64_H__ #define __PJLIB_UTIL_BASE64_H__ /** * @file base64.h * @brief Base64 encoding and decoding */ #include <pjlib-util/types.h> PJ_BEGIN_DECL /** * @defgroup PJLIB_UTIL_BASE64 Base64 Encoding/Decoding * @ingroup PJLIB_UTIL_ENCRYPTION * @{ * This module implements base64 encoding and decoding. */ /** * Helper macro to calculate the approximate length required for base256 to * base64 conversion. */ #define PJ_BASE256_TO_BASE64_LEN(len) (len * 4 / 3 + 3) /** * Helper macro to calculate the approximage length required for base64 to * base256 conversion. */ #define PJ_BASE64_TO_BASE256_LEN(len) (len * 3 / 4) /** * Encode a buffer into base64 encoding. * * @param input The input buffer. * @param in_len Size of the input buffer. * @param output Output buffer. Caller must allocate this buffer with * the appropriate size. * @param out_len On entry, it specifies the length of the output buffer. * Upon return, this will be filled with the actual * length of the output buffer. * * @return PJ_SUCCESS on success. */ PJ_DECL(pj_status_t) pj_base64_encode(const pj_uint8_t *input, int in_len, char *output, int *out_len); /** * Decode base64 string. * * @param input Input string. * @param out Buffer to store the output. Caller must allocate * this buffer with the appropriate size. * @param out_len On entry, it specifies the length of the output buffer. * Upon return, this will be filled with the actual * length of the output. */ PJ_DECL(pj_status_t) pj_base64_decode(const pj_str_t *input, pj_uint8_t *out, int *out_len); /** * @} */ PJ_END_DECL #endif /* __PJLIB_UTIL_BASE64_H__ */
pjproject-2.10pjlib-utilsrcpjlib-utilase64.c
/* $Id$ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <pjlib-util/base64.h> #include <pj/assert.h> #include <pj/errno.h> #define INV -1 #define PADDING '=' static const char base64_char[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; static int base256_char(char c) { if (c >= 'A' && c <= 'Z') return (c - 'A'); else if (c >= 'a' && c <= 'z') return (c - 'a' + 26); else if (c >= '0' && c <= '9') return (c - '0' + 52); else if (c == '+') return (62); else if (c == '/') return (63); else { /* It *may* happen on bad input, so this is not a good idea. * pj_assert(!"Should not happen as '=' should have been filtered"); */ return INV; } } static void base256to64(pj_uint8_t c1, pj_uint8_t c2, pj_uint8_t c3, int padding, char *output) { *output++ = base64_char[c1>>2]; *output++ = base64_char[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)]; switch (padding) { case 0: *output++ = base64_char[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)]; *output = base64_char[c3 & 0x3F]; break; case 1: *output++ = base64_char[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)]; *output = PADDING; break; case 2: default: *output++ = PADDING; *output = PADDING; break; } } PJ_DEF(pj_status_t) pj_base64_encode(const pj_uint8_t *input, int in_len, char *output, int *out_len) { const pj_uint8_t *pi = input; pj_uint8_t c1, c2, c3; int i = 0; char *po = output; PJ_ASSERT_RETURN(input && output && out_len, PJ_EINVAL); PJ_ASSERT_RETURN(*out_len >= PJ_BASE256_TO_BASE64_LEN(in_len), PJ_ETOOSMALL); while (i < in_len) { c1 = *pi++; ++i; if (i == in_len) { base256to64(c1, 0, 0, 2, po); po += 4; break; } else { c2 = *pi++; ++i; if (i == in_len) { base256to64(c1, c2, 0, 1, po); po += 4; break; } else { c3 = *pi++; ++i; base256to64(c1, c2, c3, 0, po); } } po += 4; } *out_len = (int)(po - output); return PJ_SUCCESS; } PJ_DEF(pj_status_t) pj_base64_decode(const pj_str_t *input, pj_uint8_t *out, int *out_len) { const char *buf; int len; int i, j, k; int c[4]; PJ_ASSERT_RETURN(input && out && out_len, PJ_EINVAL); buf = input->ptr; len = (int)input->slen; while (len && buf[len-1] == '=') --len; PJ_ASSERT_RETURN(*out_len >= PJ_BASE64_TO_BASE256_LEN(len), PJ_ETOOSMALL); for (i=0, j=0; i<len; ) { /* Fill up c, silently ignoring invalid characters */ for (k=0; k<4 && i<len; ++k) { do { c[k] = base256_char(buf[i++]); } while (c[k]==INV && i<len); } if (k<4) { if (k > 1) { out[j++] = (pj_uint8_t)((c[0]<<2) | ((c[1] & 0x30)>>4)); if (k > 2) { out[j++] = (pj_uint8_t) (((c[1] & 0x0F)<<4) | ((c[2] & 0x3C)>>2)); } } break; } out[j++] = (pj_uint8_t)((c[0]<<2) | ((c[1] & 0x30)>>4)); out[j++] = (pj_uint8_t)(((c[1] & 0x0F)<<4) | ((c[2] & 0x3C)>>2)); out[j++] = (pj_uint8_t)(((c[2] & 0x03)<<6) | (c[3] & 0x3F)); } pj_assert(j <= *out_len); *out_len = j; return PJ_SUCCESS; }
11. color_table[]
ffmpeg-4.1/libavutil/parseutils.c
typedef struct {
const char *name; ///< a string representing the name of the color
uint8_t rgb_color[3]; ///< RGB values for the color
} ColorEntry;
static const ColorEntry color_table[] = {
{ "AliceBlue", { 0xF0, 0xF8, 0xFF } },
{ "AntiqueWhite", { 0xFA, 0xEB, 0xD7 } },
{ "Aqua", { 0x00, 0xFF, 0xFF } },
{ "Aquamarine", { 0x7F, 0xFF, 0xD4 } },
{ "Azure", { 0xF0, 0xFF, 0xFF } },
{ "Beige", { 0xF5, 0xF5, 0xDC } },
{ "Bisque", { 0xFF, 0xE4, 0xC4 } },
{ "Black", { 0x00, 0x00, 0x00 } },
{ "BlanchedAlmond", { 0xFF, 0xEB, 0xCD } },
{ "Blue", { 0x00, 0x00, 0xFF } },
{ "BlueViolet", { 0x8A, 0x2B, 0xE2 } },
{ "Brown", { 0xA5, 0x2A, 0x2A } },
{ "BurlyWood", { 0xDE, 0xB8, 0x87 } },
{ "CadetBlue", { 0x5F, 0x9E, 0xA0 } },
{ "Chartreuse", { 0x7F, 0xFF, 0x00 } },
{ "Chocolate", { 0xD2, 0x69, 0x1E } },
{ "Coral", { 0xFF, 0x7F, 0x50 } },
{ "CornflowerBlue", { 0x64, 0x95, 0xED } },
{ "Cornsilk", { 0xFF, 0xF8, 0xDC } },
{ "Crimson", { 0xDC, 0x14, 0x3C } },
{ "Cyan", { 0x00, 0xFF, 0xFF } },
{ "DarkBlue", { 0x00, 0x00, 0x8B } },
{ "DarkCyan", { 0x00, 0x8B, 0x8B } },
{ "DarkGoldenRod", { 0xB8, 0x86, 0x0B } },
{ "DarkGray", { 0xA9, 0xA9, 0xA9 } },
{ "DarkGreen", { 0x00, 0x64, 0x00 } },
{ "DarkKhaki", { 0xBD, 0xB7, 0x6B } },
{ "DarkMagenta", { 0x8B, 0x00, 0x8B } },
{ "DarkOliveGreen", { 0x55, 0x6B, 0x2F } },
{ "Darkorange", { 0xFF, 0x8C, 0x00 } },
{ "DarkOrchid", { 0x99, 0x32, 0xCC } },
{ "DarkRed", { 0x8B, 0x00, 0x00 } },
{ "DarkSalmon", { 0xE9, 0x96, 0x7A } },
{