Changeset dcb74c0a in mainline


Ignore:
Timestamp:
2011-07-03T10:05:21Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
64e6945d
Parents:
8e1a819
Message:

Add function to str.c to convert space-padded ASCII to standard string
representation. Use for decoding SCSI strings.

Location:
uspace
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/usbmast/inquiry.c

    r8e1a819 rdcb74c0a  
    6262}
    6363
    64 /** Trim trailing spaces from a string (rewrite with string terminator).
    65  *
    66  * @param name String to be trimmed (in-out parameter).
    67  */
    68 static void trim_trailing_spaces(char *name)
    69 {
    70         size_t len = str_length(name);
    71         while ((len > 0) && isspace((int) name[len - 1])) {
    72                 name[len - 1] = 0;
    73                 len--;
    74         }
    75 }
    76 
    7764/** Perform SCSI INQUIRY command on USB mass storage device.
    7865 *
     
    126113            SCSI_RMB_RMB, SCSI_RMB_RMB);
    127114
    128         str_ncpy(inquiry_result->vendor, 1 + sizeof(inq_data.vendor),
    129             (const char *) &inq_data.vendor, sizeof(inq_data.vendor));
    130         trim_trailing_spaces(inquiry_result->vendor);
     115        spascii_to_str(inquiry_result->vendor, SCSI_INQ_VENDOR_STR_BUFSIZE,
     116            inq_data.vendor, sizeof(inq_data.vendor));
    131117
    132         str_ncpy(inquiry_result->product, 1 + sizeof(inq_data.product),
    133             (const char *) &inq_data.product, sizeof(inq_data.product));
    134         trim_trailing_spaces(inquiry_result->product);
     118        spascii_to_str(inquiry_result->product, SCSI_INQ_PRODUCT_STR_BUFSIZE,
     119            inq_data.product, sizeof(inq_data.product));
    135120
    136         str_ncpy(inquiry_result->revision, 1 + sizeof(inq_data.revision),
    137             (const char *) &inq_data.revision, sizeof(inq_data.revision));
    138         trim_trailing_spaces(inquiry_result->revision);
     121        spascii_to_str(inquiry_result->revision, SCSI_INQ_REVISION_STR_BUFSIZE,
     122            inq_data.revision, sizeof(inq_data.revision));
    139123
    140124        return EOK;
  • uspace/drv/bus/usb/usbmast/mast.h

    r8e1a819 rdcb74c0a  
    3737#define USB_USBMAST_MAST_H_
    3838
     39#include <scsi/spc.h>
    3940#include <sys/types.h>
    4041#include <usb/usb.h>
     
    5253        bool removable;
    5354        /** Vendor ID string */
    54         char vendor[9];
     55        char vendor[SCSI_INQ_VENDOR_STR_BUFSIZE];
    5556        /** Product ID string */
    56         char product[17];
     57        char product[SCSI_INQ_PRODUCT_STR_BUFSIZE];
    5758        /** Revision string */
    58         char revision[17];
     59        char revision[SCSI_INQ_REVISION_STR_BUFSIZE];
    5960} usb_massstor_inquiry_result_t;
    6061
  • uspace/lib/c/generic/str.c

    r8e1a819 rdcb74c0a  
    544544       
    545545        str_cpy(dest + dstr_size, size - dstr_size, src);
     546}
     547
     548/** Convert space-padded ASCII to string.
     549 *
     550 * Common legacy text encoding in hardware is 7-bit ASCII fitted into
     551 * a fixed-with byte buffer (bit 7 always zero), right-padded with spaces
     552 * (ASCII 0x20). Convert space-padded ascii to string representation.
     553 *
     554 * If the text does not fit into the destination buffer, the function converts
     555 * as many characters as possible and returns EOVERFLOW.
     556 *
     557 * If the text contains non-ASCII bytes (with bit 7 set), the whole string is
     558 * converted anyway and invalid characters are replaced with question marks
     559 * (U_SPECIAL) and the function returns EIO.
     560 *
     561 * Regardless of return value upon return @a dest will always be well-formed.
     562 *
     563 * @param dest          Destination buffer
     564 * @param size          Size of destination buffer
     565 * @param src           Space-padded ASCII.
     566 * @param n             Size of the source buffer in bytes.
     567 *
     568 * @return              EOK on success, EOVERFLOW if the text does not fit
     569 *                      destination buffer, EIO if the text contains
     570 *                      non-ASCII bytes.
     571 */
     572int spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n)
     573{
     574        size_t sidx;
     575        size_t didx;
     576        size_t dlast;
     577        uint8_t byte;
     578        int rc;
     579        int result;
     580
     581        /* There must be space for a null terminator in the buffer. */
     582        assert(size > 0);
     583        result = EOK;
     584
     585        didx = 0;
     586        dlast = 0;
     587        for (sidx = 0; sidx < n; ++sidx) {
     588                byte = src[sidx];
     589                if (!ascii_check(byte)) {
     590                        byte = U_SPECIAL;
     591                        result = EIO;
     592                }
     593
     594                rc = chr_encode(byte, dest, &didx, size - 1);
     595                if (rc != EOK) {
     596                        assert(rc == EOVERFLOW);
     597                        dest[didx] = '\0';
     598                        return rc;
     599                }
     600
     601                /* Remember dest index after last non-empty character */
     602                if (byte != 0x20)
     603                        dlast = didx;
     604        }
     605
     606        /* Terminate string after last non-empty character */
     607        dest[dlast] = '\0';
     608        return result;
    546609}
    547610
  • uspace/lib/c/include/str.h

    r8e1a819 rdcb74c0a  
    4848#define STR_BOUNDS(length)  ((length) << 2)
    4949
     50/**
     51 * Maximum size of a buffer needed to a string converted from space-padded
     52 * ASCII of size @a spa_size using spascii_to_str().
     53 */
     54#define SPASCII_STR_BUFSIZE(spa_size) ((spa_size) + 1)
     55
    5056extern wchar_t str_decode(const char *str, size_t *offset, size_t sz);
    5157extern int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz);
     
    7379extern void str_append(char *dest, size_t size, const char *src);
    7480
     81extern int spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n);
    7582extern void wstr_to_str(char *dest, size_t size, const wchar_t *src);
    7683extern char *wstr_to_astr(const wchar_t *src);
  • uspace/lib/scsi/include/scsi/spc.h

    r8e1a819 rdcb74c0a  
    3838
    3939#include <stdint.h>
     40#include <str.h>
    4041
    4142/** SCSI command codes defined in SCSI-SPC */
     
    9394} scsi_std_inquiry_data_t;
    9495
     96/** Size of struct or union member. */
     97#define SCSI_MEMBER_SIZE(type, member) \
     98    (sizeof(((type *)0) -> member))
     99
     100/** Size of string buffer needed to hold converted inquiry vendor string */
     101#define SCSI_INQ_VENDOR_STR_BUFSIZE \
     102    SPASCII_STR_BUFSIZE(SCSI_MEMBER_SIZE(scsi_std_inquiry_data_t, vendor))
     103
     104/** Size of string buffer needed to hold converted inquiry product string */
     105#define SCSI_INQ_PRODUCT_STR_BUFSIZE \
     106    SPASCII_STR_BUFSIZE(SCSI_MEMBER_SIZE(scsi_std_inquiry_data_t, product))
     107
     108/** Size of string buffer needed to hold converted inquiry revision string */
     109#define SCSI_INQ_REVISION_STR_BUFSIZE \
     110    SPASCII_STR_BUFSIZE(SCSI_MEMBER_SIZE(scsi_std_inquiry_data_t, revision))
     111
    95112/** Bits in scsi_std_inquiry_data_t.pqual_devtype */
    96113enum scsi_pqual_devtype_bits {
Note: See TracChangeset for help on using the changeset viewer.