+const struct ipcomp_algorithm *
+ipcomp_algorithm_lookup(idx)
+ int idx;
+{
+
+ if (idx == SADB_X_CALG_DEFLATE) {
+ /*
+ * Avert your gaze, ugly hack follows!
+ * We init here so our malloc can allocate using M_WAIT.
+ * We don't want to allocate if ipcomp isn't used, and we
+ * don't want to allocate on the input or output path.
+ * Allocation fails if we use M_NOWAIT because init allocates
+ * something like 256k (ouch).
+ */
+ if (deflate_stream.zalloc == NULL) {
+ deflate_stream.zalloc = deflate_alloc;
+ deflate_stream.zfree = deflate_free;
+ if (deflateInit2(&deflate_stream, deflate_policy, Z_DEFLATED,
+ deflate_window_out, deflate_memlevel, Z_DEFAULT_STRATEGY)) {
+ /* Allocation failed */
+ bzero(&deflate_stream, sizeof(deflate_stream));
+#if IPSEC_DEBUG
+ printf("ipcomp_algorithm_lookup: deflateInit2 failed.\n");
+#endif
+ }
+ }
+
+ if (inflate_stream.zalloc == NULL) {
+ inflate_stream.zalloc = deflate_alloc;
+ inflate_stream.zfree = deflate_free;
+ if (inflateInit2(&inflate_stream, deflate_window_in)) {
+ /* Allocation failed */
+ bzero(&inflate_stream, sizeof(inflate_stream));
+#if IPSEC_DEBUG
+ printf("ipcomp_algorithm_lookup: inflateInit2 failed.\n");
+#endif
+ }
+ }
+
+ return &ipcomp_algorithms[0];
+ }
+ return NULL;
+}
+