Created
February 10, 2014 18:14
-
-
Save j0sh/8921240 to your computer and use it in GitHub Desktop.
Libav Dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From 6919a147f72647de98791af7f0473643adc5854d Mon Sep 17 00:00:00 2001 | |
| From: Josh Allmann <joshua.allmann@gmail.com> | |
| Date: Fri, 4 Jun 2010 22:58:00 -0700 | |
| Subject: [PATCH 1/2] Added dictionary data structure. This will make setting custom HTTP headers more flexible. | |
| --- | |
| libavformat/http.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| 1 files changed, 91 insertions(+), 0 deletions(-) | |
| diff --git a/libavformat/http.c b/libavformat/http.c | |
| index 9be921b..199dc99 100644 | |
| --- a/libavformat/http.c | |
| +++ b/libavformat/http.c | |
| @@ -37,6 +37,19 @@ | |
| #define URL_SIZE 4096 | |
| #define MAX_REDIRECTS 8 | |
| +#define TABLESIZE 128 | |
| + | |
| +typedef struct pair { | |
| + int hashval; | |
| + char key[128]; | |
| + char value[128]; | |
| + struct pair *next; | |
| +} pair; | |
| + | |
| +typedef struct { | |
| + pair *table[TABLESIZE]; | |
| +} DictContext; | |
| + | |
| typedef struct { | |
| URLContext *hd; | |
| unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end; | |
| @@ -66,6 +79,84 @@ void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked) | |
| ((HTTPContext*)h->priv_data)->is_chunked = is_chunked; | |
| } | |
| +/** | |
| + * bernstein hash function | |
| + * source: | |
| + * http://groups.google.com/group/comp.lang.c/msg/6b82e964887d73d9?dmode=source&output=gplain | |
| + */ | |
| +static int hash(const char *key) | |
| +{ | |
| + int h = 5381; | |
| + while (*key) { | |
| + h = ((h << 5) + h) + *key++; | |
| + } | |
| + return h & (TABLESIZE-1); | |
| +} | |
| + | |
| +static int dict_add(DictContext *hc, const char *key, const char *value) | |
| +{ | |
| + int hashval = hash(key); | |
| + pair *p = hc->table[hashval], *first = p; | |
| + | |
| + while (p && strcmp(key, p->key)) | |
| + p = p->next; | |
| + | |
| + if (!p) { | |
| + pair* newp = (pair*)av_malloc(sizeof(pair)); | |
| + if (!newp) return AVERROR(ENOMEM); | |
| + strncpy(newp->key, key, sizeof(newp->key)); | |
| + newp->key[sizeof(newp->key)-1] = '\0'; | |
| + newp->next = first; | |
| + p = hc->table[hashval] = newp; | |
| + } | |
| + | |
| + strncpy(p->value, value, sizeof(p->value)); | |
| + p->value[sizeof(p->value)-1] = '\0'; | |
| + return 0; | |
| +} | |
| + | |
| +static void dict_remove(DictContext *hc, const char *key) | |
| +{ | |
| + int hashval = hash(key); | |
| + pair *p = hc->table[hashval], *prev = NULL; | |
| + while (p && strcmp(key, p->key)) { | |
| + prev = p; | |
| + p = p->next; | |
| + } | |
| + | |
| + if (p) { | |
| + if (prev) | |
| + prev->next = p->next; | |
| + else | |
| + hc->table[hashval] = p->next; | |
| + av_free(p); | |
| + } | |
| +} | |
| + | |
| +static void free_buckets(pair *p) | |
| +{ | |
| + if (p->next) | |
| + free_buckets(p->next); | |
| + av_free(p); | |
| +} | |
| + | |
| +static void dict_free(DictContext *hc) | |
| +{ | |
| + int i; | |
| + for (i = 0; i < TABLESIZE; i++) { | |
| + pair *p = hc->table[i]; | |
| + if (p) | |
| + free_buckets(p); | |
| + } | |
| + av_free(hc); | |
| +} | |
| + | |
| +static DictContext* dict_init(DictContext **hc) | |
| +{ | |
| + *hc = (DictContext*)av_mallocz(sizeof(DictContext*)*TABLESIZE); | |
| + return *hc; | |
| +} | |
| + | |
| /* return non zero if error */ | |
| static int http_open_cnx(URLContext *h) | |
| { | |
| -- | |
| 1.7.0.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment