Changeset 5f4cf872 in mainline
- Timestamp:
- 2012-07-18T22:43:55Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ff788c3
- Parents:
- ae49252
- Location:
- uspace/app/bithenge
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/linux/os.h
rae49252 r5f4cf872 100 100 } 101 101 102 static inline uint16_t uint16_t_le2host(uint16_t val) 103 { 104 return le16toh(val); 105 } 106 107 static inline uint16_t uint16_t_be2host(uint16_t val) 108 { 109 return be16toh(val); 110 } 111 102 112 static inline uint32_t uint32_t_le2host(uint32_t val) 103 113 { … … 110 120 } 111 121 122 static inline uint64_t uint64_t_le2host(uint64_t val) 123 { 124 return le64toh(val); 125 } 126 127 static inline uint64_t uint64_t_be2host(uint64_t val) 128 { 129 return be64toh(val); 130 } 131 112 132 #endif -
uspace/app/bithenge/transform.c
rae49252 r5f4cf872 102 102 }; 103 103 104 static int uint32le_apply(bithenge_transform_t *self, bithenge_node_t *in, 105 bithenge_node_t **out) 106 { 107 int rc; 108 if (bithenge_node_type(in) != BITHENGE_NODE_BLOB) 109 return EINVAL; 110 bithenge_blob_t *blob = bithenge_node_as_blob(in); 111 112 // Try to read 5 bytes and fail if the blob is too long. 113 uint32_t val[2]; 114 aoff64_t size = sizeof(val[0]) + 1; 115 rc = bithenge_blob_read(blob, 0, (char *)val, &size); 116 if (rc != EOK) 117 return rc; 118 if (size != 4) 119 return EINVAL; 120 121 return bithenge_new_integer_node(out, uint32_t_le2host(val[0])); 122 } 123 124 static int uint32be_apply(bithenge_transform_t *self, bithenge_node_t *in, 125 bithenge_node_t **out) 126 { 127 int rc; 128 if (bithenge_node_type(in) != BITHENGE_NODE_BLOB) 129 return EINVAL; 130 bithenge_blob_t *blob = bithenge_node_as_blob(in); 131 132 // Try to read 5 bytes and fail if the blob is too long. 133 uint32_t val[2]; 134 aoff64_t size = sizeof(val[0]) + 1; 135 rc = bithenge_blob_read(blob, 0, (char *)val, &size); 136 if (rc != EOK) 137 return rc; 138 if (size != 4) 139 return EINVAL; 140 141 return bithenge_new_integer_node(out, uint32_t_be2host(val[0])); 104 static int prefix_length_1(bithenge_transform_t *self, bithenge_blob_t *blob, 105 aoff64_t *out) 106 { 107 *out = 1; 108 return EOK; 109 } 110 111 static int prefix_length_2(bithenge_transform_t *self, bithenge_blob_t *blob, 112 aoff64_t *out) 113 { 114 *out = 2; 115 return EOK; 142 116 } 143 117 … … 149 123 } 150 124 151 static const bithenge_transform_ops_t uint32le_ops = { 152 .apply = uint32le_apply, 153 .prefix_length = prefix_length_4, 154 .destroy = transform_indestructible, 155 }; 156 157 static const bithenge_transform_ops_t uint32be_ops = { 158 .apply = uint32be_apply, 159 .prefix_length = prefix_length_4, 160 .destroy = transform_indestructible, 161 }; 162 163 /** The little-endian 32-bit unsigned integer transform. */ 164 bithenge_transform_t bithenge_uint32le_transform = { 165 &uint32le_ops, 1 166 }; 167 168 /** The big-endian 32-bit unsigned integer transform. */ 169 bithenge_transform_t bithenge_uint32be_transform = { 170 &uint32be_ops, 1 171 }; 125 static int prefix_length_8(bithenge_transform_t *self, bithenge_blob_t *blob, 126 aoff64_t *out) 127 { 128 *out = 8; 129 return EOK; 130 } 131 132 #define MAKE_UINT_TRANSFORM(NAME, TYPE, ENDIAN, PREFIX_LENGTH_FUNC) \ 133 static int NAME##_apply(bithenge_transform_t *self, \ 134 bithenge_node_t *in, bithenge_node_t **out) \ 135 { \ 136 int rc; \ 137 if (bithenge_node_type(in) != BITHENGE_NODE_BLOB) \ 138 return EINVAL; \ 139 bithenge_blob_t *blob = bithenge_node_as_blob(in); \ 140 \ 141 /* Read too many bytes; success means the blob is too long. */ \ 142 TYPE val[2]; \ 143 aoff64_t size = sizeof(val[0]) + 1; \ 144 rc = bithenge_blob_read(blob, 0, (char *)val, &size); \ 145 if (rc != EOK) \ 146 return rc; \ 147 if (size != sizeof(val[0])) \ 148 return EINVAL; \ 149 \ 150 return bithenge_new_integer_node(out, ENDIAN(val[0])); \ 151 } \ 152 \ 153 static const bithenge_transform_ops_t NAME##_ops = { \ 154 .apply = NAME##_apply, \ 155 .prefix_length = PREFIX_LENGTH_FUNC, \ 156 .destroy = transform_indestructible, \ 157 }; \ 158 \ 159 bithenge_transform_t bithenge_##NAME##_transform = { \ 160 &NAME##_ops, 1 \ 161 } 162 163 MAKE_UINT_TRANSFORM(uint8 , uint8_t , , prefix_length_1); 164 MAKE_UINT_TRANSFORM(uint16le, uint16_t, uint16_t_le2host, prefix_length_2); 165 MAKE_UINT_TRANSFORM(uint16be, uint16_t, uint16_t_be2host, prefix_length_2); 166 MAKE_UINT_TRANSFORM(uint32le, uint32_t, uint32_t_le2host, prefix_length_4); 167 MAKE_UINT_TRANSFORM(uint32be, uint32_t, uint32_t_be2host, prefix_length_4); 168 MAKE_UINT_TRANSFORM(uint64le, uint64_t, uint32_t_le2host, prefix_length_8); 169 MAKE_UINT_TRANSFORM(uint64be, uint64_t, uint32_t_be2host, prefix_length_8); 172 170 173 171 static int zero_terminated_apply(bithenge_transform_t *self, … … 228 226 static bithenge_named_transform_t primitive_transforms[] = { 229 227 {"ascii", &bithenge_ascii_transform}, 228 {"uint8", &bithenge_uint8_transform}, 229 {"uint16le", &bithenge_uint16le_transform}, 230 {"uint16be", &bithenge_uint16be_transform}, 230 231 {"uint32le", &bithenge_uint32le_transform}, 231 232 {"uint32be", &bithenge_uint32be_transform}, 233 {"uint64le", &bithenge_uint64le_transform}, 234 {"uint64be", &bithenge_uint64be_transform}, 232 235 {"zero_terminated", &bithenge_zero_terminated_transform}, 233 236 {NULL, NULL} -
uspace/app/bithenge/transform.h
rae49252 r5f4cf872 119 119 120 120 extern bithenge_transform_t bithenge_ascii_transform; 121 extern bithenge_transform_t bithenge_uint8_transform; 122 extern bithenge_transform_t bithenge_uint16le_transform; 123 extern bithenge_transform_t bithenge_uint16be_transform; 121 124 extern bithenge_transform_t bithenge_uint32le_transform; 122 125 extern bithenge_transform_t bithenge_uint32be_transform; 126 extern bithenge_transform_t bithenge_uint64le_transform; 127 extern bithenge_transform_t bithenge_uint64be_transform; 123 128 extern bithenge_transform_t bithenge_zero_terminated_transform; 124 129 extern bithenge_named_transform_t *bithenge_primitive_transforms;
Note:
See TracChangeset
for help on using the changeset viewer.