History

avutil: Add functions for allocating opaque contexts for algorithms

The current API where the plain size is exposed is not of much use - in most cases it is allocated dynamically anyway. If allocated e.g. on the stack via an uint8_t array, there's no guarantee that the struct's members are aligned properly (unless the array is overallocated and the opaque pointer within it manually aligned to some unspecified alignment).

Martin Storsjö committed 14 years ago

Commit 9a92aea1 parent fb32f3110 files · +78 −5Browse files

Unified · Split

10 files
  1. doc/APIchanges +4 −0
  2. libavutil/aes.c +7 −0
  3. libavutil/aes.h +11 −1
  4. libavutil/md5.c +8 −0
  5. libavutil/md5.h +7 −1
  6. libavutil/sha.c +8 −0
  7. libavutil/sha.h +11 −1
  8. libavutil/tree.c +7 −0
  9. libavutil/tree.h +11 −1
  10. libavutil/version.h +4 −1
doc/APIchanges+4−0
@@ -13,6 +13,10 @@ libavutil: 2011-04-18 · 12 unmodified lines
1313
1414 API changes, most recent first:
1515
16+2012-10-xx - xxxxxxx - lavu 51.43.0 - aes.h, md5.h, sha.h, tree.h
17+ Add functions for allocating the opaque contexts for the algorithms,
18+ deprecate the context size variables.
19+
1620 2012-10-xx - xxxxxxx - lavf 54.18.0 - avio.h
1721 Add avio_closep to complement avio_close.
1822
libavutil/aes.c+7−0
@@ -39,7 +39,14 @@ typedef struct AVAES { · 38 unmodified lines
3939 int rounds;
4040 } AVAES;
4141
42+#if FF_API_CONTEXT_SIZE
4243 const int av_aes_size= sizeof(AVAES);
44+#endif
45+
46+struct AVAES *av_aes_alloc(void)
47+{
48+ return av_mallocz(sizeof(struct AVAES));
49+}
4350
4451 static const uint8_t rcon[10] = {
4552 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
libavutil/aes.h+11−1
@@ -23,16 +23,26 @@ · 22 unmodified lines
2323
2424 #include <stdint.h>
2525
26+#include "attributes.h"
27+#include "version.h"
28+
2629 /**
2730 * @defgroup lavu_aes AES
2831 * @ingroup lavu_crypto
2932 * @{
3033 */
3134
32−extern const int av_aes_size;
35+#if FF_API_CONTEXT_SIZE
36+extern attribute_deprecated const int av_aes_size;
37+#endif
3338
3439 struct AVAES;
3540
41+/**
42+ * Allocate an AVAES context.
43+ */
44+struct AVAES *av_aes_alloc(void);
45+
3646 /**
3747 * Initialize an AVAES context.
3848 * @param key_bits 128, 192 or 256
libavutil/md5.c+8−0
@@ -34,6 +34,7 @@ · 33 unmodified lines
3434 #include "bswap.h"
3535 #include "intreadwrite.h"
3636 #include "md5.h"
37+#include "mem.h"
3738
3839 typedef struct AVMD5{
3940 uint64_t len;
@@ -41,7 +42,14 @@ typedef struct AVMD5{ · 1 unmodified line
4142 uint32_t ABCD[4];
4243 } AVMD5;
4344
45+#if FF_API_CONTEXT_SIZE
4446 const int av_md5_size = sizeof(AVMD5);
47+#endif
48+
49+struct AVMD5 *av_md5_alloc(void)
50+{
51+ return av_mallocz(sizeof(struct AVMD5));
52+}
4553
4654 static const uint8_t S[4][4] = {
4755 { 7, 12, 17, 22 }, /* round 1 */
libavutil/md5.h+7−1
@@ -23,16 +23,22 @@ · 22 unmodified lines
2323
2424 #include <stdint.h>
2525
26+#include "attributes.h"
27+#include "version.h"
28+
2629 /**
2730 * @defgroup lavu_md5 MD5
2831 * @ingroup lavu_crypto
2932 * @{
3033 */
3134
32−extern const int av_md5_size;
35+#if FF_API_CONTEXT_SIZE
36+extern attribute_deprecated const int av_md5_size;
37+#endif
3338
3439 struct AVMD5;
3540
41+struct AVMD5 *av_md5_alloc(void);
3642 void av_md5_init(struct AVMD5 *ctx);
3743 void av_md5_update(struct AVMD5 *ctx, const uint8_t *src, const int len);
3844 void av_md5_final(struct AVMD5 *ctx, uint8_t *dst);
libavutil/sha.c+8−0
@@ -26,6 +26,7 @@ · 25 unmodified lines
2626 #include "bswap.h"
2727 #include "sha.h"
2828 #include "intreadwrite.h"
29+#include "mem.h"
2930
3031 /** hash context */
3132 typedef struct AVSHA {
@@ -37,7 +38,14 @@ typedef struct AVSHA { · 5 unmodified lines
3738 void (*transform)(uint32_t *state, const uint8_t buffer[64]);
3839 } AVSHA;
3940
41+#if FF_API_CONTEXT_SIZE
4042 const int av_sha_size = sizeof(AVSHA);
43+#endif
44+
45+struct AVSHA *av_sha_alloc(void)
46+{
47+ return av_mallocz(sizeof(struct AVSHA));
48+}
4149
4250 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
4351
libavutil/sha.h+11−1
@@ -23,16 +23,26 @@ · 22 unmodified lines
2323
2424 #include <stdint.h>
2525
26+#include "attributes.h"
27+#include "version.h"
28+
2629 /**
2730 * @defgroup lavu_sha SHA
2831 * @ingroup lavu_crypto
2932 * @{
3033 */
3134
32−extern const int av_sha_size;
35+#if FF_API_CONTEXT_SIZE
36+extern attribute_deprecated const int av_sha_size;
37+#endif
3338
3439 struct AVSHA;
3540
41+/**
42+ * Allocate an AVSHA context.
43+ */
44+struct AVSHA *av_sha_alloc(void);
45+
3646 /**
3747 * Initialize SHA-1 or SHA-2 hashing.
3848 *
libavutil/tree.c+7−0
@@ -28,7 +28,14 @@ typedef struct AVTreeNode { · 27 unmodified lines
2828 int state;
2929 } AVTreeNode;
3030
31+#if FF_API_CONTEXT_SIZE
3132 const int av_tree_node_size = sizeof(AVTreeNode);
33+#endif
34+
35+struct AVTreeNode *av_tree_node_alloc(void)
36+{
37+ return av_mallocz(sizeof(struct AVTreeNode));
38+}
3239
3340 void *av_tree_find(const AVTreeNode *t, void *key,
3441 int (*cmp)(void *key, const void *b), void *next[2])
libavutil/tree.h+11−1
@@ -27,6 +27,9 @@ · 26 unmodified lines
2727 #ifndef AVUTIL_TREE_H
2828 #define AVUTIL_TREE_H
2929
30+#include "attributes.h"
31+#include "version.h"
32+
3033 /**
3134 * @addtogroup lavu_tree AVTree
3235 * @ingroup lavu_data
@@ -40,7 +43,14 @@ · 7 unmodified lines
4043
4144
4245 struct AVTreeNode;
43−extern const int av_tree_node_size;
46+#if FF_API_CONTEXT_SIZE
47+extern attribute_deprecated const int av_tree_node_size;
48+#endif
49+
50+/**
51+ * Allocate an AVTreeNode.
52+ */
53+struct AVTreeNode *av_tree_node_alloc(void);
4454
4555 /**
4656 * Find an element.
libavutil/version.h+4−1
@@ -37,7 +37,7 @@ · 36 unmodified lines
3737 */
3838
3939 #define LIBAVUTIL_VERSION_MAJOR 51
40−#define LIBAVUTIL_VERSION_MINOR 42
40+#define LIBAVUTIL_VERSION_MINOR 43
4141 #define LIBAVUTIL_VERSION_MICRO 0
4242
4343 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
@@ -76,6 +76,9 @@ · 32 unmodified lines
7676 #ifndef FF_API_PIX_FMT
7777 #define FF_API_PIX_FMT (LIBAVUTIL_VERSION_MAJOR < 52)
7878 #endif
79+#ifndef FF_API_CONTEXT_SIZE
80+#define FF_API_CONTEXT_SIZE (LIBAVUTIL_VERSION_MAJOR < 52)
81+#endif
7982
8083 /**
8184 * @}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t