[4/7] des: add av_des_alloc()

Message ID 1438032992-4680-5-git-send-email-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer July 27, 2015, 9:36 p.m.
Signed-off-by: James Almer <jamrial@gmail.com>
---
 doc/APIchanges  |  1 +
 libavutil/des.c | 13 +++++++++++++
 libavutil/des.h | 20 ++++++++++++++++++++
 3 files changed, 34 insertions(+)

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index be5d4cf..66c9537 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -17,6 +17,7 @@  API changes, most recent first:
   xxxxxxx -  Add av_blowfish_alloc().
   xxxxxxx -  Add av_rc4_alloc().
   xxxxxxx -  Add av_xtea_alloc().
+  xxxxxxx -  Add av_des_alloc().
 
 2015-xx-xx - lavc 56.35.0 - avcodec.h
   xxxxxxxxx - Rename CODEC_FLAG* defines to AV_CODEC_FLAG*.
diff --git a/libavutil/des.c b/libavutil/des.c
index ab0fc2f..406885a 100644
--- a/libavutil/des.c
+++ b/libavutil/des.c
@@ -22,9 +22,17 @@ 
 #include "avutil.h"
 #include "common.h"
 #include "intreadwrite.h"
+#include "mem.h"
 #include "des.h"
 
+#if FF_API_CRYPTO_CONTEXT
 typedef struct AVDES AVDES;
+#else
+typedef struct AVDES {
+    uint64_t round_keys[3][16];
+    int triple_des;
+} AVDES;
+#endif
 
 #define T(a, b, c, d, e, f, g, h) 64-a,64-b,64-c,64-d,64-e,64-f,64-g,64-h
 static const uint8_t IP_shuffle[] = {
@@ -286,6 +294,11 @@  static uint64_t des_encdec(uint64_t in, uint64_t K[16], int decrypt) {
     return in;
 }
 
+struct AVDES *av_des_alloc(void)
+{
+    return av_mallocz(sizeof(struct AVDES));
+}
+
 int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) {
     if (key_bits != 64 && key_bits != 192)
         return -1;
diff --git a/libavutil/des.h b/libavutil/des.h
index cda9812..f5c224b 100644
--- a/libavutil/des.h
+++ b/libavutil/des.h
@@ -24,16 +24,32 @@ 
 
 #include <stdint.h>
 
+/**
+ * @defgroup lavu_des DES
+ * @ingroup lavu_crypto
+ * @{
+ */
+
+#if FF_API_CRYPTO_CONTEXT
 struct AVDES {
     uint64_t round_keys[3][16];
     int triple_des;
 };
+#else
+struct AVDES;
+#endif
+
+/**
+ * Allocate an AVDES context.
+ */
+struct AVDES *av_des_alloc(void);
 
 /**
  * @brief Initializes an AVDES context.
  *
  * @param key_bits must be 64 or 192
  * @param decrypt 0 for encryption/CBC-MAC, 1 for decryption
+ * @return zero on success, negative value otherwise
  */
 int av_des_init(struct AVDES *d, const uint8_t *key, int key_bits, int decrypt);
 
@@ -58,4 +74,8 @@  void av_des_crypt(struct AVDES *d, uint8_t *dst, const uint8_t *src, int count,
  */
 void av_des_mac(struct AVDES *d, uint8_t *dst, const uint8_t *src, int count);
 
+/**
+ * @}
+ */
+
 #endif /* AVUTIL_DES_H */