source: mainline/uspace/app/bithenge/source.c@ f2da0bb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f2da0bb was da0fef6, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: port to Linux and allow choosing a data source

  • Property mode set to 100644
File size: 3.1 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 "blob.h"
40#include "file.h"
41#include "source.h"
42
43#ifdef __HELENOS__
44#include <loc.h>
45#include "helenos/block.h"
46#endif
47
48static 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
59static 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
80int bithenge_node_from_source(bithenge_node_t **out, const char *source)
81{
82 if (str_chr(source, ':')) {
83 if (!str_lcmp(source, "file:", 5)) {
84 // Example: file:/textdemo
85 return bithenge_new_file_blob(out, source + 5);
86#ifdef __HELENOS__
87 } else if (!str_lcmp(source, "block:", 6)) {
88 // Example: block:bd/initrd
89 service_id_t service_id;
90 int rc = loc_service_get_id(source + 6, &service_id, 0);
91 if (rc != EOK)
92 return rc;
93 return bithenge_new_block_blob(out, service_id);
94#endif
95 } else if (!str_lcmp(source, "hex:", 4)) {
96 // Example: hex:04000000
97 return blob_from_hex(out, source + 4);
98 } else {
99 return EINVAL;
100 }
101 }
102 return bithenge_new_file_blob(out, source);
103}
104
105/** @}
106 */
Note: See TracBrowser for help on using the repository browser.