Example #1
0
static void
ee_constype(const struct keytabent *ktent, char *arg)
{
    const struct strvaltabent *svp;
    u_char cons;

    if (arg) {
        for (svp = constab; svp->sv_str != NULL; ++svp)
            if (strcmp(svp->sv_str, arg) == 0)
                break;
        if (svp->sv_str == NULL)
            BARF(ktent);

        cons = svp->sv_val;
        if (doio(ktent, &cons, sizeof(cons), IO_WRITE))
            FAILEDWRITE(ktent);
    } else {
        if (doio(ktent, &cons, sizeof(cons), IO_READ))
            FAILEDREAD(ktent);

        for (svp = constab; svp->sv_str != NULL; ++svp)
            if (svp->sv_val == cons)
                break;
        if (svp->sv_str == NULL) {
            warnx("unknown type 0x%x for %s", cons,
                  ktent->kt_keyword);
            return;
        }
    }
    printf("%s=%s\n", ktent->kt_keyword, svp->sv_str);

}
Example #2
0
void sigpipe_handler( int signum )
{
    #ifdef DEBUG_BARF
        BARF( "a client died :^(", signum );
    #endif
    return;
}/* End sigpipe_handler Func */
Example #3
0
static void
ee_truefalse(const struct keytabent *ktent, char *arg)
{
    const struct strvaltabent *svp;
    u_char truth;

    if (arg) {
        for (svp = truthtab; svp->sv_str != NULL; ++svp)
            if (strcmp(svp->sv_str, arg) == 0)
                break;
        if (svp->sv_str == NULL)
            BARF(ktent);

        truth = svp->sv_val;
        if (doio(ktent, &truth, sizeof(truth), IO_WRITE))
            FAILEDWRITE(ktent);
    } else {
        if (doio(ktent, &truth, sizeof(truth), IO_READ))
            FAILEDREAD(ktent);

        for (svp = truthtab; svp->sv_str != NULL; ++svp)
            if (svp->sv_val == truth)
                break;
        if (svp->sv_str == NULL) {
            warnx("unknown truth value 0x%x for %s", truth,
                  ktent->kt_keyword);
            return;
        }
    }
    printf("%s=%s\n", ktent->kt_keyword, svp->sv_str);
}
Example #4
0
/* [PM] 4.0 Ensure all written data have reached p->buffer after writing to the stream. buffer will not be NUL terminated. */
static spio_t_error_code do_flush(SP_stream *stream, struct event_stream_data *p)
{
  spio_t_error_code code = SPIO_E_ERROR;

  CHECK(SP_flush_output(stream, 0));

  /* give the encoding a chance to finish and deallocate its state 
     The encoding will be re-initialized as needed from user_write()
  */
  /* if we use do_flush from trans_command_list then the encoder may not be inited */

    if (SPIO_MASK_IS_SET(p->flags, TCLTK_STREAM_FLAGS_ENCODER_INITED))
      {
        code = close_stream_encoder(p, FALSE, FALSE); /* not read-direction, not abortive */
     
        if (SPIO_FAILED(code))
          {
            /* free write_encoding_state */
            (void) close_stream_encoder(p, FALSE, TRUE); /* not read-direction, abortive */
            BARF(code);
          }
      }

  code = SPIO_S_NOERR;

 cleanup:
  return code;

 barf:
  goto cleanup;
}
Example #5
0
static void
ee_screensize(const struct keytabent *ktent, char *arg)
{
    const struct strvaltabent *svp;
    u_char scsize;

    if (arg) {
        for (svp = scrsizetab; svp->sv_str != NULL; ++svp)
            if (strcmp(svp->sv_str, arg) == 0)
                break;
        if (svp->sv_str == NULL)
            BARF(ktent);

        scsize = svp->sv_val;
        if (doio(ktent, &scsize, sizeof(scsize), IO_WRITE))
            FAILEDWRITE(ktent);
    } else {
        if (doio(ktent, &scsize, sizeof(scsize), IO_READ))
            FAILEDREAD(ktent);

        for (svp = scrsizetab; svp->sv_str != NULL; ++svp)
            if (svp->sv_val == scsize)
                break;
        if (svp->sv_str == NULL) {
            warnx("unknown %s value %d", ktent->kt_keyword,
                  scsize);
            return;
        }
    }
    printf("%s=%s\n", ktent->kt_keyword, svp->sv_str);
}
Example #6
0
static void
ee_kbdtype(const struct keytabent *ktent, char *arg)
{
    u_char kbd = 0;
    u_int kbd2;
    int i;

    if (arg) {
        for (i = 0; i < (int)strlen(arg) - 1; ++i)
            if (!isdigit((unsigned char)arg[i]))
                BARF(ktent);
        kbd2 = atoi(arg);
        if (kbd2 > 0xff)
            BARF(ktent);
        kbd += kbd2;
        if (doio(ktent, &kbd, sizeof(kbd), IO_WRITE))
            FAILEDWRITE(ktent);
    } else if (doio(ktent, &kbd, sizeof(kbd), IO_READ))
        FAILEDREAD(ktent);

    printf("%s=%d (%s)\n", ktent->kt_keyword, kbd, kbd ? "other" : "Sun");
}
Example #7
0
static void
ee_num16(const struct keytabent *ktent, char *arg)
{
    u_int16_t num16 = 0;
    u_int num32;
    int i;

    if (arg) {
        for (i = 0; i < (int)strlen(arg) - 1; ++i)
            if (!isdigit((unsigned char)arg[i]))
                BARF(ktent);
        num32 = atoi(arg);
        if (num32 > 0xffff)
            BARF(ktent);
        num16 += num32;
        if (doio(ktent, (u_char *)&num16, sizeof(num16), IO_WRITE))
            FAILEDWRITE(ktent);
    } else if (doio(ktent, (u_char *)&num16, sizeof(num16), IO_READ))
        FAILEDREAD(ktent);

    printf("%s=%d\n", ktent->kt_keyword, num16);
}
Example #8
0
static void
ee_diagpath(const struct keytabent *ktent, char *arg)
{
    char path[40];

    memset(path, 0, sizeof(path));
    if (arg) {
        if (strlen(arg) > sizeof(path))
            BARF(ktent);
        memcpy(path, arg, sizeof path);
        if (doio(ktent, (u_char *)&path[0], sizeof(path), IO_WRITE))
            FAILEDWRITE(ktent);
    } else if (doio(ktent, (u_char *)&path[0], sizeof(path), IO_READ))
        FAILEDREAD(ktent);

    printf("%s=%s\n", ktent->kt_keyword, path);
}
Example #9
0
static void
ee_banner(const struct keytabent *ktent, char *arg)
{
    char string[80];
    u_char enable;
    struct keytabent kt;

    kt.kt_keyword = "enable_banner";
    kt.kt_offset = EE_BANNER_ENABLE_LOC;
    kt.kt_handler = ee_notsupp;

    memset(string, '\0', sizeof(string));
    if (arg) {
        if (strlen(arg) > sizeof(string))
            BARF(ktent);
        if (*arg != '\0') {
            enable = EE_TRUE;
            memcpy(string, arg, sizeof string);
            if (doio(ktent, (u_char *)string,
                     sizeof(string), IO_WRITE))
                FAILEDWRITE(ktent);
        } else {
            enable = EE_FALSE;
            if (doio(ktent, (u_char *)string,
                     sizeof(string), IO_READ))
                FAILEDREAD(ktent);
        }

        if (doio(&kt, &enable, sizeof(enable), IO_WRITE))
            FAILEDWRITE(&kt);
    } else {
        if (doio(ktent, (u_char *)string, sizeof(string), IO_READ))
            FAILEDREAD(ktent);
        if (doio(&kt, &enable, sizeof(enable), IO_READ))
            FAILEDREAD(&kt);
    }
    printf("%s=%s (%s)\n", ktent->kt_keyword, string,
           enable == EE_TRUE ? "enabled" : "disabled");
}
Example #10
0
static void
ee_hwupdate(const struct keytabent *ktent, char *arg)
{
    uint32_t hwtime;
    time_t t;
    char *cp, *cp2;

    if (arg) {
        if ((strcmp(arg, "now") == 0) ||
                (strcmp(arg, "today") == 0)) {
            if ((t = time(NULL)) == (time_t)(-1)) {
                warnx("can't get current time");
                ++eval;
                return;
            }
        } else if ((t = parsedate(arg, NULL, NULL)) == (time_t)(-1))
            BARF(ktent);
        hwtime = (uint32_t)t;	/* XXX 32 bit time_t on hardware */
        if (hwtime != t)
            warnx("time overflow");

        if (doio(ktent, (u_char *)&hwtime, sizeof(hwtime), IO_WRITE))
            FAILEDWRITE(ktent);
    } else {
        if (doio(ktent, (u_char *)&hwtime, sizeof(hwtime), IO_READ))
            FAILEDREAD(ktent);
        t = (time_t)hwtime;	/* XXX 32 bit time_t on hardware */
    }

    cp = ctime(&t);
    if (cp != NULL && (cp2 = strrchr(cp, '\n')) != NULL)
        *cp2 = '\0';

    printf("%s=%" PRId64, ktent->kt_keyword, (int64_t)t);
    if (cp != NULL)
        printf(" (%s)", cp);
    printf("\n");
}
Example #11
0
static void main_foo(int alpha, int beta)
{
	float f = 33.44;
	double d = 55.66;
	long long ll = 77;
	static char const *str = "susu";
	char const fourbytes[] = { 0x10, 0x20, 0xef, 0xff };
	char const (*binaryp)[] = &fourbytes;

	void main_foo_foo(void)
	{
		BARF();
	}

	nop();
	nop();
	main_foo_foo();
	boo();
	alpha *= 2; beta *= 3; f *= 4; d *= 5; ll *= 6;
//	return;
	lib_foo();
	nop();
	nop();
}
Example #12
0
static void
ee_bootdev(const struct keytabent *ktent, char *arg)
{
    u_char dev[5];
    int i;
    size_t arglen;
    char *cp;

    if (arg) {
        /*
         * The format of the string we accept is the following:
         *	cc(n,n,n)
         * where:
         *	c -- an alphabetical character [a-z]
         *	n -- a number in hexadecimal, between 0 and ff,
         *	     with no leading `0x'.
         */
        arglen = strlen(arg);
        if (arglen < 9 || arglen > 12 || arg[2] != '(' ||
                arg[arglen - 1] != ')')
            BARF(ktent);

        /* Handle the first 2 letters. */
        for (i = 0; i < 2; ++i) {
            if (arg[i] < 'a' || arg[i] > 'z')
                BARF(ktent);
            dev[i] = (u_char)arg[i];
        }

        /* Handle the 3 `0x'-less hex values. */
        cp = &arg[3];
        for (i = 2; i < 5; ++i) {
            if (*cp == '\0')
                BARF(ktent);

            if (*cp >= '0' && *cp <= '9')
                dev[i] = *cp++ - '0';
            else if (*cp >= 'a' && *cp <= 'f')
                dev[i] = 10 + (*cp++ - 'a');
            else
                BARF(ktent);

            /* Deal with a second digit. */
            if (*cp >= '0' && *cp <= '9') {
                dev[i] <<= 4;
                dev[i] &= 0xf0;
                dev[i] += *cp++ - '0';
            } else if (*cp >= 'a' && *cp <= 'f') {
                dev[i] <<= 4;
                dev[i] &= 0xf0;
                dev[i] += 10 + (*cp++ - 'a');
            }

            /* Ensure we have the correct delimiter. */
            if ((*cp == ',' && i < 4) || (*cp == ')' && i == 4)) {
                ++cp;
                continue;
            } else
                BARF(ktent);
        }
        if (doio(ktent, (u_char *)&dev[0], sizeof(dev), IO_WRITE))
            FAILEDWRITE(ktent);
    } else if (doio(ktent, (u_char *)&dev[0], sizeof(dev), IO_READ))
        FAILEDREAD(ktent);

    printf("%s=%c%c(%x,%x,%x)\n", ktent->kt_keyword, dev[0],
           dev[1], dev[2], dev[3], dev[4]);
}
Example #13
0
static spio_t_error_code SPIO_CDECL user_read(void *user_data,
                                              void *buf, /* a spio_t_wchar[] */
                                              size_t *pbuf_size,
                                              spio_t_bits read_options
                                              )
{
  spio_t_error_code code = SPIO_E_ERROR;
  int encoding_state_needs_cleanup = FALSE;
  struct event_stream_data *p = (struct event_stream_data *)user_data;
  
  SP_ASSERT(read_options & SPIO_DEVICE_READ_OPTION_TEXT);
  SP_ASSERT(p->size >= p->index);
  (void)read_options;


  if (p->index >= p->length)
    {
      BARF(SPIO_E_END_OF_FILE);
    }

  {
    spio_t_byte const *src = (spio_t_byte*)&(p->buffer[p->index]);
    size_t src_size = p->length - p->index;
    size_t src_size_read = 0;
    spio_t_wchar *dst = (spio_t_wchar *)buf;
    size_t dst_size = *pbuf_size;

    size_t dst_size_wrote = 0;
    /* size_t src_items_read = 0; */
    spio_t_bits encode_options = 0;

    if ( !(SPIO_MASK_IS_SET(p->flags, TCLTK_STREAM_FLAGS_ENCODER_INITED)) ) /* first call to encoder (may happen more than once, see do_flush)*/
      {
        CHECK(SP_encoding_open(p->encoding, &p->encoding_state, NULL, SP_ENCODING_OPEN_OPTION_DECODE|SP_ENCODING_OPEN_OPTION_UTF8_TCL));
        encoding_state_needs_cleanup = TRUE;
        SPIO_SET_MASK(p->flags, TCLTK_STREAM_FLAGS_ENCODER_INITED);
      }

    SP_ASSERT(SPIO_MASK_IS_SET(p->flags, TCLTK_STREAM_FLAGS_ENCODER_INITED));
    CHECK(SP_encode_to_codes(p->encoding,
                             src, src_size,
                             &p->encoding_state,
                             dst, dst_size,
                             &src_size_read, &dst_size_wrote,
                             encode_options));
#if SICSTUS_TODO
    /* In genereal SP_encode_to_codes could succeed with
       dst_size_wrote == 0 if src starts with something that does not
       produce any output (such as a encoding signature, shift
       sequence, etc). However, nothing like that should happen for
       UTF-8 (we do not treat ZWNBS as a Unicode signature here).  */
#error "[PM] 4.0 Need to handle the case when dst_size_wrote == 0"
#endif  /* SICSTUS_TODO */
    SP_ASSERT(dst_size_wrote > 0);
    SP_ASSERT(SPIO_MASK_IS_SET(p->flags, TCLTK_STREAM_FLAGS_ENCODER_INITED));
    SP_ASSERT(src_size_read <= src_size);
    SP_ASSERT(dst_size_wrote <= dst_size);
    p->index += src_size_read;
    SP_ASSERT(p->index <= p->length);

    SP_ASSERT(dst_size_wrote <= *pbuf_size);
    *pbuf_size = dst_size_wrote;
    encoding_state_needs_cleanup = FALSE; /* protect from cleanup */
    code = SPIO_S_NOERR;
  }

 cleanup:
  if (encoding_state_needs_cleanup)
    {
      SPIO_CLEAR_MASK(p->flags, TCLTK_STREAM_FLAGS_ENCODER_INITED);
      (void) SP_encode_to_codes(p->encoding,
                                NULL, 0,
                                &p->encoding_state,
                                NULL, 0,
                                NULL, NULL, /* NULL, */
                                SP_ENCODE_CODES_OPTION_ABORT);
    }
  return code;
 barf:
  goto cleanup;
}
Example #14
0
void timer_handler( int signum )
{
    char buf[MAX_SMESG_LEN+1], * x = &buf[7]; /* buffer for outgoing messages */
    PQUEUE *temp, *r;
    int i, j;

    #ifdef DEBUG_BARF
        BARF( "timer expired", signum );
    #endif

    switch( server_status ) {
        case SHUFFL://TODO: remove DC players before here?
            if( (c_in_tab+p_in_lob) < min_ppplaying ) {/* !Enough Players */
                #ifdef DEBUG_BARF
                    BARF( "!enough players", c_in_tab+p_in_lob );
                #endif
                if( p_in_tab ) { /* Re-Enqueue Table Players Into Lobby */
                    c_in_tab = p_in_tab = 0; /* Stand Up */
                    while( (temp = warlord.next) ) { //TODO: just append remainder of warlord list to lobby and reset scumbag
                        /* Remove DC Players */
                        if( warlord.who->status != DIS_CONN ) {
                            ++p_in_lob;
                            warlord.who->status = IN_LOBBY;
                            lobby_last->who = warlord.who;
                            lobby_last = lobby_last->next = CALLOC( 1, PQUEUE ); /* WARNING: assumes calloc is successfull */
                        } else init_player( warlord.who-player );//init_p( warlord.who );
                        warlord.who = warlord.next->who;
                        warlord.next = warlord.next->next;
                        free( temp );
                    } /* End de-queue While */
//TODO: WORKING: append warlord to lobby and hold dc players in another ll
//                     lobby_last->who = warlord.who;
//                     lobby_last->next = warlord.next;
//                     lobby_last = scumbag;
//                     warlord.who = NULL; warlord.next = NULL;
                    scumbag = &warlord;
//                     //TODO: remove dc players from appended section, set status
                    send_slobb_mesg();
                }/* End Moved Players to Lobby If */
                server_status = ENQING;
                hand_status = HAND_1; /* Reset Hand For Next Players */
                return;
            } /* End !Enough Players Remain to Play If */

            if( hand_status == HAND_1 ) need_club3 = true;
            else {
                server_status = SWAPIN;
                #ifdef DEBUG_BARF
                    BARF( "PLAYING ANOTHER HAND", hand_status );
                #endif
            }/* End !HAND_1 Else */

            /* Move to Table */
            p_in_tab = 0; /* Everyone Stand Up */
            while( (temp = warlord.next) ) {
                /* Remove DC Players */
                if( warlord.who->status != DIS_CONN ) {
                    table[p_in_tab] = warlord.who;
                    ++table[p_in_tab++]->hands; /* SGUI  - Track Qt Played Hands */
                } else init_player( warlord.who-player );//init_p( warlord.who );
                warlord.who = warlord.next->who;
                warlord.next = warlord.next->next;
                free( temp );
            } /* End de-queue While */
            scumbag = &warlord;

            /* Move in From Lobby if Room */
            if(( p_in_tab < MAX_PPPLAYING )&&( p_in_lob )) {
                sprintf( buf, "[slobb|" ); /* Correct qt Filled in Later */

                /* Sit at Table */
                while( p_in_tab < MAX_PPPLAYING ) {
                    if( !lobby_head.who ) break;
                    table[p_in_tab++] = lobby_head.who;
                    lobby_head.who = lobby_head.next->who;
                    temp = lobby_head.next;
                    lobby_head.next = lobby_head.next->next;
                    free( temp ); --p_in_lob;
                }/* End Room@Table While */

                *x++ = '0'+ (int)( p_in_lob / 10 );
                *x++ = '0'+ ( p_in_lob % 10 );
                *x++ = '|';

                lobby_last = r = &lobby_head;
                while( r->who ) {
                    sprintf( x, "%s,", r->who->name );
                    x += 9;
                    lobby_last = r;
                    r = r->next;
                }/* End Current Player Exists Else */
                *(x-1) = ']'; *x = 0;
                broadcast( buf, 10+(9*p_in_lob) );
            }/* End New Players Join If */
            c_in_tab = p_in_tab;
            for( i = c_in_tab; i < MAX_PPPLAYING; i++ ) table[i] = NULL;

            /* Deal */
            assert( p_in_tab >= DEF_MIN_PLAYERS );
//             deal( time( NULL ) );
            if( deal( time( NULL ) ) ) { /* Returns True if Failed */
                /* If Got Here then !Enough Players Anymore */
                for( i = 0; i < p_in_tab; i++ ) {
                    lobby_last->who = table[i];
                    lobby_last = lobby_last->next = CALLOC( 1, PQUEUE ); /* WARNING: assumes calloc is successfull */
                    ++p_in_lob;
                } /* End Re-Enqueue For */
                send_slobb_mesg();
                server_status = ENQING;
                return;
            }/* End Failed Deal If */

            sprintf( buf, "[shand|" );
            for( i = 0; i < c_in_tab; i++ ) {
                x = &buf[7];
                for( j = 0; j < MAX_PHAND_LEN; j++ ) {
                    *x++ = '0'+ (int)( table[i]->hand[j] / 10 );
                    *x++ = '0'+ ( table[i]->hand[j] % 10 );
                    *x++ = ',';
                }/* End card in hand For */
                 *(x-1) = ']'; *x = 0;
                if( Write( table[i]->socketfd, buf, 61 ) < 0 )
                    ERROR( "write", strerror(errno), i );
            }/* End table[i] For */

            if( server_status != SWAPIN ) {
                send_tabl_mesg();
                server_status = ACTIVE;
            } else { /* Request Swap */
                char* cd = &(table[p_in_tab-1]->hand[MAX_PHAND_LEN-1]);
                while( *cd == 52 ) --cd; /* Find Highest Card */
                swapped_cd = *cd;
                assert( (unsigned)swapped_cd < 52 );/* ERROR CHECK */
                if( use_gui ) /* Decrement Count -- SGUI */
                    --deck[swapped_cd]->count[(swapped_cd-(swapped_cd%4))/4];
                deck[swapped_cd] = table[0]; /* Give Warlord Swappeed Card */
                if( use_gui ) /* Increment Count -- SGUI */
                    ++deck[swapped_cd]->count[(swapped_cd-(swapped_cd%4))/4];
                /* Update Scores For SGUI */
                ++table[0]->score; --table[p_in_tab-1]->score;
                /* Notify Clients of Change */
                ( swapped_cd < 10 ) ?
                    sprintf( buf, "[swapw|%c%d]", '0', swapped_cd ):
                    sprintf( buf, "[swapw|%d]", swapped_cd );
                if( Write( table[0]->socketfd, buf, 10 ) < 0 )
                    ERROR( "write", strerror(errno), table[0]->socketfd );
            }/* End SWAPIN Else */

            timer.it_value.tv_sec = ertimeout;
            setitimer( ITIMER_REAL, &timer, NULL );
            break;
        case ACTIVE:
            #ifdef DEBUG_BARF
                BARF( "Testing: server_status is ACTIVE.", ACTIVE );
            #endif
            for( i = 0; i < MAX_PPPLAYING; i++ )
                if( player[i].status == ACTIVE_P ) break;
            /* No Break */
        case SWAPIN:
            #ifdef DEBUG_BARF
                if( server_status != ACTIVE )
                    BARF( "Testing: server_status is SWAPIN", SWAPIN );
            #endif
            if( server_status != ACTIVE ) i = table[0] - &player[0];
            if( strike( i, PLAY_TIMEOUT ) )
                remove_player( i, buf, gfds );
            else if( server_status == ACTIVE ) send_tabl_mesg();
            timer.it_value.tv_sec = ertimeout;
            setitimer( ITIMER_REAL, &timer, NULL );
            break;
        default: /* ENQING */
            #ifdef DEBUG_BARF
                ERROR( "timer_h", "Timer went off while ENQING", signum );
            #endif
            break;
    }/* End server_status Switch */
    return;
}/* End timer_handler Func */
Example #15
0
/**
 * gnm_validation_eval:
 * @wbc:
 * @mstyle:
 * @sheet:
 *
 * validation set in the GnmStyle if applicable.
 **/
ValidationStatus
gnm_validation_eval (WorkbookControl *wbc, GnmStyle const *mstyle,
		 Sheet *sheet, GnmCellPos const *pos, gboolean *showed_dialog)
{
	GnmValidation const *v;
	GnmCell *cell;
	GnmValue *val;
	gnm_float x;
	int nok, i;
	GnmEvalPos ep;

	if (showed_dialog) *showed_dialog = FALSE;

	v = gnm_style_get_validation (mstyle);
	if (v == NULL)
		return GNM_VALIDATION_STATUS_VALID;

	if (v->type == GNM_VALIDATION_TYPE_ANY)
		return GNM_VALIDATION_STATUS_VALID;

	cell = sheet_cell_get (sheet, pos->col, pos->row);
	if (cell != NULL)
		gnm_cell_eval (cell);

	if (gnm_cell_is_empty (cell)) {
		if (v->allow_blank)
			return GNM_VALIDATION_STATUS_VALID;
		BARF (g_strdup_printf (_("Cell %s is not permitted to be blank"),
				       cell_name (cell)));
	}

	val = cell->value;
	switch (val->type) {
	case VALUE_ERROR:
		if (typeinfo[v->type].errors_not_allowed)
			BARF (g_strdup_printf (_("Cell %s is not permitted to contain error values"),
					       cell_name (cell)));
		break;

	case VALUE_BOOLEAN:
		if (typeinfo[v->type].bool_always_ok)
			return GNM_VALIDATION_STATUS_VALID;
		break;

	case VALUE_STRING:
		if (typeinfo[v->type].strings_not_allowed)
			BARF (g_strdup_printf (_("Cell %s is not permitted to contain strings"),
					       cell_name (cell)));
		break;

	default:
		break;
	}

	eval_pos_init_cell (&ep, cell);

	switch (v->type) {
	case GNM_VALIDATION_TYPE_AS_INT:
		x = value_get_as_float (val);
		if (gnm_fake_floor (x) == gnm_fake_ceil (x))
			break;
		else
			BARF (g_strdup_printf (_("'%s' is not an integer"),
					       value_peek_string (val)));

	case GNM_VALIDATION_TYPE_AS_NUMBER:
		x = value_get_as_float (val);
		break;

	case GNM_VALIDATION_TYPE_AS_DATE: /* What the hell does this do?  */
		x = value_get_as_float (val);
		if (x < 0)
			BARF (g_strdup_printf (_("'%s' is not a valid date"),
					       value_peek_string (val)));
		break;


	case GNM_VALIDATION_TYPE_AS_TIME: /* What the hell does this do?  */
		x = value_get_as_float (val);
		break;

	case GNM_VALIDATION_TYPE_IN_LIST: {
		GnmExprTop const *texpr = v->deps[0].texpr;
		if (texpr) {
			GnmValue *list = gnm_expr_top_eval
				(texpr, &ep,
				 GNM_EXPR_EVAL_PERMIT_NON_SCALAR | GNM_EXPR_EVAL_PERMIT_EMPTY);
			GnmValue *res = value_area_foreach (list, &ep, CELL_ITER_IGNORE_BLANK,
				 (GnmValueIterFunc) cb_validate_custom, val);
			value_release (list);
			if (res == NULL) {
				GnmParsePos pp;
				char *expr_str = gnm_expr_top_as_string
					(texpr,
					 parse_pos_init_evalpos (&pp, &ep),
					 ep.sheet->convs);
				char *msg = g_strdup_printf (_("%s does not contain the new value."), expr_str);
				g_free (expr_str);
				BARF (msg);
			}
		}
		return GNM_VALIDATION_STATUS_VALID;
	}

	case GNM_VALIDATION_TYPE_TEXT_LENGTH:
		/* XL appears to use a very basic value->string mapping that
		 * ignores formatting.
		 * eg len (12/13/01) == len (37238) = 5
		 * This seems wrong for
		 */
		x = g_utf8_strlen (value_peek_string (val), -1);
		break;

	case GNM_VALIDATION_TYPE_CUSTOM: {
		gboolean valid;
		GnmExprTop const *texpr = v->deps[0].texpr;

		if (!texpr)
			return GNM_VALIDATION_STATUS_VALID;

		val = gnm_expr_top_eval (texpr, &ep, GNM_EXPR_EVAL_SCALAR_NON_EMPTY);
		valid = value_get_as_bool (val, NULL);
		value_release (val);

		if (valid)
			return GNM_VALIDATION_STATUS_VALID;
		else {
			GnmParsePos pp;
			char *expr_str = gnm_expr_top_as_string
				(texpr,
				 parse_pos_init_evalpos (&pp, &ep),
				 ep.sheet->convs);
			char *msg = g_strdup_printf (_("%s is not true."), expr_str);
			g_free (expr_str);
			BARF (msg);
		}
	}

	default:
		g_assert_not_reached ();
		return GNM_VALIDATION_STATUS_VALID;
	}

	if (v->op == GNM_VALIDATION_OP_NONE)
		return GNM_VALIDATION_STATUS_VALID;

	nok = 0;
	for (i = 0; i < opinfo[v->op].nops; i++) {
		GnmExprTop const *texpr_i = v->deps[i].texpr;
		GnmExprTop const *texpr;
		GnmValue *cres;

		if (!texpr_i) {
			nok++;
			continue;
		}

		texpr = gnm_expr_top_new
			(gnm_expr_new_binary
			 (gnm_expr_new_constant (value_new_float (x)),
			  opinfo[v->op].ops[i],
			  gnm_expr_copy (texpr_i->expr)));
		cres = gnm_expr_top_eval
			(texpr, &ep, GNM_EXPR_EVAL_SCALAR_NON_EMPTY);
		if (value_get_as_bool (cres, NULL))
			nok++;
		value_release (cres);
		gnm_expr_top_unref (texpr);
	}

	if (nok < opinfo[v->op].ntrue)
		BARF (g_strdup_printf (_("%s is out of permitted range"),
				       value_peek_string (val)));

	return GNM_VALIDATION_STATUS_VALID;
}
Example #16
0
/* ---------------------------------------------------------------------------
 * Read a Targa image from <fp> to <dest>.
 *
 * Returns: TGA_NOERR on success, or a TGAERR_* code on failure.  In the
 *          case of failure, the contents of dest are not guaranteed to be
 *          valid.
 */
tga_result tga_read_from_FILE(tga_image *dest, FILE *fp)
{
    #define BARF(errcode) \
        { tga_free_buffers(dest);  return errcode; }

    #define READ(destptr, size) \
        if (fread(destptr, size, 1, fp) != 1) BARF(TGAERR_EOF)

    #define READ16(dest) \
        { if (fread(&(dest), 2, 1, fp) != 1) BARF(TGAERR_EOF); \
          dest = letoh16(dest); }

    dest->image_id = NULL;
    dest->color_map_data = NULL;
    dest->image_data = NULL;

    READ(&dest->image_id_length,1);
    READ(&dest->color_map_type,1);
    if (dest->color_map_type != TGA_COLOR_MAP_ABSENT &&
        dest->color_map_type != TGA_COLOR_MAP_PRESENT)
            BARF(TGAERR_CMAP_TYPE);

    READ(&dest->image_type, 1);
    if (dest->image_type == TGA_IMAGE_TYPE_NONE)
            BARF(TGAERR_NO_IMG);

    if (dest->image_type != TGA_IMAGE_TYPE_COLORMAP &&
        dest->image_type != TGA_IMAGE_TYPE_BGR &&
        dest->image_type != TGA_IMAGE_TYPE_MONO &&
        dest->image_type != TGA_IMAGE_TYPE_COLORMAP_RLE &&
        dest->image_type != TGA_IMAGE_TYPE_BGR_RLE &&
        dest->image_type != TGA_IMAGE_TYPE_MONO_RLE)
            BARF(TGAERR_IMG_TYPE);

    if (tga_is_colormapped(dest) &&
        dest->color_map_type == TGA_COLOR_MAP_ABSENT)
            BARF(TGAERR_CMAP_MISSING);

    if (!tga_is_colormapped(dest) &&
        dest->color_map_type == TGA_COLOR_MAP_PRESENT)
            BARF(TGAERR_CMAP_PRESENT);

    READ16(dest->color_map_origin);
    READ16(dest->color_map_length);
    READ(&dest->color_map_depth, 1);
    if (dest->color_map_type == TGA_COLOR_MAP_PRESENT)
    {
        if (dest->color_map_length == 0)
            BARF(TGAERR_CMAP_LENGTH);

        if (!UNMAP_DEPTH(dest->color_map_depth))
            BARF(TGAERR_CMAP_DEPTH);
    }

    READ16(dest->origin_x);
    READ16(dest->origin_y);
    READ16(dest->width);
    READ16(dest->height);

    if (dest->width == 0 || dest->height == 0)
            BARF(TGAERR_ZERO_SIZE);

    READ(&dest->pixel_depth, 1);
    if (!SANE_DEPTH(dest->pixel_depth) ||
       (dest->pixel_depth != 8 && tga_is_colormapped(dest)) )
            BARF(TGAERR_PIXEL_DEPTH);

    READ(&dest->image_descriptor, 1);

    if (dest->image_id_length > 0)
    {
        dest->image_id = (uint8_t*)malloc(dest->image_id_length);
        if (dest->image_id == NULL) BARF(TGAERR_NO_MEM);
        READ(dest->image_id, dest->image_id_length);
    }

    if (dest->color_map_type == TGA_COLOR_MAP_PRESENT)
    {
        dest->color_map_data = (uint8_t*)malloc(
            (dest->color_map_origin + dest->color_map_length) *
            dest->color_map_depth / 8);
        if (dest->color_map_data == NULL) BARF(TGAERR_NO_MEM);
        READ(dest->color_map_data +
            (dest->color_map_origin * dest->color_map_depth / 8),
            dest->color_map_length * dest->color_map_depth / 8);
    }

    dest->image_data = (uint8_t*) malloc(
        dest->width * dest->height * dest->pixel_depth / 8);
    if (dest->image_data == NULL)
            BARF(TGAERR_NO_MEM);

    if (tga_is_rle(dest))
    {
        /* read RLE */
        tga_result result = tga_read_rle(dest, fp);
        if (result != TGA_NOERR) BARF(result);
    }
    else
    {
        /* uncompressed */
        READ(dest->image_data,
            dest->width * dest->height * dest->pixel_depth / 8);
    }

    return TGA_NOERR;
    #undef BARF
    #undef READ
    #undef READ16
}
Example #17
0
char *
of_handler(char *keyword, char *arg)
{
	struct ofiocdesc ofio;
	const struct extabent *ex;
	char ofio_buf[BUFSIZE];
	int fd, optnode;

	if ((fd = open(path_openfirm, arg ? O_RDWR : O_RDONLY, 0640)) < 0)
		BARF(path_openfirm, strerror(errno));

	/* Check to see if it's a special-case keyword. */
	for (ex = ofextab; ex->ex_keyword != NULL; ++ex)
		if (strcmp(ex->ex_keyword, keyword) == 0)
			break;

	if (ioctl(fd, OFIOCGETOPTNODE, (char *)&optnode) < 0) {
		(void)close(fd);
		BARF("OFIOCGETOPTNODE", strerror(errno));
	}

	memset(&ofio_buf[0], 0, sizeof(ofio_buf));
	memset(&ofio, 0, sizeof(ofio));
	ofio.of_nodeid = optnode;
	ofio.of_name = keyword;
	ofio.of_namelen = strlen(ofio.of_name);

	if (arg) {
		if (verbose) {
			printf("old: ");

			ofio.of_buf = &ofio_buf[0];
			ofio.of_buflen = sizeof(ofio_buf);
			if (ioctl(fd, OFIOCGET, (char *)&ofio) < 0) {
				(void)close(fd);
				BARF("OFIOCGET", strerror(errno));
			}

			if (ofio.of_buflen <= 0) {
				printf("nothing available for %s\n", keyword);
				goto out;
			}

			if (ex->ex_keyword != NULL)
				(*ex->ex_handler)(ex, &ofio, NULL);
			else
				printf("%s\n", ofio.of_buf);
		}
 out:
		if (ex->ex_keyword != NULL)
			(*ex->ex_handler)(ex, &ofio, arg);
		else {
			ofio.of_buf = arg;
			ofio.of_buflen = strlen(arg);
		}

		if (ioctl(fd, OFIOCSET, (char *)&ofio) < 0) {
			(void)close(fd);
			BARF("invalid keyword", keyword);
		}

		if (verbose) {
			printf("new: ");
			if (ex->ex_keyword != NULL)
				(*ex->ex_handler)(ex, &ofio, NULL);
			else
				printf("%s\n", ofio.of_buf);
		}
	} else {
		ofio.of_buf = &ofio_buf[0];
		ofio.of_buflen = sizeof(ofio_buf);
		if (ioctl(fd, OFIOCGET, (char *)&ofio) < 0) {
			(void)close(fd);
			BARF("OFIOCGET", strerror(errno));
		}

		if (ofio.of_buflen <= 0) {
			(void)snprintf(err_str, sizeof err_str,
			    "nothing available for %s", keyword);
			return (err_str);
		}

		if (ex->ex_keyword != NULL)
			(*ex->ex_handler)(ex, &ofio, NULL);
		else
			printf("%s=%s\n", keyword, ofio.of_buf);
	}

	(void)close(fd);
	return (NULL);
}
Example #18
0
/*
 * The main routine.  Mostly validate cmdline params, open files, run the KDF,
 * and do the crypt.
 */
int main(int argc, char *argv[]) {
   unsigned char salt[SALT_LENGTH];
   FILE *infd = NULL, *outfd = NULL;
   int encrypt = -1;
   int hash = -1;
   int ret;
   unsigned char keyiv[KEY_LENGTH + IV_LENGTH];
   unsigned long keyivlen = (KEY_LENGTH + IV_LENGTH);
   unsigned char *key, *iv;

   /* Check proper number of cmdline args */
   if(argc < 5 || argc > 6)
      BARF("Invalid number of arguments");

   /* Check proper mode of operation */
   if     (!strncmp(argv[1], "enc", 3))
      encrypt = 1;
   else if(!strncmp(argv[1], "dec", 3))
      encrypt = 0;
   else
      BARF("Bad command name");

   /* Check we can open infile/outfile */
   infd = fopen(argv[2], "rb");
   if(infd == NULL)
      BARF("Could not open infile");
   outfd = fopen(argv[3], "wb");
   if(outfd == NULL)
      BARF("Could not open outfile");

   /* Get the salt from wherever */
   if(argc == 6) {
      /* User-provided */
      if(parse_hex_salt((unsigned char*) argv[5], salt) != CRYPT_OK)
         BARF("Bad user-specified salt");
   } else if(!strncmp(argv[1], "enc", 3)) {
      /* Encrypting; get from RNG */
      if(rng_get_bytes(salt, sizeof(salt), NULL) != sizeof(salt))
         BARF("Not enough random data");
   } else {
      /* Parse from infile (decrypt only) */
      if(parse_openssl_header(infd, salt) != CRYPT_OK)
         BARF("Invalid OpenSSL header in infile");
   }

   /* Fetch the MD5 hasher for PKCS#5 */
   hash = register_hash(&md5_desc);
   if(hash == -1)
      BARF("Could not register MD5 hash");

   /* Set things to a sane initial state */
   zeromem(keyiv, sizeof(keyiv));
   key = keyiv + 0;      /* key comes first */
   iv = keyiv + KEY_LENGTH;   /* iv comes next */

   /* Run the key derivation from the provided passphrase.  This gets us
      the key and iv. */
   ret = pkcs_5_alg1_openssl((unsigned char*)argv[4], strlen(argv[4]), salt,
                             OPENSSL_ITERATIONS, hash, keyiv, &keyivlen );
   if(ret != CRYPT_OK)
      BARF("Could not derive key/iv from passphrase");

   /* Display the salt/key/iv like OpenSSL cmdline does when -p */
   printf("salt="); dump_bytes(salt, sizeof(salt)); printf("\n");
   printf("key=");  dump_bytes(key, KEY_LENGTH);    printf("\n");
   printf("iv =");  dump_bytes(iv,  IV_LENGTH );    printf("\n");

   /* If we're encrypting, write the salt header as OpenSSL does */
   if(!strncmp(argv[1], "enc", 3)) {
      if(fwrite(salt_header, 1, sizeof(salt_header), outfd) !=
         sizeof(salt_header) )
         BARF("Error writing salt header to outfile");
      if(fwrite(salt, 1, sizeof(salt), outfd) != sizeof(salt))
         BARF("Error writing salt to outfile");
   }

   /* At this point, the files are open, the salt has been figured out,
      and we're ready to pump data through crypt. */

   /* Do the crypt operation */
   if(do_crypt(infd, outfd, key, iv, encrypt) != CRYPT_OK)
      BARF("Error during crypt operation");

   /* Clean up */
   fclose(infd); fclose(outfd);
   return 0;
}