summaryrefslogtreecommitdiffstats
path: root/contrib/slapd-modules/datamorph/datamorph.c
blob: 7767586d6824f818f9bba4cf296469c4f1053e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
/* datamorph.c - enumerated and native integer value support */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 2016-2022 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was developed in 2016 by Ondřej Kuzník for Symas Corp.
 */

#include "portable.h"

#ifdef SLAPD_OVER_DATAMORPH

#include <inttypes.h>
#include <ac/stdlib.h>

#if defined(__linux__)
#include <endian.h>

#elif defined(sun)

#define be16toh(x) BE_16(x)
#define le16toh(x) LE_16(x)
#define htobe16(x) BE_16(x)
#define htole16(x) LE_16(x)

#define be32toh(x) BE_32(x)
#define le32toh(x) LE_32(x)
#define htobe32(x) BE_32(x)
#define htole32(x) LE_32(x)

#define be64toh(x) BE_64(x)
#define le64toh(x) LE_64(x)
#define htobe64(x) BE_64(x)
#define htole64(x) LE_64(x)

#elif defined(__NetBSD__) || defined(__FreeBSD__)
#include <sys/endian.h>

#elif defined(__OpenBSD__)
#include <sys/endian.h>

#define be16toh(x) betoh16(x)
#define le16toh(x) letoh16(x)

#define be32toh(x) betoh32(x)
#define le32toh(x) letoh32(x)

#define be64toh(x) betoh64(x)
#define le64toh(x) letoh64(x)

#elif defined(__BYTE_ORDER__) && \
		( defined(__GNUC__) || defined(__clang__) )

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define be16toh(x) __builtin_bswap16(x)
#define le16toh(x) (x)
#define htobe16(x) __builtin_bswap16(x)
#define htole16(x) (x)

#define be32toh(x) __builtin_bswap32(x)
#define le32toh(x) (x)
#define htobe32(x) __builtin_bswap32(x)
#define htole32(x) (x)

#define be64toh(x) __builtin_bswap64(x)
#define le64toh(x) (x)
#define htobe64(x) __builtin_bswap64(x)
#define htole64(x) (x)

#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define be16toh(x) (x)
#define le16toh(x) __builtin_bswap16(x)
#define htobe16(x) (x)
#define htole16(x) __builtin_bswap16(x)

#define be32toh(x) (x)
#define le32toh(x) __builtin_bswap32(x)
#define htobe32(x) (x)
#define htole32(x) __builtin_bswap32(x)

#define be64toh(x) (x)
#define le64toh(x) __builtin_bswap64(x)
#define htobe64(x) (x)
#define htole64(x) __builtin_bswap64(x)

#else
#error "Only support pure big and little endian at the moment"
#endif

#else
#error "I lack the way to check my endianness and convert to/from big-endian"
#endif

#include "slap.h"
#include "slap-config.h"
#include "lutil.h"
#include "ldap_queue.h"

typedef enum datamorph_type_t {
	DATAMORPH_UNSET,
	DATAMORPH_ENUM,
	DATAMORPH_INT,
} datamorph_type;

typedef enum datamorph_flags_t {
	DATAMORPH_FLAG_SIGNED = 1 << 0,
	DATAMORPH_FLAG_LOWER = 1 << 1,
	DATAMORPH_FLAG_UPPER = 1 << 2,
} datamorph_flags;

typedef union datamorph_interval_bound_t {
	int64_t i;
	uint64_t u;
} datamorph_interval_bound;

typedef struct transformation_info_t {
	AttributeDescription *attr;
	datamorph_type type;
	union {
		struct {
			Avlnode *to_db;
			struct berval from_db[256];
		} maps;
#define ti_enum info.maps
		struct {
			datamorph_flags flags;
			unsigned int size;
			datamorph_interval_bound lower, upper;
		} interval;
#define ti_int info.interval
	} info;
} transformation_info;

typedef struct datamorph_enum_mapping_t {
	struct berval wire_value;
	uint8_t db_value;
	transformation_info *transformation;
} datamorph_enum_mapping;

typedef struct datamorph_info_t {
	Avlnode *transformations;
	transformation_info *wip_transformation;
} datamorph_info;

static int
transformation_mapping_cmp( const void *l, const void *r )
{
	const datamorph_enum_mapping *left = l, *right = r;

	return ber_bvcmp( &left->wire_value, &right->wire_value );
}

static int
transformation_info_cmp( const void *l, const void *r )
{
	const transformation_info *left = l, *right = r;

	return ( left->attr == right->attr ) ? 0 :
			( left->attr < right->attr ) ? -1 :
											1;
}

static int
transform_to_db_format_one(
		Operation *op,
		transformation_info *definition,
		struct berval *value,
		struct berval *outval )
{
	switch ( definition->type ) {
		case DATAMORPH_ENUM: {
			datamorph_enum_mapping *mapping, needle = { .wire_value = *value };
			struct berval db_value = { .bv_len = 1 };

			mapping = ldap_avl_find( definition->ti_enum.to_db, &needle,
					transformation_mapping_cmp );
			if ( !mapping ) {
				Debug( LDAP_DEBUG_ANY, "transform_to_db_format_one: "
						"value '%s' not mapped\n",
						value->bv_val );
				return LDAP_CONSTRAINT_VIOLATION;
			}

			db_value.bv_val = (char *)&mapping->db_value;
			ber_dupbv( outval, &db_value );
			assert( outval->bv_val );
			break;
		}

		case DATAMORPH_INT: {
			union {
				char s[8];
				uint8_t be8;
				uint16_t be16;
				uint32_t be32;
				uint64_t be64;
			} buf;
			struct berval db_value = { .bv_val = buf.s };
			char *ptr = value->bv_val + value->bv_len;
			uint64_t unsigned_value;
			int64_t signed_value;

			assert( definition->ti_int.size == 1 ||
					definition->ti_int.size == 2 ||
					definition->ti_int.size == 4 ||
					definition->ti_int.size == 8 );

			/* Read number */
			if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
				signed_value = strtoll( value->bv_val, &ptr, 10 );
			} else {
				unsigned_value = strtoull( value->bv_val, &ptr, 10 );
			}
			if ( *value->bv_val == '\0' || *ptr != '\0' ) {
				Debug( LDAP_DEBUG_ANY, "transform_to_db_format_one: "
						"value '%s' not an integer\n",
						value->bv_val );
				return LDAP_CONSTRAINT_VIOLATION;
			}
			/* Check it's within configured bounds */
			if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
				if ( signed_value < definition->ti_int.lower.i ||
						signed_value > definition->ti_int.upper.i ) {
					Debug( LDAP_DEBUG_ANY, "transform_to_db_format_one: "
							"value '%s' doesn't fit configured constraints\n",
							value->bv_val );
					return LDAP_CONSTRAINT_VIOLATION;
				}
			} else {
				if ( unsigned_value < definition->ti_int.lower.u ||
						unsigned_value > definition->ti_int.upper.u ) {
					Debug( LDAP_DEBUG_ANY, "transform_to_db_format_one: "
							"value '%s' doesn't fit configured constraints\n",
							value->bv_val );
					return LDAP_CONSTRAINT_VIOLATION;
				}
			}

			db_value.bv_len = definition->ti_int.size;
			switch ( definition->ti_int.size ) {
				case 1: {
					if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
						buf.be8 = (unsigned char)((char)signed_value);
					} else {
						buf.be8 = unsigned_value;
					}
					break;
				}
				case 2: {
					uint16_t h16;
					if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
						h16 = signed_value;
					} else {
						h16 = unsigned_value;
					}
					buf.be16 = htobe16( h16 );
					break;
				}
				case 4: {
					uint32_t h32;
					if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
						h32 = signed_value;
					} else {
						h32 = unsigned_value;
					}
					buf.be32 = htobe32( h32 );
					break;
				}
				case 8: {
					uint64_t h64;
					if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
						h64 = signed_value;
					} else {
						h64 = unsigned_value;
					}
					buf.be64 = htobe64( h64 );
					break;
				}
			}
			ber_dupbv( outval, &db_value );
			assert( outval->bv_val );
			break;
		}

		default:
			assert(0);
	}

	return LDAP_SUCCESS;
}

static int
transform_to_db_format(
		Operation *op,
		transformation_info *definition,
		BerVarray values,
		int numvals,
		BerVarray *out )
{
	struct berval *value;
	int i, rc = LDAP_SUCCESS;

	if ( numvals == 0 ) {
		for ( value = values; value; value++, numvals++ )
			; /* Count them */
	}

	assert( out );
	*out = ch_calloc( numvals + 1, sizeof(struct berval) );

	for ( i = 0; i < numvals; i++ ) {
		rc = transform_to_db_format_one(
				op, definition, &values[i], &(*out)[i] );
		if ( rc ) {
			break;
		}
	}

	if ( rc ) {
		for ( ; i >= 0; i-- ) {
			ch_free((*out)[i].bv_val);
		}
		ch_free(*out);
	}

	return rc;
}

static int
transform_from_db_format_one(
		Operation *op,
		transformation_info *definition,
		struct berval *value,
		struct berval *outval )
{
	switch ( definition->type ) {
		case DATAMORPH_ENUM: {
			uint8_t index = value->bv_val[0];
			struct berval *val = &definition->info.maps.from_db[index];

			if ( !BER_BVISNULL( val ) ) {
				ber_dupbv( outval, val );
				assert( outval->bv_val );
			} else {
				Debug( LDAP_DEBUG_ANY, "transform_from_db_format_one: "
						"DB value %d has no mapping!\n",
						index );
				/* FIXME: probably still need to return an error */
				BER_BVZERO( outval );
			}
			break;
		}

		case DATAMORPH_INT: {
			char buf[24];
			struct berval wire_value = { .bv_val = buf };
			union lens_t {
				uint8_t be8;
				uint16_t be16;
				uint32_t be32;
				uint64_t be64;
			} *lens = (union lens_t *)value->bv_val;
			uint64_t unsigned_value;
			int64_t signed_value;

			if ( value->bv_len != definition->ti_int.size ) {
				Debug( LDAP_DEBUG_ANY, "transform_from_db_format_one(%s): "
						"unexpected DB value of length %lu when configured "
						"for %u!\n",
						definition->attr->ad_cname.bv_val, value->bv_len,
						definition->ti_int.size );
				/* FIXME: probably still need to return an error */
				BER_BVZERO( outval );
				break;
			}

			assert( definition->ti_int.size == 1 ||
					definition->ti_int.size == 2 ||
					definition->ti_int.size == 4 ||
					definition->ti_int.size == 8 );

			switch ( definition->ti_int.size ) {
				case 1: {
					if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
						signed_value = (int8_t)lens->be8;
					} else {
						unsigned_value = (uint8_t)lens->be8;
					}
					break;
				}
				case 2: {
					uint16_t h16 = be16toh( lens->be16 );
					if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
						signed_value = (int16_t)h16;
					} else {
						unsigned_value = (uint16_t)h16;
					}
					break;
				}
				case 4: {
					uint32_t h32 = be32toh( lens->be32 );
					if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
						signed_value = (int32_t)h32;
					} else {
						unsigned_value = (uint32_t)h32;
					}
					break;
				}
				case 8: {
					uint64_t h64 = be64toh( lens->be64 );
					if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
						signed_value = (int64_t)h64;
					} else {
						unsigned_value = (uint64_t)h64;
					}
					break;
				}
			}
			if ( definition->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
				wire_value.bv_len = sprintf( buf, "%" PRId64, signed_value );
			} else {
				wire_value.bv_len = sprintf( buf, "%" PRIu64, unsigned_value );
			}
			ber_dupbv( outval, &wire_value );
			assert( outval->bv_val );
			break;
		}

		default:
			assert(0);
	}

	return LDAP_SUCCESS;
}

static int
transform_from_db_format(
		Operation *op,
		transformation_info *definition,
		BerVarray values,
		int numvals,
		BerVarray *out )
{
	struct berval *value;
	int i, rc = LDAP_SUCCESS;

	if ( numvals == 0 ) {
		for ( value = values; value; value++, numvals++ )
			; /* Count them */
	}

	assert( out );
	*out = ch_calloc( numvals + 1, sizeof(struct berval) );

	for ( i = 0; i < numvals; i++ ) {
		struct berval bv;
		rc = transform_from_db_format_one( op, definition, &values[i], &bv );
		if ( !BER_BVISNULL( &bv ) ) {
			ber_bvarray_add( out, &bv );
		}
		if ( rc ) {
			break;
		}
	}

	if ( rc ) {
		for ( ; i >= 0; i-- ) {
			ch_free( (*out)[i].bv_val );
		}
		ch_free( *out );
	}

	return rc;
}

static int
datamorph_filter( Operation *op, datamorph_info *ov, Filter *f )
{
	switch ( f->f_choice ) {
		case LDAP_FILTER_PRESENT:
		/* The matching rules are not in place,
		 * so the filter will be ignored */
		case LDAP_FILTER_APPROX:
		case LDAP_FILTER_SUBSTRINGS:
		default:
			break;
			return LDAP_SUCCESS;

		case LDAP_FILTER_AND:
		case LDAP_FILTER_OR: {
			for ( f = f->f_and; f; f = f->f_next ) {
				int rc = datamorph_filter( op, ov, f );
				if ( rc != LDAP_SUCCESS ) {
					return rc;
				}
			}
		} break;

		case LDAP_FILTER_NOT:
			return datamorph_filter( op, ov, f->f_not );

		case LDAP_FILTER_EQUALITY:
		case LDAP_FILTER_GE:
		case LDAP_FILTER_LE: {
			transformation_info *t, needle = { .attr = f->f_ava->aa_desc };

			t = ldap_avl_find(
					ov->transformations, &needle, transformation_info_cmp );
			if ( t ) {
				struct berval new_val;
				int rc = transform_to_db_format_one(
						op, t, &f->f_ava->aa_value, &new_val );
				ch_free( f->f_ava->aa_value.bv_val );

				if ( rc != LDAP_SUCCESS ) {
					f->f_choice = SLAPD_FILTER_COMPUTED;
					f->f_result = SLAPD_COMPARE_UNDEFINED;
				} else {
					f->f_ava->aa_value = new_val;
				}
			}
		} break;
	}
	return LDAP_SUCCESS;
}

static int
datamorph_op_add( Operation *op, SlapReply *rs )
{
	slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
	datamorph_info *ov = on->on_bi.bi_private;
	Entry *e = op->ora_e;
	Attribute *a, *next;
	AttributeDescription *stop = NULL;
	int rc = LDAP_SUCCESS;

	if ( !BER_BVISNULL( &e->e_nname ) && !BER_BVISEMPTY( &e->e_nname ) ) {
		LDAPRDN rDN;
		const char *p;
		int i;

		rc = ldap_bv2rdn_x( &e->e_nname, &rDN, (char **)&p, LDAP_DN_FORMAT_LDAP,
				op->o_tmpmemctx );
		if ( rc != LDAP_SUCCESS ) {
			Debug( LDAP_DEBUG_ANY, "datamorph_op_add: "
					"can't parse rdn: dn=%s\n",
					op->o_req_ndn.bv_val );
			return SLAP_CB_CONTINUE;
		}

		for ( i = 0; rDN[i]; i++ ) {
			transformation_info needle = {};

			/* If we can't resolve the attribute, ignore it */
			if ( slap_bv2ad( &rDN[i]->la_attr, &needle.attr, &p ) ) {
				continue;
			}

			if ( ldap_avl_find( ov->transformations, &needle,
						 transformation_info_cmp ) ) {
				rc = LDAP_CONSTRAINT_VIOLATION;
				Debug( LDAP_DEBUG_TRACE, "datamorph_op_add: "
						"attempted to add transformed attribute in RDN\n" );
				break;
			}
		}

		ldap_rdnfree_x( rDN, op->o_tmpmemctx );
		if ( rc != LDAP_SUCCESS ) {
			send_ldap_error( op, rs, rc,
					"datamorph: trying to add transformed attribute in RDN" );
			return rc;
		}
	}

	for ( a = e->e_attrs; a && a->a_desc != stop; a = next ) {
		transformation_info *t, needle = { .attr = a->a_desc };
		BerVarray new_vals;

		next = a->a_next;

		t = ldap_avl_find( ov->transformations, &needle, transformation_info_cmp );
		if ( !t ) continue;

		rc = transform_to_db_format(
				op, t, a->a_vals, a->a_numvals, &new_vals );
		if ( rc != LDAP_SUCCESS ) {
			goto fail;
		}

		(void)attr_delete( &e->e_attrs, needle.attr );

		rc = attr_merge( e, needle.attr, new_vals, NULL );
		ber_bvarray_free( new_vals );
		if ( rc != LDAP_SUCCESS ) {
			goto fail;
		}
		if ( !stop ) {
			stop = needle.attr;
		}
	}

	return SLAP_CB_CONTINUE;

fail:
	send_ldap_error(
			op, rs, rc, "datamorph: trying to add values outside definitions" );
	return rc;
}

static int
datamorph_op_compare( Operation *op, SlapReply *rs )
{
	slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
	datamorph_info *ov = on->on_bi.bi_private;
	transformation_info *t, needle = { .attr = op->orc_ava->aa_desc };
	int rc = SLAP_CB_CONTINUE;

	t = ldap_avl_find( ov->transformations, &needle, transformation_info_cmp );
	if ( t ) {
		struct berval new_val;

		rc = transform_to_db_format_one(
				op, t, &op->orc_ava->aa_value, &new_val );
		if ( rc != LDAP_SUCCESS ) {
			Debug( LDAP_DEBUG_TRACE, "datamorph_op_compare: "
					"transformation failed for '%s', rc=%d\n",
					op->orc_ava->aa_value.bv_val, rc );
			rs->sr_err = rc = LDAP_COMPARE_FALSE;
			send_ldap_result( op, rs );
			return rc;
		}
		ch_free( op->orc_ava->aa_value.bv_val );
		op->orc_ava->aa_value = new_val;
	}

	return SLAP_CB_CONTINUE;
}

static int
datamorph_op_mod( Operation *op, SlapReply *rs )
{
	slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
	datamorph_info *ov = on->on_bi.bi_private;
	Modifications *mod;
	int rc = SLAP_CB_CONTINUE;

	for ( mod = op->orm_modlist; mod; mod = mod->sml_next ) {
		transformation_info *t, needle = { .attr = mod->sml_desc };
		BerVarray new_vals = NULL;

		if ( mod->sml_numvals == 0 ) continue; /* Nothing to transform */

		t = ldap_avl_find( ov->transformations, &needle, transformation_info_cmp );
		if ( !t ) continue;

		assert( !mod->sml_nvalues );
		rc = transform_to_db_format(
				op, t, mod->sml_values, mod->sml_numvals, &new_vals );
		if ( rc != LDAP_SUCCESS ) {
			goto fail;
		}
		ber_bvarray_free( mod->sml_values );
		mod->sml_values = new_vals;
	}

	return SLAP_CB_CONTINUE;

fail:
	Debug( LDAP_DEBUG_TRACE, "datamorph_op_mod: "
			"dn=%s failed rc=%d\n",
			op->o_req_ndn.bv_val, rc );
	send_ldap_error( op, rs, rc,
			"datamorph: trying to operate on values outside definitions" );
	return rc;
}

static int
datamorph_op_modrdn( Operation *op, SlapReply *rs )
{
	slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
	datamorph_info *ov = on->on_bi.bi_private;
	LDAPRDN rDN;
	const char *p;
	int i, rc;

	rc = ldap_bv2rdn_x( &op->orr_nnewrdn, &rDN, (char **)&p,
			LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx );
	if ( rc != LDAP_SUCCESS ) {
		Debug( LDAP_DEBUG_ANY, "datamorph_op_modrdn: "
				"can't parse rdn for dn=%s\n",
				op->o_req_ndn.bv_val );
		return SLAP_CB_CONTINUE;
	}

	for ( i = 0; rDN[i]; i++ ) {
		transformation_info needle = {};

		/* If we can't resolve the attribute, ignore it */
		if ( slap_bv2ad( &rDN[i]->la_attr, &needle.attr, &p ) ) {
			continue;
		}

		if ( ldap_avl_find(
					 ov->transformations, &needle, transformation_info_cmp ) ) {
			rc = LDAP_CONSTRAINT_VIOLATION;
			Debug( LDAP_DEBUG_TRACE, "datamorph_op_modrdn: "
					"attempted to add transformed values in RDN\n" );
			break;
		}
	}

	ldap_rdnfree_x( rDN, op->o_tmpmemctx );
	if ( rc != LDAP_SUCCESS ) {
		send_ldap_error( op, rs, rc,
				"datamorph: trying to put transformed values in RDN" );
		return rc;
	}

	return SLAP_CB_CONTINUE;
}

static int
datamorph_response( Operation *op, SlapReply *rs )
{
	slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
	datamorph_info *ov = on->on_bi.bi_private;
	Entry *e = NULL, *e_orig = rs->sr_entry;
	AttributeDescription *stop = NULL;
	Attribute *a, *next = NULL;
	int rc = SLAP_CB_CONTINUE;

	if ( rs->sr_type != REP_SEARCH ) {
		return rc;
	}

	for ( a = e_orig->e_attrs; a && a->a_desc != stop; a = next ) {
		transformation_info *t, needle = { .attr = a->a_desc };
		BerVarray new_vals;

		next = a->a_next;

		t = ldap_avl_find( ov->transformations, &needle, transformation_info_cmp );
		if ( !t ) continue;

		rc = transform_from_db_format(
				op, t, a->a_vals, a->a_numvals, &new_vals );
		if ( rc != LDAP_SUCCESS ) {
			break;
		}
		if ( !e ) {
			if ( rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
				e = e_orig;
			} else {
				e = entry_dup( e_orig );
			}
		}

		(void)attr_delete( &e->e_attrs, needle.attr );

		rc = attr_merge( e, needle.attr, new_vals, NULL );
		ber_bvarray_free( new_vals );
		if ( rc != LDAP_SUCCESS ) {
			break;
		}
		if ( !stop ) {
			stop = needle.attr;
		}
	}

	if ( rc == LDAP_SUCCESS ) {
		rc = SLAP_CB_CONTINUE;
		if ( e && e != e_orig ) {
			rs_replace_entry( op, rs, on, e );
			rs->sr_flags &= ~REP_ENTRY_MASK;
			rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
		}
	} else if ( e && e != e_orig ) {
		entry_free( e );
	}

	return rc;
}

static int
datamorph_op_search( Operation *op, SlapReply *rs )
{
	slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
	datamorph_info *ov = on->on_bi.bi_private;
	int rc = SLAP_CB_CONTINUE;

	/*
	 * 1. check all requested attributes -> register callback if one matches
	 * 2. check filter: parse filter, traverse, for configured attributes:
	 *    - presence -> do not touch
	 *    - ava -> replace assertion value with db value if possible, assertion with undefined otherwise 
	 *    - inequality -> ???
	 *    - anything else -> undefined
	 *    - might just check for equality and leave the rest to syntax?
	 * 3. unparse filter
	 */
	if ( datamorph_filter( op, ov, op->ors_filter ) ) {
		send_ldap_error(
				op, rs, LDAP_OTHER, "datamorph: failed to process filter" );
		return LDAP_OTHER;
	}

	return rc;
}

static int
datamorph_entry_release_rw( Operation *op, Entry *e, int rw )
{
	slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
	int rc = LDAP_SUCCESS;

	if ( on->on_next ) {
		rc = overlay_entry_release_ov( op, e, rw, on->on_next );
	} else if ( on->on_info->oi_orig->bi_entry_release_rw ) {
		/* FIXME: there should be a better way */
		rc = on->on_info->oi_orig->bi_entry_release_rw( op, e, rw );
	} else {
		entry_free( e );
	}

	return rc;
}

static int
datamorph_entry_get_rw(
		Operation *op,
		struct berval *ndn,
		ObjectClass *oc,
		AttributeDescription *at,
		int rw,
		Entry **ep )
{
	slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
	datamorph_info *ov = on->on_bi.bi_private;
	Entry *e_orig, *e = NULL;
	int rc;

	if ( on->on_next ) {
		rc = overlay_entry_get_ov( op, ndn, oc, at, rw, ep, on->on_next );
	} else {
		/* FIXME: there should be a better way */
		rc = on->on_info->oi_orig->bi_entry_get_rw( op, ndn, oc, at, rw, ep );
	}
	e_orig = *ep;

	if ( rc == LDAP_SUCCESS && e_orig ) {
		AttributeDescription *stop = NULL;
		Attribute *a;

		for ( a = e_orig->e_attrs; a; a = a->a_next ) {
			transformation_info *t, needle = { .attr = a->a_desc };
			BerVarray new_vals;

			t = ldap_avl_find(
					ov->transformations, &needle, transformation_info_cmp );
			if ( !t ) continue;

			rc = transform_from_db_format(
					op, t, a->a_vals, a->a_numvals, &new_vals );
			if ( rc != LDAP_SUCCESS ) {
				goto fail;
			}
			if ( !e ) {
				e = entry_dup( e_orig );
			}

			(void)attr_delete( &e->e_attrs, needle.attr );

			rc = attr_merge( e, needle.attr, new_vals, NULL );
			ber_bvarray_free( new_vals );
			if ( rc != LDAP_SUCCESS ) {
				goto fail;
			}
			if ( !stop ) {
				stop = needle.attr;
			}
		}
	}
	if ( e ) {
		datamorph_entry_release_rw( op, e_orig, rw );
		*ep = e;
	}

	return rc;

fail:
	if ( e ) {
		entry_free( e );
	}
	(void)datamorph_entry_release_rw( op, *ep, rw );
	return rc;
}

/* Schema */

static int
datamorphBlobValidate( Syntax *syntax, struct berval *in )
{
	/* any value allowed */
	return LDAP_SUCCESS;
}

int
datamorphBinarySignedOrderingMatch( int *matchp,
		slap_mask_t flags,
		Syntax *syntax,
		MatchingRule *mr,
		struct berval *value,
		void *assertedValue )
{
	struct berval *asserted = assertedValue;
	ber_len_t v_len = value->bv_len;
	ber_len_t av_len = asserted->bv_len;

	/* Ordering:
	 * 1. Negative always before non-negative
	 * 2. Shorter before longer
	 * 3. Rest ordered by memory contents (they are big-endian numbers)
	 */
	int match = ( *value->bv_val >= 0 ) - ( *asserted->bv_val >= 0 );

	if ( match == 0 ) match = (int)v_len - (int)av_len;

	if ( match == 0 ) match = memcmp( value->bv_val, asserted->bv_val, v_len );

	/* If used in extensible match filter, match if value < asserted */
	if ( flags & SLAP_MR_EXT ) match = ( match >= 0 );

	*matchp = match;
	return LDAP_SUCCESS;
}

/* Index generation function: Ordered index */
int
datamorphUnsignedIndexer( slap_mask_t use,
		slap_mask_t flags,
		Syntax *syntax,
		MatchingRule *mr,
		struct berval *prefix,
		BerVarray values,
		BerVarray *keysp,
		void *ctx )
{
	int i;
	BerVarray keys;

	for ( i = 0; values[i].bv_val != NULL; i++ ) {
		/* just count them */
	}

	/* we should have at least one value at this point */
	assert( i > 0 );

	keys = slap_sl_malloc( sizeof(struct berval) * ( i + 1 ), ctx );

	for ( i = 0; values[i].bv_val != NULL; i++ ) {
		ber_dupbv_x( &keys[i], &values[i], ctx );
	}

	BER_BVZERO( &keys[i] );

	*keysp = keys;

	return LDAP_SUCCESS;
}

/* Index generation function: Ordered index */
int
datamorphUnsignedFilter(
		slap_mask_t use,
		slap_mask_t flags,
		Syntax *syntax,
		MatchingRule *mr,
		struct berval *prefix,
		void *assertedValue,
		BerVarray *keysp,
		void *ctx )
{
	BerVarray keys;
	BerValue *value = assertedValue;

	keys = slap_sl_malloc( sizeof(struct berval) * 2, ctx );
	ber_dupbv_x( &keys[0], value, ctx );

	BER_BVZERO( &keys[1] );

	*keysp = keys;

	return LDAP_SUCCESS;
}

/* Index generation function: Ordered index */
int
datamorphSignedIndexer(
		slap_mask_t use,
		slap_mask_t flags,
		Syntax *syntax,
		MatchingRule *mr,
		struct berval *prefix,
		BerVarray values,
		BerVarray *keysp,
		void *ctx )
{
	int i;
	BerVarray keys;

	for ( i = 0; values[i].bv_val != NULL; i++ ) {
		/* just count them */
	}

	/* we should have at least one value at this point */
	assert( i > 0 );

	keys = slap_sl_malloc( sizeof(struct berval) * ( i + 1 ), ctx );

	for ( i = 0; values[i].bv_val != NULL; i++ ) {
		keys[i].bv_len = values[i].bv_len + 1;
		keys[i].bv_val = slap_sl_malloc( keys[i].bv_len, ctx );

		/* if positive (highest bit is not set), note that in the first byte */
		*keys[i].bv_val = ~( *values[i].bv_val & 0x80 );
		AC_MEMCPY( keys[i].bv_val + 1, values[i].bv_val, values[i].bv_len );
	}

	BER_BVZERO( &keys[i] );

	*keysp = keys;

	return LDAP_SUCCESS;
}

/* Index generation function: Ordered index */
int
datamorphSignedFilter(
		slap_mask_t use,
		slap_mask_t flags,
		Syntax *syntax,
		MatchingRule *mr,
		struct berval *prefix,
		void *assertedValue,
		BerVarray *keysp,
		void *ctx )
{
	BerVarray keys;
	BerValue *value = assertedValue;

	keys = slap_sl_malloc( sizeof(struct berval) * 2, ctx );

	keys[0].bv_len = value->bv_len + 1;
	keys[0].bv_val = slap_sl_malloc( keys[0].bv_len, ctx );

	/* if positive (highest bit is not set), note that in the first byte */
	*keys[0].bv_val = ~( *value->bv_val & 0x80 );
	AC_MEMCPY( keys[0].bv_val + 1, value->bv_val, value->bv_len );

	BER_BVZERO( &keys[1] );

	*keysp = keys;

	return LDAP_SUCCESS;
}

#define DATAMORPH_ARC "1.3.6.1.4.1.4203.666.11.12"

#define DATAMORPH_SYNTAXES DATAMORPH_ARC ".1"
#define DATAMORPH_SYNTAX_BASE DATAMORPH_SYNTAXES ".1"
#define DATAMORPH_SYNTAX_ENUM DATAMORPH_SYNTAXES ".2"
#define DATAMORPH_SYNTAX_INT DATAMORPH_SYNTAXES ".3"
#define DATAMORPH_SYNTAX_SIGNED_INT DATAMORPH_SYNTAXES ".4"

#define DATAMORPH_MATCHES DATAMORPH_ARC ".2"
#define DATAMORPH_MATCH_EQUALITY DATAMORPH_MATCHES ".1"
#define DATAMORPH_MATCH_SIGNED_EQUALITY DATAMORPH_MATCHES ".2"
#define DATAMORPH_MATCH_ORDERING DATAMORPH_MATCHES ".3"
#define DATAMORPH_MATCH_SIGNED_ORDERING DATAMORPH_MATCHES ".4"

static char *datamorph_sups[] = {
	DATAMORPH_SYNTAX_BASE,
	NULL
};

static char *datamorphSyntaxes[] = {
	DATAMORPH_SYNTAX_SIGNED_INT,
	DATAMORPH_SYNTAX_ENUM,
	DATAMORPH_SYNTAX_INT,

	NULL
};

static slap_syntax_defs_rec datamorph_syntax_defs[] = {
	{ "( " DATAMORPH_SYNTAX_BASE " DESC 'Fixed size value' )",
		0, NULL, NULL, NULL
	},
	{ "( " DATAMORPH_SYNTAX_ENUM " DESC 'Enumerated value' )",
		0, datamorph_sups, datamorphBlobValidate, NULL
	},
	{ "( " DATAMORPH_SYNTAX_INT " DESC 'Fixed-size integer' )",
		0, datamorph_sups, datamorphBlobValidate, NULL
	},
	{ "( " DATAMORPH_SYNTAX_SIGNED_INT " DESC 'Fixed-size signed integer' )",
		0, datamorph_sups, datamorphBlobValidate, NULL
	},

	{ NULL, 0, NULL, NULL, NULL }
};

static Syntax *datamorph_base_syntax;

static slap_mrule_defs_rec datamorph_mrule_defs[] = {
	{ "( " DATAMORPH_MATCH_EQUALITY
		" NAME 'fixedSizeIntegerMatch'"
		" SYNTAX " DATAMORPH_SYNTAX_INT " )",
		SLAP_MR_EQUALITY|SLAP_MR_EXT|SLAP_MR_ORDERED_INDEX,
		datamorphSyntaxes + 1,
		NULL, NULL, octetStringOrderingMatch,
		datamorphUnsignedIndexer, datamorphUnsignedFilter,
		NULL
	},

	{ "( " DATAMORPH_MATCH_SIGNED_EQUALITY
		" NAME 'fixedSizeSignedIntegerMatch'"
		" SYNTAX " DATAMORPH_SYNTAX_SIGNED_INT " )",
		SLAP_MR_EQUALITY|SLAP_MR_EXT|SLAP_MR_ORDERED_INDEX,
		NULL,
		NULL, NULL, datamorphBinarySignedOrderingMatch,
		datamorphSignedIndexer, datamorphSignedFilter,
		NULL
	},

	{ "( " DATAMORPH_MATCH_ORDERING
		" NAME 'fixedSizeIntegerOrderingMatch'"
		" SYNTAX " DATAMORPH_SYNTAX_INT " )",
		SLAP_MR_ORDERING|SLAP_MR_EXT|SLAP_MR_ORDERED_INDEX,
		datamorphSyntaxes + 1,
		NULL, NULL, octetStringOrderingMatch,
		datamorphUnsignedIndexer, datamorphUnsignedFilter,
		"octetStringMatch" },

	{ "( " DATAMORPH_MATCH_SIGNED_ORDERING
		" NAME 'fixedSizeSignedIntegerOrderingMatch'"
		" SYNTAX " DATAMORPH_SYNTAX_SIGNED_INT " )",
		SLAP_MR_ORDERING|SLAP_MR_EXT|SLAP_MR_ORDERED_INDEX,
		NULL,
		NULL, NULL, datamorphBinarySignedOrderingMatch,
		datamorphSignedIndexer, datamorphSignedFilter,
		"octetStringMatch" },

	{ NULL, SLAP_MR_NONE, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};

/* Configuration */

static ConfigLDAPadd datamorph_ldadd_enum;
static ConfigLDAPadd datamorph_ldadd_interval;
static ConfigLDAPadd datamorph_ldadd_mapping;

static ConfigDriver datamorph_set_attribute;
static ConfigDriver datamorph_set_size;
static ConfigDriver datamorph_set_signed;
static ConfigDriver datamorph_set_bounds;
static ConfigDriver datamorph_set_index;
static ConfigDriver datamorph_set_value;
static ConfigDriver datamorph_add_mapping;
static ConfigDriver datamorph_add_transformation;

static ConfigCfAdd datamorph_cfadd;

enum {
	DATAMORPH_INT_SIZE = 1,
	DATAMORPH_INT_SIGNED,
	DATAMORPH_INT_LOWER,
	DATAMORPH_INT_UPPER,

	DATAMORPH_INT_LAST,
};

static ConfigTable datamorph_cfg[] = {
	{ "datamorph_attribute", "attr", 2, 2, 0,
		ARG_STRING|ARG_QUOTE|ARG_MAGIC,
		datamorph_set_attribute,
		"( OLcfgCtAt:7.1 NAME 'olcDatamorphAttribute' "
			"DESC 'Attribute to transform' "
			"EQUALITY caseIgnoreMatch "
			"SYNTAX OMsDirectoryString "
			"SINGLE-VALUE )",
		NULL, NULL
	},
	{ "datamorph_size", "<1|2|4|8>", 2, 2, 0,
		ARG_INT|ARG_MAGIC|DATAMORPH_INT_SIZE,
		datamorph_set_size,
		"( OLcfgCtAt:7.2 NAME 'olcDatamorphIntegerBytes' "
			"DESC 'Integer size in bytes' "
			"EQUALITY integerMatch "
			"SYNTAX OMsInteger "
			"SINGLE-VALUE )",
		NULL, NULL
	},
	{ "datamorph_signed", "TRUE|FALSE", 2, 2, 0,
		ARG_ON_OFF|ARG_MAGIC|DATAMORPH_INT_SIGNED,
		datamorph_set_signed,
		"( OLcfgCtAt:7.3 NAME 'olcDatamorphIntegerSigned' "
			"DESC 'Whether integers maintain sign' "
			"EQUALITY booleanMatch "
			"SYNTAX OMsBoolean "
				"SINGLE-VALUE )",
			NULL, NULL
	},
	{ "datamorph_lower_bound", "int", 2, 2, 0,
		ARG_BERVAL|ARG_MAGIC|DATAMORPH_INT_LOWER,
		datamorph_set_bounds,
		"( OLcfgCtAt:7.4 NAME 'olcDatamorphIntegerLowerBound' "
			"DESC 'Lowest valid value for the attribute' "
			"EQUALITY integerMatch "
			"SYNTAX OMsInteger "
			"SINGLE-VALUE )",
		NULL, NULL
	},
	{ "datamorph_upper_bound", "int", 2, 2, 0,
		ARG_BERVAL|ARG_MAGIC|DATAMORPH_INT_UPPER,
		datamorph_set_bounds,
		"( OLcfgCtAt:7.5 NAME 'olcDatamorphIntegerUpperBound' "
			"DESC 'Highest valid value for the attribute' "
			"EQUALITY integerMatch "
			"SYNTAX OMsInteger "
			"SINGLE-VALUE )",
		NULL, NULL
	},

	/* These have no equivalent in slapd.conf */
	{ "", NULL, 2, 2, 0,
		ARG_INT|ARG_MAGIC,
		datamorph_set_index,
		"( OLcfgCtAt:7.6 NAME 'olcDatamorphIndex' "
			"DESC 'Internal DB value' "
			"EQUALITY integerMatch "
			"SYNTAX OMsInteger "
			"SINGLE-VALUE )",
		NULL, NULL
	},
	{ "", NULL, 2, 2, 0,
		ARG_BERVAL|ARG_QUOTE|ARG_MAGIC,
		datamorph_set_value,
		"( OLcfgCtAt:7.7 NAME 'olcDatamorphValue' "
			"DESC 'Wire value' "
			"EQUALITY caseExactMatch "
			"SYNTAX OMsDirectoryString "
			"SINGLE-VALUE )",
		NULL, NULL
	},

	/* slapd.conf alternative for the two above */
	{ "datamorph_value", "int> <name", 3, 3, 0,
		ARG_QUOTE|ARG_MAGIC,
		datamorph_add_mapping,
		NULL, NULL, NULL
	},

	/* slapd.conf alternative for objectclasses below */
	{ "datamorph", "enum|int> <attr", 3, 3, 0,
		ARG_QUOTE|ARG_MAGIC,
		datamorph_add_transformation,
		NULL, NULL, NULL
	},

	{ NULL, NULL, 0, 0, 0, ARG_IGNORED }
};

static ConfigOCs datamorph_ocs[] = {
	{ "( OLcfgCtOc:7.1 "
		"NAME 'olcDatamorphConfig' "
		"DESC 'Datamorph overlay configuration' "
		"SUP olcOverlayConfig )",
		Cft_Overlay, datamorph_cfg, NULL, datamorph_cfadd },
	{ "( OLcfgCtOc:7.2 "
		"NAME 'olcTransformation' "
		"DESC 'Transformation configuration' "
		"MUST ( olcDatamorphAttribute ) "
		"SUP top "
		"ABSTRACT )",
		Cft_Misc, datamorph_cfg, NULL },
	{ "( OLcfgCtOc:7.3 "
		"NAME 'olcDatamorphEnum' "
		"DESC 'Configuration for an enumerated attribute' "
		"SUP olcTransformation "
		"STRUCTURAL )",
		Cft_Misc, datamorph_cfg, datamorph_ldadd_enum },
	{ "( OLcfgCtOc:7.4 "
		"NAME 'olcDatamorphInteger' "
		"DESC 'Configuration for a compact integer attribute' "
		"MUST ( olcDatamorphIntegerBytes ) "
		"MAY ( olcDatamorphIntegerLowerBound $ "
			"olcDatamorphIntegerUpperBound $ "
			"olcDatamorphIntegerSigned "
		") "
		"SUP olcTransformation "
		"STRUCTURAL )",
		Cft_Misc, datamorph_cfg, datamorph_ldadd_interval },
	{ "( OLcfgCtOc:7.5 "
		"NAME 'olcDatamorphEnumValue' "
		"DESC 'Configuration for an enumerated attribute' "
		"MUST ( olcDatamorphIndex $ "
			"olcDatamorphValue "
		") "
		"STRUCTURAL )",
		Cft_Misc, datamorph_cfg, datamorph_ldadd_mapping },

	{ NULL, 0, NULL }
};

static void
datamorph_mapping_free( void *arg )
{
	datamorph_enum_mapping *mapping = arg;

	ch_free( mapping->wire_value.bv_val );
	ch_free( mapping );
}

static void
datamorph_info_free( void *arg )
{
	transformation_info *info = arg;

	if ( info->type == DATAMORPH_ENUM ) {
		ldap_avl_free( info->ti_enum.to_db, datamorph_mapping_free );
	}
	ch_free( info );
}

static int
datamorph_set_attribute( ConfigArgs *ca )
{
	transformation_info needle = {}, *info = ca->ca_private;
	slap_overinst *on = (slap_overinst *)ca->bi;
	datamorph_info *ov = on->on_bi.bi_private;
	char *s = ca->value_string;
	const char *text;
	int rc = LDAP_SUCCESS;

	if ( ca->op == SLAP_CONFIG_EMIT ) {
		ca->value_string = info->attr->ad_cname.bv_val;
		return LDAP_SUCCESS;
	} else if ( ca->op == LDAP_MOD_DELETE ) {
		info = ldap_avl_delete( &ov->transformations, info,
				transformation_info_cmp );
		assert( info );

		info->attr = NULL;
		return LDAP_SUCCESS;
	}

	if ( *s == '{' ) {
		s = strchr( s, '}' );
		if ( !s ) {
			rc = LDAP_UNDEFINED_TYPE;
			goto done;
		}
		s += 1;
	}

	rc = slap_str2ad( s, &info->attr, &text );
	ch_free( ca->value_string );
	if ( rc ) {
		goto done;
	}

	/* The type has to be set appropriately */
	if ( !info->attr->ad_type->sat_syntax->ssyn_sups ||
			info->attr->ad_type->sat_syntax->ssyn_sups[0] !=
					datamorph_base_syntax ) {
		snprintf( ca->cr_msg, sizeof(ca->cr_msg),
				"improper syntax for attribute %s",
				info->attr->ad_cname.bv_val );
		Debug( LDAP_DEBUG_ANY, "%s: %s\n", ca->log, ca->cr_msg );
		rc = LDAP_CONSTRAINT_VIOLATION;
		goto done;
	}

	needle.attr = info->attr;
	if ( ldap_avl_find( ov->transformations, &needle, transformation_info_cmp ) ) {
		rc = LDAP_CONSTRAINT_VIOLATION;
		goto done;
	}

done:
	if ( rc ) {
		ca->reply.err = rc;
	}
	return rc;
}

static int
datamorph_set_size( ConfigArgs *ca )
{
	transformation_info *info = ca->ca_private;

	if ( !info ) {
		slap_overinst *on = (slap_overinst *)ca->bi;
		datamorph_info *ov = on->on_bi.bi_private;
		info = ov->wip_transformation;
		assert( ca->op == SLAP_CONFIG_ADD );
	}

	if ( ca->op == SLAP_CONFIG_EMIT ) {
		ca->value_int = info->ti_int.size;
		return LDAP_SUCCESS;
	} else if ( ca->op == LDAP_MOD_DELETE ) {
		info->ti_int.size = 0;
		return LDAP_SUCCESS;
	}

	if ( ca->value_int != 1 &&
			ca->value_int != 2 &&
			ca->value_int != 4 &&
			ca->value_int != 8 ) {
		snprintf( ca->cr_msg, sizeof(ca->cr_msg), "invalid size %d",
				ca->value_int );
		Debug( LDAP_DEBUG_ANY, "%s: %s\n", ca->log, ca->cr_msg );
		ca->reply.err = LDAP_CONSTRAINT_VIOLATION;
		return ca->reply.err;
	}
	info->ti_int.size = ca->value_int;

	return LDAP_SUCCESS;
}

static int
datamorph_set_signed( ConfigArgs *ca )
{
	transformation_info *info = ca->ca_private;

	if ( !info ) {
		slap_overinst *on = (slap_overinst *)ca->bi;
		datamorph_info *ov = on->on_bi.bi_private;
		info = ov->wip_transformation;
		assert( ca->op == SLAP_CONFIG_ADD );
	}

	if ( ca->op == SLAP_CONFIG_EMIT ) {
		ca->value_int = info->ti_int.flags & DATAMORPH_FLAG_SIGNED;
		return LDAP_SUCCESS;
	} else if ( ca->op == LDAP_MOD_DELETE ) {
		info->ti_int.flags &= ~DATAMORPH_FLAG_SIGNED;
		return LDAP_SUCCESS;
	}

	info->ti_int.flags &= ~DATAMORPH_FLAG_SIGNED;
	if ( ca->value_int ) {
		info->ti_int.flags |= DATAMORPH_FLAG_SIGNED;
	}

	return LDAP_SUCCESS;
}

static int
datamorph_set_bounds( ConfigArgs *ca )
{
	transformation_info *info = ca->ca_private;
	datamorph_interval_bound *bound;
	uint64_t unsigned_bound;
	int64_t signed_bound;
	char *ptr = ca->value_bv.bv_val + ca->value_bv.bv_len;
	int flag;

	if ( !info ) {
		slap_overinst *on = (slap_overinst *)ca->bi;
		datamorph_info *ov = on->on_bi.bi_private;
		info = ov->wip_transformation;
		assert( ca->op == SLAP_CONFIG_ADD );
	}

	switch ( ca->type ) {
		case DATAMORPH_INT_LOWER:
			bound = &info->ti_int.lower;
			flag = DATAMORPH_FLAG_LOWER;
			break;
		case DATAMORPH_INT_UPPER:
			bound = &info->ti_int.upper;
			flag = DATAMORPH_FLAG_UPPER;
			break;
		default:
			assert(0);
	}

	if ( ca->op == SLAP_CONFIG_EMIT ) {
		char buf[24];
		struct berval bv = { .bv_val = buf };

		if ( !(info->ti_int.flags & flag) ) {
			/* Bound not set, do not emit */
			return LDAP_SUCCESS;
		}
		if ( info->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
			bv.bv_len = sprintf( buf, "%" PRId64, bound->i );
		} else {
			bv.bv_len = sprintf( buf, "%" PRIu64, bound->u );
		}
		ber_dupbv_x( &ca->value_bv, &bv, ca->ca_op->o_tmpmemctx );

		return LDAP_SUCCESS;
	} else if ( ca->op == LDAP_MOD_DELETE ) {
		info->ti_int.flags &= ~flag;
		if ( info->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
			bound->i = (flag == DATAMORPH_FLAG_LOWER) ? INT64_MIN : INT64_MAX;
		} else {
			bound->u = (flag == DATAMORPH_FLAG_LOWER) ? 0 : UINT64_MAX;
		}
		return LDAP_SUCCESS;
	}

	/* FIXME: if attributes in the Add operation come in the wrong order
	 * (signed=true after the bound definition), we can't check the interval
	 * sanity. */
	/*
	if ( info->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
		signed_bound = strtoll( ca->value_bv.bv_val, &ptr, 10 );
	} else {
		unsigned_bound = strtoull( ca->value_bv.bv_val, &ptr, 10 );
	}
	*/
	/* Also, no idea what happens in the case of big-endian, hopefully,
	 * it behaves the same */
	unsigned_bound = strtoull( ca->value_bv.bv_val, &ptr, 10 );
	signed_bound = (int64_t)unsigned_bound;

	if ( *ca->value_bv.bv_val == '\0' || *ptr != '\0' ) {
		snprintf( ca->cr_msg, sizeof(ca->cr_msg),
				"failed to parse '%s' as integer",
				ca->value_bv.bv_val );
		Debug( LDAP_DEBUG_ANY, "%s: %s\n", ca->log, ca->cr_msg );
		ca->reply.err = LDAP_CONSTRAINT_VIOLATION;
		return ca->reply.err;
	}
	ch_free( ca->value_bv.bv_val );

	info->ti_int.flags |= flag;
	switch ( info->ti_int.size ) {
		case 1:
			if ( info->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
				/* See FIXME above
				if ( signed_bound < INT8_MIN || signed_bound > INT8_MAX ) {
					goto fail;
				}
				*/
			} else {
				/* See FIXME above
				if ( unsigned_bound > UINT8_MAX ) {
					goto fail;
				}
				*/
			}
			break;
		case 2:
			if ( info->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
				/* See FIXME above
				if ( signed_bound < INT16_MIN || signed_bound > INT16_MAX ) {
					goto fail;
				}
				*/
			} else {
				/* See FIXME above
				if ( unsigned_bound > UINT16_MAX ) {
					goto fail;
				}
				*/
			}
			break;
		case 4:
			if ( info->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
				/* See FIXME above
				if ( signed_bound < INT32_MIN || signed_bound > INT32_MAX ) {
					goto fail;
				}
				*/
			} else {
				/* See FIXME above
				if ( unsigned_bound > UINT32_MAX ) {
					goto fail;
				}
				*/
			}
			break;
		case 8:
			break;
		default:
			/* Should only happen in these two cases:
			 * 1. datamorph_size not yet encountered for this one (when
			 *    processing slapd.conf)
			 * 2. When someone runs a fun modification on the config entry
			 *    messing with more attributes at once
			 *
			 * The error message is expected to be helpful only for the former,
			 * so use the slapd.conf name.
			 */
			snprintf( ca->cr_msg, sizeof(ca->cr_msg),
					"datamorph_size has to be set first!" );
			Debug( LDAP_DEBUG_ANY, "%s: %s\n", ca->log, ca->cr_msg );
			ca->reply.err = LDAP_CONSTRAINT_VIOLATION;
			return ca->reply.err;
	}
	if ( info->ti_int.flags & DATAMORPH_FLAG_SIGNED ) {
		bound->i = signed_bound;
	} else {
		bound->u = unsigned_bound;
	}

	return LDAP_SUCCESS;
}

static int
datamorph_set_value( ConfigArgs *ca )
{
	datamorph_enum_mapping *mapping = ca->ca_private;
	char *s = ca->value_bv.bv_val;

	if ( ca->op == SLAP_CONFIG_EMIT ) {
		/* We generate the value as part of the RDN, don't add anything */
		return LDAP_SUCCESS;
	} else if ( ca->op == LDAP_MOD_DELETE ) {
		ch_free( mapping->wire_value.bv_val );
		BER_BVZERO( &mapping->wire_value );
		/* TODO: remove from info->ti_enum.to_db? */
		return LDAP_SUCCESS;
	}

	/* As long as this attribute can be in the RDN,
	 * we have to expect the '{n}' prefix */
	if ( *s == '{' ) {
		ber_len_t len;
		s = memchr( s, '}', ca->value_bv.bv_len );
		if ( !s ) {
			ca->reply.err = LDAP_UNDEFINED_TYPE;
			return ca->reply.err;
		}
		s += 1;

		len = ca->value_bv.bv_len - ( s - ca->value_bv.bv_val );
		ber_str2bv( s, len, 1, &mapping->wire_value );
		ch_free( ca->value_bv.bv_val );
	} else {
		mapping->wire_value = ca->value_bv;
	}

	return LDAP_SUCCESS;
}

static int
datamorph_set_index( ConfigArgs *ca )
{
	datamorph_enum_mapping *mapping = ca->ca_private;
	struct berval *from_db = mapping->transformation->ti_enum.from_db;

	if ( ca->op == SLAP_CONFIG_EMIT ) {
		ca->value_int = mapping->db_value;
		return LDAP_SUCCESS;
	} else if ( ca->op == LDAP_MOD_DELETE ) {
		BER_BVZERO( &from_db[mapping->db_value] );
		return LDAP_SUCCESS;
	}

	if ( ca->value_int < 0 || ca->value_int >= 256 ) {
		ca->reply.err = LDAP_CONSTRAINT_VIOLATION;
		return ca->reply.err;
	} else if ( !BER_BVISNULL( &from_db[ca->value_int] ) ) {
		snprintf( ca->cr_msg, sizeof(ca->cr_msg), "duplicate index %d",
				ca->value_int );
		Debug( LDAP_DEBUG_ANY, "%s: %s\n", ca->log, ca->cr_msg );
		ca->reply.err = LDAP_CONSTRAINT_VIOLATION;
		return ca->reply.err;
	}
	mapping->db_value = ca->value_int;
	from_db[ca->value_int] = mapping->wire_value;

	return LDAP_SUCCESS;
}

/* Called when processing slapd.conf only,
 * cn=config uses the objectclass to decide which type we're dealing with.
 */
static int
datamorph_add_transformation( ConfigArgs *ca )
{
	slap_overinst *on = (slap_overinst *)ca->bi;
	datamorph_info *ov = on->on_bi.bi_private;
	transformation_info *info;

	if ( ov->wip_transformation ) {
		/* We checked everything as were processing the lines */
		int rc = ldap_avl_insert( &ov->transformations, ov->wip_transformation,
				transformation_info_cmp, ldap_avl_dup_error );
		assert( rc == LDAP_SUCCESS );
	}

	info = ch_calloc( 1, sizeof(transformation_info) );
	ov->wip_transformation = ca->ca_private = info;

	if ( !strcasecmp( ca->argv[1], "enum" ) ) {
		info->type = DATAMORPH_ENUM;
	} else if ( !strcasecmp( ca->argv[1], "int" ) ) {
		info->type = DATAMORPH_INT;
	} else {
		snprintf( ca->cr_msg, sizeof(ca->cr_msg),
				"unknown transformation type '%s'", ca->argv[1] );
		Debug( LDAP_DEBUG_ANY, "%s: %s\n", ca->log, ca->cr_msg );
		ca->reply.err = LDAP_CONSTRAINT_VIOLATION;
		return ca->reply.err;
	}

	ca->value_string = strdup( ca->argv[2] );

	return datamorph_set_attribute( ca );
}

static int
datamorph_add_mapping( ConfigArgs *ca )
{
	slap_overinst *on = (slap_overinst *)ca->bi;
	datamorph_info *ov = on->on_bi.bi_private;
	transformation_info *info = ov->wip_transformation;
	datamorph_enum_mapping *mapping;
	int rc = LDAP_CONSTRAINT_VIOLATION;

	if ( !info ) {
		snprintf( ca->cr_msg, sizeof(ca->cr_msg), "no attribute configured" );
		Debug( LDAP_DEBUG_ANY, "%s: %s\n", ca->log, ca->cr_msg );
		goto done;
	}

	mapping = ch_calloc( 1, sizeof(datamorph_enum_mapping) );
	mapping->transformation = info;
	ca->ca_private = mapping;

	ber_str2bv( ca->argv[2], 0, 1, &ca->value_bv );
	rc = datamorph_set_value( ca );
	if ( rc != LDAP_SUCCESS ) {
		goto done;
	}

	rc = lutil_atoix( &ca->value_int, ca->argv[1], 0 );
	if ( rc != LDAP_SUCCESS ) {
		snprintf( ca->cr_msg, sizeof(ca->cr_msg), "invalid integer %s",
				ca->argv[1] );
		Debug( LDAP_DEBUG_ANY, "%s: %s\n", ca->log, ca->cr_msg );
		goto done;
	}

	rc = datamorph_set_index( ca );
	if ( rc != LDAP_SUCCESS ) {
		goto done;
	}

done:
	if ( rc == LDAP_SUCCESS ) {
		rc = ldap_avl_insert( &info->ti_enum.to_db, mapping,
				transformation_mapping_cmp, ldap_avl_dup_error );
	}
	if ( rc ) {
		ca->reply.err = rc;
	}

	return rc;
}

static int
datamorph_ldadd_info_cleanup( ConfigArgs *ca )
{
	slap_overinst *on = (slap_overinst *)ca->bi;
	datamorph_info *ov = on->on_bi.bi_private;
	transformation_info *info = ca->ca_private;

	if ( ca->reply.err != LDAP_SUCCESS ) {
		/* Not reached since cleanup is only called on success */
fail:
		ch_free( info );
		return LDAP_SUCCESS;
	}

	if ( ldap_avl_insert( &ov->transformations, info, transformation_info_cmp,
			ldap_avl_dup_error ) ) {
		goto fail;
	}
	return LDAP_SUCCESS;
}

static int
datamorph_ldadd_transformation(
		CfEntryInfo *cei,
		Entry *e,
		ConfigArgs *ca,
		datamorph_type type )
{
	transformation_info *info;

	if ( cei->ce_type != Cft_Overlay || !cei->ce_bi ||
			cei->ce_bi->bi_cf_ocs != datamorph_ocs )
		return LDAP_CONSTRAINT_VIOLATION;

	info = ch_calloc( 1, sizeof(transformation_info) );
	info->type = type;

	ca->bi = cei->ce_bi;
	ca->ca_private = info;
	config_push_cleanup( ca, datamorph_ldadd_info_cleanup );
	/* config_push_cleanup is only run in the case of online config but we use it to
	 * enable the new config when done with the entry */
	ca->lineno = 0;

	return LDAP_SUCCESS;
}

static int
datamorph_ldadd_enum( CfEntryInfo *cei, Entry *e, ConfigArgs *ca )
{
	return datamorph_ldadd_transformation( cei, e, ca, DATAMORPH_ENUM );
}

static int
datamorph_ldadd_interval( CfEntryInfo *cei, Entry *e, ConfigArgs *ca )
{
	return datamorph_ldadd_transformation( cei, e, ca, DATAMORPH_INT );
}

static int
datamorph_ldadd_mapping_cleanup( ConfigArgs *ca )
{
	datamorph_enum_mapping *mapping = ca->ca_private;
	transformation_info *info = mapping->transformation;

	if ( ca->reply.err != LDAP_SUCCESS ) {
		/* Not reached since cleanup is only called on success */
fail:
		datamorph_mapping_free( mapping );
		return LDAP_SUCCESS;
	}

	if ( ldap_avl_insert( &info->ti_enum.to_db, mapping, transformation_mapping_cmp,
			ldap_avl_dup_error ) ) {
		goto fail;
	}
	info->ti_enum.from_db[mapping->db_value] = mapping->wire_value;

	return LDAP_SUCCESS;
}

static int
datamorph_ldadd_mapping( CfEntryInfo *cei, Entry *e, ConfigArgs *ca )
{
	transformation_info *info;
	datamorph_enum_mapping *mapping;
	CfEntryInfo *parent = cei->ce_parent;

	if ( cei->ce_type != Cft_Misc || !parent || !parent->ce_bi ||
			parent->ce_bi->bi_cf_ocs != datamorph_ocs )
		return LDAP_CONSTRAINT_VIOLATION;

	info = cei->ce_private;

	mapping = ch_calloc( 1, sizeof(datamorph_enum_mapping) );
	mapping->transformation = info;

	ca->ca_private = mapping;
	config_push_cleanup( ca, datamorph_ldadd_mapping_cleanup );
	/* config_push_cleanup is only run in the case of online config but we use it to
	 * enable the new config when done with the entry */
	ca->lineno = 0;

	return LDAP_SUCCESS;
}

struct datamorph_cfadd_args {
	Operation *op;
	SlapReply *rs;
	Entry *p;
	ConfigArgs *ca;
	int index;
};

static int
datamorph_config_build_enum( void *item, void *arg )
{
	datamorph_enum_mapping *mapping = item;
	struct datamorph_cfadd_args *args = arg;
	struct berval rdn;
	Entry *e;
	char *p;
	ber_len_t index;

	rdn.bv_len = snprintf( args->ca->cr_msg, sizeof(args->ca->cr_msg),
			"olcDatamorphValue={%d}", args->index++ );
	rdn.bv_val = args->ca->cr_msg;
	p = rdn.bv_val + rdn.bv_len;

	rdn.bv_len += mapping->wire_value.bv_len;
	for ( index = 0; index < mapping->wire_value.bv_len; index++ ) {
		if ( RDN_NEEDSESCAPE(mapping->wire_value.bv_val[index]) ) {
			rdn.bv_len++;
			*p++ = '\\';
		}
		*p++ = mapping->wire_value.bv_val[index];
	}
	*p = '\0';

	args->ca->ca_private = mapping;
	args->ca->ca_op = args->op;
	e = config_build_entry( args->op, args->rs, args->p->e_private, args->ca,
			&rdn, &datamorph_ocs[4], NULL );
	assert( e );

	return LDAP_SUCCESS;
}

static int
datamorph_config_build_attr( void *item, void *arg )
{
	transformation_info *info = item;
	struct datamorph_cfadd_args *args = arg;
	struct berval rdn;
	ConfigOCs *oc;
	Entry *e;

	rdn.bv_len = snprintf( args->ca->cr_msg, sizeof(args->ca->cr_msg),
			"olcDatamorphAttribute={%d}%s", args->index++,
			info->attr->ad_cname.bv_val );
	rdn.bv_val = args->ca->cr_msg;

	switch ( info->type ) {
		case DATAMORPH_ENUM:
			oc = &datamorph_ocs[2];
			break;
		case DATAMORPH_INT:
			oc = &datamorph_ocs[3];
			break;
		default:
			assert(0);
			break;
	}

	args->ca->ca_private = info;
	args->ca->ca_op = args->op;
	e = config_build_entry(
			args->op, args->rs, args->p->e_private, args->ca, &rdn, oc, NULL );
	assert( e );

	if ( info->type == DATAMORPH_ENUM ) {
		struct datamorph_cfadd_args new_args = *args;
		new_args.p = e;
		new_args.index = 0;

		return ldap_avl_apply( info->ti_enum.to_db, datamorph_config_build_enum,
				&new_args, 1, AVL_PREORDER );
	}

	return LDAP_SUCCESS;
}

static int
datamorph_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
{
	slap_overinst *on = (slap_overinst *)ca->bi;
	datamorph_info *ov = on->on_bi.bi_private;
	struct datamorph_cfadd_args args = {
			.op = op,
			.rs = rs,
			.p = p,
			.ca = ca,
			.index = 0,
	};

	if ( ov->wip_transformation ) {
		/* There is one last item that is unfinished */
		int rc = ldap_avl_insert( &ov->transformations, ov->wip_transformation,
				transformation_info_cmp, ldap_avl_dup_error );
		assert( rc == LDAP_SUCCESS );
	}

	return ldap_avl_apply( ov->transformations, &datamorph_config_build_attr, &args,
			1, AVL_PREORDER );
}

static slap_overinst datamorph;

static int
datamorph_db_init( BackendDB *be, ConfigReply *cr )
{
	slap_overinst *on = (slap_overinst *)be->bd_info;
	datamorph_info *ov;

	/* TODO: can this be global? */
	if ( SLAP_ISGLOBALOVERLAY(be) ) {
		Debug( LDAP_DEBUG_ANY, "datamorph overlay must be instantiated "
				"within a database.\n" );
		return 1;
	}

	ov = ch_calloc( 1, sizeof(datamorph_info) );
	on->on_bi.bi_private = ov;

	return LDAP_SUCCESS;
}

static int
datamorph_db_destroy( BackendDB *be, ConfigReply *cr )
{
	slap_overinst *on = (slap_overinst *)be->bd_info;
	datamorph_info *ov = on->on_bi.bi_private;

	if ( ov ) {
		ldap_avl_free( ov->transformations, datamorph_info_free );
	}
	ch_free( ov );

	return LDAP_SUCCESS;
}

int
datamorph_initialize()
{
	int rc, i;

	datamorph.on_bi.bi_type = "datamorph";
	datamorph.on_bi.bi_db_init = datamorph_db_init;
	datamorph.on_bi.bi_db_destroy = datamorph_db_destroy;

	datamorph.on_bi.bi_op_add = datamorph_op_add;
	datamorph.on_bi.bi_op_compare = datamorph_op_compare;
	datamorph.on_bi.bi_op_modify = datamorph_op_mod;
	datamorph.on_bi.bi_op_modrdn = datamorph_op_modrdn;
	datamorph.on_bi.bi_op_search = datamorph_op_search;
	datamorph.on_response = datamorph_response;

	datamorph.on_bi.bi_entry_release_rw = datamorph_entry_release_rw;
	datamorph.on_bi.bi_entry_get_rw = datamorph_entry_get_rw;

	datamorph.on_bi.bi_cf_ocs = datamorph_ocs;

	for ( i = 0; datamorph_syntax_defs[i].sd_desc != NULL; i++ ) {
		rc = register_syntax( &datamorph_syntax_defs[i] );

		if ( rc ) {
			Debug( LDAP_DEBUG_ANY, "datamorph_initialize: "
					"error registering syntax %s\n",
					datamorph_syntax_defs[i].sd_desc );
			return rc;
		}
	}

	datamorph_base_syntax = syn_find( DATAMORPH_SYNTAX_BASE );
	assert( datamorph_base_syntax );

	for ( i = 0; datamorph_mrule_defs[i].mrd_desc != NULL; i++ ) {
		rc = register_matching_rule( &datamorph_mrule_defs[i] );

		if ( rc ) {
			Debug( LDAP_DEBUG_ANY, "datamorph_initialize: "
					"error registering matching rule %s\n",
					datamorph_mrule_defs[i].mrd_desc );
			return rc;
		}
	}

	rc = config_register_schema( datamorph_cfg, datamorph_ocs );
	if ( rc ) return rc;

	return overlay_register( &datamorph );
}

#if SLAPD_OVER_DATAMORPH == SLAPD_MOD_DYNAMIC
int
init_module( int argc, char *argv[] )
{
	return datamorph_initialize();
}
#endif

#endif /* SLAPD_OVER_DATAMORPH */