Exemple #1
0
void keymap_actuate(const uint8_t row, const uint8_t col, const int16_t idle_time)
{
#ifdef SIMPLE_DEVICE
	const uint8_t code = getmap(row,col);
#else
	const uint8_t code = translate_code(getmap(row,col));
#endif /* SIMPLE_DEVICE */
	const uint8_t action = getaction(row,col);
	const uint8_t tapkey = gettapkey(row,col);
	
#ifdef KEYMAP_MEMORY_SAVE
	g_matrixlayer[row][col] = g_layer_select;
#else
	g_matrixcode[row][col] = code;
	g_matrixaction[row][col] = action;
	g_matrixtapkey[row][col] = tapkey;
#endif /* KEYMAP_MEMORY_SAVE */
	
	doubletap_down(row,col,idle_time);
	
	handle_code_actuate(code, action, tapkey);

#ifdef MAX_NUMBER_OF_BACKLIGHTS
	backlight_react();
#endif /* MAX_NUMBER_OF_BACKLIGHTS */

	USB_wakeup();
}
Exemple #2
0
bool CffPlayer::cff_unpacker::startup(const std::vector<std::vector<uint8_t>>& dictionary, std::vector<uint8_t>& obuf, std::vector<uint8_t>::const_iterator& it)
{
    auto oldCode = get_code(it);

    translate_code(oldCode, m_theString, dictionary);

    for(int i = 0; i < m_theString[0]; i++)
        obuf.emplace_back( m_theString[i + 1] );

    return true;
}
Exemple #3
0
int CcffLoader::cff_unpacker::startup()
{
  old_code = get_code();

  translate_code(old_code,the_string);

  if(output_length + the_string[0] > 0x10000) {
    output_length = 0;
    return 0;
  }

  for (int i=0;i<the_string[0];i++)
    output[output_length++] = the_string[i+1];

  return 1;
}
Exemple #4
0
/* Generate C source code for the parser */
void ReportTable(struct lmno *lmnop, int mhflag)
{
	FILE *out, *in;
	char line[LINESIZE];
	int  lineno;
	struct state *stp;
	struct action *ap;
	struct rule *rp;
	struct acttab *pActtab;
	int i, j, n;
	int mnTknOfst, mxTknOfst;
	int mnNtOfst, mxNtOfst;
	struct axset *ax;

	// open input template and target output file and advance to first insertion
	in = tplt_open(lmnop);
	if(in == 0)
		return;
	out = (lmnop->export_c ?
			file_open(lmnop,".c","w") :
			file_open(lmnop,".cpp","w") );
	if(out == 0)
	{
		fclose(in);
		return;
	}
	lineno = 1;
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	// insert user's copyright and license information.
	copyright_print(out, lmnop, &lineno);
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Generate the action table and its associates:
	 **
	 **  yy_action[]		A single table containing all actions.
	 **  yy_lookahead[]	 A table containing the lookahead for each entry in
	 **					 yy_action.  Used to detect hash collisions.
	 **  yy_shift_ofst[]	For each state, the offset into yy_action for
	 **					 shifting terminals.
	 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
	 **					 shifting non-terminals after a reduce.
	 **  yy_default[]	   Default action for each state.
	 */
	
	/* Compute the actions on all states and count them up */
	ax = malloc(sizeof(ax[0])*lmnop->nstate*2);
	if(ax==0)
	{
		fprintf(stderr,"malloc failed\n");
		exit(1);
	}
	for(i=0; i<lmnop->nstate; i++)
	{
		stp = lmnop->sorted[i];
		stp->nTknAct = stp->nNtAct = 0;
		stp->iDflt = lmnop->nstate + lmnop->nrule;
		stp->iTknOfst = NO_OFFSET;
		stp->iNtOfst = NO_OFFSET;
		for(ap=stp->ap; ap; ap=ap->next)
		{
			if(compute_action(lmnop,ap)>=0)
			{
				if(ap->sp->index<lmnop->nterminal)
				{
					stp->nTknAct++;
				}
				else if(ap->sp->index<lmnop->nsymbol)
				{
					stp->nNtAct++;
				}
				else
				{
					stp->iDflt = compute_action(lmnop, ap);
				}
			}
		}
		ax[i*2].stp = stp;
		ax[i*2].isTkn = 1;
		ax[i*2].nAction = stp->nTknAct;
		ax[i*2+1].stp = stp;
		ax[i*2+1].isTkn = 0;
		ax[i*2+1].nAction = stp->nNtAct;
	}
	mxTknOfst = mnTknOfst = 0;
	mxNtOfst = mnNtOfst = 0;
	
	/* Compute the action table.  In order to try to keep the size of the
	 ** action table to a minimum, the heuristic of placing the largest action
	 ** sets first is used.
	 */
	qsort(ax, lmnop->nstate*2, sizeof(ax[0]), axset_compare);
	pActtab = acttab_alloc();
	for(i=0; i<lmnop->nstate*2 && ax[i].nAction>0; i++)
	{
		stp = ax[i].stp;
		if(ax[i].isTkn)
		{
			for(ap=stp->ap; ap; ap=ap->next)
			{
				int action;
				if(ap->sp->index>=lmnop->nterminal)
					continue;
				action = compute_action(lmnop, ap);
				if(action<0)
					continue;
				acttab_action(pActtab, ap->sp->index, action);
			}
			stp->iTknOfst = acttab_insert(pActtab);
			if(stp->iTknOfst<mnTknOfst)
				mnTknOfst = stp->iTknOfst;
			if(stp->iTknOfst>mxTknOfst)
				mxTknOfst = stp->iTknOfst;
		}
		else
		{
			for(ap=stp->ap; ap; ap=ap->next)
			{
				int action;
				if(ap->sp->index<lmnop->nterminal)
					continue;
				if(ap->sp->index==lmnop->nsymbol)
					continue;
				action = compute_action(lmnop, ap);
				if(action<0)
					continue;
				acttab_action(pActtab, ap->sp->index, action);
			}
			stp->iNtOfst = acttab_insert(pActtab);
			if(stp->iNtOfst<mnNtOfst)
				mnNtOfst = stp->iNtOfst;
			if(stp->iNtOfst>mxNtOfst)
				mxNtOfst = stp->iNtOfst;
		}
	}
	free(ax);
	
	/* Output the yy_action table */
	fprintf(out,"static YYACTIONTYPE yy_action[] = {\n");
	lineno++;
	n = acttab_size(pActtab);
	for(i=j=0; i<n; i++)
	{
		int action = acttab_yyaction(pActtab, i);
		if(action<0)
			action = lmnop->nsymbol + lmnop->nrule + 2;
		if(j==0)
			fprintf(out," /* %5d */ ", i);
		fprintf(out, " %4d,", action);
		if(j==9 || i==n-1)
		{
			fprintf(out, "\n");
			lineno++;
			j = 0;
		}
		else
		{
			j++;
		}
	}
	fprintf(out, "};\n"); lineno++;
	
	/* Output the yy_lookahead table */
	fprintf(out,"static YYCODETYPE yy_lookahead[] = {\n");
	lineno++;
	for(i=j=0; i<n; i++)
	{
		int la = acttab_yylookahead(pActtab, i);
		if(la<0)
			la = lmnop->nsymbol;
		if(j==0)
			fprintf(out," /* %5d */ ", i);
		fprintf(out, " %4d,", la);
		if(j==9 || i==n-1)
		{
			fprintf(out, "\n");
			lineno++;
			j = 0;
		}
		else
		{
			j++;
		}
	}
	fprintf(out, "};\n");
	lineno++;
	
	/* Output the yy_shift_ofst[] table */
	fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1);
	lineno++;
	fprintf(out, "static %s yy_shift_ofst[] = {\n", 
			minimum_size_type(mnTknOfst-1, mxTknOfst));
	lineno++;
	n = lmnop->nstate;
	for(i=j=0; i<n; i++)
	{
		int ofst;
		stp = lmnop->sorted[i];
		ofst = stp->iTknOfst;
		if(ofst==NO_OFFSET)
			ofst = mnTknOfst - 1;
		if(j==0)
			fprintf(out," /* %5d */ ", i);
		fprintf(out, " %4d,", ofst);
		if(j==9 || i==n-1)
		{
			fprintf(out, "\n");
			lineno++;
			j = 0;
		}
		else
		{
			j++;
		}
	}
	fprintf(out, "};\n"); lineno++;
	
	/* Output the yy_reduce_ofst[] table */
	fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1);
	lineno++;
	fprintf(out, "static %s yy_reduce_ofst[] = {\n", 
			minimum_size_type(mnNtOfst-1, mxNtOfst));
	lineno++;
	n = lmnop->nstate;
	for(i=j=0; i<n; i++)
	{
		int ofst;
		stp = lmnop->sorted[i];
		ofst = stp->iNtOfst;
		if(ofst==NO_OFFSET)
			ofst = mnNtOfst - 1;
		if(j==0)
			fprintf(out," /* %5d */ ", i);
		fprintf(out, " %4d,", ofst);
		if(j==9 || i==n-1)
		{
			fprintf(out, "\n");
			lineno++;
			j = 0;
		}
		else
		{
			j++;
		}
	}
	fprintf(out, "};\n"); lineno++;
	
	/* Output the default action table */
	fprintf(out, "static YYACTIONTYPE yy_default[] = {\n");
	lineno++;
	n = lmnop->nstate;
	for(i=j=0; i<n; i++)
	{
		stp = lmnop->sorted[i];
		if(j==0)
			fprintf(out," /* %5d */ ", i);
		fprintf(out, " %4d,", stp->iDflt);
		if(j==9 || i==n-1)
		{
			fprintf(out, "\n");
			lineno++;
			j = 0;
		}
		else
		{
			j++;
		}
	}
	fprintf(out, "};\n");
	lineno++;
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Generate the table of fallback tokens.
	 */
	if(lmnop->has_fallback)
	{
		for(i=0; i<lmnop->nterminal; i++)
		{
			struct symbol *p = lmnop->symbols[i];
			if(p->fallback==0)
			{
				fprintf(out, "	0,  /* %10s => nothing */\n", p->name);
			}
			else
			{
				fprintf(out, "  %3d,  /* %10s => %s */\n", p->fallback->index,
						p->name, p->fallback->name);
			}
			lineno++;
		}
	}
	tplt_xfer(lmnop->name, in, out, &lineno);
	
	/* Generate a table containing the symbolic name of every symbol
	 */
	for(i=0; i<lmnop->nsymbol; i++)
	{
		sprintf(line,"\"%s\",",lmnop->symbols[i]->name);
		fprintf(out,"  %-15s",line);
		if((i&3)==3)
		{
			fprintf(out,"\n");
			lineno++;
		}
	}
	if((i&3)!=0)
	{
		fprintf(out,"\n");
		lineno++;
	}
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Generate a table containing a text string that describes every
	 ** rule in the rule set of the grammer.  This information is used
	 ** when tracing REDUCE actions.
	 */
	for(i=0, rp=lmnop->rule; rp; rp=rp->next, i++)
	{
		assert(rp->index==i);
		fprintf(out," /* %3d */ \"%s ->", i, rp->lhs->name);
		for(j=0; j<rp->nrhs; j++)
			fprintf(out," %s",rp->rhs[j]->name);
		fprintf(out,"\",\n");
		lineno++;
	}
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Generate code which executes every time a symbol is popped from
	 ** the stack while processing errors or while destroying the parser. 
	 ** (In other words, generate the %destructor actions)
	 */
	if(lmnop->tokendest)
	{
		for(i=0; i<lmnop->nsymbol; i++)
		{
			struct symbol *sp = lmnop->symbols[i];
			if(sp==0 || sp->type!=TERMINAL) continue;
			fprintf(out,"	case %d:\n",sp->index);
			lineno++;
		}
		for(i=0; i<lmnop->nsymbol && lmnop->symbols[i]->type!=TERMINAL; i++) { }
		if(i<lmnop->nsymbol)
		{
			emit_destructor_code(out,lmnop->symbols[i],lmnop,&lineno);
			fprintf(out,"	  break;\n");
			lineno++;
		}
	}
	for(i=0; i<lmnop->nsymbol; i++)
	{
		struct symbol *sp = lmnop->symbols[i];
		if(sp==0 || sp->type==TERMINAL || sp->destructor==0)
			continue;
		fprintf(out,"	case %d:\n",sp->index);
		lineno++;
		
		/* Combine duplicate destructors into a single case */
		for(j=i+1; j<lmnop->nsymbol; j++)
		{
			struct symbol *sp2 = lmnop->symbols[j];
			if(sp2 && sp2->type!=TERMINAL && sp2->destructor &&
				sp2->dtnum==sp->dtnum &&
				strcmp(sp->destructor,sp2->destructor)==0)
			{
				fprintf(out,"	case %d:\n",sp2->index);
				lineno++;
				sp2->destructor = 0;
			}
		}
		
		emit_destructor_code(out,lmnop->symbols[i],lmnop,&lineno);
		fprintf(out,"	  break;\n");
		lineno++;
	}
	if(lmnop->vardest)
	{
		struct symbol *dflt_sp = 0;
		for(i=0; i<lmnop->nsymbol; i++)
		{
			struct symbol *sp = lmnop->symbols[i];
			if(sp==0 || sp->type==TERMINAL ||
				sp->index<=0 || sp->destructor!=0)
				continue;
			fprintf(out,"	case %d:\n",sp->index);
			lineno++;
			dflt_sp = sp;
		}
		if(dflt_sp!=0)
		{
			emit_destructor_code(out,dflt_sp,lmnop,&lineno);
			fprintf(out,"	  break;\n");
			lineno++;
		}
	}
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Generate code which executes whenever the parser stack overflows */
	if(lmnop->export_c)
	{
		tplt_print(out,lmnop,lmnop->overflow,lmnop->overflowln,&lineno);
		tplt_xfer(lmnop->name,in,out,&lineno);
	}
	
	/* Generate the table of rule information 
	 **
	 ** Note: This code depends on the fact that rules are number
	 ** sequentually beginning with 0.
	 */
	for(rp=lmnop->rule; rp; rp=rp->next)
	{
		fprintf(out,"  { %d, %d },\n",rp->lhs->index,rp->nrhs);
		lineno++;
	}
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Generate code which execution during each REDUCE action */
	for(rp=lmnop->rule; rp; rp=rp->next)
	{
		if(rp->code)
			translate_code(lmnop, rp);
	}
	for(rp=lmnop->rule; rp; rp=rp->next)
	{
		struct rule *rp2;
		if(rp->code==0)
			continue;
		fprintf(out,"	  case %d:\n",rp->index);
		lineno++;
		for(rp2=rp->next; rp2; rp2=rp2->next)
		{
			if(rp2->code==rp->code)
			{
				fprintf(out,"	  case %d:\n",rp2->index);
				lineno++;
				rp2->code = 0;
			}
		}
		emit_code(out,rp,lmnop,&lineno);
		fprintf(out,"		break;\n");
		lineno++;
	}
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Generate code which executes if a parse fails */
	tplt_print(out,lmnop,lmnop->failure,lmnop->failureln,&lineno);
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Generate code which executes when a syntax error occurs */
	tplt_print(out,lmnop,lmnop->error,lmnop->errorln,&lineno);
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Generate code which executes when the parser accepts its input */
	tplt_print(out,lmnop,lmnop->accept,lmnop->acceptln,&lineno);
	tplt_xfer(lmnop->name,in,out,&lineno);
	
	/* Append any addition code the user desires */
	tplt_print(out,lmnop,lmnop->extracode,lmnop->extracodeln,&lineno);
	
	fclose(in);
	fclose(out);
	return;
}
Exemple #5
0
/*
  Lempel-Ziv-Tyr ;-)
*/
long CcffLoader::cff_unpacker::unpack(unsigned char *ibuf, unsigned char *obuf)
{
  if (memcmp(ibuf,"YsComp""\x07""CUD1997""\x1A\x04",16))
    return 0;

  input = ibuf + 16;
  output = obuf;

  output_length = 0;

  heap = (unsigned char *)malloc(0x10000);
  dictionary = (unsigned char **)malloc(sizeof(unsigned char *)*0x8000);

  memset(heap,0,0x10000);
  memset(dictionary,0,0x8000);

  cleanup();
  if(!startup())
    goto out;

  // LZW
  while (1)
    {
      new_code = get_code();

      // 0x00: end of data
      if (new_code == 0)
	break;

      // 0x01: end of block
      if (new_code == 1)
	{
	  cleanup();
	  if(!startup())
	    goto out;

	  continue;
	}

      // 0x02: expand code length
      if (new_code == 2)
	{
	  code_length++;

	  continue;
	}

      // 0x03: RLE
      if (new_code == 3)
	{
	  unsigned char old_code_length = code_length;

	  code_length = 2;

	  unsigned char repeat_length = get_code() + 1;

	  code_length = 4 << get_code();

	  unsigned long repeat_counter = get_code();

	  if(output_length + repeat_counter * repeat_length > 0x10000) {
	    output_length = 0;
	    goto out;
	  }

	  for (unsigned int i=0;i<repeat_counter*repeat_length;i++)
	    output[output_length++] = output[output_length - repeat_length];

	  code_length = old_code_length;

	  if(!startup())
	    goto out;

	  continue;
	}

      if (new_code >= (0x104 + dictionary_length))
	{
	  // dictionary <- old.code.string + old.code.char
	  the_string[++the_string[0]] = the_string[1];
	}
      else
	{
	  // dictionary <- old.code.string + new.code.char
	  unsigned char temp_string[256];

	  translate_code(new_code,temp_string);

	  the_string[++the_string[0]] = temp_string[1];
	}

      expand_dictionary(the_string);

      // output <- new.code.string
      translate_code(new_code,the_string);

      if(output_length + the_string[0] > 0x10000) {
	output_length = 0;
	goto out;
      }

      for (int i=0;i<the_string[0];i++)
	output[output_length++] = the_string[i+1];

      old_code = new_code;
    }

 out:
  free(heap);
  free(dictionary);
  return output_length;
}
Exemple #6
0
/*
  Lempel-Ziv-Tyr ;-)
*/
void CffPlayer::cff_unpacker::unpack(const std::vector<uint8_t>& ibuf, std::vector<uint8_t>& obuf)
{
    obuf.clear();

    if(memcmp(ibuf.data(), "YsComp"
              "\x07"
              "CUD1997"
              "\x1A\x04",
              16))
        return;

    auto it = std::next(ibuf.begin(), 16);

    std::vector<std::vector<uint8_t>> dictionary;
    cleanup();
    if(!startup(dictionary, obuf, it))
        return;

    // LZW
    while(auto newCode = get_code(it))
    {
        // 0x01: end of block
        if(newCode == 1)
        {
            cleanup();
            dictionary.clear();
            if(!startup(dictionary, obuf, it))
                return;

            continue;
        }

        // 0x02: expand code length
        if(newCode == 2)
        {
            m_codeLength++;

            continue;
        }

        // 0x03: RLE
        if(newCode == 3)
        {
            uint8_t old_code_length = m_codeLength;

            m_codeLength = 2;

            uint8_t repeat_length = get_code(it) + 1;

            m_codeLength = 4 << get_code(it);

            unsigned long repeat_counter = get_code(it);

            if(obuf.size() + repeat_counter * repeat_length)
            {
                obuf.clear();
                return;
            }

            for(auto i = 0; i < repeat_counter * repeat_length; i++)
            {
                obuf.emplace_back(obuf[obuf.size() - repeat_length]);
            }

            m_codeLength = old_code_length;

            if(!startup(dictionary, obuf, it))
                return;

            continue;
        }

        if(newCode >= (0x104 + dictionary.size()))
        {
            // dictionary <- old.code.string + old.code.char
            m_theString[++m_theString[0]] = m_theString[1];
        }
        else
        {
            // dictionary <- old.code.string + new.code.char
            uint8_t temp_string[256];

            translate_code(newCode, temp_string, dictionary);

            m_theString[++m_theString[0]] = temp_string[1];
        }

        expand_dictionary(m_theString, dictionary);

        // output <- new.code.string
        translate_code(newCode, m_theString, dictionary);

        for(int i = 0; i < m_theString[0]; i++)
            obuf.emplace_back(m_theString[i + 1]);
    }
}