[da0fef6] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Sean Bartell
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup bithenge
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * Provide various external sources of data.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
[8fc0f47c] | 39 | #include <bithenge/blob.h>
|
---|
| 40 | #include <bithenge/file.h>
|
---|
| 41 | #include <bithenge/source.h>
|
---|
[da0fef6] | 42 |
|
---|
| 43 | #ifdef __HELENOS__
|
---|
| 44 | #include <loc.h>
|
---|
| 45 | #include "helenos/block.h"
|
---|
| 46 | #endif
|
---|
| 47 |
|
---|
| 48 | static inline int hex_digit(char digit)
|
---|
| 49 | {
|
---|
| 50 | if ('0' <= digit && digit <= '9')
|
---|
| 51 | return digit - '0' + 0x0;
|
---|
| 52 | if ('a' <= digit && digit <= 'f')
|
---|
| 53 | return digit - 'a' + 0xa;
|
---|
| 54 | if ('A' <= digit && digit <= 'F')
|
---|
| 55 | return digit - 'A' + 0xA;
|
---|
| 56 | return -1;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | static int blob_from_hex(bithenge_node_t **out, const char *hex)
|
---|
| 60 | {
|
---|
| 61 | size_t size = str_length(hex);
|
---|
| 62 | if (size % 2)
|
---|
| 63 | return EINVAL;
|
---|
| 64 | size /= 2;
|
---|
| 65 | char *buffer = malloc(size);
|
---|
| 66 | if (!buffer)
|
---|
| 67 | return ENOMEM;
|
---|
| 68 | for (size_t i = 0; i < size; i++) {
|
---|
| 69 | int upper = hex_digit(hex[2 * i + 0]);
|
---|
| 70 | int lower = hex_digit(hex[2 * i + 1]);
|
---|
| 71 | if (upper == -1 || lower == -1) {
|
---|
| 72 | free(buffer);
|
---|
| 73 | return EINVAL;
|
---|
| 74 | }
|
---|
| 75 | buffer[i] = upper << 4 | lower;
|
---|
| 76 | }
|
---|
| 77 | return bithenge_new_blob_from_buffer(out, buffer, size, true);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[978ccaf1] | 80 | /** Create a node from a source described with a string. For instance,
|
---|
| 81 | * "hex:55aa" will result in a blob node. If there is no colon in the string,
|
---|
| 82 | * it is assumed to be a filename.
|
---|
| 83 | * @param[out] out Stores the created node.
|
---|
| 84 | * @param source Specifies the node to be created.
|
---|
| 85 | * @return EOK on success or an error code from errno.h. */
|
---|
[da0fef6] | 86 | int bithenge_node_from_source(bithenge_node_t **out, const char *source)
|
---|
| 87 | {
|
---|
| 88 | if (str_chr(source, ':')) {
|
---|
| 89 | if (!str_lcmp(source, "file:", 5)) {
|
---|
| 90 | // Example: file:/textdemo
|
---|
| 91 | return bithenge_new_file_blob(out, source + 5);
|
---|
| 92 | #ifdef __HELENOS__
|
---|
| 93 | } else if (!str_lcmp(source, "block:", 6)) {
|
---|
| 94 | // Example: block:bd/initrd
|
---|
| 95 | service_id_t service_id;
|
---|
| 96 | int rc = loc_service_get_id(source + 6, &service_id, 0);
|
---|
| 97 | if (rc != EOK)
|
---|
| 98 | return rc;
|
---|
| 99 | return bithenge_new_block_blob(out, service_id);
|
---|
| 100 | #endif
|
---|
| 101 | } else if (!str_lcmp(source, "hex:", 4)) {
|
---|
| 102 | // Example: hex:04000000
|
---|
| 103 | return blob_from_hex(out, source + 4);
|
---|
| 104 | } else {
|
---|
| 105 | return EINVAL;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | return bithenge_new_file_blob(out, source);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /** @}
|
---|
| 112 | */
|
---|