source: mainline/uspace/lib/bithenge/src/source.c@ d5409da

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d5409da was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 3.5 KB
Line 
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>
39#include <bithenge/blob.h>
40#include <bithenge/file.h>
41#include <bithenge/source.h>
42#include "common.h"
43
44#ifdef __HELENOS__
45#include <loc.h>
46#include "helenos/block.h"
47#endif
48
49static inline int hex_digit(char digit)
50{
51 if ('0' <= digit && digit <= '9')
52 return digit - '0' + 0x0;
53 if ('a' <= digit && digit <= 'f')
54 return digit - 'a' + 0xa;
55 if ('A' <= digit && digit <= 'F')
56 return digit - 'A' + 0xA;
57 return -1;
58}
59
60static errno_t blob_from_hex(bithenge_node_t **out, const char *hex)
61{
62 size_t size = str_length(hex);
63 if (size % 2)
64 return EINVAL;
65 size /= 2;
66 char *buffer = malloc(size);
67 if (!buffer)
68 return ENOMEM;
69 for (size_t i = 0; i < size; i++) {
70 int upper = hex_digit(hex[2 * i + 0]);
71 int lower = hex_digit(hex[2 * i + 1]);
72 if (upper == -1 || lower == -1) {
73 free(buffer);
74 return EINVAL;
75 }
76 buffer[i] = upper << 4 | lower;
77 }
78 return bithenge_new_blob_from_buffer(out, buffer, size, true);
79}
80
81/** Create a node from a source described with a string. For instance,
82 * "hex:55aa" will result in a blob node. If there is no colon in the string,
83 * it is assumed to be a filename.
84 * @param[out] out Stores the created node.
85 * @param source Specifies the node to be created.
86 * @return EOK on success or an error code from errno.h.
87 */
88errno_t bithenge_node_from_source(bithenge_node_t **out, const char *source)
89{
90 if (str_chr(source, ':')) {
91 if (!str_lcmp(source, "file:", 5)) {
92 // Example: file:/textdemo
93 return bithenge_new_file_blob(out, source + 5);
94#ifdef __HELENOS__
95 } else if (!str_lcmp(source, "block:", 6)) {
96 // Example: block:bd/initrd
97 service_id_t service_id;
98 errno_t rc = loc_service_get_id(source + 6, &service_id, 0);
99 if (rc != EOK)
100 return rc;
101 return bithenge_new_block_blob(out, service_id);
102#endif
103 } else if (!str_lcmp(source, "hex:", 4)) {
104 // Example: hex:04000000
105 return blob_from_hex(out, source + 4);
106 } else {
107 return EINVAL;
108 }
109 }
110 return bithenge_new_file_blob(out, source);
111}
112
113/** @}
114 */
Note: See TracBrowser for help on using the repository browser.