From e359da1369caf436878cd05386b83b54359ecce1 Mon Sep 17 00:00:00 2001 From: Douglas Gilbert Date: Thu, 10 Dec 2015 18:29:41 +0000 Subject: unaligned cleanups for C++; misc work git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@653 6180dd3e-e324-4e3e-922d-17de1ae2f315 --- examples/sg_iovec_tst.c | 11 ++++------- examples/sgq_dd.c | 17 ++++++----------- 2 files changed, 10 insertions(+), 18 deletions(-) (limited to 'examples') diff --git a/examples/sg_iovec_tst.c b/examples/sg_iovec_tst.c index 2a924261..6a0848da 100644 --- a/examples/sg_iovec_tst.c +++ b/examples/sg_iovec_tst.c @@ -11,6 +11,7 @@ #include #include "sg_lib.h" #include "sg_io_linux.h" +#include "sg_unaligned.h" /* Test code for D. Gilbert's extensions to the Linux OS SCSI generic ("sg") device driver. @@ -25,7 +26,7 @@ normal file. The purpose is to test the sg_iovec mechanism within the sg_io_hdr structure. - Version 0.13 (20150204) + Version 0.14 (20151209) */ @@ -66,12 +67,8 @@ static int sg_read(int sg_fd, unsigned char * buff, int num_blocks, int dxfer_len = bs * num_blocks; int k, pos, rem, res; - rdCmd[2] = (unsigned char)((from_block >> 24) & 0xff); - rdCmd[3] = (unsigned char)((from_block >> 16) & 0xff); - rdCmd[4] = (unsigned char)((from_block >> 8) & 0xff); - rdCmd[5] = (unsigned char)(from_block & 0xff); - rdCmd[7] = (unsigned char)((num_blocks >> 8) & 0xff); - rdCmd[8] = (unsigned char)(num_blocks & 0xff); + sg_put_unaligned_be32((uint32_t)from_block, rdCmd + 2); + sg_put_unaligned_be16((uint16_t)from_block, rdCmd + 7); for (k = 0, pos = 0, rem = dxfer_len; k < IOVEC_ELEMS; ++k) { iovec[k].iov_base = buff + pos; diff --git a/examples/sgq_dd.c b/examples/sgq_dd.c index 380493f7..b3d82736 100644 --- a/examples/sgq_dd.c +++ b/examples/sgq_dd.c @@ -19,6 +19,7 @@ typedef unsigned char u_char; /* horrible, for scsi.h */ #include "sg_lib.h" #include "sg_io_linux.h" +#include "sg_unaligned.h" /* A utility program for the Linux OS SCSI generic ("sg") device driver. * Copyright (C) 1999-2002 D. Gilbert and P. Allworth @@ -47,7 +48,7 @@ typedef unsigned char u_char; /* horrible, for scsi.h */ */ -static char * version_str = "0.57 20140819"; +static char * version_str = "0.58 20151209"; /* resurrected from "0.55 20020509" */ #define DEF_BLOCK_SIZE 512 @@ -311,10 +312,8 @@ read_capacity(int sg_fd, int * num_sect, int * sect_sz) sg_chk_n_print3("read capacity", &io_hdr, 1); return -1; } - *num_sect = 1 + ((rcBuff[0] << 24) | (rcBuff[1] << 16) | - (rcBuff[2] << 8) | rcBuff[3]); - *sect_sz = (rcBuff[4] << 24) | (rcBuff[5] << 16) | - (rcBuff[6] << 8) | rcBuff[7]; + *num_sect = 1 + sg_get_unaligned_be32(rcBuff + 0); + *sect_sz = sg_get_unaligned_be32(rcBuff + 4); #ifdef SG_DEBUG fprintf(stderr, "number of sectors=%d, sector size=%d\n", *num_sect, *sect_sz); @@ -460,12 +459,8 @@ sg_start_io(Rq_elem * rep) rep->qstate = rep->wr ? QS_OUT_STARTED : QS_IN_STARTED; memset(rep->cmd, 0, sizeof(rep->cmd)); rep->cmd[0] = rep->wr ? SGP_WRITE10 : SGP_READ10; - rep->cmd[2] = (unsigned char)((rep->blk >> 24) & 0xFF); - rep->cmd[3] = (unsigned char)((rep->blk >> 16) & 0xFF); - rep->cmd[4] = (unsigned char)((rep->blk >> 8) & 0xFF); - rep->cmd[5] = (unsigned char)(rep->blk & 0xFF); - rep->cmd[7] = (unsigned char)((rep->num_blks >> 8) & 0xff); - rep->cmd[8] = (unsigned char)(rep->num_blks & 0xff); + sg_put_unaligned_be32((uint32_t)rep->blk, rep->cmd + 2); + sg_put_unaligned_be16((uint16_t)rep->num_blks, rep->cmd + 7); memset(hp, 0, sizeof(sg_io_hdr_t)); hp->interface_id = 'S'; hp->cmd_len = sizeof(rep->cmd); -- cgit v1.2.3