Mercurial > projects > libpws
annotate pws-field.c @ 12:1926dfc9feb0 version-1.0.0
Release version 1.0.0
author | Guido Berhoerster <guido+libpws@berhoerster.name> |
---|---|
date | Sun, 04 Aug 2019 21:36:05 +0200 |
parents | 96a507a110c8 |
children |
rev | line source |
---|---|
0
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1 /* |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
2 * Copyright (C) 2015 Guido Berhoerster <guido+libpws@berhoerster.name> |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
3 * |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
4 * Permission is hereby granted, free of charge, to any person obtaining |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
5 * a copy of this software and associated documentation files (the |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
6 * "Software"), to deal in the Software without restriction, including |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
7 * without limitation the rights to use, copy, modify, merge, publish, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
8 * distribute, sublicense, and/or sell copies of the Software, and to |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
9 * permit persons to whom the Software is furnished to do so, subject to |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
10 * the following conditions: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
11 * |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
12 * The above copyright notice and this permission notice shall be included |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
13 * in all copies or substantial portions of the Software. |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
14 * |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
22 */ |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
23 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
24 #include "compat.h" |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
25 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
26 #include <stdlib.h> |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
27 #include <string.h> |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
28 #include <limits.h> |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
29 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
30 #include "pws-internal.h" |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
31 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
32 static const enum pws_data_type header_data_types[256] = { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
33 [PWS3_HEADER_FIELD_VERSION] = PWS_DATA_TYPE_UINT16, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
34 [PWS3_HEADER_FIELD_UUID] = PWS_DATA_TYPE_UUID, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
35 [PWS3_HEADER_FIELD_NON_DEFAULT_PREFERENCES] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
36 [PWS3_HEADER_FIELD_TREE_DISPLAY_STATUS] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
37 [PWS3_HEADER_FIELD_SAVE_TIMESTAMP] = PWS_DATA_TYPE_TIME, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
38 [PWS3_HEADER_FIELD_SAVE_USER_HOST] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
39 [PWS3_HEADER_FIELD_SAVE_APPLICATION] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
40 [PWS3_HEADER_FIELD_SAVE_USER] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
41 [PWS3_HEADER_FIELD_SAVE_HOST] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
42 [PWS3_HEADER_FIELD_DATABASE_NAME] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
43 [PWS3_HEADER_FIELD_DATABASE_DESCRIPTION] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
44 [PWS3_HEADER_FIELD_DATABASE_FILTERS] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
45 [PWS3_HEADER_FIELD_RESERVED_1] = PWS_DATA_TYPE_BYTES, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
46 [PWS3_HEADER_FIELD_RESERVED_2] = PWS_DATA_TYPE_BYTES, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
47 [PWS3_HEADER_FIELD_RESERVED_3] = PWS_DATA_TYPE_BYTES, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
48 [PWS3_HEADER_FIELD_RECENTLY_USED_ENTRIES] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
49 [PWS3_HEADER_FIELD_NAMED_PASSWORD_POLICIES] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
50 [PWS3_HEADER_FIELD_EMPTY_GROUPS] = PWS_DATA_TYPE_TEXT, |
11
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
51 [PWS3_HEADER_FIELD_YUBICO] = PWS_DATA_TYPE_TEXT, |
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
52 [PWS3_HEADER_FIELD_PASSWORD_TIMESTAMP] = PWS_DATA_TYPE_TIME |
0
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
53 }; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
54 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
55 static const enum pws_data_type record_data_types[256] = { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
56 [PWS3_RECORD_FIELD_UUID] = PWS_DATA_TYPE_UUID, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
57 [PWS3_RECORD_FIELD_GROUP] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
58 [PWS3_RECORD_FIELD_TITLE] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
59 [PWS3_RECORD_FIELD_USERNAME] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
60 [PWS3_RECORD_FIELD_NOTES] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
61 [PWS3_RECORD_FIELD_PASSWORD] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
62 [PWS3_RECORD_FIELD_CREATION_TIME] = PWS_DATA_TYPE_TIME, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
63 [PWS3_RECORD_FIELD_PASSWORD_MODIFICATION_TIME] = PWS_DATA_TYPE_TIME, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
64 [PWS3_RECORD_FIELD_ACCESS_TIME] = PWS_DATA_TYPE_TIME, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
65 [PWS3_RECORD_FIELD_PASSWORD_EXPIRY_TIME] = PWS_DATA_TYPE_TIME, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
66 [PWS3_RECORD_FIELD_RESERVED_1] = PWS_DATA_TYPE_BYTES, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
67 [PWS3_RECORD_FIELD_MODIFICATION_TIME] = PWS_DATA_TYPE_TIME, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
68 [PWS3_RECORD_FIELD_URL] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
69 [PWS3_RECORD_FIELD_AUTOTYPE] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
70 [PWS3_RECORD_FIELD_PASSWORD_HISTORY] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
71 [PWS3_RECORD_FIELD_PASSWORD_POLICY] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
72 [PWS3_RECORD_FIELD_PASSWORD_EXPIRY_INTERVAL] = PWS_DATA_TYPE_UINT32, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
73 [PWS3_RECORD_FIELD_RUN_COMMAND] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
74 [PWS3_RECORD_FIELD_DOUBLE_CLICK_ACTION] = PWS_DATA_TYPE_BYTES, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
75 [PWS3_RECORD_FIELD_EMAIL_ADDRESS] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
76 [PWS3_RECORD_FIELD_PROTECTED] = PWS_DATA_TYPE_UINT8, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
77 [PWS3_RECORD_FIELD_ALLOWED_PASSWORD_SYMBOLS] = PWS_DATA_TYPE_TEXT, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
78 [PWS3_RECORD_FIELD_SHIFT_DOUBLE_CLICK_ACTION] = PWS_DATA_TYPE_BYTES, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
79 [PWS3_RECORD_FIELD_PASSWORD_POLICY_NAME] = PWS_DATA_TYPE_TEXT, |
11
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
80 [PWS3_RECORD_FIELD_KEYBOARD_SHORTCUT] = PWS_DATA_TYPE_BYTES, |
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
81 [PWS3_RECORD_FIELD_RESERVED_2] = PWS_DATA_TYPE_UUID, |
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
82 [PWS3_RECORD_FIELD_TWO_FACTOR_KEY] = PWS_DATA_TYPE_BYTES, |
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
83 [PWS3_RECORD_FIELD_CREDIT_CARD_NUMBER] = PWS_DATA_TYPE_TEXT, |
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
84 [PWS3_RECORD_FIELD_CREDIT_CARD_EXPIRATION] = PWS_DATA_TYPE_TEXT, |
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
85 [PWS3_RECORD_FIELD_CREDIT_CARD_VERIFICATION_VALUE] = PWS_DATA_TYPE_TEXT, |
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
86 [PWS3_RECORD_FIELD_CREDIT_CARD_PIN] = PWS_DATA_TYPE_TEXT, |
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
87 [PWS3_RECORD_FIELD_QR_CODE] = PWS_DATA_TYPE_TEXT, |
96a507a110c8
Add support for format 0x030E
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
6
diff
changeset
|
88 [PWS3_RECORD_FIELD_TESTING_1] = PWS_DATA_TYPE_BYTES |
0
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
89 }; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
90 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
91 struct pws3_field * |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
92 pws3_field_create(int is_header, uint8_t field_type) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
93 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
94 struct pws3_field *field; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
95 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
96 field = pws_alloc(sizeof (struct pws3_field)); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
97 if (field == NULL) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
98 return (NULL); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
99 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
100 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
101 field->is_header = is_header; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
102 field->field_type = field_type; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
103 field->size = 0; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
104 switch (pws3_field_get_data_type(field)) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
105 case PWS_DATA_TYPE_BYTES: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
106 field->value.bytes = NULL; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
107 break; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
108 case PWS_DATA_TYPE_UUID: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
109 memset(field->value.uuid, 0, PWS3_UUID_SIZE); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
110 break; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
111 case PWS_DATA_TYPE_TEXT: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
112 field->value.text = NULL; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
113 break; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
114 case PWS_DATA_TYPE_UINT8: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
115 field->value.uint8 = 0; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
116 break; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
117 case PWS_DATA_TYPE_UINT16: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
118 field->value.uint16 = 0; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
119 break; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
120 case PWS_DATA_TYPE_TIME: /* FALLTHROUGH */ |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
121 case PWS_DATA_TYPE_UINT32: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
122 field->value.uint32 = 0; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
123 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
124 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
125 return (field); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
126 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
127 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
128 void |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
129 pws3_field_destroy(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
130 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
131 if (field == NULL) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
132 return; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
133 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
134 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
135 switch (pws3_field_get_data_type(field)) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
136 case PWS_DATA_TYPE_BYTES: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
137 pws_free(field->value.bytes, field->size); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
138 break; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
139 case PWS_DATA_TYPE_TEXT: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
140 if (!field->is_header && |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
141 (field->field_type == PWS3_RECORD_FIELD_PASSWORD)) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
142 pws_secure_free(field->value.text, field->size + 1); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
143 } else { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
144 pws_free(field->value.text, field->size + 1); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
145 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
146 break; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
147 default: |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
148 break; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
149 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
150 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
151 pws_free(field, sizeof (struct pws3_field)); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
152 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
153 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
154 int |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
155 pws3_field_is_header(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
156 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
157 return (field->is_header); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
158 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
159 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
160 uint8_t |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
161 pws3_field_get_type(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
162 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
163 return (field->field_type); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
164 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
165 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
166 enum pws_data_type |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
167 pws3_field_get_data_type(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
168 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
169 return (field->is_header ? header_data_types[field->field_type] : |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
170 record_data_types[field->field_type]); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
171 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
172 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
173 int |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
174 pws3_field_set_uuid(struct pws3_field *field, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
175 const unsigned char s[static PWS3_UUID_SIZE]) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
176 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
177 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UUID); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
178 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
179 field->size = PWS3_UUID_SIZE; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
180 memcpy(field->value.uuid, s, PWS3_UUID_SIZE); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
181 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
182 return (0); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
183 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
184 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
185 int |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
186 pws3_field_set_text(struct pws3_field *field, const char s[static 1]) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
187 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
188 size_t len; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
189 char *t; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
190 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
191 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_TEXT); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
192 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
193 len = strlen(s); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
194 if (len > PWS3_MAX_FIELD_SIZE) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
195 return (-1); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
196 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
197 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
198 if (!field->is_header && |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
199 (field->field_type == PWS3_RECORD_FIELD_PASSWORD)) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
200 if (len > PWS3_MAX_PASSWORD_LEN) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
201 return (-1); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
202 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
203 t = pws_secure_realloc(field->value.text, len + 1); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
204 } else { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
205 t = pws_realloc(field->value.text, len + 1); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
206 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
207 if (t == NULL) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
208 return (-1); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
209 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
210 field->value.text = t; |
6
1c0e7f79e737
Fix incorrect field size for text fields
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
0
diff
changeset
|
211 field->size = len; |
1c0e7f79e737
Fix incorrect field size for text fields
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
0
diff
changeset
|
212 memcpy(field->value.text, s, len + 1); |
0
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
213 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
214 return (0); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
215 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
216 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
217 int |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
218 pws3_field_set_time(struct pws3_field *field, time_t time) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
219 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
220 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_TIME); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
221 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
222 field->size = 4; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
223 field->value.uint32 = CLAMP(time, 0, UINT32_MAX); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
224 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
225 return (0); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
226 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
227 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
228 int |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
229 pws3_field_set_uint8(struct pws3_field *field, uint8_t u8) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
230 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
231 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT8); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
232 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
233 field->size = 1; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
234 field->value.uint8 = u8; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
235 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
236 return (0); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
237 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
238 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
239 int |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
240 pws3_field_set_uint16(struct pws3_field *field, uint16_t u16) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
241 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
242 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT16); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
243 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
244 field->size = 2; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
245 field->value.uint16 = u16; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
246 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
247 return (0); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
248 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
249 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
250 int |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
251 pws3_field_set_uint32(struct pws3_field *field, uint32_t u32) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
252 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
253 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT32); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
254 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
255 field->size = 4; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
256 field->value.uint32 = u32; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
257 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
258 return (0); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
259 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
260 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
261 int |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
262 pws3_field_set_bytes(struct pws3_field *field, const unsigned char s[static 1], |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
263 size_t n) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
264 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
265 unsigned char *t; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
266 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
267 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_BYTES); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
268 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
269 if (n > PWS3_MAX_FIELD_SIZE) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
270 return (-1); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
271 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
272 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
273 t = pws_realloc(field->value.bytes, n); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
274 if (t == NULL) { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
275 return (-1); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
276 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
277 field->size = n; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
278 field->value.bytes = t; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
279 memcpy(t, s, n); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
280 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
281 return (0); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
282 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
283 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
284 const unsigned char * |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
285 pws3_field_get_uuid(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
286 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
287 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UUID); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
288 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
289 return (field->value.uuid); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
290 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
291 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
292 const char * |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
293 pws3_field_get_text(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
294 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
295 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_TEXT); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
296 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
297 return (field->value.text); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
298 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
299 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
300 time_t |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
301 pws3_field_get_time(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
302 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
303 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_TIME); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
304 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
305 /* assume time_t can hold at least a INT32_MAX */ |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
306 return ((time_t)CLAMP(field->value.uint32, 0, INT32_MAX)); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
307 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
308 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
309 uint8_t |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
310 pws3_field_get_uint8(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
311 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
312 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT8); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
313 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
314 return (field->value.uint8); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
315 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
316 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
317 uint16_t |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
318 pws3_field_get_uint16(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
319 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
320 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT16); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
321 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
322 return (field->value.uint16); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
323 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
324 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
325 uint32_t |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
326 pws3_field_get_uint32(struct pws3_field *field) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
327 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
328 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT32); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
329 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
330 return (field->value.uint32); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
331 } |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
332 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
333 void |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
334 pws3_field_get_bytes(struct pws3_field *field, const unsigned char **sp, |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
335 size_t *np) |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
336 { |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
337 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_BYTES); |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
338 |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
339 *sp = field->value.bytes; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
340 *np = field->size; |
d541e748cfd8
Initial revision
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
341 } |